Whamcloud - gitweb
8a689afbd22007a97ded0549c5d4d72f8c5aa907
[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: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19 is_sles11()                                             # LU-4341
20 {
21         if [ -r /etc/SuSE-release ]
22         then
23                 local vers=$(grep VERSION /etc/SuSE-release | awk '{print $3}')
24                 local patchlev=$(grep PATCHLEVEL /etc/SuSE-release |
25                         awk '{ print $3 }')
26                 if [ $vers -eq 11 ] && [ $patchlev -ge 3 ]; then
27                         return 0
28                 fi
29         fi
30         return 1
31 }
32
33 if is_sles11; then                                      # LU-4341
34         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 170"
35 fi
36
37 SRCDIR=$(cd $(dirname $0); echo $PWD)
38 export PATH=$PATH:/sbin
39
40 TMP=${TMP:-/tmp}
41
42 CC=${CC:-cc}
43 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
44 CREATETEST=${CREATETEST:-createtest}
45 LFS=${LFS:-lfs}
46 LFIND=${LFIND:-"$LFS find"}
47 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
48 LCTL=${LCTL:-lctl}
49 OPENFILE=${OPENFILE:-openfile}
50 OPENUNLINK=${OPENUNLINK:-openunlink}
51 export MULTIOP=${MULTIOP:-multiop}
52 READS=${READS:-"reads"}
53 MUNLINK=${MUNLINK:-munlink}
54 SOCKETSERVER=${SOCKETSERVER:-socketserver}
55 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
56 MEMHOG=${MEMHOG:-memhog}
57 DIRECTIO=${DIRECTIO:-directio}
58 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
59 STRIPES_PER_OBJ=-1
60 CHECK_GRANT=${CHECK_GRANT:-"yes"}
61 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
62 export PARALLEL=${PARALLEL:-"no"}
63
64 export NAME=${NAME:-local}
65
66 SAVE_PWD=$PWD
67
68 CLEANUP=${CLEANUP:-:}
69 SETUP=${SETUP:-:}
70 TRACE=${TRACE:-""}
71 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
72 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
73 . $LUSTRE/tests/test-framework.sh
74 init_test_env $@
75 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
76 init_logging
77
78 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24D 27m 64b 68 71 77f 78 115 124b 300o"
79
80 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
81         # bug number for skipped test: LU-4536 LU-1957 LU-2805
82         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  65ic    180     184c"
83         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b 51ba"
84 fi
85
86 FAIL_ON_ERROR=false
87
88 cleanup() {
89         echo -n "cln.."
90         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
91         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
92 }
93 setup() {
94         echo -n "mnt.."
95         load_modules
96         setupall || exit 10
97         echo "done"
98 }
99
100 check_swap_layouts_support()
101 {
102         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
103                 { skip "Does not support layout lock."; return 0; }
104         return 1
105 }
106
107 check_and_setup_lustre
108
109 DIR=${DIR:-$MOUNT}
110 assert_DIR
111
112 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
113         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
114 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
115 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
116 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
117 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
118 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
119 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
120
121 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
122 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
123 rm -rf $DIR/[Rdfs][0-9]*
124
125 # $RUNAS_ID may get set incorrectly somewhere else
126 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
127
128 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
129
130 build_test_filter
131
132 if [ "${ONLY}" = "MOUNT" ] ; then
133         echo "Lustre is up, please go on"
134         exit
135 fi
136
137 echo "preparing for tests involving mounts"
138 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
139 touch $EXT2_DEV
140 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
141 echo # add a newline after mke2fs.
142
143 umask 077
144
145 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
146 lctl set_param debug=-1 2> /dev/null || true
147 test_0a() {
148         touch $DIR/$tfile
149         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
150         rm $DIR/$tfile
151         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
152 }
153 run_test 0a "touch; rm ====================="
154
155 test_0b() {
156         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
157         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
158 }
159 run_test 0b "chmod 0755 $DIR ============================="
160
161 test_0c() {
162         $LCTL get_param mdc.*.import | grep "state: FULL" ||
163                 error "import not FULL"
164         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
165                 error "bad target"
166 }
167 run_test 0c "check import proc ============================="
168
169 test_1() {
170         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
171         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
172         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
173         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
174         rmdir $DIR/$tdir/d2
175         rmdir $DIR/$tdir
176         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
177 }
178 run_test 1 "mkdir; remkdir; rmdir =============================="
179
180 test_2() {
181         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
182         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
183         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
184         rm -r $DIR/$tdir
185         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
186 }
187 run_test 2 "mkdir; touch; rmdir; check file ===================="
188
189 test_3() {
190         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
191         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
192         touch $DIR/$tdir/$tfile
193         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
194         rm -r $DIR/$tdir
195         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
196 }
197 run_test 3 "mkdir; touch; rmdir; check dir ====================="
198
199 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
200 test_4() {
201         local MDTIDX=1
202
203         test_mkdir $DIR/$tdir ||
204                 error "Create remote directory failed"
205
206         touch $DIR/$tdir/$tfile ||
207                 error "Create file under remote directory failed"
208
209         rmdir $DIR/$tdir &&
210                 error "Expect error removing in-use dir $DIR/$tdir"
211
212         test -d $DIR/$tdir || error "Remote directory disappeared"
213
214         rm -rf $DIR/$tdir || error "remove remote dir error"
215 }
216 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
217
218 test_5() {
219         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
220         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
221         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
222         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
223         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
224 }
225 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
226
227 test_6a() {
228         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
229         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
230         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
231                 error "$tfile does not have perm 0666 or UID $UID"
232         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
233         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
234                 error "$tfile should be 0666 and owned by UID $UID"
235 }
236 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
237
238 test_6c() {
239         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
240         touch $DIR/$tfile
241         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
242         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
243                 error "$tfile should be owned by UID $RUNAS_ID"
244         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
245         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
246                 error "$tfile should be owned by UID $RUNAS_ID"
247 }
248 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
249
250 test_6e() {
251         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
252         touch $DIR/$tfile
253         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
254         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
255                 error "$tfile should be owned by GID $UID"
256         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
257         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
258                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
259 }
260 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
261
262 test_6g() {
263         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
264         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
265         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
266         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
267         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
268         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
269         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
270                 error "$tdir/d/subdir should be GID $RUNAS_GID"
271 }
272 run_test 6g "Is new dir in sgid dir inheriting group?"
273
274 test_6h() { # bug 7331
275         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
276         touch $DIR/$tfile || error "touch failed"
277         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
278         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
279                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
280         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
281                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
282 }
283 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
284
285 test_7a() {
286         test_mkdir $DIR/$tdir
287         $MCREATE $DIR/$tdir/$tfile
288         chmod 0666 $DIR/$tdir/$tfile
289         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
290                 error "$tdir/$tfile should be mode 0666"
291 }
292 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
293
294 test_7b() {
295         if [ ! -d $DIR/$tdir ]; then
296                 test_mkdir $DIR/$tdir
297         fi
298         $MCREATE $DIR/$tdir/$tfile
299         echo -n foo > $DIR/$tdir/$tfile
300         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
301         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
302 }
303 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
304
305 test_8() {
306         test_mkdir $DIR/$tdir
307         touch $DIR/$tdir/$tfile
308         chmod 0666 $DIR/$tdir/$tfile
309         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
310                 error "$tfile mode not 0666"
311 }
312 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
313
314 test_9() {
315         test_mkdir $DIR/$tdir
316         test_mkdir $DIR/$tdir/d2
317         test_mkdir $DIR/$tdir/d2/d3
318         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
319 }
320 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
321
322 test_10() {
323         test_mkdir $DIR/$tdir
324         test_mkdir $DIR/$tdir/d2
325         touch $DIR/$tdir/d2/$tfile
326         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
327                 error "$tdir/d2/$tfile not a file"
328 }
329 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
330
331 test_11() {
332         test_mkdir $DIR/$tdir
333         test_mkdir $DIR/$tdir/d2
334         chmod 0666 $DIR/$tdir/d2
335         chmod 0705 $DIR/$tdir/d2
336         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
337                 error "$tdir/d2 mode not 0705"
338 }
339 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
340
341 test_12() {
342         test_mkdir $DIR/$tdir
343         touch $DIR/$tdir/$tfile
344         chmod 0666 $DIR/$tdir/$tfile
345         chmod 0654 $DIR/$tdir/$tfile
346         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
347                 error "$tdir/d2 mode not 0654"
348 }
349 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
350
351 test_13() {
352         test_mkdir $DIR/$tdir
353         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
354         >  $DIR/$tdir/$tfile
355         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
356                 error "$tdir/$tfile size not 0 after truncate"
357 }
358 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
359
360 test_14() {
361         test_mkdir $DIR/$tdir
362         touch $DIR/$tdir/$tfile
363         rm $DIR/$tdir/$tfile
364         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
365 }
366 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
367
368 test_15() {
369         test_mkdir $DIR/$tdir
370         touch $DIR/$tdir/$tfile
371         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
372         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
373                 error "$tdir/${tfile_2} not a file after rename"
374         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
375 }
376 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
377
378 test_16() {
379         test_mkdir $DIR/$tdir
380         touch $DIR/$tdir/$tfile
381         rm -rf $DIR/$tdir/$tfile
382         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
383 }
384 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
385
386 test_17a() {
387         test_mkdir -p $DIR/$tdir
388         touch $DIR/$tdir/$tfile
389         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
390         ls -l $DIR/$tdir
391         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
392                 error "$tdir/l-exist not a symlink"
393         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
394                 error "$tdir/l-exist not referencing a file"
395         rm -f $DIR/$tdir/l-exist
396         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
397 }
398 run_test 17a "symlinks: create, remove (real) =================="
399
400 test_17b() {
401         test_mkdir -p $DIR/$tdir
402         ln -s no-such-file $DIR/$tdir/l-dangle
403         ls -l $DIR/$tdir
404         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
405                 error "$tdir/l-dangle not referencing no-such-file"
406         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
407                 error "$tdir/l-dangle not referencing non-existent file"
408         rm -f $DIR/$tdir/l-dangle
409         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
410 }
411 run_test 17b "symlinks: create, remove (dangling) =============="
412
413 test_17c() { # bug 3440 - don't save failed open RPC for replay
414         test_mkdir -p $DIR/$tdir
415         ln -s foo $DIR/$tdir/$tfile
416         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
417 }
418 run_test 17c "symlinks: open dangling (should return error) ===="
419
420 test_17d() {
421         test_mkdir -p $DIR/$tdir
422         ln -s foo $DIR/$tdir/$tfile
423         touch $DIR/$tdir/$tfile || error "creating to new symlink"
424 }
425 run_test 17d "symlinks: create dangling ========================"
426
427 test_17e() {
428         test_mkdir -p $DIR/$tdir
429         local foo=$DIR/$tdir/$tfile
430         ln -s $foo $foo || error "create symlink failed"
431         ls -l $foo || error "ls -l failed"
432         ls $foo && error "ls not failed" || true
433 }
434 run_test 17e "symlinks: create recursive symlink (should return error) ===="
435
436 test_17f() {
437         test_mkdir -p $DIR/$tdir
438         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
439         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
440         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
441         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
442         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
443         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
444         ls -l  $DIR/$tdir
445 }
446 run_test 17f "symlinks: long and very long symlink name ========================"
447
448 # str_repeat(S, N) generate a string that is string S repeated N times
449 str_repeat() {
450         local s=$1
451         local n=$2
452         local ret=''
453         while [ $((n -= 1)) -ge 0 ]; do
454                 ret=$ret$s
455         done
456         echo $ret
457 }
458
459 # Long symlinks and LU-2241
460 test_17g() {
461         test_mkdir -p $DIR/$tdir
462         local TESTS="59 60 61 4094 4095"
463
464         # Fix for inode size boundary in 2.1.4
465         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
466                 TESTS="4094 4095"
467
468         # Patch not applied to 2.2 or 2.3 branches
469         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
470         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
471                 TESTS="4094 4095"
472
473         # skip long symlink name for rhel6.5.
474         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
475         grep -q '6.5' /etc/redhat-release &>/dev/null &&
476                 TESTS="59 60 61 4062 4063"
477
478         for i in $TESTS; do
479                 local SYMNAME=$(str_repeat 'x' $i)
480                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
481                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
482         done
483 }
484 run_test 17g "symlinks: really long symlink name and inode boundaries"
485
486 test_17h() { #bug 17378
487         remote_mds_nodsh && skip "remote MDS with nodsh" && return
488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
489         local mdt_idx
490         test_mkdir -p $DIR/$tdir
491         if [[ $MDSCOUNT -gt 1 ]]; then
492                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
493         else
494                 mdt_idx=0
495         fi
496         $SETSTRIPE -c -1 $DIR/$tdir
497 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
498         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
499         touch $DIR/$tdir/$tfile || true
500 }
501 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
502
503 test_17i() { #bug 20018
504         remote_mds_nodsh && skip "remote MDS with nodsh" && return
505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
506         test_mkdir -c1 $DIR/$tdir
507         local foo=$DIR/$tdir/$tfile
508         local mdt_idx
509         if [[ $MDSCOUNT -gt 1 ]]; then
510                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
511         else
512                 mdt_idx=0
513         fi
514         ln -s $foo $foo || error "create symlink failed"
515 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
516         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
517         ls -l $foo && error "error not detected"
518         return 0
519 }
520 run_test 17i "don't panic on short symlink"
521
522 test_17k() { #bug 22301
523         [[ -z "$(which rsync 2>/dev/null)" ]] &&
524                 skip "no rsync command" && return 0
525         rsync --help | grep -q xattr ||
526                 skip_env "$(rsync --version | head -n1) does not support xattrs"
527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
528         test_mkdir -p $DIR/$tdir
529         test_mkdir -p $DIR/$tdir.new
530         touch $DIR/$tdir/$tfile
531         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
532         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
533                 error "rsync failed with xattrs enabled"
534 }
535 run_test 17k "symlinks: rsync with xattrs enabled ========================="
536
537 test_17l() { # LU-279
538         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
539                 skip "no getfattr command" && return 0
540         test_mkdir -p $DIR/$tdir
541         touch $DIR/$tdir/$tfile
542         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
543         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
544                 # -h to not follow symlinks. -m '' to list all the xattrs.
545                 # grep to remove first line: '# file: $path'.
546                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
547                 do
548                         lgetxattr_size_check $path $xattr ||
549                                 error "lgetxattr_size_check $path $xattr failed"
550                 done
551         done
552 }
553 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
554
555 # LU-1540
556 test_17m() {
557         local short_sym="0123456789"
558         local WDIR=$DIR/${tdir}m
559         local i
560
561         remote_mds_nodsh && skip "remote MDS with nodsh" && return
562         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
563         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
564                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
565
566         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
567                 skip "only for ldiskfs MDT" && return 0
568
569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
570
571         test_mkdir -p $WDIR
572         long_sym=$short_sym
573         # create a long symlink file
574         for ((i = 0; i < 4; ++i)); do
575                 long_sym=${long_sym}${long_sym}
576         done
577
578         echo "create 512 short and long symlink files under $WDIR"
579         for ((i = 0; i < 256; ++i)); do
580                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
581                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
582         done
583
584         echo "erase them"
585         rm -f $WDIR/*
586         sync
587         wait_delete_completed
588
589         echo "recreate the 512 symlink files with a shorter string"
590         for ((i = 0; i < 512; ++i)); do
591                 # rewrite the symlink file with a shorter string
592                 ln -sf ${long_sym} $WDIR/long-$i || error "long_sym failed"
593                 ln -sf ${short_sym} $WDIR/short-$i || error "short_sym failed"
594         done
595
596         local mds_index=$(($($LFS getstripe -M $WDIR) + 1))
597         local devname=$(mdsdevname $mds_index)
598
599         echo "stop and checking mds${mds_index}:"
600         # e2fsck should not return error
601         stop mds${mds_index}
602         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
603         rc=$?
604
605         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
606         df $MOUNT > /dev/null 2>&1
607         [ $rc -eq 0 ] ||
608                 error "e2fsck detected error for short/long symlink: rc=$rc"
609 }
610 run_test 17m "run e2fsck against MDT which contains short/long symlink"
611
612 check_fs_consistency_17n() {
613         local mdt_index
614         local rc=0
615
616         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
617         # so it only check MDT1/MDT2 instead of all of MDTs.
618         for mdt_index in 1 2; do
619                 local devname=$(mdsdevname $mdt_index)
620                 # e2fsck should not return error
621                 stop mds${mdt_index}
622                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
623                         rc=$((rc + $?))
624
625                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
626                         error "mount mds$mdt_index failed"
627                 df $MOUNT > /dev/null 2>&1
628         done
629         return $rc
630 }
631
632 test_17n() {
633         local i
634
635         remote_mds_nodsh && skip "remote MDS with nodsh" && return
636         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
637         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
638                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
639
640         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
641                 skip "only for ldiskfs MDT" && return 0
642
643         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
644
645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
646
647         test_mkdir $DIR/$tdir
648         for ((i=0; i<10; i++)); do
649                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
650                         error "create remote dir error $i"
651                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
652                         error "create files under remote dir failed $i"
653         done
654
655         check_fs_consistency_17n ||
656                 error "e2fsck report error after create files under remote dir"
657
658         for ((i = 0; i < 10; i++)); do
659                 rm -rf $DIR/$tdir/remote_dir_${i} ||
660                         error "destroy remote dir error $i"
661         done
662
663         check_fs_consistency_17n ||
664                 error "e2fsck report error after unlink files under remote dir"
665
666         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
667                 skip "lustre < 2.4.50 does not support migrate mv " && return
668
669         for ((i = 0; i < 10; i++)); do
670                 mkdir -p $DIR/$tdir/remote_dir_${i}
671                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
672                         error "create files under remote dir failed $i"
673                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
674                         error "migrate remote dir error $i"
675         done
676         check_fs_consistency_17n || error "e2fsck report error after migration"
677
678         for ((i = 0; i < 10; i++)); do
679                 rm -rf $DIR/$tdir/remote_dir_${i} ||
680                         error "destroy remote dir error $i"
681         done
682
683         check_fs_consistency_17n || error "e2fsck report error after unlink"
684 }
685 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
686
687 test_17o() {
688         remote_mds_nodsh && skip "remote MDS with nodsh" && return
689         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
690                 skip "Need MDS version at least 2.3.64" && return
691
692         local WDIR=$DIR/${tdir}o
693         local mdt_index
694         local rc=0
695
696         test_mkdir -p $WDIR
697         mdt_index=$($LFS getstripe -M $WDIR)
698         mdt_index=$((mdt_index+1))
699
700         touch $WDIR/$tfile
701
702         #fail mds will wait the failover finish then set
703         #following fail_loc to avoid interfer the recovery process.
704         fail mds${mdt_index}
705
706         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
707         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
708         ls -l $WDIR/$tfile && rc=1
709         do_facet mds${mdt_index} lctl set_param fail_loc=0
710         [[ $rc -ne 0 ]] && error "stat file should fail"
711         true
712 }
713 run_test 17o "stat file with incompat LMA feature"
714
715 test_18() {
716         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
717         ls $DIR || error "Failed to ls $DIR: $?"
718 }
719 run_test 18 "touch .../f ; ls ... =============================="
720
721 test_19a() {
722         touch $DIR/$tfile
723         ls -l $DIR
724         rm $DIR/$tfile
725         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
726 }
727 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
728
729 test_19b() {
730         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
731 }
732 run_test 19b "ls -l .../f19 (should return error) =============="
733
734 test_19c() {
735         [ $RUNAS_ID -eq $UID ] &&
736                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
737         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
738 }
739 run_test 19c "$RUNAS touch .../f19 (should return error) =="
740
741 test_19d() {
742         cat $DIR/f19 && error || true
743 }
744 run_test 19d "cat .../f19 (should return error) =============="
745
746 test_20() {
747         touch $DIR/$tfile
748         rm $DIR/$tfile
749         log "1 done"
750         touch $DIR/$tfile
751         rm $DIR/$tfile
752         log "2 done"
753         touch $DIR/$tfile
754         rm $DIR/$tfile
755         log "3 done"
756         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
757 }
758 run_test 20 "touch .../f ; ls -l ... ==========================="
759
760 test_21() {
761         test_mkdir -p $DIR/$tdir
762         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
763         ln -s dangle $DIR/$tdir/link
764         echo foo >> $DIR/$tdir/link
765         cat $DIR/$tdir/dangle
766         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
767         $CHECKSTAT -f -t file $DIR/$tdir/link ||
768                 error "$tdir/link not linked to a file"
769 }
770 run_test 21 "write to dangling link ============================"
771
772 test_22() {
773         WDIR=$DIR/$tdir
774         test_mkdir -p $DIR/$tdir
775         chown $RUNAS_ID:$RUNAS_GID $WDIR
776         (cd $WDIR || error "cd $WDIR failed";
777         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
778         $RUNAS tar xf -)
779         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
780         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
781         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
782 }
783 run_test 22 "unpack tar archive as non-root user ==============="
784
785 # was test_23
786 test_23a() {
787         test_mkdir -p $DIR/$tdir
788         local file=$DIR/$tdir/$tfile
789
790         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
791         openfile -f O_CREAT:O_EXCL $file &&
792                 error "$file recreate succeeded" || true
793 }
794 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
795
796 test_23b() { # bug 18988
797         test_mkdir -p $DIR/$tdir
798         local file=$DIR/$tdir/$tfile
799
800         rm -f $file
801         echo foo > $file || error "write filed"
802         echo bar >> $file || error "append filed"
803         $CHECKSTAT -s 8 $file || error "wrong size"
804         rm $file
805 }
806 run_test 23b "O_APPEND check =========================="
807
808 # rename sanity
809 test_24a() {
810         echo '-- same directory rename'
811         test_mkdir $DIR/$tdir
812         touch $DIR/$tdir/$tfile.1
813         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
814         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
815 }
816 run_test 24a "rename file to non-existent target"
817
818 test_24b() {
819         test_mkdir $DIR/$tdir
820         touch $DIR/$tdir/$tfile.{1,2}
821         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
822         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
823         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
824 }
825 run_test 24b "rename file to existing target"
826
827 test_24c() {
828         test_mkdir $DIR/$tdir
829         test_mkdir $DIR/$tdir/d$testnum.1
830         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
831         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
832         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
833 }
834 run_test 24c "rename directory to non-existent target"
835
836 test_24d() {
837         test_mkdir -c1 $DIR/$tdir
838         test_mkdir -c1 $DIR/$tdir/d$testnum.1
839         test_mkdir -c1 $DIR/$tdir/d$testnum.2
840         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
841         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
842         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
843 }
844 run_test 24d "rename directory to existing target"
845
846 test_24e() {
847         echo '-- cross directory renames --'
848         test_mkdir $DIR/R5a
849         test_mkdir $DIR/R5b
850         touch $DIR/R5a/f
851         mv $DIR/R5a/f $DIR/R5b/g
852         $CHECKSTAT -a $DIR/R5a/f || error
853         $CHECKSTAT -t file $DIR/R5b/g || error
854 }
855 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
856
857 test_24f() {
858         test_mkdir $DIR/R6a
859         test_mkdir $DIR/R6b
860         touch $DIR/R6a/f $DIR/R6b/g
861         mv $DIR/R6a/f $DIR/R6b/g
862         $CHECKSTAT -a $DIR/R6a/f || error
863         $CHECKSTAT -t file $DIR/R6b/g || error
864 }
865 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
866
867 test_24g() {
868         test_mkdir $DIR/R7a
869         test_mkdir $DIR/R7b
870         test_mkdir $DIR/R7a/d
871         mv $DIR/R7a/d $DIR/R7b/e
872         $CHECKSTAT -a $DIR/R7a/d || error
873         $CHECKSTAT -t dir $DIR/R7b/e || error
874 }
875 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
876
877 test_24h() {
878         test_mkdir -c1 $DIR/R8a
879         test_mkdir -c1 $DIR/R8b
880         test_mkdir -c1 $DIR/R8a/d
881         test_mkdir -c1 $DIR/R8b/e
882         mrename $DIR/R8a/d $DIR/R8b/e
883         $CHECKSTAT -a $DIR/R8a/d || error
884         $CHECKSTAT -t dir $DIR/R8b/e || error
885 }
886 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
887
888 test_24i() {
889         echo "-- rename error cases"
890         test_mkdir $DIR/R9
891         test_mkdir $DIR/R9/a
892         touch $DIR/R9/f
893         mrename $DIR/R9/f $DIR/R9/a
894         $CHECKSTAT -t file $DIR/R9/f || error
895         $CHECKSTAT -t dir  $DIR/R9/a || error
896         $CHECKSTAT -a $DIR/R9/a/f || error
897 }
898 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
899
900 test_24j() {
901         test_mkdir $DIR/R10
902         mrename $DIR/R10/f $DIR/R10/g
903         $CHECKSTAT -t dir $DIR/R10 || error
904         $CHECKSTAT -a $DIR/R10/f || error
905         $CHECKSTAT -a $DIR/R10/g || error
906 }
907 run_test 24j "source does not exist ============================"
908
909 test_24k() {
910         test_mkdir $DIR/R11a
911         test_mkdir $DIR/R11a/d
912         touch $DIR/R11a/f
913         mv $DIR/R11a/f $DIR/R11a/d
914         $CHECKSTAT -a $DIR/R11a/f || error
915         $CHECKSTAT -t file $DIR/R11a/d/f || error
916 }
917 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
918
919 # bug 2429 - rename foo foo foo creates invalid file
920 test_24l() {
921         f="$DIR/f24l"
922         $MULTIOP $f OcNs || error
923 }
924 run_test 24l "Renaming a file to itself ========================"
925
926 test_24m() {
927         f="$DIR/f24m"
928         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
929         # on ext3 this does not remove either the source or target files
930         # though the "expected" operation would be to remove the source
931         $CHECKSTAT -t file ${f} || error "${f} missing"
932         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
933 }
934 run_test 24m "Renaming a file to a hard link to itself ========="
935
936 test_24n() {
937     f="$DIR/f24n"
938     # this stats the old file after it was renamed, so it should fail
939     touch ${f}
940     $CHECKSTAT ${f}
941     mv ${f} ${f}.rename
942     $CHECKSTAT ${f}.rename
943     $CHECKSTAT -a ${f}
944 }
945 run_test 24n "Statting the old file after renaming (Posix rename 2)"
946
947 test_24o() {
948         test_mkdir -p $DIR/d24o
949         rename_many -s random -v -n 10 $DIR/d24o
950 }
951 run_test 24o "rename of files during htree split ==============="
952
953 test_24p() {
954         test_mkdir $DIR/R12a
955         test_mkdir $DIR/R12b
956         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
957         mrename $DIR/R12a $DIR/R12b
958         $CHECKSTAT -a $DIR/R12a || error
959         $CHECKSTAT -t dir $DIR/R12b || error
960         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
961         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
962 }
963 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
964
965 cleanup_multiop_pause() {
966         trap 0
967         kill -USR1 $MULTIPID
968 }
969
970 test_24q() {
971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
972         test_mkdir $DIR/R13a
973         test_mkdir $DIR/R13b
974         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
975         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
976         MULTIPID=$!
977
978         trap cleanup_multiop_pause EXIT
979         mrename $DIR/R13a $DIR/R13b
980         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
981         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
982         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
983         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
984         cleanup_multiop_pause
985         wait $MULTIPID || error "multiop close failed"
986 }
987 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
988
989 test_24r() { #bug 3789
990         test_mkdir $DIR/R14a
991         test_mkdir $DIR/R14a/b
992         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
993         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
994         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
995 }
996 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
997
998 test_24s() {
999         test_mkdir $DIR/R15a
1000         test_mkdir $DIR/R15a/b
1001         test_mkdir $DIR/R15a/b/c
1002         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1003         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1004         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1005 }
1006 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1007 test_24t() {
1008         test_mkdir $DIR/R16a
1009         test_mkdir $DIR/R16a/b
1010         test_mkdir $DIR/R16a/b/c
1011         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1012         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1013         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1014 }
1015 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1016
1017 test_24u() { # bug12192
1018         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1019         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1020 }
1021 run_test 24u "create stripe file"
1022
1023 page_size() {
1024         getconf PAGE_SIZE
1025 }
1026
1027 simple_cleanup_common() {
1028         trap 0
1029         rm -rf $DIR/$tdir
1030         wait_delete_completed
1031 }
1032
1033 max_pages_per_rpc() {
1034         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1035 }
1036
1037 test_24v() {
1038         local NRFILES=100000
1039         local FREE_INODES=$(mdt_free_inodes 0)
1040         [[ $FREE_INODES -lt $NRFILES ]] &&
1041                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1042                 return
1043
1044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1045         trap simple_cleanup_common EXIT
1046
1047         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1048         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1049
1050         test_mkdir -p $DIR/$tdir
1051         createmany -m $DIR/$tdir/$tfile $NRFILES
1052
1053         cancel_lru_locks mdc
1054         lctl set_param mdc.*.stats clear
1055
1056         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1057
1058         # LU-5 large readdir
1059         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1060         #               8 bytes for name(filename is mostly 5 in this test) +
1061         #               8 bytes for luda_type
1062         # take into account of overhead in lu_dirpage header and end mark in
1063         # each page, plus one in RPC_NUM calculation.
1064         DIRENT_SIZE=48
1065         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1066         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1067         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1068                                 awk '/^mds_readpage/ {print $2}')
1069         [[ $mds_readpage -gt $RPC_NUM ]] &&
1070                 error "large readdir doesn't take effect"
1071
1072         simple_cleanup_common
1073 }
1074 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1075
1076 test_24w() { # bug21506
1077         SZ1=234852
1078         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1079         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1080         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1081         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1082         [[ "$SZ1" -eq "$SZ2" ]] ||
1083                 error "Error reading at the end of the file $tfile"
1084 }
1085 run_test 24w "Reading a file larger than 4Gb"
1086
1087 test_24x() {
1088         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1090         local MDTIDX=1
1091         local remote_dir=$DIR/$tdir/remote_dir
1092
1093         test_mkdir -p $DIR/$tdir
1094         $LFS mkdir -i $MDTIDX $remote_dir ||
1095                 error "create remote directory failed"
1096
1097         test_mkdir -p $DIR/$tdir/src_dir
1098         touch $DIR/$tdir/src_file
1099         test_mkdir -p $remote_dir/tgt_dir
1100         touch $remote_dir/tgt_file
1101
1102         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1103                 error "rename dir cross MDT failed!"
1104
1105         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1106                 error "rename file cross MDT failed!"
1107
1108         touch $DIR/$tdir/ln_file
1109         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1110                 error "ln file cross MDT failed"
1111
1112         rm -rf $DIR/$tdir || error "Can not delete directories"
1113 }
1114 run_test 24x "cross MDT rename/link"
1115
1116 test_24y() {
1117         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1119         local MDTIDX=1
1120         local remote_dir=$DIR/$tdir/remote_dir
1121
1122         test_mkdir -p $DIR/$tdir
1123         $LFS mkdir -i $MDTIDX $remote_dir ||
1124                    error "create remote directory failed"
1125
1126         test_mkdir -p $remote_dir/src_dir
1127         touch $remote_dir/src_file
1128         test_mkdir -p $remote_dir/tgt_dir
1129         touch $remote_dir/tgt_file
1130
1131         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1132                 error "rename subdir in the same remote dir failed!"
1133
1134         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1135                 error "rename files in the same remote dir failed!"
1136
1137         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1138                 error "link files in the same remote dir failed!"
1139
1140         rm -rf $DIR/$tdir || error "Can not delete directories"
1141 }
1142 run_test 24y "rename/link on the same dir should succeed"
1143
1144 test_24A() { # LU-3182
1145         local NFILES=5000
1146
1147         rm -rf $DIR/$tdir
1148         test_mkdir -p $DIR/$tdir
1149         createmany -m $DIR/$tdir/$tfile $NFILES
1150         local t=$(ls $DIR/$tdir | wc -l)
1151         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1152         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1153         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1154                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1155         fi
1156
1157         rm -rf $DIR/$tdir || error "Can not delete directories"
1158 }
1159 run_test 24A "readdir() returns correct number of entries."
1160
1161 test_24B() { # LU-4805
1162         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1163         local count
1164
1165         test_mkdir $DIR/$tdir
1166         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1167                 error "create striped dir failed"
1168
1169         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1170         [ $count -eq 2 ] || error "Expected 2, got $count"
1171
1172         touch $DIR/$tdir/striped_dir/a
1173
1174         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1175         [ $count -eq 3 ] || error "Expected 3, got $count"
1176
1177         touch $DIR/$tdir/striped_dir/.f
1178
1179         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1180         [ $count -eq 4 ] || error "Expected 4, got $count"
1181
1182         rm -rf $DIR/$tdir || error "Can not delete directories"
1183 }
1184 run_test 24B "readdir for striped dir return correct number of entries"
1185
1186 test_24C() {
1187         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1188
1189         mkdir $DIR/$tdir
1190         mkdir $DIR/$tdir/d0
1191         mkdir $DIR/$tdir/d1
1192
1193         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1194                 error "create striped dir failed"
1195
1196         cd $DIR/$tdir/d0/striped_dir
1197
1198         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1199         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1200         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1201
1202         [ "$d0_ino" = "$parent_ino" ] ||
1203                 error ".. wrong, expect $d0_ino, get $parent_ino"
1204
1205         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1206                 error "mv striped dir failed"
1207
1208         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1209
1210         [ "$d1_ino" = "$parent_ino" ] ||
1211                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1212 }
1213 run_test 24C "check .. in striped dir"
1214
1215 test_24D() { # LU-6101
1216         local NFILES=50000
1217
1218         rm -rf $DIR/$tdir
1219         mkdir -p $DIR/$tdir
1220         createmany -m $DIR/$tdir/$tfile $NFILES
1221         local t=$(ls $DIR/$tdir | wc -l)
1222         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1223         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1224         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1225                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1226         fi
1227
1228         rm -rf $DIR/$tdir || error "Can not delete directories"
1229 }
1230 run_test 24D "readdir() returns correct number of entries after cursor reload"
1231
1232 test_24E() {
1233         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1234         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1235
1236         mkdir -p $DIR/$tdir
1237         mkdir $DIR/$tdir/src_dir
1238         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1239                 error "create remote source failed"
1240
1241         touch $DIR/$tdir/src_dir/src_child/a
1242
1243         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1244                 error "create remote target dir failed"
1245
1246         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1247                 error "create remote target child failed"
1248
1249         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1250                 error "rename dir cross MDT failed!"
1251
1252         find $DIR/$tdir
1253
1254         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1255                 error "src_child still exists after rename"
1256
1257         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1258                 error "missing file(a) after rename"
1259
1260         rm -rf $DIR/$tdir || error "Can not delete directories"
1261 }
1262 run_test 24E "cross MDT rename/link"
1263
1264 test_25a() {
1265         echo '== symlink sanity ============================================='
1266
1267         test_mkdir $DIR/d25
1268         ln -s d25 $DIR/s25
1269         touch $DIR/s25/foo || error
1270 }
1271 run_test 25a "create file in symlinked directory ==============="
1272
1273 test_25b() {
1274         [ ! -d $DIR/d25 ] && test_25a
1275         $CHECKSTAT -t file $DIR/s25/foo || error
1276 }
1277 run_test 25b "lookup file in symlinked directory ==============="
1278
1279 test_26a() {
1280         test_mkdir $DIR/d26
1281         test_mkdir $DIR/d26/d26-2
1282         ln -s d26/d26-2 $DIR/s26
1283         touch $DIR/s26/foo || error
1284 }
1285 run_test 26a "multiple component symlink ======================="
1286
1287 test_26b() {
1288         test_mkdir -p $DIR/d26b/d26-2
1289         ln -s d26b/d26-2/foo $DIR/s26-2
1290         touch $DIR/s26-2 || error
1291 }
1292 run_test 26b "multiple component symlink at end of lookup ======"
1293
1294 test_26c() {
1295         test_mkdir $DIR/d26.2
1296         touch $DIR/d26.2/foo
1297         ln -s d26.2 $DIR/s26.2-1
1298         ln -s s26.2-1 $DIR/s26.2-2
1299         ln -s s26.2-2 $DIR/s26.2-3
1300         chmod 0666 $DIR/s26.2-3/foo
1301 }
1302 run_test 26c "chain of symlinks ================================"
1303
1304 # recursive symlinks (bug 439)
1305 test_26d() {
1306         ln -s d26-3/foo $DIR/d26-3
1307 }
1308 run_test 26d "create multiple component recursive symlink ======"
1309
1310 test_26e() {
1311         [ ! -h $DIR/d26-3 ] && test_26d
1312         rm $DIR/d26-3
1313 }
1314 run_test 26e "unlink multiple component recursive symlink ======"
1315
1316 # recursive symlinks (bug 7022)
1317 test_26f() {
1318         test_mkdir -p $DIR/$tdir
1319         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1320         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1321         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1322         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1323         cd $tfile                || error "cd $tfile failed"
1324         ln -s .. dotdot          || error "ln dotdot failed"
1325         ln -s dotdot/lndir lndir || error "ln lndir failed"
1326         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1327         output=`ls $tfile/$tfile/lndir/bar1`
1328         [ "$output" = bar1 ] && error "unexpected output"
1329         rm -r $tfile             || error "rm $tfile failed"
1330         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1331 }
1332 run_test 26f "rm -r of a directory which has recursive symlink ="
1333
1334 test_27a() {
1335         echo '== stripe sanity =============================================='
1336         test_mkdir -p $DIR/d27 || error "mkdir failed"
1337         $GETSTRIPE $DIR/d27
1338         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1339         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1340         log "== test_27a: write to one stripe file ========================="
1341         cp /etc/hosts $DIR/d27/f0 || error
1342 }
1343 run_test 27a "one stripe file =================================="
1344
1345 test_27b() {
1346         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1347         test_mkdir -p $DIR/d27
1348         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1349         $GETSTRIPE -c $DIR/d27/f01
1350         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1351                 error "two-stripe file doesn't have two stripes"
1352
1353         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1354 }
1355 run_test 27b "create and write to two stripe file"
1356
1357 test_27d() {
1358         test_mkdir -p $DIR/d27
1359         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1360         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1361         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1362 }
1363 run_test 27d "create file with default settings ================"
1364
1365 test_27e() {
1366         # LU-5839 adds check for existed layout before setting it
1367         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1368                 skip "Need MDS version at least 2.7.56" && return
1369         test_mkdir -p $DIR/d27
1370         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1371         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1372         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1373 }
1374 run_test 27e "setstripe existing file (should return error) ======"
1375
1376 test_27f() {
1377         test_mkdir $DIR/$tdir
1378         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1379                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1380         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1381                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1382         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1383         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1384 }
1385 run_test 27f "setstripe with bad stripe size (should return error)"
1386
1387 test_27g() {
1388         test_mkdir -p $DIR/d27
1389         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1390         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1391                 error "$DIR/d27/fnone has object"
1392 }
1393 run_test 27g "$GETSTRIPE with no objects"
1394
1395 test_27i() {
1396         test_mkdir $DIR/$tdir
1397         touch $DIR/$tdir/$tfile || error "touch failed"
1398         [[ $($GETSTRIPE -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1399                 error "missing objects"
1400 }
1401 run_test 27i "$GETSTRIPE with some objects"
1402
1403 test_27j() {
1404         test_mkdir -p $DIR/d27
1405         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1406 }
1407 run_test 27j "setstripe with bad stripe offset (should return error)"
1408
1409 test_27k() { # bug 2844
1410         test_mkdir -p $DIR/d27
1411         FILE=$DIR/d27/f27k
1412         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1413         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1414         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1415         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1416         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1417         dd if=/dev/zero of=$FILE bs=4k count=1
1418         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1419         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1420 }
1421 run_test 27k "limit i_blksize for broken user apps ============="
1422
1423 test_27l() {
1424         test_mkdir -p $DIR/d27
1425         mcreate $DIR/f27l || error "creating file"
1426         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1427                 error "setstripe should have failed" || true
1428 }
1429 run_test 27l "check setstripe permissions (should return error)"
1430
1431 test_27m() {
1432         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1433                 return
1434         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1435                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1436                 return
1437         fi
1438         trap simple_cleanup_common EXIT
1439         test_mkdir -p $DIR/$tdir
1440         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1441         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1442                 error "dd should fill OST0"
1443         i=2
1444         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1445                 i=$((i + 1))
1446                 [ $i -gt 256 ] && break
1447         done
1448         i=$((i + 1))
1449         touch $DIR/$tdir/f27m_$i
1450         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1451                 error "OST0 was full but new created file still use it"
1452         i=$((i + 1))
1453         touch $DIR/$tdir/f27m_$i
1454         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1455                 error "OST0 was full but new created file still use it"
1456         simple_cleanup_common
1457 }
1458 run_test 27m "create file while OST0 was full =================="
1459
1460 sleep_maxage() {
1461         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1462         sleep $DELAY
1463 }
1464
1465 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1466 # if the OST isn't full anymore.
1467 reset_enospc() {
1468         local OSTIDX=${1:-""}
1469
1470         local list=$(comma_list $(osts_nodes))
1471         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1472
1473         do_nodes $list lctl set_param fail_loc=0
1474         sync    # initiate all OST_DESTROYs from MDS to OST
1475         sleep_maxage
1476 }
1477
1478 exhaust_precreations() {
1479         local OSTIDX=$1
1480         local FAILLOC=$2
1481         local FAILIDX=${3:-$OSTIDX}
1482         local ofacet=ost$((OSTIDX + 1))
1483
1484         test_mkdir -p -c1 $DIR/$tdir
1485         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1486         local mfacet=mds$((mdtidx + 1))
1487         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1488
1489         local OST=$(ostname_from_index $OSTIDX)
1490
1491         # on the mdt's osc
1492         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1493         local last_id=$(do_facet $mfacet lctl get_param -n \
1494                         osc.$mdtosc_proc1.prealloc_last_id)
1495         local next_id=$(do_facet $mfacet lctl get_param -n \
1496                         osc.$mdtosc_proc1.prealloc_next_id)
1497
1498         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1499         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1500
1501         test_mkdir -p $DIR/$tdir/${OST}
1502         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1503 #define OBD_FAIL_OST_ENOSPC              0x215
1504         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1505         echo "Creating to objid $last_id on ost $OST..."
1506         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1507         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1508         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1509         sleep_maxage
1510 }
1511
1512 exhaust_all_precreations() {
1513         local i
1514         for (( i=0; i < OSTCOUNT; i++ )) ; do
1515                 exhaust_precreations $i $1 -1
1516         done
1517 }
1518
1519 test_27n() {
1520         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1522         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1523         remote_ost_nodsh && skip "remote OST with nodsh" && return
1524
1525         reset_enospc
1526         rm -f $DIR/$tdir/$tfile
1527         exhaust_precreations 0 0x80000215
1528         $SETSTRIPE -c -1 $DIR/$tdir
1529         touch $DIR/$tdir/$tfile || error
1530         $GETSTRIPE $DIR/$tdir/$tfile
1531         reset_enospc
1532 }
1533 run_test 27n "create file with some full OSTs =================="
1534
1535 test_27o() {
1536         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1538         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1539         remote_ost_nodsh && skip "remote OST with nodsh" && return
1540
1541         reset_enospc
1542         rm -f $DIR/$tdir/$tfile
1543         exhaust_all_precreations 0x215
1544
1545         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1546
1547         reset_enospc
1548         rm -rf $DIR/$tdir/*
1549 }
1550 run_test 27o "create file with all full OSTs (should error) ===="
1551
1552 test_27p() {
1553         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1555         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1556         remote_ost_nodsh && skip "remote OST with nodsh" && return
1557
1558         reset_enospc
1559         rm -f $DIR/$tdir/$tfile
1560         test_mkdir -p $DIR/$tdir
1561
1562         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1563         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1564         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1565
1566         exhaust_precreations 0 0x80000215
1567         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1568         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1569         $GETSTRIPE $DIR/$tdir/$tfile
1570
1571         reset_enospc
1572 }
1573 run_test 27p "append to a truncated file with some full OSTs ==="
1574
1575 test_27q() {
1576         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1578         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1579         remote_ost_nodsh && skip "remote OST with nodsh" && return
1580
1581         reset_enospc
1582         rm -f $DIR/$tdir/$tfile
1583
1584         test_mkdir -p $DIR/$tdir
1585         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1586         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1587         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1588
1589         exhaust_all_precreations 0x215
1590
1591         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1592         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1593
1594         reset_enospc
1595 }
1596 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1597
1598 test_27r() {
1599         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1601         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1602         remote_ost_nodsh && skip "remote OST with nodsh" && return
1603
1604         reset_enospc
1605         rm -f $DIR/$tdir/$tfile
1606         exhaust_precreations 0 0x80000215
1607
1608         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1609
1610         reset_enospc
1611 }
1612 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1613
1614 test_27s() { # bug 10725
1615         test_mkdir -p $DIR/$tdir
1616         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1617         local stripe_count=0
1618         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1619         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1620                 error "stripe width >= 2^32 succeeded" || true
1621
1622 }
1623 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1624
1625 test_27t() { # bug 10864
1626         WDIR=$(pwd)
1627         WLFS=$(which lfs)
1628         cd $DIR
1629         touch $tfile
1630         $WLFS getstripe $tfile
1631         cd $WDIR
1632 }
1633 run_test 27t "check that utils parse path correctly"
1634
1635 test_27u() { # bug 4900
1636         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1637         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1638         local index
1639         local list=$(comma_list $(mdts_nodes))
1640
1641 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1642         do_nodes $list $LCTL set_param fail_loc=0x139
1643         test_mkdir -p $DIR/$tdir
1644         rm -rf $DIR/$tdir/*
1645         createmany -o $DIR/$tdir/t- 1000
1646         do_nodes $list $LCTL set_param fail_loc=0
1647
1648         TLOG=$DIR/$tfile.getstripe
1649         $GETSTRIPE $DIR/$tdir > $TLOG
1650         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1651         unlinkmany $DIR/$tdir/t- 1000
1652         [[ $OBJS -gt 0 ]] &&
1653                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1654 }
1655 run_test 27u "skip object creation on OSC w/o objects =========="
1656
1657 test_27v() { # bug 4900
1658         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1660         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1661         remote_ost_nodsh && skip "remote OST with nodsh" && return
1662
1663         exhaust_all_precreations 0x215
1664         reset_enospc
1665
1666         test_mkdir -p $DIR/$tdir
1667         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1668
1669         touch $DIR/$tdir/$tfile
1670         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1671         # all except ost1
1672         for (( i=1; i < OSTCOUNT; i++ )); do
1673                 do_facet ost$i lctl set_param fail_loc=0x705
1674         done
1675         local START=`date +%s`
1676         createmany -o $DIR/$tdir/$tfile 32
1677
1678         local FINISH=`date +%s`
1679         local TIMEOUT=`lctl get_param -n timeout`
1680         local PROCESS=$((FINISH - START))
1681         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1682                error "$FINISH - $START >= $TIMEOUT / 2"
1683         sleep $((TIMEOUT / 2 - PROCESS))
1684         reset_enospc
1685 }
1686 run_test 27v "skip object creation on slow OST ================="
1687
1688 test_27w() { # bug 10997
1689         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1690         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1691         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1692                 error "stripe size $size != 65536" || true
1693         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1694                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1695 }
1696 run_test 27w "check $SETSTRIPE -S option"
1697
1698 test_27wa() {
1699         [[ $OSTCOUNT -lt 2 ]] &&
1700                 skip_env "skipping multiple stripe count/offset test" && return
1701
1702         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1703         for i in $(seq 1 $OSTCOUNT); do
1704                 offset=$((i - 1))
1705                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1706                         error "setstripe -c $i -i $offset failed"
1707                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1708                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1709                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1710                 [ $index -ne $offset ] &&
1711                         error "stripe offset $index != $offset" || true
1712         done
1713 }
1714 run_test 27wa "check $SETSTRIPE -c -i options"
1715
1716 test_27x() {
1717         remote_ost_nodsh && skip "remote OST with nodsh" && return
1718         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1720         OFFSET=$(($OSTCOUNT - 1))
1721         OSTIDX=0
1722         local OST=$(ostname_from_index $OSTIDX)
1723
1724         test_mkdir -p $DIR/$tdir
1725         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1726         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1727         sleep_maxage
1728         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1729         for i in `seq 0 $OFFSET`; do
1730                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1731                 error "OST0 was degraded but new created file still use it"
1732         done
1733         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1734 }
1735 run_test 27x "create files while OST0 is degraded"
1736
1737 test_27y() {
1738         [[ $OSTCOUNT -lt 2 ]] &&
1739                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1740         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1741         remote_ost_nodsh && skip "remote OST with nodsh" && return
1742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1743
1744         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1745         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1746             osc.$mdtosc.prealloc_last_id)
1747         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1748             osc.$mdtosc.prealloc_next_id)
1749         local fcount=$((last_id - next_id))
1750         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1751         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1752
1753         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1754                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1755         local OST_DEACTIVE_IDX=-1
1756         local OSC
1757         local OSTIDX
1758         local OST
1759
1760         for OSC in $MDS_OSCS; do
1761                 OST=$(osc_to_ost $OSC)
1762                 OSTIDX=$(index_from_ostuuid $OST)
1763                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1764                         OST_DEACTIVE_IDX=$OSTIDX
1765                 fi
1766                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1767                         echo $OSC "is Deactivated:"
1768                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1769                 fi
1770         done
1771
1772         OSTIDX=$(index_from_ostuuid $OST)
1773         test_mkdir -p $DIR/$tdir
1774         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1775
1776         for OSC in $MDS_OSCS; do
1777                 OST=$(osc_to_ost $OSC)
1778                 OSTIDX=$(index_from_ostuuid $OST)
1779                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1780                         echo $OST "is degraded:"
1781                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1782                                                 obdfilter.$OST.degraded=1
1783                 fi
1784         done
1785
1786         sleep_maxage
1787         createmany -o $DIR/$tdir/$tfile $fcount
1788
1789         for OSC in $MDS_OSCS; do
1790                 OST=$(osc_to_ost $OSC)
1791                 OSTIDX=$(index_from_ostuuid $OST)
1792                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1793                         echo $OST "is recovered from degraded:"
1794                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1795                                                 obdfilter.$OST.degraded=0
1796                 else
1797                         do_facet $SINGLEMDS lctl --device %$OSC activate
1798                 fi
1799         done
1800
1801         # all osp devices get activated, hence -1 stripe count restored
1802         local stripecnt=0
1803
1804         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1805         # devices get activated.
1806         sleep_maxage
1807         $SETSTRIPE -c -1 $DIR/$tfile
1808         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1809         rm -f $DIR/$tfile
1810         [ $stripecnt -ne $OSTCOUNT ] &&
1811                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1812         return 0
1813 }
1814 run_test 27y "create files while OST0 is degraded and the rest inactive"
1815
1816 check_seq_oid()
1817 {
1818         log "check file $1"
1819
1820         lmm_count=$($GETSTRIPE -c $1)
1821         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1822         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1823
1824         local old_ifs="$IFS"
1825         IFS=$'[:]'
1826         fid=($($LFS path2fid $1))
1827         IFS="$old_ifs"
1828
1829         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1830         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1831
1832         # compare lmm_seq and lu_fid->f_seq
1833         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1834         # compare lmm_object_id and lu_fid->oid
1835         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1836
1837         # check the trusted.fid attribute of the OST objects of the file
1838         local have_obdidx=false
1839         local stripe_nr=0
1840         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1841                 # skip lines up to and including "obdidx"
1842                 [ -z "$obdidx" ] && break
1843                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1844                 $have_obdidx || continue
1845
1846                 local ost=$((obdidx + 1))
1847                 local dev=$(ostdevname $ost)
1848                 local oid_hex
1849
1850                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1851
1852                 seq=$(echo $seq | sed -e "s/^0x//g")
1853                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1854                         oid_hex=$(echo $oid)
1855                 else
1856                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1857                 fi
1858                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1859
1860                 local ff
1861                 #
1862                 # Don't unmount/remount the OSTs if we don't need to do that.
1863                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1864                 # update too, until that use mount/ll_decode_filter_fid/mount.
1865                 # Re-enable when debugfs will understand new filter_fid.
1866                 #
1867                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1868                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1869                                 $dev 2>/dev/null" | grep "parent=")
1870                 else
1871                         stop ost$ost
1872                         mount_fstype ost$ost
1873                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1874                                 $(facet_mntpt ost$ost)/$obj_file)
1875                         unmount_fstype ost$ost
1876                         start ost$ost $dev $OST_MOUNT_OPTS
1877                 fi
1878
1879                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1880
1881                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1882
1883                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1884                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1885                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1886                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1887                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1888                 local ff_pstripe
1889                 if echo $ff_parent | grep -q 'stripe='; then
1890                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1891                 else
1892                         #
1893                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1894                         # into f_ver in this case.  See the comment on
1895                         # ff_parent.
1896                         #
1897                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1898                                 sed -e 's/\]//')
1899                 fi
1900
1901                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1902                 [ $ff_pseq = $lmm_seq ] ||
1903                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1904                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1905                 [ $ff_poid = $lmm_oid ] ||
1906                         error "FF parent OID $ff_poid != $lmm_oid"
1907                 (($ff_pstripe == $stripe_nr)) ||
1908                         error "FF stripe $ff_pstripe != $stripe_nr"
1909
1910                 stripe_nr=$((stripe_nr + 1))
1911         done
1912 }
1913
1914 test_27z() {
1915         remote_ost_nodsh && skip "remote OST with nodsh" && return
1916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1917         test_mkdir -p $DIR/$tdir
1918
1919         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1920                 { error "setstripe -c -1 failed"; return 1; }
1921         # We need to send a write to every object to get parent FID info set.
1922         # This _should_ also work for setattr, but does not currently.
1923         # touch $DIR/$tdir/$tfile-1 ||
1924         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1925                 { error "dd $tfile-1 failed"; return 2; }
1926         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1927                 { error "setstripe -c -1 failed"; return 3; }
1928         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1929                 { error "dd $tfile-2 failed"; return 4; }
1930
1931         # make sure write RPCs have been sent to OSTs
1932         sync; sleep 5; sync
1933
1934         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1935         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1936 }
1937 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1938
1939 test_27A() { # b=19102
1940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1941         local restore_size=$($GETSTRIPE -S $MOUNT)
1942         local restore_count=$($GETSTRIPE -c $MOUNT)
1943         local restore_offset=$($GETSTRIPE -i $MOUNT)
1944         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1945         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1946                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1947         local default_size=$($GETSTRIPE -S $MOUNT)
1948         local default_offset=$($GETSTRIPE -i $MOUNT)
1949         local dsize=$((1024 * 1024))
1950         [ $default_size -eq $dsize ] ||
1951                 error "stripe size $default_size != $dsize"
1952         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1953         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1954 }
1955 run_test 27A "check filesystem-wide default LOV EA values"
1956
1957 test_27B() { # LU-2523
1958         test_mkdir -p $DIR/$tdir
1959         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1960         touch $DIR/$tdir/f0
1961         # open f1 with O_LOV_DELAY_CREATE
1962         # rename f0 onto f1
1963         # call setstripe ioctl on open file descriptor for f1
1964         # close
1965         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1966                 $DIR/$tdir/f0
1967
1968         rm -f $DIR/$tdir/f1
1969         # open f1 with O_LOV_DELAY_CREATE
1970         # unlink f1
1971         # call setstripe ioctl on open file descriptor for f1
1972         # close
1973         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1974
1975         # Allow multiop to fail in imitation of NFS's busted semantics.
1976         true
1977 }
1978 run_test 27B "call setstripe on open unlinked file/rename victim"
1979
1980 test_27C() { #LU-2871
1981         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1982
1983         declare -a ost_idx
1984         local index
1985         local found
1986         local i
1987         local j
1988
1989         test_mkdir -p $DIR/$tdir
1990         cd $DIR/$tdir
1991         for i in $(seq 0 $((OSTCOUNT - 1))); do
1992                 # set stripe across all OSTs starting from OST$i
1993                 $SETSTRIPE -i $i -c -1 $tfile$i
1994                 # get striping information
1995                 ost_idx=($($GETSTRIPE $tfile$i |
1996                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1997                 echo ${ost_idx[@]}
1998
1999                 # check the layout
2000                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2001                         error "${#ost_idx[@]} != $OSTCOUNT"
2002
2003                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2004                         found=0
2005                         for j in $(echo ${ost_idx[@]}); do
2006                                 if [ $index -eq $j ]; then
2007                                         found=1
2008                                         break
2009                                 fi
2010                         done
2011                         [ $found = 1 ] ||
2012                                 error "Can not find $index in ${ost_idx[@]}"
2013                 done
2014         done
2015 }
2016 run_test 27C "check full striping across all OSTs"
2017
2018 test_27D() {
2019         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2020         local POOL=${POOL:-testpool}
2021         local first_ost=0
2022         local last_ost=$(($OSTCOUNT - 1))
2023         local ost_step=1
2024         local ost_list=$(seq $first_ost $ost_step $last_ost)
2025         local ost_range="$first_ost $last_ost $ost_step"
2026
2027         test_mkdir -p $DIR/$tdir
2028         pool_add $POOL || error "pool_add failed"
2029         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2030         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2031                 error "llapi_layout_test failed"
2032         cleanup_pools || error "cleanup_pools failed"
2033 }
2034 run_test 27D "validate llapi_layout API"
2035
2036 # Verify that default_easize is increased from its initial value after
2037 # accessing a widely striped file.
2038 test_27E() {
2039         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2040
2041         # 72 bytes is the minimum space required to store striping
2042         # information for a file striped across one OST:
2043         # (sizeof(struct lov_user_md_v3) +
2044         #  sizeof(struct lov_user_ost_data_v1))
2045         local min_easize=72
2046         $LCTL set_param -n llite.*.default_easize $min_easize ||
2047                 error "lctl set_param failed"
2048         local easize=$($LCTL get_param -n llite.*.default_easize)
2049
2050         [ $easize -eq $min_easize ] ||
2051                 error "failed to set default_easize"
2052
2053         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2054                 error "setstripe failed"
2055         cat $DIR/$tfile
2056         rm $DIR/$tfile
2057
2058         easize=$($LCTL get_param -n llite.*.default_easize)
2059
2060         [ $easize -gt $min_easize ] ||
2061                 error "default_easize not updated"
2062 }
2063 run_test 27E "check that default extended attribute size properly increases"
2064
2065 # createtest also checks that device nodes are created and
2066 # then visible correctly (#2091)
2067 test_28() { # bug 2091
2068         test_mkdir $DIR/d28
2069         $CREATETEST $DIR/d28/ct || error
2070 }
2071 run_test 28 "create/mknod/mkdir with bad file types ============"
2072
2073 test_29() {
2074         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2075         cancel_lru_locks mdc
2076         test_mkdir $DIR/d29
2077         touch $DIR/d29/foo
2078         log 'first d29'
2079         ls -l $DIR/d29
2080
2081         declare -i LOCKCOUNTORIG=0
2082         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2083                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2084         done
2085         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2086
2087         declare -i LOCKUNUSEDCOUNTORIG=0
2088         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2089                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2090         done
2091
2092         log 'second d29'
2093         ls -l $DIR/d29
2094         log 'done'
2095
2096         declare -i LOCKCOUNTCURRENT=0
2097         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2098                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2099         done
2100
2101         declare -i LOCKUNUSEDCOUNTCURRENT=0
2102         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2103                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2104         done
2105
2106         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2107                 $LCTL set_param -n ldlm.dump_namespaces ""
2108                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2109                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2110                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2111                 return 2
2112         fi
2113         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2114                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2115                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2116                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2117                 return 3
2118         fi
2119 }
2120 run_test 29 "IT_GETATTR regression  ============================"
2121
2122 test_30a() { # was test_30
2123         cp $(which ls) $DIR || cp /bin/ls $DIR
2124         $DIR/ls / || error
2125         rm $DIR/ls
2126 }
2127 run_test 30a "execute binary from Lustre (execve) =============="
2128
2129 test_30b() {
2130         cp `which ls` $DIR || cp /bin/ls $DIR
2131         chmod go+rx $DIR/ls
2132         $RUNAS $DIR/ls / || error
2133         rm $DIR/ls
2134 }
2135 run_test 30b "execute binary from Lustre as non-root ==========="
2136
2137 test_30c() { # b=22376
2138         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2139         cp `which ls` $DIR || cp /bin/ls $DIR
2140         chmod a-rw $DIR/ls
2141         cancel_lru_locks mdc
2142         cancel_lru_locks osc
2143         $RUNAS $DIR/ls / || error
2144         rm -f $DIR/ls
2145 }
2146 run_test 30c "execute binary from Lustre without read perms ===="
2147
2148 test_31a() {
2149         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2150         $CHECKSTAT -a $DIR/f31 || error
2151 }
2152 run_test 31a "open-unlink file =================================="
2153
2154 test_31b() {
2155         touch $DIR/f31 || error
2156         ln $DIR/f31 $DIR/f31b || error
2157         $MULTIOP $DIR/f31b Ouc || error
2158         $CHECKSTAT -t file $DIR/f31 || error
2159 }
2160 run_test 31b "unlink file with multiple links while open ======="
2161
2162 test_31c() {
2163         touch $DIR/f31 || error
2164         ln $DIR/f31 $DIR/f31c || error
2165         multiop_bg_pause $DIR/f31 O_uc || return 1
2166         MULTIPID=$!
2167         $MULTIOP $DIR/f31c Ouc
2168         kill -USR1 $MULTIPID
2169         wait $MULTIPID
2170 }
2171 run_test 31c "open-unlink file with multiple links ============="
2172
2173 test_31d() {
2174         opendirunlink $DIR/d31d $DIR/d31d || error
2175         $CHECKSTAT -a $DIR/d31d || error
2176 }
2177 run_test 31d "remove of open directory ========================="
2178
2179 test_31e() { # bug 2904
2180         openfilleddirunlink $DIR/d31e || error
2181 }
2182 run_test 31e "remove of open non-empty directory ==============="
2183
2184 test_31f() { # bug 4554
2185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2186         set -vx
2187         test_mkdir $DIR/d31f
2188         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2189         cp /etc/hosts $DIR/d31f
2190         ls -l $DIR/d31f
2191         $GETSTRIPE $DIR/d31f/hosts
2192         multiop_bg_pause $DIR/d31f D_c || return 1
2193         MULTIPID=$!
2194
2195         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2196         test_mkdir $DIR/d31f
2197         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2198         cp /etc/hosts $DIR/d31f
2199         ls -l $DIR/d31f
2200         $GETSTRIPE $DIR/d31f/hosts
2201         multiop_bg_pause $DIR/d31f D_c || return 1
2202         MULTIPID2=$!
2203
2204         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2205         wait $MULTIPID || error "first opendir $MULTIPID failed"
2206
2207         sleep 6
2208
2209         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2210         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2211         set +vx
2212 }
2213 run_test 31f "remove of open directory with open-unlink file ==="
2214
2215 test_31g() {
2216         echo "-- cross directory link --"
2217         test_mkdir -c1 $DIR/${tdir}ga
2218         test_mkdir -c1 $DIR/${tdir}gb
2219         touch $DIR/${tdir}ga/f
2220         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2221         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2222         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2223         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2224         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2225 }
2226 run_test 31g "cross directory link==============="
2227
2228 test_31h() {
2229         echo "-- cross directory link --"
2230         test_mkdir -c1 $DIR/${tdir}
2231         test_mkdir -c1 $DIR/${tdir}/dir
2232         touch $DIR/${tdir}/f
2233         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2234         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2235         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2236         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2237         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2238 }
2239 run_test 31h "cross directory link under child==============="
2240
2241 test_31i() {
2242         echo "-- cross directory link --"
2243         test_mkdir -c1 $DIR/$tdir
2244         test_mkdir -c1 $DIR/$tdir/dir
2245         touch $DIR/$tdir/dir/f
2246         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2247         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2248         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2249         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2250         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2251 }
2252 run_test 31i "cross directory link under parent==============="
2253
2254 test_31j() {
2255         test_mkdir -c1 -p $DIR/$tdir
2256         test_mkdir -c1 -p $DIR/$tdir/dir1
2257         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2258         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2259         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2260         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2261         return 0
2262 }
2263 run_test 31j "link for directory==============="
2264
2265 test_31k() {
2266         test_mkdir -c1 -p $DIR/$tdir
2267         touch $DIR/$tdir/s
2268         touch $DIR/$tdir/exist
2269         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2270         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2271         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2272         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2273         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2274         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2275         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2276         return 0
2277 }
2278 run_test 31k "link to file: the same, non-existing, dir==============="
2279
2280 test_31m() {
2281         mkdir $DIR/d31m
2282         touch $DIR/d31m/s
2283         mkdir $DIR/d31m2
2284         touch $DIR/d31m2/exist
2285         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2286         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2287         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2288         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2289         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2290         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2291         return 0
2292 }
2293 run_test 31m "link to file: the same, non-existing, dir==============="
2294
2295 test_31n() {
2296         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2297         nlink=$(stat --format=%h $DIR/$tfile)
2298         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2299         local fd=$(free_fd)
2300         local cmd="exec $fd<$DIR/$tfile"
2301         eval $cmd
2302         cmd="exec $fd<&-"
2303         trap "eval $cmd" EXIT
2304         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2305         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2306         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2307         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2308         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2309         eval $cmd
2310 }
2311 run_test 31n "check link count of unlinked file"
2312
2313 link_one() {
2314         local TEMPNAME=$(mktemp $1_XXXXXX)
2315         mlink $TEMPNAME $1 2> /dev/null &&
2316                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2317         munlink $TEMPNAME
2318 }
2319
2320 test_31o() { # LU-2901
2321         test_mkdir -p $DIR/$tdir
2322         for LOOP in $(seq 100); do
2323                 rm -f $DIR/$tdir/$tfile*
2324                 for THREAD in $(seq 8); do
2325                         link_one $DIR/$tdir/$tfile.$LOOP &
2326                 done
2327                 wait
2328                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2329                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2330                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2331                         break || true
2332         done
2333 }
2334 run_test 31o "duplicate hard links with same filename"
2335
2336 test_31p() {
2337         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2338
2339         test_mkdir $DIR/$tdir
2340         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2341         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2342
2343         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2344                 error "open unlink test1 failed"
2345         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2346                 error "open unlink test2 failed"
2347
2348         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2349                 error "test1 still exists"
2350         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2351                 error "test2 still exists"
2352 }
2353 run_test 31p "remove of open striped directory"
2354
2355 cleanup_test32_mount() {
2356         trap 0
2357         $UMOUNT $DIR/$tdir/ext2-mountpoint
2358 }
2359
2360 test_32a() {
2361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2362         echo "== more mountpoints and symlinks ================="
2363         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2364         trap cleanup_test32_mount EXIT
2365         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2366         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2367         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2368         cleanup_test32_mount
2369 }
2370 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2371
2372 test_32b() {
2373         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2374         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2375         trap cleanup_test32_mount EXIT
2376         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2377         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2378         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2379         cleanup_test32_mount
2380 }
2381 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2382
2383 test_32c() {
2384         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2385         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2386         trap cleanup_test32_mount EXIT
2387         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2388         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2389         test_mkdir -p $DIR/$tdir/d2/test_dir
2390         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2391         cleanup_test32_mount
2392 }
2393 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2394
2395 test_32d() {
2396         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2397         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2398         trap cleanup_test32_mount EXIT
2399         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2400         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2401         test_mkdir -p $DIR/$tdir/d2/test_dir
2402         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2403         cleanup_test32_mount
2404 }
2405 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2406
2407 test_32e() {
2408         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2409         test_mkdir -p $DIR/d32e/tmp
2410         TMP_DIR=$DIR/d32e/tmp
2411         ln -s $DIR/d32e $TMP_DIR/symlink11
2412         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2413         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2414         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2415 }
2416 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2417
2418 test_32f() {
2419         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2420         test_mkdir -p $DIR/d32f/tmp
2421         TMP_DIR=$DIR/d32f/tmp
2422         ln -s $DIR/d32f $TMP_DIR/symlink11
2423         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2424         ls $DIR/d32f/tmp/symlink11  || error
2425         ls $DIR/d32f/symlink01 || error
2426 }
2427 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2428
2429 test_32g() {
2430         TMP_DIR=$DIR/$tdir/tmp
2431         test_mkdir -p $DIR/$tdir/tmp
2432         test_mkdir $DIR/${tdir}2
2433         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2434         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2435         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2436         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2437         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2438         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2439 }
2440 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2441
2442 test_32h() {
2443         rm -fr $DIR/$tdir $DIR/${tdir}2
2444         TMP_DIR=$DIR/$tdir/tmp
2445         test_mkdir -p $DIR/$tdir/tmp
2446         test_mkdir $DIR/${tdir}2
2447         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2448         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2449         ls $TMP_DIR/symlink12 || error
2450         ls $DIR/$tdir/symlink02  || error
2451 }
2452 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2453
2454 test_32i() {
2455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2456         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2457         trap cleanup_test32_mount EXIT
2458         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2459         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2460         touch $DIR/$tdir/test_file
2461         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2462         cleanup_test32_mount
2463 }
2464 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2465
2466 test_32j() {
2467         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2468         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2469         trap cleanup_test32_mount EXIT
2470         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2471         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2472         touch $DIR/$tdir/test_file
2473         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2474         cleanup_test32_mount
2475 }
2476 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2477
2478 test_32k() {
2479         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2480         rm -fr $DIR/$tdir
2481         trap cleanup_test32_mount EXIT
2482         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2483         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2484         test_mkdir -p $DIR/$tdir/d2
2485         touch $DIR/$tdir/d2/test_file || error
2486         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2487         cleanup_test32_mount
2488 }
2489 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2490
2491 test_32l() {
2492         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2493         rm -fr $DIR/$tdir
2494         trap cleanup_test32_mount EXIT
2495         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2496         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2497         test_mkdir -p $DIR/$tdir/d2
2498         touch $DIR/$tdir/d2/test_file
2499         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2500         cleanup_test32_mount
2501 }
2502 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2503
2504 test_32m() {
2505         rm -fr $DIR/d32m
2506         test_mkdir -p $DIR/d32m/tmp
2507         TMP_DIR=$DIR/d32m/tmp
2508         ln -s $DIR $TMP_DIR/symlink11
2509         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2510         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2511         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2512 }
2513 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2514
2515 test_32n() {
2516         rm -fr $DIR/d32n
2517         test_mkdir -p $DIR/d32n/tmp
2518         TMP_DIR=$DIR/d32n/tmp
2519         ln -s $DIR $TMP_DIR/symlink11
2520         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2521         ls -l $DIR/d32n/tmp/symlink11  || error
2522         ls -l $DIR/d32n/symlink01 || error
2523 }
2524 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2525
2526 test_32o() {
2527         touch $DIR/$tfile
2528         test_mkdir -p $DIR/d32o/tmp
2529         TMP_DIR=$DIR/d32o/tmp
2530         ln -s $DIR/$tfile $TMP_DIR/symlink12
2531         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2532         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2533         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2534         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2535         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2536 }
2537 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2538
2539 test_32p() {
2540     log 32p_1
2541         rm -fr $DIR/d32p
2542     log 32p_2
2543         rm -f $DIR/$tfile
2544     log 32p_3
2545         touch $DIR/$tfile
2546     log 32p_4
2547         test_mkdir -p $DIR/d32p/tmp
2548     log 32p_5
2549         TMP_DIR=$DIR/d32p/tmp
2550     log 32p_6
2551         ln -s $DIR/$tfile $TMP_DIR/symlink12
2552     log 32p_7
2553         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2554     log 32p_8
2555         cat $DIR/d32p/tmp/symlink12 || error
2556     log 32p_9
2557         cat $DIR/d32p/symlink02 || error
2558     log 32p_10
2559 }
2560 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2561
2562 cleanup_testdir_mount() {
2563         trap 0
2564         $UMOUNT $DIR/$tdir
2565 }
2566
2567 test_32q() {
2568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2569         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2570         trap cleanup_testdir_mount EXIT
2571         test_mkdir -p $DIR/$tdir
2572         touch $DIR/$tdir/under_the_mount
2573         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2574         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2575         cleanup_testdir_mount
2576 }
2577 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2578
2579 test_32r() {
2580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2581         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2582         trap cleanup_testdir_mount EXIT
2583         test_mkdir -p $DIR/$tdir
2584         touch $DIR/$tdir/under_the_mount
2585         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2586         ls $DIR/$tdir | grep -q under_the_mount && error || true
2587         cleanup_testdir_mount
2588 }
2589 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2590
2591 test_33aa() {
2592         rm -f $DIR/$tfile
2593         touch $DIR/$tfile
2594         chmod 444 $DIR/$tfile
2595         chown $RUNAS_ID $DIR/$tfile
2596         log 33_1
2597         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2598         log 33_2
2599 }
2600 run_test 33aa "write file with mode 444 (should return error) ===="
2601
2602 test_33a() {
2603         rm -fr $DIR/d33
2604         test_mkdir -p $DIR/d33
2605         chown $RUNAS_ID $DIR/d33
2606         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2607         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2608                 error "open RDWR" || true
2609 }
2610 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2611
2612 test_33b() {
2613         rm -fr $DIR/d33
2614         test_mkdir -p $DIR/d33
2615         chown $RUNAS_ID $DIR/d33
2616         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2617 }
2618 run_test 33b "test open file with malformed flags (No panic)"
2619
2620 test_33c() {
2621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2622         local ostnum
2623         local ostname
2624         local write_bytes
2625         local all_zeros
2626
2627         remote_ost_nodsh && skip "remote OST with nodsh" && return
2628         all_zeros=:
2629         rm -fr $DIR/d33
2630         test_mkdir -p $DIR/d33
2631         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2632
2633         sync
2634         for ostnum in $(seq $OSTCOUNT); do
2635                 # test-framework's OST numbering is one-based, while Lustre's
2636                 # is zero-based
2637                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2638                 # Parsing llobdstat's output sucks; we could grep the /proc
2639                 # path, but that's likely to not be as portable as using the
2640                 # llobdstat utility.  So we parse lctl output instead.
2641                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2642                         obdfilter/$ostname/stats |
2643                         awk '/^write_bytes/ {print $7}' )
2644                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2645                 if (( ${write_bytes:-0} > 0 ))
2646                 then
2647                         all_zeros=false
2648                         break;
2649                 fi
2650         done
2651
2652         $all_zeros || return 0
2653
2654         # Write four bytes
2655         echo foo > $DIR/d33/bar
2656         # Really write them
2657         sync
2658
2659         # Total up write_bytes after writing.  We'd better find non-zeros.
2660         for ostnum in $(seq $OSTCOUNT); do
2661                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2662                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2663                         obdfilter/$ostname/stats |
2664                         awk '/^write_bytes/ {print $7}' )
2665                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2666                 if (( ${write_bytes:-0} > 0 ))
2667                 then
2668                         all_zeros=false
2669                         break;
2670                 fi
2671         done
2672
2673         if $all_zeros
2674         then
2675                 for ostnum in $(seq $OSTCOUNT); do
2676                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2677                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2678                         do_facet ost$ostnum lctl get_param -n \
2679                                 obdfilter/$ostname/stats
2680                 done
2681                 error "OST not keeping write_bytes stats (b22312)"
2682         fi
2683 }
2684 run_test 33c "test llobdstat and write_bytes"
2685
2686 test_33d() {
2687         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2688         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2689         local MDTIDX=1
2690         local remote_dir=$DIR/$tdir/remote_dir
2691
2692         test_mkdir -p $DIR/$tdir
2693         $LFS mkdir -i $MDTIDX $remote_dir ||
2694                 error "create remote directory failed"
2695
2696         touch $remote_dir/$tfile
2697         chmod 444 $remote_dir/$tfile
2698         chown $RUNAS_ID $remote_dir/$tfile
2699
2700         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2701
2702         chown $RUNAS_ID $remote_dir
2703         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2704                                         error "create" || true
2705         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2706                                     error "open RDWR" || true
2707         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2708 }
2709 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2710
2711 test_33e() {
2712         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2713
2714         mkdir $DIR/$tdir
2715
2716         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2717         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2718         mkdir $DIR/$tdir/local_dir
2719
2720         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2721         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2722         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2723
2724         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2725                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2726
2727         rmdir $DIR/$tdir/* || error "rmdir failed"
2728
2729         umask 777
2730         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2731         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2732         mkdir $DIR/$tdir/local_dir
2733
2734         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2735         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2736         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2737
2738         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2739                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2740
2741         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2742
2743         umask 000
2744         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2745         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2746         mkdir $DIR/$tdir/local_dir
2747
2748         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2749         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2750         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2751
2752         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2753                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2754 }
2755 run_test 33e "mkdir and striped directory should have same mode"
2756
2757 cleanup_33f() {
2758         trap 0
2759         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2760 }
2761
2762 test_33f() {
2763         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2764
2765         mkdir $DIR/$tdir
2766         chmod go+rwx $DIR/$tdir
2767         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2768         trap cleanup_33f EXIT
2769
2770         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2771                 error "cannot create striped directory"
2772
2773         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2774                 error "cannot create files in striped directory"
2775
2776         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2777                 error "cannot remove files in striped directory"
2778
2779         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2780                 error "cannot remove striped directory"
2781
2782         cleanup_33f
2783 }
2784 run_test 33f "nonroot user can create, access, and remove a striped directory"
2785
2786 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2787 test_34a() {
2788         rm -f $DIR/f34
2789         $MCREATE $DIR/f34 || error
2790         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2791         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2792         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2793         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2794 }
2795 run_test 34a "truncate file that has not been opened ==========="
2796
2797 test_34b() {
2798         [ ! -f $DIR/f34 ] && test_34a
2799         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2800         $OPENFILE -f O_RDONLY $DIR/f34
2801         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2802         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2803 }
2804 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2805
2806 test_34c() {
2807         [ ! -f $DIR/f34 ] && test_34a
2808         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2809         $OPENFILE -f O_RDWR $DIR/f34
2810         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2811         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2812 }
2813 run_test 34c "O_RDWR opening file-with-size works =============="
2814
2815 test_34d() {
2816         [ ! -f $DIR/f34 ] && test_34a
2817         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2818         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2819         rm $DIR/f34
2820 }
2821 run_test 34d "write to sparse file ============================="
2822
2823 test_34e() {
2824         rm -f $DIR/f34e
2825         $MCREATE $DIR/f34e || error
2826         $TRUNCATE $DIR/f34e 1000 || error
2827         $CHECKSTAT -s 1000 $DIR/f34e || error
2828         $OPENFILE -f O_RDWR $DIR/f34e
2829         $CHECKSTAT -s 1000 $DIR/f34e || error
2830 }
2831 run_test 34e "create objects, some with size and some without =="
2832
2833 test_34f() { # bug 6242, 6243
2834         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2835         SIZE34F=48000
2836         rm -f $DIR/f34f
2837         $MCREATE $DIR/f34f || error
2838         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2839         dd if=$DIR/f34f of=$TMP/f34f
2840         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2841         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2842         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2843         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2844         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2845 }
2846 run_test 34f "read from a file with no objects until EOF ======="
2847
2848 test_34g() {
2849         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2850         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2851         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2852         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2853         cancel_lru_locks osc
2854         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2855                 error "wrong size after lock cancel"
2856
2857         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2858         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2859                 error "expanding truncate failed"
2860         cancel_lru_locks osc
2861         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2862                 error "wrong expanded size after lock cancel"
2863 }
2864 run_test 34g "truncate long file ==============================="
2865
2866 test_34h() {
2867         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2868         local gid=10
2869         local sz=1000
2870
2871         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2872         sync # Flush the cache so that multiop below does not block on cache
2873              # flush when getting the group lock
2874         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2875         MULTIPID=$!
2876
2877         # Since just timed wait is not good enough, let's do a sync write
2878         # that way we are sure enough time for a roundtrip + processing
2879         # passed + 2 seconds of extra margin.
2880         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2881         rm $DIR/${tfile}-1
2882         sleep 2
2883
2884         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2885                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2886                 kill -9 $MULTIPID
2887         fi
2888         wait $MULTIPID
2889         local nsz=`stat -c %s $DIR/$tfile`
2890         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2891 }
2892 run_test 34h "ftruncate file under grouplock should not block"
2893
2894 test_35a() {
2895         cp /bin/sh $DIR/f35a
2896         chmod 444 $DIR/f35a
2897         chown $RUNAS_ID $DIR/f35a
2898         $RUNAS $DIR/f35a && error || true
2899         rm $DIR/f35a
2900 }
2901 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2902
2903 test_36a() {
2904         rm -f $DIR/f36
2905         utime $DIR/f36 || error
2906 }
2907 run_test 36a "MDS utime check (mknod, utime) ==================="
2908
2909 test_36b() {
2910         echo "" > $DIR/f36
2911         utime $DIR/f36 || error
2912 }
2913 run_test 36b "OST utime check (open, utime) ===================="
2914
2915 test_36c() {
2916         rm -f $DIR/d36/f36
2917         test_mkdir $DIR/d36
2918         chown $RUNAS_ID $DIR/d36
2919         $RUNAS utime $DIR/d36/f36 || error
2920 }
2921 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2922
2923 test_36d() {
2924         [ ! -d $DIR/d36 ] && test_36c
2925         echo "" > $DIR/d36/f36
2926         $RUNAS utime $DIR/d36/f36 || error
2927 }
2928 run_test 36d "non-root OST utime check (open, utime) ==========="
2929
2930 test_36e() {
2931         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2932         test_mkdir -p $DIR/$tdir
2933         touch $DIR/$tdir/$tfile
2934         $RUNAS utime $DIR/$tdir/$tfile && \
2935                 error "utime worked, expected failure" || true
2936 }
2937 run_test 36e "utime on non-owned file (should return error) ===="
2938
2939 subr_36fh() {
2940         local fl="$1"
2941         local LANG_SAVE=$LANG
2942         local LC_LANG_SAVE=$LC_LANG
2943         export LANG=C LC_LANG=C # for date language
2944
2945         DATESTR="Dec 20  2000"
2946         test_mkdir -p $DIR/$tdir
2947         lctl set_param fail_loc=$fl
2948         date; date +%s
2949         cp /etc/hosts $DIR/$tdir/$tfile
2950         sync & # write RPC generated with "current" inode timestamp, but delayed
2951         sleep 1
2952         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2953         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2954         cancel_lru_locks osc
2955         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2956         date; date +%s
2957         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2958                 echo "BEFORE: $LS_BEFORE" && \
2959                 echo "AFTER : $LS_AFTER" && \
2960                 echo "WANT  : $DATESTR" && \
2961                 error "$DIR/$tdir/$tfile timestamps changed" || true
2962
2963         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2964 }
2965
2966 test_36f() {
2967         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2968         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2969         subr_36fh "0x80000214"
2970 }
2971 run_test 36f "utime on file racing with OST BRW write =========="
2972
2973 test_36g() {
2974         remote_ost_nodsh && skip "remote OST with nodsh" && return
2975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2976         local fmd_max_age
2977         local fmd_before
2978         local fmd_after
2979
2980         test_mkdir -p $DIR/$tdir
2981         fmd_max_age=$(do_facet ost1 \
2982                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2983                 head -n 1")
2984
2985         fmd_before=$(do_facet ost1 \
2986                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2987         touch $DIR/$tdir/$tfile
2988         sleep $((fmd_max_age + 12))
2989         fmd_after=$(do_facet ost1 \
2990                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2991
2992         echo "fmd_before: $fmd_before"
2993         echo "fmd_after: $fmd_after"
2994         [[ $fmd_after -gt $fmd_before ]] &&
2995                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2996                 error "fmd didn't expire after ping" || true
2997 }
2998 run_test 36g "filter mod data cache expiry ====================="
2999
3000 test_36h() {
3001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3002         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3003         subr_36fh "0x80000227"
3004 }
3005 run_test 36h "utime on file racing with OST BRW write =========="
3006
3007 test_36i() {
3008         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3009
3010         test_mkdir $DIR/$tdir
3011         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3012
3013         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3014         local new_mtime=$((mtime + 200))
3015
3016         #change Modify time of striped dir
3017         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3018                         error "change mtime failed"
3019
3020         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3021
3022         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3023 }
3024 run_test 36i "change mtime on striped directory"
3025
3026 # test_37 - duplicate with tests 32q 32r
3027
3028 test_38() {
3029         local file=$DIR/$tfile
3030         touch $file
3031         openfile -f O_DIRECTORY $file
3032         local RC=$?
3033         local ENOTDIR=20
3034         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3035         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3036 }
3037 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3038
3039 test_39() {
3040         touch $DIR/$tfile
3041         touch $DIR/${tfile}2
3042 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3043 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3044 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3045         sleep 2
3046         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3047         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3048                 echo "mtime"
3049                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3050                 echo "atime"
3051                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3052                 echo "ctime"
3053                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3054                 error "O_TRUNC didn't change timestamps"
3055         fi
3056 }
3057 run_test 39 "mtime changed on create ==========================="
3058
3059 test_39b() {
3060         test_mkdir -p -c1 $DIR/$tdir
3061         cp -p /etc/passwd $DIR/$tdir/fopen
3062         cp -p /etc/passwd $DIR/$tdir/flink
3063         cp -p /etc/passwd $DIR/$tdir/funlink
3064         cp -p /etc/passwd $DIR/$tdir/frename
3065         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3066
3067         sleep 1
3068         echo "aaaaaa" >> $DIR/$tdir/fopen
3069         echo "aaaaaa" >> $DIR/$tdir/flink
3070         echo "aaaaaa" >> $DIR/$tdir/funlink
3071         echo "aaaaaa" >> $DIR/$tdir/frename
3072
3073         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3074         local link_new=`stat -c %Y $DIR/$tdir/flink`
3075         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3076         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3077
3078         cat $DIR/$tdir/fopen > /dev/null
3079         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3080         rm -f $DIR/$tdir/funlink2
3081         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3082
3083         for (( i=0; i < 2; i++ )) ; do
3084                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3085                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3086                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3087                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3088
3089                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3090                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3091                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3092                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3093
3094                 cancel_lru_locks osc
3095                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3096         done
3097 }
3098 run_test 39b "mtime change on open, link, unlink, rename  ======"
3099
3100 # this should be set to past
3101 TEST_39_MTIME=`date -d "1 year ago" +%s`
3102
3103 # bug 11063
3104 test_39c() {
3105         touch $DIR1/$tfile
3106         sleep 2
3107         local mtime0=`stat -c %Y $DIR1/$tfile`
3108
3109         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3110         local mtime1=`stat -c %Y $DIR1/$tfile`
3111         [ "$mtime1" = $TEST_39_MTIME ] || \
3112                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3113
3114         local d1=`date +%s`
3115         echo hello >> $DIR1/$tfile
3116         local d2=`date +%s`
3117         local mtime2=`stat -c %Y $DIR1/$tfile`
3118         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3119                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3120
3121         mv $DIR1/$tfile $DIR1/$tfile-1
3122
3123         for (( i=0; i < 2; i++ )) ; do
3124                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3125                 [ "$mtime2" = "$mtime3" ] || \
3126                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3127
3128                 cancel_lru_locks osc
3129                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3130         done
3131 }
3132 run_test 39c "mtime change on rename ==========================="
3133
3134 # bug 21114
3135 test_39d() {
3136         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3137         touch $DIR1/$tfile
3138
3139         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3140
3141         for (( i=0; i < 2; i++ )) ; do
3142                 local mtime=`stat -c %Y $DIR1/$tfile`
3143                 [ $mtime = $TEST_39_MTIME ] || \
3144                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3145
3146                 cancel_lru_locks osc
3147                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3148         done
3149 }
3150 run_test 39d "create, utime, stat =============================="
3151
3152 # bug 21114
3153 test_39e() {
3154         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3155         touch $DIR1/$tfile
3156         local mtime1=`stat -c %Y $DIR1/$tfile`
3157
3158         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3159
3160         for (( i=0; i < 2; i++ )) ; do
3161                 local mtime2=`stat -c %Y $DIR1/$tfile`
3162                 [ $mtime2 = $TEST_39_MTIME ] || \
3163                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3164
3165                 cancel_lru_locks osc
3166                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3167         done
3168 }
3169 run_test 39e "create, stat, utime, stat ========================"
3170
3171 # bug 21114
3172 test_39f() {
3173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3174         touch $DIR1/$tfile
3175         mtime1=`stat -c %Y $DIR1/$tfile`
3176
3177         sleep 2
3178         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3179
3180         for (( i=0; i < 2; i++ )) ; do
3181                 local mtime2=`stat -c %Y $DIR1/$tfile`
3182                 [ $mtime2 = $TEST_39_MTIME ] || \
3183                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3184
3185                 cancel_lru_locks osc
3186                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3187         done
3188 }
3189 run_test 39f "create, stat, sleep, utime, stat ================="
3190
3191 # bug 11063
3192 test_39g() {
3193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3194         echo hello >> $DIR1/$tfile
3195         local mtime1=`stat -c %Y $DIR1/$tfile`
3196
3197         sleep 2
3198         chmod o+r $DIR1/$tfile
3199
3200         for (( i=0; i < 2; i++ )) ; do
3201                 local mtime2=`stat -c %Y $DIR1/$tfile`
3202                 [ "$mtime1" = "$mtime2" ] || \
3203                         error "lost mtime: $mtime2, should be $mtime1"
3204
3205                 cancel_lru_locks osc
3206                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3207         done
3208 }
3209 run_test 39g "write, chmod, stat ==============================="
3210
3211 # bug 11063
3212 test_39h() {
3213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3214         touch $DIR1/$tfile
3215         sleep 1
3216
3217         local d1=`date`
3218         echo hello >> $DIR1/$tfile
3219         local mtime1=`stat -c %Y $DIR1/$tfile`
3220
3221         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3222         local d2=`date`
3223         if [ "$d1" != "$d2" ]; then
3224                 echo "write and touch not within one second"
3225         else
3226                 for (( i=0; i < 2; i++ )) ; do
3227                         local mtime2=`stat -c %Y $DIR1/$tfile`
3228                         [ "$mtime2" = $TEST_39_MTIME ] || \
3229                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3230
3231                         cancel_lru_locks osc
3232                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3233                 done
3234         fi
3235 }
3236 run_test 39h "write, utime within one second, stat ============="
3237
3238 test_39i() {
3239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3240         touch $DIR1/$tfile
3241         sleep 1
3242
3243         echo hello >> $DIR1/$tfile
3244         local mtime1=`stat -c %Y $DIR1/$tfile`
3245
3246         mv $DIR1/$tfile $DIR1/$tfile-1
3247
3248         for (( i=0; i < 2; i++ )) ; do
3249                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3250
3251                 [ "$mtime1" = "$mtime2" ] || \
3252                         error "lost mtime: $mtime2, should be $mtime1"
3253
3254                 cancel_lru_locks osc
3255                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3256         done
3257 }
3258 run_test 39i "write, rename, stat =============================="
3259
3260 test_39j() {
3261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3262         start_full_debug_logging
3263         touch $DIR1/$tfile
3264         sleep 1
3265
3266         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3267         lctl set_param fail_loc=0x80000412
3268         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3269                 error "multiop failed"
3270         local multipid=$!
3271         local mtime1=`stat -c %Y $DIR1/$tfile`
3272
3273         mv $DIR1/$tfile $DIR1/$tfile-1
3274
3275         kill -USR1 $multipid
3276         wait $multipid || error "multiop close failed"
3277
3278         for (( i=0; i < 2; i++ )) ; do
3279                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3280                 [ "$mtime1" = "$mtime2" ] ||
3281                         error "mtime is lost on close: $mtime2, " \
3282                               "should be $mtime1"
3283
3284                 cancel_lru_locks osc
3285                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3286         done
3287         lctl set_param fail_loc=0
3288         stop_full_debug_logging
3289 }
3290 run_test 39j "write, rename, close, stat ======================="
3291
3292 test_39k() {
3293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3294         touch $DIR1/$tfile
3295         sleep 1
3296
3297         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3298         local multipid=$!
3299         local mtime1=`stat -c %Y $DIR1/$tfile`
3300
3301         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3302
3303         kill -USR1 $multipid
3304         wait $multipid || error "multiop close failed"
3305
3306         for (( i=0; i < 2; i++ )) ; do
3307                 local mtime2=`stat -c %Y $DIR1/$tfile`
3308
3309                 [ "$mtime2" = $TEST_39_MTIME ] || \
3310                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3311
3312                 cancel_lru_locks osc
3313                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3314         done
3315 }
3316 run_test 39k "write, utime, close, stat ========================"
3317
3318 # this should be set to future
3319 TEST_39_ATIME=`date -d "1 year" +%s`
3320
3321 test_39l() {
3322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3323         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3324         local atime_diff=$(do_facet $SINGLEMDS \
3325                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3326         rm -rf $DIR/$tdir
3327         mkdir -p $DIR/$tdir
3328
3329         # test setting directory atime to future
3330         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3331         local atime=$(stat -c %X $DIR/$tdir)
3332         [ "$atime" = $TEST_39_ATIME ] || \
3333                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3334
3335         # test setting directory atime from future to now
3336         local d1=$(date +%s)
3337         ls $DIR/$tdir
3338         local d2=$(date +%s)
3339
3340         cancel_lru_locks mdc
3341         atime=$(stat -c %X $DIR/$tdir)
3342         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3343                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3344
3345         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3346         sleep 3
3347
3348         # test setting directory atime when now > dir atime + atime_diff
3349         d1=$(date +%s)
3350         ls $DIR/$tdir
3351         d2=$(date +%s)
3352         cancel_lru_locks mdc
3353         atime=$(stat -c %X $DIR/$tdir)
3354         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3355                 error "atime is not updated  : $atime, should be $d2"
3356
3357         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3358         sleep 3
3359
3360         # test not setting directory atime when now < dir atime + atime_diff
3361         ls $DIR/$tdir
3362         cancel_lru_locks mdc
3363         atime=$(stat -c %X $DIR/$tdir)
3364         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3365                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3366
3367         do_facet $SINGLEMDS \
3368                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3369 }
3370 run_test 39l "directory atime update ==========================="
3371
3372 test_39m() {
3373         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3374         touch $DIR1/$tfile
3375         sleep 2
3376         local far_past_mtime=$(date -d "May 29 1953" +%s)
3377         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3378
3379         touch -m -d @$far_past_mtime $DIR1/$tfile
3380         touch -a -d @$far_past_atime $DIR1/$tfile
3381
3382         for (( i=0; i < 2; i++ )) ; do
3383                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3384                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3385                         error "atime or mtime set incorrectly"
3386
3387                 cancel_lru_locks osc
3388                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3389         done
3390 }
3391 run_test 39m "test atime and mtime before 1970"
3392
3393 test_39n() { # LU-3832
3394         local atime_diff=$(do_facet $SINGLEMDS \
3395                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3396         local atime0
3397         local atime1
3398         local atime2
3399
3400         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3401
3402         rm -rf $DIR/$tfile
3403         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3404         atime0=$(stat -c %X $DIR/$tfile)
3405
3406         sleep 5
3407         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3408         atime1=$(stat -c %X $DIR/$tfile)
3409
3410         sleep 5
3411         cancel_lru_locks mdc
3412         cancel_lru_locks osc
3413         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3414         atime2=$(stat -c %X $DIR/$tfile)
3415
3416         do_facet $SINGLEMDS \
3417                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3418
3419         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3420         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3421 }
3422 run_test 39n "check that O_NOATIME is honored"
3423
3424 test_39o() {
3425         TESTDIR=$DIR/$tdir/$tfile
3426         [ -e $TESTDIR ] && rm -rf $TESTDIR
3427         mkdir -p $TESTDIR
3428         cd $TESTDIR
3429         links1=2
3430         ls
3431         mkdir a b
3432         ls
3433         links2=$(stat -c %h .)
3434         [ $(($links1 + 2)) != $links2 ] &&
3435                 error "wrong links count $(($links1 + 2)) != $links2"
3436         rmdir b
3437         links3=$(stat -c %h .)
3438         [ $(($links1 + 1)) != $links3 ] &&
3439                 error "wrong links count $links1 != $links3"
3440         return 0
3441 }
3442 run_test 39o "directory cached attributes updated after create ========"
3443
3444 test_39p() {
3445         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3446         local MDTIDX=1
3447         TESTDIR=$DIR/$tdir/$tfile
3448         [ -e $TESTDIR ] && rm -rf $TESTDIR
3449         test_mkdir -p $TESTDIR
3450         cd $TESTDIR
3451         links1=2
3452         ls
3453         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3454         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3455         ls
3456         links2=$(stat -c %h .)
3457         [ $(($links1 + 2)) != $links2 ] &&
3458                 error "wrong links count $(($links1 + 2)) != $links2"
3459         rmdir remote_dir2
3460         links3=$(stat -c %h .)
3461         [ $(($links1 + 1)) != $links3 ] &&
3462                 error "wrong links count $links1 != $links3"
3463         return 0
3464 }
3465 run_test 39p "remote directory cached attributes updated after create ========"
3466
3467
3468 test_40() {
3469         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3470         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3471                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3472         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3473                 error "$tfile is not 4096 bytes in size"
3474 }
3475 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3476
3477 test_41() {
3478         # bug 1553
3479         small_write $DIR/f41 18
3480 }
3481 run_test 41 "test small file write + fstat ====================="
3482
3483 count_ost_writes() {
3484         lctl get_param -n osc.*.stats |
3485                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3486                         END { printf("%0.0f", writes) }'
3487 }
3488
3489 # decent default
3490 WRITEBACK_SAVE=500
3491 DIRTY_RATIO_SAVE=40
3492 MAX_DIRTY_RATIO=50
3493 BG_DIRTY_RATIO_SAVE=10
3494 MAX_BG_DIRTY_RATIO=25
3495
3496 start_writeback() {
3497         trap 0
3498         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3499         # dirty_ratio, dirty_background_ratio
3500         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3501                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3502                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3503                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3504         else
3505                 # if file not here, we are a 2.4 kernel
3506                 kill -CONT `pidof kupdated`
3507         fi
3508 }
3509
3510 stop_writeback() {
3511         # setup the trap first, so someone cannot exit the test at the
3512         # exact wrong time and mess up a machine
3513         trap start_writeback EXIT
3514         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3515         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3516                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3517                 sysctl -w vm.dirty_writeback_centisecs=0
3518                 sysctl -w vm.dirty_writeback_centisecs=0
3519                 # save and increase /proc/sys/vm/dirty_ratio
3520                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3521                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3522                 # save and increase /proc/sys/vm/dirty_background_ratio
3523                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3524                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3525         else
3526                 # if file not here, we are a 2.4 kernel
3527                 kill -STOP `pidof kupdated`
3528         fi
3529 }
3530
3531 # ensure that all stripes have some grant before we test client-side cache
3532 setup_test42() {
3533         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3534                 dd if=/dev/zero of=$i bs=4k count=1
3535                 rm $i
3536         done
3537 }
3538
3539 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3540 # file truncation, and file removal.
3541 test_42a() {
3542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3543         setup_test42
3544         cancel_lru_locks osc
3545         stop_writeback
3546         sync; sleep 1; sync # just to be safe
3547         BEFOREWRITES=`count_ost_writes`
3548         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3549         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3550         AFTERWRITES=`count_ost_writes`
3551         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3552                 error "$BEFOREWRITES < $AFTERWRITES"
3553         start_writeback
3554 }
3555 run_test 42a "ensure that we don't flush on close =============="
3556
3557 test_42b() {
3558         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3559         setup_test42
3560         cancel_lru_locks osc
3561         stop_writeback
3562         sync
3563         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3564         BEFOREWRITES=$(count_ost_writes)
3565         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3566         AFTERWRITES=$(count_ost_writes)
3567         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3568                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3569         fi
3570         BEFOREWRITES=$(count_ost_writes)
3571         sync || error "sync: $?"
3572         AFTERWRITES=$(count_ost_writes)
3573         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3574                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3575         fi
3576         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3577         start_writeback
3578         return 0
3579 }
3580 run_test 42b "test destroy of file with cached dirty data ======"
3581
3582 # if these tests just want to test the effect of truncation,
3583 # they have to be very careful.  consider:
3584 # - the first open gets a {0,EOF}PR lock
3585 # - the first write conflicts and gets a {0, count-1}PW
3586 # - the rest of the writes are under {count,EOF}PW
3587 # - the open for truncate tries to match a {0,EOF}PR
3588 #   for the filesize and cancels the PWs.
3589 # any number of fixes (don't get {0,EOF} on open, match
3590 # composite locks, do smarter file size management) fix
3591 # this, but for now we want these tests to verify that
3592 # the cancellation with truncate intent works, so we
3593 # start the file with a full-file pw lock to match against
3594 # until the truncate.
3595 trunc_test() {
3596         test=$1
3597         file=$DIR/$test
3598         offset=$2
3599         cancel_lru_locks osc
3600         stop_writeback
3601         # prime the file with 0,EOF PW to match
3602         touch $file
3603         $TRUNCATE $file 0
3604         sync; sync
3605         # now the real test..
3606         dd if=/dev/zero of=$file bs=1024 count=100
3607         BEFOREWRITES=`count_ost_writes`
3608         $TRUNCATE $file $offset
3609         cancel_lru_locks osc
3610         AFTERWRITES=`count_ost_writes`
3611         start_writeback
3612 }
3613
3614 test_42c() {
3615         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3616         trunc_test 42c 1024
3617         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3618             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3619         rm $file
3620 }
3621 run_test 42c "test partial truncate of file with cached dirty data"
3622
3623 test_42d() {
3624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3625         trunc_test 42d 0
3626         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3627             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3628         rm $file
3629 }
3630 run_test 42d "test complete truncate of file with cached dirty data"
3631
3632 test_42e() { # bug22074
3633         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3634         local TDIR=$DIR/${tdir}e
3635         local pagesz=$(page_size)
3636         local pages=16 # hardcoded 16 pages, don't change it.
3637         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3638         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3639         local max_dirty_mb
3640         local warmup_files
3641
3642         test_mkdir -p $DIR/${tdir}e
3643         $SETSTRIPE -c 1 $TDIR
3644         createmany -o $TDIR/f $files
3645
3646         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3647
3648         # we assume that with $OSTCOUNT files, at least one of them will
3649         # be allocated on OST0.
3650         warmup_files=$((OSTCOUNT * max_dirty_mb))
3651         createmany -o $TDIR/w $warmup_files
3652
3653         # write a large amount of data into one file and sync, to get good
3654         # avail_grant number from OST.
3655         for ((i=0; i<$warmup_files; i++)); do
3656                 idx=$($GETSTRIPE -i $TDIR/w$i)
3657                 [ $idx -ne 0 ] && continue
3658                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3659                 break
3660         done
3661         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3662         sync
3663         $LCTL get_param $proc_osc0/cur_dirty_bytes
3664         $LCTL get_param $proc_osc0/cur_grant_bytes
3665
3666         # create as much dirty pages as we can while not to trigger the actual
3667         # RPCs directly. but depends on the env, VFS may trigger flush during this
3668         # period, hopefully we are good.
3669         for ((i=0; i<$warmup_files; i++)); do
3670                 idx=$($GETSTRIPE -i $TDIR/w$i)
3671                 [ $idx -ne 0 ] && continue
3672                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3673         done
3674         $LCTL get_param $proc_osc0/cur_dirty_bytes
3675         $LCTL get_param $proc_osc0/cur_grant_bytes
3676
3677         # perform the real test
3678         $LCTL set_param $proc_osc0/rpc_stats 0
3679         for ((;i<$files; i++)); do
3680                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3681                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3682         done
3683         sync
3684         $LCTL get_param $proc_osc0/rpc_stats
3685
3686         local percent=0
3687         local have_ppr=false
3688         $LCTL get_param $proc_osc0/rpc_stats |
3689                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3690                         # skip lines until we are at the RPC histogram data
3691                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3692                         $have_ppr || continue
3693
3694                         # we only want the percent stat for < 16 pages
3695                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3696
3697                         percent=$((percent + WPCT))
3698                         if [[ $percent -gt 15 ]]; then
3699                                 error "less than 16-pages write RPCs" \
3700                                       "$percent% > 15%"
3701                                 break
3702                         fi
3703                 done
3704         rm -rf $TDIR
3705 }
3706 run_test 42e "verify sub-RPC writes are not done synchronously"
3707
3708 test_43() {
3709         test_mkdir -p $DIR/$tdir
3710         cp -p /bin/ls $DIR/$tdir/$tfile
3711         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3712         pid=$!
3713         # give multiop a chance to open
3714         sleep 1
3715
3716         $DIR/$tdir/$tfile && error || true
3717         kill -USR1 $pid
3718 }
3719 run_test 43 "execution of file opened for write should return -ETXTBSY"
3720
3721 test_43a() {
3722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3723         test_mkdir -p $DIR/$tdir
3724         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3725                         cp -p multiop $DIR/$tdir/multiop
3726         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3727                         return 1
3728         MULTIOP_PID=$!
3729         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3730         kill -USR1 $MULTIOP_PID || return 2
3731         wait $MULTIOP_PID || return 3
3732         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3733 }
3734 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3735
3736 test_43b() {
3737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3738         test_mkdir -p $DIR/$tdir
3739         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3740                         cp -p multiop $DIR/$tdir/multiop
3741         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3742                         return 1
3743         MULTIOP_PID=$!
3744         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3745         kill -USR1 $MULTIOP_PID || return 2
3746         wait $MULTIOP_PID || return 3
3747         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3748 }
3749 run_test 43b "truncate of file being executed should return -ETXTBSY"
3750
3751 test_43c() {
3752         local testdir="$DIR/$tdir"
3753         test_mkdir -p $DIR/$tdir
3754         cp $SHELL $testdir/
3755         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3756                 ( cd $testdir && md5sum -c)
3757 }
3758 run_test 43c "md5sum of copy into lustre========================"
3759
3760 test_44() {
3761         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3762         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3763         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3764 }
3765 run_test 44 "zero length read from a sparse stripe ============="
3766
3767 test_44a() {
3768         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3769                 awk '{ print $2 }')
3770         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3771         [[ $nstripe -gt $OSTCOUNT ]] &&
3772             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3773             return
3774         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3775                 awk '{ print $2 }')
3776         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3777                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3778                         awk '{ print $2 }')
3779         fi
3780
3781         OFFSETS="0 $((stride/2)) $((stride-1))"
3782         for offset in $OFFSETS; do
3783                 for i in $(seq 0 $((nstripe-1))); do
3784                         local GLOBALOFFSETS=""
3785                         # size in Bytes
3786                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3787                         local myfn=$DIR/d44a-$size
3788                         echo "--------writing $myfn at $size"
3789                         ll_sparseness_write $myfn $size ||
3790                                 error "ll_sparseness_write"
3791                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3792                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3793                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3794
3795                         for j in $(seq 0 $((nstripe-1))); do
3796                                 # size in Bytes
3797                                 size=$((((j + $nstripe )*$stride + $offset)))
3798                                 ll_sparseness_write $myfn $size ||
3799                                         error "ll_sparseness_write"
3800                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3801                         done
3802                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3803                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3804                         rm -f $myfn
3805                 done
3806         done
3807 }
3808 run_test 44a "test sparse pwrite ==============================="
3809
3810 dirty_osc_total() {
3811         tot=0
3812         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3813                 tot=$(($tot + $d))
3814         done
3815         echo $tot
3816 }
3817 do_dirty_record() {
3818         before=`dirty_osc_total`
3819         echo executing "\"$*\""
3820         eval $*
3821         after=`dirty_osc_total`
3822         echo before $before, after $after
3823 }
3824 test_45() {
3825         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3826         f="$DIR/f45"
3827         # Obtain grants from OST if it supports it
3828         echo blah > ${f}_grant
3829         stop_writeback
3830         sync
3831         do_dirty_record "echo blah > $f"
3832         [[ $before -eq $after ]] && error "write wasn't cached"
3833         do_dirty_record "> $f"
3834         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3835         do_dirty_record "echo blah > $f"
3836         [[ $before -eq $after ]] && error "write wasn't cached"
3837         do_dirty_record "sync"
3838         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3839         do_dirty_record "echo blah > $f"
3840         [[ $before -eq $after ]] && error "write wasn't cached"
3841         do_dirty_record "cancel_lru_locks osc"
3842         [[ $before -gt $after ]] ||
3843                 error "lock cancellation didn't lower dirty count"
3844         start_writeback
3845 }
3846 run_test 45 "osc io page accounting ============================"
3847
3848 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3849 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3850 # objects offset and an assert hit when an rpc was built with 1023's mapped
3851 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3852 test_46() {
3853         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3854         f="$DIR/f46"
3855         stop_writeback
3856         sync
3857         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3858         sync
3859         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3860         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3861         sync
3862         start_writeback
3863 }
3864 run_test 46 "dirtying a previously written page ================"
3865
3866 # test_47 is removed "Device nodes check" is moved to test_28
3867
3868 test_48a() { # bug 2399
3869         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3870         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3871                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3872                 return
3873         test_mkdir $DIR/$tdir
3874         cd $DIR/$tdir
3875         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3876         test_mkdir $DIR/$tdir || error "recreate directory failed"
3877         touch foo || error "'touch foo' failed after recreating cwd"
3878         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
3879         touch .foo || error "'touch .foo' failed after recreating cwd"
3880         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3881         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3882         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3883         cd . || error "'cd .' failed after recreating cwd"
3884         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3885         rmdir . && error "'rmdir .' worked after recreating cwd"
3886         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3887         cd .. || error "'cd ..' failed after recreating cwd"
3888 }
3889 run_test 48a "Access renamed working dir (should return errors)="
3890
3891 test_48b() { # bug 2399
3892         rm -rf $DIR/$tdir
3893         test_mkdir $DIR/$tdir
3894         cd $DIR/$tdir
3895         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3896         touch foo && error "'touch foo' worked after removing cwd"
3897         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3898         touch .foo && error "'touch .foo' worked after removing cwd"
3899         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3900         ls . > /dev/null && error "'ls .' worked after removing cwd"
3901         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3902         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3903         test_mkdir . && error "'mkdir .' worked after removing cwd"
3904         rmdir . && error "'rmdir .' worked after removing cwd"
3905         ln -s . foo && error "'ln -s .' worked after removing cwd"
3906         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3907 }
3908 run_test 48b "Access removed working dir (should return errors)="
3909
3910 test_48c() { # bug 2350
3911         #lctl set_param debug=-1
3912         #set -vx
3913         rm -rf $DIR/$tdir
3914         test_mkdir -p $DIR/$tdir/dir
3915         cd $DIR/$tdir/dir
3916         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3917         $TRACE touch foo && error "touch foo worked after removing cwd"
3918         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3919         touch .foo && error "touch .foo worked after removing cwd"
3920         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3921         $TRACE ls . && error "'ls .' worked after removing cwd"
3922         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3923         is_patchless || ( $TRACE cd . &&
3924                         error "'cd .' worked after removing cwd" )
3925         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3926         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3927         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3928         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3929 }
3930 run_test 48c "Access removed working subdir (should return errors)"
3931
3932 test_48d() { # bug 2350
3933         #lctl set_param debug=-1
3934         #set -vx
3935         rm -rf $DIR/$tdir
3936         test_mkdir -p $DIR/$tdir/dir
3937         cd $DIR/$tdir/dir
3938         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3939         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3940         $TRACE touch foo && error "'touch foo' worked after removing parent"
3941         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3942         touch .foo && error "'touch .foo' worked after removing parent"
3943         test_mkdir .foo && error "mkdir .foo worked after removing parent"
3944         $TRACE ls . && error "'ls .' worked after removing parent"
3945         $TRACE ls .. && error "'ls ..' worked after removing parent"
3946         is_patchless || ( $TRACE cd . &&
3947                         error "'cd .' worked after recreate parent" )
3948         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3949         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3950         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3951         is_patchless || ( $TRACE cd .. &&
3952                         error "'cd ..' worked after removing parent" || true )
3953 }
3954 run_test 48d "Access removed parent subdir (should return errors)"
3955
3956 test_48e() { # bug 4134
3957         #lctl set_param debug=-1
3958         #set -vx
3959         rm -rf $DIR/$tdir
3960         test_mkdir -p $DIR/$tdir/dir
3961         cd $DIR/$tdir/dir
3962         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3963         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3964         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3965         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3966         # On a buggy kernel addition of "touch foo" after cd .. will
3967         # produce kernel oops in lookup_hash_it
3968         touch ../foo && error "'cd ..' worked after recreate parent"
3969         cd $DIR
3970         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3971 }
3972 run_test 48e "Access to recreated parent subdir (should return errors)"
3973
3974 test_49() { # LU-1030
3975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3976         # get ost1 size - lustre-OST0000
3977         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3978                 awk '{ print $4 }')
3979         # write 800M at maximum
3980         [[ $ost1_size -lt 2 ]] && ost1_size=2
3981         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3982
3983         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3984         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3985         local dd_pid=$!
3986
3987         # change max_pages_per_rpc while writing the file
3988         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3989         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3990         # loop until dd process exits
3991         while ps ax -opid | grep -wq $dd_pid; do
3992                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3993                 sleep $((RANDOM % 5 + 1))
3994         done
3995         # restore original max_pages_per_rpc
3996         $LCTL set_param $osc1_mppc=$orig_mppc
3997         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3998 }
3999 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4000
4001 test_50() {
4002         # bug 1485
4003         test_mkdir $DIR/$tdir
4004         cd $DIR/$tdir
4005         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4006 }
4007 run_test 50 "special situations: /proc symlinks  ==============="
4008
4009 test_51a() {    # was test_51
4010         # bug 1516 - create an empty entry right after ".." then split dir
4011         test_mkdir -c1 $DIR/$tdir
4012         touch $DIR/$tdir/foo
4013         $MCREATE $DIR/$tdir/bar
4014         rm $DIR/$tdir/foo
4015         createmany -m $DIR/$tdir/longfile 201
4016         FNUM=202
4017         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4018                 $MCREATE $DIR/$tdir/longfile$FNUM
4019                 FNUM=$(($FNUM + 1))
4020                 echo -n "+"
4021         done
4022         echo
4023         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4024 }
4025 run_test 51a "special situations: split htree with empty entry =="
4026
4027 export NUMTEST=70000
4028 test_51b() {
4029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4030         local BASE=$DIR/d${base}.${TESTSUITE}
4031
4032         # cleanup the directory
4033         rm -fr $BASE
4034
4035         test_mkdir -p -c1 $BASE
4036
4037         $LFS df
4038         $LFS df -i
4039         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4040         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4041         [[ $numfree -lt 21000 ]] &&
4042                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4043                 return
4044
4045         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4046                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4047
4048         # need to check free space for the directories as well
4049         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4050         numfree=$((blkfree / 4))
4051         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4052                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4053
4054         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4055         {
4056                 $LFS df
4057                 $LFS df -i
4058                 echo "failed" > $BASE/fnum
4059                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4060         }
4061 }
4062 run_test 51b "exceed 64k subdirectory nlink limit"
4063
4064 test_51ba() { # LU-993
4065         local BASE=$DIR/d${base}.${TESTSUITE}
4066         # unlink all but 100 subdirectories, then check it still works
4067         local LEFT=100
4068         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4069
4070         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4071         local DELETE=$((NUMTEST - LEFT))
4072
4073         # continue on to run this test even if 51b didn't finish,
4074         # just to delete the many subdirectories created.
4075         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4076
4077         # for ldiskfs the nlink count should be 1, but this is OSD specific
4078         # and so this is listed for informational purposes only
4079         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4080         unlinkmany -d $BASE/d $DELETE
4081         RC=$?
4082
4083         if [ $RC -ne 0 ]; then
4084                 if [ "$NUMPREV" == "failed" ]; then
4085                         skip "previous setup failed"
4086                         return 0
4087                 else
4088                         error "unlink of first $DELETE subdirs failed"
4089                         return $RC
4090                 fi
4091         fi
4092
4093         echo "nlink between: $(stat -c %h $BASE)"
4094         # trim the first line of ls output
4095         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4096         [ $FOUND -ne $LEFT ] &&
4097                 error "can't find subdirs: found only $FOUND/$LEFT"
4098
4099         unlinkmany -d $BASE/d $DELETE $LEFT ||
4100                 error "unlink of second $LEFT subdirs failed"
4101         # regardless of whether the backing filesystem tracks nlink accurately
4102         # or not, the nlink count shouldn't be more than "." and ".." here
4103         local AFTER=$(stat -c %h $BASE)
4104         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4105                 echo "nlink after: $AFTER"
4106 }
4107 run_test 51ba "verify nlink for many subdirectory cleanup"
4108
4109 test_51d() {
4110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4111         [[ $OSTCOUNT -lt 3 ]] &&
4112                 skip_env "skipping test with few OSTs" && return
4113         test_mkdir -p $DIR/$tdir
4114         createmany -o $DIR/$tdir/t- 1000
4115         $GETSTRIPE $DIR/$tdir > $TMP/files
4116         for N in $(seq 0 $((OSTCOUNT - 1))); do
4117                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4118                         END { printf("%0.0f", objs) }' $TMP/files)
4119                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4120                         '($1 == '$N') { objs += 1 } \
4121                         END { printf("%0.0f", objs) }')
4122                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4123         done
4124         unlinkmany $DIR/$tdir/t- 1000
4125
4126         NLAST=0
4127         for N in $(seq 1 $((OSTCOUNT - 1))); do
4128                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4129                         error "OST $N has less objects vs OST $NLAST" \
4130                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4131                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4132                         error "OST $N has less objects vs OST $NLAST" \
4133                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4134
4135                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4136                         error "OST $N has less #0 objects vs OST $NLAST" \
4137                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4138                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4139                         error "OST $N has less #0 objects vs OST $NLAST" \
4140                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4141                 NLAST=$N
4142         done
4143 }
4144 run_test 51d "check object distribution ===================="
4145
4146 test_51e() {
4147         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4148                 skip "Only applicable to ldiskfs-based MDTs"
4149                 return
4150         fi
4151
4152         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4153         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4154
4155         touch $DIR/$tdir/d0/foo
4156         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4157                 error "file exceed 65000 nlink limit!"
4158         unlinkmany $DIR/$tdir/d0/f- 65001
4159         return 0
4160 }
4161 run_test 51e "check file nlink limit"
4162
4163 test_52a() {
4164         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4165         test_mkdir -p $DIR/$tdir
4166         touch $DIR/$tdir/foo
4167         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4168         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4169         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4170         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4171         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4172                                         error "link worked"
4173         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4174         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4175         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4176                                                      error "lsattr"
4177         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4178         cp -r $DIR/$tdir /tmp/
4179         rm -fr $DIR/$tdir || error "cleanup rm failed"
4180 }
4181 run_test 52a "append-only flag test (should return errors) ====="
4182
4183 test_52b() {
4184         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4185         test_mkdir -p $DIR/$tdir
4186         touch $DIR/$tdir/foo
4187         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4188         cat test > $DIR/$tdir/foo && error "cat test worked"
4189         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4190         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4191         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4192                                         error "link worked"
4193         echo foo >> $DIR/$tdir/foo && error "echo worked"
4194         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4195         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4196         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4197         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4198                                                         error "lsattr"
4199         chattr -i $DIR/$tdir/foo || error "chattr failed"
4200
4201         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4202 }
4203 run_test 52b "immutable flag test (should return errors) ======="
4204
4205 test_53() {
4206         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4207         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4208         remote_ost_nodsh && skip "remote OST with nodsh" && return
4209
4210         local param
4211         local param_seq
4212         local ostname
4213         local mds_last
4214         local mds_last_seq
4215         local ost_last
4216         local ost_last_seq
4217         local ost_last_id
4218         local ostnum
4219         local node
4220         local found=false
4221         local support_last_seq=true
4222
4223         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4224                 support_last_seq=false
4225
4226         # only test MDT0000
4227         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4228         local value
4229         for value in $(do_facet $SINGLEMDS \
4230                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4231                 param=$(echo ${value[0]} | cut -d "=" -f1)
4232                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4233
4234                 if $support_last_seq; then
4235                         param_seq=$(echo $param |
4236                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4237                         mds_last_seq=$(do_facet $SINGLEMDS \
4238                                        $LCTL get_param -n $param_seq)
4239                 fi
4240                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4241
4242                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4243                 node=$(facet_active_host ost$((ostnum+1)))
4244                 param="obdfilter.$ostname.last_id"
4245                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4246                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4247                         ost_last_id=$ost_last
4248
4249                         if $support_last_seq; then
4250                                 ost_last_id=$(echo $ost_last |
4251                                               awk -F':' '{print $2}' |
4252                                               sed -e "s/^0x//g")
4253                                 ost_last_seq=$(echo $ost_last |
4254                                                awk -F':' '{print $1}')
4255                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4256                         fi
4257
4258                         if [[ $ost_last_id != $mds_last ]]; then
4259                                 error "$ost_last_id != $mds_last"
4260                         else
4261                                 found=true
4262                                 break
4263                         fi
4264                 done
4265         done
4266         $found || error "can not match last_seq/last_id for $mdtosc"
4267         return 0
4268 }
4269 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4270
4271 test_54a() {
4272         $SOCKETSERVER $DIR/socket ||
4273                 error "$SOCKETSERVER $DIR/socket failed: $?"
4274         $SOCKETCLIENT $DIR/socket ||
4275                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4276         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4277 }
4278 run_test 54a "unix domain socket test =========================="
4279
4280 test_54b() {
4281         f="$DIR/f54b"
4282         mknod $f c 1 3
4283         chmod 0666 $f
4284         dd if=/dev/zero of=$f bs=$(page_size) count=1
4285 }
4286 run_test 54b "char device works in lustre ======================"
4287
4288 find_loop_dev() {
4289         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4290         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4291         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4292
4293         for i in $(seq 3 7); do
4294                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4295                 LOOPDEV=$LOOPBASE$i
4296                 LOOPNUM=$i
4297                 break
4298         done
4299 }
4300
4301 cleanup_54c() {
4302         loopdev="$DIR/loop54c"
4303
4304         trap 0
4305         $UMOUNT $DIR/$tdir || rc=$?
4306         losetup -d $loopdev || true
4307         losetup -d $LOOPDEV || true
4308         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4309         return $rc
4310 }
4311
4312 test_54c() {
4313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4314         loopdev="$DIR/loop54c"
4315
4316         find_loop_dev
4317         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4318         trap cleanup_54c EXIT
4319         mknod $loopdev b 7 $LOOPNUM
4320         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4321         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4322         losetup $loopdev $DIR/$tfile ||
4323                 error "can't set up $loopdev for $DIR/$tfile"
4324         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4325         test_mkdir -p $DIR/$tdir
4326         mount -t ext2 $loopdev $DIR/$tdir ||
4327                 error "error mounting $loopdev on $DIR/$tdir"
4328         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4329                 error "dd write"
4330         df $DIR/$tdir
4331         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4332                 error "dd read"
4333         cleanup_54c
4334 }
4335 run_test 54c "block device works in lustre ====================="
4336
4337 test_54d() {
4338         f="$DIR/f54d"
4339         string="aaaaaa"
4340         mknod $f p
4341         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4342 }
4343 run_test 54d "fifo device works in lustre ======================"
4344
4345 test_54e() {
4346         f="$DIR/f54e"
4347         string="aaaaaa"
4348         cp -aL /dev/console $f
4349         echo $string > $f || error "echo $string to $f failed"
4350 }
4351 run_test 54e "console/tty device works in lustre ======================"
4352
4353 #The test_55 used to be iopen test and it was removed by bz#24037.
4354 #run_test 55 "check iopen_connect_dentry() ======================"
4355
4356 test_56a() {    # was test_56
4357         rm -rf $DIR/$tdir
4358         $SETSTRIPE -d $DIR
4359         test_mkdir -p $DIR/$tdir/dir
4360         NUMFILES=3
4361         NUMFILESx2=$(($NUMFILES * 2))
4362         for i in $(seq 1 $NUMFILES); do
4363                 touch $DIR/$tdir/file$i
4364                 touch $DIR/$tdir/dir/file$i
4365         done
4366
4367         # test lfs getstripe with --recursive
4368         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4369         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4370                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4371         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4372         [[ $FILENUM -eq $NUMFILES ]] ||
4373                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4374         echo "$GETSTRIPE --recursive passed."
4375
4376         # test lfs getstripe with file instead of dir
4377         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4378         [[ $FILENUM -eq 1 ]] ||
4379                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4380         echo "$GETSTRIPE file1 passed."
4381
4382         #test lfs getstripe with --verbose
4383         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4384                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4385                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4386         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4387                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4388         echo "$GETSTRIPE --verbose passed."
4389
4390         #test lfs getstripe with --obd
4391         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4392                 grep -q "unknown obduuid" ||
4393                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4394
4395         [[ $OSTCOUNT -lt 2 ]] &&
4396                 skip_env "skipping other $GETSTRIPE --obd test" && return
4397
4398         OSTIDX=1
4399         OBDUUID=$(ostuuid_from_index $OSTIDX)
4400         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4401         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4402         [[ $FOUND -eq $FILENUM ]] ||
4403                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4404         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4405                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4406                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4407                 error "$GETSTRIPE --obd: should not show file on other obd"
4408         echo "$GETSTRIPE --obd passed"
4409 }
4410 run_test 56a "check $GETSTRIPE"
4411
4412 NUMFILES=3
4413 NUMDIRS=3
4414 setup_56() {
4415         local LOCAL_NUMFILES="$1"
4416         local LOCAL_NUMDIRS="$2"
4417         local MKDIR_PARAMS="$3"
4418         local DIR_STRIPE_PARAMS="$4"
4419
4420         if [ ! -d "$TDIR" ] ; then
4421                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4422                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4423                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4424                         touch $TDIR/file$i
4425                 done
4426                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4427                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4428                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4429                                 touch $TDIR/dir$i/file$j
4430                         done
4431                 done
4432         fi
4433 }
4434
4435 setup_56_special() {
4436         LOCAL_NUMFILES=$1
4437         LOCAL_NUMDIRS=$2
4438         setup_56 $1 $2
4439         if [ ! -e "$TDIR/loop1b" ] ; then
4440                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4441                         mknod $TDIR/loop${i}b b 7 $i
4442                         mknod $TDIR/null${i}c c 1 3
4443                         ln -s $TDIR/file1 $TDIR/link${i}l
4444                 done
4445                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4446                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4447                         mknod $TDIR/dir$i/null${i}c c 1 3
4448                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4449                 done
4450         fi
4451 }
4452
4453 test_56g() {
4454         $SETSTRIPE -d $DIR
4455
4456         TDIR=$DIR/${tdir}g
4457         setup_56 $NUMFILES $NUMDIRS
4458
4459         EXPECTED=$(($NUMDIRS + 2))
4460         # test lfs find with -name
4461         for i in $(seq 1 $NUMFILES) ; do
4462                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4463                 [ $NUMS -eq $EXPECTED ] ||
4464                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4465                               "found $NUMS, expected $EXPECTED"
4466         done
4467 }
4468 run_test 56g "check lfs find -name ============================="
4469
4470 test_56h() {
4471         $SETSTRIPE -d $DIR
4472
4473         TDIR=$DIR/${tdir}g
4474         setup_56 $NUMFILES $NUMDIRS
4475
4476         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4477         # test lfs find with ! -name
4478         for i in $(seq 1 $NUMFILES) ; do
4479                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4480                 [ $NUMS -eq $EXPECTED ] ||
4481                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4482                               "found $NUMS, expected $EXPECTED"
4483         done
4484 }
4485 run_test 56h "check lfs find ! -name ============================="
4486
4487 test_56i() {
4488        tdir=${tdir}i
4489        test_mkdir -p $DIR/$tdir
4490        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4491        CMD="$LFIND -ost $UUID $DIR/$tdir"
4492        OUT=$($CMD)
4493        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4494 }
4495 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4496
4497 test_56j() {
4498         TDIR=$DIR/${tdir}g
4499         setup_56_special $NUMFILES $NUMDIRS
4500
4501         EXPECTED=$((NUMDIRS + 1))
4502         CMD="$LFIND -type d $TDIR"
4503         NUMS=$($CMD | wc -l)
4504         [ $NUMS -eq $EXPECTED ] ||
4505                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4506 }
4507 run_test 56j "check lfs find -type d ============================="
4508
4509 test_56k() {
4510         TDIR=$DIR/${tdir}g
4511         setup_56_special $NUMFILES $NUMDIRS
4512
4513         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4514         CMD="$LFIND -type f $TDIR"
4515         NUMS=$($CMD | wc -l)
4516         [ $NUMS -eq $EXPECTED ] ||
4517                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4518 }
4519 run_test 56k "check lfs find -type f ============================="
4520
4521 test_56l() {
4522         TDIR=$DIR/${tdir}g
4523         setup_56_special $NUMFILES $NUMDIRS
4524
4525         EXPECTED=$((NUMDIRS + NUMFILES))
4526         CMD="$LFIND -type b $TDIR"
4527         NUMS=$($CMD | wc -l)
4528         [ $NUMS -eq $EXPECTED ] ||
4529                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4530 }
4531 run_test 56l "check lfs find -type b ============================="
4532
4533 test_56m() {
4534         TDIR=$DIR/${tdir}g
4535         setup_56_special $NUMFILES $NUMDIRS
4536
4537         EXPECTED=$((NUMDIRS + NUMFILES))
4538         CMD="$LFIND -type c $TDIR"
4539         NUMS=$($CMD | wc -l)
4540         [ $NUMS -eq $EXPECTED ] ||
4541                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4542 }
4543 run_test 56m "check lfs find -type c ============================="
4544
4545 test_56n() {
4546         TDIR=$DIR/${tdir}g
4547         setup_56_special $NUMFILES $NUMDIRS
4548
4549         EXPECTED=$((NUMDIRS + NUMFILES))
4550         CMD="$LFIND -type l $TDIR"
4551         NUMS=$($CMD | wc -l)
4552         [ $NUMS -eq $EXPECTED ] ||
4553                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4554 }
4555 run_test 56n "check lfs find -type l ============================="
4556
4557 test_56o() {
4558         TDIR=$DIR/${tdir}o
4559         setup_56 $NUMFILES $NUMDIRS
4560         utime $TDIR/file1 > /dev/null || error "utime (1)"
4561         utime $TDIR/file2 > /dev/null || error "utime (2)"
4562         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4563         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4564         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4565         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4566
4567         EXPECTED=4
4568         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4569         [ $NUMS -eq $EXPECTED ] || \
4570                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4571
4572         EXPECTED=12
4573         CMD="$LFIND -mtime 0 $TDIR"
4574         NUMS=$($CMD | wc -l)
4575         [ $NUMS -eq $EXPECTED ] ||
4576                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4577 }
4578 run_test 56o "check lfs find -mtime for old files =========================="
4579
4580 test_56p() {
4581         [ $RUNAS_ID -eq $UID ] &&
4582                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4583
4584         TDIR=$DIR/${tdir}p
4585         setup_56 $NUMFILES $NUMDIRS
4586
4587         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4588         EXPECTED=$NUMFILES
4589         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4590         NUMS=$($CMD | wc -l)
4591         [ $NUMS -eq $EXPECTED ] || \
4592                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4593
4594         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4595         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4596         NUMS=$($CMD | wc -l)
4597         [ $NUMS -eq $EXPECTED ] || \
4598                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4599 }
4600 run_test 56p "check lfs find -uid and ! -uid ==============================="
4601
4602 test_56q() {
4603         [ $RUNAS_ID -eq $UID ] &&
4604                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4605
4606         TDIR=$DIR/${tdir}q
4607         setup_56 $NUMFILES $NUMDIRS
4608
4609         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4610
4611         EXPECTED=$NUMFILES
4612         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4613         NUMS=$($CMD | wc -l)
4614         [ $NUMS -eq $EXPECTED ] ||
4615                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4616
4617         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4618         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4619         NUMS=$($CMD | wc -l)
4620         [ $NUMS -eq $EXPECTED ] ||
4621                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4622 }
4623 run_test 56q "check lfs find -gid and ! -gid ==============================="
4624
4625 test_56r() {
4626         TDIR=$DIR/${tdir}r
4627         setup_56 $NUMFILES $NUMDIRS
4628
4629         EXPECTED=12
4630         CMD="$LFIND -size 0 -type f $TDIR"
4631         NUMS=$($CMD | wc -l)
4632         [ $NUMS -eq $EXPECTED ] ||
4633                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4634         EXPECTED=0
4635         CMD="$LFIND ! -size 0 -type f $TDIR"
4636         NUMS=$($CMD | wc -l)
4637         [ $NUMS -eq $EXPECTED ] ||
4638                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4639         echo "test" > $TDIR/$tfile
4640         echo "test2" > $TDIR/$tfile.2 && sync
4641         EXPECTED=1
4642         CMD="$LFIND -size 5 -type f $TDIR"
4643         NUMS=$($CMD | wc -l)
4644         [ $NUMS -eq $EXPECTED ] ||
4645                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4646         EXPECTED=1
4647         CMD="$LFIND -size +5 -type f $TDIR"
4648         NUMS=$($CMD | wc -l)
4649         [ $NUMS -eq $EXPECTED ] ||
4650                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4651         EXPECTED=2
4652         CMD="$LFIND -size +0 -type f $TDIR"
4653         NUMS=$($CMD | wc -l)
4654         [ $NUMS -eq $EXPECTED ] ||
4655                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4656         EXPECTED=2
4657         CMD="$LFIND ! -size -5 -type f $TDIR"
4658         NUMS=$($CMD | wc -l)
4659         [ $NUMS -eq $EXPECTED ] ||
4660                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4661         EXPECTED=12
4662         CMD="$LFIND -size -5 -type f $TDIR"
4663         NUMS=$($CMD | wc -l)
4664         [ $NUMS -eq $EXPECTED ] ||
4665                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4666 }
4667 run_test 56r "check lfs find -size works =========================="
4668
4669 test_56s() { # LU-611
4670         TDIR=$DIR/${tdir}s
4671         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4672
4673         if [[ $OSTCOUNT -gt 1 ]]; then
4674                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4675                 ONESTRIPE=4
4676                 EXTRA=4
4677         else
4678                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4679                 EXTRA=0
4680         fi
4681
4682         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4683         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4684         NUMS=$($CMD | wc -l)
4685         [ $NUMS -eq $EXPECTED ] || {
4686                 $GETSTRIPE -R $TDIR
4687                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4688         }
4689
4690         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4691         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4692         NUMS=$($CMD | wc -l)
4693         [ $NUMS -eq $EXPECTED ] || {
4694                 $GETSTRIPE -R $TDIR
4695                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4696         }
4697
4698         EXPECTED=$ONESTRIPE
4699         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4700         NUMS=$($CMD | wc -l)
4701         [ $NUMS -eq $EXPECTED ] || {
4702                 $GETSTRIPE -R $TDIR
4703                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4704         }
4705
4706         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4707         NUMS=$($CMD | wc -l)
4708         [ $NUMS -eq $EXPECTED ] || {
4709                 $GETSTRIPE -R $TDIR
4710                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4711         }
4712
4713         EXPECTED=0
4714         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4715         NUMS=$($CMD | wc -l)
4716         [ $NUMS -eq $EXPECTED ] || {
4717                 $GETSTRIPE -R $TDIR
4718                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4719         }
4720 }
4721 run_test 56s "check lfs find -stripe-count works"
4722
4723 test_56t() { # LU-611
4724         TDIR=$DIR/${tdir}t
4725         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4726
4727         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4728
4729         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4730         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4731         NUMS=$($CMD | wc -l)
4732         [ $NUMS -eq $EXPECTED ] ||
4733                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4734
4735         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4736         NUMS=$($CMD | wc -l)
4737         [ $NUMS -eq $EXPECTED ] ||
4738                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4739
4740         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4741         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4742         NUMS=$($CMD | wc -l)
4743         [ $NUMS -eq $EXPECTED ] ||
4744                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4745
4746         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4747         NUMS=$($CMD | wc -l)
4748         [ $NUMS -eq $EXPECTED ] ||
4749                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4750
4751         EXPECTED=4
4752         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4753         NUMS=$($CMD | wc -l)
4754         [ $NUMS -eq $EXPECTED ] ||
4755                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4756
4757         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4758         NUMS=$($CMD | wc -l)
4759         [ $NUMS -eq $EXPECTED ] ||
4760                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4761
4762         EXPECTED=0
4763         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4764         NUMS=$($CMD | wc -l)
4765         [ $NUMS -eq $EXPECTED ] ||
4766                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4767 }
4768 run_test 56t "check lfs find -stripe-size works"
4769
4770 test_56u() { # LU-611
4771         TDIR=$DIR/${tdir}u
4772         setup_56 $NUMFILES $NUMDIRS "-i 0"
4773
4774         if [[ $OSTCOUNT -gt 1 ]]; then
4775                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4776                 ONESTRIPE=4
4777         else
4778                 ONESTRIPE=0
4779         fi
4780
4781         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4782         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4783         NUMS=$($CMD | wc -l)
4784         [ $NUMS -eq $EXPECTED ] ||
4785                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4786
4787         EXPECTED=$ONESTRIPE
4788         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4789         NUMS=$($CMD | wc -l)
4790         [ $NUMS -eq $EXPECTED ] ||
4791                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4792
4793         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4794         NUMS=$($CMD | wc -l)
4795         [ $NUMS -eq $EXPECTED ] ||
4796                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4797
4798         EXPECTED=0
4799         # This should produce an error and not return any files
4800         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4801         NUMS=$($CMD 2>/dev/null | wc -l)
4802         [ $NUMS -eq $EXPECTED ] ||
4803                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4804
4805         if [[ $OSTCOUNT -gt 1 ]]; then
4806                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4807                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4808                 NUMS=$($CMD | wc -l)
4809                 [ $NUMS -eq $EXPECTED ] ||
4810                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4811         fi
4812 }
4813 run_test 56u "check lfs find -stripe-index works"
4814
4815 test_56v() {
4816     local MDT_IDX=0
4817
4818     TDIR=$DIR/${tdir}v
4819     rm -rf $TDIR
4820     setup_56 $NUMFILES $NUMDIRS
4821
4822     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4823     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4824
4825     for file in $($LFIND -mdt $UUID $TDIR); do
4826         file_mdt_idx=$($GETSTRIPE -M $file)
4827         [ $file_mdt_idx -eq $MDT_IDX ] ||
4828             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4829     done
4830 }
4831 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4832
4833 test_56w() {
4834         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4835                 return
4836         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4837         TDIR=$DIR/${tdir}w
4838
4839     rm -rf $TDIR || error "remove $TDIR failed"
4840     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4841
4842     local stripe_size
4843     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4844         error "$GETSTRIPE -S -d $TDIR failed"
4845     stripe_size=${stripe_size%% *}
4846
4847     local file_size=$((stripe_size * OSTCOUNT))
4848     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4849     local required_space=$((file_num * file_size))
4850     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4851     [[ $free_space -le $((required_space / 1024)) ]] &&
4852         skip_env "need at least $required_space bytes free space," \
4853                  "have $free_space kbytes" && return
4854
4855     local dd_bs=65536
4856     local dd_count=$((file_size / dd_bs))
4857
4858     # write data into the files
4859     local i
4860     local j
4861     local file
4862     for i in $(seq 1 $NUMFILES); do
4863         file=$TDIR/file$i
4864         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4865             error "write data into $file failed"
4866     done
4867     for i in $(seq 1 $NUMDIRS); do
4868         for j in $(seq 1 $NUMFILES); do
4869             file=$TDIR/dir$i/file$j
4870             yes | dd bs=$dd_bs count=$dd_count of=$file \
4871                 >/dev/null 2>&1 ||
4872                 error "write data into $file failed"
4873         done
4874     done
4875
4876     local expected=-1
4877     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4878
4879     # lfs_migrate file
4880     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4881     echo "$cmd"
4882     eval $cmd || error "$cmd failed"
4883
4884     check_stripe_count $TDIR/file1 $expected
4885
4886         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4887         then
4888                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4889                 # OST 1 if it is on OST 0. This file is small enough to
4890                 # be on only one stripe.
4891                 file=$TDIR/migr_1_ost
4892                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4893                         error "write data into $file failed"
4894                 local obdidx=$($LFS getstripe -i $file)
4895                 local oldmd5=$(md5sum $file)
4896                 local newobdidx=0
4897                 [[ $obdidx -eq 0 ]] && newobdidx=1
4898                 cmd="$LFS migrate -i $newobdidx $file"
4899                 echo $cmd
4900                 eval $cmd || error "$cmd failed"
4901                 local realobdix=$($LFS getstripe -i $file)
4902                 local newmd5=$(md5sum $file)
4903                 [[ $newobdidx -ne $realobdix ]] &&
4904                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4905                 [[ "$oldmd5" != "$newmd5" ]] &&
4906                         error "md5sum differ: $oldmd5, $newmd5"
4907         fi
4908
4909     # lfs_migrate dir
4910     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4911     echo "$cmd"
4912     eval $cmd || error "$cmd failed"
4913
4914     for j in $(seq 1 $NUMFILES); do
4915         check_stripe_count $TDIR/dir1/file$j $expected
4916     done
4917
4918     # lfs_migrate works with lfs find
4919     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4920          $LFS_MIGRATE -y -c $expected"
4921     echo "$cmd"
4922     eval $cmd || error "$cmd failed"
4923
4924     for i in $(seq 2 $NUMFILES); do
4925         check_stripe_count $TDIR/file$i $expected
4926     done
4927     for i in $(seq 2 $NUMDIRS); do
4928         for j in $(seq 1 $NUMFILES); do
4929             check_stripe_count $TDIR/dir$i/file$j $expected
4930         done
4931     done
4932 }
4933 run_test 56w "check lfs_migrate -c stripe_count works"
4934
4935 test_56x() {
4936         check_swap_layouts_support && return 0
4937         [[ $OSTCOUNT -lt 2 ]] &&
4938                 skip_env "need 2 OST, skipping test" && return
4939
4940         local dir0=$DIR/$tdir/$testnum
4941         test_mkdir -p $dir0 || error "creating dir $dir0"
4942
4943         local ref1=/etc/passwd
4944         local file1=$dir0/file1
4945
4946         $SETSTRIPE -c 2 $file1
4947         cp $ref1 $file1
4948         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4949         stripe=$($GETSTRIPE -c $file1)
4950         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4951         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4952
4953         # clean up
4954         rm -f $file1
4955 }
4956 run_test 56x "lfs migration support"
4957
4958 test_56y() {
4959         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4960                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
4961                 return
4962
4963         local res=""
4964         local dir0=$DIR/$tdir/$testnum
4965         test_mkdir -p $dir0 || error "creating dir $dir0"
4966         local f1=$dir0/file1
4967         local f2=$dir0/file2
4968
4969         touch $f1 || error "creating std file $f1"
4970         $MULTIOP $f2 H2c || error "creating released file $f2"
4971
4972         # a directory can be raid0, so ask only for files
4973         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4974         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4975
4976         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4977         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4978
4979         # only files can be released, so no need to force file search
4980         res=$($LFIND $dir0 -L released)
4981         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4982
4983         res=$($LFIND $dir0 \! -L released)
4984         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4985
4986 }
4987 run_test 56y "lfs find -L raid0|released"
4988
4989 test_56z() { # LU-4824
4990         # This checks to make sure 'lfs find' continues after errors
4991         # There are two classes of errors that should be caught:
4992         # - If multiple paths are provided, all should be searched even if one
4993         #   errors out
4994         # - If errors are encountered during the search, it should not terminate
4995         #   early
4996         local i
4997         test_mkdir $DIR/$tdir
4998         for i in d{0..9}; do
4999                 test_mkdir $DIR/$tdir/$i
5000         done
5001         touch $DIR/$tdir/d{0..9}/$tfile
5002         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5003                 error "$LFS find did not return an error"
5004         # Make a directory unsearchable. This should NOT be the last entry in
5005         # directory order.  Arbitrarily pick the 6th entry
5006         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5007         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5008         # The user should be able to see 10 directories and 9 files
5009         [ $count == 19 ] || error "$LFS find did not continue after error"
5010 }
5011 run_test 56z "lfs find should continue after an error"
5012
5013 test_56aa() { # LU-5937
5014         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5015
5016         mkdir $DIR/$tdir
5017         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5018
5019         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5020         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5021
5022         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5023 }
5024 run_test 56aa "lfs find --size under striped dir"
5025
5026 test_57a() {
5027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5028         # note test will not do anything if MDS is not local
5029         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5030                 skip "Only applicable to ldiskfs-based MDTs"
5031                 return
5032         fi
5033
5034         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5035         local MNTDEV="osd*.*MDT*.mntdev"
5036         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5037         [ -z "$DEV" ] && error "can't access $MNTDEV"
5038         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5039                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5040                         error "can't access $DEV"
5041                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5042                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5043                 rm $TMP/t57a.dump
5044         done
5045 }
5046 run_test 57a "verify MDS filesystem created with large inodes =="
5047
5048 test_57b() {
5049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5050         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5051                 skip "Only applicable to ldiskfs-based MDTs"
5052                 return
5053         fi
5054
5055         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5056         local dir=$DIR/$tdir
5057
5058         local FILECOUNT=100
5059         local FILE1=$dir/f1
5060         local FILEN=$dir/f$FILECOUNT
5061
5062         rm -rf $dir || error "removing $dir"
5063         test_mkdir -p -c1 $dir || error "creating $dir"
5064         local mdtidx=$($LFS getstripe -M $dir)
5065         local mdtname=MDT$(printf %04x $mdtidx)
5066         local facet=mds$((mdtidx + 1))
5067
5068         echo "mcreating $FILECOUNT files"
5069         createmany -m $dir/f 1 $FILECOUNT || \
5070                 error "creating files in $dir"
5071
5072         # verify that files do not have EAs yet
5073         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5074         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5075
5076         sync
5077         sleep 1
5078         df $dir  #make sure we get new statfs data
5079         local MDSFREE=$(do_facet $facet \
5080                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5081         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5082         echo "opening files to create objects/EAs"
5083         local FILE
5084         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5085                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5086         done
5087
5088         # verify that files have EAs now
5089         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5090         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5091
5092         sleep 1  #make sure we get new statfs data
5093         df $dir
5094         local MDSFREE2=$(do_facet $facet \
5095                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5096         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5097         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5098                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5099                         error "MDC before $MDCFREE != after $MDCFREE2"
5100                 else
5101                         echo "MDC before $MDCFREE != after $MDCFREE2"
5102                         echo "unable to confirm if MDS has large inodes"
5103                 fi
5104         fi
5105         rm -rf $dir
5106 }
5107 run_test 57b "default LOV EAs are stored inside large inodes ==="
5108
5109 test_58() {
5110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5111         [ -z "$(which wiretest 2>/dev/null)" ] &&
5112                         skip_env "could not find wiretest" && return
5113         wiretest
5114 }
5115 run_test 58 "verify cross-platform wire constants =============="
5116
5117 test_59() {
5118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5119         echo "touch 130 files"
5120         createmany -o $DIR/f59- 130
5121         echo "rm 130 files"
5122         unlinkmany $DIR/f59- 130
5123         sync
5124         # wait for commitment of removal
5125         wait_delete_completed
5126 }
5127 run_test 59 "verify cancellation of llog records async ========="
5128
5129 TEST60_HEAD="test_60 run $RANDOM"
5130 test_60a() {
5131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5132         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5133         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5134                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5135                         skip_env "missing subtest run-llog.sh" && return
5136
5137         log "$TEST60_HEAD - from kernel mode"
5138         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5139         do_facet mgs sh run-llog.sh
5140         do_facet mgs $LCTL dk > $TMP/$tfile
5141
5142         # LU-6388: test llog_reader
5143         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5144         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5145         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5146                         skip_env "missing llog_reader" && return
5147         local fstype=$(facet_fstype mgs)
5148         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5149                 skip_env "Only for ldiskfs or zfs type mgs" && return
5150
5151         local mntpt=$(facet_mntpt mgs)
5152         local mgsdev=$(mgsdevname 1)
5153         local fid_list
5154         local fid
5155         local rec_list
5156         local rec
5157         local rec_type
5158         local obj_file
5159         local path
5160         local seq
5161         local oid
5162         local pass=true
5163
5164         #get fid and record list
5165         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5166                 tail -n 4))
5167         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5168                 tail -n 4))
5169         #remount mgs as ldiskfs or zfs type
5170         stop mgs || error "stop mgs failed"
5171         mount_fstype mgs || error "remount mgs failed"
5172         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5173                 fid=${fid_list[i]}
5174                 rec=${rec_list[i]}
5175                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5176                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5177                 oid=$((16#$oid))
5178
5179                 case $fstype in
5180                         ldiskfs )
5181                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5182                         zfs )
5183                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5184                 esac
5185                 echo "obj_file is $obj_file"
5186                 do_facet mgs $llog_reader $obj_file
5187
5188                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5189                         awk '{ print $3 }' | sed -e "s/^type=//g")
5190                 if [ $rec_type != $rec ]; then
5191                         echo "FAILED test_60a wrong record type $rec_type," \
5192                               "should be $rec"
5193                         pass=false
5194                         break
5195                 fi
5196
5197                 #check obj path if record type is LLOG_LOGID_MAGIC
5198                 if [ "$rec" == "1064553b" ]; then
5199                         path=$(do_facet mgs $llog_reader $obj_file |
5200                                 grep "path=" | awk '{ print $NF }' |
5201                                 sed -e "s/^path=//g")
5202                         if [ $obj_file != $mntpt/$path ]; then
5203                                 echo "FAILED test_60a wrong obj path" \
5204                                       "$montpt/$path, should be $obj_file"
5205                                 pass=false
5206                                 break
5207                         fi
5208                 fi
5209         done
5210         rm -f $TMP/$tfile
5211         #restart mgs before "error", otherwise it will block the next test
5212         stop mgs || error "stop mgs failed"
5213         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5214         $pass || error "test failed, see FAILED test_60a messages for specifics"
5215 }
5216 run_test 60a "llog_test run from kernel module and test llog_reader =========="
5217
5218 test_60b() { # bug 6411
5219         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5220         dmesg > $DIR/$tfile
5221         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5222                                  /llog.test/ {
5223                                          if (marker)
5224                                                  from_marker++
5225                                          from_begin++
5226                                  }
5227                                  END {
5228                                          if (marker)
5229                                                  print from_marker
5230                                          else
5231                                                  print from_begin
5232                                  }"`
5233         [[ $LLOG_COUNT -gt 50 ]] &&
5234                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5235 }
5236 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5237
5238 test_60c() {
5239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5240         echo "create 5000 files"
5241         createmany -o $DIR/f60c- 5000
5242 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5243         lctl set_param fail_loc=0x80000137
5244         unlinkmany $DIR/f60c- 5000
5245         lctl set_param fail_loc=0
5246 }
5247 run_test 60c "unlink file when mds full"
5248
5249 test_60d() {
5250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5251         SAVEPRINTK=$(lctl get_param -n printk)
5252
5253         # verify "lctl mark" is even working"
5254         MESSAGE="test message ID $RANDOM $$"
5255         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5256         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5257
5258         lctl set_param printk=0 || error "set lnet.printk failed"
5259         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5260         MESSAGE="new test message ID $RANDOM $$"
5261         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5262         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5263         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5264
5265         lctl set_param -n printk="$SAVEPRINTK"
5266 }
5267 run_test 60d "test printk console message masking"
5268
5269 test_60e() {
5270         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5271         touch $DIR/$tfile
5272 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5273         do_facet mds1 lctl set_param fail_loc=0x15b
5274         rm $DIR/$tfile
5275 }
5276 run_test 60e "no space while new llog is being created"
5277
5278 test_61() {
5279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5280         f="$DIR/f61"
5281         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5282         cancel_lru_locks osc
5283         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5284         sync
5285 }
5286 run_test 61 "mmap() writes don't make sync hang ================"
5287
5288 # bug 2330 - insufficient obd_match error checking causes LBUG
5289 test_62() {
5290         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5291         f="$DIR/f62"
5292         echo foo > $f
5293         cancel_lru_locks osc
5294         lctl set_param fail_loc=0x405
5295         cat $f && error "cat succeeded, expect -EIO"
5296         lctl set_param fail_loc=0
5297 }
5298 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5299 # match every page all of the time.
5300 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5301
5302 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5303 # Though this test is irrelevant anymore, it helped to reveal some
5304 # other grant bugs (LU-4482), let's keep it.
5305 test_63a() {   # was test_63
5306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5307         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5308         for i in `seq 10` ; do
5309                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5310                 sleep 5
5311                 kill $!
5312                 sleep 1
5313         done
5314
5315         rm -f $DIR/f63 || true
5316 }
5317 run_test 63a "Verify oig_wait interruption does not crash ======="
5318
5319 # bug 2248 - async write errors didn't return to application on sync
5320 # bug 3677 - async write errors left page locked
5321 test_63b() {
5322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5323         debugsave
5324         lctl set_param debug=-1
5325
5326         # ensure we have a grant to do async writes
5327         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5328         rm $DIR/$tfile
5329
5330         sync    # sync lest earlier test intercept the fail_loc
5331
5332         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5333         lctl set_param fail_loc=0x80000406
5334         $MULTIOP $DIR/$tfile Owy && \
5335                 error "sync didn't return ENOMEM"
5336         sync; sleep 2; sync     # do a real sync this time to flush page
5337         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5338                 error "locked page left in cache after async error" || true
5339         debugrestore
5340 }
5341 run_test 63b "async write errors should be returned to fsync ==="
5342
5343 test_64a () {
5344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5345         df $DIR
5346         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5347 }
5348 run_test 64a "verify filter grant calculations (in kernel) ====="
5349
5350 test_64b () {
5351         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5352         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5353 }
5354 run_test 64b "check out-of-space detection on client ==========="
5355
5356 test_64c() {
5357         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5358 }
5359 run_test 64c "verify grant shrink ========================------"
5360
5361 # bug 1414 - set/get directories' stripe info
5362 test_65a() {
5363         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5364         test_mkdir -p $DIR/$tdir
5365         touch $DIR/$tdir/f1
5366         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5367 }
5368 run_test 65a "directory with no stripe info ===================="
5369
5370 test_65b() {
5371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5372         test_mkdir -p $DIR/$tdir
5373         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5374                                                 error "setstripe"
5375         touch $DIR/$tdir/f2
5376         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5377 }
5378 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5379
5380 test_65c() {
5381         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5382         if [[ $OSTCOUNT -gt 1 ]]; then
5383                 test_mkdir -p $DIR/$tdir
5384                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5385                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5386                 touch $DIR/$tdir/f3
5387                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5388         fi
5389 }
5390 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5391
5392 test_65d() {
5393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5394         test_mkdir -p $DIR/$tdir
5395         if [[ $STRIPECOUNT -le 0 ]]; then
5396                 sc=1
5397         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5398 #LOV_MAX_STRIPE_COUNT is 2000
5399                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5400         else
5401                 sc=$(($STRIPECOUNT - 1))
5402         fi
5403         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5404         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5405         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5406                 error "lverify failed"
5407 }
5408 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5409
5410 test_65e() {
5411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5412         test_mkdir -p $DIR/$tdir
5413
5414         $SETSTRIPE $DIR/$tdir || error "setstripe"
5415         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5416                                         error "no stripe info failed"
5417         touch $DIR/$tdir/f6
5418         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5419 }
5420 run_test 65e "directory setstripe defaults ======================="
5421
5422 test_65f() {
5423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5424         test_mkdir -p $DIR/${tdir}f
5425         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5426 }
5427 run_test 65f "dir setstripe permission (should return error) ==="
5428
5429 test_65g() {
5430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5431         test_mkdir -p $DIR/$tdir
5432         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5433                                                         error "setstripe"
5434         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5435         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5436                 error "delete default stripe failed"
5437 }
5438 run_test 65g "directory setstripe -d ==========================="
5439
5440 test_65h() {
5441         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5442         test_mkdir -p $DIR/$tdir
5443         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5444                                                         error "setstripe"
5445         test_mkdir -p $DIR/$tdir/dd1
5446         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5447                 error "stripe info inherit failed"
5448 }
5449 run_test 65h "directory stripe info inherit ===================="
5450
5451 test_65i() { # bug6367
5452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5453         $SETSTRIPE -S 65536 -c -1 $MOUNT
5454 }
5455 run_test 65i "set non-default striping on root directory (bug 6367)="
5456
5457 test_65ia() { # bug12836
5458         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5459         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5460 }
5461 run_test 65ia "getstripe on -1 default directory striping"
5462
5463 test_65ib() { # bug12836
5464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5465         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5466 }
5467 run_test 65ib "getstripe -v on -1 default directory striping"
5468
5469 test_65ic() { # bug12836
5470         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5471         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5472 }
5473 run_test 65ic "new find on -1 default directory striping"
5474
5475 test_65j() { # bug6367
5476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5477         sync; sleep 1
5478         # if we aren't already remounting for each test, do so for this test
5479         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5480                 cleanup || error "failed to unmount"
5481                 setup
5482         fi
5483         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5484 }
5485 run_test 65j "set default striping on root directory (bug 6367)="
5486
5487 test_65k() { # bug11679
5488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5489         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5490         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5491
5492     echo "Check OST status: "
5493     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5494               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5495
5496     for OSC in $MDS_OSCS; do
5497         echo $OSC "is activate"
5498         do_facet $SINGLEMDS lctl --device %$OSC activate
5499     done
5500
5501     mkdir -p $DIR/$tdir
5502     for INACTIVE_OSC in $MDS_OSCS; do
5503         echo "Deactivate: " $INACTIVE_OSC
5504         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5505         for STRIPE_OSC in $MDS_OSCS; do
5506             OST=`osc_to_ost $STRIPE_OSC`
5507             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5508                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5509
5510             [ -f $DIR/$tdir/$IDX ] && continue
5511             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5512             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5513             RC=$?
5514             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5515         done
5516         rm -f $DIR/$tdir/*
5517         echo $INACTIVE_OSC "is Activate."
5518         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5519     done
5520 }
5521 run_test 65k "validate manual striping works properly with deactivated OSCs"
5522
5523 test_65l() { # bug 12836
5524         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5525         test_mkdir -p $DIR/$tdir/test_dir
5526         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5527         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5528 }
5529 run_test 65l "lfs find on -1 stripe dir ========================"
5530
5531 # bug 2543 - update blocks count on client
5532 test_66() {
5533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5534         COUNT=${COUNT:-8}
5535         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5536         sync; sync_all_data; sync; sync_all_data
5537         cancel_lru_locks osc
5538         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5539         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5540 }
5541 run_test 66 "update inode blocks count on client ==============="
5542
5543 LLOOP=
5544 LLITELOOPLOAD=
5545 cleanup_68() {
5546         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5547         trap 0
5548         if [ ! -z "$LLOOP" ]; then
5549                 if swapon -s | grep -q $LLOOP; then
5550                         swapoff $LLOOP || error "swapoff failed"
5551                 fi
5552
5553                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5554                 rm -f $LLOOP
5555                 unset LLOOP
5556         fi
5557         if [ ! -z "$LLITELOOPLOAD" ]; then
5558                 rmmod llite_lloop
5559                 unset LLITELOOPLOAD
5560         fi
5561         rm -f $DIR/f68*
5562 }
5563
5564 meminfo() {
5565         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5566 }
5567
5568 swap_used() {
5569         swapon -s | awk '($1 == "'$1'") { print $4 }'
5570 }
5571
5572 # test case for lloop driver, basic function
5573 test_68a() {
5574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5575         [ "$UID" != 0 ] && skip_env "must run as root" && return
5576         llite_lloop_enabled || \
5577                 { skip_env "llite_lloop module disabled" && return; }
5578
5579         trap cleanup_68 EXIT
5580
5581         if ! module_loaded llite_lloop; then
5582                 if load_module llite/llite_lloop; then
5583                         LLITELOOPLOAD=yes
5584                 else
5585                         skip_env "can't find module llite_lloop"
5586                         return
5587                 fi
5588         fi
5589
5590         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5591         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5592         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5593
5594         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5595         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5596
5597         cleanup_68
5598 }
5599 run_test 68a "lloop driver - basic test ========================"
5600
5601 # excercise swapping to lustre by adding a high priority swapfile entry
5602 # and then consuming memory until it is used.
5603 test_68b() {  # was test_68
5604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5605         [ "$UID" != 0 ] && skip_env "must run as root" && return
5606         lctl get_param -n devices | grep -q obdfilter && \
5607                 skip "local OST" && return
5608
5609         grep -q llite_lloop /proc/modules
5610         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5611
5612         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5613                 skip "can't reliably test swap with TCP" && return
5614
5615         MEMTOTAL=`meminfo MemTotal`
5616         NR_BLOCKS=$((MEMTOTAL>>8))
5617         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5618
5619         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5620         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5621         mkswap $DIR/f68b
5622
5623         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5624
5625         trap cleanup_68 EXIT
5626
5627         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5628
5629         echo "before: `swapon -s | grep $LLOOP`"
5630         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5631         echo "after: `swapon -s | grep $LLOOP`"
5632         SWAPUSED=`swap_used $LLOOP`
5633
5634         cleanup_68
5635
5636         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5637 }
5638 run_test 68b "support swapping to Lustre ========================"
5639
5640 # bug5265, obdfilter oa2dentry return -ENOENT
5641 # #define OBD_FAIL_SRV_ENOENT 0x217
5642 test_69() {
5643         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5644         remote_ost_nodsh && skip "remote OST with nodsh" && return
5645
5646         f="$DIR/$tfile"
5647         $SETSTRIPE -c 1 -i 0 $f
5648
5649         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5650
5651         do_facet ost1 lctl set_param fail_loc=0x217
5652         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5653         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5654
5655         do_facet ost1 lctl set_param fail_loc=0
5656         $DIRECTIO write $f 0 2 || error "write error"
5657
5658         cancel_lru_locks osc
5659         $DIRECTIO read $f 0 1 || error "read error"
5660
5661         do_facet ost1 lctl set_param fail_loc=0x217
5662         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5663
5664         do_facet ost1 lctl set_param fail_loc=0
5665         rm -f $f
5666 }
5667 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5668
5669 test_71() {
5670         test_mkdir -p $DIR/$tdir
5671         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
5672         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5673 }
5674 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5675
5676 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5677         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5678         [ "$RUNAS_ID" = "$UID" ] &&
5679                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5680
5681         # Check that testing environment is properly set up. Skip if not
5682         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5683                 skip_env "User $RUNAS_ID does not exist - skipping"
5684                 return 0
5685         }
5686         touch $DIR/$tfile
5687         chmod 777 $DIR/$tfile
5688         chmod ug+s $DIR/$tfile
5689         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5690                 error "$RUNAS dd $DIR/$tfile failed"
5691         # See if we are still setuid/sgid
5692         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5693                 error "S/gid is not dropped on write"
5694         # Now test that MDS is updated too
5695         cancel_lru_locks mdc
5696         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5697                 error "S/gid is not dropped on MDS"
5698         rm -f $DIR/$tfile
5699 }
5700 run_test 72a "Test that remove suid works properly (bug5695) ===="
5701
5702 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5703         local perm
5704
5705         [ "$RUNAS_ID" = "$UID" ] && \
5706                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5707         [ "$RUNAS_ID" -eq 0 ] && \
5708                 skip_env "RUNAS_ID = 0 -- skipping" && return
5709
5710         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5711         # Check that testing environment is properly set up. Skip if not
5712         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5713                 skip_env "User $RUNAS_ID does not exist - skipping"
5714                 return 0
5715         }
5716         touch $DIR/${tfile}-f{g,u}
5717         test_mkdir $DIR/${tfile}-dg
5718         test_mkdir $DIR/${tfile}-du
5719         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5720         chmod g+s $DIR/${tfile}-{f,d}g
5721         chmod u+s $DIR/${tfile}-{f,d}u
5722         for perm in 777 2777 4777; do
5723                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5724                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5725                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5726                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5727         done
5728         true
5729 }
5730 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5731
5732 # bug 3462 - multiple simultaneous MDC requests
5733 test_73() {
5734         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5735         test_mkdir $DIR/d73-1
5736         test_mkdir $DIR/d73-2
5737         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5738         pid1=$!
5739
5740         lctl set_param fail_loc=0x80000129
5741         $MULTIOP $DIR/d73-1/f73-2 Oc &
5742         sleep 1
5743         lctl set_param fail_loc=0
5744
5745         $MULTIOP $DIR/d73-2/f73-3 Oc &
5746         pid3=$!
5747
5748         kill -USR1 $pid1
5749         wait $pid1 || return 1
5750
5751         sleep 25
5752
5753         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5754         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5755         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5756
5757         rm -rf $DIR/d73-*
5758 }
5759 run_test 73 "multiple MDC requests (should not deadlock)"
5760
5761 test_74a() { # bug 6149, 6184
5762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5763         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5764         #
5765         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5766         # will spin in a tight reconnection loop
5767         touch $DIR/f74a
5768         $LCTL set_param fail_loc=0x8000030e
5769         # get any lock that won't be difficult - lookup works.
5770         ls $DIR/f74a
5771         $LCTL set_param fail_loc=0
5772         rm -f $DIR/f74a
5773         true
5774 }
5775 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5776
5777 test_74b() { # bug 13310
5778         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5779         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5780         #
5781         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5782         # will spin in a tight reconnection loop
5783         $LCTL set_param fail_loc=0x8000030e
5784         # get a "difficult" lock
5785         touch $DIR/f74b
5786         $LCTL set_param fail_loc=0
5787         rm -f $DIR/f74b
5788         true
5789 }
5790 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5791
5792 test_74c() {
5793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5794         #define OBD_FAIL_LDLM_NEW_LOCK
5795         $LCTL set_param fail_loc=0x319
5796         touch $DIR/$tfile && error "touch successful"
5797         $LCTL set_param fail_loc=0
5798         true
5799 }
5800 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5801
5802 num_inodes() {
5803         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5804 }
5805
5806 get_inode_slab_tunables() {
5807         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5808 }
5809
5810 set_inode_slab_tunables() {
5811         echo "lustre_inode_cache $1" > /proc/slabinfo
5812 }
5813
5814 test_76() { # Now for bug 20433, added originally in bug 1443
5815         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5816         local SLAB_SETTINGS=`get_inode_slab_tunables`
5817         local CPUS=`getconf _NPROCESSORS_ONLN`
5818         # we cannot set limit below 1 which means 1 inode in each
5819         # per-cpu cache is still allowed
5820         set_inode_slab_tunables "1 1 0"
5821         cancel_lru_locks osc
5822         BEFORE_INODES=$(num_inodes)
5823         echo "before inodes: $BEFORE_INODES"
5824         local COUNT=1000
5825         [ "$SLOW" = "no" ] && COUNT=100
5826         for i in $(seq $COUNT); do
5827                 touch $DIR/$tfile
5828                 rm -f $DIR/$tfile
5829         done
5830         cancel_lru_locks osc
5831         AFTER_INODES=$(num_inodes)
5832         echo "after inodes: $AFTER_INODES"
5833         local wait=0
5834         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5835                 sleep 2
5836                 AFTER_INODES=$(num_inodes)
5837                 wait=$((wait+2))
5838                 echo "wait $wait seconds inodes: $AFTER_INODES"
5839                 if [ $wait -gt 30 ]; then
5840                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5841                 fi
5842         done
5843         set_inode_slab_tunables "$SLAB_SETTINGS"
5844 }
5845 run_test 76 "confirm clients recycle inodes properly ===="
5846
5847
5848 export ORIG_CSUM=""
5849 set_checksums()
5850 {
5851         # Note: in sptlrpc modes which enable its own bulk checksum, the
5852         # original crc32_le bulk checksum will be automatically disabled,
5853         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5854         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5855         # In this case set_checksums() will not be no-op, because sptlrpc
5856         # bulk checksum will be enabled all through the test.
5857
5858         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5859         lctl set_param -n osc.*.checksums $1
5860         return 0
5861 }
5862
5863 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5864                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5865 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5866 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5867 set_checksum_type()
5868 {
5869         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5870         log "set checksum type to $1"
5871         return 0
5872 }
5873 F77_TMP=$TMP/f77-temp
5874 F77SZ=8
5875 setup_f77() {
5876         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5877                 error "error writing to $F77_TMP"
5878 }
5879
5880 test_77a() { # bug 10889
5881         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5882         $GSS && skip "could not run with gss" && return
5883         [ ! -f $F77_TMP ] && setup_f77
5884         set_checksums 1
5885         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5886         set_checksums 0
5887         rm -f $DIR/$tfile
5888 }
5889 run_test 77a "normal checksum read/write operation"
5890
5891 test_77b() { # bug 10889
5892         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5893         $GSS && skip "could not run with gss" && return
5894         [ ! -f $F77_TMP ] && setup_f77
5895         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5896         $LCTL set_param fail_loc=0x80000409
5897         set_checksums 1
5898
5899         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5900                 error "dd error: $?"
5901         $LCTL set_param fail_loc=0
5902
5903         for algo in $CKSUM_TYPES; do
5904                 cancel_lru_locks osc
5905                 set_checksum_type $algo
5906                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5907                 $LCTL set_param fail_loc=0x80000408
5908                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5909                 $LCTL set_param fail_loc=0
5910         done
5911         set_checksums 0
5912         set_checksum_type $ORIG_CSUM_TYPE
5913         rm -f $DIR/$tfile
5914 }
5915 run_test 77b "checksum error on client write, read"
5916
5917 test_77d() { # bug 10889
5918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5919         $GSS && skip "could not run with gss" && return
5920         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5921         $LCTL set_param fail_loc=0x80000409
5922         set_checksums 1
5923         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5924                 error "direct write: rc=$?"
5925         $LCTL set_param fail_loc=0
5926         set_checksums 0
5927
5928         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5929         $LCTL set_param fail_loc=0x80000408
5930         set_checksums 1
5931         cancel_lru_locks osc
5932         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5933                 error "direct read: rc=$?"
5934         $LCTL set_param fail_loc=0
5935         set_checksums 0
5936 }
5937 run_test 77d "checksum error on OST direct write, read"
5938
5939 test_77f() { # bug 10889
5940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5941         $GSS && skip "could not run with gss" && return
5942         set_checksums 1
5943         for algo in $CKSUM_TYPES; do
5944                 cancel_lru_locks osc
5945                 set_checksum_type $algo
5946                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5947                 $LCTL set_param fail_loc=0x409
5948                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5949                         error "direct write succeeded"
5950                 $LCTL set_param fail_loc=0
5951         done
5952         set_checksum_type $ORIG_CSUM_TYPE
5953         set_checksums 0
5954 }
5955 run_test 77f "repeat checksum error on write (expect error)"
5956
5957 test_77g() { # bug 10889
5958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5959         $GSS && skip "could not run with gss" && return
5960         remote_ost_nodsh && skip "remote OST with nodsh" && return
5961
5962         [ ! -f $F77_TMP ] && setup_f77
5963
5964         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5965         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5966         do_facet ost1 lctl set_param fail_loc=0x8000021a
5967         set_checksums 1
5968         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5969                 error "write error: rc=$?"
5970         do_facet ost1 lctl set_param fail_loc=0
5971         set_checksums 0
5972
5973         cancel_lru_locks osc
5974         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5975         do_facet ost1 lctl set_param fail_loc=0x8000021b
5976         set_checksums 1
5977         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5978         do_facet ost1 lctl set_param fail_loc=0
5979         set_checksums 0
5980 }
5981 run_test 77g "checksum error on OST write, read"
5982
5983 test_77i() { # bug 13805
5984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5985         $GSS && skip "could not run with gss" && return
5986         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5987         lctl set_param fail_loc=0x40b
5988         remount_client $MOUNT
5989         lctl set_param fail_loc=0
5990         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5991                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5992                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5993                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5994         done
5995         remount_client $MOUNT
5996 }
5997 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5998
5999 test_77j() { # bug 13805
6000         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6001         $GSS && skip "could not run with gss" && return
6002         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6003         lctl set_param fail_loc=0x40c
6004         remount_client $MOUNT
6005         lctl set_param fail_loc=0
6006         sleep 2 # wait async osc connect to finish
6007         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
6008                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6009                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
6010                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6011         done
6012         remount_client $MOUNT
6013 }
6014 run_test 77j "client only supporting ADLER32"
6015
6016 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6017 rm -f $F77_TMP
6018 unset F77_TMP
6019
6020 test_78() { # bug 10901
6021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6022         remote_ost || { skip_env "local OST" && return; }
6023
6024         NSEQ=5
6025         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6026         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6027         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6028         echo "MemTotal: $MEMTOTAL"
6029
6030         # reserve 256MB of memory for the kernel and other running processes,
6031         # and then take 1/2 of the remaining memory for the read/write buffers.
6032         if [ $MEMTOTAL -gt 512 ] ;then
6033                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6034         else
6035                 # for those poor memory-starved high-end clusters...
6036                 MEMTOTAL=$((MEMTOTAL / 2))
6037         fi
6038         echo "Mem to use for directio: $MEMTOTAL"
6039
6040         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6041         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6042         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6043         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6044                 head -n1)
6045         echo "Smallest OST: $SMALLESTOST"
6046         [[ $SMALLESTOST -lt 10240 ]] &&
6047                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6048
6049         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6050                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6051
6052         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6053         echo "File size: $F78SIZE"
6054         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6055         for i in $(seq 1 $NSEQ); do
6056                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6057                 echo directIO rdwr round $i of $NSEQ
6058                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6059         done
6060
6061         rm -f $DIR/$tfile
6062 }
6063 run_test 78 "handle large O_DIRECT writes correctly ============"
6064
6065 test_79() { # bug 12743
6066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6067         wait_delete_completed
6068
6069         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6070         BKFREE=$(calc_osc_kbytes kbytesfree)
6071         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6072
6073         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6074         DFTOTAL=`echo $STRING | cut -d, -f1`
6075         DFUSED=`echo $STRING  | cut -d, -f2`
6076         DFAVAIL=`echo $STRING | cut -d, -f3`
6077         DFFREE=$(($DFTOTAL - $DFUSED))
6078
6079         ALLOWANCE=$((64 * $OSTCOUNT))
6080
6081         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6082            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6083                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6084         fi
6085         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6086            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6087                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6088         fi
6089         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6090            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6091                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6092         fi
6093 }
6094 run_test 79 "df report consistency check ======================="
6095
6096 test_80() { # bug 10718
6097         remote_ost_nodsh && skip "remote OST with nodsh" && return
6098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6099         # relax strong synchronous semantics for slow backends like ZFS
6100         local soc="obdfilter.*.sync_on_lock_cancel"
6101         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6102         local hosts=
6103         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6104                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6105                           facet_active_host $host; done | sort -u)
6106                 do_nodes $hosts lctl set_param $soc=never
6107         fi
6108
6109         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6110         sync; sleep 1; sync
6111         local BEFORE=`date +%s`
6112         cancel_lru_locks osc
6113         local AFTER=`date +%s`
6114         local DIFF=$((AFTER-BEFORE))
6115         if [ $DIFF -gt 1 ] ; then
6116                 error "elapsed for 1M@1T = $DIFF"
6117         fi
6118
6119         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6120
6121         rm -f $DIR/$tfile
6122 }
6123 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6124
6125 test_81a() { # LU-456
6126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6127         remote_ost_nodsh && skip "remote OST with nodsh" && return
6128         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6129         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6130         do_facet ost1 lctl set_param fail_loc=0x80000228
6131
6132         # write should trigger a retry and success
6133         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6134         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6135         RC=$?
6136         if [ $RC -ne 0 ] ; then
6137                 error "write should success, but failed for $RC"
6138         fi
6139 }
6140 run_test 81a "OST should retry write when get -ENOSPC ==============="
6141
6142 test_81b() { # LU-456
6143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6144         remote_ost_nodsh && skip "remote OST with nodsh" && return
6145         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6146         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6147         do_facet ost1 lctl set_param fail_loc=0x228
6148
6149         # write should retry several times and return -ENOSPC finally
6150         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6151         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6152         RC=$?
6153         ENOSPC=28
6154         if [ $RC -ne $ENOSPC ] ; then
6155                 error "dd should fail for -ENOSPC, but succeed."
6156         fi
6157 }
6158 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6159
6160 test_82() { # LU-1031
6161         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6162         local gid1=14091995
6163         local gid2=16022000
6164
6165         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6166         local MULTIPID1=$!
6167         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6168         local MULTIPID2=$!
6169         kill -USR1 $MULTIPID2
6170         sleep 2
6171         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6172                 error "First grouplock does not block second one"
6173         else
6174                 echo "Second grouplock blocks first one"
6175         fi
6176         kill -USR1 $MULTIPID1
6177         wait $MULTIPID1
6178         wait $MULTIPID2
6179 }
6180 run_test 82 "Basic grouplock test ==============================="
6181
6182 test_99a() {
6183         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6184                 return
6185         test_mkdir -p $DIR/d99cvsroot
6186         chown $RUNAS_ID $DIR/d99cvsroot
6187         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6188         cd $TMP
6189
6190         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6191         cd $oldPWD
6192 }
6193 run_test 99a "cvs init ========================================="
6194
6195 test_99b() {
6196         [ -z "$(which cvs 2>/dev/null)" ] &&
6197                 skip_env "could not find cvs" && return
6198         [ ! -d $DIR/d99cvsroot ] && test_99a
6199         cd /etc/init.d
6200         # some versions of cvs import exit(1) when asked to import links or
6201         # files they can't read.  ignore those files.
6202         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6203                         ! -perm +4 -printf '-I %f\n')
6204         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6205                 d99reposname vtag rtag
6206 }
6207 run_test 99b "cvs import ======================================="
6208
6209 test_99c() {
6210         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6211         [ ! -d $DIR/d99cvsroot ] && test_99b
6212         cd $DIR
6213         test_mkdir -p $DIR/d99reposname
6214         chown $RUNAS_ID $DIR/d99reposname
6215         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6216 }
6217 run_test 99c "cvs checkout ====================================="
6218
6219 test_99d() {
6220         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6221         [ ! -d $DIR/d99cvsroot ] && test_99c
6222         cd $DIR/d99reposname
6223         $RUNAS touch foo99
6224         $RUNAS cvs add -m 'addmsg' foo99
6225 }
6226 run_test 99d "cvs add =========================================="
6227
6228 test_99e() {
6229         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6230         [ ! -d $DIR/d99cvsroot ] && test_99c
6231         cd $DIR/d99reposname
6232         $RUNAS cvs update
6233 }
6234 run_test 99e "cvs update ======================================="
6235
6236 test_99f() {
6237         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6238         [ ! -d $DIR/d99cvsroot ] && test_99d
6239         cd $DIR/d99reposname
6240         $RUNAS cvs commit -m 'nomsg' foo99
6241     rm -fr $DIR/d99cvsroot
6242 }
6243 run_test 99f "cvs commit ======================================="
6244
6245 test_100() {
6246         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6247         [ "$NETTYPE" = tcp ] || \
6248                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6249                         return ; }
6250
6251         remote_ost_nodsh && skip "remote OST with nodsh" && return
6252         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6253         remote_servers || \
6254                 { skip "useless for local single node setup" && return; }
6255
6256         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6257                 [ "$PROT" != "tcp" ] && continue
6258                 RPORT=$(echo $REMOTE | cut -d: -f2)
6259                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6260
6261                 rc=0
6262                 LPORT=`echo $LOCAL | cut -d: -f2`
6263                 if [ $LPORT -ge 1024 ]; then
6264                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6265                         netstat -tna
6266                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6267                 fi
6268         done
6269         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6270 }
6271 run_test 100 "check local port using privileged port ==========="
6272
6273 function get_named_value()
6274 {
6275     local tag
6276
6277     tag=$1
6278     while read ;do
6279         line=$REPLY
6280         case $line in
6281         $tag*)
6282             echo $line | sed "s/^$tag[ ]*//"
6283             break
6284             ;;
6285         esac
6286     done
6287 }
6288
6289 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6290                    awk '/^max_cached_mb/ { print $2 }')
6291
6292 cleanup_101a() {
6293         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6294         trap 0
6295 }
6296
6297 test_101a() {
6298         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6299         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6300         local s
6301         local discard
6302         local nreads=10000
6303         local cache_limit=32
6304
6305         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6306         trap cleanup_101a EXIT
6307         $LCTL set_param -n llite.*.read_ahead_stats 0
6308         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6309
6310         #
6311         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6312         #
6313         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6314         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6315
6316         discard=0
6317         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6318                 get_named_value 'read but discarded' | cut -d" " -f1); do
6319                         discard=$(($discard + $s))
6320         done
6321         cleanup_101a
6322
6323         if [[ $(($discard * 10)) -gt $nreads ]]; then
6324                 $LCTL get_param osc.*-osc*.rpc_stats
6325                 $LCTL get_param llite.*.read_ahead_stats
6326                 error "too many ($discard) discarded pages"
6327         fi
6328         rm -f $DIR/$tfile || true
6329 }
6330 run_test 101a "check read-ahead for random reads ================"
6331
6332 setup_test101bc() {
6333         test_mkdir -p $DIR/$tdir
6334         local STRIPE_SIZE=$1
6335         local FILE_LENGTH=$2
6336         STRIPE_OFFSET=0
6337
6338         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6339
6340         local list=$(comma_list $(osts_nodes))
6341         set_osd_param $list '' read_cache_enable 0
6342         set_osd_param $list '' writethrough_cache_enable 0
6343
6344         trap cleanup_test101bc EXIT
6345         # prepare the read-ahead file
6346         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6347
6348         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6349                                 count=$FILE_SIZE_MB 2> /dev/null
6350
6351 }
6352
6353 cleanup_test101bc() {
6354         trap 0
6355         rm -rf $DIR/$tdir
6356         rm -f $DIR/$tfile
6357
6358         local list=$(comma_list $(osts_nodes))
6359         set_osd_param $list '' read_cache_enable 1
6360         set_osd_param $list '' writethrough_cache_enable 1
6361 }
6362
6363 calc_total() {
6364         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6365 }
6366
6367 ra_check_101() {
6368         local READ_SIZE=$1
6369         local STRIPE_SIZE=$2
6370         local FILE_LENGTH=$3
6371         local RA_INC=1048576
6372         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6373         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6374                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6375         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6376                         get_named_value 'read but discarded' |
6377                         cut -d" " -f1 | calc_total)
6378         if [[ $DISCARD -gt $discard_limit ]]; then
6379                 $LCTL get_param llite.*.read_ahead_stats
6380                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6381         else
6382                 echo "Read-ahead success for size ${READ_SIZE}"
6383         fi
6384 }
6385
6386 test_101b() {
6387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6388         [[ $OSTCOUNT -lt 2 ]] &&
6389                 skip_env "skipping stride IO stride-ahead test" && return
6390         local STRIPE_SIZE=1048576
6391         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6392         if [ $SLOW == "yes" ]; then
6393                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6394         else
6395                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6396         fi
6397
6398         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6399
6400         # prepare the read-ahead file
6401         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6402         cancel_lru_locks osc
6403         for BIDX in 2 4 8 16 32 64 128 256
6404         do
6405                 local BSIZE=$((BIDX*4096))
6406                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6407                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6408                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6409                 $LCTL set_param -n llite.*.read_ahead_stats 0
6410                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6411                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6412                 cancel_lru_locks osc
6413                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6414         done
6415         cleanup_test101bc
6416         true
6417 }
6418 run_test 101b "check stride-io mode read-ahead ================="
6419
6420 test_101c() {
6421         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6422         local STRIPE_SIZE=1048576
6423         local FILE_LENGTH=$((STRIPE_SIZE*100))
6424         local nreads=10000
6425         local osc_rpc_stats
6426
6427         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6428
6429         cancel_lru_locks osc
6430         $LCTL set_param osc.*.rpc_stats 0
6431         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6432         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6433                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6434                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6435                 local size
6436
6437                 if [ $lines -le 20 ]; then
6438                         continue
6439                 fi
6440                 for size in 1 2 4 8; do
6441                         local rpc=$(echo "$stats" |
6442                                     awk '($1 == "'$size':") {print $2; exit; }')
6443                         [ $rpc != 0 ] &&
6444                                 error "Small $((size*4))k read IO $rpc !"
6445                 done
6446                 echo "$osc_rpc_stats check passed!"
6447         done
6448         cleanup_test101bc
6449         true
6450 }
6451 run_test 101c "check stripe_size aligned read-ahead ================="
6452
6453 set_read_ahead() {
6454         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6455         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6456 }
6457
6458 test_101d() {
6459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6460         local file=$DIR/$tfile
6461         local sz_MB=${FILESIZE_101d:-500}
6462         local ra_MB=${READAHEAD_MB:-40}
6463
6464         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6465         [ $free_MB -lt $sz_MB ] &&
6466                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6467
6468         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6469         $SETSTRIPE -c -1 $file || error "setstripe failed"
6470
6471         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6472         echo Cancel LRU locks on lustre client to flush the client cache
6473         cancel_lru_locks osc
6474
6475         echo Disable read-ahead
6476         local old_READAHEAD=$(set_read_ahead 0)
6477
6478         echo Reading the test file $file with read-ahead disabled
6479         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6480
6481         echo Cancel LRU locks on lustre client to flush the client cache
6482         cancel_lru_locks osc
6483         echo Enable read-ahead with ${ra_MB}MB
6484         set_read_ahead $ra_MB
6485
6486         echo Reading the test file $file with read-ahead enabled
6487         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6488
6489         echo "read-ahead disabled time read $raOFF"
6490         echo "read-ahead enabled  time read $raON"
6491
6492         set_read_ahead $old_READAHEAD
6493         rm -f $file
6494         wait_delete_completed
6495
6496         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6497                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6498 }
6499 run_test 101d "file read with and without read-ahead enabled"
6500
6501 test_101e() {
6502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6503         local file=$DIR/$tfile
6504         local size_KB=500  #KB
6505         local count=100
6506         local bsize=1024
6507
6508         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6509         local need_KB=$((count * size_KB))
6510         [[ $free_KB -le $need_KB ]] &&
6511                 skip_env "Need free space $need_KB, have $free_KB" && return
6512
6513         echo "Creating $count ${size_KB}K test files"
6514         for ((i = 0; i < $count; i++)); do
6515                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6516         done
6517
6518         echo "Cancel LRU locks on lustre client to flush the client cache"
6519         cancel_lru_locks osc
6520
6521         echo "Reset readahead stats"
6522         $LCTL set_param -n llite.*.read_ahead_stats 0
6523
6524         for ((i = 0; i < $count; i++)); do
6525                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6526         done
6527
6528         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6529                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6530
6531         for ((i = 0; i < $count; i++)); do
6532                 rm -rf $file.$i 2>/dev/null
6533         done
6534
6535         #10000 means 20% reads are missing in readahead
6536         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6537 }
6538 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6539
6540 cleanup_test101f() {
6541     trap 0
6542     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6543     rm -rf $DIR/$tfile 2>/dev/null
6544 }
6545
6546 test_101f() {
6547         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6548     local file=$DIR/$tfile
6549     local nreads=1000
6550
6551     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6552     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6553     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6554     trap cleanup_test101f EXIT
6555
6556     echo Cancel LRU locks on lustre client to flush the client cache
6557     cancel_lru_locks osc
6558
6559     echo Reset readahead stats
6560     $LCTL set_param -n llite.*.read_ahead_stats 0
6561     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6562     # readahead should read in 2M file on second read, so only miss
6563     # 2 pages.
6564     echo Random 4K reads on 2M file for 1000 times
6565     $READS -f $file -s 2097152 -b 4096 -n $nreads
6566
6567     echo checking missing pages
6568     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6569           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6570
6571     [ $miss -lt 3 ] || error "misses too much pages!"
6572     cleanup_test101f
6573 }
6574 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6575
6576 setup_test102() {
6577         test_mkdir -p $DIR/$tdir
6578         chown $RUNAS_ID $DIR/$tdir
6579         STRIPE_SIZE=65536
6580         STRIPE_OFFSET=1
6581         STRIPE_COUNT=$OSTCOUNT
6582         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6583
6584         trap cleanup_test102 EXIT
6585         cd $DIR
6586         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6587         cd $DIR/$tdir
6588         for num in 1 2 3 4; do
6589                 for count in $(seq 1 $STRIPE_COUNT); do
6590                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6591                                 local size=`expr $STRIPE_SIZE \* $num`
6592                                 local file=file"$num-$idx-$count"
6593                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6594                         done
6595                 done
6596         done
6597
6598         cd $DIR
6599         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6600 }
6601
6602 cleanup_test102() {
6603         trap 0
6604         rm -f $TMP/f102.tar
6605         rm -rf $DIR/d0.sanity/d102
6606 }
6607
6608 test_102a() {
6609         local testfile=$DIR/$tfile
6610
6611         touch $testfile
6612
6613         [ "$UID" != 0 ] && skip_env "must run as root" && return
6614         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6615                 skip_env "must have user_xattr" && return
6616
6617         [ -z "$(which setfattr 2>/dev/null)" ] &&
6618                 skip_env "could not find setfattr" && return
6619
6620         echo "set/get xattr..."
6621         setfattr -n trusted.name1 -v value1 $testfile ||
6622                 error "setfattr -n trusted.name1=value1 $testfile failed"
6623         getfattr -n trusted.name1 $testfile 2> /dev/null |
6624           grep "trusted.name1=.value1" ||
6625                 error "$testfile missing trusted.name1=value1"
6626
6627         setfattr -n user.author1 -v author1 $testfile ||
6628                 error "setfattr -n user.author1=author1 $testfile failed"
6629         getfattr -n user.author1 $testfile 2> /dev/null |
6630           grep "user.author1=.author1" ||
6631                 error "$testfile missing trusted.author1=author1"
6632
6633         echo "listxattr..."
6634         setfattr -n trusted.name2 -v value2 $testfile ||
6635                 error "$testfile unable to set trusted.name2"
6636         setfattr -n trusted.name3 -v value3 $testfile ||
6637                 error "$testfile unable to set trusted.name3"
6638         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6639             grep "trusted.name" | wc -l) -eq 3 ] ||
6640                 error "$testfile missing 3 trusted.name xattrs"
6641
6642         setfattr -n user.author2 -v author2 $testfile ||
6643                 error "$testfile unable to set user.author2"
6644         setfattr -n user.author3 -v author3 $testfile ||
6645                 error "$testfile unable to set user.author3"
6646         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6647             grep "user.author" | wc -l) -eq 3 ] ||
6648                 error "$testfile missing 3 user.author xattrs"
6649
6650         echo "remove xattr..."
6651         setfattr -x trusted.name1 $testfile ||
6652                 error "$testfile error deleting trusted.name1"
6653         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6654                 error "$testfile did not delete trusted.name1 xattr"
6655
6656         setfattr -x user.author1 $testfile ||
6657                 error "$testfile error deleting user.author1"
6658         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6659                 error "$testfile did not delete trusted.name1 xattr"
6660
6661         # b10667: setting lustre special xattr be silently discarded
6662         echo "set lustre special xattr ..."
6663         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6664                 error "$testfile allowed setting trusted.lov"
6665 }
6666 run_test 102a "user xattr test =================================="
6667
6668 test_102b() {
6669         [ -z "$(which setfattr 2>/dev/null)" ] &&
6670                 skip_env "could not find setfattr" && return
6671
6672         # b10930: get/set/list trusted.lov xattr
6673         echo "get/set/list trusted.lov xattr ..."
6674         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6675         local testfile=$DIR/$tfile
6676         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6677                 error "setstripe failed"
6678         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6679                 error "getstripe failed"
6680         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6681                 error "can't get trusted.lov from $testfile"
6682
6683         local testfile2=${testfile}2
6684         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6685                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6686
6687         $MCREATE $testfile2
6688         setfattr -n trusted.lov -v $value $testfile2
6689         local stripe_size=$($GETSTRIPE -S $testfile2)
6690         local stripe_count=$($GETSTRIPE -c $testfile2)
6691         [[ $stripe_size -eq 65536 ]] ||
6692                 error "stripe size $stripe_size != 65536"
6693         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6694                 error "stripe count $stripe_count != $STRIPECOUNT"
6695         rm -f $DIR/$tfile
6696 }
6697 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6698
6699 test_102c() {
6700         [ -z "$(which setfattr 2>/dev/null)" ] &&
6701                 skip_env "could not find setfattr" && return
6702
6703         # b10930: get/set/list lustre.lov xattr
6704         echo "get/set/list lustre.lov xattr ..."
6705         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6706         test_mkdir -p $DIR/$tdir
6707         chown $RUNAS_ID $DIR/$tdir
6708         local testfile=$DIR/$tdir/$tfile
6709         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6710                 error "setstripe failed"
6711         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6712                 error "getstripe failed"
6713         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6714         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6715
6716         local testfile2=${testfile}2
6717         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6718                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6719
6720         $RUNAS $MCREATE $testfile2
6721         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6722         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6723         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6724         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6725         [ $stripe_count -eq $STRIPECOUNT ] ||
6726                 error "stripe count $stripe_count != $STRIPECOUNT"
6727 }
6728 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6729
6730 compare_stripe_info1() {
6731         local stripe_index_all_zero=true
6732
6733         for num in 1 2 3 4; do
6734                 for count in $(seq 1 $STRIPE_COUNT); do
6735                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6736                                 local size=$((STRIPE_SIZE * num))
6737                                 local file=file"$num-$offset-$count"
6738                                 stripe_size=$($LFS getstripe -S $PWD/$file)
6739                                 [[ $stripe_size -ne $size ]] &&
6740                                     error "$file: size $stripe_size != $size"
6741                                 stripe_count=$($LFS getstripe -c $PWD/$file)
6742                                 # allow fewer stripes to be created, ORI-601
6743                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6744                                     error "$file: count $stripe_count != $count"
6745                                 stripe_index=$($LFS getstripe -i $PWD/$file)
6746                                 [[ $stripe_index -ne 0 ]] &&
6747                                         stripe_index_all_zero=false
6748                         done
6749                 done
6750         done
6751         $stripe_index_all_zero &&
6752                 error "all files are being extracted starting from OST index 0"
6753         return 0
6754 }
6755
6756 find_lustre_tar() {
6757         [ -n "$(which tar 2>/dev/null)" ] &&
6758                 strings $(which tar) | grep -q "lustre" && echo tar
6759 }
6760
6761 test_102d() {
6762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6763         # b10930: tar test for trusted.lov xattr
6764         TAR=$(find_lustre_tar)
6765         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6766         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6767         setup_test102
6768         test_mkdir -p $DIR/d102d
6769         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6770         cd $DIR/d102d/$tdir
6771         compare_stripe_info1
6772 }
6773 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6774
6775 test_102f() {
6776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6777         # b10930: tar test for trusted.lov xattr
6778         TAR=$(find_lustre_tar)
6779         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6780         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6781         setup_test102
6782         test_mkdir -p $DIR/d102f
6783         cd $DIR
6784         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6785         cd $DIR/d102f/$tdir
6786         compare_stripe_info1
6787 }
6788 run_test 102f "tar copy files, not keep osts ==========="
6789
6790 grow_xattr() {
6791         local xsize=${1:-1024}  # in bytes
6792         local file=$DIR/$tfile
6793
6794         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
6795                 skip "must have user_xattr" && return 0
6796         [ -z "$(which setfattr 2>/dev/null)" ] &&
6797                 skip_env "could not find setfattr" && return 0
6798         [ -z "$(which getfattr 2>/dev/null)" ] &&
6799                 skip_env "could not find getfattr" && return 0
6800
6801         touch $file
6802
6803         local value="$(generate_string $xsize)"
6804
6805         local xbig=trusted.big
6806         log "save $xbig on $file"
6807         setfattr -n $xbig -v $value $file ||
6808                 error "saving $xbig on $file failed"
6809
6810         local orig=$(get_xattr_value $xbig $file)
6811         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6812
6813         local xsml=trusted.sml
6814         log "save $xsml on $file"
6815         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6816
6817         local new=$(get_xattr_value $xbig $file)
6818         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6819
6820         log "grow $xsml on $file"
6821         setfattr -n $xsml -v "$value" $file ||
6822                 error "growing $xsml on $file failed"
6823
6824         new=$(get_xattr_value $xbig $file)
6825         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6826         log "$xbig still valid after growing $xsml"
6827
6828         rm -f $file
6829 }
6830
6831 test_102h() { # bug 15777
6832         grow_xattr 1024
6833 }
6834 run_test 102h "grow xattr from inside inode to external block"
6835
6836 test_102ha() {
6837         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6838         grow_xattr $(max_xattr_size)
6839 }
6840 run_test 102ha "grow xattr from inside inode to external inode"
6841
6842 test_102i() { # bug 17038
6843         [ -z "$(which getfattr 2>/dev/null)" ] &&
6844                 skip "could not find getfattr" && return
6845         touch $DIR/$tfile
6846         ln -s $DIR/$tfile $DIR/${tfile}link
6847         getfattr -n trusted.lov $DIR/$tfile ||
6848                 error "lgetxattr on $DIR/$tfile failed"
6849         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6850                 grep -i "no such attr" ||
6851                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6852         rm -f $DIR/$tfile $DIR/${tfile}link
6853 }
6854 run_test 102i "lgetxattr test on symbolic link ============"
6855
6856 test_102j() {
6857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6858         TAR=$(find_lustre_tar)
6859         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6860         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6861         setup_test102 "$RUNAS"
6862         test_mkdir -p $DIR/d102j
6863         chown $RUNAS_ID $DIR/d102j
6864         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6865         cd $DIR/d102j/$tdir
6866         compare_stripe_info1 "$RUNAS"
6867 }
6868 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6869
6870 test_102k() {
6871         [ -z "$(which setfattr 2>/dev/null)" ] &&
6872                 skip "could not find setfattr" && return
6873         touch $DIR/$tfile
6874         # b22187 just check that does not crash for regular file.
6875         setfattr -n trusted.lov $DIR/$tfile
6876         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6877         local test_kdir=$DIR/d102k
6878         test_mkdir $test_kdir
6879         local default_size=`$GETSTRIPE -S $test_kdir`
6880         local default_count=`$GETSTRIPE -c $test_kdir`
6881         local default_offset=`$GETSTRIPE -i $test_kdir`
6882         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6883                 error 'dir setstripe failed'
6884         setfattr -n trusted.lov $test_kdir
6885         local stripe_size=`$GETSTRIPE -S $test_kdir`
6886         local stripe_count=`$GETSTRIPE -c $test_kdir`
6887         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6888         [ $stripe_size -eq $default_size ] ||
6889                 error "stripe size $stripe_size != $default_size"
6890         [ $stripe_count -eq $default_count ] ||
6891                 error "stripe count $stripe_count != $default_count"
6892         [ $stripe_offset -eq $default_offset ] ||
6893                 error "stripe offset $stripe_offset != $default_offset"
6894         rm -rf $DIR/$tfile $test_kdir
6895 }
6896 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6897
6898 test_102l() {
6899         [ -z "$(which getfattr 2>/dev/null)" ] &&
6900                 skip "could not find getfattr" && return
6901
6902         # LU-532 trusted. xattr is invisible to non-root
6903         local testfile=$DIR/$tfile
6904
6905         touch $testfile
6906
6907         echo "listxattr as user..."
6908         chown $RUNAS_ID $testfile
6909         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6910             grep -q "trusted" &&
6911                 error "$testfile trusted xattrs are user visible"
6912
6913         return 0;
6914 }
6915 run_test 102l "listxattr size test =================================="
6916
6917 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6918         local path=$DIR/$tfile
6919         touch $path
6920
6921         listxattr_size_check $path || error "listattr_size_check $path failed"
6922 }
6923 run_test 102m "Ensure listxattr fails on small bufffer ========"
6924
6925 cleanup_test102
6926
6927 getxattr() { # getxattr path name
6928         # Return the base64 encoding of the value of xattr name on path.
6929         local path=$1
6930         local name=$2
6931
6932         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6933         # file: $path
6934         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6935         #
6936         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6937
6938         getfattr --absolute-names --encoding=base64 --name=$name $path |
6939                 awk -F= -v name=$name '$1 == name {
6940                         print substr($0, index($0, "=") + 1);
6941         }'
6942 }
6943
6944 test_102n() { # LU-4101 mdt: protect internal xattrs
6945         local file0=$DIR/$tfile.0
6946         local file1=$DIR/$tfile.1
6947         local xattr0=$TMP/$tfile.0
6948         local xattr1=$TMP/$tfile.1
6949         local name
6950         local value
6951
6952         [ -z "$(which setfattr 2>/dev/null)" ] &&
6953                 skip "could not find setfattr" && return
6954
6955         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6956         then
6957                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6958                 return
6959         fi
6960
6961         rm -rf $file0 $file1 $xattr0 $xattr1
6962         touch $file0 $file1
6963
6964         # Get 'before' xattrs of $file1.
6965         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6966
6967         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6968                 # Try to copy xattr from $file0 to $file1.
6969                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6970
6971                 setfattr --name=trusted.$name --value="$value" $file1 ||
6972                         error "setxattr 'trusted.$name' failed"
6973
6974                 # Try to set a garbage xattr.
6975                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6976
6977                 setfattr --name=trusted.$name --value="$value" $file1 ||
6978                         error "setxattr 'trusted.$name' failed"
6979
6980                 # Try to remove the xattr from $file1. We don't care if this
6981                 # appears to succeed or fail, we just don't want there to be
6982                 # any changes or crashes.
6983                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6984         done
6985
6986         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6987         then
6988                 name="lfsck_ns"
6989                 # Try to copy xattr from $file0 to $file1.
6990                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6991
6992                 setfattr --name=trusted.$name --value="$value" $file1 ||
6993                         error "setxattr 'trusted.$name' failed"
6994
6995                 # Try to set a garbage xattr.
6996                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6997
6998                 setfattr --name=trusted.$name --value="$value" $file1 ||
6999                         error "setxattr 'trusted.$name' failed"
7000
7001                 # Try to remove the xattr from $file1. We don't care if this
7002                 # appears to succeed or fail, we just don't want there to be
7003                 # any changes or crashes.
7004                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7005         fi
7006
7007         # Get 'after' xattrs of file1.
7008         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7009
7010         if ! diff $xattr0 $xattr1; then
7011                 error "before and after xattrs of '$file1' differ"
7012         fi
7013
7014         rm -rf $file0 $file1 $xattr0 $xattr1
7015
7016         return 0
7017 }
7018 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7019
7020 test_102p() { # LU-4703 setxattr did not check ownership
7021         local testfile=$DIR/$tfile
7022
7023         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7024                 skip "MDS needs to be at least 2.5.56" && return
7025
7026         touch $testfile
7027
7028         echo "setfacl as user..."
7029         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7030         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7031
7032         echo "setfattr as user..."
7033         setfacl -m "u:$RUNAS_ID:---" $testfile
7034         $RUNAS setfattr -x system.posix_acl_access $testfile
7035         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7036 }
7037 run_test 102p "check setxattr(2) correctly fails without permission"
7038
7039 test_102q() {
7040         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7041                 skip "MDS needs to be at least 2.6.92" && return
7042         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7043 }
7044 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7045
7046 test_102r() {
7047         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7048                 skip "MDS needs to be at least 2.6.93" && return
7049         touch $DIR/$tfile || error "touch"
7050         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7051         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7052         rm $DIR/$tfile || error "rm"
7053
7054         #normal directory
7055         mkdir -p $DIR/$tdir || error "mkdir"
7056         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7057         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7058         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7059                 error "$testfile error deleting user.author1"
7060         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7061                 grep "user.$(basename $tdir)" &&
7062                 error "$tdir did not delete user.$(basename $tdir)"
7063         rmdir $DIR/$tdir || error "rmdir"
7064
7065         #striped directory
7066         test_mkdir -p $DIR/$tdir || error "make striped dir"
7067         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7068         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7069         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7070                 error "$testfile error deleting user.author1"
7071         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7072                 grep "user.$(basename $tdir)" &&
7073                 error "$tdir did not delete user.$(basename $tdir)"
7074         rmdir $DIR/$tdir || error "rm striped dir"
7075 }
7076 run_test 102r "set EAs with empty values"
7077
7078 run_acl_subtest()
7079 {
7080     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7081     return $?
7082 }
7083
7084 test_103a() {
7085         [ "$UID" != 0 ] && skip_env "must run as root" && return
7086         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7087                 skip "must have acl enabled" && return
7088         [ -z "$(which setfacl 2>/dev/null)" ] &&
7089                 skip_env "could not find setfacl" && return
7090         $GSS && skip "could not run under gss" && return
7091
7092         gpasswd -a daemon bin                           # LU-5641
7093         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7094
7095         declare -a identity_old
7096
7097         for num in $(seq $MDSCOUNT); do
7098                 switch_identity $num true || identity_old[$num]=$?
7099         done
7100
7101         SAVE_UMASK=$(umask)
7102         umask 0022
7103         cd $DIR
7104
7105         echo "performing cp ..."
7106         run_acl_subtest cp || error "run_acl_subtest cp failed"
7107         echo "performing getfacl-noacl..."
7108         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7109         echo "performing misc..."
7110         run_acl_subtest misc || error  "misc test failed"
7111         echo "performing permissions..."
7112         run_acl_subtest permissions || error "permissions failed"
7113         echo "performing setfacl..."
7114         run_acl_subtest setfacl || error  "setfacl test failed"
7115
7116         # inheritance test got from HP
7117         echo "performing inheritance..."
7118         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7119         chmod +x make-tree || error "chmod +x failed"
7120         run_acl_subtest inheritance || error "inheritance test failed"
7121         rm -f make-tree
7122
7123         echo "LU-974 ignore umask when acl is enabled..."
7124         run_acl_subtest 974 || error "LU-974 umask test failed"
7125         if [ $MDSCOUNT -ge 2 ]; then
7126                 run_acl_subtest 974_remote ||
7127                         error "LU-974 umask test failed under remote dir"
7128         fi
7129
7130         echo "LU-2561 newly created file is same size as directory..."
7131         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7132                 run_acl_subtest 2561 || error "LU-2561 test failed"
7133         else
7134                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7135         fi
7136
7137         run_acl_subtest 4924 || error "LU-4924 test failed"
7138
7139         cd $SAVE_PWD
7140         umask $SAVE_UMASK
7141
7142         for num in $(seq $MDSCOUNT); do
7143                 if [ "${identity_old[$num]}" = 1 ]; then
7144                         switch_identity $num false || identity_old[$num]=$?
7145                 fi
7146         done
7147 }
7148 run_test 103a "acl test ========================================="
7149
7150 test_103b() {
7151         local noacl=false
7152         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7153         local mountopts=$MDS_MOUNT_OPTS
7154
7155         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7156                 noacl=true
7157         else
7158                 # stop the MDT
7159                 stop $SINGLEMDS || error "failed to stop MDT."
7160                 # remount the MDT
7161                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7162                         MDS_MOUNT_OPTS="-o noacl"
7163                 else
7164                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7165                 fi
7166                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7167                         error "failed to start MDT."
7168                 MDS_MOUNT_OPTS=$mountopts
7169         fi
7170
7171         touch $DIR/$tfile
7172         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7173
7174         if ! $noacl; then
7175                 # stop the MDT
7176                 stop $SINGLEMDS || error "failed to stop MDT."
7177                 # remount the MDT
7178                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7179                         error "failed to start MDT."
7180         fi
7181
7182         true
7183 }
7184 run_test 103b "MDS mount option 'noacl'"
7185
7186 test_103c() {
7187         mkdir -p $DIR/$tdir
7188         cp -rp $DIR/$tdir $DIR/$tdir.bak
7189
7190         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7191                 error "$DIR/$tdir shouldn't contain default ACL"
7192         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7193                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7194         true
7195 }
7196 run_test 103c "'cp -rp' won't set empty acl"
7197
7198 test_104a() {
7199         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7200         touch $DIR/$tfile
7201         lfs df || error "lfs df failed"
7202         lfs df -ih || error "lfs df -ih failed"
7203         lfs df -h $DIR || error "lfs df -h $DIR failed"
7204         lfs df -i $DIR || error "lfs df -i $DIR failed"
7205         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7206         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7207
7208         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7209         lctl --device %$OSC deactivate
7210         lfs df || error "lfs df with deactivated OSC failed"
7211         lctl --device %$OSC activate
7212         # wait the osc back to normal
7213         wait_osc_import_state client ost FULL
7214
7215         lfs df || error "lfs df with reactivated OSC failed"
7216         rm -f $DIR/$tfile
7217 }
7218 run_test 104a "lfs df [-ih] [path] test ========================="
7219
7220 test_104b() {
7221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7222         [ $RUNAS_ID -eq $UID ] &&
7223                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7224         chmod 666 /dev/obd
7225         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7226                         grep "Permission denied" | wc -l)))
7227         if [ $denied_cnt -ne 0 ]; then
7228                 error "lfs check servers test failed"
7229         fi
7230 }
7231 run_test 104b "$RUNAS lfs check servers test ===================="
7232
7233 test_105a() {
7234         # doesn't work on 2.4 kernels
7235         touch $DIR/$tfile
7236         if $(flock_is_enabled); then
7237                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7238         else
7239                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7240         fi
7241         rm -f $DIR/$tfile
7242 }
7243 run_test 105a "flock when mounted without -o flock test ========"
7244
7245 test_105b() {
7246         touch $DIR/$tfile
7247         if $(flock_is_enabled); then
7248                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7249         else
7250                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7251         fi
7252         rm -f $DIR/$tfile
7253 }
7254 run_test 105b "fcntl when mounted without -o flock test ========"
7255
7256 test_105c() {
7257         touch $DIR/$tfile
7258         if $(flock_is_enabled); then
7259                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7260         else
7261                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7262         fi
7263         rm -f $DIR/$tfile
7264 }
7265 run_test 105c "lockf when mounted without -o flock test ========"
7266
7267 test_105d() { # bug 15924
7268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7269         test_mkdir -p $DIR/$tdir
7270         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7271         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7272         $LCTL set_param fail_loc=0x80000315
7273         flocks_test 2 $DIR/$tdir
7274 }
7275 run_test 105d "flock race (should not freeze) ========"
7276
7277 test_105e() { # bug 22660 && 22040
7278         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7279         touch $DIR/$tfile
7280         flocks_test 3 $DIR/$tfile
7281 }
7282 run_test 105e "Two conflicting flocks from same process ======="
7283
7284 test_106() { #bug 10921
7285         test_mkdir -p $DIR/$tdir
7286         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7287         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7288 }
7289 run_test 106 "attempt exec of dir followed by chown of that dir"
7290
7291 test_107() {
7292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7293         CDIR=`pwd`
7294         cd $DIR
7295
7296         local file=core
7297         rm -f $file
7298
7299         local save_pattern=$(sysctl -n kernel.core_pattern)
7300         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7301         sysctl -w kernel.core_pattern=$file
7302         sysctl -w kernel.core_uses_pid=0
7303
7304         ulimit -c unlimited
7305         sleep 60 &
7306         SLEEPPID=$!
7307
7308         sleep 1
7309
7310         kill -s 11 $SLEEPPID
7311         wait $SLEEPPID
7312         if [ -e $file ]; then
7313                 size=`stat -c%s $file`
7314                 [ $size -eq 0 ] && error "Fail to create core file $file"
7315         else
7316                 error "Fail to create core file $file"
7317         fi
7318         rm -f $file
7319         sysctl -w kernel.core_pattern=$save_pattern
7320         sysctl -w kernel.core_uses_pid=$save_uses_pid
7321         cd $CDIR
7322 }
7323 run_test 107 "Coredump on SIG"
7324
7325 test_110() {
7326         test_mkdir -p $DIR/$tdir
7327         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7328                 error "mkdir with 255 char failed"
7329         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7330                 error "mkdir with 256 char should fail, but did not"
7331         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7332                 error "create with 255 char failed"
7333         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7334                 error "create with 256 char should fail, but did not"
7335
7336         ls -l $DIR/$tdir
7337         rm -rf $DIR/$tdir
7338 }
7339 run_test 110 "filename length checking"
7340
7341 test_115() {
7342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7343         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7344                 tail -1 | cut -c11-20)
7345         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7346         echo "Starting with $OSTIO_pre threads"
7347
7348         NUMTEST=20000
7349         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7350         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7351         echo "$NUMTEST creates/unlinks"
7352         test_mkdir -p $DIR/$tdir
7353         createmany -o $DIR/$tdir/$tfile $NUMTEST
7354         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7355
7356         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7357                 tail -1 | cut -c11-20)
7358
7359         # don't return an error
7360         [ $OSTIO_post == $OSTIO_pre ] && echo \
7361             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7362             echo "This may be fine, depending on what ran before this test" &&
7363             echo "and how fast this system is." && return
7364
7365         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7366 }
7367 run_test 115 "verify dynamic thread creation===================="
7368
7369 free_min_max () {
7370         wait_delete_completed
7371         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7372         echo OST kbytes available: ${AVAIL[@]}
7373         MAXI=0; MAXV=${AVAIL[0]}
7374         MINI=0; MINV=${AVAIL[0]}
7375         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7376                 #echo OST $i: ${AVAIL[i]}kb
7377                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7378                         MAXV=${AVAIL[i]}; MAXI=$i
7379                 fi
7380                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7381                         MINV=${AVAIL[i]}; MINI=$i
7382                 fi
7383         done
7384         echo Min free space: OST $MINI: $MINV
7385         echo Max free space: OST $MAXI: $MAXV
7386 }
7387
7388 test_116a() { # was previously test_116()
7389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7390         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7391
7392         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7393
7394         echo -n "Free space priority "
7395         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7396                 head -n1
7397         declare -a AVAIL
7398         free_min_max
7399
7400         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7401         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7402                 && return
7403         trap simple_cleanup_common EXIT
7404
7405
7406         # Check if we need to generate uneven OSTs
7407         test_mkdir -p $DIR/$tdir/OST${MINI}
7408         local FILL=$(($MINV / 4))
7409         local DIFF=$(($MAXV - $MINV))
7410         local DIFF2=$(($DIFF * 100 / $MINV))
7411
7412         local threshold=$(do_facet $SINGLEMDS \
7413                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7414         threshold=${threshold%%%}
7415         echo -n "Check for uneven OSTs: "
7416         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7417
7418         if [[ $DIFF2 -gt $threshold ]]; then
7419                 echo "ok"
7420                 echo "Don't need to fill OST$MINI"
7421         else
7422                 # generate uneven OSTs. Write 2% over the QOS threshold value
7423                 echo "no"
7424                 DIFF=$(($threshold - $DIFF2 + 2))
7425                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7426                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7427                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7428                         error "setstripe failed"
7429                 DIFF=$(($DIFF2 / 2048))
7430                 i=0
7431                 while [ $i -lt $DIFF ]; do
7432                         i=$(($i + 1))
7433                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7434                                 bs=2M count=1 2>/dev/null
7435                         echo -n .
7436                 done
7437                 echo .
7438                 sync
7439                 sleep_maxage
7440                 free_min_max
7441         fi
7442
7443         DIFF=$(($MAXV - $MINV))
7444         DIFF2=$(($DIFF * 100 / $MINV))
7445         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7446         if [[ $DIFF2 -gt $threshold ]]; then
7447                 echo "ok"
7448         else
7449                 echo "failed - QOS mode won't be used"
7450                 skip "QOS imbalance criteria not met"
7451                 simple_cleanup_common
7452                 return
7453         fi
7454
7455         MINI1=$MINI; MINV1=$MINV
7456         MAXI1=$MAXI; MAXV1=$MAXV
7457
7458         # now fill using QOS
7459         $SETSTRIPE -c 1 $DIR/$tdir
7460         FILL=$(($FILL / 200))
7461         if [ $FILL -gt 600 ]; then
7462                 FILL=600
7463         fi
7464         echo "writing $FILL files to QOS-assigned OSTs"
7465         i=0
7466         while [ $i -lt $FILL ]; do
7467                 i=$((i + 1))
7468                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7469                         count=1 2>/dev/null
7470                 echo -n .
7471         done
7472         echo "wrote $i 200k files"
7473         sync
7474         sleep_maxage
7475
7476         echo "Note: free space may not be updated, so measurements might be off"
7477         free_min_max
7478         DIFF2=$(($MAXV - $MINV))
7479         echo "free space delta: orig $DIFF final $DIFF2"
7480         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7481         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7482         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7483         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7484         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7485         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7486         [ $DIFF -gt 0 ] &&
7487                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7488
7489         # Figure out which files were written where
7490         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7491                   awk '/'$MINI1': / {print $2; exit}')
7492         echo $UUID
7493         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7494         echo "$MINC files created on smaller OST $MINI1"
7495         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7496                   awk '/'$MAXI1': / {print $2; exit}')
7497         echo $UUID
7498         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7499         echo "$MAXC files created on larger OST $MAXI1"
7500         FILL=$(($MAXC * 100 / $MINC - 100))
7501         [[ $MINC -gt 0 ]] &&
7502                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7503         [[ $MAXC -gt $MINC ]] ||
7504                 error_ignore LU-9 "stripe QOS didn't balance free space"
7505         simple_cleanup_common
7506 }
7507 run_test 116a "stripe QOS: free space balance ==================="
7508
7509 test_116b() { # LU-2093
7510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7511         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7512
7513 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7514         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7515                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7516         [ -z "$old_rr" ] && skip "no QOS" && return 0
7517         do_facet $SINGLEMDS lctl set_param \
7518                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7519         mkdir -p $DIR/$tdir
7520         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7521         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7522         do_facet $SINGLEMDS lctl set_param fail_loc=0
7523         rm -rf $DIR/$tdir
7524         do_facet $SINGLEMDS lctl set_param \
7525                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7526 }
7527 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7528
7529 test_117() # bug 10891
7530 {
7531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7532         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7533         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7534         lctl set_param fail_loc=0x21e
7535         > $DIR/$tfile || error "truncate failed"
7536         lctl set_param fail_loc=0
7537         echo "Truncate succeeded."
7538         rm -f $DIR/$tfile
7539 }
7540 run_test 117 "verify osd extend =========="
7541
7542 NO_SLOW_RESENDCOUNT=4
7543 export OLD_RESENDCOUNT=""
7544 set_resend_count () {
7545         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7546         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7547         lctl set_param -n $PROC_RESENDCOUNT $1
7548         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7549 }
7550
7551 # for reduce test_118* time (b=14842)
7552 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7553
7554 # Reset async IO behavior after error case
7555 reset_async() {
7556         FILE=$DIR/reset_async
7557
7558         # Ensure all OSCs are cleared
7559         $SETSTRIPE -c -1 $FILE
7560         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7561         sync
7562         rm $FILE
7563 }
7564
7565 test_118a() #bug 11710
7566 {
7567         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7568         reset_async
7569
7570         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7571         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7572         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7573
7574         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7575                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7576                 return 1;
7577         fi
7578         rm -f $DIR/$tfile
7579 }
7580 run_test 118a "verify O_SYNC works =========="
7581
7582 test_118b()
7583 {
7584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7585         remote_ost_nodsh && skip "remote OST with nodsh" && return
7586
7587         reset_async
7588
7589         #define OBD_FAIL_SRV_ENOENT 0x217
7590         set_nodes_failloc "$(osts_nodes)" 0x217
7591         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7592         RC=$?
7593         set_nodes_failloc "$(osts_nodes)" 0
7594         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7595         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7596                     grep -c writeback)
7597
7598         if [[ $RC -eq 0 ]]; then
7599                 error "Must return error due to dropped pages, rc=$RC"
7600                 return 1;
7601         fi
7602
7603         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7604                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7605                 return 1;
7606         fi
7607
7608         echo "Dirty pages not leaked on ENOENT"
7609
7610         # Due to the above error the OSC will issue all RPCs syncronously
7611         # until a subsequent RPC completes successfully without error.
7612         $MULTIOP $DIR/$tfile Ow4096yc
7613         rm -f $DIR/$tfile
7614
7615         return 0
7616 }
7617 run_test 118b "Reclaim dirty pages on fatal error =========="
7618
7619 test_118c()
7620 {
7621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7622
7623         # for 118c, restore the original resend count, LU-1940
7624         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7625                                 set_resend_count $OLD_RESENDCOUNT
7626         remote_ost_nodsh && skip "remote OST with nodsh" && return
7627
7628         reset_async
7629
7630         #define OBD_FAIL_OST_EROFS               0x216
7631         set_nodes_failloc "$(osts_nodes)" 0x216
7632
7633         # multiop should block due to fsync until pages are written
7634         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7635         MULTIPID=$!
7636         sleep 1
7637
7638         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7639                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7640         fi
7641
7642         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7643                     grep -c writeback)
7644         if [[ $WRITEBACK -eq 0 ]]; then
7645                 error "No page in writeback, writeback=$WRITEBACK"
7646         fi
7647
7648         set_nodes_failloc "$(osts_nodes)" 0
7649         wait $MULTIPID
7650         RC=$?
7651         if [[ $RC -ne 0 ]]; then
7652                 error "Multiop fsync failed, rc=$RC"
7653         fi
7654
7655         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7656         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7657                     grep -c writeback)
7658         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7659                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7660         fi
7661
7662         rm -f $DIR/$tfile
7663         echo "Dirty pages flushed via fsync on EROFS"
7664         return 0
7665 }
7666 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7667
7668 # continue to use small resend count to reduce test_118* time (b=14842)
7669 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7670
7671 test_118d()
7672 {
7673         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7674         remote_ost_nodsh && skip "remote OST with nodsh" && return
7675
7676         reset_async
7677
7678         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7679         set_nodes_failloc "$(osts_nodes)" 0x214
7680         # multiop should block due to fsync until pages are written
7681         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7682         MULTIPID=$!
7683         sleep 1
7684
7685         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7686                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7687         fi
7688
7689         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7690                     grep -c writeback)
7691         if [[ $WRITEBACK -eq 0 ]]; then
7692                 error "No page in writeback, writeback=$WRITEBACK"
7693         fi
7694
7695         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7696         set_nodes_failloc "$(osts_nodes)" 0
7697
7698         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7699         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7700                     grep -c writeback)
7701         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7702                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7703         fi
7704
7705         rm -f $DIR/$tfile
7706         echo "Dirty pages gaurenteed flushed via fsync"
7707         return 0
7708 }
7709 run_test 118d "Fsync validation inject a delay of the bulk =========="
7710
7711 test_118f() {
7712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7713         reset_async
7714
7715         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7716         lctl set_param fail_loc=0x8000040a
7717
7718         # Should simulate EINVAL error which is fatal
7719         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7720         RC=$?
7721         if [[ $RC -eq 0 ]]; then
7722                 error "Must return error due to dropped pages, rc=$RC"
7723         fi
7724
7725         lctl set_param fail_loc=0x0
7726
7727         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7728         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7729         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7730                     grep -c writeback)
7731         if [[ $LOCKED -ne 0 ]]; then
7732                 error "Locked pages remain in cache, locked=$LOCKED"
7733         fi
7734
7735         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7736                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7737         fi
7738
7739         rm -f $DIR/$tfile
7740         echo "No pages locked after fsync"
7741
7742         reset_async
7743         return 0
7744 }
7745 run_test 118f "Simulate unrecoverable OSC side error =========="
7746
7747 test_118g() {
7748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7749         reset_async
7750
7751         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7752         lctl set_param fail_loc=0x406
7753
7754         # simulate local -ENOMEM
7755         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7756         RC=$?
7757
7758         lctl set_param fail_loc=0
7759         if [[ $RC -eq 0 ]]; then
7760                 error "Must return error due to dropped pages, rc=$RC"
7761         fi
7762
7763         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7764         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7765         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7766                         grep -c writeback)
7767         if [[ $LOCKED -ne 0 ]]; then
7768                 error "Locked pages remain in cache, locked=$LOCKED"
7769         fi
7770
7771         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7772                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7773         fi
7774
7775         rm -f $DIR/$tfile
7776         echo "No pages locked after fsync"
7777
7778         reset_async
7779         return 0
7780 }
7781 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7782
7783 test_118h() {
7784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7785         remote_ost_nodsh && skip "remote OST with nodsh" && return
7786
7787         reset_async
7788
7789         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7790         set_nodes_failloc "$(osts_nodes)" 0x20e
7791         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7792         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7793         RC=$?
7794
7795         set_nodes_failloc "$(osts_nodes)" 0
7796         if [[ $RC -eq 0 ]]; then
7797                 error "Must return error due to dropped pages, rc=$RC"
7798         fi
7799
7800         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7801         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7802         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7803                     grep -c writeback)
7804         if [[ $LOCKED -ne 0 ]]; then
7805                 error "Locked pages remain in cache, locked=$LOCKED"
7806         fi
7807
7808         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7809                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7810         fi
7811
7812         rm -f $DIR/$tfile
7813         echo "No pages locked after fsync"
7814
7815         return 0
7816 }
7817 run_test 118h "Verify timeout in handling recoverables errors  =========="
7818
7819 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7820
7821 test_118i() {
7822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7823         remote_ost_nodsh && skip "remote OST with nodsh" && return
7824
7825         reset_async
7826
7827         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7828         set_nodes_failloc "$(osts_nodes)" 0x20e
7829
7830         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7831         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7832         PID=$!
7833         sleep 5
7834         set_nodes_failloc "$(osts_nodes)" 0
7835
7836         wait $PID
7837         RC=$?
7838         if [[ $RC -ne 0 ]]; then
7839                 error "got error, but should be not, rc=$RC"
7840         fi
7841
7842         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7843         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7844         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7845         if [[ $LOCKED -ne 0 ]]; then
7846                 error "Locked pages remain in cache, locked=$LOCKED"
7847         fi
7848
7849         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7850                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7851         fi
7852
7853         rm -f $DIR/$tfile
7854         echo "No pages locked after fsync"
7855
7856         return 0
7857 }
7858 run_test 118i "Fix error before timeout in recoverable error  =========="
7859
7860 [ "$SLOW" = "no" ] && set_resend_count 4
7861
7862 test_118j() {
7863         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7864         remote_ost_nodsh && skip "remote OST with nodsh" && return
7865
7866         reset_async
7867
7868         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7869         set_nodes_failloc "$(osts_nodes)" 0x220
7870
7871         # return -EIO from OST
7872         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7873         RC=$?
7874         set_nodes_failloc "$(osts_nodes)" 0x0
7875         if [[ $RC -eq 0 ]]; then
7876                 error "Must return error due to dropped pages, rc=$RC"
7877         fi
7878
7879         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7880         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7881         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7882         if [[ $LOCKED -ne 0 ]]; then
7883                 error "Locked pages remain in cache, locked=$LOCKED"
7884         fi
7885
7886         # in recoverable error on OST we want resend and stay until it finished
7887         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7888                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7889         fi
7890
7891         rm -f $DIR/$tfile
7892         echo "No pages locked after fsync"
7893
7894         return 0
7895 }
7896 run_test 118j "Simulate unrecoverable OST side error =========="
7897
7898 test_118k()
7899 {
7900         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7901         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7902
7903         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7904         set_nodes_failloc "$(osts_nodes)" 0x20e
7905         test_mkdir -p $DIR/$tdir
7906
7907         for ((i=0;i<10;i++)); do
7908                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7909                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7910                 SLEEPPID=$!
7911                 sleep 0.500s
7912                 kill $SLEEPPID
7913                 wait $SLEEPPID
7914         done
7915
7916         set_nodes_failloc "$(osts_nodes)" 0
7917         rm -rf $DIR/$tdir
7918 }
7919 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7920
7921 test_118l()
7922 {
7923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7924         # LU-646
7925         test_mkdir -p $DIR/$tdir
7926         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7927         rm -rf $DIR/$tdir
7928 }
7929 run_test 118l "fsync dir ========="
7930
7931 test_118m() # LU-3066
7932 {
7933         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7934         test_mkdir -p $DIR/$tdir
7935         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7936         rm -rf $DIR/$tdir
7937 }
7938 run_test 118m "fdatasync dir ========="
7939
7940 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7941
7942 test_119a() # bug 11737
7943 {
7944         BSIZE=$((512 * 1024))
7945         directio write $DIR/$tfile 0 1 $BSIZE
7946         # We ask to read two blocks, which is more than a file size.
7947         # directio will indicate an error when requested and actual
7948         # sizes aren't equeal (a normal situation in this case) and
7949         # print actual read amount.
7950         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7951         if [ "$NOB" != "$BSIZE" ]; then
7952                 error "read $NOB bytes instead of $BSIZE"
7953         fi
7954         rm -f $DIR/$tfile
7955 }
7956 run_test 119a "Short directIO read must return actual read amount"
7957
7958 test_119b() # bug 11737
7959 {
7960         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7961
7962         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7963         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7964         sync
7965         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7966                 error "direct read failed"
7967         rm -f $DIR/$tfile
7968 }
7969 run_test 119b "Sparse directIO read must return actual read amount"
7970
7971 test_119c() # bug 13099
7972 {
7973         BSIZE=1048576
7974         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7975         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7976         rm -f $DIR/$tfile
7977 }
7978 run_test 119c "Testing for direct read hitting hole"
7979
7980 test_119d() # bug 15950
7981 {
7982         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7983         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7984         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7985         BSIZE=1048576
7986         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7987         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7988         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7989         lctl set_param fail_loc=0x40d
7990         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7991         pid_dio=$!
7992         sleep 1
7993         cat $DIR/$tfile > /dev/null &
7994         lctl set_param fail_loc=0
7995         pid_reads=$!
7996         wait $pid_dio
7997         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7998         sleep 2
7999         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8000         error "the read rpcs have not completed in 2s"
8001         rm -f $DIR/$tfile
8002         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8003 }
8004 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8005
8006 test_120a() {
8007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8008         test_mkdir -p $DIR/$tdir
8009         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8010                skip "no early lock cancel on server" && return 0
8011
8012         lru_resize_disable mdc
8013         lru_resize_disable osc
8014         cancel_lru_locks mdc
8015         # asynchronous object destroy at MDT could cause bl ast to client
8016         cancel_lru_locks osc
8017
8018         stat $DIR/$tdir > /dev/null
8019         can1=$(do_facet $SINGLEMDS \
8020                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8021                awk '/ldlm_cancel/ {print $2}')
8022         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8023                awk '/ldlm_bl_callback/ {print $2}')
8024         test_mkdir -c1 $DIR/$tdir/d1
8025         can2=$(do_facet $SINGLEMDS \
8026                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8027                awk '/ldlm_cancel/ {print $2}')
8028         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8029                awk '/ldlm_bl_callback/ {print $2}')
8030         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8031         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8032         lru_resize_enable mdc
8033         lru_resize_enable osc
8034 }
8035 run_test 120a "Early Lock Cancel: mkdir test"
8036
8037 test_120b() {
8038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8039         test_mkdir $DIR/$tdir
8040         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8041                skip "no early lock cancel on server" && return 0
8042         lru_resize_disable mdc
8043         lru_resize_disable osc
8044         cancel_lru_locks mdc
8045         stat $DIR/$tdir > /dev/null
8046         can1=$(do_facet $SINGLEMDS \
8047                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8048                awk '/ldlm_cancel/ {print $2}')
8049         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8050                awk '/ldlm_bl_callback/ {print $2}')
8051         touch $DIR/$tdir/f1
8052         can2=$(do_facet $SINGLEMDS \
8053                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8054                awk '/ldlm_cancel/ {print $2}')
8055         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8056                awk '/ldlm_bl_callback/ {print $2}')
8057         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8058         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8059         lru_resize_enable mdc
8060         lru_resize_enable osc
8061 }
8062 run_test 120b "Early Lock Cancel: create test"
8063
8064 test_120c() {
8065         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8066         test_mkdir -c1 $DIR/$tdir
8067         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8068                skip "no early lock cancel on server" && return 0
8069         lru_resize_disable mdc
8070         lru_resize_disable osc
8071         test_mkdir -p -c1 $DIR/$tdir/d1
8072         test_mkdir -p -c1 $DIR/$tdir/d2
8073         touch $DIR/$tdir/d1/f1
8074         cancel_lru_locks mdc
8075         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8076         can1=$(do_facet $SINGLEMDS \
8077                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8078                awk '/ldlm_cancel/ {print $2}')
8079         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8080                awk '/ldlm_bl_callback/ {print $2}')
8081         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8082         can2=$(do_facet $SINGLEMDS \
8083                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8084                awk '/ldlm_cancel/ {print $2}')
8085         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8086                awk '/ldlm_bl_callback/ {print $2}')
8087         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8088         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8089         lru_resize_enable mdc
8090         lru_resize_enable osc
8091 }
8092 run_test 120c "Early Lock Cancel: link test"
8093
8094 test_120d() {
8095         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8096         test_mkdir -p -c1 $DIR/$tdir
8097         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8098                skip "no early lock cancel on server" && return 0
8099         lru_resize_disable mdc
8100         lru_resize_disable osc
8101         touch $DIR/$tdir
8102         cancel_lru_locks mdc
8103         stat $DIR/$tdir > /dev/null
8104         can1=$(do_facet $SINGLEMDS \
8105                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8106                awk '/ldlm_cancel/ {print $2}')
8107         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8108                awk '/ldlm_bl_callback/ {print $2}')
8109         chmod a+x $DIR/$tdir
8110         can2=$(do_facet $SINGLEMDS \
8111                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8112                awk '/ldlm_cancel/ {print $2}')
8113         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8114                awk '/ldlm_bl_callback/ {print $2}')
8115         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8116         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8117         lru_resize_enable mdc
8118         lru_resize_enable osc
8119 }
8120 run_test 120d "Early Lock Cancel: setattr test"
8121
8122 test_120e() {
8123         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8124         test_mkdir -p -c1 $DIR/$tdir
8125         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8126                skip "no early lock cancel on server" && return 0
8127         lru_resize_disable mdc
8128         lru_resize_disable osc
8129         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8130         cancel_lru_locks mdc
8131         cancel_lru_locks osc
8132         dd if=$DIR/$tdir/f1 of=/dev/null
8133         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8134         # XXX client can not do early lock cancel of OST lock
8135         # during unlink (LU-4206), so cancel osc lock now.
8136         cancel_lru_locks osc
8137         can1=$(do_facet $SINGLEMDS \
8138                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8139                awk '/ldlm_cancel/ {print $2}')
8140         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8141                awk '/ldlm_bl_callback/ {print $2}')
8142         unlink $DIR/$tdir/f1
8143         sleep 5
8144         can2=$(do_facet $SINGLEMDS \
8145                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8146                awk '/ldlm_cancel/ {print $2}')
8147         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8148                awk '/ldlm_bl_callback/ {print $2}')
8149         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8150         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8151         lru_resize_enable mdc
8152         lru_resize_enable osc
8153 }
8154 run_test 120e "Early Lock Cancel: unlink test"
8155
8156 test_120f() {
8157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8158         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8159                skip "no early lock cancel on server" && return 0
8160         test_mkdir -p -c1 $DIR/$tdir
8161         lru_resize_disable mdc
8162         lru_resize_disable osc
8163         test_mkdir -p -c1 $DIR/$tdir/d1
8164         test_mkdir -p -c1 $DIR/$tdir/d2
8165         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8166         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8167         cancel_lru_locks mdc
8168         cancel_lru_locks osc
8169         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8170         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8171         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8172         # XXX client can not do early lock cancel of OST lock
8173         # during rename (LU-4206), so cancel osc lock now.
8174         cancel_lru_locks osc
8175         can1=$(do_facet $SINGLEMDS \
8176                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8177                awk '/ldlm_cancel/ {print $2}')
8178         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8179                awk '/ldlm_bl_callback/ {print $2}')
8180         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8181         sleep 5
8182         can2=$(do_facet $SINGLEMDS \
8183                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8184                awk '/ldlm_cancel/ {print $2}')
8185         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8186                awk '/ldlm_bl_callback/ {print $2}')
8187         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8188         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8189         lru_resize_enable mdc
8190         lru_resize_enable osc
8191 }
8192 run_test 120f "Early Lock Cancel: rename test"
8193
8194 test_120g() {
8195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8196         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8197                skip "no early lock cancel on server" && return 0
8198         lru_resize_disable mdc
8199         lru_resize_disable osc
8200         count=10000
8201         echo create $count files
8202         test_mkdir -p $DIR/$tdir
8203         cancel_lru_locks mdc
8204         cancel_lru_locks osc
8205         t0=`date +%s`
8206
8207         can0=$(do_facet $SINGLEMDS \
8208                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8209                awk '/ldlm_cancel/ {print $2}')
8210         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8211                awk '/ldlm_bl_callback/ {print $2}')
8212         createmany -o $DIR/$tdir/f $count
8213         sync
8214         can1=$(do_facet $SINGLEMDS \
8215                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8216                awk '/ldlm_cancel/ {print $2}')
8217         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8218                awk '/ldlm_bl_callback/ {print $2}')
8219         t1=$(date +%s)
8220         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8221         echo rm $count files
8222         rm -r $DIR/$tdir
8223         sync
8224         can2=$(do_facet $SINGLEMDS \
8225                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8226                awk '/ldlm_cancel/ {print $2}')
8227         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8228                awk '/ldlm_bl_callback/ {print $2}')
8229         t2=$(date +%s)
8230         echo total: $count removes in $((t2-t1))
8231         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8232         sleep 2
8233         # wait for commitment of removal
8234         lru_resize_enable mdc
8235         lru_resize_enable osc
8236 }
8237 run_test 120g "Early Lock Cancel: performance test"
8238
8239 test_121() { #bug #10589
8240         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8241         rm -rf $DIR/$tfile
8242         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8243 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8244         lctl set_param fail_loc=0x310
8245         cancel_lru_locks osc > /dev/null
8246         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8247         lctl set_param fail_loc=0
8248         [[ $reads -eq $writes ]] ||
8249                 error "read $reads blocks, must be $writes blocks"
8250 }
8251 run_test 121 "read cancel race ========="
8252
8253 test_123a() { # was test 123, statahead(bug 11401)
8254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8255         SLOWOK=0
8256         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8257             log "testing on UP system. Performance may be not as good as expected."
8258                         SLOWOK=1
8259         fi
8260
8261         rm -rf $DIR/$tdir
8262         test_mkdir -p $DIR/$tdir
8263         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8264         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8265         MULT=10
8266         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8267                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8268
8269                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8270                 lctl set_param -n llite.*.statahead_max 0
8271                 lctl get_param llite.*.statahead_max
8272                 cancel_lru_locks mdc
8273                 cancel_lru_locks osc
8274                 stime=`date +%s`
8275                 time ls -l $DIR/$tdir | wc -l
8276                 etime=`date +%s`
8277                 delta=$((etime - stime))
8278                 log "ls $i files without statahead: $delta sec"
8279                 lctl set_param llite.*.statahead_max=$max
8280
8281                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8282                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8283                 cancel_lru_locks mdc
8284                 cancel_lru_locks osc
8285                 stime=`date +%s`
8286                 time ls -l $DIR/$tdir | wc -l
8287                 etime=`date +%s`
8288                 delta_sa=$((etime - stime))
8289                 log "ls $i files with statahead: $delta_sa sec"
8290                 lctl get_param -n llite.*.statahead_stats
8291                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8292
8293                 [[ $swrong -lt $ewrong ]] &&
8294                         log "statahead was stopped, maybe too many locks held!"
8295                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8296
8297                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8298                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8299                     lctl set_param -n llite.*.statahead_max 0
8300                     lctl get_param llite.*.statahead_max
8301                     cancel_lru_locks mdc
8302                     cancel_lru_locks osc
8303                     stime=`date +%s`
8304                     time ls -l $DIR/$tdir | wc -l
8305                     etime=`date +%s`
8306                     delta=$((etime - stime))
8307                     log "ls $i files again without statahead: $delta sec"
8308                     lctl set_param llite.*.statahead_max=$max
8309                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8310                         if [  $SLOWOK -eq 0 ]; then
8311                                 error "ls $i files is slower with statahead!"
8312                         else
8313                                 log "ls $i files is slower with statahead!"
8314                         fi
8315                         break
8316                     fi
8317                 fi
8318
8319                 [ $delta -gt 20 ] && break
8320                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8321                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8322         done
8323         log "ls done"
8324
8325         stime=`date +%s`
8326         rm -r $DIR/$tdir
8327         sync
8328         etime=`date +%s`
8329         delta=$((etime - stime))
8330         log "rm -r $DIR/$tdir/: $delta seconds"
8331         log "rm done"
8332         lctl get_param -n llite.*.statahead_stats
8333 }
8334 run_test 123a "verify statahead work"
8335
8336 test_123b () { # statahead(bug 15027)
8337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8338         test_mkdir -p $DIR/$tdir
8339         createmany -o $DIR/$tdir/$tfile-%d 1000
8340
8341         cancel_lru_locks mdc
8342         cancel_lru_locks osc
8343
8344 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8345         lctl set_param fail_loc=0x80000803
8346         ls -lR $DIR/$tdir > /dev/null
8347         log "ls done"
8348         lctl set_param fail_loc=0x0
8349         lctl get_param -n llite.*.statahead_stats
8350         rm -r $DIR/$tdir
8351         sync
8352
8353 }
8354 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8355
8356 test_124a() {
8357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8358         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8359                 skip "no lru resize on server" && return 0
8360         local NR=2000
8361         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8362
8363         log "create $NR files at $DIR/$tdir"
8364         createmany -o $DIR/$tdir/f $NR ||
8365                 error "failed to create $NR files in $DIR/$tdir"
8366
8367         cancel_lru_locks mdc
8368         ls -l $DIR/$tdir > /dev/null
8369
8370         local NSDIR=""
8371         local LRU_SIZE=0
8372         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8373                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8374                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8375                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8376                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8377                         log "NSDIR=$NSDIR"
8378                         log "NS=$(basename $NSDIR)"
8379                         break
8380                 fi
8381         done
8382
8383         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8384                 skip "Not enough cached locks created!"
8385                 return 0
8386         fi
8387         log "LRU=$LRU_SIZE"
8388
8389         local SLEEP=30
8390
8391         # We know that lru resize allows one client to hold $LIMIT locks
8392         # for 10h. After that locks begin to be killed by client.
8393         local MAX_HRS=10
8394         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8395         log "LIMIT=$LIMIT"
8396         if [ $LIMIT -lt $LRU_SIZE ]; then
8397             skip "Limit is too small $LIMIT"
8398             return 0
8399         fi
8400
8401         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8402         # killing locks. Some time was spent for creating locks. This means
8403         # that up to the moment of sleep finish we must have killed some of
8404         # them (10-100 locks). This depends on how fast ther were created.
8405         # Many of them were touched in almost the same moment and thus will
8406         # be killed in groups.
8407         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8408
8409         # Use $LRU_SIZE_B here to take into account real number of locks
8410         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8411         local LRU_SIZE_B=$LRU_SIZE
8412         log "LVF=$LVF"
8413         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8414         log "OLD_LVF=$OLD_LVF"
8415         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8416
8417         # Let's make sure that we really have some margin. Client checks
8418         # cached locks every 10 sec.
8419         SLEEP=$((SLEEP+20))
8420         log "Sleep ${SLEEP} sec"
8421         local SEC=0
8422         while ((SEC<$SLEEP)); do
8423                 echo -n "..."
8424                 sleep 5
8425                 SEC=$((SEC+5))
8426                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8427                 echo -n "$LRU_SIZE"
8428         done
8429         echo ""
8430         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8431         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8432
8433         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8434                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8435                 unlinkmany $DIR/$tdir/f $NR
8436                 return
8437         }
8438
8439         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8440         log "unlink $NR files at $DIR/$tdir"
8441         unlinkmany $DIR/$tdir/f $NR
8442 }
8443 run_test 124a "lru resize ======================================="
8444
8445 get_max_pool_limit()
8446 {
8447         local limit=$($LCTL get_param \
8448                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8449         local max=0
8450         for l in $limit; do
8451                 if [[ $l -gt $max ]]; then
8452                         max=$l
8453                 fi
8454         done
8455         echo $max
8456 }
8457
8458 test_124b() {
8459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8460         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8461                 skip "no lru resize on server" && return 0
8462
8463         LIMIT=$(get_max_pool_limit)
8464
8465         NR=$(($(default_lru_size)*20))
8466         if [[ $NR -gt $LIMIT ]]; then
8467                 log "Limit lock number by $LIMIT locks"
8468                 NR=$LIMIT
8469         fi
8470         lru_resize_disable mdc
8471         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8472                 error "failed to create $DIR/$tdir/disable_lru_resize"
8473
8474         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8475         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8476         cancel_lru_locks mdc
8477         stime=`date +%s`
8478         PID=""
8479         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8480         PID="$PID $!"
8481         sleep 2
8482         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8483         PID="$PID $!"
8484         sleep 2
8485         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8486         PID="$PID $!"
8487         wait $PID
8488         etime=`date +%s`
8489         nolruresize_delta=$((etime-stime))
8490         log "ls -la time: $nolruresize_delta seconds"
8491         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8492         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8493
8494         lru_resize_enable mdc
8495         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8496                 error "failed to create $DIR/$tdir/enable_lru_resize"
8497
8498         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8499         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8500         cancel_lru_locks mdc
8501         stime=`date +%s`
8502         PID=""
8503         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8504         PID="$PID $!"
8505         sleep 2
8506         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8507         PID="$PID $!"
8508         sleep 2
8509         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8510         PID="$PID $!"
8511         wait $PID
8512         etime=`date +%s`
8513         lruresize_delta=$((etime-stime))
8514         log "ls -la time: $lruresize_delta seconds"
8515         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8516
8517         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8518                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8519         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8520                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8521         else
8522                 log "lru resize performs the same with no lru resize"
8523         fi
8524         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8525 }
8526 run_test 124b "lru resize (performance test) ======================="
8527
8528 test_124c() {
8529         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8530         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8531                 skip "no lru resize on server" && return 0
8532
8533         # cache ununsed locks on client
8534         local nr=100
8535         cancel_lru_locks mdc
8536         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8537         createmany -o $DIR/$tdir/f $nr ||
8538                 error "failed to create $nr files in $DIR/$tdir"
8539         ls -l $DIR/$tdir > /dev/null
8540
8541         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
8542         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
8543         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
8544         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
8545         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
8546
8547         # set lru_max_age to 1 sec
8548         $LCTL set_param $nsdir.lru_max_age=1000 # jiffies
8549         echo "sleep $((recalc_p * 2)) seconds..."
8550         sleep $((recalc_p * 2))
8551
8552         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
8553         # restore lru_max_age
8554         $LCTL set_param -n $nsdir.lru_max_age $max_age
8555         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
8556         unlinkmany $DIR/$tdir/f $nr
8557 }
8558 run_test 124c "LRUR cancel very aged locks"
8559
8560 test_125() { # 13358
8561         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8562         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8563         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8564         test_mkdir -p $DIR/d125 || error "mkdir failed"
8565         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8566         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8567         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8568 }
8569 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8570
8571 test_126() { # bug 12829/13455
8572         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8573         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8574         $GSS && skip "must run as gss disabled" && return
8575
8576         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8577         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8578         rm -f $DIR/$tfile
8579         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8580 }
8581 run_test 126 "check that the fsgid provided by the client is taken into account"
8582
8583 test_127a() { # bug 15521
8584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8585         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8586         $LCTL set_param osc.*.stats=0
8587         FSIZE=$((2048 * 1024))
8588         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8589         cancel_lru_locks osc
8590         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8591
8592         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8593         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8594                 echo "got $COUNT $NAME"
8595                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8596                 eval $NAME=$COUNT || error "Wrong proc format"
8597
8598                 case $NAME in
8599                         read_bytes|write_bytes)
8600                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8601                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8602                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8603                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8604                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8605                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8606                                 error "sumsquare is too small: $SUMSQ"
8607                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8608                                 error "sumsquare is too big: $SUMSQ"
8609                         ;;
8610                         *) ;;
8611                 esac
8612         done < $DIR/${tfile}.tmp
8613
8614         #check that we actually got some stats
8615         [ "$read_bytes" ] || error "Missing read_bytes stats"
8616         [ "$write_bytes" ] || error "Missing write_bytes stats"
8617         [ "$read_bytes" != 0 ] || error "no read done"
8618         [ "$write_bytes" != 0 ] || error "no write done"
8619 }
8620 run_test 127a "verify the client stats are sane"
8621
8622 test_127b() { # bug LU-333
8623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8624         $LCTL set_param llite.*.stats=0
8625         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8626         # perform 2 reads and writes so MAX is different from SUM.
8627         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8628         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8629         cancel_lru_locks osc
8630         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8631         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8632
8633         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8634         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8635                 echo "got $COUNT $NAME"
8636                 eval $NAME=$COUNT || error "Wrong proc format"
8637
8638         case $NAME in
8639                 read_bytes)
8640                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8641                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8642                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8643                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8644                         ;;
8645                 write_bytes)
8646                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8647                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8648                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8649                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8650                         ;;
8651                         *) ;;
8652                 esac
8653         done < $TMP/${tfile}.tmp
8654
8655         #check that we actually got some stats
8656         [ "$read_bytes" ] || error "Missing read_bytes stats"
8657         [ "$write_bytes" ] || error "Missing write_bytes stats"
8658         [ "$read_bytes" != 0 ] || error "no read done"
8659         [ "$write_bytes" != 0 ] || error "no write done"
8660 }
8661 run_test 127b "verify the llite client stats are sane"
8662
8663 test_128() { # bug 15212
8664         touch $DIR/$tfile
8665         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8666                 find $DIR/$tfile
8667                 find $DIR/$tfile
8668         EOF
8669
8670         result=$(grep error $TMP/$tfile.log)
8671         rm -f $DIR/$tfile
8672         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8673 }
8674 run_test 128 "interactive lfs for 2 consecutive find's"
8675
8676 set_dir_limits () {
8677         local mntdev
8678         local canondev
8679         local node
8680
8681         local LDPROC=/proc/fs/ldiskfs
8682         local facets=$(get_facets MDS)
8683
8684         for facet in ${facets//,/ }; do
8685                 canondev=$(ldiskfs_canon \
8686                            *.$(convert_facet2label $facet).mntdev $facet)
8687                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8688                                                 LDPROC=/sys/fs/ldiskfs
8689                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8690         done
8691 }
8692
8693 test_129() {
8694         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8695         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8696                 skip "Only applicable to ldiskfs-based MDTs"
8697                 return
8698         fi
8699         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8700         ENOSPC=28
8701         EFBIG=27
8702
8703         rm -rf $DIR/$tdir
8704         test_mkdir -p $DIR/$tdir
8705
8706         # block size of mds1
8707         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8708         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8709         local MAX=$((MDSBLOCKSIZE * 3))
8710         set_dir_limits $MAX
8711         local I=$(stat -c%s "$DIR/$tdir")
8712         local J=0
8713         local STRIPE_COUNT=1
8714         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8715         MAX=$((MAX*STRIPE_COUNT))
8716         while [[ $I -le $MAX ]]; do
8717                 $MULTIOP $DIR/$tdir/$J Oc
8718                 rc=$?
8719                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8720                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8721                 #and EFBIG for previous versions
8722                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8723                         set_dir_limits 0
8724                         echo "return code $rc received as expected"
8725                         multiop $DIR/$tdir/$J Oc ||
8726                                 error_exit "multiop failed w/o dir size limit"
8727
8728                         I=$(stat -c%s "$DIR/$tdir")
8729
8730                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8731                                         $(version_code 2.4.51) ]
8732                         then
8733                                 [[ $I -eq $MAX ]] && return 0
8734                         else
8735                                 [[ $I -gt $MAX ]] && return 0
8736                         fi
8737                         error_exit "current dir size $I, previous limit $MAX"
8738                 elif [ $rc -ne 0 ]; then
8739                         set_dir_limits 0
8740                         error_exit "return code $rc received instead of expected " \
8741                                    "$EFBIG or $ENOSPC, files in dir $I"
8742                 fi
8743                 J=$((J+1))
8744                 I=$(stat -c%s "$DIR/$tdir")
8745         done
8746
8747         set_dir_limits 0
8748         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8749 }
8750 run_test 129 "test directory size limit ========================"
8751
8752 OLDIFS="$IFS"
8753 cleanup_130() {
8754         trap 0
8755         IFS="$OLDIFS"
8756 }
8757
8758 test_130a() {
8759         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8760         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8761                 return
8762
8763         trap cleanup_130 EXIT RETURN
8764
8765         local fm_file=$DIR/$tfile
8766         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8767         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8768                 error "dd failed for $fm_file"
8769
8770         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8771         filefrag -ves $fm_file
8772         RC=$?
8773         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8774                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8775         [ $RC != 0 ] && error "filefrag $fm_file failed"
8776
8777         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8778                       grep -v "ext:" | grep -v "found")
8779         lun=$($GETSTRIPE -i $fm_file)
8780
8781         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8782         IFS=$'\n'
8783         tot_len=0
8784         for line in $filefrag_op
8785         do
8786                 frag_lun=`echo $line | cut -d: -f5`
8787                 ext_len=`echo $line | cut -d: -f4`
8788                 if (( $frag_lun != $lun )); then
8789                         cleanup_130
8790                         error "FIEMAP on 1-stripe file($fm_file) failed"
8791                         return
8792                 fi
8793                 (( tot_len += ext_len ))
8794         done
8795
8796         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8797                 cleanup_130
8798                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8799                 return
8800         fi
8801
8802         cleanup_130
8803
8804         echo "FIEMAP on single striped file succeeded"
8805 }
8806 run_test 130a "FIEMAP (1-stripe file)"
8807
8808 test_130b() {
8809         [ "$OSTCOUNT" -lt "2" ] &&
8810                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8811
8812         [ "$OSTCOUNT" -ge "10" ] &&
8813                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8814
8815         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8816         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8817                 return
8818
8819         trap cleanup_130 EXIT RETURN
8820
8821         local fm_file=$DIR/$tfile
8822         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8823         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8824                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8825
8826         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8827                 error "dd failed on $fm_file"
8828
8829         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8830         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8831                       grep -v "ext:" | grep -v "found")
8832
8833         last_lun=$(echo $filefrag_op | cut -d: -f5)
8834
8835         IFS=$'\n'
8836         tot_len=0
8837         num_luns=1
8838         for line in $filefrag_op
8839         do
8840                 frag_lun=`echo $line | cut -d: -f5`
8841                 ext_len=`echo $line | cut -d: -f4`
8842                 if (( $frag_lun != $last_lun )); then
8843                         if (( tot_len != 1024 )); then
8844                                 cleanup_130
8845                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8846                                 return
8847                         else
8848                                 (( num_luns += 1 ))
8849                                 tot_len=0
8850                         fi
8851                 fi
8852                 (( tot_len += ext_len ))
8853                 last_lun=$frag_lun
8854         done
8855         if (( num_luns != 2 || tot_len != 1024 )); then
8856                 cleanup_130
8857                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8858                 return
8859         fi
8860
8861         cleanup_130
8862
8863         echo "FIEMAP on 2-stripe file succeeded"
8864 }
8865 run_test 130b "FIEMAP (2-stripe file)"
8866
8867 test_130c() {
8868         [ "$OSTCOUNT" -lt "2" ] &&
8869                 skip_env "skipping FIEMAP on 2-stripe file" && return
8870
8871         [ "$OSTCOUNT" -ge "10" ] &&
8872                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8873
8874         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8875         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8876                 return
8877
8878         trap cleanup_130 EXIT RETURN
8879
8880         local fm_file=$DIR/$tfile
8881         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8882         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8883                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8884
8885         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8886
8887         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8888         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8889
8890         last_lun=`echo $filefrag_op | cut -d: -f5`
8891
8892         IFS=$'\n'
8893         tot_len=0
8894         num_luns=1
8895         for line in $filefrag_op
8896         do
8897                 frag_lun=`echo $line | cut -d: -f5`
8898                 ext_len=`echo $line | cut -d: -f4`
8899                 if (( $frag_lun != $last_lun )); then
8900                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8901                         if (( logical != 512 )); then
8902                                 cleanup_130
8903                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8904                                 return
8905                         fi
8906                         if (( tot_len != 512 )); then
8907                                 cleanup_130
8908                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8909                                 return
8910                         else
8911                                 (( num_luns += 1 ))
8912                                 tot_len=0
8913                         fi
8914                 fi
8915                 (( tot_len += ext_len ))
8916                 last_lun=$frag_lun
8917         done
8918         if (( num_luns != 2 || tot_len != 512 )); then
8919                 cleanup_130
8920                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8921                 return
8922         fi
8923
8924         cleanup_130
8925
8926         echo "FIEMAP on 2-stripe file with hole succeeded"
8927 }
8928 run_test 130c "FIEMAP (2-stripe file with hole)"
8929
8930 test_130d() {
8931         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8932
8933         [ "$OSTCOUNT" -ge "10" ] &&
8934                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8935
8936         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8937         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8938
8939         trap cleanup_130 EXIT RETURN
8940
8941         local fm_file=$DIR/$tfile
8942         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8943         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8944                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8945
8946         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8947         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8948                 error "dd failed on $fm_file"
8949
8950         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8951         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8952                 grep -v "ext:" | grep -v "found"`
8953
8954         last_lun=`echo $filefrag_op | cut -d: -f5`
8955
8956         IFS=$'\n'
8957         tot_len=0
8958         num_luns=1
8959         for line in $filefrag_op
8960         do
8961                 frag_lun=`echo $line | cut -d: -f5`
8962                 ext_len=`echo $line | cut -d: -f4`
8963                 if (( $frag_lun != $last_lun )); then
8964                         if (( tot_len != 1024 )); then
8965                                 cleanup_130
8966                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8967                                 return
8968                         else
8969                                 (( num_luns += 1 ))
8970                                 tot_len=0
8971                         fi
8972                 fi
8973                 (( tot_len += ext_len ))
8974                 last_lun=$frag_lun
8975         done
8976         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8977                 cleanup_130
8978                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8979                 return
8980         fi
8981
8982         cleanup_130
8983
8984         echo "FIEMAP on N-stripe file succeeded"
8985 }
8986 run_test 130d "FIEMAP (N-stripe file)"
8987
8988 test_130e() {
8989         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8990
8991         [ "$OSTCOUNT" -ge "10" ] &&
8992                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8993
8994         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8995         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8996
8997         trap cleanup_130 EXIT RETURN
8998
8999         local fm_file=$DIR/$tfile
9000         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9001         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9002                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9003
9004         NUM_BLKS=512
9005         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9006         for ((i = 0; i < $NUM_BLKS; i++))
9007         do
9008                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9009         done
9010
9011         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9012         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
9013
9014         last_lun=`echo $filefrag_op | cut -d: -f5`
9015
9016         IFS=$'\n'
9017         tot_len=0
9018         num_luns=1
9019         for line in $filefrag_op
9020         do
9021                 frag_lun=`echo $line | cut -d: -f5`
9022                 ext_len=`echo $line | cut -d: -f4`
9023                 if (( $frag_lun != $last_lun )); then
9024                         if (( tot_len != $EXPECTED_LEN )); then
9025                                 cleanup_130
9026                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
9027                                 return
9028                         else
9029                                 (( num_luns += 1 ))
9030                                 tot_len=0
9031                         fi
9032                 fi
9033                 (( tot_len += ext_len ))
9034                 last_lun=$frag_lun
9035         done
9036         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9037                 cleanup_130
9038                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
9039                 return
9040         fi
9041
9042         cleanup_130
9043
9044         echo "FIEMAP with continuation calls succeeded"
9045 }
9046 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9047
9048 # Test for writev/readv
9049 test_131a() {
9050         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
9051         error "writev test failed"
9052         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
9053         error "readv failed"
9054         rm -f $DIR/$tfile
9055 }
9056 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9057
9058 test_131b() {
9059         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
9060         error "append writev test failed"
9061         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
9062         error "append writev test failed"
9063         rm -f $DIR/$tfile
9064 }
9065 run_test 131b "test append writev"
9066
9067 test_131c() {
9068         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9069         error "NOT PASS"
9070 }
9071 run_test 131c "test read/write on file w/o objects"
9072
9073 test_131d() {
9074         rwv -f $DIR/$tfile -w -n 1 1572864
9075         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9076         if [ "$NOB" != 1572864 ]; then
9077                 error "Short read filed: read $NOB bytes instead of 1572864"
9078         fi
9079         rm -f $DIR/$tfile
9080 }
9081 run_test 131d "test short read"
9082
9083 test_131e() {
9084         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9085         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9086         error "read hitting hole failed"
9087         rm -f $DIR/$tfile
9088 }
9089 run_test 131e "test read hitting hole"
9090
9091 check_stats() {
9092         local res
9093         local count
9094         case $1 in
9095         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9096                  ;;
9097         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9098                  ;;
9099         *) error "Wrong argument $1" ;;
9100         esac
9101         echo $res
9102         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9103         # if the argument $3 is zero, it means any stat increment is ok.
9104         if [[ $3 -gt 0 ]]; then
9105                 count=$(echo $res | awk '{ print $2 }')
9106                 [[ $count -ne $3 ]] &&
9107                         error "The $2 counter on $1 is wrong - expected $3"
9108         fi
9109 }
9110
9111 test_133a() {
9112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9113         remote_ost_nodsh && skip "remote OST with nodsh" && return
9114         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9115
9116         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9117                 { skip "MDS doesn't support rename stats"; return; }
9118         local testdir=$DIR/${tdir}/stats_testdir
9119         mkdir -p $DIR/${tdir}
9120
9121         # clear stats.
9122         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9123         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9124
9125         # verify mdt stats first.
9126         mkdir ${testdir} || error "mkdir failed"
9127         check_stats $SINGLEMDS "mkdir" 1
9128         touch ${testdir}/${tfile} || "touch failed"
9129         check_stats $SINGLEMDS "open" 1
9130         check_stats $SINGLEMDS "close" 1
9131         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9132         check_stats $SINGLEMDS "mknod" 1
9133         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9134         check_stats $SINGLEMDS "unlink" 1
9135         rm -f ${testdir}/${tfile} || error "file remove failed"
9136         check_stats $SINGLEMDS "unlink" 2
9137
9138         # remove working dir and check mdt stats again.
9139         rmdir ${testdir} || error "rmdir failed"
9140         check_stats $SINGLEMDS "rmdir" 1
9141
9142         local testdir1=$DIR/${tdir}/stats_testdir1
9143         mkdir -p ${testdir}
9144         mkdir -p ${testdir1}
9145         touch ${testdir1}/test1
9146         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9147         check_stats $SINGLEMDS "crossdir_rename" 1
9148
9149         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9150         check_stats $SINGLEMDS "samedir_rename" 1
9151
9152         rm -rf $DIR/${tdir}
9153 }
9154 run_test 133a "Verifying MDT stats ========================================"
9155
9156 test_133b() {
9157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9158         remote_ost_nodsh && skip "remote OST with nodsh" && return
9159         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9160         local testdir=$DIR/${tdir}/stats_testdir
9161         mkdir -p ${testdir} || error "mkdir failed"
9162         touch ${testdir}/${tfile} || "touch failed"
9163         cancel_lru_locks mdc
9164
9165         # clear stats.
9166         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9167         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9168
9169         # extra mdt stats verification.
9170         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9171         check_stats $SINGLEMDS "setattr" 1
9172         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9173         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9174         then            # LU-1740
9175                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9176                 check_stats $SINGLEMDS "getattr" 1
9177         fi
9178         $LFS df || error "lfs failed"
9179         check_stats $SINGLEMDS "statfs" 1
9180
9181         rm -rf $DIR/${tdir}
9182 }
9183 run_test 133b "Verifying extra MDT stats =================================="
9184
9185 test_133c() {
9186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9187         remote_ost_nodsh && skip "remote OST with nodsh" && return
9188         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9189         local testdir=$DIR/${tdir}/stats_testdir
9190         test_mkdir -p ${testdir} || error "mkdir failed"
9191
9192         # verify obdfilter stats.
9193         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9194         sync
9195         cancel_lru_locks osc
9196         wait_delete_completed
9197
9198         # clear stats.
9199         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9200         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9201
9202         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9203         sync
9204         cancel_lru_locks osc
9205         check_stats ost "write" 1
9206
9207         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9208         check_stats ost "read" 1
9209
9210         > ${testdir}/${tfile} || error "truncate failed"
9211         check_stats ost "punch" 1
9212
9213         rm -f ${testdir}/${tfile} || error "file remove failed"
9214         wait_delete_completed
9215         check_stats ost "destroy" 1
9216
9217         rm -rf $DIR/${tdir}
9218 }
9219 run_test 133c "Verifying OST stats ========================================"
9220
9221 order_2() {
9222         local value=$1
9223         local orig=$value
9224         local order=1
9225
9226         while [ $value -ge 2 ]; do
9227                 order=$((order*2))
9228                 value=$((value/2))
9229         done
9230
9231         if [ $orig -gt $order ]; then
9232                 order=$((order*2))
9233         fi
9234         echo $order
9235 }
9236
9237 size_in_KMGT() {
9238     local value=$1
9239     local size=('K' 'M' 'G' 'T');
9240     local i=0
9241     local size_string=$value
9242
9243     while [ $value -ge 1024 ]; do
9244         if [ $i -gt 3 ]; then
9245             #T is the biggest unit we get here, if that is bigger,
9246             #just return XXXT
9247             size_string=${value}T
9248             break
9249         fi
9250         value=$((value >> 10))
9251         if [ $value -lt 1024 ]; then
9252             size_string=${value}${size[$i]}
9253             break
9254         fi
9255         i=$((i + 1))
9256     done
9257
9258     echo $size_string
9259 }
9260
9261 get_rename_size() {
9262     local size=$1
9263     local context=${2:-.}
9264     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9265                 grep -A1 $context |
9266                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9267     echo $sample
9268 }
9269
9270 test_133d() {
9271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9272         remote_ost_nodsh && skip "remote OST with nodsh" && return
9273         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9274         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9275         { skip "MDS doesn't support rename stats"; return; }
9276
9277         local testdir1=$DIR/${tdir}/stats_testdir1
9278         local testdir2=$DIR/${tdir}/stats_testdir2
9279
9280         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9281
9282         mkdir -p ${testdir1} || error "mkdir failed"
9283         mkdir -p ${testdir2} || error "mkdir failed"
9284
9285         createmany -o $testdir1/test 512 || error "createmany failed"
9286
9287         # check samedir rename size
9288         mv ${testdir1}/test0 ${testdir1}/test_0
9289
9290         local testdir1_size=$(ls -l $DIR/${tdir} |
9291                 awk '/stats_testdir1/ {print $5}')
9292         local testdir2_size=$(ls -l $DIR/${tdir} |
9293                 awk '/stats_testdir2/ {print $5}')
9294
9295         testdir1_size=$(order_2 $testdir1_size)
9296         testdir2_size=$(order_2 $testdir2_size)
9297
9298         testdir1_size=$(size_in_KMGT $testdir1_size)
9299         testdir2_size=$(size_in_KMGT $testdir2_size)
9300
9301         echo "source rename dir size: ${testdir1_size}"
9302         echo "target rename dir size: ${testdir2_size}"
9303
9304         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9305         eval $cmd || error "$cmd failed"
9306         local samedir=$($cmd | grep 'same_dir')
9307         local same_sample=$(get_rename_size $testdir1_size)
9308         [ -z "$samedir" ] && error "samedir_rename_size count error"
9309         [[ $same_sample -eq 1 ]] ||
9310                 error "samedir_rename_size error $same_sample"
9311         echo "Check same dir rename stats success"
9312
9313         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9314
9315         # check crossdir rename size
9316         mv ${testdir1}/test_0 ${testdir2}/test_0
9317
9318         testdir1_size=$(ls -l $DIR/${tdir} |
9319                 awk '/stats_testdir1/ {print $5}')
9320         testdir2_size=$(ls -l $DIR/${tdir} |
9321                 awk '/stats_testdir2/ {print $5}')
9322
9323         testdir1_size=$(order_2 $testdir1_size)
9324         testdir2_size=$(order_2 $testdir2_size)
9325
9326         testdir1_size=$(size_in_KMGT $testdir1_size)
9327         testdir2_size=$(size_in_KMGT $testdir2_size)
9328
9329         echo "source rename dir size: ${testdir1_size}"
9330         echo "target rename dir size: ${testdir2_size}"
9331
9332         eval $cmd || error "$cmd failed"
9333         local crossdir=$($cmd | grep 'crossdir')
9334         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9335         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9336         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9337         [[ $src_sample -eq 1 ]] ||
9338                 error "crossdir_rename_size error $src_sample"
9339         [[ $tgt_sample -eq 1 ]] ||
9340                 error "crossdir_rename_size error $tgt_sample"
9341         echo "Check cross dir rename stats success"
9342         rm -rf $DIR/${tdir}
9343 }
9344 run_test 133d "Verifying rename_stats ========================================"
9345
9346 test_133e() {
9347         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9348         remote_ost_nodsh && skip "remote OST with nodsh" && return
9349         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9350         local testdir=$DIR/${tdir}/stats_testdir
9351         local ctr f0 f1 bs=32768 count=42 sum
9352
9353         mkdir -p ${testdir} || error "mkdir failed"
9354
9355         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9356
9357         for ctr in {write,read}_bytes; do
9358                 sync
9359                 cancel_lru_locks osc
9360
9361                 do_facet ost1 $LCTL set_param -n \
9362                         "obdfilter.*.exports.clear=clear"
9363
9364                 if [ $ctr = write_bytes ]; then
9365                         f0=/dev/zero
9366                         f1=${testdir}/${tfile}
9367                 else
9368                         f0=${testdir}/${tfile}
9369                         f1=/dev/null
9370                 fi
9371
9372                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9373                         error "dd failed"
9374                 sync
9375                 cancel_lru_locks osc
9376
9377                 sum=$(do_facet ost1 $LCTL get_param \
9378                         "obdfilter.*.exports.*.stats" |
9379                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9380                                 $1 == ctr { sum += $7 }
9381                                 END { printf("%0.0f", sum) }')
9382
9383                 if ((sum != bs * count)); then
9384                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9385                 fi
9386         done
9387
9388         rm -rf $DIR/${tdir}
9389 }
9390 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9391
9392 test_133f() {
9393         local proc_dirs
9394
9395         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9396 /sys/fs/lustre/ /sys/fs/lnet/"
9397         local dir
9398         for dir in $dirs; do
9399                 if [ -d $dir ]; then
9400                         proc_dirs="$proc_dirs $dir"
9401                 fi
9402         done
9403
9404         local facet
9405
9406         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9407         remote_ost_nodsh && skip "remote OST with nodsh" && return
9408         # First without trusting modes.
9409         find $proc_dirs -exec cat '{}' \; &> /dev/null
9410
9411         # Second verifying readability.
9412         find $proc_dirs \
9413                 -type f \
9414                 -exec cat '{}' \; &> /dev/null ||
9415                         error "proc file read failed"
9416
9417         for facet in $SINGLEMDS ost1; do
9418                 do_facet $facet find $proc_dirs \
9419                         ! -name req_history \
9420                         -exec cat '{}' \\\; &> /dev/null
9421
9422                 do_facet $facet find $proc_dirs \
9423                         ! -name req_history \
9424                         -type f \
9425                         -exec cat '{}' \\\; &> /dev/null ||
9426                                 error "proc file read failed"
9427         done
9428 }
9429 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9430
9431 test_133g() {
9432         local proc_dirs
9433
9434         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9435 /sys/fs/lustre/ /sys/fs/lnet/"
9436         local dir
9437         for dir in $dirs; do
9438                 if [ -d $dir ]; then
9439                         proc_dirs="$proc_dirs $dir"
9440                 fi
9441         done
9442
9443         local facet
9444
9445         # Second verifying readability.
9446         find $proc_dirs \
9447                 -type f \
9448                 -not -name force_lbug \
9449                 -not -name changelog_mask \
9450                 -exec badarea_io '{}' \; &> /dev/null ||
9451                 error "find $proc_dirs failed"
9452
9453         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9454                 skip "Too old lustre on MDS" && return
9455
9456         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9457                 skip "Too old lustre on ost1" && return
9458
9459         for facet in $SINGLEMDS ost1; do
9460                 do_facet $facet find $proc_dirs \
9461                         -type f \
9462                         -not -name force_lbug \
9463                         -not -name changelog_mask \
9464                         -exec badarea_io '{}' \\\; &> /dev/null ||
9465                 error "$facet find $proc_dirs failed"
9466
9467         done
9468
9469         # remount the FS in case writes/reads /proc break the FS
9470         cleanup || error "failed to unmount"
9471         setup || error "failed to setup"
9472         true
9473 }
9474 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9475
9476 test_134a() {
9477         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9478                 skip "Need MDS version at least 2.7.54" && return
9479
9480         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9481         cancel_lru_locks mdc
9482
9483         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9484         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9485         [ $unused -eq 0 ] || "$unused locks are not cleared"
9486
9487         local nr=1000
9488         createmany -o $DIR/$tdir/f $nr ||
9489                 error "failed to create $nr files in $DIR/$tdir"
9490         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9491
9492         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
9493         do_facet mds1 $LCTL set_param fail_loc=0x327
9494         do_facet mds1 $LCTL set_param fail_val=500
9495         touch $DIR/$tdir/m
9496
9497         echo "sleep 10 seconds ..."
9498         sleep 10
9499         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
9500
9501         do_facet mds1 $LCTL set_param fail_loc=0
9502         do_facet mds1 $LCTL set_param fail_val=0
9503         [ $lck_cnt -lt $unused ] ||
9504                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
9505
9506         rm $DIR/$tdir/m
9507         unlinkmany $DIR/$tdir/f $nr
9508 }
9509 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
9510
9511 test_134b() {
9512         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9513                 skip "Need MDS version at least 2.7.54" && return
9514
9515         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9516         cancel_lru_locks mdc
9517
9518         local low_wm=$(do_facet mds1 $LCTL get_param -n \
9519                         ldlm.lock_reclaim_threshold_mb)
9520         # disable reclaim temporarily
9521         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
9522
9523         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
9524         do_facet mds1 $LCTL set_param fail_loc=0x328
9525         do_facet mds1 $LCTL set_param fail_val=500
9526
9527         $LCTL set_param debug=+trace
9528
9529         local nr=600
9530         createmany -o $DIR/$tdir/f $nr &
9531         local create_pid=$!
9532
9533         echo "Sleep $TIMEOUT seconds ..."
9534         sleep $TIMEOUT
9535         if ! ps -p $create_pid  > /dev/null 2>&1; then
9536                 do_facet mds1 $LCTL set_param fail_loc=0
9537                 do_facet mds1 $LCTL set_param fail_val=0
9538                 do_facet mds1 $LCTL set_param \
9539                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
9540                 error "createmany finished incorrectly!"
9541         fi
9542         do_facet mds1 $LCTL set_param fail_loc=0
9543         do_facet mds1 $LCTL set_param fail_val=0
9544         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
9545         wait $create_pid || return 1
9546
9547         unlinkmany $DIR/$tdir/f $nr
9548 }
9549 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
9550
9551 test_140() { #bug-17379
9552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9553         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9554         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9555         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9556
9557         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9558         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9559         local i=0
9560         while i=`expr $i + 1`; do
9561                 test_mkdir -p $i || error "Creating dir $i"
9562                 cd $i || error "Changing to $i"
9563                 ln -s ../stat stat || error "Creating stat symlink"
9564                 # Read the symlink until ELOOP present,
9565                 # not LBUGing the system is considered success,
9566                 # we didn't overrun the stack.
9567                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9568                 [ $ret -ne 0 ] && {
9569                         if [ $ret -eq 40 ]; then
9570                                 break  # -ELOOP
9571                         else
9572                                 error "Open stat symlink"
9573                                 return
9574                         fi
9575                 }
9576         done
9577         i=`expr $i - 1`
9578         echo "The symlink depth = $i"
9579         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9580                                         error "Invalid symlink depth"
9581
9582         # Test recursive symlink
9583         ln -s symlink_self symlink_self
9584         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9585         echo "open symlink_self returns $ret"
9586         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9587 }
9588 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9589
9590 test_150() {
9591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9592         local TF="$TMP/$tfile"
9593
9594         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9595         cp $TF $DIR/$tfile
9596         cancel_lru_locks osc
9597         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9598         remount_client $MOUNT
9599         df -P $MOUNT
9600         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9601
9602         $TRUNCATE $TF 6000
9603         $TRUNCATE $DIR/$tfile 6000
9604         cancel_lru_locks osc
9605         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9606
9607         echo "12345" >>$TF
9608         echo "12345" >>$DIR/$tfile
9609         cancel_lru_locks osc
9610         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9611
9612         echo "12345" >>$TF
9613         echo "12345" >>$DIR/$tfile
9614         cancel_lru_locks osc
9615         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9616
9617         rm -f $TF
9618         true
9619 }
9620 run_test 150 "truncate/append tests"
9621
9622 #LU-2902 roc_hit was not able to read all values from lproc
9623 function roc_hit_init() {
9624         local list=$(comma_list $(osts_nodes))
9625         local dir=$DIR/$tdir-check
9626         local file=$dir/file
9627         local BEFORE
9628         local AFTER
9629         local idx
9630
9631         test_mkdir -p $dir
9632         #use setstripe to do a write to every ost
9633         for i in $(seq 0 $((OSTCOUNT-1))); do
9634                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9635                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9636                 idx=$(printf %04x $i)
9637                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9638                         awk '$1 == "cache_access" {sum += $7}
9639                                 END { printf("%0.0f", sum) }')
9640
9641                 cancel_lru_locks osc
9642                 cat $file >/dev/null
9643
9644                 AFTER=$(get_osd_param $list *OST*$idx stats |
9645                         awk '$1 == "cache_access" {sum += $7}
9646                                 END { printf("%0.0f", sum) }')
9647
9648                 echo BEFORE:$BEFORE AFTER:$AFTER
9649                 if ! let "AFTER - BEFORE == 4"; then
9650                         rm -rf $dir
9651                         error "roc_hit is not safe to use"
9652                 fi
9653                 rm $file
9654         done
9655
9656         rm -rf $dir
9657 }
9658
9659 function roc_hit() {
9660         local list=$(comma_list $(osts_nodes))
9661         echo $(get_osd_param $list '' stats |
9662                 awk '$1 == "cache_hit" {sum += $7}
9663                         END { printf("%0.0f", sum) }')
9664 }
9665
9666 function set_cache() {
9667         local on=1
9668
9669         if [ "$2" == "off" ]; then
9670                 on=0;
9671         fi
9672         local list=$(comma_list $(osts_nodes))
9673         set_osd_param $list '' $1_cache_enable $on
9674
9675         cancel_lru_locks osc
9676 }
9677
9678 test_151() {
9679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9680         remote_ost_nodsh && skip "remote OST with nodsh" && return
9681
9682         local CPAGES=3
9683         local list=$(comma_list $(osts_nodes))
9684
9685         # check whether obdfilter is cache capable at all
9686         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9687                 echo "not cache-capable obdfilter"
9688                 return 0
9689         fi
9690
9691         # check cache is enabled on all obdfilters
9692         if get_osd_param $list '' read_cache_enable | grep 0; then
9693                 echo "oss cache is disabled"
9694                 return 0
9695         fi
9696
9697         set_osd_param $list '' writethrough_cache_enable 1
9698
9699         # check write cache is enabled on all obdfilters
9700         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9701                 echo "oss write cache is NOT enabled"
9702                 return 0
9703         fi
9704
9705         roc_hit_init
9706
9707         #define OBD_FAIL_OBD_NO_LRU  0x609
9708         do_nodes $list $LCTL set_param fail_loc=0x609
9709
9710         # pages should be in the case right after write
9711         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9712                 error "dd failed"
9713
9714         local BEFORE=$(roc_hit)
9715         cancel_lru_locks osc
9716         cat $DIR/$tfile >/dev/null
9717         local AFTER=$(roc_hit)
9718
9719         do_nodes $list $LCTL set_param fail_loc=0
9720
9721         if ! let "AFTER - BEFORE == CPAGES"; then
9722                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9723         fi
9724
9725         # the following read invalidates the cache
9726         cancel_lru_locks osc
9727         set_osd_param $list '' read_cache_enable 0
9728         cat $DIR/$tfile >/dev/null
9729
9730         # now data shouldn't be found in the cache
9731         BEFORE=$(roc_hit)
9732         cancel_lru_locks osc
9733         cat $DIR/$tfile >/dev/null
9734         AFTER=$(roc_hit)
9735         if let "AFTER - BEFORE != 0"; then
9736                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9737         fi
9738
9739         set_osd_param $list '' read_cache_enable 1
9740         rm -f $DIR/$tfile
9741 }
9742 run_test 151 "test cache on oss and controls ==============================="
9743
9744 test_152() {
9745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9746         local TF="$TMP/$tfile"
9747
9748         # simulate ENOMEM during write
9749 #define OBD_FAIL_OST_NOMEM      0x226
9750         lctl set_param fail_loc=0x80000226
9751         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9752         cp $TF $DIR/$tfile
9753         sync || error "sync failed"
9754         lctl set_param fail_loc=0
9755
9756         # discard client's cache
9757         cancel_lru_locks osc
9758
9759         # simulate ENOMEM during read
9760         lctl set_param fail_loc=0x80000226
9761         cmp $TF $DIR/$tfile || error "cmp failed"
9762         lctl set_param fail_loc=0
9763
9764         rm -f $TF
9765 }
9766 run_test 152 "test read/write with enomem ============================"
9767
9768 test_153() {
9769         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9770 }
9771 run_test 153 "test if fdatasync does not crash ======================="
9772
9773 dot_lustre_fid_permission_check() {
9774         local fid=$1
9775         local ffid=$MOUNT/.lustre/fid/$fid
9776         local test_dir=$2
9777
9778         echo "stat fid $fid"
9779         stat $ffid > /dev/null || error "stat $ffid failed."
9780         echo "touch fid $fid"
9781         touch $ffid || error "touch $ffid failed."
9782         echo "write to fid $fid"
9783         cat /etc/hosts > $ffid || error "write $ffid failed."
9784         echo "read fid $fid"
9785         diff /etc/hosts $ffid || error "read $ffid failed."
9786         echo "append write to fid $fid"
9787         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9788         echo "rename fid $fid"
9789         mv $ffid $test_dir/$tfile.1 &&
9790                 error "rename $ffid to $tfile.1 should fail."
9791         touch $test_dir/$tfile.1
9792         mv $test_dir/$tfile.1 $ffid &&
9793                 error "rename $tfile.1 to $ffid should fail."
9794         rm -f $test_dir/$tfile.1
9795         echo "truncate fid $fid"
9796         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9797         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9798                 echo "link fid $fid"
9799                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9800         fi
9801         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9802                 echo "setfacl fid $fid"
9803                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9804                 echo "getfacl fid $fid"
9805                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9806         fi
9807         echo "unlink fid $fid"
9808         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9809         echo "mknod fid $fid"
9810         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9811
9812         fid=[0xf00000400:0x1:0x0]
9813         ffid=$MOUNT/.lustre/fid/$fid
9814
9815         echo "stat non-exist fid $fid"
9816         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9817         echo "write to non-exist fid $fid"
9818         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9819         echo "link new fid $fid"
9820         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9821
9822         mkdir -p $test_dir/$tdir
9823         touch $test_dir/$tdir/$tfile
9824         fid=$($LFS path2fid $test_dir/$tdir)
9825         rc=$?
9826         [ $rc -ne 0 ] &&
9827                 error "error: could not get fid for $test_dir/$dir/$tfile."
9828
9829         ffid=$MOUNT/.lustre/fid/$fid
9830
9831         echo "ls $fid"
9832         ls $ffid > /dev/null || error "ls $ffid failed."
9833         echo "touch $fid/$tfile.1"
9834         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9835
9836         echo "touch $MOUNT/.lustre/fid/$tfile"
9837         touch $MOUNT/.lustre/fid/$tfile && \
9838                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9839
9840         echo "setxattr to $MOUNT/.lustre/fid"
9841         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9842
9843         echo "listxattr for $MOUNT/.lustre/fid"
9844         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9845
9846         echo "delxattr from $MOUNT/.lustre/fid"
9847         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9848
9849         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9850         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9851                 error "touch invalid fid should fail."
9852
9853         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9854         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9855                 error "touch non-normal fid should fail."
9856
9857         echo "rename $tdir to $MOUNT/.lustre/fid"
9858         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9859                 error "rename to $MOUNT/.lustre/fid should fail."
9860
9861         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9862         then            # LU-3547
9863                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9864                 local new_obf_mode=777
9865
9866                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9867                 chmod $new_obf_mode $DIR/.lustre/fid ||
9868                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9869
9870                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9871                 [ $obf_mode -eq $new_obf_mode ] ||
9872                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9873
9874                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9875                 chmod $old_obf_mode $DIR/.lustre/fid ||
9876                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9877         fi
9878
9879         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9880         fid=$($LFS path2fid $test_dir/$tfile-2)
9881
9882         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9883         then # LU-5424
9884                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9885                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9886                         error "create lov data thru .lustre failed"
9887         fi
9888         echo "cp /etc/passwd $test_dir/$tfile-2"
9889         cp /etc/passwd $test_dir/$tfile-2 ||
9890                 error "copy to $test_dir/$tfile-2 failed."
9891         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9892         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9893                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9894
9895         rm -rf $test_dir/tfile.lnk
9896         rm -rf $test_dir/$tfile-2
9897 }
9898
9899 test_154A() {
9900         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9901                 skip "Need MDS version at least 2.4.1" && return
9902
9903         touch $DIR/$tfile
9904         local FID=$($LFS path2fid $DIR/$tfile)
9905         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9906
9907         # check that we get the same pathname back
9908         local FOUND=$($LFS fid2path $MOUNT "$FID")
9909         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9910         [ "$FOUND" != "$DIR/$tfile" ] &&
9911                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9912
9913         rm -rf $DIR/$tfile
9914 }
9915 run_test 154A "lfs path2fid and fid2path basic checks"
9916
9917 test_154a() {
9918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9919         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9920                 { skip "Need MDS version at least 2.2.51"; return 0; }
9921         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9922
9923         cp /etc/hosts $DIR/$tfile
9924
9925         fid=$($LFS path2fid $DIR/$tfile)
9926         rc=$?
9927         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9928
9929         dot_lustre_fid_permission_check "$fid" $DIR ||
9930                 error "dot lustre permission check $fid failed"
9931
9932         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9933
9934         touch $MOUNT/.lustre/file &&
9935                 error "creation is not allowed under .lustre"
9936
9937         mkdir $MOUNT/.lustre/dir &&
9938                 error "mkdir is not allowed under .lustre"
9939
9940         rm -rf $DIR/$tfile
9941 }
9942 run_test 154a "Open-by-FID"
9943
9944 test_154b() {
9945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9946         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9947                 { skip "Need MDS version at least 2.2.51"; return 0; }
9948
9949         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9950
9951         local remote_dir=$DIR/$tdir/remote_dir
9952         local MDTIDX=1
9953         local rc=0
9954
9955         mkdir -p $DIR/$tdir
9956         $LFS mkdir -i $MDTIDX $remote_dir ||
9957                 error "create remote directory failed"
9958
9959         cp /etc/hosts $remote_dir/$tfile
9960
9961         fid=$($LFS path2fid $remote_dir/$tfile)
9962         rc=$?
9963         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9964
9965         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9966                 error "dot lustre permission check $fid failed"
9967         rm -rf $DIR/$tdir
9968 }
9969 run_test 154b "Open-by-FID for remote directory"
9970
9971 test_154c() {
9972         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9973                 skip "Need MDS version at least 2.4.1" && return
9974
9975         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9976         local FID1=$($LFS path2fid $DIR/$tfile.1)
9977         local FID2=$($LFS path2fid $DIR/$tfile.2)
9978         local FID3=$($LFS path2fid $DIR/$tfile.3)
9979
9980         local N=1
9981         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9982                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9983                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9984                 local want=FID$N
9985                 [ "$FID" = "${!want}" ] ||
9986                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9987                 N=$((N + 1))
9988         done
9989
9990         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
9991         do
9992                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9993                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9994                 N=$((N + 1))
9995         done
9996 }
9997 run_test 154c "lfs path2fid and fid2path multiple arguments"
9998
9999 test_154d() {
10000         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10001                 skip "Need MDS version at least 2.5.53" && return
10002
10003         if remote_mds; then
10004                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10005         else
10006                 nid="0@lo"
10007         fi
10008         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10009         local fd
10010         local cmd
10011
10012         rm -f $DIR/$tfile
10013         touch $DIR/$tfile
10014
10015         local fid=$($LFS path2fid $DIR/$tfile)
10016         # Open the file
10017         fd=$(free_fd)
10018         cmd="exec $fd<$DIR/$tfile"
10019         eval $cmd
10020         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10021         echo "$fid_list" | grep "$fid"
10022         rc=$?
10023
10024         cmd="exec $fd>/dev/null"
10025         eval $cmd
10026         if [ $rc -ne 0 ]; then
10027                 error "FID $fid not found in open files list $fid_list"
10028         fi
10029 }
10030 run_test 154d "Verify open file fid"
10031
10032 test_154e()
10033 {
10034         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10035                 skip "Need MDS version at least 2.6.50" && return
10036
10037         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10038                 error ".lustre returned by readdir"
10039         fi
10040 }
10041 run_test 154e ".lustre is not returned by readdir"
10042
10043 test_154f() {
10044         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10045         test_mkdir -p -c1 $DIR/$tdir/d
10046         # test dirs inherit from its stripe
10047         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10048         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10049         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10050         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10051         touch $DIR/f
10052
10053         # get fid of parents
10054         local FID0=$($LFS path2fid $DIR/$tdir/d)
10055         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10056         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10057         local FID3=$($LFS path2fid $DIR)
10058
10059         # check that path2fid --parents returns expected <parent_fid>/name
10060         # 1) test for a directory (single parent)
10061         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10062         [ "$parent" == "$FID0/foo1" ] ||
10063                 error "expected parent: $FID0/foo1, got: $parent"
10064
10065         # 2) test for a file with nlink > 1 (multiple parents)
10066         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10067         echo "$parent" | grep -F "$FID1/$tfile" ||
10068                 error "$FID1/$tfile not returned in parent list"
10069         echo "$parent" | grep -F "$FID2/link" ||
10070                 error "$FID2/link not returned in parent list"
10071
10072         # 3) get parent by fid
10073         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10074         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10075         echo "$parent" | grep -F "$FID1/$tfile" ||
10076                 error "$FID1/$tfile not returned in parent list (by fid)"
10077         echo "$parent" | grep -F "$FID2/link" ||
10078                 error "$FID2/link not returned in parent list (by fid)"
10079
10080         # 4) test for entry in root directory
10081         parent=$($LFS path2fid --parents $DIR/f)
10082         echo "$parent" | grep -F "$FID3/f" ||
10083                 error "$FID3/f not returned in parent list"
10084
10085         # 5) test it on root directory
10086         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10087                 error "$MOUNT should not have parents"
10088
10089         # enable xattr caching and check that linkea is correctly updated
10090         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10091         save_lustre_params client "llite.*.xattr_cache" > $save
10092         lctl set_param llite.*.xattr_cache 1
10093
10094         # 6.1) linkea update on rename
10095         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10096
10097         # get parents by fid
10098         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10099         # foo1 should no longer be returned in parent list
10100         echo "$parent" | grep -F "$FID1" &&
10101                 error "$FID1 should no longer be in parent list"
10102         # the new path should appear
10103         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10104                 error "$FID2/$tfile.moved is not in parent list"
10105
10106         # 6.2) linkea update on unlink
10107         rm -f $DIR/$tdir/d/foo2/link
10108         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10109         # foo2/link should no longer be returned in parent list
10110         echo "$parent" | grep -F "$FID2/link" &&
10111                 error "$FID2/link should no longer be in parent list"
10112         true
10113
10114         rm -f $DIR/f
10115         restore_lustre_params < $save
10116 }
10117 run_test 154f "get parent fids by reading link ea"
10118
10119 test_154g()
10120 {
10121         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) ]] ||
10122                 { skip "Need MDS version at least 2.6.92"; return 0; }
10123
10124         mkdir -p $DIR/$tdir
10125         llapi_fid_test -d $DIR/$tdir
10126 }
10127 run_test 154g "various llapi FID tests"
10128
10129 test_155_small_load() {
10130     local temp=$TMP/$tfile
10131     local file=$DIR/$tfile
10132
10133     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10134         error "dd of=$temp bs=6096 count=1 failed"
10135     cp $temp $file
10136     cancel_lru_locks osc
10137     cmp $temp $file || error "$temp $file differ"
10138
10139     $TRUNCATE $temp 6000
10140     $TRUNCATE $file 6000
10141     cmp $temp $file || error "$temp $file differ (truncate1)"
10142
10143     echo "12345" >>$temp
10144     echo "12345" >>$file
10145     cmp $temp $file || error "$temp $file differ (append1)"
10146
10147     echo "12345" >>$temp
10148     echo "12345" >>$file
10149     cmp $temp $file || error "$temp $file differ (append2)"
10150
10151     rm -f $temp $file
10152     true
10153 }
10154
10155 test_155_big_load() {
10156     remote_ost_nodsh && skip "remote OST with nodsh" && return
10157     local temp=$TMP/$tfile
10158     local file=$DIR/$tfile
10159
10160     free_min_max
10161     local cache_size=$(do_facet ost$((MAXI+1)) \
10162         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10163     local large_file_size=$((cache_size * 2))
10164
10165     echo "OSS cache size: $cache_size KB"
10166     echo "Large file size: $large_file_size KB"
10167
10168     [ $MAXV -le $large_file_size ] && \
10169         skip_env "max available OST size needs > $large_file_size KB" && \
10170         return 0
10171
10172     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10173
10174     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10175         error "dd of=$temp bs=$large_file_size count=1k failed"
10176     cp $temp $file
10177     ls -lh $temp $file
10178     cancel_lru_locks osc
10179     cmp $temp $file || error "$temp $file differ"
10180
10181     rm -f $temp $file
10182     true
10183 }
10184
10185 test_155a() {
10186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10187         set_cache read on
10188         set_cache writethrough on
10189         test_155_small_load
10190 }
10191 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10192
10193 test_155b() {
10194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10195         set_cache read on
10196         set_cache writethrough off
10197         test_155_small_load
10198 }
10199 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10200
10201 test_155c() {
10202         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10203         set_cache read off
10204         set_cache writethrough on
10205         test_155_small_load
10206 }
10207 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10208
10209 test_155d() {
10210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10211         set_cache read off
10212         set_cache writethrough off
10213         test_155_small_load
10214 }
10215 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10216
10217 test_155e() {
10218         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10219         set_cache read on
10220         set_cache writethrough on
10221         test_155_big_load
10222 }
10223 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10224
10225 test_155f() {
10226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10227         set_cache read on
10228         set_cache writethrough off
10229         test_155_big_load
10230 }
10231 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10232
10233 test_155g() {
10234         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10235         set_cache read off
10236         set_cache writethrough on
10237         test_155_big_load
10238 }
10239 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10240
10241 test_155h() {
10242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10243         set_cache read off
10244         set_cache writethrough off
10245         test_155_big_load
10246 }
10247 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10248
10249 test_156() {
10250         remote_ost_nodsh && skip "remote OST with nodsh" && return
10251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10252         local CPAGES=3
10253         local BEFORE
10254         local AFTER
10255         local file="$DIR/$tfile"
10256
10257         [ "$(facet_fstype ost1)" = "zfs" -a \
10258            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10259                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10260                 return
10261
10262         roc_hit_init
10263
10264         log "Turn on read and write cache"
10265         set_cache read on
10266         set_cache writethrough on
10267
10268         log "Write data and read it back."
10269         log "Read should be satisfied from the cache."
10270         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10271         BEFORE=$(roc_hit)
10272         cancel_lru_locks osc
10273         cat $file >/dev/null
10274         AFTER=$(roc_hit)
10275         if ! let "AFTER - BEFORE == CPAGES"; then
10276                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10277         else
10278                 log "cache hits:: before: $BEFORE, after: $AFTER"
10279         fi
10280
10281         log "Read again; it should be satisfied from the cache."
10282         BEFORE=$AFTER
10283         cancel_lru_locks osc
10284         cat $file >/dev/null
10285         AFTER=$(roc_hit)
10286         if ! let "AFTER - BEFORE == CPAGES"; then
10287                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10288         else
10289                 log "cache hits:: before: $BEFORE, after: $AFTER"
10290         fi
10291
10292         log "Turn off the read cache and turn on the write cache"
10293         set_cache read off
10294         set_cache writethrough on
10295
10296         log "Read again; it should be satisfied from the cache."
10297         BEFORE=$(roc_hit)
10298         cancel_lru_locks osc
10299         cat $file >/dev/null
10300         AFTER=$(roc_hit)
10301         if ! let "AFTER - BEFORE == CPAGES"; then
10302                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10303         else
10304                 log "cache hits:: before: $BEFORE, after: $AFTER"
10305         fi
10306
10307         log "Read again; it should not be satisfied from the cache."
10308         BEFORE=$AFTER
10309         cancel_lru_locks osc
10310         cat $file >/dev/null
10311         AFTER=$(roc_hit)
10312         if ! let "AFTER - BEFORE == 0"; then
10313                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10314         else
10315                 log "cache hits:: before: $BEFORE, after: $AFTER"
10316         fi
10317
10318         log "Write data and read it back."
10319         log "Read should be satisfied from the cache."
10320         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10321         BEFORE=$(roc_hit)
10322         cancel_lru_locks osc
10323         cat $file >/dev/null
10324         AFTER=$(roc_hit)
10325         if ! let "AFTER - BEFORE == CPAGES"; then
10326                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10327         else
10328                 log "cache hits:: before: $BEFORE, after: $AFTER"
10329         fi
10330
10331         log "Read again; it should not be satisfied from the cache."
10332         BEFORE=$AFTER
10333         cancel_lru_locks osc
10334         cat $file >/dev/null
10335         AFTER=$(roc_hit)
10336         if ! let "AFTER - BEFORE == 0"; then
10337                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10338         else
10339                 log "cache hits:: before: $BEFORE, after: $AFTER"
10340         fi
10341
10342         log "Turn off read and write cache"
10343         set_cache read off
10344         set_cache writethrough off
10345
10346         log "Write data and read it back"
10347         log "It should not be satisfied from the cache."
10348         rm -f $file
10349         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10350         cancel_lru_locks osc
10351         BEFORE=$(roc_hit)
10352         cat $file >/dev/null
10353         AFTER=$(roc_hit)
10354         if ! let "AFTER - BEFORE == 0"; then
10355                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10356         else
10357                 log "cache hits:: before: $BEFORE, after: $AFTER"
10358         fi
10359
10360         log "Turn on the read cache and turn off the write cache"
10361         set_cache read on
10362         set_cache writethrough off
10363
10364         log "Write data and read it back"
10365         log "It should not be satisfied from the cache."
10366         rm -f $file
10367         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10368         BEFORE=$(roc_hit)
10369         cancel_lru_locks osc
10370         cat $file >/dev/null
10371         AFTER=$(roc_hit)
10372         if ! let "AFTER - BEFORE == 0"; then
10373                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10374         else
10375                 log "cache hits:: before: $BEFORE, after: $AFTER"
10376         fi
10377
10378         log "Read again; it should be satisfied from the cache."
10379         BEFORE=$(roc_hit)
10380         cancel_lru_locks osc
10381         cat $file >/dev/null
10382         AFTER=$(roc_hit)
10383         if ! let "AFTER - BEFORE == CPAGES"; then
10384                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10385         else
10386                 log "cache hits:: before: $BEFORE, after: $AFTER"
10387         fi
10388
10389         rm -f $file
10390 }
10391 run_test 156 "Verification of tunables"
10392
10393 #Changelogs
10394 err17935 () {
10395         if [[ $MDSCOUNT -gt 1 ]]; then
10396                 error_ignore bz17935 $*
10397         else
10398                 error $*
10399         fi
10400 }
10401
10402 changelog_chmask()
10403 {
10404         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10405
10406         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10407
10408         if [ $MASK -eq 1 ]; then
10409                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10410         else
10411                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10412         fi
10413 }
10414
10415 changelog_extract_field() {
10416         local mdt=$1
10417         local cltype=$2
10418         local file=$3
10419         local identifier=$4
10420
10421         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10422                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10423                 tail -1
10424 }
10425
10426 test_160a() {
10427         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10428         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10429         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10430                 { skip "Need MDS version at least 2.2.0"; return; }
10431
10432         local CL_USERS="mdd.$MDT0.changelog_users"
10433         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10434         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10435         echo "Registered as changelog user $USER"
10436         $GET_CL_USERS | grep -q $USER ||
10437                 error "User $USER not found in changelog_users"
10438
10439         # change something
10440         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10441         touch $DIR/$tdir/pics/2008/zachy/timestamp
10442         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10443         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10444         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10445         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10446         rm $DIR/$tdir/pics/desktop.jpg
10447
10448         $LFS changelog $MDT0 | tail -5
10449
10450         echo "verifying changelog mask"
10451         changelog_chmask "MKDIR"
10452         changelog_chmask "CLOSE"
10453
10454         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10455         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10456
10457         changelog_chmask "MKDIR"
10458         changelog_chmask "CLOSE"
10459
10460         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10461         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10462
10463         $LFS changelog $MDT0
10464         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10465         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10466         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10467         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10468
10469         # verify contents
10470         echo "verifying target fid"
10471         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10472         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10473         [ "$fidc" == "$fidf" ] ||
10474                 err17935 "fid in changelog $fidc != file fid $fidf"
10475         echo "verifying parent fid"
10476         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10477         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10478         [ "$fidc" == "$fidf" ] ||
10479                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10480
10481         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10482         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10483         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10484         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10485         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10486                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10487
10488         MIN_REC=$($GET_CL_USERS |
10489                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10490         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10491         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10492         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10493                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10494
10495         # LU-3446 changelog index reset on MDT restart
10496         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10497         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10498         $LFS changelog_clear $MDT0 $USER 0
10499         stop $SINGLEMDS || error "Fail to stop MDT."
10500         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10501         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10502         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10503         [ $CUR_REC1 == $CUR_REC2 ] ||
10504                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10505
10506         echo "verifying user deregister"
10507         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10508         $GET_CL_USERS | grep -q $USER &&
10509                 error "User $USER still in changelog_users"
10510
10511         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10512         if [ $USERS -eq 0 ]; then
10513                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10514                 touch $DIR/$tdir/chloe
10515                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10516                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10517                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10518         else
10519                 echo "$USERS other changelog users; can't verify off"
10520         fi
10521 }
10522 run_test 160a "changelog sanity"
10523
10524 test_160b() { # LU-3587
10525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10526         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10527         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10528                 { skip "Need MDS version at least 2.2.0"; return; }
10529
10530         local CL_USERS="mdd.$MDT0.changelog_users"
10531         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10532         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10533         echo "Registered as changelog user $USER"
10534         $GET_CL_USERS | grep -q $USER ||
10535                 error "User $USER not found in changelog_users"
10536
10537         local LONGNAME1=$(str_repeat a 255)
10538         local LONGNAME2=$(str_repeat b 255)
10539
10540         cd $DIR
10541         echo "creating very long named file"
10542         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10543         echo "moving very long named file"
10544         mv $LONGNAME1 $LONGNAME2
10545
10546         $LFS changelog $MDT0 | grep RENME
10547
10548         echo "deregistering $USER"
10549         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10550
10551         rm -f $LONGNAME2
10552 }
10553 run_test 160b "Verify that very long rename doesn't crash in changelog"
10554
10555 test_160c() {
10556         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10557
10558         local rc=0
10559         local server_version=$(lustre_version_code $SINGLEMDS)
10560
10561         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10562                 [[ $server_version -gt $(version_code 2.5.1) &&
10563                    $server_version -lt $(version_code 2.5.50) ]] ||
10564                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10566
10567         # Registration step
10568         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10569                 changelog_register -n)
10570
10571         rm -rf $DIR/$tdir
10572         mkdir -p $DIR/$tdir
10573         $MCREATE $DIR/$tdir/foo_160c
10574         changelog_chmask "TRUNC"
10575         $TRUNCATE $DIR/$tdir/foo_160c 200
10576         changelog_chmask "TRUNC"
10577         $TRUNCATE $DIR/$tdir/foo_160c 199
10578         $LFS changelog $MDT0
10579         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10580         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10581         $LFS changelog_clear $MDT0 $USER 0
10582
10583         # Deregistration step
10584         echo "deregistering $USER"
10585         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10586 }
10587 run_test 160c "verify that changelog log catch the truncate event"
10588
10589 test_160d() {
10590         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10591         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10592
10593         local server_version=$(lustre_version_code mds1)
10594         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10595
10596         [[ $server_version -ge $(version_code 2.7.60) ]] ||
10597                 { skip "Need MDS version at least 2.7.60+"; return; }
10598         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10599
10600         # Registration step
10601         local USER=$(do_facet mds1 $LCTL --device $MDT0 \
10602                 changelog_register -n)
10603
10604         mkdir -p $DIR/$tdir/migrate_dir
10605         $LFS changelog_clear $MDT0 $USER 0
10606
10607         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
10608         $LFS changelog $MDT0
10609         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
10610         $LFS changelog_clear $MDT0 $USER 0
10611         [ $MIGRATES -eq 1 ] ||
10612                 error "MIGRATE changelog mask count $MIGRATES != 1"
10613
10614         # Deregistration step
10615         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $USER
10616 }
10617 run_test 160d "verify that changelog log catch the migrate event"
10618
10619 test_161a() {
10620         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10621         test_mkdir -p -c1 $DIR/$tdir
10622         cp /etc/hosts $DIR/$tdir/$tfile
10623         test_mkdir -c1 $DIR/$tdir/foo1
10624         test_mkdir -c1 $DIR/$tdir/foo2
10625         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10626         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10627         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10628         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10629         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10630         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10631                 $LFS fid2path $DIR $FID
10632                 err17935 "bad link ea"
10633         fi
10634     # middle
10635     rm $DIR/$tdir/foo2/zachary
10636     # last
10637     rm $DIR/$tdir/foo2/thor
10638     # first
10639     rm $DIR/$tdir/$tfile
10640     # rename
10641     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10642     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10643         then
10644         $LFS fid2path $DIR $FID
10645         err17935 "bad link rename"
10646     fi
10647     rm $DIR/$tdir/foo2/maggie
10648
10649         # overflow the EA
10650         local longname=filename_avg_len_is_thirty_two_
10651         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10652                 error "failed to hardlink many files"
10653         links=$($LFS fid2path $DIR $FID | wc -l)
10654         echo -n "${links}/1000 links in link EA"
10655         [[ $links -gt 60 ]] ||
10656                 err17935 "expected at least 60 links in link EA"
10657         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10658                 error "failed to unlink many hardlinks"
10659 }
10660 run_test 161a "link ea sanity"
10661
10662 test_161b() {
10663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10664         [ $MDSCOUNT -lt 2 ] &&
10665                 skip "skipping remote directory test" && return
10666         local MDTIDX=1
10667         local remote_dir=$DIR/$tdir/remote_dir
10668
10669         mkdir -p $DIR/$tdir
10670         $LFS mkdir -i $MDTIDX $remote_dir ||
10671                 error "create remote directory failed"
10672
10673         cp /etc/hosts $remote_dir/$tfile
10674         mkdir -p $remote_dir/foo1
10675         mkdir -p $remote_dir/foo2
10676         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10677         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10678         ln $remote_dir/$tfile $remote_dir/foo1/luna
10679         ln $remote_dir/$tfile $remote_dir/foo2/thor
10680
10681         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10682                      tr -d ']')
10683         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10684                 $LFS fid2path $DIR $FID
10685                 err17935 "bad link ea"
10686         fi
10687         # middle
10688         rm $remote_dir/foo2/zachary
10689         # last
10690         rm $remote_dir/foo2/thor
10691         # first
10692         rm $remote_dir/$tfile
10693         # rename
10694         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10695         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10696         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10697                 $LFS fid2path $DIR $FID
10698                 err17935 "bad link rename"
10699         fi
10700         rm $remote_dir/foo2/maggie
10701
10702         # overflow the EA
10703         local longname=filename_avg_len_is_thirty_two_
10704         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10705                 error "failed to hardlink many files"
10706         links=$($LFS fid2path $DIR $FID | wc -l)
10707         echo -n "${links}/1000 links in link EA"
10708         [[ ${links} -gt 60 ]] ||
10709                 err17935 "expected at least 60 links in link EA"
10710         unlinkmany $remote_dir/foo2/$longname 1000 ||
10711         error "failed to unlink many hardlinks"
10712 }
10713 run_test 161b "link ea sanity under remote directory"
10714
10715 test_161c() {
10716         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10718         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10719                 skip "Need MDS version at least 2.1.5" && return
10720
10721         # define CLF_RENAME_LAST 0x0001
10722         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10723         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10724                 changelog_register -n)
10725         rm -rf $DIR/$tdir
10726         mkdir -p $DIR/$tdir
10727         touch $DIR/$tdir/foo_161c
10728         touch $DIR/$tdir/bar_161c
10729         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10730         $LFS changelog $MDT0 | grep RENME
10731         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10732                 cut -f5 -d' ')
10733         $LFS changelog_clear $MDT0 $USER 0
10734         if [ x$flags != "x0x1" ]; then
10735                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10736                         $USER
10737                 error "flag $flags is not 0x1"
10738         fi
10739         echo "rename overwrite a target having nlink = 1," \
10740                 "changelog record has flags of $flags"
10741
10742         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10743         touch $DIR/$tdir/foo_161c
10744         touch $DIR/$tdir/bar_161c
10745         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10746         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10747         $LFS changelog $MDT0 | grep RENME
10748         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10749         $LFS changelog_clear $MDT0 $USER 0
10750         if [ x$flags != "x0x0" ]; then
10751                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10752                         $USER
10753                 error "flag $flags is not 0x0"
10754         fi
10755         echo "rename overwrite a target having nlink > 1," \
10756                 "changelog record has flags of $flags"
10757
10758         # rename doesn't overwrite a target (changelog flag 0x0)
10759         touch $DIR/$tdir/foo_161c
10760         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10761         $LFS changelog $MDT0 | grep RENME
10762         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10763         $LFS changelog_clear $MDT0 $USER 0
10764         if [ x$flags != "x0x0" ]; then
10765                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10766                         $USER
10767                 error "flag $flags is not 0x0"
10768         fi
10769         echo "rename doesn't overwrite a target," \
10770                 "changelog record has flags of $flags"
10771
10772         # define CLF_UNLINK_LAST 0x0001
10773         # unlink a file having nlink = 1 (changelog flag 0x1)
10774         rm -f $DIR/$tdir/foo2_161c
10775         $LFS changelog $MDT0 | grep UNLNK
10776         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10777         $LFS changelog_clear $MDT0 $USER 0
10778         if [ x$flags != "x0x1" ]; then
10779                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10780                         $USER
10781                 error "flag $flags is not 0x1"
10782         fi
10783         echo "unlink a file having nlink = 1," \
10784                 "changelog record has flags of $flags"
10785
10786         # unlink a file having nlink > 1 (changelog flag 0x0)
10787         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10788         rm -f $DIR/$tdir/foobar_161c
10789         $LFS changelog $MDT0 | grep UNLNK
10790         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10791         $LFS changelog_clear $MDT0 $USER 0
10792         if [ x$flags != "x0x0" ]; then
10793                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10794                         $USER
10795                 error "flag $flags is not 0x0"
10796         fi
10797         echo "unlink a file having nlink > 1," \
10798                 "changelog record has flags of $flags"
10799         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10800 }
10801 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10802
10803 check_path() {
10804     local expected=$1
10805     shift
10806     local fid=$2
10807
10808     local path=$(${LFS} fid2path $*)
10809     # Remove the '//' indicating a remote directory
10810     path=$(echo $path | sed 's#//#/#g')
10811     RC=$?
10812
10813     if [ $RC -ne 0 ]; then
10814         err17935 "path looked up of $expected failed. Error $RC"
10815         return $RC
10816     elif [ "${path}" != "${expected}" ]; then
10817         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10818         return 2
10819     fi
10820     echo "fid $fid resolves to path $path (expected $expected)"
10821 }
10822
10823 test_162a() { # was test_162
10824         # Make changes to filesystem
10825         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10826         test_mkdir -p -c1 $DIR/$tdir/d2
10827         touch $DIR/$tdir/d2/$tfile
10828         touch $DIR/$tdir/d2/x1
10829         touch $DIR/$tdir/d2/x2
10830         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10831         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10832         # regular file
10833         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10834         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10835                 error "check path $tdir/d2/$tfile failed"
10836
10837         # softlink
10838         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10839         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10840         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10841                 error "check path $tdir/d2/p/q/r/slink failed"
10842
10843         # softlink to wrong file
10844         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10845         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10846         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10847                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10848
10849         # hardlink
10850         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10851         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10852         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10853         # fid2path dir/fsname should both work
10854         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10855                 error "check path $tdir/d2/a/b/c/new_file failed"
10856         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10857                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10858
10859         # hardlink count: check that there are 2 links
10860         # Doesnt work with CMD yet: 17935
10861         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10862                 err17935 "expected 2 links"
10863
10864         # hardlink indexing: remove the first link
10865         rm $DIR/$tdir/d2/p/q/r/hlink
10866         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10867                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10868
10869         return 0
10870 }
10871 run_test 162a "path lookup sanity"
10872
10873 test_162b() {
10874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10875         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10876
10877         mkdir $DIR/$tdir
10878         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10879                                 error "create striped dir failed"
10880
10881         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10882                                         tail -n 1 | awk '{print $2}')
10883         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10884
10885         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10886         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10887
10888         # regular file
10889         for ((i=0;i<5;i++)); do
10890                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10891                         error "get fid for f$i failed"
10892                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10893                         error "check path $tdir/striped_dir/f$i failed"
10894
10895                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10896                         error "get fid for d$i failed"
10897                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10898                         error "check path $tdir/striped_dir/d$i failed"
10899         done
10900
10901         return 0
10902 }
10903 run_test 162b "striped directory path lookup sanity"
10904
10905 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10906 test_162c() {
10907         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
10908                 skip "Need MDS version at least 2.7.51" && return
10909         test_mkdir $DIR/$tdir.local
10910         test_mkdir $DIR/$tdir.remote
10911         local lpath=$tdir.local
10912         local rpath=$tdir.remote
10913
10914         for ((i = 0; i <= 101; i++)); do
10915                 lpath="$lpath/$i"
10916                 mkdir $DIR/$lpath
10917                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10918                         error "get fid for local directory $DIR/$lpath failed"
10919                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10920                         error "check path for local directory $DIR/$lpath failed"
10921
10922                 rpath="$rpath/$i"
10923                 test_mkdir $DIR/$rpath
10924                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10925                         error "get fid for remote directory $DIR/$rpath failed"
10926                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10927                         error "check path for remote directory $DIR/$rpath failed"
10928         done
10929
10930         return 0
10931 }
10932 run_test 162c "fid2path works with paths 100 or more directories deep"
10933
10934 test_169() {
10935         # do directio so as not to populate the page cache
10936         log "creating a 10 Mb file"
10937         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10938         log "starting reads"
10939         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10940         log "truncating the file"
10941         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10942         log "killing dd"
10943         kill %+ || true # reads might have finished
10944         echo "wait until dd is finished"
10945         wait
10946         log "removing the temporary file"
10947         rm -rf $DIR/$tfile || error "tmp file removal failed"
10948 }
10949 run_test 169 "parallel read and truncate should not deadlock"
10950
10951 test_170() {
10952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10953         $LCTL clear     # bug 18514
10954         $LCTL debug_daemon start $TMP/${tfile}_log_good
10955         touch $DIR/$tfile
10956         $LCTL debug_daemon stop
10957         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10958                error "sed failed to read log_good"
10959
10960         $LCTL debug_daemon start $TMP/${tfile}_log_good
10961         rm -rf $DIR/$tfile
10962         $LCTL debug_daemon stop
10963
10964         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10965                error "lctl df log_bad failed"
10966
10967         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10968         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10969
10970         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10971         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10972
10973         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10974                 error "bad_line good_line1 good_line2 are empty"
10975
10976         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10977         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10978         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10979
10980         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10981         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10982         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10983
10984         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10985                 error "bad_line_new good_line_new are empty"
10986
10987         local expected_good=$((good_line1 + good_line2*2))
10988
10989         rm -f $TMP/${tfile}*
10990         # LU-231, short malformed line may not be counted into bad lines
10991         if [ $bad_line -ne $bad_line_new ] &&
10992                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10993                 error "expected $bad_line bad lines, but got $bad_line_new"
10994                 return 1
10995         fi
10996
10997         if [ $expected_good -ne $good_line_new ]; then
10998                 error "expected $expected_good good lines, but got $good_line_new"
10999                 return 2
11000         fi
11001         true
11002 }
11003 run_test 170 "test lctl df to handle corrupted log ====================="
11004
11005 test_171() { # bug20592
11006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11007 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11008         $LCTL set_param fail_loc=0x50e
11009         $LCTL set_param fail_val=3000
11010         multiop_bg_pause $DIR/$tfile O_s || true
11011         local MULTIPID=$!
11012         kill -USR1 $MULTIPID
11013         # cause log dump
11014         sleep 3
11015         wait $MULTIPID
11016         if dmesg | grep "recursive fault"; then
11017                 error "caught a recursive fault"
11018         fi
11019         $LCTL set_param fail_loc=0
11020         true
11021 }
11022 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11023
11024 # it would be good to share it with obdfilter-survey/iokit-libecho code
11025 setup_obdecho_osc () {
11026         local rc=0
11027         local ost_nid=$1
11028         local obdfilter_name=$2
11029         echo "Creating new osc for $obdfilter_name on $ost_nid"
11030         # make sure we can find loopback nid
11031         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11032
11033         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11034                            ${obdfilter_name}_osc_UUID || rc=2; }
11035         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11036                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11037         return $rc
11038 }
11039
11040 cleanup_obdecho_osc () {
11041         local obdfilter_name=$1
11042         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11043         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11044         return 0
11045 }
11046
11047 obdecho_test() {
11048         local OBD=$1
11049         local node=$2
11050         local pages=${3:-64}
11051         local rc=0
11052         local id
11053
11054         local count=10
11055         local obd_size=$(get_obd_size $node $OBD)
11056         local page_size=$(get_page_size $node)
11057         if [[ -n "$obd_size" ]]; then
11058                 local new_count=$((obd_size / (pages * page_size / 1024)))
11059                 [[ $new_count -ge $count ]] || count=$new_count
11060         fi
11061
11062         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11063         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11064                            rc=2; }
11065         if [ $rc -eq 0 ]; then
11066             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11067             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11068         fi
11069         echo "New object id is $id"
11070         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11071                            rc=4; }
11072         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11073                            "test_brw $count w v $pages $id" || rc=4; }
11074         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11075                            rc=4; }
11076         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11077                                         "cleanup" || rc=5; }
11078         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11079                                         "detach" || rc=6; }
11080         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11081         return $rc
11082 }
11083
11084 test_180a() {
11085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11086         remote_ost_nodsh && skip "remote OST with nodsh" && return
11087         local rc=0
11088         local rmmod_local=0
11089
11090         if ! module_loaded obdecho; then
11091             load_module obdecho/obdecho
11092             rmmod_local=1
11093         fi
11094
11095         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11096         local host=$(lctl get_param -n osc.$osc.import |
11097                              awk '/current_connection:/ {print $2}' )
11098         local target=$(lctl get_param -n osc.$osc.import |
11099                              awk '/target:/ {print $2}' )
11100         target=${target%_UUID}
11101
11102         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11103         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11104         [[ -n $target ]] && cleanup_obdecho_osc $target
11105         [ $rmmod_local -eq 1 ] && rmmod obdecho
11106         return $rc
11107 }
11108 run_test 180a "test obdecho on osc"
11109
11110 test_180b() {
11111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11112         remote_ost_nodsh && skip "remote OST with nodsh" && return
11113         local rc=0
11114         local rmmod_remote=0
11115
11116         do_facet ost1 "lsmod | grep -q obdecho || "                      \
11117                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
11118                       "modprobe obdecho; }" && rmmod_remote=1
11119         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11120         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11121         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
11122         return $rc
11123 }
11124 run_test 180b "test obdecho directly on obdfilter"
11125
11126 test_180c() { # LU-2598
11127         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11128         remote_ost_nodsh && skip "remote OST with nodsh" && return
11129         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11130                 skip "Need MDS version at least 2.4.0" && return
11131
11132         local rc=0
11133         local rmmod_remote=false
11134         local pages=16384 # 64MB bulk I/O RPC size
11135         local target
11136
11137         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11138                 rmmod_remote=true || error "failed to load module obdecho"
11139
11140         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11141                 head -n1)
11142         if [ -n "$target" ]; then
11143                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11144         else
11145                 echo "there is no obdfilter target on ost1"
11146                 rc=2
11147         fi
11148         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11149         return $rc
11150 }
11151 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11152
11153 test_181() { # bug 22177
11154         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11155         # create enough files to index the directory
11156         createmany -o $DIR/$tdir/foobar 4000
11157         # print attributes for debug purpose
11158         lsattr -d .
11159         # open dir
11160         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11161         MULTIPID=$!
11162         # remove the files & current working dir
11163         unlinkmany $DIR/$tdir/foobar 4000
11164         rmdir $DIR/$tdir
11165         kill -USR1 $MULTIPID
11166         wait $MULTIPID
11167         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11168         return 0
11169 }
11170 run_test 181 "Test open-unlinked dir ========================"
11171
11172 test_182() {
11173         local fcount=1000
11174         local tcount=10
11175
11176         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11177
11178         $LCTL set_param mdc.*.rpc_stats=clear
11179
11180         for (( i = 0; i < $tcount; i++ )) ; do
11181                 mkdir $DIR/$tdir/$i
11182         done
11183
11184         for (( i = 0; i < $tcount; i++ )) ; do
11185                 createmany -o $DIR/$tdir/$i/f- $fcount &
11186         done
11187         wait
11188
11189         for (( i = 0; i < $tcount; i++ )) ; do
11190                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11191         done
11192         wait
11193
11194         $LCTL get_param mdc.*.rpc_stats
11195
11196         rm -rf $DIR/$tdir
11197 }
11198 run_test 182 "Test parallel modify metadata operations ================"
11199
11200 test_183() { # LU-2275
11201         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11202         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11203                 skip "Need MDS version at least 2.3.56" && return
11204
11205         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11206         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11207         echo aaa > $DIR/$tdir/$tfile
11208
11209 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11210         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11211
11212         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11213         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11214
11215         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11216
11217         # Flush negative dentry cache
11218         touch $DIR/$tdir/$tfile
11219
11220         # We are not checking for any leaked references here, they'll
11221         # become evident next time we do cleanup with module unload.
11222         rm -rf $DIR/$tdir
11223 }
11224 run_test 183 "No crash or request leak in case of strange dispositions ========"
11225
11226 # test suite 184 is for LU-2016, LU-2017
11227 test_184a() {
11228         check_swap_layouts_support && return 0
11229
11230         dir0=$DIR/$tdir/$testnum
11231         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11232         ref1=/etc/passwd
11233         ref2=/etc/group
11234         file1=$dir0/f1
11235         file2=$dir0/f2
11236         $SETSTRIPE -c1 $file1
11237         cp $ref1 $file1
11238         $SETSTRIPE -c2 $file2
11239         cp $ref2 $file2
11240         gen1=$($GETSTRIPE -g $file1)
11241         gen2=$($GETSTRIPE -g $file2)
11242
11243         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11244         gen=$($GETSTRIPE -g $file1)
11245         [[ $gen1 != $gen ]] ||
11246                 "Layout generation on $file1 does not change"
11247         gen=$($GETSTRIPE -g $file2)
11248         [[ $gen2 != $gen ]] ||
11249                 "Layout generation on $file2 does not change"
11250
11251         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11252         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11253 }
11254 run_test 184a "Basic layout swap"
11255
11256 test_184b() {
11257         check_swap_layouts_support && return 0
11258
11259         dir0=$DIR/$tdir/$testnum
11260         mkdir -p $dir0 || error "creating dir $dir0"
11261         file1=$dir0/f1
11262         file2=$dir0/f2
11263         file3=$dir0/f3
11264         dir1=$dir0/d1
11265         dir2=$dir0/d2
11266         mkdir $dir1 $dir2
11267         $SETSTRIPE -c1 $file1
11268         $SETSTRIPE -c2 $file2
11269         $SETSTRIPE -c1 $file3
11270         chown $RUNAS_ID $file3
11271         gen1=$($GETSTRIPE -g $file1)
11272         gen2=$($GETSTRIPE -g $file2)
11273
11274         $LFS swap_layouts $dir1 $dir2 &&
11275                 error "swap of directories layouts should fail"
11276         $LFS swap_layouts $dir1 $file1 &&
11277                 error "swap of directory and file layouts should fail"
11278         $RUNAS $LFS swap_layouts $file1 $file2 &&
11279                 error "swap of file we cannot write should fail"
11280         $LFS swap_layouts $file1 $file3 &&
11281                 error "swap of file with different owner should fail"
11282         /bin/true # to clear error code
11283 }
11284 run_test 184b "Forbidden layout swap (will generate errors)"
11285
11286 test_184c() {
11287         check_swap_layouts_support && return 0
11288
11289         local dir0=$DIR/$tdir/$testnum
11290         mkdir -p $dir0 || error "creating dir $dir0"
11291
11292         local ref1=$dir0/ref1
11293         local ref2=$dir0/ref2
11294         local file1=$dir0/file1
11295         local file2=$dir0/file2
11296         # create a file large enough for the concurrent test
11297         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11298         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11299         echo "ref file size: ref1($(stat -c %s $ref1))," \
11300              "ref2($(stat -c %s $ref2))"
11301
11302         cp $ref2 $file2
11303         dd if=$ref1 of=$file1 bs=16k &
11304         local DD_PID=$!
11305
11306         # Make sure dd starts to copy file
11307         while [ ! -f $file1 ]; do sleep 0.1; done
11308
11309         $LFS swap_layouts $file1 $file2
11310         local rc=$?
11311         wait $DD_PID
11312         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11313         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11314
11315         # how many bytes copied before swapping layout
11316         local copied=$(stat -c %s $file2)
11317         local remaining=$(stat -c %s $ref1)
11318         remaining=$((remaining - copied))
11319         echo "Copied $copied bytes before swapping layout..."
11320
11321         cmp -n $copied $file1 $ref2 | grep differ &&
11322                 error "Content mismatch [0, $copied) of ref2 and file1"
11323         cmp -n $copied $file2 $ref1 ||
11324                 error "Content mismatch [0, $copied) of ref1 and file2"
11325         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11326                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11327
11328         # clean up
11329         rm -f $ref1 $ref2 $file1 $file2
11330 }
11331 run_test 184c "Concurrent write and layout swap"
11332
11333 test_184d() {
11334         check_swap_layouts_support && return 0
11335         [ -z "$(which getfattr 2>/dev/null)" ] &&
11336                 skip "no getfattr command" && return 0
11337
11338         local file1=$DIR/$tdir/$tfile-1
11339         local file2=$DIR/$tdir/$tfile-2
11340         local file3=$DIR/$tdir/$tfile-3
11341         local lovea1
11342         local lovea2
11343
11344         mkdir -p $DIR/$tdir
11345         touch $file1 || error "create $file1 failed"
11346         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11347                 error "create $file2 failed"
11348         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11349                 error "create $file3 failed"
11350         lovea1=$($LFS getstripe $file1 | sed 1d)
11351
11352         $LFS swap_layouts $file2 $file3 ||
11353                 error "swap $file2 $file3 layouts failed"
11354         $LFS swap_layouts $file1 $file2 ||
11355                 error "swap $file1 $file2 layouts failed"
11356
11357         lovea2=$($LFS getstripe $file2 | sed 1d)
11358         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11359
11360         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11361         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11362 }
11363 run_test 184d "allow stripeless layouts swap"
11364
11365 test_184e() {
11366         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
11367                 { skip "Need MDS version at least 2.6.94"; return 0; }
11368         check_swap_layouts_support && return 0
11369         [ -z "$(which getfattr 2>/dev/null)" ] &&
11370                 skip "no getfattr command" && return 0
11371
11372         local file1=$DIR/$tdir/$tfile-1
11373         local file2=$DIR/$tdir/$tfile-2
11374         local file3=$DIR/$tdir/$tfile-3
11375         local lovea
11376
11377         mkdir -p $DIR/$tdir
11378         touch $file1 || error "create $file1 failed"
11379         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11380                 error "create $file2 failed"
11381         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11382                 error "create $file3 failed"
11383
11384         $LFS swap_layouts $file1 $file2 ||
11385                 error "swap $file1 $file2 layouts failed"
11386
11387         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11388         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11389
11390         echo 123 > $file1 || error "Should be able to write into $file1"
11391
11392         $LFS swap_layouts $file1 $file3 ||
11393                 error "swap $file1 $file3 layouts failed"
11394
11395         echo 123 > $file1 || error "Should be able to write into $file1"
11396
11397         rm -rf $file1 $file2 $file3
11398 }
11399 run_test 184e "Recreate layout after stripeless layout swaps"
11400
11401 test_185() { # LU-2441
11402         # LU-3553 - no volatile file support in old servers
11403         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11404                 { skip "Need MDS version at least 2.3.60"; return 0; }
11405
11406         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11407         touch $DIR/$tdir/spoo
11408         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11409         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11410                 error "cannot create/write a volatile file"
11411         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11412                 error "FID is still valid after close"
11413
11414         multiop_bg_pause $DIR/$tdir vVw4096_c
11415         local multi_pid=$!
11416
11417         local OLD_IFS=$IFS
11418         IFS=":"
11419         local fidv=($fid)
11420         IFS=$OLD_IFS
11421         # assume that the next FID for this client is sequential, since stdout
11422         # is unfortunately eaten by multiop_bg_pause
11423         local n=$((${fidv[1]} + 1))
11424         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11425         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11426                 error "FID is missing before close"
11427         kill -USR1 $multi_pid
11428         # 1 second delay, so if mtime change we will see it
11429         sleep 1
11430         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11431         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11432 }
11433 run_test 185 "Volatile file support"
11434
11435 test_187a() {
11436         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11437         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11438                 skip "Need MDS version at least 2.3.0" && return
11439
11440         local dir0=$DIR/$tdir/$testnum
11441         mkdir -p $dir0 || error "creating dir $dir0"
11442
11443         local file=$dir0/file1
11444         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11445         local dv1=$($LFS data_version $file)
11446         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11447         local dv2=$($LFS data_version $file)
11448         [[ $dv1 != $dv2 ]] ||
11449                 error "data version did not change on write $dv1 == $dv2"
11450
11451         # clean up
11452         rm -f $file1
11453 }
11454 run_test 187a "Test data version change"
11455
11456 test_187b() {
11457         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11458         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11459                 skip "Need MDS version at least 2.3.0" && return
11460
11461         local dir0=$DIR/$tdir/$testnum
11462         mkdir -p $dir0 || error "creating dir $dir0"
11463
11464         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11465         [[ ${DV[0]} != ${DV[1]} ]] ||
11466                 error "data version did not change on write"\
11467                       " ${DV[0]} == ${DV[1]}"
11468
11469         # clean up
11470         rm -f $file1
11471 }
11472 run_test 187b "Test data version change on volatile file"
11473
11474 test_200() {
11475         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11476         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11477
11478         local POOL=${POOL:-cea1}
11479         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11480         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11481         # Pool OST targets
11482         local first_ost=0
11483         local last_ost=$(($OSTCOUNT - 1))
11484         local ost_step=2
11485         local ost_list=$(seq $first_ost $ost_step $last_ost)
11486         local ost_range="$first_ost $last_ost $ost_step"
11487         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11488         local file_dir=$POOL_ROOT/file_tst
11489         local subdir=$test_path/subdir
11490
11491         local rc=0
11492         while : ; do
11493                 # former test_200a test_200b
11494                 pool_add $POOL                          || { rc=$? ; break; }
11495                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11496                 # former test_200c test_200d
11497                 mkdir -p $test_path
11498                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11499                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11500                 mkdir -p $subdir
11501                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11502                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11503                                                         || { rc=$? ; break; }
11504                 # former test_200e test_200f
11505                 local files=$((OSTCOUNT*3))
11506                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11507                                                         || { rc=$? ; break; }
11508                 pool_create_files $POOL $file_dir $files "$ost_list" \
11509                                                         || { rc=$? ; break; }
11510                 # former test_200g test_200h
11511                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11512                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11513
11514                 # former test_201a test_201b test_201c
11515                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11516
11517                 local f=$test_path/$tfile
11518                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11519                 pool_remove $POOL $f                    || { rc=$? ; break; }
11520                 break
11521         done
11522
11523         cleanup_pools
11524         return $rc
11525 }
11526 run_test 200 "OST pools"
11527
11528 # usage: default_attr <count | size | offset>
11529 default_attr() {
11530         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11531 }
11532
11533 # usage: check_default_stripe_attr
11534 check_default_stripe_attr() {
11535         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11536         case $1 in
11537         --stripe-count|--count)
11538                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11539         --stripe-size|--size)
11540                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11541         --stripe-index|--index)
11542                 EXPECTED=-1;;
11543         *)
11544                 error "unknown getstripe attr '$1'"
11545         esac
11546
11547         [ $ACTUAL != $EXPECTED ] &&
11548                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11549 }
11550
11551 test_204a() {
11552         test_mkdir -p $DIR/$tdir
11553         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11554
11555         check_default_stripe_attr --stripe-count
11556         check_default_stripe_attr --stripe-size
11557         check_default_stripe_attr --stripe-index
11558
11559         return 0
11560 }
11561 run_test 204a "Print default stripe attributes ================="
11562
11563 test_204b() {
11564         test_mkdir -p $DIR/$tdir
11565         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11566
11567         check_default_stripe_attr --stripe-size
11568         check_default_stripe_attr --stripe-index
11569
11570         return 0
11571 }
11572 run_test 204b "Print default stripe size and offset  ==========="
11573
11574 test_204c() {
11575         test_mkdir -p $DIR/$tdir
11576         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11577
11578         check_default_stripe_attr --stripe-count
11579         check_default_stripe_attr --stripe-index
11580
11581         return 0
11582 }
11583 run_test 204c "Print default stripe count and offset ==========="
11584
11585 test_204d() {
11586         test_mkdir -p $DIR/$tdir
11587         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11588
11589         check_default_stripe_attr --stripe-count
11590         check_default_stripe_attr --stripe-size
11591
11592         return 0
11593 }
11594 run_test 204d "Print default stripe count and size ============="
11595
11596 test_204e() {
11597         test_mkdir -p $DIR/$tdir
11598         $SETSTRIPE -d $DIR/$tdir
11599
11600         check_default_stripe_attr --stripe-count --raw
11601         check_default_stripe_attr --stripe-size --raw
11602         check_default_stripe_attr --stripe-index --raw
11603
11604         return 0
11605 }
11606 run_test 204e "Print raw stripe attributes ================="
11607
11608 test_204f() {
11609         test_mkdir -p $DIR/$tdir
11610         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11611
11612         check_default_stripe_attr --stripe-size --raw
11613         check_default_stripe_attr --stripe-index --raw
11614
11615         return 0
11616 }
11617 run_test 204f "Print raw stripe size and offset  ==========="
11618
11619 test_204g() {
11620         test_mkdir -p $DIR/$tdir
11621         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11622
11623         check_default_stripe_attr --stripe-count --raw
11624         check_default_stripe_attr --stripe-index --raw
11625
11626         return 0
11627 }
11628 run_test 204g "Print raw stripe count and offset ==========="
11629
11630 test_204h() {
11631         test_mkdir -p $DIR/$tdir
11632         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11633
11634         check_default_stripe_attr --stripe-count --raw
11635         check_default_stripe_attr --stripe-size --raw
11636
11637         return 0
11638 }
11639 run_test 204h "Print raw stripe count and size ============="
11640
11641 # Figure out which job scheduler is being used, if any,
11642 # or use a fake one
11643 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11644         JOBENV=SLURM_JOB_ID
11645 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11646         JOBENV=LSB_JOBID
11647 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11648         JOBENV=PBS_JOBID
11649 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11650         JOBENV=LOADL_STEP_ID
11651 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11652         JOBENV=JOB_ID
11653 else
11654         $LCTL list_param jobid_name > /dev/null 2>&1
11655         if [ $? -eq 0 ]; then
11656                 JOBENV=nodelocal
11657         else
11658                 JOBENV=FAKE_JOBID
11659         fi
11660 fi
11661
11662 verify_jobstats() {
11663         local cmd=($1)
11664         shift
11665         local facets="$@"
11666
11667 # we don't really need to clear the stats for this test to work, since each
11668 # command has a unique jobid, but it makes debugging easier if needed.
11669 #       for facet in $facets; do
11670 #               local dev=$(convert_facet2label $facet)
11671 #               # clear old jobstats
11672 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
11673 #       done
11674
11675         # use a new JobID for each test, or we might see an old one
11676         [ "$JOBENV" = "FAKE_JOBID" ] &&
11677                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11678
11679         JOBVAL=${!JOBENV}
11680
11681         [ "$JOBENV" = "nodelocal" ] && {
11682                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11683                 $LCTL set_param jobid_name=$FAKE_JOBID
11684                 JOBVAL=$FAKE_JOBID
11685         }
11686
11687         log "Test: ${cmd[*]}"
11688         log "Using JobID environment variable $JOBENV=$JOBVAL"
11689
11690         if [ $JOBENV = "FAKE_JOBID" ]; then
11691                 FAKE_JOBID=$JOBVAL ${cmd[*]}
11692         else
11693                 ${cmd[*]}
11694         fi
11695
11696         # all files are created on OST0000
11697         for facet in $facets; do
11698                 local stats="*.$(convert_facet2label $facet).job_stats"
11699                 if [ $(do_facet $facet lctl get_param $stats |
11700                        grep -c $JOBVAL) -ne 1 ]; then
11701                         do_facet $facet lctl get_param $stats
11702                         error "No jobstats for $JOBVAL found on $facet::$stats"
11703                 fi
11704         done
11705 }
11706
11707 jobstats_set() {
11708         trap 0
11709         NEW_JOBENV=${1:-$OLD_JOBENV}
11710         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11711         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11712 }
11713
11714 cleanup_205() {
11715         do_facet $SINGLEMDS \
11716                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
11717         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11718         do_facet $SINGLEMDS lctl --device $MDT0 changelog_deregister $CL_USER
11719 }
11720
11721 test_205() { # Job stats
11722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11723         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11724         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11725         remote_ost_nodsh && skip "remote OST with nodsh" && return
11726
11727         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11728                 skip "Server doesn't support jobstats" && return 0
11729         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11730
11731         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11732         if [ $OLD_JOBENV != $JOBENV ]; then
11733                 jobstats_set $JOBENV
11734                 trap cleanup_205 EXIT
11735         fi
11736
11737         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
11738         echo "Registered as changelog user $CL_USER"
11739
11740         OLD_INTERVAL=$(do_facet $SINGLEMDS \
11741                        lctl get_param -n mdt.*.job_cleanup_interval)
11742         local interval_new=5
11743         do_facet $SINGLEMDS \
11744                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
11745         local start=$SECONDS
11746
11747         local cmd
11748         # mkdir
11749         cmd="mkdir $DIR/$tdir"
11750         verify_jobstats "$cmd" "$SINGLEMDS"
11751         # rmdir
11752         cmd="rmdir $DIR/$tdir"
11753         verify_jobstats "$cmd" "$SINGLEMDS"
11754         # mkdir on secondary MDT
11755         if [ $MDSCOUNT -gt 1 ]; then
11756                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
11757                 verify_jobstats "$cmd" "mds2"
11758         fi
11759         # mknod
11760         cmd="mknod $DIR/$tfile c 1 3"
11761         verify_jobstats "$cmd" "$SINGLEMDS"
11762         # unlink
11763         cmd="rm -f $DIR/$tfile"
11764         verify_jobstats "$cmd" "$SINGLEMDS"
11765         # create all files on OST0000 so verify_jobstats can find OST stats
11766         # open & close
11767         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11768         verify_jobstats "$cmd" "$SINGLEMDS"
11769         # setattr
11770         cmd="touch $DIR/$tfile"
11771         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11772         # write
11773         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11774         verify_jobstats "$cmd" "ost1"
11775         # read
11776         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11777         verify_jobstats "$cmd" "ost1"
11778         # truncate
11779         cmd="$TRUNCATE $DIR/$tfile 0"
11780         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11781         # rename
11782         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
11783         verify_jobstats "$cmd" "$SINGLEMDS"
11784         # jobstats expiry - sleep until old stats should be expired
11785         local left=$((interval_new + 2 - (SECONDS - start)))
11786         [ $left -ge 0 ] && echo "sleep $left for expiry" && sleep $((left + 1))
11787         cmd="mkdir $DIR/$tdir.expire"
11788         verify_jobstats "$cmd" "$SINGLEMDS"
11789         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
11790             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
11791
11792         # Ensure that jobid are present in changelog (if supported by MDS)
11793         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11794         then
11795                 $LFS changelog $MDT0 | tail -9
11796                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11797                 [ $jobids -eq 9 ] ||
11798                         error "Wrong changelog jobid count $jobids != 9"
11799
11800                 # LU-5862
11801                 JOBENV="disable"
11802                 jobstats_set $JOBENV
11803                 touch $DIR/$tfile
11804                 $LFS changelog $MDT0 | tail -1
11805                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11806                 [ $jobids -eq 0 ] ||
11807                         error "Unexpected jobids when jobid_var=$JOBENV"
11808         fi
11809
11810         cleanup_205
11811 }
11812 run_test 205 "Verify job stats"
11813
11814 # LU-1480, LU-1773 and LU-1657
11815 test_206() {
11816         mkdir -p $DIR/$tdir
11817         $SETSTRIPE -c -1 $DIR/$tdir
11818 #define OBD_FAIL_LOV_INIT 0x1403
11819         $LCTL set_param fail_loc=0xa0001403
11820         $LCTL set_param fail_val=1
11821         touch $DIR/$tdir/$tfile || true
11822 }
11823 run_test 206 "fail lov_init_raid0() doesn't lbug"
11824
11825 test_207a() {
11826         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11827         local fsz=`stat -c %s $DIR/$tfile`
11828         cancel_lru_locks mdc
11829
11830         # do not return layout in getattr intent
11831 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11832         $LCTL set_param fail_loc=0x170
11833         local sz=`stat -c %s $DIR/$tfile`
11834
11835         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11836
11837         rm -rf $DIR/$tfile
11838 }
11839 run_test 207a "can refresh layout at glimpse"
11840
11841 test_207b() {
11842         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11843         local cksum=`md5sum $DIR/$tfile`
11844         local fsz=`stat -c %s $DIR/$tfile`
11845         cancel_lru_locks mdc
11846         cancel_lru_locks osc
11847
11848         # do not return layout in getattr intent
11849 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11850         $LCTL set_param fail_loc=0x171
11851
11852         # it will refresh layout after the file is opened but before read issues
11853         echo checksum is "$cksum"
11854         echo "$cksum" |md5sum -c --quiet || error "file differs"
11855
11856         rm -rf $DIR/$tfile
11857 }
11858 run_test 207b "can refresh layout at open"
11859
11860 test_208() {
11861         # FIXME: in this test suite, only RD lease is used. This is okay
11862         # for now as only exclusive open is supported. After generic lease
11863         # is done, this test suite should be revised. - Jinshan
11864
11865         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11866         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11867                 { skip "Need MDS version at least 2.4.52"; return 0; }
11868
11869         echo "==== test 1: verify get lease work"
11870         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11871
11872         echo "==== test 2: verify lease can be broken by upcoming open"
11873         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11874         local PID=$!
11875         sleep 1
11876
11877         $MULTIOP $DIR/$tfile oO_RDONLY:c
11878         kill -USR1 $PID && wait $PID || error "break lease error"
11879
11880         echo "==== test 3: verify lease can't be granted if an open already exists"
11881         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11882         local PID=$!
11883         sleep 1
11884
11885         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11886         kill -USR1 $PID && wait $PID || error "open file error"
11887
11888         echo "==== test 4: lease can sustain over recovery"
11889         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11890         PID=$!
11891         sleep 1
11892
11893         fail mds1
11894
11895         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11896
11897         echo "==== test 5: lease broken can't be regained by replay"
11898         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11899         PID=$!
11900         sleep 1
11901
11902         # open file to break lease and then recovery
11903         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11904         fail mds1
11905
11906         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11907
11908         rm -f $DIR/$tfile
11909 }
11910 run_test 208 "Exclusive open"
11911
11912 test_209() {
11913         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11914                 skip_env "must have disp_stripe" && return
11915
11916         touch $DIR/$tfile
11917         sync; sleep 5; sync;
11918
11919         echo 3 > /proc/sys/vm/drop_caches
11920         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11921
11922         # open/close 500 times
11923         for i in $(seq 500); do
11924                 cat $DIR/$tfile
11925         done
11926
11927         echo 3 > /proc/sys/vm/drop_caches
11928         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11929
11930         echo "before: $req_before, after: $req_after"
11931         [ $((req_after - req_before)) -ge 300 ] &&
11932                 error "open/close requests are not freed"
11933         return 0
11934 }
11935 run_test 209 "read-only open/close requests should be freed promptly"
11936
11937 test_212() {
11938         size=`date +%s`
11939         size=$((size % 8192 + 1))
11940         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11941         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11942         rm -f $DIR/f212 $DIR/f212.xyz
11943 }
11944 run_test 212 "Sendfile test ============================================"
11945
11946 test_213() {
11947         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11948         cancel_lru_locks osc
11949         lctl set_param fail_loc=0x8000040f
11950         # generate a read lock
11951         cat $DIR/$tfile > /dev/null
11952         # write to the file, it will try to cancel the above read lock.
11953         cat /etc/hosts >> $DIR/$tfile
11954 }
11955 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11956
11957 test_214() { # for bug 20133
11958         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11959         for (( i=0; i < 340; i++ )) ; do
11960                 touch $DIR/$tdir/d214c/a$i
11961         done
11962
11963         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11964         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11965         ls $DIR/d214c || error "ls $DIR/d214c failed"
11966         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11967         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11968 }
11969 run_test 214 "hash-indexed directory test - bug 20133"
11970
11971 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11972 create_lnet_proc_files() {
11973         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11974         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11975
11976         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11977         rm -f "$TMP/lnet_$1.sys_tmp"
11978 }
11979
11980 # counterpart of create_lnet_proc_files
11981 remove_lnet_proc_files() {
11982         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11983 }
11984
11985 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11986 # 3rd arg as regexp for body
11987 check_lnet_proc_stats() {
11988         local l=$(cat "$TMP/lnet_$1" |wc -l)
11989         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11990
11991         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11992 }
11993
11994 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11995 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11996 # optional and can be regexp for 2nd line (lnet.routes case)
11997 check_lnet_proc_entry() {
11998         local blp=2          # blp stands for 'position of 1st line of body'
11999         [ -z "$5" ] || blp=3 # lnet.routes case
12000
12001         local l=$(cat "$TMP/lnet_$1" |wc -l)
12002         # subtracting one from $blp because the body can be empty
12003         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12004
12005         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12006                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12007
12008         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12009                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12010
12011         # bail out if any unexpected line happened
12012         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12013         [ "$?" != 0 ] || error "$2 misformatted"
12014 }
12015
12016 test_215() { # for bugs 18102, 21079, 21517
12017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12018         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12019         local P='[1-9][0-9]*'           # positive numeric
12020         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12021         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12022         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12023         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12024
12025         local L1 # regexp for 1st line
12026         local L2 # regexp for 2nd line (optional)
12027         local BR # regexp for the rest (body)
12028
12029         # lnet.stats should look as 11 space-separated non-negative numerics
12030         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12031         create_lnet_proc_files "stats"
12032         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12033         remove_lnet_proc_files "stats"
12034
12035         # lnet.routes should look like this:
12036         # Routing disabled/enabled
12037         # net hops priority state router
12038         # where net is a string like tcp0, hops > 0, priority >= 0,
12039         # state is up/down,
12040         # router is a string like 192.168.1.1@tcp2
12041         L1="^Routing (disabled|enabled)$"
12042         L2="^net +hops +priority +state +router$"
12043         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12044         create_lnet_proc_files "routes"
12045         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12046         remove_lnet_proc_files "routes"
12047
12048         # lnet.routers should look like this:
12049         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12050         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12051         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12052         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12053         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12054         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12055         create_lnet_proc_files "routers"
12056         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12057         remove_lnet_proc_files "routers"
12058
12059         # lnet.peers should look like this:
12060         # nid refs state last max rtr min tx min queue
12061         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12062         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12063         # numeric (0 or >0 or <0), queue >= 0.
12064         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12065         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12066         create_lnet_proc_files "peers"
12067         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12068         remove_lnet_proc_files "peers"
12069
12070         # lnet.buffers  should look like this:
12071         # pages count credits min
12072         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12073         L1="^pages +count +credits +min$"
12074         BR="^ +$N +$N +$I +$I$"
12075         create_lnet_proc_files "buffers"
12076         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12077         remove_lnet_proc_files "buffers"
12078
12079         # lnet.nis should look like this:
12080         # nid status alive refs peer rtr max tx min
12081         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12082         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12083         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12084         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12085         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12086         create_lnet_proc_files "nis"
12087         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12088         remove_lnet_proc_files "nis"
12089
12090         # can we successfully write to lnet.stats?
12091         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12092         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12093 }
12094 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12095
12096 test_216() { # bug 20317
12097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12098         remote_ost_nodsh && skip "remote OST with nodsh" && return
12099
12100         local node
12101         local facets=$(get_facets OST)
12102         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12103
12104         save_lustre_params client "osc.*.contention_seconds" > $p
12105         save_lustre_params $facets \
12106                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12107         save_lustre_params $facets \
12108                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12109         save_lustre_params $facets \
12110                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12111         clear_osc_stats
12112
12113         # agressive lockless i/o settings
12114         for node in $(osts_nodes); do
12115                 do_node $node 'lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes 2000000; lctl set_param -n ldlm.namespaces.filter-*.contended_locks 0; lctl set_param -n ldlm.namespaces.filter-*.contention_seconds 60'
12116         done
12117         lctl set_param -n osc.*.contention_seconds 60
12118
12119         $DIRECTIO write $DIR/$tfile 0 10 4096
12120         $CHECKSTAT -s 40960 $DIR/$tfile
12121
12122         # disable lockless i/o
12123         for node in $(osts_nodes); do
12124                 do_node $node 'lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes 0; lctl set_param -n ldlm.namespaces.filter-*.contended_locks 32; lctl set_param -n ldlm.namespaces.filter-*.contention_seconds 0'
12125         done
12126         lctl set_param -n osc.*.contention_seconds 0
12127         clear_osc_stats
12128
12129         dd if=/dev/zero of=$DIR/$tfile count=0
12130         $CHECKSTAT -s 0 $DIR/$tfile
12131
12132         restore_lustre_params <$p
12133         rm -f $p
12134         rm $DIR/$tfile
12135 }
12136 run_test 216 "check lockless direct write works and updates file size and kms correctly"
12137
12138 test_217() { # bug 22430
12139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12140         local node
12141         local nid
12142
12143         for node in $(nodes_list); do
12144                 nid=$(host_nids_address $node $NETTYPE)
12145                 if [[ $nid = *-* ]] ; then
12146                         echo "lctl ping $nid@$NETTYPE"
12147                         lctl ping $nid@$NETTYPE
12148                 else
12149                         echo "skipping $node (no hyphen detected)"
12150                 fi
12151         done
12152 }
12153 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12154
12155 test_218() {
12156        # do directio so as not to populate the page cache
12157        log "creating a 10 Mb file"
12158        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12159        log "starting reads"
12160        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12161        log "truncating the file"
12162        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12163        log "killing dd"
12164        kill %+ || true # reads might have finished
12165        echo "wait until dd is finished"
12166        wait
12167        log "removing the temporary file"
12168        rm -rf $DIR/$tfile || error "tmp file removal failed"
12169 }
12170 run_test 218 "parallel read and truncate should not deadlock ======================="
12171
12172 test_219() {
12173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12174         # write one partial page
12175         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12176         # set no grant so vvp_io_commit_write will do sync write
12177         $LCTL set_param fail_loc=0x411
12178         # write a full page at the end of file
12179         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12180
12181         $LCTL set_param fail_loc=0
12182         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12183         $LCTL set_param fail_loc=0x411
12184         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12185
12186         # LU-4201
12187         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12188         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12189 }
12190 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
12191
12192 test_220() { #LU-325
12193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12194         remote_ost_nodsh && skip "remote OST with nodsh" && return
12195         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12196         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12197         local OSTIDX=0
12198
12199         # create on MDT0000 so the last_id and next_id are correct
12200         mkdir $DIR/$tdir
12201         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
12202         OST=${OST%_UUID}
12203
12204         # on the mdt's osc
12205         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
12206         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
12207                         osc.$mdtosc_proc1.prealloc_last_id)
12208         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
12209                         osc.$mdtosc_proc1.prealloc_next_id)
12210
12211         $LFS df -i
12212
12213         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
12214         #define OBD_FAIL_OST_ENOINO              0x229
12215         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
12216         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
12217         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
12218
12219         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
12220
12221         MDSOBJS=$((last_id - next_id))
12222         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
12223
12224         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
12225         echo "OST still has $count kbytes free"
12226
12227         echo "create $MDSOBJS files @next_id..."
12228         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
12229
12230         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12231                         osc.$mdtosc_proc1.prealloc_last_id)
12232         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12233                         osc.$mdtosc_proc1.prealloc_next_id)
12234
12235         echo "after creation, last_id=$last_id2, next_id=$next_id2"
12236         $LFS df -i
12237
12238         echo "cleanup..."
12239
12240         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12241         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12242
12243         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12244         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12245         echo "unlink $MDSOBJS files @$next_id..."
12246         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12247 }
12248 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12249
12250 test_221() {
12251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12252         dd if=`which date` of=$MOUNT/date oflag=sync
12253         chmod +x $MOUNT/date
12254
12255         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12256         $LCTL set_param fail_loc=0x80001401
12257
12258         $MOUNT/date > /dev/null
12259         rm -f $MOUNT/date
12260 }
12261 run_test 221 "make sure fault and truncate race to not cause OOM"
12262
12263 test_222a () {
12264         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12265        rm -rf $DIR/$tdir
12266        test_mkdir -p $DIR/$tdir
12267        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12268        createmany -o $DIR/$tdir/$tfile 10
12269        cancel_lru_locks mdc
12270        cancel_lru_locks osc
12271        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12272        $LCTL set_param fail_loc=0x31a
12273        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12274        $LCTL set_param fail_loc=0
12275        rm -r $DIR/$tdir
12276 }
12277 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12278
12279 test_222b () {
12280         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12281        rm -rf $DIR/$tdir
12282        test_mkdir -p $DIR/$tdir
12283        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12284        createmany -o $DIR/$tdir/$tfile 10
12285        cancel_lru_locks mdc
12286        cancel_lru_locks osc
12287        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12288        $LCTL set_param fail_loc=0x31a
12289        rm -r $DIR/$tdir || "AGL for rmdir failed"
12290        $LCTL set_param fail_loc=0
12291 }
12292 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12293
12294 test_223 () {
12295         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12296        rm -rf $DIR/$tdir
12297        test_mkdir -p $DIR/$tdir
12298        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12299        createmany -o $DIR/$tdir/$tfile 10
12300        cancel_lru_locks mdc
12301        cancel_lru_locks osc
12302        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12303        $LCTL set_param fail_loc=0x31b
12304        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12305        $LCTL set_param fail_loc=0
12306        rm -r $DIR/$tdir
12307 }
12308 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12309
12310 test_224a() { # LU-1039, MRP-303
12311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12312         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12313         $LCTL set_param fail_loc=0x508
12314         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12315         $LCTL set_param fail_loc=0
12316         df $DIR
12317 }
12318 run_test 224a "Don't panic on bulk IO failure"
12319
12320 test_224b() { # LU-1039, MRP-303
12321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12322         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12323         cancel_lru_locks osc
12324         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12325         $LCTL set_param fail_loc=0x515
12326         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12327         $LCTL set_param fail_loc=0
12328         df $DIR
12329 }
12330 run_test 224b "Don't panic on bulk IO failure"
12331
12332 test_224c() { # LU-6441
12333         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12334         local pages_per_rpc=$($LCTL get_param \
12335                                 osc.*.max_pages_per_rpc)
12336         local at_max=$($LCTL get_param -n at_max)
12337         local timeout=$($LCTL get_param -n timeout)
12338         local test_at="$LCTL get_param -n at_max"
12339         local param_at="$FSNAME.sys.at_max"
12340         local test_timeout="$LCTL get_param -n timeout"
12341         local param_timeout="$FSNAME.sys.timeout"
12342
12343         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
12344
12345         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
12346                 error "conf_param at_max=0 failed"
12347         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
12348                 error "conf_param timeout=5 failed"
12349
12350         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
12351         $LCTL set_param fail_loc=0x520
12352         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
12353         sync
12354         $LCTL set_param fail_loc=0
12355
12356         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
12357                 error "conf_param at_max=$at_max failed"
12358         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
12359                 $timeout || error "conf_param timeout=$timeout failed"
12360
12361         $LCTL set_param -n $pages_per_rpc
12362 }
12363 run_test 224c "Don't hang if one of md lost during large bulk RPC"
12364
12365 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12366 test_225a () {
12367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12368         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12369         if [ -z ${MDSSURVEY} ]; then
12370               skip_env "mds-survey not found" && return
12371         fi
12372
12373         [ $MDSCOUNT -ge 2 ] &&
12374                 skip "skipping now for more than one MDT" && return
12375
12376        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12377             { skip "Need MDS version at least 2.2.51"; return; }
12378
12379        local mds=$(facet_host $SINGLEMDS)
12380        local target=$(do_nodes $mds 'lctl dl' | \
12381                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12382
12383        local cmd1="file_count=1000 thrhi=4"
12384        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12385        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12386        local cmd="$cmd1 $cmd2 $cmd3"
12387
12388        rm -f ${TMP}/mds_survey*
12389        echo + $cmd
12390        eval $cmd || error "mds-survey with zero-stripe failed"
12391        cat ${TMP}/mds_survey*
12392        rm -f ${TMP}/mds_survey*
12393 }
12394 run_test 225a "Metadata survey sanity with zero-stripe"
12395
12396 test_225b () {
12397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12398         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12399         if [ -z ${MDSSURVEY} ]; then
12400               skip_env "mds-survey not found" && return
12401         fi
12402         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12403             { skip "Need MDS version at least 2.2.51"; return; }
12404
12405         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12406               skip_env "Need to mount OST to test" && return
12407         fi
12408
12409         [ $MDSCOUNT -ge 2 ] &&
12410                 skip "skipping now for more than one MDT" && return
12411        local mds=$(facet_host $SINGLEMDS)
12412        local target=$(do_nodes $mds 'lctl dl' | \
12413                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12414
12415        local cmd1="file_count=1000 thrhi=4"
12416        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12417        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12418        local cmd="$cmd1 $cmd2 $cmd3"
12419
12420        rm -f ${TMP}/mds_survey*
12421        echo + $cmd
12422        eval $cmd || error "mds-survey with stripe_count failed"
12423        cat ${TMP}/mds_survey*
12424        rm -f ${TMP}/mds_survey*
12425 }
12426 run_test 225b "Metadata survey sanity with stripe_count = 1"
12427
12428 mcreate_path2fid () {
12429         local mode=$1
12430         local major=$2
12431         local minor=$3
12432         local name=$4
12433         local desc=$5
12434         local path=$DIR/$tdir/$name
12435         local fid
12436         local rc
12437         local fid_path
12438
12439         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12440                 error "cannot create $desc"
12441
12442         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12443         rc=$?
12444         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12445
12446         fid_path=$($LFS fid2path $MOUNT $fid)
12447         rc=$?
12448         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12449
12450         [ "$path" == "$fid_path" ] ||
12451                 error "fid2path returned $fid_path, expected $path"
12452
12453         echo "pass with $path and $fid"
12454 }
12455
12456 test_226a () {
12457         rm -rf $DIR/$tdir
12458         mkdir -p $DIR/$tdir
12459
12460         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12461         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12462         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12463         mcreate_path2fid 0040666 0 0 dir "directory"
12464         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12465         mcreate_path2fid 0100666 0 0 file "regular file"
12466         mcreate_path2fid 0120666 0 0 link "symbolic link"
12467         mcreate_path2fid 0140666 0 0 sock "socket"
12468 }
12469 run_test 226a "call path2fid and fid2path on files of all type"
12470
12471 test_226b () {
12472         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12473         rm -rf $DIR/$tdir
12474         local MDTIDX=1
12475
12476         mkdir -p $DIR/$tdir
12477         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12478                 error "create remote directory failed"
12479         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12480         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12481                                 "character special file (null)"
12482         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12483                                 "character special file (no device)"
12484         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12485         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12486                                 "block special file (loop)"
12487         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12488         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12489         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12490 }
12491 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12492
12493 # LU-1299 Executing or running ldd on a truncated executable does not
12494 # cause an out-of-memory condition.
12495 test_227() {
12496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12497         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12498         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12499         chmod +x $MOUNT/date
12500
12501         $MOUNT/date > /dev/null
12502         ldd $MOUNT/date > /dev/null
12503         rm -f $MOUNT/date
12504 }
12505 run_test 227 "running truncated executable does not cause OOM"
12506
12507 # LU-1512 try to reuse idle OI blocks
12508 test_228a() {
12509         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12510         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12511         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12512                 skip "non-ldiskfs backend" && return
12513
12514         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12515         local myDIR=$DIR/$tdir
12516
12517         mkdir -p $myDIR
12518         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12519         $LCTL set_param fail_loc=0x80001002
12520         createmany -o $myDIR/t- 10000
12521         $LCTL set_param fail_loc=0
12522         # The guard is current the largest FID holder
12523         touch $myDIR/guard
12524         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12525                     tr -d '[')
12526         local IDX=$(($SEQ % 64))
12527
12528         do_facet $SINGLEMDS sync
12529         # Make sure journal flushed.
12530         sleep 6
12531         local blk1=$(do_facet $SINGLEMDS \
12532                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12533                      grep Blockcount | awk '{print $4}')
12534
12535         # Remove old files, some OI blocks will become idle.
12536         unlinkmany $myDIR/t- 10000
12537         # Create new files, idle OI blocks should be reused.
12538         createmany -o $myDIR/t- 2000
12539         do_facet $SINGLEMDS sync
12540         # Make sure journal flushed.
12541         sleep 6
12542         local blk2=$(do_facet $SINGLEMDS \
12543                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12544                      grep Blockcount | awk '{print $4}')
12545
12546         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12547 }
12548 run_test 228a "try to reuse idle OI blocks"
12549
12550 test_228b() {
12551         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12552         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12553         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12554                 skip "non-ldiskfs backend" && return
12555
12556         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12557         local myDIR=$DIR/$tdir
12558
12559         mkdir -p $myDIR
12560         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12561         $LCTL set_param fail_loc=0x80001002
12562         createmany -o $myDIR/t- 10000
12563         $LCTL set_param fail_loc=0
12564         # The guard is current the largest FID holder
12565         touch $myDIR/guard
12566         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12567                     tr -d '[')
12568         local IDX=$(($SEQ % 64))
12569
12570         do_facet $SINGLEMDS sync
12571         # Make sure journal flushed.
12572         sleep 6
12573         local blk1=$(do_facet $SINGLEMDS \
12574                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12575                      grep Blockcount | awk '{print $4}')
12576
12577         # Remove old files, some OI blocks will become idle.
12578         unlinkmany $myDIR/t- 10000
12579
12580         # stop the MDT
12581         stop $SINGLEMDS || error "Fail to stop MDT."
12582         # remount the MDT
12583         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12584
12585         df $MOUNT || error "Fail to df."
12586         # Create new files, idle OI blocks should be reused.
12587         createmany -o $myDIR/t- 2000
12588         do_facet $SINGLEMDS sync
12589         # Make sure journal flushed.
12590         sleep 6
12591         local blk2=$(do_facet $SINGLEMDS \
12592                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12593                      grep Blockcount | awk '{print $4}')
12594
12595         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12596 }
12597 run_test 228b "idle OI blocks can be reused after MDT restart"
12598
12599 #LU-1881
12600 test_228c() {
12601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12602         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12603         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12604                 skip "non-ldiskfs backend" && return
12605
12606         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12607         local myDIR=$DIR/$tdir
12608
12609         mkdir -p $myDIR
12610         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12611         $LCTL set_param fail_loc=0x80001002
12612         # 20000 files can guarantee there are index nodes in the OI file
12613         createmany -o $myDIR/t- 20000
12614         $LCTL set_param fail_loc=0
12615         # The guard is current the largest FID holder
12616         touch $myDIR/guard
12617         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12618                     tr -d '[')
12619         local IDX=$(($SEQ % 64))
12620
12621         do_facet $SINGLEMDS sync
12622         # Make sure journal flushed.
12623         sleep 6
12624         local blk1=$(do_facet $SINGLEMDS \
12625                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12626                      grep Blockcount | awk '{print $4}')
12627
12628         # Remove old files, some OI blocks will become idle.
12629         unlinkmany $myDIR/t- 20000
12630         rm -f $myDIR/guard
12631         # The OI file should become empty now
12632
12633         # Create new files, idle OI blocks should be reused.
12634         createmany -o $myDIR/t- 2000
12635         do_facet $SINGLEMDS sync
12636         # Make sure journal flushed.
12637         sleep 6
12638         local blk2=$(do_facet $SINGLEMDS \
12639                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12640                      grep Blockcount | awk '{print $4}')
12641
12642         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12643 }
12644 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12645
12646 test_229() { # LU-2482, LU-3448
12647         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12648                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
12649                 return
12650         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12651         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12652
12653         rm -f $DIR/$tfile
12654
12655         # Create a file with a released layout and stripe count 2.
12656         $MULTIOP $DIR/$tfile H2c ||
12657                 error "failed to create file with released layout"
12658
12659         $GETSTRIPE -v $DIR/$tfile
12660
12661         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12662         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12663
12664         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12665         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12666         stat $DIR/$tfile || error "failed to stat released file"
12667
12668         chown $RUNAS_ID $DIR/$tfile ||
12669                 error "chown $RUNAS_ID $DIR/$tfile failed"
12670
12671         chgrp $RUNAS_ID $DIR/$tfile ||
12672                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12673
12674         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12675         rm $DIR/$tfile || error "failed to remove released file"
12676 }
12677 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12678
12679 test_230a() {
12680         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12681         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12682         local MDTIDX=1
12683
12684         test_mkdir $DIR/$tdir
12685         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
12686         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12687         [ $mdt_idx -ne 0 ] &&
12688                 error "create local directory on wrong MDT $mdt_idx"
12689
12690         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12691                         error "create remote directory failed"
12692         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12693         [ $mdt_idx -ne $MDTIDX ] &&
12694                 error "create remote directory on wrong MDT $mdt_idx"
12695
12696         createmany -o $DIR/$tdir/test_230/t- 10 ||
12697                 error "create files on remote directory failed"
12698         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12699         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12700         rm -r $DIR/$tdir || error "unlink remote directory failed"
12701 }
12702 run_test 230a "Create remote directory and files under the remote directory"
12703
12704 test_230b() {
12705         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12706         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12707         local MDTIDX=1
12708         local mdt_index
12709         local i
12710         local file
12711         local pid
12712         local stripe_count
12713         local migrate_dir=$DIR/$tdir/migrate_dir
12714         local other_dir=$DIR/$tdir/other_dir
12715
12716         test_mkdir $DIR/$tdir
12717         test_mkdir -i0 -c1 $migrate_dir
12718         test_mkdir -i0 -c1 $other_dir
12719         for ((i=0; i<10; i++)); do
12720                 mkdir -p $migrate_dir/dir_${i}
12721                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12722                         error "create files under remote dir failed $i"
12723         done
12724
12725         cp /etc/passwd $migrate_dir/$tfile
12726         cp /etc/passwd $other_dir/$tfile
12727         chattr +SAD $migrate_dir
12728         chattr +SAD $migrate_dir/$tfile
12729
12730         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12731         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12732         local old_dir_mode=$(stat -c%f $migrate_dir)
12733         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12734
12735         mkdir -p $migrate_dir/dir_default_stripe2
12736         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12737         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12738
12739         mkdir -p $other_dir
12740         ln $migrate_dir/$tfile $other_dir/luna
12741         ln $migrate_dir/$tfile $migrate_dir/sofia
12742         ln $other_dir/$tfile $migrate_dir/david
12743         ln -s $migrate_dir/$tfile $other_dir/zachary
12744         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12745         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12746
12747         $LFS migrate -m $MDTIDX $migrate_dir ||
12748                 error "fails on migrating remote dir to MDT1"
12749
12750         echo "migratate to MDT1, then checking.."
12751         for ((i = 0; i < 10; i++)); do
12752                 for file in $(find $migrate_dir/dir_${i}); do
12753                         mdt_index=$($LFS getstripe -M $file)
12754                         [ $mdt_index == $MDTIDX ] ||
12755                                 error "$file is not on MDT${MDTIDX}"
12756                 done
12757         done
12758
12759         # the multiple link file should still in MDT0
12760         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12761         [ $mdt_index == 0 ] ||
12762                 error "$file is not on MDT${MDTIDX}"
12763
12764         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12765         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12766                 error " expect $old_dir_flag get $new_dir_flag"
12767
12768         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12769         [ "$old_file_flag" = "$new_file_flag" ] ||
12770                 error " expect $old_file_flag get $new_file_flag"
12771
12772         local new_dir_mode=$(stat -c%f $migrate_dir)
12773         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12774                 error "expect mode $old_dir_mode get $new_dir_mode"
12775
12776         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12777         [ "$old_file_mode" = "$new_file_mode" ] ||
12778                 error "expect mode $old_file_mode get $new_file_mode"
12779
12780         diff /etc/passwd $migrate_dir/$tfile ||
12781                 error "$tfile different after migration"
12782
12783         diff /etc/passwd $other_dir/luna ||
12784                 error "luna different after migration"
12785
12786         diff /etc/passwd $migrate_dir/sofia ||
12787                 error "sofia different after migration"
12788
12789         diff /etc/passwd $migrate_dir/david ||
12790                 error "david different after migration"
12791
12792         diff /etc/passwd $other_dir/zachary ||
12793                 error "zachary different after migration"
12794
12795         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12796                 error "${tfile}_ln different after migration"
12797
12798         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12799                 error "${tfile}_ln_other different after migration"
12800
12801         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12802         [ $stripe_count = 2 ] ||
12803                 error "dir strpe_count $d != 2 after migration."
12804
12805         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12806         [ $stripe_count = 2 ] ||
12807                 error "file strpe_count $d != 2 after migration."
12808
12809         #migrate back to MDT0
12810         MDTIDX=0
12811
12812         $LFS migrate -m $MDTIDX $migrate_dir ||
12813                 error "fails on migrating remote dir to MDT0"
12814
12815         echo "migrate back to MDT0, checking.."
12816         for file in $(find $migrate_dir); do
12817                 mdt_index=$($LFS getstripe -M $file)
12818                 [ $mdt_index == $MDTIDX ] ||
12819                         error "$file is not on MDT${MDTIDX}"
12820         done
12821
12822         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12823         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12824                 error " expect $old_dir_flag get $new_dir_flag"
12825
12826         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12827         [ "$old_file_flag" = "$new_file_flag" ] ||
12828                 error " expect $old_file_flag get $new_file_flag"
12829
12830         local new_dir_mode=$(stat -c%f $migrate_dir)
12831         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12832                 error "expect mode $old_dir_mode get $new_dir_mode"
12833
12834         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12835         [ "$old_file_mode" = "$new_file_mode" ] ||
12836                 error "expect mode $old_file_mode get $new_file_mode"
12837
12838         diff /etc/passwd ${migrate_dir}/$tfile ||
12839                 error "$tfile different after migration"
12840
12841         diff /etc/passwd ${other_dir}/luna ||
12842                 error "luna different after migration"
12843
12844         diff /etc/passwd ${migrate_dir}/sofia ||
12845                 error "sofia different after migration"
12846
12847         diff /etc/passwd ${other_dir}/zachary ||
12848                 error "zachary different after migration"
12849
12850         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12851                 error "${tfile}_ln different after migration"
12852
12853         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12854                 error "${tfile}_ln_other different after migration"
12855
12856         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12857         [ $stripe_count = 2 ] ||
12858                 error "dir strpe_count $d != 2 after migration."
12859
12860         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12861         [ $stripe_count = 2 ] ||
12862                 error "file strpe_count $d != 2 after migration."
12863
12864         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12865 }
12866 run_test 230b "migrate directory"
12867
12868 test_230c() {
12869         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12870         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12871         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12872         local MDTIDX=1
12873         local mdt_index
12874         local file
12875         local migrate_dir=$DIR/$tdir/migrate_dir
12876
12877         #If migrating directory fails in the middle, all entries of
12878         #the directory is still accessiable.
12879         test_mkdir $DIR/$tdir
12880         test_mkdir -i0 -c1 $migrate_dir
12881         stat $migrate_dir
12882         createmany -o $migrate_dir/f 10 ||
12883                 error "create files under ${migrate_dir} failed"
12884
12885         #failed after migrating 5 entries
12886         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12887         do_facet mds1 lctl set_param fail_loc=0x20001801
12888         do_facet mds1 lctl  set_param fail_val=5
12889         local t=$(ls $migrate_dir | wc -l)
12890         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
12891                 error "migrate should fail after 5 entries"
12892
12893         mkdir $migrate_dir/dir &&
12894                 error "mkdir succeeds under migrating directory"
12895         touch $migrate_dir/file &&
12896                 error "touch file succeeds under migrating directory"
12897
12898         local u=$(ls $migrate_dir | wc -l)
12899         [ "$u" == "$t" ] || error "$u != $t during migration"
12900
12901         for file in $(find $migrate_dir); do
12902                 stat $file || error "stat $file failed"
12903         done
12904
12905         do_facet mds1 lctl set_param fail_loc=0
12906         do_facet mds1 lctl set_param fail_val=0
12907
12908         $LFS migrate -m $MDTIDX $migrate_dir ||
12909                 error "migrate open files should failed with open files"
12910
12911         echo "Finish migration, then checking.."
12912         for file in $(find $migrate_dir); do
12913                 mdt_index=$($LFS getstripe -M $file)
12914                 [ $mdt_index == $MDTIDX ] ||
12915                         error "$file is not on MDT${MDTIDX}"
12916         done
12917
12918         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12919 }
12920 run_test 230c "check directory accessiblity if migration is failed"
12921
12922 test_230d() {
12923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12924         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12925         local MDTIDX=1
12926         local mdt_index
12927         local migrate_dir=$DIR/$tdir/migrate_dir
12928         local i
12929         local j
12930
12931         test_mkdir $DIR/$tdir
12932         test_mkdir -i0 -c1 $migrate_dir
12933
12934         for ((i=0; i<100; i++)); do
12935                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
12936                 createmany -o $migrate_dir/dir_${i}/f 100 ||
12937                         error "create files under remote dir failed $i"
12938         done
12939
12940         $LFS migrate -m $MDTIDX $migrate_dir ||
12941                 error "migrate remote dir error"
12942
12943         echo "Finish migration, then checking.."
12944         for file in $(find $migrate_dir); do
12945                 mdt_index=$($LFS getstripe -M $file)
12946                 [ $mdt_index == $MDTIDX ] ||
12947                         error "$file is not on MDT${MDTIDX}"
12948         done
12949
12950         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12951 }
12952 run_test 230d "check migrate big directory"
12953
12954 test_230e() {
12955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12956         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12957         local i
12958         local j
12959         local a_fid
12960         local b_fid
12961
12962         mkdir -p $DIR/$tdir
12963         mkdir $DIR/$tdir/migrate_dir
12964         mkdir $DIR/$tdir/other_dir
12965         touch $DIR/$tdir/migrate_dir/a
12966         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
12967         ls $DIR/$tdir/other_dir
12968
12969         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
12970                 error "migrate dir fails"
12971
12972         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
12973         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
12974
12975         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12976         [ $mdt_index == 0 ] || error "a is not on MDT0"
12977
12978         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
12979                 error "migrate dir fails"
12980
12981         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
12982         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
12983
12984         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12985         [ $mdt_index == 1 ] || error "a is not on MDT1"
12986
12987         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
12988         [ $mdt_index == 1 ] || error "b is not on MDT1"
12989
12990         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
12991         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
12992
12993         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
12994
12995         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12996 }
12997 run_test 230e "migrate mulitple local link files"
12998
12999 test_230f() {
13000         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13001         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13002         local a_fid
13003         local ln_fid
13004
13005         mkdir -p $DIR/$tdir
13006         mkdir $DIR/$tdir/migrate_dir
13007         $LFS mkdir -i1 $DIR/$tdir/other_dir
13008         touch $DIR/$tdir/migrate_dir/a
13009         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
13010         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
13011         ls $DIR/$tdir/other_dir
13012
13013         # a should be migrated to MDT1, since no other links on MDT0
13014         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13015                 error "migrate dir fails"
13016         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13017         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13018         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13019         [ $mdt_index == 1 ] || error "a is not on MDT1"
13020
13021         # a should stay on MDT1, because it is a mulitple link file
13022         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13023                 error "migrate dir fails"
13024         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13025         [ $mdt_index == 1 ] || error "a is not on MDT1"
13026
13027         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13028                 error "migrate dir fails"
13029
13030         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13031         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
13032         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
13033
13034         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
13035         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
13036
13037         # a should be migrated to MDT0, since no other links on MDT1
13038         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13039                 error "migrate dir fails"
13040         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13041         [ $mdt_index == 0 ] || error "a is not on MDT0"
13042
13043         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13044 }
13045 run_test 230f "migrate mulitple remote link files"
13046
13047 test_230g() {
13048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13049         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13050
13051         mkdir -p $DIR/$tdir/migrate_dir
13052
13053         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
13054                 error "migrating dir to non-exist MDT succeeds"
13055         true
13056 }
13057 run_test 230g "migrate dir to non-exist MDT"
13058
13059 test_231a()
13060 {
13061         # For simplicity this test assumes that max_pages_per_rpc
13062         # is the same across all OSCs
13063         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
13064         local bulk_size=$((max_pages * 4096))
13065
13066         mkdir -p $DIR/$tdir
13067
13068         # clear the OSC stats
13069         $LCTL set_param osc.*.stats=0 &>/dev/null
13070
13071         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
13072         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
13073                 oflag=direct &>/dev/null || error "dd failed"
13074
13075         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
13076         if [ x$nrpcs != "x1" ]; then
13077                 error "found $nrpc ost_write RPCs, not 1 as expected"
13078         fi
13079
13080         # Drop the OSC cache, otherwise we will read from it
13081         cancel_lru_locks osc
13082
13083         # clear the OSC stats
13084         $LCTL set_param osc.*.stats=0 &>/dev/null
13085
13086         # Client reads $bulk_size.
13087         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
13088                 iflag=direct &>/dev/null || error "dd failed"
13089
13090         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
13091         if [ x$nrpcs != "x1" ]; then
13092                 error "found $nrpc ost_read RPCs, not 1 as expected"
13093         fi
13094 }
13095 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
13096
13097 test_231b() {
13098         mkdir -p $DIR/$tdir
13099         local i
13100         for i in {0..1023}; do
13101                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
13102                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
13103                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
13104         done
13105         sync
13106 }
13107 run_test 231b "must not assert on fully utilized OST request buffer"
13108
13109 test_232() {
13110         mkdir -p $DIR/$tdir
13111         #define OBD_FAIL_LDLM_OST_LVB            0x31c
13112         $LCTL set_param fail_loc=0x31c
13113
13114         # ignore dd failure
13115         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
13116
13117         $LCTL set_param fail_loc=0
13118         umount_client $MOUNT || error "umount failed"
13119         mount_client $MOUNT || error "mount failed"
13120 }
13121 run_test 232 "failed lock should not block umount"
13122
13123 test_233a() {
13124         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
13125         { skip "Need MDS version at least 2.3.64"; return; }
13126
13127         local fid=$($LFS path2fid $MOUNT)
13128         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13129                 error "cannot access $MOUNT using its FID '$fid'"
13130 }
13131 run_test 233a "checking that OBF of the FS root succeeds"
13132
13133 test_233b() {
13134         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
13135         { skip "Need MDS version at least 2.5.90"; return; }
13136
13137         local fid=$($LFS path2fid $MOUNT/.lustre)
13138         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13139                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
13140
13141         fid=$($LFS path2fid $MOUNT/.lustre/fid)
13142         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13143                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
13144 }
13145 run_test 233b "checking that OBF of the FS .lustre succeeds"
13146
13147 test_234() {
13148         local p="$TMP/sanityN-$TESTNAME.parameters"
13149         save_lustre_params client "llite.*.xattr_cache" > $p
13150         lctl set_param llite.*.xattr_cache 1 ||
13151                 { skip "xattr cache is not supported"; return 0; }
13152
13153         mkdir -p $DIR/$tdir || error "mkdir failed"
13154         touch $DIR/$tdir/$tfile || error "touch failed"
13155         # OBD_FAIL_LLITE_XATTR_ENOMEM
13156         $LCTL set_param fail_loc=0x1405
13157         # output of the form: attr 2 4 44 3 fc13 x86_64
13158         V=($(IFS=".-" rpm -q attr))
13159         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
13160               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
13161                 # attr pre-2.4.44-7 had a bug with rc
13162                 # LU-3703 - SLES 11 and FC13 clients have older attr
13163                 getfattr -n user.attr $DIR/$tdir/$tfile &&
13164                         error "getfattr should have failed with ENOMEM"
13165         else
13166                 skip "LU-3703: attr version $(getfattr --version) too old"
13167         fi
13168         $LCTL set_param fail_loc=0x0
13169         rm -rf $DIR/$tdir
13170
13171         restore_lustre_params < $p
13172         rm -f $p
13173 }
13174 run_test 234 "xattr cache should not crash on ENOMEM"
13175
13176 test_235() {
13177         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
13178                 skip "Need MDS version at least 2.4.52" && return
13179         flock_deadlock $DIR/$tfile
13180         local RC=$?
13181         case $RC in
13182                 0)
13183                 ;;
13184                 124) error "process hangs on a deadlock"
13185                 ;;
13186                 *) error "error executing flock_deadlock $DIR/$tfile"
13187                 ;;
13188         esac
13189 }
13190 run_test 235 "LU-1715: flock deadlock detection does not work properly"
13191
13192 #LU-2935
13193 test_236() {
13194         check_swap_layouts_support && return 0
13195         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
13196
13197         local ref1=/etc/passwd
13198         local ref2=/etc/group
13199         local file1=$DIR/$tdir/f1
13200         local file2=$DIR/$tdir/f2
13201
13202         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
13203         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
13204         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
13205         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
13206         local fd=$(free_fd)
13207         local cmd="exec $fd<>$file2"
13208         eval $cmd
13209         rm $file2
13210         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
13211                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
13212         cmd="exec $fd>&-"
13213         eval $cmd
13214         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13215
13216         #cleanup
13217         rm -rf $DIR/$tdir
13218 }
13219 run_test 236 "Layout swap on open unlinked file"
13220
13221 # test to verify file handle related system calls
13222 # (name_to_handle_at/open_by_handle_at)
13223 # The new system calls are supported in glibc >= 2.14.
13224
13225 test_237() {
13226         echo "Test file_handle syscalls" > $DIR/$tfile ||
13227                 error "write failed"
13228         check_fhandle_syscalls $DIR/$tfile ||
13229                 error "check_fhandle_syscalls failed"
13230 }
13231 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
13232
13233 # LU-4659 linkea consistency
13234 test_238() {
13235         local server_version=$(lustre_version_code $SINGLEMDS)
13236
13237         [[ $server_version -gt $(version_code 2.5.57) ]] ||
13238                 [[ $server_version -gt $(version_code 2.5.1) &&
13239                    $server_version -lt $(version_code 2.5.50) ]] ||
13240                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
13241
13242         touch $DIR/$tfile
13243         ln $DIR/$tfile $DIR/$tfile.lnk
13244         touch $DIR/$tfile.new
13245         mv $DIR/$tfile.new $DIR/$tfile
13246         local fid1=$($LFS path2fid $DIR/$tfile)
13247         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
13248         local path1=$($LFS fid2path $FSNAME "$fid1")
13249         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
13250         local path2=$($LFS fid2path $FSNAME "$fid2")
13251         [ $tfile.lnk == $path2 ] ||
13252                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
13253         rm -f $DIR/$tfile*
13254 }
13255 run_test 238 "Verify linkea consistency"
13256
13257 test_239() {
13258         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
13259                 skip "Need MDS version at least 2.5.60" && return
13260         local list=$(comma_list $(mdts_nodes))
13261
13262         mkdir -p $DIR/$tdir
13263         createmany -o $DIR/$tdir/f- 5000
13264         unlinkmany $DIR/$tdir/f- 5000
13265         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
13266         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
13267                         osc.*MDT*.sync_in_flight" | calc_sum)
13268         [ "$changes" -eq 0 ] || error "$changes not synced"
13269 }
13270 run_test 239 "osp_sync test"
13271
13272 test_239a() { #LU-5297
13273         touch $DIR/$tfile
13274         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
13275         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
13276         chgrp $RUNAS_GID $DIR/$tfile
13277         wait_delete_completed
13278 }
13279 run_test 239a "process invalid osp sync record correctly"
13280
13281 test_239b() { #LU-5297
13282         touch $DIR/$tfile1
13283         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
13284         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
13285         chgrp $RUNAS_GID $DIR/$tfile1
13286         wait_delete_completed
13287         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13288         touch $DIR/$tfile2
13289         chgrp $RUNAS_GID $DIR/$tfile2
13290         wait_delete_completed
13291 }
13292 run_test 239b "process osp sync record with ENOMEM error correctly"
13293
13294 test_240() {
13295         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13296
13297         mkdir -p $DIR/$tdir
13298
13299         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
13300                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
13301         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
13302                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
13303
13304         umount_client $MOUNT || error "umount failed"
13305         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
13306         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
13307         mount_client $MOUNT || error "failed to mount client"
13308
13309         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
13310         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
13311 }
13312 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
13313
13314 test_241_bio() {
13315         for LOOP in $(seq $1); do
13316                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
13317                 cancel_lru_locks osc
13318         done
13319 }
13320
13321 test_241_dio() {
13322         for LOOP in $(seq $1); do
13323                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
13324                                                 iflag=direct 2>/dev/null
13325         done
13326 }
13327
13328 test_241() {
13329         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13330         ls -la $DIR/$tfile
13331         cancel_lru_locks osc
13332         test_241_bio 1000 &
13333         PID=$!
13334         test_241_dio 1000
13335         wait $PID
13336 }
13337 run_test 241 "bio vs dio"
13338
13339 test_241b() {
13340         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13341         ls -la $DIR/$tfile
13342         test_241_dio 1000 &
13343         PID=$!
13344         test_241_dio 1000
13345         wait $PID
13346 }
13347 run_test 241b "dio vs dio"
13348
13349 test_242() {
13350         mkdir -p $DIR/$tdir
13351         touch $DIR/$tdir/$tfile
13352
13353         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
13354         do_facet mds1 lctl set_param fail_loc=0x105
13355         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
13356
13357         do_facet mds1 lctl set_param fail_loc=0
13358         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
13359 }
13360 run_test 242 "mdt_readpage failure should not cause directory unreadable"
13361
13362 test_243()
13363 {
13364         test_mkdir -p $DIR/$tdir
13365         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
13366 }
13367 run_test 243 "various group lock tests"
13368
13369 test_244()
13370 {
13371         test_mkdir -p $DIR/$tdir
13372         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
13373         sendfile_grouplock $DIR/$tdir/$tfile || \
13374                 error "sendfile+grouplock failed"
13375         rm -rf $DIR/$tdir
13376 }
13377 run_test 244 "sendfile with group lock tests"
13378
13379 test_245() {
13380         local flagname="multi_mod_rpcs"
13381         local connect_data_name="max_mod_rpcs"
13382         local out
13383
13384         # check if multiple modify RPCs flag is set
13385         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
13386                 grep "connect_flags:")
13387         echo "$out"
13388
13389         echo "$out" | grep -qw $flagname
13390         if [ $? -ne 0 ]; then
13391                 echo "connect flag $flagname is not set"
13392                 return
13393         fi
13394
13395         # check if multiple modify RPCs data is set
13396         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
13397         echo "$out"
13398
13399         echo "$out" | grep -qw $connect_data_name ||
13400                 error "import should have connect data $connect_data_name"
13401 }
13402 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
13403
13404 test_246() { # LU-7371
13405         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
13406                 skip "Need OST version >= 2.7.62" && return 0
13407         do_facet ost1 $LCTL set_param fail_val=4095
13408 #define OBD_FAIL_OST_READ_SIZE          0x234
13409         do_facet ost1 $LCTL set_param fail_loc=0x234
13410         $LFS setstripe $DIR/$tfile -i 0 -c 1
13411         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
13412         cancel_lru_locks $FSNAME-OST0000
13413         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
13414 }
13415 run_test 246 "Read file of size 4095 should return right length"
13416
13417 test_250() {
13418         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
13419          && skip "no 16TB file size limit on ZFS" && return
13420
13421         $SETSTRIPE -c 1 $DIR/$tfile
13422         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
13423         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
13424         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
13425         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
13426                 conv=notrunc,fsync && error "append succeeded"
13427         return 0
13428 }
13429 run_test 250 "Write above 16T limit"
13430
13431 test_251() {
13432         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
13433
13434         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
13435         #Skip once - writing the first stripe will succeed
13436         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13437         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
13438                 error "short write happened"
13439
13440         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13441         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
13442                 error "short read happened"
13443
13444         rm -f $DIR/$tfile
13445 }
13446 run_test 251 "Handling short read and write correctly"
13447
13448 test_252() {
13449         local tgt
13450         local dev
13451         local out
13452         local uuid
13453         local num
13454         local gen
13455
13456         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
13457              "$(facet_fstype mds1)" != "ldiskfs" ]; then
13458                 skip "can only run lr_reader on ldiskfs target"
13459                 return
13460         fi
13461
13462         # check lr_reader on OST0000
13463         tgt=ost1
13464         dev=$(facet_device $tgt)
13465         out=$(do_facet $tgt $LR_READER $dev)
13466         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13467         echo "$out"
13468         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
13469         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
13470                 error "Invalid uuid returned by $LR_READER on target $tgt"
13471         echo -e "uuid returned by $LR_READER is '$uuid'\n"
13472
13473         # check lr_reader -c on MDT0000
13474         tgt=mds1
13475         dev=$(facet_device $tgt)
13476         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
13477                 echo "$LR_READER does not support additional options"
13478                 return 0
13479         fi
13480         out=$(do_facet $tgt $LR_READER -c $dev)
13481         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13482         echo "$out"
13483         num=$(echo "$out" | grep -c "mdtlov")
13484         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
13485                 error "Invalid number of mdtlov clients returned by $LR_READER"
13486         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
13487
13488         # check lr_reader -cr on MDT0000
13489         out=$(do_facet $tgt $LR_READER -cr $dev)
13490         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13491         echo "$out"
13492         echo "$out" | grep -q "^reply_data:$" ||
13493                 error "$LR_READER should have returned 'reply_data' section"
13494         num=$(echo "$out" | grep -c "client_generation")
13495         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
13496 }
13497 run_test 252 "check lr_reader tool"
13498
13499
13500 cleanup_test_300() {
13501         trap 0
13502         umask $SAVE_UMASK
13503 }
13504 test_striped_dir() {
13505         local mdt_index=$1
13506         local stripe_count
13507         local stripe_index
13508
13509         mkdir -p $DIR/$tdir
13510
13511         SAVE_UMASK=$(umask)
13512         trap cleanup_test_300 RETURN EXIT
13513
13514         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
13515                                                 $DIR/$tdir/striped_dir ||
13516                 error "set striped dir error"
13517
13518         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
13519         [ "$mode" = "755" ] || error "expect 755 got $mode"
13520
13521         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
13522                 error "getdirstripe failed"
13523         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
13524         if [ "$stripe_count" != "2" ]; then
13525                 error "stripe_count is $stripe_count, expect 2"
13526         fi
13527
13528         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
13529         if [ "$stripe_index" != "$mdt_index" ]; then
13530                 error "stripe_index is $stripe_index, expect $mdt_index"
13531         fi
13532
13533         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13534                 error "nlink error after create striped dir"
13535
13536         mkdir $DIR/$tdir/striped_dir/a
13537         mkdir $DIR/$tdir/striped_dir/b
13538
13539         stat $DIR/$tdir/striped_dir/a ||
13540                 error "create dir under striped dir failed"
13541         stat $DIR/$tdir/striped_dir/b ||
13542                 error "create dir under striped dir failed"
13543
13544         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
13545                 error "nlink error after mkdir"
13546
13547         rmdir $DIR/$tdir/striped_dir/a
13548         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
13549                 error "nlink error after rmdir"
13550
13551         rmdir $DIR/$tdir/striped_dir/b
13552         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13553                 error "nlink error after rmdir"
13554
13555         chattr +i $DIR/$tdir/striped_dir
13556         createmany -o $DIR/$tdir/striped_dir/f 10 &&
13557                 error "immutable flags not working under striped dir!"
13558         chattr -i $DIR/$tdir/striped_dir
13559
13560         rmdir $DIR/$tdir/striped_dir ||
13561                 error "rmdir striped dir error"
13562
13563         cleanup_test_300
13564
13565         true
13566 }
13567
13568 test_300a() {
13569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13570         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13571
13572         test_striped_dir 0 || error "failed on striped dir on MDT0"
13573         test_striped_dir 1 || error "failed on striped dir on MDT0"
13574 }
13575 run_test 300a "basic striped dir sanity test"
13576
13577 test_300b() {
13578         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13579         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13580         local i
13581         local mtime1
13582         local mtime2
13583         local mtime3
13584
13585         test_mkdir $DIR/$tdir || error "mkdir fail"
13586         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13587                 error "set striped dir error"
13588         for ((i=0; i<10; i++)); do
13589                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
13590                 sleep 1
13591                 touch $DIR/$tdir/striped_dir/file_$i ||
13592                                         error "touch error $i"
13593                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
13594                 [ $mtime1 -eq $mtime2 ] &&
13595                         error "mtime not change after create"
13596                 sleep 1
13597                 rm -f $DIR/$tdir/striped_dir/file_$i ||
13598                                         error "unlink error $i"
13599                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
13600                 [ $mtime2 -eq $mtime3 ] &&
13601                         error "mtime did not change after unlink"
13602         done
13603         true
13604 }
13605 run_test 300b "check ctime/mtime for striped dir"
13606
13607 test_300c() {
13608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13609         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13610         local file_count
13611
13612         mkdir -p $DIR/$tdir
13613         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
13614                 error "set striped dir error"
13615
13616         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
13617                 error "chown striped dir failed"
13618
13619         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
13620                 error "create 5k files failed"
13621
13622         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
13623
13624         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
13625
13626         rm -rf $DIR/$tdir
13627 }
13628 run_test 300c "chown && check ls under striped directory"
13629
13630 test_300d() {
13631         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13632         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13633         local stripe_count
13634         local file
13635
13636         mkdir -p $DIR/$tdir
13637         $SETSTRIPE -c 2 $DIR/$tdir
13638
13639         #local striped directory
13640         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13641                 error "set striped dir error"
13642         createmany -o $DIR/$tdir/striped_dir/f 10 ||
13643                 error "create 10 files failed"
13644
13645         #remote striped directory
13646         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
13647                 error "set striped dir error"
13648         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
13649                 error "create 10 files failed"
13650
13651         for file in $(find $DIR/$tdir); do
13652                 stripe_count=$($GETSTRIPE -c $file)
13653                 [ $stripe_count -eq 2 ] ||
13654                         error "wrong stripe $stripe_count for $file"
13655         done
13656
13657         rm -rf $DIR/$tdir
13658 }
13659 run_test 300d "check default stripe under striped directory"
13660
13661 test_300e() {
13662         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13663                 skip "Need MDS version at least 2.7.55" && return
13664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13665         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13666         local stripe_count
13667         local file
13668
13669         mkdir -p $DIR/$tdir
13670
13671         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13672                 error "set striped dir error"
13673
13674         touch $DIR/$tdir/striped_dir/a
13675         touch $DIR/$tdir/striped_dir/b
13676         touch $DIR/$tdir/striped_dir/c
13677
13678         mkdir $DIR/$tdir/striped_dir/dir_a
13679         mkdir $DIR/$tdir/striped_dir/dir_b
13680         mkdir $DIR/$tdir/striped_dir/dir_c
13681
13682         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13683                 error "set striped dir under striped dir error"
13684
13685         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13686                 error "set striped dir under striped dir error"
13687
13688         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13689                 error "set striped dir under striped dir error"
13690
13691         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
13692                 error "rename dir under striped dir fails"
13693
13694         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
13695                 error "rename dir under different stripes fails"
13696
13697         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13698                 error "rename file under striped dir should succeed"
13699
13700         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
13701                 error "rename dir under striped dir should succeed"
13702
13703         rm -rf $DIR/$tdir
13704 }
13705 run_test 300e "check rename under striped directory"
13706
13707 test_300f() {
13708         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13709                 skip "Need MDS version at least 2.7.55" && return
13710         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13711         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13712         local stripe_count
13713         local file
13714
13715         rm -rf $DIR/$tdir
13716         mkdir -p $DIR/$tdir
13717
13718         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13719                 error "set striped dir error"
13720
13721         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13722                 error "set striped dir error"
13723
13724         touch $DIR/$tdir/striped_dir/a
13725         mkdir $DIR/$tdir/striped_dir/dir_a
13726         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13727                 error "create striped dir under striped dir fails"
13728
13729         touch $DIR/$tdir/striped_dir1/b
13730         mkdir $DIR/$tdir/striped_dir1/dir_b
13731         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13732                 error "create striped dir under striped dir fails"
13733
13734         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
13735                 error "rename dir under different striped dir should fail"
13736
13737         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
13738                 error "rename striped dir under diff striped dir should fail"
13739
13740         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13741                 error "rename file under diff striped dirs fails"
13742
13743         rm -rf $DIR/$tdir
13744 }
13745 run_test 300f "check rename cross striped directory"
13746
13747 test_300_check_default_striped_dir()
13748 {
13749         local dirname=$1
13750         local default_count=$2
13751         local default_index=$3
13752         local stripe_count
13753         local stripe_index
13754         local dir_stripe_index
13755         local dir
13756
13757         echo "checking $dirname $default_count $default_index"
13758         $LFS setdirstripe -D -c $default_count -i $default_index \
13759                                 -t all_char $DIR/$tdir/$dirname ||
13760                 error "set default stripe on striped dir error"
13761         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
13762         [ $stripe_count -eq $default_count ] ||
13763                 error "expect $default_count get $stripe_count for $dirname"
13764
13765         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
13766         [ $stripe_index -eq $default_index ] ||
13767                 error "expect $default_index get $stripe_index for $dirname"
13768
13769         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
13770                                                 error "create dirs failed"
13771
13772         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
13773         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
13774         for dir in $(find $DIR/$tdir/$dirname/*); do
13775                 stripe_count=$($LFS getdirstripe -c $dir)
13776                 [ $stripe_count -eq $default_count ] ||
13777                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
13778                 error "stripe count $default_count != $stripe_count for $dir"
13779
13780                 stripe_index=$($LFS getdirstripe -i $dir)
13781                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
13782                         error "$stripe_index != $default_index for $dir"
13783
13784                 #check default stripe
13785                 stripe_count=$($LFS getdirstripe -D -c $dir)
13786                 [ $stripe_count -eq $default_count ] ||
13787                 error "default count $default_count != $stripe_count for $dir"
13788
13789                 stripe_index=$($LFS getdirstripe -D -i $dir)
13790                 [ $stripe_index -eq $default_index ] ||
13791                 error "default index $default_index != $stripe_index for $dir"
13792         done
13793         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
13794 }
13795
13796 test_300g() {
13797         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13798                 skip "Need MDS version at least 2.7.55" && return
13799         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13800         local dir
13801         local stripe_count
13802         local stripe_index
13803
13804         mkdir $DIR/$tdir
13805         mkdir $DIR/$tdir/normal_dir
13806
13807         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
13808         test_300_check_default_striped_dir normal_dir 1 0
13809         test_300_check_default_striped_dir normal_dir 2 1
13810         test_300_check_default_striped_dir normal_dir 2 -1
13811
13812         #delete default stripe information
13813         echo "delete default stripeEA"
13814         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
13815                 error "set default stripe on striped dir error"
13816
13817         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
13818         for dir in $(find $DIR/$tdir/normal_dir/*); do
13819                 stripe_count=$($LFS getdirstripe -c $dir)
13820                 [ $stripe_count -eq 0 ] ||
13821                         error "expect 1 get $stripe_count for $dir"
13822                 stripe_index=$($LFS getdirstripe -i $dir)
13823                 [ $stripe_index -eq 0 ] ||
13824                         error "expect 0 get $stripe_index for $dir"
13825         done
13826 }
13827 run_test 300g "check default striped directory for normal directory"
13828
13829 test_300h() {
13830         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13831                 skip "Need MDS version at least 2.7.55" && return
13832         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13833         local dir
13834         local stripe_count
13835
13836         mkdir $DIR/$tdir
13837         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13838                                         $DIR/$tdir/striped_dir ||
13839                 error "set striped dir error"
13840
13841         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
13842         test_300_check_default_striped_dir striped_dir 1 0
13843         test_300_check_default_striped_dir striped_dir 2 1
13844         test_300_check_default_striped_dir striped_dir 2 -1
13845
13846         #delete default stripe information
13847         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
13848                 error "set default stripe on striped dir error"
13849
13850         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13851         for dir in $(find $DIR/$tdir/striped_dir/*); do
13852                 stripe_count=$($LFS getdirstripe -c $dir)
13853                 [ $stripe_count -eq 0 ] ||
13854                         error "expect 1 get $stripe_count for $dir"
13855         done
13856 }
13857 run_test 300h "check default striped directory for striped directory"
13858
13859 test_300i() {
13860         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13861                 skip "Need MDS version at least 2.7.55" && return
13862         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13863         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13864         local stripe_count
13865         local file
13866
13867         mkdir $DIR/$tdir
13868
13869         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13870                 error "set striped dir error"
13871
13872         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13873                 error "create files under striped dir failed"
13874
13875         # unfortunately, we need to umount to clear dir layout cache for now
13876         # once we fully implement dir layout, we can drop this
13877         umount_client $MOUNT || error "umount failed"
13878         mount_client $MOUNT || error "mount failed"
13879
13880         #set the stripe to be unknown hash type
13881         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13882         $LCTL set_param fail_loc=0x1901
13883         for ((i = 0; i < 10; i++)); do
13884                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13885                         error "stat f-$i failed"
13886                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13887         done
13888
13889         touch $DIR/$tdir/striped_dir/f0 &&
13890                 error "create under striped dir with unknown hash should fail"
13891
13892         $LCTL set_param fail_loc=0
13893
13894         umount_client $MOUNT || error "umount failed"
13895         mount_client $MOUNT || error "mount failed"
13896
13897         return 0
13898 }
13899 run_test 300i "client handle unknown hash type striped directory"
13900
13901 test_300j() {
13902         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13903         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13904                 skip "Need MDS version at least 2.7.55" && return
13905         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13906         local stripe_count
13907         local file
13908
13909         mkdir $DIR/$tdir
13910
13911         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
13912         $LCTL set_param fail_loc=0x1702
13913         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13914                 error "set striped dir error"
13915
13916         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13917                 error "create files under striped dir failed"
13918
13919         $LCTL set_param fail_loc=0
13920
13921         rm -rf $DIR/$tdir || error "unlink striped dir fails"
13922
13923         return 0
13924 }
13925 run_test 300j "test large update record"
13926
13927 test_300k() {
13928         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13929                 skip "Need MDS version at least 2.7.55" && return
13930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13931         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13932         local stripe_count
13933         local file
13934
13935         mkdir $DIR/$tdir
13936
13937         #define OBD_FAIL_LARGE_STRIPE   0x1703
13938         $LCTL set_param fail_loc=0x1703
13939         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
13940                 error "set striped dir error"
13941         $LCTL set_param fail_loc=0
13942
13943         $LFS getdirstripe $DIR/$tdir/striped_dir ||
13944                 error "getstripeddir fails"
13945         rm -rf $DIR/$tdir/striped_dir ||
13946                 error "unlink striped dir fails"
13947
13948         return 0
13949 }
13950 run_test 300k "test large striped directory"
13951
13952 test_300l() {
13953         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13954                 skip "Need MDS version at least 2.7.55" && return
13955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13956         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13957         local stripe_index
13958
13959         test_mkdir -p $DIR/$tdir/striped_dir
13960         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
13961                         error "chown $RUNAS_ID failed"
13962         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
13963                 error "set default striped dir failed"
13964
13965         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
13966         $LCTL set_param fail_loc=0x80000158
13967         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
13968
13969         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
13970         [ $stripe_index -eq 1 ] ||
13971                 error "expect 1 get $stripe_index for $dir"
13972 }
13973 run_test 300l "non-root user to create dir under striped dir with stale layout"
13974
13975 test_300m() {
13976         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13977                 skip "Need MDS version at least 2.7.55" && return
13978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13979         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
13980
13981         mkdir -p $DIR/$tdir/striped_dir
13982         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
13983                 error "set default stripes dir error"
13984
13985         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
13986
13987         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
13988         [ $stripe_count -eq 0 ] ||
13989                         error "expect 0 get $stripe_count for a"
13990
13991         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
13992                 error "set default stripes dir error"
13993
13994         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
13995
13996         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
13997         [ $stripe_count -eq 0 ] ||
13998                         error "expect 0 get $stripe_count for b"
13999
14000         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
14001                 error "set default stripes dir error"
14002
14003         mkdir $DIR/$tdir/striped_dir/c &&
14004                 error "default stripe_index is invalid, mkdir c should fails"
14005
14006         rm -rf $DIR/$tdir || error "rmdir fails"
14007 }
14008 run_test 300m "setstriped directory on single MDT FS"
14009
14010 cleanup_300n() {
14011         local list=$(comma_list $(mdts_nodes))
14012
14013         trap 0
14014         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
14015 }
14016
14017 test_300n() {
14018         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14019                 skip "Need MDS version at least 2.7.55" && return
14020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14021         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14022         local stripe_index
14023         local list=$(comma_list $(mdts_nodes))
14024
14025         trap cleanup_300n RETURN EXIT
14026         mkdir -p $DIR/$tdir
14027         chmod 777 $DIR/$tdir
14028         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
14029                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
14030                 error "create striped dir succeeds with gid=0"
14031
14032         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
14033         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
14034                 error "create striped dir fails with gid=-1"
14035
14036         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
14037         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
14038                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
14039                 error "set default striped dir succeeds with gid=0"
14040
14041
14042         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
14043         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
14044                 error "set default striped dir fails with gid=-1"
14045
14046
14047         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
14048         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
14049                                         error "create test_dir fails"
14050         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
14051                                         error "create test_dir1 fails"
14052         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
14053                                         error "create test_dir2 fails"
14054         cleanup_300n
14055 }
14056 run_test 300n "non-root user to create dir under striped dir with default EA"
14057
14058 test_300o() {
14059         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14060                 skip "Need MDS version at least 2.7.55" && return
14061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14062         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14063         local numfree1
14064         local numfree2
14065
14066         mkdir -p $DIR/$tdir
14067
14068         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
14069         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
14070         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
14071                 skip "not enough free inodes $numfree1 $numfree2"
14072                 return
14073         fi
14074
14075         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
14076         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
14077         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
14078                 skip "not enough free space $numfree1 $numfree2"
14079                 return
14080         fi
14081
14082         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
14083                 error "setdirstripe fails"
14084
14085         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
14086                 error "create dirs fails"
14087
14088         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
14089         ls $DIR/$tdir/striped_dir > /dev/null ||
14090                 error "ls striped dir fails"
14091         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
14092                 error "unlink big striped dir fails"
14093 }
14094 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
14095
14096 test_300p() {
14097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14098         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14099
14100         mkdir -p $DIR/$tdir
14101
14102         #define OBD_FAIL_OUT_ENOSPC     0x1704
14103         do_facet mds2 lctl set_param fail_loc=0x80001704
14104         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
14105                         error "create striped directory should fail"
14106
14107         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
14108         true
14109 }
14110 run_test 300p "create striped directory without space"
14111
14112 prepare_remote_file() {
14113         mkdir $DIR/$tdir/src_dir ||
14114                 error "create remote source failed"
14115
14116         cp /etc/hosts $DIR/$tdir/src_dir/a || error
14117         touch $DIR/$tdir/src_dir/a
14118
14119         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
14120                 error "create remote target dir failed"
14121
14122         touch $DIR/$tdir/tgt_dir/b
14123
14124         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
14125                 error "rename dir cross MDT failed!"
14126
14127         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
14128                 error "src_child still exists after rename"
14129
14130         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
14131                 error "missing file(a) after rename"
14132
14133         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
14134                 error "diff after rename"
14135 }
14136
14137 test_310a() {
14138         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
14139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14140         local remote_file=$DIR/$tdir/tgt_dir/b
14141
14142         mkdir -p $DIR/$tdir
14143
14144         prepare_remote_file || error "prepare remote file failed"
14145
14146         #open-unlink file
14147         $OPENUNLINK $remote_file $remote_file || error
14148         $CHECKSTAT -a $remote_file || error
14149 }
14150 run_test 310a "open unlink remote file"
14151
14152 test_310b() {
14153         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
14154         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14155         local remote_file=$DIR/$tdir/tgt_dir/b
14156
14157         mkdir -p $DIR/$tdir
14158
14159         prepare_remote_file || error "prepare remote file failed"
14160
14161         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14162         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
14163         $CHECKSTAT -t file $remote_file || error "check file failed"
14164 }
14165 run_test 310b "unlink remote file with multiple links while open"
14166
14167 test_310c() {
14168         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
14169         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14170         local remote_file=$DIR/$tdir/tgt_dir/b
14171
14172         mkdir -p $DIR/$tdir
14173
14174         prepare_remote_file || error "prepare remote file failed"
14175
14176         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14177         multiop_bg_pause $remote_file O_uc ||
14178                         error "mulitop failed for remote file"
14179         MULTIPID=$!
14180         $MULTIOP $DIR/$tfile Ouc
14181         kill -USR1 $MULTIPID
14182         wait $MULTIPID
14183 }
14184 run_test 310c "open-unlink remote file with multiple links"
14185
14186 test_400a() { # LU-1606, was conf-sanity test_74
14187         local extra_flags=''
14188         local out=$TMP/$tfile
14189         local prefix=/usr/include/lustre
14190         local prog
14191
14192         if ! which $CC > /dev/null 2>&1; then
14193                 skip_env "$CC is not installed"
14194                 return 0
14195         fi
14196
14197         if ! [[ -d $prefix ]]; then
14198                 # Assume we're running in tree and fixup the include path.
14199                 extra_flags+=" -I$LUSTRE/../libcfs/include"
14200                 extra_flags+=" -I$LUSTRE/include"
14201                 extra_flags+=" -L$LUSTRE/utils"
14202         fi
14203
14204         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
14205                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
14206                         error "client api broken"
14207         done
14208 }
14209 run_test 400a "Lustre client api program can compile and link"
14210
14211 test_400b() { # LU-1606, LU-5011
14212         local header
14213         local out=$TMP/$tfile
14214         local prefix=/usr/include/lustre
14215
14216         # We use a hard coded prefix so that this test will not fail
14217         # when run in tree. There are headers in lustre/include/lustre/
14218         # that are not packaged (like lustre_idl.h) and have more
14219         # complicated include dependencies (like config.h and lnet/types.h).
14220         # Since this test about correct packaging we just skip them when
14221         # they don't exist (see below) rather than try to fixup cppflags.
14222
14223         if ! which $CC > /dev/null 2>&1; then
14224                 skip_env "$CC is not installed"
14225                 return 0
14226         fi
14227
14228         for header in $prefix/*.h; do
14229                 if ! [[ -f "$header" ]]; then
14230                         continue
14231                 fi
14232
14233                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
14234                         continue # liblustreapi.h is deprecated.
14235                 fi
14236
14237                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
14238                         error "cannot compile '$header'"
14239         done
14240 }
14241 run_test 400b "packaged headers can be compiled"
14242
14243 test_401() { #LU-7437
14244         local params
14245         local procs
14246
14247         #count the number of parameters by "list_param -R"
14248         params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
14249         #count the number of parameters by listing proc files
14250         ls -lRL /proc/{fs,sys}/{lnet,lustre} 2>/dev/null |
14251                 grep -v "^t" | grep -v "^d" > $TMP/$tfile
14252         #Since there is no /proc/fs/lnet, we need to remove other
14253         #3 directories, /proc/{fs,sys}/lustre and /proc/sys/lnet.
14254         procs=$(($(sed /^$/d $TMP/$tfile | wc -l)-3))
14255
14256         [ $params -eq $procs ] ||
14257                 error "found $params parameters vs. $procs proc files"
14258
14259         rm -f $TMP/$tfile
14260 }
14261 run_test 401 "Verify if 'lctl list_param -R' can list parameters recursively"
14262
14263 #
14264 # tests that do cleanup/setup should be run at the end
14265 #
14266
14267 test_900() {
14268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14269         local ls
14270         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
14271         $LCTL set_param fail_loc=0x903
14272
14273         cancel_lru_locks MGC
14274
14275         FAIL_ON_ERROR=true cleanup
14276         FAIL_ON_ERROR=true setup
14277 }
14278 run_test 900 "umount should not race with any mgc requeue thread"
14279
14280 complete $SECONDS
14281 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
14282 check_and_cleanup_lustre
14283 if [ "$I_MOUNTED" != "yes" ]; then
14284         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
14285 fi
14286 exit_status