Whamcloud - gitweb
LU-7195 jobstats: Allow setting static content for jobid_var
[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 UMOUNT=${UMOUNT:-"umount -d"}
60 STRIPES_PER_OBJ=-1
61 CHECK_GRANT=${CHECK_GRANT:-"yes"}
62 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
63 export PARALLEL=${PARALLEL:-"no"}
64
65 export NAME=${NAME:-local}
66
67 SAVE_PWD=$PWD
68
69 CLEANUP=${CLEANUP:-:}
70 SETUP=${SETUP:-:}
71 TRACE=${TRACE:-""}
72 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
73 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
74 . $LUSTRE/tests/test-framework.sh
75 init_test_env $@
76 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
77 init_logging
78
79 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24D 27m 64b 68 71 77f 78 115 124b 300o"
80
81 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
82         # bug number for skipped test: LU-4536 LU-1957 LU-2805
83         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  65ic    180     184c"
84         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b 51ba"
85 fi
86
87 FAIL_ON_ERROR=false
88
89 cleanup() {
90         echo -n "cln.."
91         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
92         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
93 }
94 setup() {
95         echo -n "mnt.."
96         load_modules
97         setupall || exit 10
98         echo "done"
99 }
100
101 check_swap_layouts_support()
102 {
103         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
104                 { skip "Does not support layout lock."; return 0; }
105         return 1
106 }
107
108 check_and_setup_lustre
109
110 DIR=${DIR:-$MOUNT}
111 assert_DIR
112
113 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
114         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
115 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
116 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
117 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
118 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
119 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
120 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
121
122 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
123 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
124 rm -rf $DIR/[Rdfs][0-9]*
125
126 # $RUNAS_ID may get set incorrectly somewhere else
127 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
128
129 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
130
131 build_test_filter
132
133 if [ "${ONLY}" = "MOUNT" ] ; then
134         echo "Lustre is up, please go on"
135         exit
136 fi
137
138 echo "preparing for tests involving mounts"
139 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
140 touch $EXT2_DEV
141 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
142 echo # add a newline after mke2fs.
143
144 umask 077
145
146 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
147 lctl set_param debug=-1 2> /dev/null || true
148 test_0a() {
149         touch $DIR/$tfile
150         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
151         rm $DIR/$tfile
152         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
153 }
154 run_test 0a "touch; rm ====================="
155
156 test_0b() {
157         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
158         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
159 }
160 run_test 0b "chmod 0755 $DIR ============================="
161
162 test_0c() {
163         $LCTL get_param mdc.*.import | grep "state: FULL" ||
164                 error "import not FULL"
165         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
166                 error "bad target"
167 }
168 run_test 0c "check import proc ============================="
169
170 test_1() {
171         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
172         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
173         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
174         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
175         rmdir $DIR/$tdir/d2
176         rmdir $DIR/$tdir
177         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
178 }
179 run_test 1 "mkdir; remkdir; rmdir =============================="
180
181 test_2() {
182         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
183         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
184         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
185         rm -r $DIR/$tdir
186         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
187 }
188 run_test 2 "mkdir; touch; rmdir; check file ===================="
189
190 test_3() {
191         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
192         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
193         touch $DIR/$tdir/$tfile
194         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
195         rm -r $DIR/$tdir
196         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
197 }
198 run_test 3 "mkdir; touch; rmdir; check dir ====================="
199
200 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
201 test_4() {
202         local MDTIDX=1
203
204         test_mkdir $DIR/$tdir ||
205                 error "Create remote directory failed"
206
207         touch $DIR/$tdir/$tfile ||
208                 error "Create file under remote directory failed"
209
210         rmdir $DIR/$tdir &&
211                 error "Expect error removing in-use dir $DIR/$tdir"
212
213         test -d $DIR/$tdir || error "Remote directory disappeared"
214
215         rm -rf $DIR/$tdir || error "remove remote dir error"
216 }
217 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
218
219 test_5() {
220         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
221         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
222         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
223         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
224         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
225 }
226 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
227
228 test_6a() {
229         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
230         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
231         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
232                 error "$tfile does not have perm 0666 or UID $UID"
233         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
234         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
235                 error "$tfile should be 0666 and owned by UID $UID"
236 }
237 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
238
239 test_6c() {
240         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
241         touch $DIR/$tfile
242         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
243         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
244                 error "$tfile should be owned by UID $RUNAS_ID"
245         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
246         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
247                 error "$tfile should be owned by UID $RUNAS_ID"
248 }
249 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
250
251 test_6e() {
252         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
253         touch $DIR/$tfile
254         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
255         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
256                 error "$tfile should be owned by GID $UID"
257         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
258         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
259                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
260 }
261 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
262
263 test_6g() {
264         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
265         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
266         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
267         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
268         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
269         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
270         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
271                 error "$tdir/d/subdir should be GID $RUNAS_GID"
272 }
273 run_test 6g "Is new dir in sgid dir inheriting group?"
274
275 test_6h() { # bug 7331
276         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
277         touch $DIR/$tfile || error "touch failed"
278         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
279         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
280                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
281         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
282                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
283 }
284 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
285
286 test_7a() {
287         test_mkdir $DIR/$tdir
288         $MCREATE $DIR/$tdir/$tfile
289         chmod 0666 $DIR/$tdir/$tfile
290         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
291                 error "$tdir/$tfile should be mode 0666"
292 }
293 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
294
295 test_7b() {
296         if [ ! -d $DIR/$tdir ]; then
297                 test_mkdir $DIR/$tdir
298         fi
299         $MCREATE $DIR/$tdir/$tfile
300         echo -n foo > $DIR/$tdir/$tfile
301         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
302         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
303 }
304 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
305
306 test_8() {
307         test_mkdir $DIR/$tdir
308         touch $DIR/$tdir/$tfile
309         chmod 0666 $DIR/$tdir/$tfile
310         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
311                 error "$tfile mode not 0666"
312 }
313 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
314
315 test_9() {
316         test_mkdir $DIR/$tdir
317         test_mkdir $DIR/$tdir/d2
318         test_mkdir $DIR/$tdir/d2/d3
319         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
320 }
321 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
322
323 test_10() {
324         test_mkdir $DIR/$tdir
325         test_mkdir $DIR/$tdir/d2
326         touch $DIR/$tdir/d2/$tfile
327         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
328                 error "$tdir/d2/$tfile not a file"
329 }
330 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
331
332 test_11() {
333         test_mkdir $DIR/$tdir
334         test_mkdir $DIR/$tdir/d2
335         chmod 0666 $DIR/$tdir/d2
336         chmod 0705 $DIR/$tdir/d2
337         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
338                 error "$tdir/d2 mode not 0705"
339 }
340 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
341
342 test_12() {
343         test_mkdir $DIR/$tdir
344         touch $DIR/$tdir/$tfile
345         chmod 0666 $DIR/$tdir/$tfile
346         chmod 0654 $DIR/$tdir/$tfile
347         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
348                 error "$tdir/d2 mode not 0654"
349 }
350 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
351
352 test_13() {
353         test_mkdir $DIR/$tdir
354         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
355         >  $DIR/$tdir/$tfile
356         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
357                 error "$tdir/$tfile size not 0 after truncate"
358 }
359 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
360
361 test_14() {
362         test_mkdir $DIR/$tdir
363         touch $DIR/$tdir/$tfile
364         rm $DIR/$tdir/$tfile
365         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
366 }
367 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
368
369 test_15() {
370         test_mkdir $DIR/$tdir
371         touch $DIR/$tdir/$tfile
372         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
373         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
374                 error "$tdir/${tfile_2} not a file after rename"
375         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
376 }
377 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
378
379 test_16() {
380         test_mkdir $DIR/$tdir
381         touch $DIR/$tdir/$tfile
382         rm -rf $DIR/$tdir/$tfile
383         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
384 }
385 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
386
387 test_17a() {
388         test_mkdir -p $DIR/$tdir
389         touch $DIR/$tdir/$tfile
390         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
391         ls -l $DIR/$tdir
392         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
393                 error "$tdir/l-exist not a symlink"
394         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
395                 error "$tdir/l-exist not referencing a file"
396         rm -f $DIR/$tdir/l-exist
397         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
398 }
399 run_test 17a "symlinks: create, remove (real) =================="
400
401 test_17b() {
402         test_mkdir -p $DIR/$tdir
403         ln -s no-such-file $DIR/$tdir/l-dangle
404         ls -l $DIR/$tdir
405         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
406                 error "$tdir/l-dangle not referencing no-such-file"
407         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
408                 error "$tdir/l-dangle not referencing non-existent file"
409         rm -f $DIR/$tdir/l-dangle
410         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
411 }
412 run_test 17b "symlinks: create, remove (dangling) =============="
413
414 test_17c() { # bug 3440 - don't save failed open RPC for replay
415         test_mkdir -p $DIR/$tdir
416         ln -s foo $DIR/$tdir/$tfile
417         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
418 }
419 run_test 17c "symlinks: open dangling (should return error) ===="
420
421 test_17d() {
422         test_mkdir -p $DIR/$tdir
423         ln -s foo $DIR/$tdir/$tfile
424         touch $DIR/$tdir/$tfile || error "creating to new symlink"
425 }
426 run_test 17d "symlinks: create dangling ========================"
427
428 test_17e() {
429         test_mkdir -p $DIR/$tdir
430         local foo=$DIR/$tdir/$tfile
431         ln -s $foo $foo || error "create symlink failed"
432         ls -l $foo || error "ls -l failed"
433         ls $foo && error "ls not failed" || true
434 }
435 run_test 17e "symlinks: create recursive symlink (should return error) ===="
436
437 test_17f() {
438         test_mkdir -p $DIR/$tdir
439         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
440         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
441         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
442         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
443         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
444         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
445         ls -l  $DIR/$tdir
446 }
447 run_test 17f "symlinks: long and very long symlink name ========================"
448
449 # str_repeat(S, N) generate a string that is string S repeated N times
450 str_repeat() {
451         local s=$1
452         local n=$2
453         local ret=''
454         while [ $((n -= 1)) -ge 0 ]; do
455                 ret=$ret$s
456         done
457         echo $ret
458 }
459
460 # Long symlinks and LU-2241
461 test_17g() {
462         test_mkdir -p $DIR/$tdir
463         local TESTS="59 60 61 4094 4095"
464
465         # Fix for inode size boundary in 2.1.4
466         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
467                 TESTS="4094 4095"
468
469         # Patch not applied to 2.2 or 2.3 branches
470         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
471         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
472                 TESTS="4094 4095"
473
474         # skip long symlink name for rhel6.5.
475         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
476         grep -q '6.5' /etc/redhat-release &>/dev/null &&
477                 TESTS="59 60 61 4062 4063"
478
479         for i in $TESTS; do
480                 local SYMNAME=$(str_repeat 'x' $i)
481                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
482                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
483         done
484 }
485 run_test 17g "symlinks: really long symlink name and inode boundaries"
486
487 test_17h() { #bug 17378
488         remote_mds_nodsh && skip "remote MDS with nodsh" && return
489         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
490         local mdt_idx
491         test_mkdir -p $DIR/$tdir
492         if [[ $MDSCOUNT -gt 1 ]]; then
493                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
494         else
495                 mdt_idx=0
496         fi
497         $SETSTRIPE -c -1 $DIR/$tdir
498 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
499         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
500         touch $DIR/$tdir/$tfile || true
501 }
502 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
503
504 test_17i() { #bug 20018
505         remote_mds_nodsh && skip "remote MDS with nodsh" && return
506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
507         test_mkdir -c1 $DIR/$tdir
508         local foo=$DIR/$tdir/$tfile
509         local mdt_idx
510         if [[ $MDSCOUNT -gt 1 ]]; then
511                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
512         else
513                 mdt_idx=0
514         fi
515         ln -s $foo $foo || error "create symlink failed"
516 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
517         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
518         ls -l $foo && error "error not detected"
519         return 0
520 }
521 run_test 17i "don't panic on short symlink"
522
523 test_17k() { #bug 22301
524         [[ -z "$(which rsync 2>/dev/null)" ]] &&
525                 skip "no rsync command" && return 0
526         rsync --help | grep -q xattr ||
527                 skip_env "$(rsync --version | head -n1) does not support xattrs"
528         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
529         test_mkdir -p $DIR/$tdir
530         test_mkdir -p $DIR/$tdir.new
531         touch $DIR/$tdir/$tfile
532         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
533         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
534                 error "rsync failed with xattrs enabled"
535 }
536 run_test 17k "symlinks: rsync with xattrs enabled ========================="
537
538 test_17l() { # LU-279
539         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
540                 skip "no getfattr command" && return 0
541         test_mkdir -p $DIR/$tdir
542         touch $DIR/$tdir/$tfile
543         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
544         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
545                 # -h to not follow symlinks. -m '' to list all the xattrs.
546                 # grep to remove first line: '# file: $path'.
547                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
548                 do
549                         lgetxattr_size_check $path $xattr ||
550                                 error "lgetxattr_size_check $path $xattr failed"
551                 done
552         done
553 }
554 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
555
556 # LU-1540
557 test_17m() {
558         local short_sym="0123456789"
559         local WDIR=$DIR/${tdir}m
560         local mds_index
561         local devname
562         local cmd
563         local i
564         local rc=0
565
566         remote_mds_nodsh && skip "remote MDS with nodsh" && return
567         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
568         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
569                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
570
571         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
572                 skip "only for ldiskfs MDT" && return 0
573
574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
575
576         test_mkdir -p $WDIR
577         long_sym=$short_sym
578         # create a long symlink file
579         for ((i = 0; i < 4; ++i)); do
580                 long_sym=${long_sym}${long_sym}
581         done
582
583         echo "create 512 short and long symlink files under $WDIR"
584         for ((i = 0; i < 256; ++i)); do
585                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
586                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
587         done
588
589         echo "erase them"
590         rm -f $WDIR/*
591         sync
592         wait_delete_completed
593
594         echo "recreate the 512 symlink files with a shorter string"
595         for ((i = 0; i < 512; ++i)); do
596                 # rewrite the symlink file with a shorter string
597                 ln -sf ${long_sym} $WDIR/long-$i
598                 ln -sf ${short_sym} $WDIR/short-$i
599         done
600
601         mds_index=$($LFS getstripe -M $WDIR)
602         mds_index=$((mds_index+1))
603         devname=$(mdsdevname $mds_index)
604         cmd="$E2FSCK -fnvd $devname"
605
606         echo "stop and checking mds${mds_index}: $cmd"
607         # e2fsck should not return error
608         stop mds${mds_index}
609         do_facet mds${mds_index} $cmd || rc=$?
610
611         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
612         df $MOUNT > /dev/null 2>&1
613         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
614                 "short/long symlink MDT: rc=$rc"
615         return $rc
616 }
617 run_test 17m "run e2fsck against MDT which contains short/long symlink"
618
619 check_fs_consistency_17n() {
620         local mdt_index
621         local devname
622         local cmd
623         local rc=0
624
625         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
626         # so it only check MDT1/MDT2 instead of all of MDTs.
627         for mdt_index in $(seq 1 2); do
628                 devname=$(mdsdevname $mdt_index)
629                 cmd="$E2FSCK -fnvd $devname"
630
631                 echo "stop and checking mds${mdt_index}: $cmd"
632                 # e2fsck should not return error
633                 stop mds${mdt_index}
634                 do_facet mds${mdt_index} $cmd || rc=$?
635
636                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
637                         error "mount mds${mdt_index} failed"
638                 df $MOUNT > /dev/null 2>&1
639                 [ $rc -ne 0 ] && break
640         done
641         return $rc
642 }
643
644 test_17n() {
645         local i
646
647         remote_mds_nodsh && skip "remote MDS with nodsh" && return
648         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
649         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
650                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
651
652         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
653                 skip "only for ldiskfs MDT" && return 0
654
655         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
656
657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
658
659         test_mkdir $DIR/$tdir
660         for ((i=0; i<10; i++)); do
661                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
662                         error "create remote dir error $i"
663                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
664                         error "create files under remote dir failed $i"
665         done
666
667         check_fs_consistency_17n ||
668                 error "e2fsck report error after create files under remote dir"
669
670         for ((i = 0; i < 10; i++)); do
671                 rm -rf $DIR/$tdir/remote_dir_${i} ||
672                         error "destroy remote dir error $i"
673         done
674
675         check_fs_consistency_17n ||
676                 error "e2fsck report error after unlink files under remote dir"
677
678         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
679                 skip "lustre < 2.4.50 does not support migrate mv " && return
680
681         for ((i = 0; i < 10; i++)); do
682                 mkdir -p $DIR/$tdir/remote_dir_${i}
683                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
684                         error "create files under remote dir failed $i"
685                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
686                         error "migrate remote dir error $i"
687         done
688         check_fs_consistency_17n || error "e2fsck report error after migration"
689
690         for ((i = 0; i < 10; i++)); do
691                 rm -rf $DIR/$tdir/remote_dir_${i} ||
692                         error "destroy remote dir error $i"
693         done
694
695         check_fs_consistency_17n || error "e2fsck report error after unlink"
696 }
697 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
698
699 test_17o() {
700         remote_mds_nodsh && skip "remote MDS with nodsh" && return
701         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
702                 skip "Need MDS version at least 2.3.64" && return
703
704         local WDIR=$DIR/${tdir}o
705         local mdt_index
706         local rc=0
707
708         test_mkdir -p $WDIR
709         mdt_index=$($LFS getstripe -M $WDIR)
710         mdt_index=$((mdt_index+1))
711
712         touch $WDIR/$tfile
713
714         #fail mds will wait the failover finish then set
715         #following fail_loc to avoid interfer the recovery process.
716         fail mds${mdt_index}
717
718         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
719         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
720         ls -l $WDIR/$tfile && rc=1
721         do_facet mds${mdt_index} lctl set_param fail_loc=0
722         [[ $rc -ne 0 ]] && error "stat file should fail"
723         true
724 }
725 run_test 17o "stat file with incompat LMA feature"
726
727 test_18() {
728         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
729         ls $DIR || error "Failed to ls $DIR: $?"
730 }
731 run_test 18 "touch .../f ; ls ... =============================="
732
733 test_19a() {
734         touch $DIR/$tfile
735         ls -l $DIR
736         rm $DIR/$tfile
737         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
738 }
739 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
740
741 test_19b() {
742         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
743 }
744 run_test 19b "ls -l .../f19 (should return error) =============="
745
746 test_19c() {
747         [ $RUNAS_ID -eq $UID ] &&
748                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
749         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
750 }
751 run_test 19c "$RUNAS touch .../f19 (should return error) =="
752
753 test_19d() {
754         cat $DIR/f19 && error || true
755 }
756 run_test 19d "cat .../f19 (should return error) =============="
757
758 test_20() {
759         touch $DIR/$tfile
760         rm $DIR/$tfile
761         log "1 done"
762         touch $DIR/$tfile
763         rm $DIR/$tfile
764         log "2 done"
765         touch $DIR/$tfile
766         rm $DIR/$tfile
767         log "3 done"
768         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
769 }
770 run_test 20 "touch .../f ; ls -l ... ==========================="
771
772 test_21() {
773         test_mkdir -p $DIR/$tdir
774         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
775         ln -s dangle $DIR/$tdir/link
776         echo foo >> $DIR/$tdir/link
777         cat $DIR/$tdir/dangle
778         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
779         $CHECKSTAT -f -t file $DIR/$tdir/link ||
780                 error "$tdir/link not linked to a file"
781 }
782 run_test 21 "write to dangling link ============================"
783
784 test_22() {
785         WDIR=$DIR/$tdir
786         test_mkdir -p $DIR/$tdir
787         chown $RUNAS_ID:$RUNAS_GID $WDIR
788         (cd $WDIR || error "cd $WDIR failed";
789         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
790         $RUNAS tar xf -)
791         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
792         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
793         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
794 }
795 run_test 22 "unpack tar archive as non-root user ==============="
796
797 # was test_23
798 test_23a() {
799         test_mkdir -p $DIR/$tdir
800         local file=$DIR/$tdir/$tfile
801
802         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
803         openfile -f O_CREAT:O_EXCL $file &&
804                 error "$file recreate succeeded" || true
805 }
806 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
807
808 test_23b() { # bug 18988
809         test_mkdir -p $DIR/$tdir
810         local file=$DIR/$tdir/$tfile
811
812         rm -f $file
813         echo foo > $file || error "write filed"
814         echo bar >> $file || error "append filed"
815         $CHECKSTAT -s 8 $file || error "wrong size"
816         rm $file
817 }
818 run_test 23b "O_APPEND check =========================="
819
820 # rename sanity
821 test_24a() {
822         echo '-- same directory rename'
823         test_mkdir $DIR/$tdir
824         touch $DIR/$tdir/$tfile.1
825         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
826         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
827 }
828 run_test 24a "rename file to non-existent target"
829
830 test_24b() {
831         test_mkdir $DIR/$tdir
832         touch $DIR/$tdir/$tfile.{1,2}
833         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
834         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
835         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
836 }
837 run_test 24b "rename file to existing target"
838
839 test_24c() {
840         test_mkdir $DIR/$tdir
841         test_mkdir $DIR/$tdir/d$testnum.1
842         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
843         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
844         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
845 }
846 run_test 24c "rename directory to non-existent target"
847
848 test_24d() {
849         test_mkdir -c1 $DIR/$tdir
850         test_mkdir -c1 $DIR/$tdir/d$testnum.1
851         test_mkdir -c1 $DIR/$tdir/d$testnum.2
852         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
853         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
854         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
855 }
856 run_test 24d "rename directory to existing target"
857
858 test_24e() {
859         echo '-- cross directory renames --'
860         test_mkdir $DIR/R5a
861         test_mkdir $DIR/R5b
862         touch $DIR/R5a/f
863         mv $DIR/R5a/f $DIR/R5b/g
864         $CHECKSTAT -a $DIR/R5a/f || error
865         $CHECKSTAT -t file $DIR/R5b/g || error
866 }
867 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
868
869 test_24f() {
870         test_mkdir $DIR/R6a
871         test_mkdir $DIR/R6b
872         touch $DIR/R6a/f $DIR/R6b/g
873         mv $DIR/R6a/f $DIR/R6b/g
874         $CHECKSTAT -a $DIR/R6a/f || error
875         $CHECKSTAT -t file $DIR/R6b/g || error
876 }
877 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
878
879 test_24g() {
880         test_mkdir $DIR/R7a
881         test_mkdir $DIR/R7b
882         test_mkdir $DIR/R7a/d
883         mv $DIR/R7a/d $DIR/R7b/e
884         $CHECKSTAT -a $DIR/R7a/d || error
885         $CHECKSTAT -t dir $DIR/R7b/e || error
886 }
887 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
888
889 test_24h() {
890         test_mkdir -c1 $DIR/R8a
891         test_mkdir -c1 $DIR/R8b
892         test_mkdir -c1 $DIR/R8a/d
893         test_mkdir -c1 $DIR/R8b/e
894         mrename $DIR/R8a/d $DIR/R8b/e
895         $CHECKSTAT -a $DIR/R8a/d || error
896         $CHECKSTAT -t dir $DIR/R8b/e || error
897 }
898 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
899
900 test_24i() {
901         echo "-- rename error cases"
902         test_mkdir $DIR/R9
903         test_mkdir $DIR/R9/a
904         touch $DIR/R9/f
905         mrename $DIR/R9/f $DIR/R9/a
906         $CHECKSTAT -t file $DIR/R9/f || error
907         $CHECKSTAT -t dir  $DIR/R9/a || error
908         $CHECKSTAT -a $DIR/R9/a/f || error
909 }
910 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
911
912 test_24j() {
913         test_mkdir $DIR/R10
914         mrename $DIR/R10/f $DIR/R10/g
915         $CHECKSTAT -t dir $DIR/R10 || error
916         $CHECKSTAT -a $DIR/R10/f || error
917         $CHECKSTAT -a $DIR/R10/g || error
918 }
919 run_test 24j "source does not exist ============================"
920
921 test_24k() {
922         test_mkdir $DIR/R11a
923         test_mkdir $DIR/R11a/d
924         touch $DIR/R11a/f
925         mv $DIR/R11a/f $DIR/R11a/d
926         $CHECKSTAT -a $DIR/R11a/f || error
927         $CHECKSTAT -t file $DIR/R11a/d/f || error
928 }
929 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
930
931 # bug 2429 - rename foo foo foo creates invalid file
932 test_24l() {
933         f="$DIR/f24l"
934         $MULTIOP $f OcNs || error
935 }
936 run_test 24l "Renaming a file to itself ========================"
937
938 test_24m() {
939         f="$DIR/f24m"
940         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
941         # on ext3 this does not remove either the source or target files
942         # though the "expected" operation would be to remove the source
943         $CHECKSTAT -t file ${f} || error "${f} missing"
944         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
945 }
946 run_test 24m "Renaming a file to a hard link to itself ========="
947
948 test_24n() {
949     f="$DIR/f24n"
950     # this stats the old file after it was renamed, so it should fail
951     touch ${f}
952     $CHECKSTAT ${f}
953     mv ${f} ${f}.rename
954     $CHECKSTAT ${f}.rename
955     $CHECKSTAT -a ${f}
956 }
957 run_test 24n "Statting the old file after renaming (Posix rename 2)"
958
959 test_24o() {
960         test_mkdir -p $DIR/d24o
961         rename_many -s random -v -n 10 $DIR/d24o
962 }
963 run_test 24o "rename of files during htree split ==============="
964
965 test_24p() {
966         test_mkdir $DIR/R12a
967         test_mkdir $DIR/R12b
968         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
969         mrename $DIR/R12a $DIR/R12b
970         $CHECKSTAT -a $DIR/R12a || error
971         $CHECKSTAT -t dir $DIR/R12b || error
972         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
973         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
974 }
975 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
976
977 cleanup_multiop_pause() {
978         trap 0
979         kill -USR1 $MULTIPID
980 }
981
982 test_24q() {
983         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
984         test_mkdir $DIR/R13a
985         test_mkdir $DIR/R13b
986         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
987         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
988         MULTIPID=$!
989
990         trap cleanup_multiop_pause EXIT
991         mrename $DIR/R13a $DIR/R13b
992         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
993         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
994         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
995         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
996         cleanup_multiop_pause
997         wait $MULTIPID || error "multiop close failed"
998 }
999 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1000
1001 test_24r() { #bug 3789
1002         test_mkdir $DIR/R14a
1003         test_mkdir $DIR/R14a/b
1004         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1005         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1006         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1007 }
1008 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1009
1010 test_24s() {
1011         test_mkdir $DIR/R15a
1012         test_mkdir $DIR/R15a/b
1013         test_mkdir $DIR/R15a/b/c
1014         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1015         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1016         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1017 }
1018 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1019 test_24t() {
1020         test_mkdir $DIR/R16a
1021         test_mkdir $DIR/R16a/b
1022         test_mkdir $DIR/R16a/b/c
1023         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1024         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1025         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1026 }
1027 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1028
1029 test_24u() { # bug12192
1030         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1031         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1032 }
1033 run_test 24u "create stripe file"
1034
1035 page_size() {
1036         getconf PAGE_SIZE
1037 }
1038
1039 simple_cleanup_common() {
1040         trap 0
1041         rm -rf $DIR/$tdir
1042         wait_delete_completed
1043 }
1044
1045 max_pages_per_rpc() {
1046         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1047 }
1048
1049 test_24v() {
1050         local NRFILES=100000
1051         local FREE_INODES=$(mdt_free_inodes 0)
1052         [[ $FREE_INODES -lt $NRFILES ]] &&
1053                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1054                 return
1055
1056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1057         trap simple_cleanup_common EXIT
1058
1059         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1060         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1061
1062         test_mkdir -p $DIR/$tdir
1063         createmany -m $DIR/$tdir/$tfile $NRFILES
1064
1065         cancel_lru_locks mdc
1066         lctl set_param mdc.*.stats clear
1067
1068         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1069
1070         # LU-5 large readdir
1071         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1072         #               8 bytes for name(filename is mostly 5 in this test) +
1073         #               8 bytes for luda_type
1074         # take into account of overhead in lu_dirpage header and end mark in
1075         # each page, plus one in RPC_NUM calculation.
1076         DIRENT_SIZE=48
1077         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1078         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1079         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1080                                 awk '/^mds_readpage/ {print $2}')
1081         [[ $mds_readpage -gt $RPC_NUM ]] &&
1082                 error "large readdir doesn't take effect"
1083
1084         simple_cleanup_common
1085 }
1086 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1087
1088 test_24w() { # bug21506
1089         SZ1=234852
1090         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1091         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1092         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1093         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1094         [[ "$SZ1" -eq "$SZ2" ]] ||
1095                 error "Error reading at the end of the file $tfile"
1096 }
1097 run_test 24w "Reading a file larger than 4Gb"
1098
1099 test_24x() {
1100         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1102         local MDTIDX=1
1103         local remote_dir=$DIR/$tdir/remote_dir
1104
1105         test_mkdir -p $DIR/$tdir
1106         $LFS mkdir -i $MDTIDX $remote_dir ||
1107                 error "create remote directory failed"
1108
1109         test_mkdir -p $DIR/$tdir/src_dir
1110         touch $DIR/$tdir/src_file
1111         test_mkdir -p $remote_dir/tgt_dir
1112         touch $remote_dir/tgt_file
1113
1114         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1115                 error "rename dir cross MDT failed!"
1116
1117         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1118                 error "rename file cross MDT failed!"
1119
1120         touch $DIR/$tdir/ln_file
1121         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1122                 error "ln file cross MDT failed"
1123
1124         rm -rf $DIR/$tdir || error "Can not delete directories"
1125 }
1126 run_test 24x "cross MDT rename/link"
1127
1128 test_24y() {
1129         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1131         local MDTIDX=1
1132         local remote_dir=$DIR/$tdir/remote_dir
1133
1134         test_mkdir -p $DIR/$tdir
1135         $LFS mkdir -i $MDTIDX $remote_dir ||
1136                    error "create remote directory failed"
1137
1138         test_mkdir -p $remote_dir/src_dir
1139         touch $remote_dir/src_file
1140         test_mkdir -p $remote_dir/tgt_dir
1141         touch $remote_dir/tgt_file
1142
1143         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1144                 error "rename subdir in the same remote dir failed!"
1145
1146         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1147                 error "rename files in the same remote dir failed!"
1148
1149         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1150                 error "link files in the same remote dir failed!"
1151
1152         rm -rf $DIR/$tdir || error "Can not delete directories"
1153 }
1154 run_test 24y "rename/link on the same dir should succeed"
1155
1156 test_24A() { # LU-3182
1157         local NFILES=5000
1158
1159         rm -rf $DIR/$tdir
1160         test_mkdir -p $DIR/$tdir
1161         createmany -m $DIR/$tdir/$tfile $NFILES
1162         local t=$(ls $DIR/$tdir | wc -l)
1163         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1164         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1165         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1166                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1167         fi
1168
1169         rm -rf $DIR/$tdir || error "Can not delete directories"
1170 }
1171 run_test 24A "readdir() returns correct number of entries."
1172
1173 test_24B() { # LU-4805
1174         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1175         local count
1176
1177         test_mkdir $DIR/$tdir
1178         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1179                 error "create striped dir failed"
1180
1181         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1182         [ $count -eq 2 ] || error "Expected 2, got $count"
1183
1184         touch $DIR/$tdir/striped_dir/a
1185
1186         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1187         [ $count -eq 3 ] || error "Expected 3, got $count"
1188
1189         touch $DIR/$tdir/striped_dir/.f
1190
1191         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1192         [ $count -eq 4 ] || error "Expected 4, got $count"
1193
1194         rm -rf $DIR/$tdir || error "Can not delete directories"
1195 }
1196 run_test 24B "readdir for striped dir return correct number of entries"
1197
1198 test_24C() {
1199         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1200
1201         mkdir $DIR/$tdir
1202         mkdir $DIR/$tdir/d0
1203         mkdir $DIR/$tdir/d1
1204
1205         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1206                 error "create striped dir failed"
1207
1208         cd $DIR/$tdir/d0/striped_dir
1209
1210         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1211         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1212         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1213
1214         [ "$d0_ino" = "$parent_ino" ] ||
1215                 error ".. wrong, expect $d0_ino, get $parent_ino"
1216
1217         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1218                 error "mv striped dir failed"
1219
1220         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1221
1222         [ "$d1_ino" = "$parent_ino" ] ||
1223                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1224 }
1225 run_test 24C "check .. in striped dir"
1226
1227 test_24D() { # LU-6101
1228         local NFILES=50000
1229
1230         rm -rf $DIR/$tdir
1231         mkdir -p $DIR/$tdir
1232         createmany -m $DIR/$tdir/$tfile $NFILES
1233         local t=$(ls $DIR/$tdir | wc -l)
1234         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1235         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1236         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1237                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1238         fi
1239
1240         rm -rf $DIR/$tdir || error "Can not delete directories"
1241 }
1242 run_test 24D "readdir() returns correct number of entries after cursor reload"
1243
1244 test_24E() {
1245         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1246         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1247
1248         mkdir -p $DIR/$tdir
1249         mkdir $DIR/$tdir/src_dir
1250         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1251                 error "create remote source failed"
1252
1253         touch $DIR/$tdir/src_dir/src_child/a
1254
1255         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1256                 error "create remote target dir failed"
1257
1258         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1259                 error "create remote target child failed"
1260
1261         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1262                 error "rename dir cross MDT failed!"
1263
1264         find $DIR/$tdir
1265
1266         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1267                 error "src_child still exists after rename"
1268
1269         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1270                 error "missing file(a) after rename"
1271
1272         rm -rf $DIR/$tdir || error "Can not delete directories"
1273 }
1274 run_test 24E "cross MDT rename/link"
1275
1276 test_25a() {
1277         echo '== symlink sanity ============================================='
1278
1279         test_mkdir $DIR/d25
1280         ln -s d25 $DIR/s25
1281         touch $DIR/s25/foo || error
1282 }
1283 run_test 25a "create file in symlinked directory ==============="
1284
1285 test_25b() {
1286         [ ! -d $DIR/d25 ] && test_25a
1287         $CHECKSTAT -t file $DIR/s25/foo || error
1288 }
1289 run_test 25b "lookup file in symlinked directory ==============="
1290
1291 test_26a() {
1292         test_mkdir $DIR/d26
1293         test_mkdir $DIR/d26/d26-2
1294         ln -s d26/d26-2 $DIR/s26
1295         touch $DIR/s26/foo || error
1296 }
1297 run_test 26a "multiple component symlink ======================="
1298
1299 test_26b() {
1300         test_mkdir -p $DIR/d26b/d26-2
1301         ln -s d26b/d26-2/foo $DIR/s26-2
1302         touch $DIR/s26-2 || error
1303 }
1304 run_test 26b "multiple component symlink at end of lookup ======"
1305
1306 test_26c() {
1307         test_mkdir $DIR/d26.2
1308         touch $DIR/d26.2/foo
1309         ln -s d26.2 $DIR/s26.2-1
1310         ln -s s26.2-1 $DIR/s26.2-2
1311         ln -s s26.2-2 $DIR/s26.2-3
1312         chmod 0666 $DIR/s26.2-3/foo
1313 }
1314 run_test 26c "chain of symlinks ================================"
1315
1316 # recursive symlinks (bug 439)
1317 test_26d() {
1318         ln -s d26-3/foo $DIR/d26-3
1319 }
1320 run_test 26d "create multiple component recursive symlink ======"
1321
1322 test_26e() {
1323         [ ! -h $DIR/d26-3 ] && test_26d
1324         rm $DIR/d26-3
1325 }
1326 run_test 26e "unlink multiple component recursive symlink ======"
1327
1328 # recursive symlinks (bug 7022)
1329 test_26f() {
1330         test_mkdir -p $DIR/$tdir
1331         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1332         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1333         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1334         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1335         cd $tfile                || error "cd $tfile failed"
1336         ln -s .. dotdot          || error "ln dotdot failed"
1337         ln -s dotdot/lndir lndir || error "ln lndir failed"
1338         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1339         output=`ls $tfile/$tfile/lndir/bar1`
1340         [ "$output" = bar1 ] && error "unexpected output"
1341         rm -r $tfile             || error "rm $tfile failed"
1342         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1343 }
1344 run_test 26f "rm -r of a directory which has recursive symlink ="
1345
1346 test_27a() {
1347         echo '== stripe sanity =============================================='
1348         test_mkdir -p $DIR/d27 || error "mkdir failed"
1349         $GETSTRIPE $DIR/d27
1350         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1351         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1352         log "== test_27a: write to one stripe file ========================="
1353         cp /etc/hosts $DIR/d27/f0 || error
1354 }
1355 run_test 27a "one stripe file =================================="
1356
1357 test_27b() {
1358         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1359         test_mkdir -p $DIR/d27
1360         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1361         $GETSTRIPE -c $DIR/d27/f01
1362         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1363                 error "two-stripe file doesn't have two stripes"
1364
1365         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1366 }
1367 run_test 27b "create and write to two stripe file"
1368
1369 test_27d() {
1370         test_mkdir -p $DIR/d27
1371         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1372         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1373         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1374 }
1375 run_test 27d "create file with default settings ================"
1376
1377 test_27e() {
1378         # LU-5839 adds check for existed layout before setting it
1379         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1380                 skip "Need MDS version at least 2.7.56" && return
1381         test_mkdir -p $DIR/d27
1382         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1383         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1384         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1385 }
1386 run_test 27e "setstripe existing file (should return error) ======"
1387
1388 test_27f() {
1389         test_mkdir $DIR/$tdir
1390         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1391                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1392         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1393                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1394         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1395         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1396 }
1397 run_test 27f "setstripe with bad stripe size (should return error)"
1398
1399 test_27g() {
1400         test_mkdir -p $DIR/d27
1401         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1402         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1403                 error "$DIR/d27/fnone has object"
1404 }
1405 run_test 27g "$GETSTRIPE with no objects"
1406
1407 test_27i() {
1408         test_mkdir $DIR/$tdir
1409         touch $DIR/$tdir/$tfile || error "touch failed"
1410         [[ $($GETSTRIPE -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1411                 error "missing objects"
1412 }
1413 run_test 27i "$GETSTRIPE with some objects"
1414
1415 test_27j() {
1416         test_mkdir -p $DIR/d27
1417         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1418 }
1419 run_test 27j "setstripe with bad stripe offset (should return error)"
1420
1421 test_27k() { # bug 2844
1422         test_mkdir -p $DIR/d27
1423         FILE=$DIR/d27/f27k
1424         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1425         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1426         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1427         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1428         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1429         dd if=/dev/zero of=$FILE bs=4k count=1
1430         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1431         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1432 }
1433 run_test 27k "limit i_blksize for broken user apps ============="
1434
1435 test_27l() {
1436         test_mkdir -p $DIR/d27
1437         mcreate $DIR/f27l || error "creating file"
1438         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1439                 error "setstripe should have failed" || true
1440 }
1441 run_test 27l "check setstripe permissions (should return error)"
1442
1443 test_27m() {
1444         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1445                 return
1446         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1447                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1448                 return
1449         fi
1450         trap simple_cleanup_common EXIT
1451         test_mkdir -p $DIR/$tdir
1452         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1453         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1454                 error "dd should fill OST0"
1455         i=2
1456         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1457                 i=$((i + 1))
1458                 [ $i -gt 256 ] && break
1459         done
1460         i=$((i + 1))
1461         touch $DIR/$tdir/f27m_$i
1462         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1463                 error "OST0 was full but new created file still use it"
1464         i=$((i + 1))
1465         touch $DIR/$tdir/f27m_$i
1466         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1467                 error "OST0 was full but new created file still use it"
1468         simple_cleanup_common
1469 }
1470 run_test 27m "create file while OST0 was full =================="
1471
1472 sleep_maxage() {
1473         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1474         sleep $DELAY
1475 }
1476
1477 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1478 # if the OST isn't full anymore.
1479 reset_enospc() {
1480         local OSTIDX=${1:-""}
1481
1482         local list=$(comma_list $(osts_nodes))
1483         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1484
1485         do_nodes $list lctl set_param fail_loc=0
1486         sync    # initiate all OST_DESTROYs from MDS to OST
1487         sleep_maxage
1488 }
1489
1490 exhaust_precreations() {
1491         local OSTIDX=$1
1492         local FAILLOC=$2
1493         local FAILIDX=${3:-$OSTIDX}
1494         local ofacet=ost$((OSTIDX + 1))
1495
1496         test_mkdir -p -c1 $DIR/$tdir
1497         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1498         local mfacet=mds$((mdtidx + 1))
1499         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1500
1501         local OST=$(ostname_from_index $OSTIDX)
1502
1503         # on the mdt's osc
1504         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1505         local last_id=$(do_facet $mfacet lctl get_param -n \
1506                         osc.$mdtosc_proc1.prealloc_last_id)
1507         local next_id=$(do_facet $mfacet lctl get_param -n \
1508                         osc.$mdtosc_proc1.prealloc_next_id)
1509
1510         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1511         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1512
1513         test_mkdir -p $DIR/$tdir/${OST}
1514         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1515 #define OBD_FAIL_OST_ENOSPC              0x215
1516         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1517         echo "Creating to objid $last_id on ost $OST..."
1518         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1519         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1520         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1521         sleep_maxage
1522 }
1523
1524 exhaust_all_precreations() {
1525         local i
1526         for (( i=0; i < OSTCOUNT; i++ )) ; do
1527                 exhaust_precreations $i $1 -1
1528         done
1529 }
1530
1531 test_27n() {
1532         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1534         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1535         remote_ost_nodsh && skip "remote OST with nodsh" && return
1536
1537         reset_enospc
1538         rm -f $DIR/$tdir/$tfile
1539         exhaust_precreations 0 0x80000215
1540         $SETSTRIPE -c -1 $DIR/$tdir
1541         touch $DIR/$tdir/$tfile || error
1542         $GETSTRIPE $DIR/$tdir/$tfile
1543         reset_enospc
1544 }
1545 run_test 27n "create file with some full OSTs =================="
1546
1547 test_27o() {
1548         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1550         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1551         remote_ost_nodsh && skip "remote OST with nodsh" && return
1552
1553         reset_enospc
1554         rm -f $DIR/$tdir/$tfile
1555         exhaust_all_precreations 0x215
1556
1557         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1558
1559         reset_enospc
1560         rm -rf $DIR/$tdir/*
1561 }
1562 run_test 27o "create file with all full OSTs (should error) ===="
1563
1564 test_27p() {
1565         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1567         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1568         remote_ost_nodsh && skip "remote OST with nodsh" && return
1569
1570         reset_enospc
1571         rm -f $DIR/$tdir/$tfile
1572         test_mkdir -p $DIR/$tdir
1573
1574         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1575         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1576         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1577
1578         exhaust_precreations 0 0x80000215
1579         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1580         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1581         $GETSTRIPE $DIR/$tdir/$tfile
1582
1583         reset_enospc
1584 }
1585 run_test 27p "append to a truncated file with some full OSTs ==="
1586
1587 test_27q() {
1588         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1590         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1591         remote_ost_nodsh && skip "remote OST with nodsh" && return
1592
1593         reset_enospc
1594         rm -f $DIR/$tdir/$tfile
1595
1596         test_mkdir -p $DIR/$tdir
1597         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1598         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1599         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1600
1601         exhaust_all_precreations 0x215
1602
1603         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1604         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1605
1606         reset_enospc
1607 }
1608 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1609
1610 test_27r() {
1611         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1612         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1613         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1614         remote_ost_nodsh && skip "remote OST with nodsh" && return
1615
1616         reset_enospc
1617         rm -f $DIR/$tdir/$tfile
1618         exhaust_precreations 0 0x80000215
1619
1620         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1621
1622         reset_enospc
1623 }
1624 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1625
1626 test_27s() { # bug 10725
1627         test_mkdir -p $DIR/$tdir
1628         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1629         local stripe_count=0
1630         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1631         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1632                 error "stripe width >= 2^32 succeeded" || true
1633
1634 }
1635 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1636
1637 test_27t() { # bug 10864
1638         WDIR=$(pwd)
1639         WLFS=$(which lfs)
1640         cd $DIR
1641         touch $tfile
1642         $WLFS getstripe $tfile
1643         cd $WDIR
1644 }
1645 run_test 27t "check that utils parse path correctly"
1646
1647 test_27u() { # bug 4900
1648         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1649         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1650         local index
1651         local list=$(comma_list $(mdts_nodes))
1652
1653 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1654         do_nodes $list $LCTL set_param fail_loc=0x139
1655         test_mkdir -p $DIR/$tdir
1656         rm -rf $DIR/$tdir/*
1657         createmany -o $DIR/$tdir/t- 1000
1658         do_nodes $list $LCTL set_param fail_loc=0
1659
1660         TLOG=$DIR/$tfile.getstripe
1661         $GETSTRIPE $DIR/$tdir > $TLOG
1662         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1663         unlinkmany $DIR/$tdir/t- 1000
1664         [[ $OBJS -gt 0 ]] &&
1665                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1666 }
1667 run_test 27u "skip object creation on OSC w/o objects =========="
1668
1669 test_27v() { # bug 4900
1670         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1672         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1673         remote_ost_nodsh && skip "remote OST with nodsh" && return
1674
1675         exhaust_all_precreations 0x215
1676         reset_enospc
1677
1678         test_mkdir -p $DIR/$tdir
1679         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1680
1681         touch $DIR/$tdir/$tfile
1682         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1683         # all except ost1
1684         for (( i=1; i < OSTCOUNT; i++ )); do
1685                 do_facet ost$i lctl set_param fail_loc=0x705
1686         done
1687         local START=`date +%s`
1688         createmany -o $DIR/$tdir/$tfile 32
1689
1690         local FINISH=`date +%s`
1691         local TIMEOUT=`lctl get_param -n timeout`
1692         local PROCESS=$((FINISH - START))
1693         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1694                error "$FINISH - $START >= $TIMEOUT / 2"
1695         sleep $((TIMEOUT / 2 - PROCESS))
1696         reset_enospc
1697 }
1698 run_test 27v "skip object creation on slow OST ================="
1699
1700 test_27w() { # bug 10997
1701         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1702         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1703         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1704                 error "stripe size $size != 65536" || true
1705         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1706                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1707 }
1708 run_test 27w "check $SETSTRIPE -S option"
1709
1710 test_27wa() {
1711         [[ $OSTCOUNT -lt 2 ]] &&
1712                 skip_env "skipping multiple stripe count/offset test" && return
1713
1714         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1715         for i in $(seq 1 $OSTCOUNT); do
1716                 offset=$((i - 1))
1717                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1718                         error "setstripe -c $i -i $offset failed"
1719                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1720                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1721                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1722                 [ $index -ne $offset ] &&
1723                         error "stripe offset $index != $offset" || true
1724         done
1725 }
1726 run_test 27wa "check $SETSTRIPE -c -i options"
1727
1728 test_27x() {
1729         remote_ost_nodsh && skip "remote OST with nodsh" && return
1730         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1732         OFFSET=$(($OSTCOUNT - 1))
1733         OSTIDX=0
1734         local OST=$(ostname_from_index $OSTIDX)
1735
1736         test_mkdir -p $DIR/$tdir
1737         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1738         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1739         sleep_maxage
1740         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1741         for i in `seq 0 $OFFSET`; do
1742                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1743                 error "OST0 was degraded but new created file still use it"
1744         done
1745         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1746 }
1747 run_test 27x "create files while OST0 is degraded"
1748
1749 test_27y() {
1750         [[ $OSTCOUNT -lt 2 ]] &&
1751                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1752         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1753         remote_ost_nodsh && skip "remote OST with nodsh" && return
1754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1755
1756         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1757         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1758             osc.$mdtosc.prealloc_last_id)
1759         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1760             osc.$mdtosc.prealloc_next_id)
1761         local fcount=$((last_id - next_id))
1762         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1763         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1764
1765         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1766                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1767         local OST_DEACTIVE_IDX=-1
1768         local OSC
1769         local OSTIDX
1770         local OST
1771
1772         for OSC in $MDS_OSCS; do
1773                 OST=$(osc_to_ost $OSC)
1774                 OSTIDX=$(index_from_ostuuid $OST)
1775                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1776                         OST_DEACTIVE_IDX=$OSTIDX
1777                 fi
1778                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1779                         echo $OSC "is Deactivated:"
1780                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1781                 fi
1782         done
1783
1784         OSTIDX=$(index_from_ostuuid $OST)
1785         test_mkdir -p $DIR/$tdir
1786         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1787
1788         for OSC in $MDS_OSCS; do
1789                 OST=$(osc_to_ost $OSC)
1790                 OSTIDX=$(index_from_ostuuid $OST)
1791                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1792                         echo $OST "is degraded:"
1793                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1794                                                 obdfilter.$OST.degraded=1
1795                 fi
1796         done
1797
1798         sleep_maxage
1799         createmany -o $DIR/$tdir/$tfile $fcount
1800
1801         for OSC in $MDS_OSCS; do
1802                 OST=$(osc_to_ost $OSC)
1803                 OSTIDX=$(index_from_ostuuid $OST)
1804                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1805                         echo $OST "is recovered from degraded:"
1806                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1807                                                 obdfilter.$OST.degraded=0
1808                 else
1809                         do_facet $SINGLEMDS lctl --device %$OSC activate
1810                 fi
1811         done
1812
1813         # all osp devices get activated, hence -1 stripe count restored
1814         local stripecnt=0
1815
1816         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1817         # devices get activated.
1818         sleep_maxage
1819         $SETSTRIPE -c -1 $DIR/$tfile
1820         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1821         rm -f $DIR/$tfile
1822         [ $stripecnt -ne $OSTCOUNT ] &&
1823                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1824         return 0
1825 }
1826 run_test 27y "create files while OST0 is degraded and the rest inactive"
1827
1828 check_seq_oid()
1829 {
1830         log "check file $1"
1831
1832         lmm_count=$($GETSTRIPE -c $1)
1833         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1834         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1835
1836         local old_ifs="$IFS"
1837         IFS=$'[:]'
1838         fid=($($LFS path2fid $1))
1839         IFS="$old_ifs"
1840
1841         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1842         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1843
1844         # compare lmm_seq and lu_fid->f_seq
1845         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1846         # compare lmm_object_id and lu_fid->oid
1847         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1848
1849         # check the trusted.fid attribute of the OST objects of the file
1850         local have_obdidx=false
1851         local stripe_nr=0
1852         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1853                 # skip lines up to and including "obdidx"
1854                 [ -z "$obdidx" ] && break
1855                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1856                 $have_obdidx || continue
1857
1858                 local ost=$((obdidx + 1))
1859                 local dev=$(ostdevname $ost)
1860                 local oid_hex
1861
1862                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1863
1864                 seq=$(echo $seq | sed -e "s/^0x//g")
1865                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1866                         oid_hex=$(echo $oid)
1867                 else
1868                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1869                 fi
1870                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1871
1872                 local ff
1873                 #
1874                 # Don't unmount/remount the OSTs if we don't need to do that.
1875                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1876                 # update too, until that use mount/ll_decode_filter_fid/mount.
1877                 # Re-enable when debugfs will understand new filter_fid.
1878                 #
1879                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1880                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1881                                 $dev 2>/dev/null" | grep "parent=")
1882                 else
1883                         stop ost$ost
1884                         mount_fstype ost$ost
1885                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1886                                 $(facet_mntpt ost$ost)/$obj_file)
1887                         unmount_fstype ost$ost
1888                         start ost$ost $dev $OST_MOUNT_OPTS
1889                 fi
1890
1891                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1892
1893                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1894
1895                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1896                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1897                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1898                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1899                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1900                 local ff_pstripe
1901                 if echo $ff_parent | grep -q 'stripe='; then
1902                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1903                 else
1904                         #
1905                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1906                         # into f_ver in this case.  See the comment on
1907                         # ff_parent.
1908                         #
1909                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1910                                 sed -e 's/\]//')
1911                 fi
1912
1913                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1914                 [ $ff_pseq = $lmm_seq ] ||
1915                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1916                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1917                 [ $ff_poid = $lmm_oid ] ||
1918                         error "FF parent OID $ff_poid != $lmm_oid"
1919                 (($ff_pstripe == $stripe_nr)) ||
1920                         error "FF stripe $ff_pstripe != $stripe_nr"
1921
1922                 stripe_nr=$((stripe_nr + 1))
1923         done
1924 }
1925
1926 test_27z() {
1927         remote_ost_nodsh && skip "remote OST with nodsh" && return
1928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1929         test_mkdir -p $DIR/$tdir
1930
1931         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1932                 { error "setstripe -c -1 failed"; return 1; }
1933         # We need to send a write to every object to get parent FID info set.
1934         # This _should_ also work for setattr, but does not currently.
1935         # touch $DIR/$tdir/$tfile-1 ||
1936         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1937                 { error "dd $tfile-1 failed"; return 2; }
1938         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1939                 { error "setstripe -c -1 failed"; return 3; }
1940         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1941                 { error "dd $tfile-2 failed"; return 4; }
1942
1943         # make sure write RPCs have been sent to OSTs
1944         sync; sleep 5; sync
1945
1946         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1947         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1948 }
1949 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1950
1951 test_27A() { # b=19102
1952         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1953         local restore_size=$($GETSTRIPE -S $MOUNT)
1954         local restore_count=$($GETSTRIPE -c $MOUNT)
1955         local restore_offset=$($GETSTRIPE -i $MOUNT)
1956         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1957         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1958                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1959         local default_size=$($GETSTRIPE -S $MOUNT)
1960         local default_offset=$($GETSTRIPE -i $MOUNT)
1961         local dsize=$((1024 * 1024))
1962         [ $default_size -eq $dsize ] ||
1963                 error "stripe size $default_size != $dsize"
1964         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1965         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1966 }
1967 run_test 27A "check filesystem-wide default LOV EA values"
1968
1969 test_27B() { # LU-2523
1970         test_mkdir -p $DIR/$tdir
1971         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1972         touch $DIR/$tdir/f0
1973         # open f1 with O_LOV_DELAY_CREATE
1974         # rename f0 onto f1
1975         # call setstripe ioctl on open file descriptor for f1
1976         # close
1977         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1978                 $DIR/$tdir/f0
1979
1980         rm -f $DIR/$tdir/f1
1981         # open f1 with O_LOV_DELAY_CREATE
1982         # unlink f1
1983         # call setstripe ioctl on open file descriptor for f1
1984         # close
1985         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1986
1987         # Allow multiop to fail in imitation of NFS's busted semantics.
1988         true
1989 }
1990 run_test 27B "call setstripe on open unlinked file/rename victim"
1991
1992 test_27C() { #LU-2871
1993         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1994
1995         declare -a ost_idx
1996         local index
1997         local found
1998         local i
1999         local j
2000
2001         test_mkdir -p $DIR/$tdir
2002         cd $DIR/$tdir
2003         for i in $(seq 0 $((OSTCOUNT - 1))); do
2004                 # set stripe across all OSTs starting from OST$i
2005                 $SETSTRIPE -i $i -c -1 $tfile$i
2006                 # get striping information
2007                 ost_idx=($($GETSTRIPE $tfile$i |
2008                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2009                 echo ${ost_idx[@]}
2010
2011                 # check the layout
2012                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2013                         error "${#ost_idx[@]} != $OSTCOUNT"
2014
2015                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2016                         found=0
2017                         for j in $(echo ${ost_idx[@]}); do
2018                                 if [ $index -eq $j ]; then
2019                                         found=1
2020                                         break
2021                                 fi
2022                         done
2023                         [ $found = 1 ] ||
2024                                 error "Can not find $index in ${ost_idx[@]}"
2025                 done
2026         done
2027 }
2028 run_test 27C "check full striping across all OSTs"
2029
2030 test_27D() {
2031         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2032         local POOL=${POOL:-testpool}
2033         local first_ost=0
2034         local last_ost=$(($OSTCOUNT - 1))
2035         local ost_step=1
2036         local ost_list=$(seq $first_ost $ost_step $last_ost)
2037         local ost_range="$first_ost $last_ost $ost_step"
2038
2039         test_mkdir -p $DIR/$tdir
2040         pool_add $POOL || error "pool_add failed"
2041         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2042         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2043                 error "llapi_layout_test failed"
2044         cleanup_pools || error "cleanup_pools failed"
2045 }
2046 run_test 27D "validate llapi_layout API"
2047
2048 # Verify that default_easize is increased from its initial value after
2049 # accessing a widely striped file.
2050 test_27E() {
2051         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2052
2053         # 72 bytes is the minimum space required to store striping
2054         # information for a file striped across one OST:
2055         # (sizeof(struct lov_user_md_v3) +
2056         #  sizeof(struct lov_user_ost_data_v1))
2057         local min_easize=72
2058         $LCTL set_param -n llite.*.default_easize $min_easize ||
2059                 error "lctl set_param failed"
2060         local easize=$($LCTL get_param -n llite.*.default_easize)
2061
2062         [ $easize -eq $min_easize ] ||
2063                 error "failed to set default_easize"
2064
2065         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2066                 error "setstripe failed"
2067         cat $DIR/$tfile
2068         rm $DIR/$tfile
2069
2070         easize=$($LCTL get_param -n llite.*.default_easize)
2071
2072         [ $easize -gt $min_easize ] ||
2073                 error "default_easize not updated"
2074 }
2075 run_test 27E "check that default extended attribute size properly increases"
2076
2077 # createtest also checks that device nodes are created and
2078 # then visible correctly (#2091)
2079 test_28() { # bug 2091
2080         test_mkdir $DIR/d28
2081         $CREATETEST $DIR/d28/ct || error
2082 }
2083 run_test 28 "create/mknod/mkdir with bad file types ============"
2084
2085 test_29() {
2086         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2087         cancel_lru_locks mdc
2088         test_mkdir $DIR/d29
2089         touch $DIR/d29/foo
2090         log 'first d29'
2091         ls -l $DIR/d29
2092
2093         declare -i LOCKCOUNTORIG=0
2094         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2095                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2096         done
2097         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2098
2099         declare -i LOCKUNUSEDCOUNTORIG=0
2100         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2101                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2102         done
2103
2104         log 'second d29'
2105         ls -l $DIR/d29
2106         log 'done'
2107
2108         declare -i LOCKCOUNTCURRENT=0
2109         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2110                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2111         done
2112
2113         declare -i LOCKUNUSEDCOUNTCURRENT=0
2114         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2115                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2116         done
2117
2118         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2119                 $LCTL set_param -n ldlm.dump_namespaces ""
2120                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2121                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2122                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2123                 return 2
2124         fi
2125         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2126                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2127                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2128                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2129                 return 3
2130         fi
2131 }
2132 run_test 29 "IT_GETATTR regression  ============================"
2133
2134 test_30a() { # was test_30
2135         cp $(which ls) $DIR || cp /bin/ls $DIR
2136         $DIR/ls / || error
2137         rm $DIR/ls
2138 }
2139 run_test 30a "execute binary from Lustre (execve) =============="
2140
2141 test_30b() {
2142         cp `which ls` $DIR || cp /bin/ls $DIR
2143         chmod go+rx $DIR/ls
2144         $RUNAS $DIR/ls / || error
2145         rm $DIR/ls
2146 }
2147 run_test 30b "execute binary from Lustre as non-root ==========="
2148
2149 test_30c() { # b=22376
2150         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2151         cp `which ls` $DIR || cp /bin/ls $DIR
2152         chmod a-rw $DIR/ls
2153         cancel_lru_locks mdc
2154         cancel_lru_locks osc
2155         $RUNAS $DIR/ls / || error
2156         rm -f $DIR/ls
2157 }
2158 run_test 30c "execute binary from Lustre without read perms ===="
2159
2160 test_31a() {
2161         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2162         $CHECKSTAT -a $DIR/f31 || error
2163 }
2164 run_test 31a "open-unlink file =================================="
2165
2166 test_31b() {
2167         touch $DIR/f31 || error
2168         ln $DIR/f31 $DIR/f31b || error
2169         $MULTIOP $DIR/f31b Ouc || error
2170         $CHECKSTAT -t file $DIR/f31 || error
2171 }
2172 run_test 31b "unlink file with multiple links while open ======="
2173
2174 test_31c() {
2175         touch $DIR/f31 || error
2176         ln $DIR/f31 $DIR/f31c || error
2177         multiop_bg_pause $DIR/f31 O_uc || return 1
2178         MULTIPID=$!
2179         $MULTIOP $DIR/f31c Ouc
2180         kill -USR1 $MULTIPID
2181         wait $MULTIPID
2182 }
2183 run_test 31c "open-unlink file with multiple links ============="
2184
2185 test_31d() {
2186         opendirunlink $DIR/d31d $DIR/d31d || error
2187         $CHECKSTAT -a $DIR/d31d || error
2188 }
2189 run_test 31d "remove of open directory ========================="
2190
2191 test_31e() { # bug 2904
2192         openfilleddirunlink $DIR/d31e || error
2193 }
2194 run_test 31e "remove of open non-empty directory ==============="
2195
2196 test_31f() { # bug 4554
2197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2198         set -vx
2199         test_mkdir $DIR/d31f
2200         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2201         cp /etc/hosts $DIR/d31f
2202         ls -l $DIR/d31f
2203         $GETSTRIPE $DIR/d31f/hosts
2204         multiop_bg_pause $DIR/d31f D_c || return 1
2205         MULTIPID=$!
2206
2207         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2208         test_mkdir $DIR/d31f
2209         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2210         cp /etc/hosts $DIR/d31f
2211         ls -l $DIR/d31f
2212         $GETSTRIPE $DIR/d31f/hosts
2213         multiop_bg_pause $DIR/d31f D_c || return 1
2214         MULTIPID2=$!
2215
2216         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2217         wait $MULTIPID || error "first opendir $MULTIPID failed"
2218
2219         sleep 6
2220
2221         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2222         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2223         set +vx
2224 }
2225 run_test 31f "remove of open directory with open-unlink file ==="
2226
2227 test_31g() {
2228         echo "-- cross directory link --"
2229         test_mkdir -c1 $DIR/${tdir}ga
2230         test_mkdir -c1 $DIR/${tdir}gb
2231         touch $DIR/${tdir}ga/f
2232         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2233         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2234         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2235         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2236         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2237 }
2238 run_test 31g "cross directory link==============="
2239
2240 test_31h() {
2241         echo "-- cross directory link --"
2242         test_mkdir -c1 $DIR/${tdir}
2243         test_mkdir -c1 $DIR/${tdir}/dir
2244         touch $DIR/${tdir}/f
2245         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2246         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2247         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2248         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2249         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2250 }
2251 run_test 31h "cross directory link under child==============="
2252
2253 test_31i() {
2254         echo "-- cross directory link --"
2255         test_mkdir -c1 $DIR/$tdir
2256         test_mkdir -c1 $DIR/$tdir/dir
2257         touch $DIR/$tdir/dir/f
2258         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2259         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2260         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2261         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2262         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2263 }
2264 run_test 31i "cross directory link under parent==============="
2265
2266 test_31j() {
2267         test_mkdir -c1 -p $DIR/$tdir
2268         test_mkdir -c1 -p $DIR/$tdir/dir1
2269         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2270         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2271         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2272         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2273         return 0
2274 }
2275 run_test 31j "link for directory==============="
2276
2277 test_31k() {
2278         test_mkdir -c1 -p $DIR/$tdir
2279         touch $DIR/$tdir/s
2280         touch $DIR/$tdir/exist
2281         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2282         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2283         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2284         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2285         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2286         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2287         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2288         return 0
2289 }
2290 run_test 31k "link to file: the same, non-existing, dir==============="
2291
2292 test_31m() {
2293         mkdir $DIR/d31m
2294         touch $DIR/d31m/s
2295         mkdir $DIR/d31m2
2296         touch $DIR/d31m2/exist
2297         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2298         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2299         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2300         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2301         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2302         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2303         return 0
2304 }
2305 run_test 31m "link to file: the same, non-existing, dir==============="
2306
2307 test_31n() {
2308         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2309         nlink=$(stat --format=%h $DIR/$tfile)
2310         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2311         local fd=$(free_fd)
2312         local cmd="exec $fd<$DIR/$tfile"
2313         eval $cmd
2314         cmd="exec $fd<&-"
2315         trap "eval $cmd" EXIT
2316         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2317         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2318         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2319         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2320         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2321         eval $cmd
2322 }
2323 run_test 31n "check link count of unlinked file"
2324
2325 link_one() {
2326         local TEMPNAME=$(mktemp $1_XXXXXX)
2327         mlink $TEMPNAME $1 2> /dev/null &&
2328                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2329         munlink $TEMPNAME
2330 }
2331
2332 test_31o() { # LU-2901
2333         test_mkdir -p $DIR/$tdir
2334         for LOOP in $(seq 100); do
2335                 rm -f $DIR/$tdir/$tfile*
2336                 for THREAD in $(seq 8); do
2337                         link_one $DIR/$tdir/$tfile.$LOOP &
2338                 done
2339                 wait
2340                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2341                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2342                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2343                         break || true
2344         done
2345 }
2346 run_test 31o "duplicate hard links with same filename"
2347
2348 test_31p() {
2349         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2350
2351         test_mkdir $DIR/$tdir
2352         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2353         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2354
2355         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2356                 error "open unlink test1 failed"
2357         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2358                 error "open unlink test2 failed"
2359
2360         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2361                 error "test1 still exists"
2362         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2363                 error "test2 still exists"
2364 }
2365 run_test 31p "remove of open striped directory"
2366
2367 cleanup_test32_mount() {
2368         trap 0
2369         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2370 }
2371
2372 test_32a() {
2373         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2374         echo "== more mountpoints and symlinks ================="
2375         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2376         trap cleanup_test32_mount EXIT
2377         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2378         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2379         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2380         cleanup_test32_mount
2381 }
2382 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2383
2384 test_32b() {
2385         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2386         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2387         trap cleanup_test32_mount EXIT
2388         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2389         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2390         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2391         cleanup_test32_mount
2392 }
2393 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2394
2395 test_32c() {
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         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2403         cleanup_test32_mount
2404 }
2405 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2406
2407 test_32d() {
2408         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2409         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2410         trap cleanup_test32_mount EXIT
2411         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2412         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2413         test_mkdir -p $DIR/$tdir/d2/test_dir
2414         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2415         cleanup_test32_mount
2416 }
2417 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2418
2419 test_32e() {
2420         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2421         test_mkdir -p $DIR/d32e/tmp
2422         TMP_DIR=$DIR/d32e/tmp
2423         ln -s $DIR/d32e $TMP_DIR/symlink11
2424         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2425         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2426         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2427 }
2428 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2429
2430 test_32f() {
2431         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2432         test_mkdir -p $DIR/d32f/tmp
2433         TMP_DIR=$DIR/d32f/tmp
2434         ln -s $DIR/d32f $TMP_DIR/symlink11
2435         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2436         ls $DIR/d32f/tmp/symlink11  || error
2437         ls $DIR/d32f/symlink01 || error
2438 }
2439 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2440
2441 test_32g() {
2442         TMP_DIR=$DIR/$tdir/tmp
2443         test_mkdir -p $DIR/$tdir/tmp
2444         test_mkdir $DIR/${tdir}2
2445         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2446         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2447         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2448         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2449         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2450         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2451 }
2452 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2453
2454 test_32h() {
2455         rm -fr $DIR/$tdir $DIR/${tdir}2
2456         TMP_DIR=$DIR/$tdir/tmp
2457         test_mkdir -p $DIR/$tdir/tmp
2458         test_mkdir $DIR/${tdir}2
2459         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2460         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2461         ls $TMP_DIR/symlink12 || error
2462         ls $DIR/$tdir/symlink02  || error
2463 }
2464 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2465
2466 test_32i() {
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         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2474         cleanup_test32_mount
2475 }
2476 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2477
2478 test_32j() {
2479         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2480         [ -e $DIR/$tdir ] && 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 || error
2484         touch $DIR/$tdir/test_file
2485         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2486         cleanup_test32_mount
2487 }
2488 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2489
2490 test_32k() {
2491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2492         rm -fr $DIR/$tdir
2493         trap cleanup_test32_mount EXIT
2494         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2495         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2496         test_mkdir -p $DIR/$tdir/d2
2497         touch $DIR/$tdir/d2/test_file || error
2498         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2499         cleanup_test32_mount
2500 }
2501 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2502
2503 test_32l() {
2504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2505         rm -fr $DIR/$tdir
2506         trap cleanup_test32_mount EXIT
2507         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2508         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2509         test_mkdir -p $DIR/$tdir/d2
2510         touch $DIR/$tdir/d2/test_file
2511         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2512         cleanup_test32_mount
2513 }
2514 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2515
2516 test_32m() {
2517         rm -fr $DIR/d32m
2518         test_mkdir -p $DIR/d32m/tmp
2519         TMP_DIR=$DIR/d32m/tmp
2520         ln -s $DIR $TMP_DIR/symlink11
2521         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2522         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2523         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2524 }
2525 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2526
2527 test_32n() {
2528         rm -fr $DIR/d32n
2529         test_mkdir -p $DIR/d32n/tmp
2530         TMP_DIR=$DIR/d32n/tmp
2531         ln -s $DIR $TMP_DIR/symlink11
2532         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2533         ls -l $DIR/d32n/tmp/symlink11  || error
2534         ls -l $DIR/d32n/symlink01 || error
2535 }
2536 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2537
2538 test_32o() {
2539         touch $DIR/$tfile
2540         test_mkdir -p $DIR/d32o/tmp
2541         TMP_DIR=$DIR/d32o/tmp
2542         ln -s $DIR/$tfile $TMP_DIR/symlink12
2543         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2544         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2545         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2546         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2547         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2548 }
2549 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2550
2551 test_32p() {
2552     log 32p_1
2553         rm -fr $DIR/d32p
2554     log 32p_2
2555         rm -f $DIR/$tfile
2556     log 32p_3
2557         touch $DIR/$tfile
2558     log 32p_4
2559         test_mkdir -p $DIR/d32p/tmp
2560     log 32p_5
2561         TMP_DIR=$DIR/d32p/tmp
2562     log 32p_6
2563         ln -s $DIR/$tfile $TMP_DIR/symlink12
2564     log 32p_7
2565         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2566     log 32p_8
2567         cat $DIR/d32p/tmp/symlink12 || error
2568     log 32p_9
2569         cat $DIR/d32p/symlink02 || error
2570     log 32p_10
2571 }
2572 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2573
2574 cleanup_testdir_mount() {
2575         trap 0
2576         $UMOUNT -d $DIR/$tdir
2577 }
2578
2579 test_32q() {
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 "\<under_the_mount\>" && error
2587         cleanup_testdir_mount
2588 }
2589 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2590
2591 test_32r() {
2592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2593         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2594         trap cleanup_testdir_mount EXIT
2595         test_mkdir -p $DIR/$tdir
2596         touch $DIR/$tdir/under_the_mount
2597         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2598         ls $DIR/$tdir | grep -q under_the_mount && error || true
2599         cleanup_testdir_mount
2600 }
2601 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2602
2603 test_33aa() {
2604         rm -f $DIR/$tfile
2605         touch $DIR/$tfile
2606         chmod 444 $DIR/$tfile
2607         chown $RUNAS_ID $DIR/$tfile
2608         log 33_1
2609         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2610         log 33_2
2611 }
2612 run_test 33aa "write file with mode 444 (should return error) ===="
2613
2614 test_33a() {
2615         rm -fr $DIR/d33
2616         test_mkdir -p $DIR/d33
2617         chown $RUNAS_ID $DIR/d33
2618         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2619         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2620                 error "open RDWR" || true
2621 }
2622 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2623
2624 test_33b() {
2625         rm -fr $DIR/d33
2626         test_mkdir -p $DIR/d33
2627         chown $RUNAS_ID $DIR/d33
2628         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2629 }
2630 run_test 33b "test open file with malformed flags (No panic)"
2631
2632 test_33c() {
2633         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2634         local ostnum
2635         local ostname
2636         local write_bytes
2637         local all_zeros
2638
2639         remote_ost_nodsh && skip "remote OST with nodsh" && return
2640         all_zeros=:
2641         rm -fr $DIR/d33
2642         test_mkdir -p $DIR/d33
2643         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2644
2645         sync
2646         for ostnum in $(seq $OSTCOUNT); do
2647                 # test-framework's OST numbering is one-based, while Lustre's
2648                 # is zero-based
2649                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2650                 # Parsing llobdstat's output sucks; we could grep the /proc
2651                 # path, but that's likely to not be as portable as using the
2652                 # llobdstat utility.  So we parse lctl output instead.
2653                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2654                         obdfilter/$ostname/stats |
2655                         awk '/^write_bytes/ {print $7}' )
2656                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2657                 if (( ${write_bytes:-0} > 0 ))
2658                 then
2659                         all_zeros=false
2660                         break;
2661                 fi
2662         done
2663
2664         $all_zeros || return 0
2665
2666         # Write four bytes
2667         echo foo > $DIR/d33/bar
2668         # Really write them
2669         sync
2670
2671         # Total up write_bytes after writing.  We'd better find non-zeros.
2672         for ostnum in $(seq $OSTCOUNT); do
2673                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2674                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2675                         obdfilter/$ostname/stats |
2676                         awk '/^write_bytes/ {print $7}' )
2677                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2678                 if (( ${write_bytes:-0} > 0 ))
2679                 then
2680                         all_zeros=false
2681                         break;
2682                 fi
2683         done
2684
2685         if $all_zeros
2686         then
2687                 for ostnum in $(seq $OSTCOUNT); do
2688                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2689                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2690                         do_facet ost$ostnum lctl get_param -n \
2691                                 obdfilter/$ostname/stats
2692                 done
2693                 error "OST not keeping write_bytes stats (b22312)"
2694         fi
2695 }
2696 run_test 33c "test llobdstat and write_bytes"
2697
2698 test_33d() {
2699         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2701         local MDTIDX=1
2702         local remote_dir=$DIR/$tdir/remote_dir
2703
2704         test_mkdir -p $DIR/$tdir
2705         $LFS mkdir -i $MDTIDX $remote_dir ||
2706                 error "create remote directory failed"
2707
2708         touch $remote_dir/$tfile
2709         chmod 444 $remote_dir/$tfile
2710         chown $RUNAS_ID $remote_dir/$tfile
2711
2712         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2713
2714         chown $RUNAS_ID $remote_dir
2715         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2716                                         error "create" || true
2717         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2718                                     error "open RDWR" || true
2719         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2720 }
2721 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2722
2723 test_33e() {
2724         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2725
2726         mkdir $DIR/$tdir
2727
2728         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2729         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2730         mkdir $DIR/$tdir/local_dir
2731
2732         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2733         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2734         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2735
2736         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2737                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2738
2739         rmdir $DIR/$tdir/* || error "rmdir failed"
2740
2741         umask 777
2742         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2743         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2744         mkdir $DIR/$tdir/local_dir
2745
2746         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2747         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2748         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2749
2750         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2751                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2752
2753         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2754
2755         umask 000
2756         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2757         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2758         mkdir $DIR/$tdir/local_dir
2759
2760         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2761         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2762         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2763
2764         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2765                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2766 }
2767 run_test 33e "mkdir and striped directory should have same mode"
2768
2769 cleanup_33f() {
2770         trap 0
2771         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2772 }
2773
2774 test_33f() {
2775         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2776
2777         mkdir $DIR/$tdir
2778         chmod go+rwx $DIR/$tdir
2779         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2780         trap cleanup_33f EXIT
2781
2782         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2783                 error "cannot create striped directory"
2784
2785         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2786                 error "cannot create files in striped directory"
2787
2788         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2789                 error "cannot remove files in striped directory"
2790
2791         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2792                 error "cannot remove striped directory"
2793
2794         cleanup_33f
2795 }
2796 run_test 33f "nonroot user can create, access, and remove a striped directory"
2797
2798 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2799 test_34a() {
2800         rm -f $DIR/f34
2801         $MCREATE $DIR/f34 || error
2802         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2803         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2804         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2805         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2806 }
2807 run_test 34a "truncate file that has not been opened ==========="
2808
2809 test_34b() {
2810         [ ! -f $DIR/f34 ] && test_34a
2811         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2812         $OPENFILE -f O_RDONLY $DIR/f34
2813         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2814         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2815 }
2816 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2817
2818 test_34c() {
2819         [ ! -f $DIR/f34 ] && test_34a
2820         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2821         $OPENFILE -f O_RDWR $DIR/f34
2822         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2823         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2824 }
2825 run_test 34c "O_RDWR opening file-with-size works =============="
2826
2827 test_34d() {
2828         [ ! -f $DIR/f34 ] && test_34a
2829         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2830         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2831         rm $DIR/f34
2832 }
2833 run_test 34d "write to sparse file ============================="
2834
2835 test_34e() {
2836         rm -f $DIR/f34e
2837         $MCREATE $DIR/f34e || error
2838         $TRUNCATE $DIR/f34e 1000 || error
2839         $CHECKSTAT -s 1000 $DIR/f34e || error
2840         $OPENFILE -f O_RDWR $DIR/f34e
2841         $CHECKSTAT -s 1000 $DIR/f34e || error
2842 }
2843 run_test 34e "create objects, some with size and some without =="
2844
2845 test_34f() { # bug 6242, 6243
2846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2847         SIZE34F=48000
2848         rm -f $DIR/f34f
2849         $MCREATE $DIR/f34f || error
2850         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2851         dd if=$DIR/f34f of=$TMP/f34f
2852         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2853         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2854         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2855         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2856         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2857 }
2858 run_test 34f "read from a file with no objects until EOF ======="
2859
2860 test_34g() {
2861         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2862         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2863         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2864         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2865         cancel_lru_locks osc
2866         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2867                 error "wrong size after lock cancel"
2868
2869         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2870         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2871                 error "expanding truncate failed"
2872         cancel_lru_locks osc
2873         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2874                 error "wrong expanded size after lock cancel"
2875 }
2876 run_test 34g "truncate long file ==============================="
2877
2878 test_34h() {
2879         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2880         local gid=10
2881         local sz=1000
2882
2883         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2884         sync # Flush the cache so that multiop below does not block on cache
2885              # flush when getting the group lock
2886         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2887         MULTIPID=$!
2888
2889         # Since just timed wait is not good enough, let's do a sync write
2890         # that way we are sure enough time for a roundtrip + processing
2891         # passed + 2 seconds of extra margin.
2892         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2893         rm $DIR/${tfile}-1
2894         sleep 2
2895
2896         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2897                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2898                 kill -9 $MULTIPID
2899         fi
2900         wait $MULTIPID
2901         local nsz=`stat -c %s $DIR/$tfile`
2902         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2903 }
2904 run_test 34h "ftruncate file under grouplock should not block"
2905
2906 test_35a() {
2907         cp /bin/sh $DIR/f35a
2908         chmod 444 $DIR/f35a
2909         chown $RUNAS_ID $DIR/f35a
2910         $RUNAS $DIR/f35a && error || true
2911         rm $DIR/f35a
2912 }
2913 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2914
2915 test_36a() {
2916         rm -f $DIR/f36
2917         utime $DIR/f36 || error
2918 }
2919 run_test 36a "MDS utime check (mknod, utime) ==================="
2920
2921 test_36b() {
2922         echo "" > $DIR/f36
2923         utime $DIR/f36 || error
2924 }
2925 run_test 36b "OST utime check (open, utime) ===================="
2926
2927 test_36c() {
2928         rm -f $DIR/d36/f36
2929         test_mkdir $DIR/d36
2930         chown $RUNAS_ID $DIR/d36
2931         $RUNAS utime $DIR/d36/f36 || error
2932 }
2933 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2934
2935 test_36d() {
2936         [ ! -d $DIR/d36 ] && test_36c
2937         echo "" > $DIR/d36/f36
2938         $RUNAS utime $DIR/d36/f36 || error
2939 }
2940 run_test 36d "non-root OST utime check (open, utime) ==========="
2941
2942 test_36e() {
2943         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2944         test_mkdir -p $DIR/$tdir
2945         touch $DIR/$tdir/$tfile
2946         $RUNAS utime $DIR/$tdir/$tfile && \
2947                 error "utime worked, expected failure" || true
2948 }
2949 run_test 36e "utime on non-owned file (should return error) ===="
2950
2951 subr_36fh() {
2952         local fl="$1"
2953         local LANG_SAVE=$LANG
2954         local LC_LANG_SAVE=$LC_LANG
2955         export LANG=C LC_LANG=C # for date language
2956
2957         DATESTR="Dec 20  2000"
2958         test_mkdir -p $DIR/$tdir
2959         lctl set_param fail_loc=$fl
2960         date; date +%s
2961         cp /etc/hosts $DIR/$tdir/$tfile
2962         sync & # write RPC generated with "current" inode timestamp, but delayed
2963         sleep 1
2964         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2965         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2966         cancel_lru_locks osc
2967         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2968         date; date +%s
2969         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2970                 echo "BEFORE: $LS_BEFORE" && \
2971                 echo "AFTER : $LS_AFTER" && \
2972                 echo "WANT  : $DATESTR" && \
2973                 error "$DIR/$tdir/$tfile timestamps changed" || true
2974
2975         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2976 }
2977
2978 test_36f() {
2979         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2980         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2981         subr_36fh "0x80000214"
2982 }
2983 run_test 36f "utime on file racing with OST BRW write =========="
2984
2985 test_36g() {
2986         remote_ost_nodsh && skip "remote OST with nodsh" && return
2987         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2988         local fmd_max_age
2989         local fmd_before
2990         local fmd_after
2991
2992         test_mkdir -p $DIR/$tdir
2993         fmd_max_age=$(do_facet ost1 \
2994                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2995                 head -n 1")
2996
2997         fmd_before=$(do_facet ost1 \
2998                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2999         touch $DIR/$tdir/$tfile
3000         sleep $((fmd_max_age + 12))
3001         fmd_after=$(do_facet ost1 \
3002                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3003
3004         echo "fmd_before: $fmd_before"
3005         echo "fmd_after: $fmd_after"
3006         [[ $fmd_after -gt $fmd_before ]] &&
3007                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3008                 error "fmd didn't expire after ping" || true
3009 }
3010 run_test 36g "filter mod data cache expiry ====================="
3011
3012 test_36h() {
3013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3014         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3015         subr_36fh "0x80000227"
3016 }
3017 run_test 36h "utime on file racing with OST BRW write =========="
3018
3019 test_36i() {
3020         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3021
3022         test_mkdir $DIR/$tdir
3023         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3024
3025         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3026         local new_mtime=$((mtime + 200))
3027
3028         #change Modify time of striped dir
3029         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3030                         error "change mtime failed"
3031
3032         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3033
3034         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3035 }
3036 run_test 36i "change mtime on striped directory"
3037
3038 # test_37 - duplicate with tests 32q 32r
3039
3040 test_38() {
3041         local file=$DIR/$tfile
3042         touch $file
3043         openfile -f O_DIRECTORY $file
3044         local RC=$?
3045         local ENOTDIR=20
3046         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3047         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3048 }
3049 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3050
3051 test_39() {
3052         touch $DIR/$tfile
3053         touch $DIR/${tfile}2
3054 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3055 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3056 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3057         sleep 2
3058         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3059         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3060                 echo "mtime"
3061                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3062                 echo "atime"
3063                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3064                 echo "ctime"
3065                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3066                 error "O_TRUNC didn't change timestamps"
3067         fi
3068 }
3069 run_test 39 "mtime changed on create ==========================="
3070
3071 test_39b() {
3072         test_mkdir -p -c1 $DIR/$tdir
3073         cp -p /etc/passwd $DIR/$tdir/fopen
3074         cp -p /etc/passwd $DIR/$tdir/flink
3075         cp -p /etc/passwd $DIR/$tdir/funlink
3076         cp -p /etc/passwd $DIR/$tdir/frename
3077         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3078
3079         sleep 1
3080         echo "aaaaaa" >> $DIR/$tdir/fopen
3081         echo "aaaaaa" >> $DIR/$tdir/flink
3082         echo "aaaaaa" >> $DIR/$tdir/funlink
3083         echo "aaaaaa" >> $DIR/$tdir/frename
3084
3085         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3086         local link_new=`stat -c %Y $DIR/$tdir/flink`
3087         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3088         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3089
3090         cat $DIR/$tdir/fopen > /dev/null
3091         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3092         rm -f $DIR/$tdir/funlink2
3093         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3094
3095         for (( i=0; i < 2; i++ )) ; do
3096                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3097                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3098                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3099                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3100
3101                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3102                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3103                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3104                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3105
3106                 cancel_lru_locks osc
3107                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3108         done
3109 }
3110 run_test 39b "mtime change on open, link, unlink, rename  ======"
3111
3112 # this should be set to past
3113 TEST_39_MTIME=`date -d "1 year ago" +%s`
3114
3115 # bug 11063
3116 test_39c() {
3117         touch $DIR1/$tfile
3118         sleep 2
3119         local mtime0=`stat -c %Y $DIR1/$tfile`
3120
3121         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3122         local mtime1=`stat -c %Y $DIR1/$tfile`
3123         [ "$mtime1" = $TEST_39_MTIME ] || \
3124                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3125
3126         local d1=`date +%s`
3127         echo hello >> $DIR1/$tfile
3128         local d2=`date +%s`
3129         local mtime2=`stat -c %Y $DIR1/$tfile`
3130         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3131                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3132
3133         mv $DIR1/$tfile $DIR1/$tfile-1
3134
3135         for (( i=0; i < 2; i++ )) ; do
3136                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3137                 [ "$mtime2" = "$mtime3" ] || \
3138                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3139
3140                 cancel_lru_locks osc
3141                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3142         done
3143 }
3144 run_test 39c "mtime change on rename ==========================="
3145
3146 # bug 21114
3147 test_39d() {
3148         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3149         touch $DIR1/$tfile
3150
3151         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3152
3153         for (( i=0; i < 2; i++ )) ; do
3154                 local mtime=`stat -c %Y $DIR1/$tfile`
3155                 [ $mtime = $TEST_39_MTIME ] || \
3156                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3157
3158                 cancel_lru_locks osc
3159                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3160         done
3161 }
3162 run_test 39d "create, utime, stat =============================="
3163
3164 # bug 21114
3165 test_39e() {
3166         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3167         touch $DIR1/$tfile
3168         local mtime1=`stat -c %Y $DIR1/$tfile`
3169
3170         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3171
3172         for (( i=0; i < 2; i++ )) ; do
3173                 local mtime2=`stat -c %Y $DIR1/$tfile`
3174                 [ $mtime2 = $TEST_39_MTIME ] || \
3175                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3176
3177                 cancel_lru_locks osc
3178                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3179         done
3180 }
3181 run_test 39e "create, stat, utime, stat ========================"
3182
3183 # bug 21114
3184 test_39f() {
3185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3186         touch $DIR1/$tfile
3187         mtime1=`stat -c %Y $DIR1/$tfile`
3188
3189         sleep 2
3190         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3191
3192         for (( i=0; i < 2; i++ )) ; do
3193                 local mtime2=`stat -c %Y $DIR1/$tfile`
3194                 [ $mtime2 = $TEST_39_MTIME ] || \
3195                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3196
3197                 cancel_lru_locks osc
3198                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3199         done
3200 }
3201 run_test 39f "create, stat, sleep, utime, stat ================="
3202
3203 # bug 11063
3204 test_39g() {
3205         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3206         echo hello >> $DIR1/$tfile
3207         local mtime1=`stat -c %Y $DIR1/$tfile`
3208
3209         sleep 2
3210         chmod o+r $DIR1/$tfile
3211
3212         for (( i=0; i < 2; i++ )) ; do
3213                 local mtime2=`stat -c %Y $DIR1/$tfile`
3214                 [ "$mtime1" = "$mtime2" ] || \
3215                         error "lost mtime: $mtime2, should be $mtime1"
3216
3217                 cancel_lru_locks osc
3218                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3219         done
3220 }
3221 run_test 39g "write, chmod, stat ==============================="
3222
3223 # bug 11063
3224 test_39h() {
3225         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3226         touch $DIR1/$tfile
3227         sleep 1
3228
3229         local d1=`date`
3230         echo hello >> $DIR1/$tfile
3231         local mtime1=`stat -c %Y $DIR1/$tfile`
3232
3233         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3234         local d2=`date`
3235         if [ "$d1" != "$d2" ]; then
3236                 echo "write and touch not within one second"
3237         else
3238                 for (( i=0; i < 2; i++ )) ; do
3239                         local mtime2=`stat -c %Y $DIR1/$tfile`
3240                         [ "$mtime2" = $TEST_39_MTIME ] || \
3241                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3242
3243                         cancel_lru_locks osc
3244                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3245                 done
3246         fi
3247 }
3248 run_test 39h "write, utime within one second, stat ============="
3249
3250 test_39i() {
3251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3252         touch $DIR1/$tfile
3253         sleep 1
3254
3255         echo hello >> $DIR1/$tfile
3256         local mtime1=`stat -c %Y $DIR1/$tfile`
3257
3258         mv $DIR1/$tfile $DIR1/$tfile-1
3259
3260         for (( i=0; i < 2; i++ )) ; do
3261                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3262
3263                 [ "$mtime1" = "$mtime2" ] || \
3264                         error "lost mtime: $mtime2, should be $mtime1"
3265
3266                 cancel_lru_locks osc
3267                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3268         done
3269 }
3270 run_test 39i "write, rename, stat =============================="
3271
3272 test_39j() {
3273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3274         start_full_debug_logging
3275         touch $DIR1/$tfile
3276         sleep 1
3277
3278         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3279         lctl set_param fail_loc=0x80000412
3280         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3281                 error "multiop failed"
3282         local multipid=$!
3283         local mtime1=`stat -c %Y $DIR1/$tfile`
3284
3285         mv $DIR1/$tfile $DIR1/$tfile-1
3286
3287         kill -USR1 $multipid
3288         wait $multipid || error "multiop close failed"
3289
3290         for (( i=0; i < 2; i++ )) ; do
3291                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3292                 [ "$mtime1" = "$mtime2" ] ||
3293                         error "mtime is lost on close: $mtime2, " \
3294                               "should be $mtime1"
3295
3296                 cancel_lru_locks osc
3297                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3298         done
3299         lctl set_param fail_loc=0
3300         stop_full_debug_logging
3301 }
3302 run_test 39j "write, rename, close, stat ======================="
3303
3304 test_39k() {
3305         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3306         touch $DIR1/$tfile
3307         sleep 1
3308
3309         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3310         local multipid=$!
3311         local mtime1=`stat -c %Y $DIR1/$tfile`
3312
3313         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3314
3315         kill -USR1 $multipid
3316         wait $multipid || error "multiop close failed"
3317
3318         for (( i=0; i < 2; i++ )) ; do
3319                 local mtime2=`stat -c %Y $DIR1/$tfile`
3320
3321                 [ "$mtime2" = $TEST_39_MTIME ] || \
3322                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3323
3324                 cancel_lru_locks osc
3325                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3326         done
3327 }
3328 run_test 39k "write, utime, close, stat ========================"
3329
3330 # this should be set to future
3331 TEST_39_ATIME=`date -d "1 year" +%s`
3332
3333 test_39l() {
3334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3335         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3336         local atime_diff=$(do_facet $SINGLEMDS \
3337                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3338         rm -rf $DIR/$tdir
3339         mkdir -p $DIR/$tdir
3340
3341         # test setting directory atime to future
3342         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3343         local atime=$(stat -c %X $DIR/$tdir)
3344         [ "$atime" = $TEST_39_ATIME ] || \
3345                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3346
3347         # test setting directory atime from future to now
3348         local d1=$(date +%s)
3349         ls $DIR/$tdir
3350         local d2=$(date +%s)
3351
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 from future: $atime, $d1<atime<$d2"
3356
3357         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3358         sleep 3
3359
3360         # test setting directory atime when now > dir atime + atime_diff
3361         d1=$(date +%s)
3362         ls $DIR/$tdir
3363         d2=$(date +%s)
3364         cancel_lru_locks mdc
3365         atime=$(stat -c %X $DIR/$tdir)
3366         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3367                 error "atime is not updated  : $atime, should be $d2"
3368
3369         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3370         sleep 3
3371
3372         # test not setting directory atime when now < dir atime + atime_diff
3373         ls $DIR/$tdir
3374         cancel_lru_locks mdc
3375         atime=$(stat -c %X $DIR/$tdir)
3376         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3377                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3378
3379         do_facet $SINGLEMDS \
3380                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3381 }
3382 run_test 39l "directory atime update ==========================="
3383
3384 test_39m() {
3385         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3386         touch $DIR1/$tfile
3387         sleep 2
3388         local far_past_mtime=$(date -d "May 29 1953" +%s)
3389         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3390
3391         touch -m -d @$far_past_mtime $DIR1/$tfile
3392         touch -a -d @$far_past_atime $DIR1/$tfile
3393
3394         for (( i=0; i < 2; i++ )) ; do
3395                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3396                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3397                         error "atime or mtime set incorrectly"
3398
3399                 cancel_lru_locks osc
3400                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3401         done
3402 }
3403 run_test 39m "test atime and mtime before 1970"
3404
3405 test_39n() { # LU-3832
3406         local atime_diff=$(do_facet $SINGLEMDS \
3407                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3408         local atime0
3409         local atime1
3410         local atime2
3411
3412         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3413
3414         rm -rf $DIR/$tfile
3415         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3416         atime0=$(stat -c %X $DIR/$tfile)
3417
3418         sleep 5
3419         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3420         atime1=$(stat -c %X $DIR/$tfile)
3421
3422         sleep 5
3423         cancel_lru_locks mdc
3424         cancel_lru_locks osc
3425         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3426         atime2=$(stat -c %X $DIR/$tfile)
3427
3428         do_facet $SINGLEMDS \
3429                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3430
3431         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3432         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3433 }
3434 run_test 39n "check that O_NOATIME is honored"
3435
3436 test_39o() {
3437         TESTDIR=$DIR/$tdir/$tfile
3438         [ -e $TESTDIR ] && rm -rf $TESTDIR
3439         mkdir -p $TESTDIR
3440         cd $TESTDIR
3441         links1=2
3442         ls
3443         mkdir a b
3444         ls
3445         links2=$(stat -c %h .)
3446         [ $(($links1 + 2)) != $links2 ] &&
3447                 error "wrong links count $(($links1 + 2)) != $links2"
3448         rmdir b
3449         links3=$(stat -c %h .)
3450         [ $(($links1 + 1)) != $links3 ] &&
3451                 error "wrong links count $links1 != $links3"
3452         return 0
3453 }
3454 run_test 39o "directory cached attributes updated after create ========"
3455
3456 test_39p() {
3457         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3458         local MDTIDX=1
3459         TESTDIR=$DIR/$tdir/$tfile
3460         [ -e $TESTDIR ] && rm -rf $TESTDIR
3461         test_mkdir -p $TESTDIR
3462         cd $TESTDIR
3463         links1=2
3464         ls
3465         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3466         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3467         ls
3468         links2=$(stat -c %h .)
3469         [ $(($links1 + 2)) != $links2 ] &&
3470                 error "wrong links count $(($links1 + 2)) != $links2"
3471         rmdir remote_dir2
3472         links3=$(stat -c %h .)
3473         [ $(($links1 + 1)) != $links3 ] &&
3474                 error "wrong links count $links1 != $links3"
3475         return 0
3476 }
3477 run_test 39p "remote directory cached attributes updated after create ========"
3478
3479
3480 test_40() {
3481         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3482         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3483                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3484         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3485                 error "$tfile is not 4096 bytes in size"
3486 }
3487 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3488
3489 test_41() {
3490         # bug 1553
3491         small_write $DIR/f41 18
3492 }
3493 run_test 41 "test small file write + fstat ====================="
3494
3495 count_ost_writes() {
3496         lctl get_param -n osc.*.stats |
3497                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3498                         END { printf("%0.0f", writes) }'
3499 }
3500
3501 # decent default
3502 WRITEBACK_SAVE=500
3503 DIRTY_RATIO_SAVE=40
3504 MAX_DIRTY_RATIO=50
3505 BG_DIRTY_RATIO_SAVE=10
3506 MAX_BG_DIRTY_RATIO=25
3507
3508 start_writeback() {
3509         trap 0
3510         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3511         # dirty_ratio, dirty_background_ratio
3512         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3513                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3514                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3515                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3516         else
3517                 # if file not here, we are a 2.4 kernel
3518                 kill -CONT `pidof kupdated`
3519         fi
3520 }
3521
3522 stop_writeback() {
3523         # setup the trap first, so someone cannot exit the test at the
3524         # exact wrong time and mess up a machine
3525         trap start_writeback EXIT
3526         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3527         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3528                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3529                 sysctl -w vm.dirty_writeback_centisecs=0
3530                 sysctl -w vm.dirty_writeback_centisecs=0
3531                 # save and increase /proc/sys/vm/dirty_ratio
3532                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3533                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3534                 # save and increase /proc/sys/vm/dirty_background_ratio
3535                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3536                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3537         else
3538                 # if file not here, we are a 2.4 kernel
3539                 kill -STOP `pidof kupdated`
3540         fi
3541 }
3542
3543 # ensure that all stripes have some grant before we test client-side cache
3544 setup_test42() {
3545         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3546                 dd if=/dev/zero of=$i bs=4k count=1
3547                 rm $i
3548         done
3549 }
3550
3551 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3552 # file truncation, and file removal.
3553 test_42a() {
3554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3555         setup_test42
3556         cancel_lru_locks osc
3557         stop_writeback
3558         sync; sleep 1; sync # just to be safe
3559         BEFOREWRITES=`count_ost_writes`
3560         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3561         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3562         AFTERWRITES=`count_ost_writes`
3563         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3564                 error "$BEFOREWRITES < $AFTERWRITES"
3565         start_writeback
3566 }
3567 run_test 42a "ensure that we don't flush on close =============="
3568
3569 test_42b() {
3570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3571         setup_test42
3572         cancel_lru_locks osc
3573         stop_writeback
3574         sync
3575         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3576         BEFOREWRITES=$(count_ost_writes)
3577         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3578         AFTERWRITES=$(count_ost_writes)
3579         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3580                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3581         fi
3582         BEFOREWRITES=$(count_ost_writes)
3583         sync || error "sync: $?"
3584         AFTERWRITES=$(count_ost_writes)
3585         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3586                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3587         fi
3588         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3589         start_writeback
3590         return 0
3591 }
3592 run_test 42b "test destroy of file with cached dirty data ======"
3593
3594 # if these tests just want to test the effect of truncation,
3595 # they have to be very careful.  consider:
3596 # - the first open gets a {0,EOF}PR lock
3597 # - the first write conflicts and gets a {0, count-1}PW
3598 # - the rest of the writes are under {count,EOF}PW
3599 # - the open for truncate tries to match a {0,EOF}PR
3600 #   for the filesize and cancels the PWs.
3601 # any number of fixes (don't get {0,EOF} on open, match
3602 # composite locks, do smarter file size management) fix
3603 # this, but for now we want these tests to verify that
3604 # the cancellation with truncate intent works, so we
3605 # start the file with a full-file pw lock to match against
3606 # until the truncate.
3607 trunc_test() {
3608         test=$1
3609         file=$DIR/$test
3610         offset=$2
3611         cancel_lru_locks osc
3612         stop_writeback
3613         # prime the file with 0,EOF PW to match
3614         touch $file
3615         $TRUNCATE $file 0
3616         sync; sync
3617         # now the real test..
3618         dd if=/dev/zero of=$file bs=1024 count=100
3619         BEFOREWRITES=`count_ost_writes`
3620         $TRUNCATE $file $offset
3621         cancel_lru_locks osc
3622         AFTERWRITES=`count_ost_writes`
3623         start_writeback
3624 }
3625
3626 test_42c() {
3627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3628         trunc_test 42c 1024
3629         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3630             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3631         rm $file
3632 }
3633 run_test 42c "test partial truncate of file with cached dirty data"
3634
3635 test_42d() {
3636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3637         trunc_test 42d 0
3638         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3639             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3640         rm $file
3641 }
3642 run_test 42d "test complete truncate of file with cached dirty data"
3643
3644 test_42e() { # bug22074
3645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3646         local TDIR=$DIR/${tdir}e
3647         local pagesz=$(page_size)
3648         local pages=16 # hardcoded 16 pages, don't change it.
3649         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3650         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3651         local max_dirty_mb
3652         local warmup_files
3653
3654         test_mkdir -p $DIR/${tdir}e
3655         $SETSTRIPE -c 1 $TDIR
3656         createmany -o $TDIR/f $files
3657
3658         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3659
3660         # we assume that with $OSTCOUNT files, at least one of them will
3661         # be allocated on OST0.
3662         warmup_files=$((OSTCOUNT * max_dirty_mb))
3663         createmany -o $TDIR/w $warmup_files
3664
3665         # write a large amount of data into one file and sync, to get good
3666         # avail_grant number from OST.
3667         for ((i=0; i<$warmup_files; i++)); do
3668                 idx=$($GETSTRIPE -i $TDIR/w$i)
3669                 [ $idx -ne 0 ] && continue
3670                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3671                 break
3672         done
3673         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3674         sync
3675         $LCTL get_param $proc_osc0/cur_dirty_bytes
3676         $LCTL get_param $proc_osc0/cur_grant_bytes
3677
3678         # create as much dirty pages as we can while not to trigger the actual
3679         # RPCs directly. but depends on the env, VFS may trigger flush during this
3680         # period, hopefully we are good.
3681         for ((i=0; i<$warmup_files; i++)); do
3682                 idx=$($GETSTRIPE -i $TDIR/w$i)
3683                 [ $idx -ne 0 ] && continue
3684                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3685         done
3686         $LCTL get_param $proc_osc0/cur_dirty_bytes
3687         $LCTL get_param $proc_osc0/cur_grant_bytes
3688
3689         # perform the real test
3690         $LCTL set_param $proc_osc0/rpc_stats 0
3691         for ((;i<$files; i++)); do
3692                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3693                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3694         done
3695         sync
3696         $LCTL get_param $proc_osc0/rpc_stats
3697
3698         local percent=0
3699         local have_ppr=false
3700         $LCTL get_param $proc_osc0/rpc_stats |
3701                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3702                         # skip lines until we are at the RPC histogram data
3703                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3704                         $have_ppr || continue
3705
3706                         # we only want the percent stat for < 16 pages
3707                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3708
3709                         percent=$((percent + WPCT))
3710                         if [[ $percent -gt 15 ]]; then
3711                                 error "less than 16-pages write RPCs" \
3712                                       "$percent% > 15%"
3713                                 break
3714                         fi
3715                 done
3716         rm -rf $TDIR
3717 }
3718 run_test 42e "verify sub-RPC writes are not done synchronously"
3719
3720 test_43() {
3721         test_mkdir -p $DIR/$tdir
3722         cp -p /bin/ls $DIR/$tdir/$tfile
3723         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3724         pid=$!
3725         # give multiop a chance to open
3726         sleep 1
3727
3728         $DIR/$tdir/$tfile && error || true
3729         kill -USR1 $pid
3730 }
3731 run_test 43 "execution of file opened for write should return -ETXTBSY"
3732
3733 test_43a() {
3734         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3735         test_mkdir -p $DIR/$tdir
3736         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3737                         cp -p multiop $DIR/$tdir/multiop
3738         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3739                         return 1
3740         MULTIOP_PID=$!
3741         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3742         kill -USR1 $MULTIOP_PID || return 2
3743         wait $MULTIOP_PID || return 3
3744         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3745 }
3746 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3747
3748 test_43b() {
3749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3750         test_mkdir -p $DIR/$tdir
3751         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3752                         cp -p multiop $DIR/$tdir/multiop
3753         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3754                         return 1
3755         MULTIOP_PID=$!
3756         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3757         kill -USR1 $MULTIOP_PID || return 2
3758         wait $MULTIOP_PID || return 3
3759         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3760 }
3761 run_test 43b "truncate of file being executed should return -ETXTBSY"
3762
3763 test_43c() {
3764         local testdir="$DIR/$tdir"
3765         test_mkdir -p $DIR/$tdir
3766         cp $SHELL $testdir/
3767         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3768                 ( cd $testdir && md5sum -c)
3769 }
3770 run_test 43c "md5sum of copy into lustre========================"
3771
3772 test_44() {
3773         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3774         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3775         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3776 }
3777 run_test 44 "zero length read from a sparse stripe ============="
3778
3779 test_44a() {
3780         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3781                 awk '{ print $2 }')
3782         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3783         [[ $nstripe -gt $OSTCOUNT ]] &&
3784             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3785             return
3786         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3787                 awk '{ print $2 }')
3788         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3789                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3790                         awk '{ print $2 }')
3791         fi
3792
3793         OFFSETS="0 $((stride/2)) $((stride-1))"
3794         for offset in $OFFSETS; do
3795                 for i in $(seq 0 $((nstripe-1))); do
3796                         local GLOBALOFFSETS=""
3797                         # size in Bytes
3798                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3799                         local myfn=$DIR/d44a-$size
3800                         echo "--------writing $myfn at $size"
3801                         ll_sparseness_write $myfn $size ||
3802                                 error "ll_sparseness_write"
3803                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3804                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3805                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3806
3807                         for j in $(seq 0 $((nstripe-1))); do
3808                                 # size in Bytes
3809                                 size=$((((j + $nstripe )*$stride + $offset)))
3810                                 ll_sparseness_write $myfn $size ||
3811                                         error "ll_sparseness_write"
3812                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3813                         done
3814                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3815                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3816                         rm -f $myfn
3817                 done
3818         done
3819 }
3820 run_test 44a "test sparse pwrite ==============================="
3821
3822 dirty_osc_total() {
3823         tot=0
3824         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3825                 tot=$(($tot + $d))
3826         done
3827         echo $tot
3828 }
3829 do_dirty_record() {
3830         before=`dirty_osc_total`
3831         echo executing "\"$*\""
3832         eval $*
3833         after=`dirty_osc_total`
3834         echo before $before, after $after
3835 }
3836 test_45() {
3837         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3838         f="$DIR/f45"
3839         # Obtain grants from OST if it supports it
3840         echo blah > ${f}_grant
3841         stop_writeback
3842         sync
3843         do_dirty_record "echo blah > $f"
3844         [[ $before -eq $after ]] && error "write wasn't cached"
3845         do_dirty_record "> $f"
3846         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3847         do_dirty_record "echo blah > $f"
3848         [[ $before -eq $after ]] && error "write wasn't cached"
3849         do_dirty_record "sync"
3850         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3851         do_dirty_record "echo blah > $f"
3852         [[ $before -eq $after ]] && error "write wasn't cached"
3853         do_dirty_record "cancel_lru_locks osc"
3854         [[ $before -gt $after ]] ||
3855                 error "lock cancellation didn't lower dirty count"
3856         start_writeback
3857 }
3858 run_test 45 "osc io page accounting ============================"
3859
3860 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3861 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3862 # objects offset and an assert hit when an rpc was built with 1023's mapped
3863 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3864 test_46() {
3865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3866         f="$DIR/f46"
3867         stop_writeback
3868         sync
3869         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3870         sync
3871         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3872         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3873         sync
3874         start_writeback
3875 }
3876 run_test 46 "dirtying a previously written page ================"
3877
3878 # test_47 is removed "Device nodes check" is moved to test_28
3879
3880 test_48a() { # bug 2399
3881         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3882         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3883                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3884                 return
3885         test_mkdir $DIR/$tdir
3886         cd $DIR/$tdir
3887         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3888         test_mkdir $DIR/$tdir || error "recreate directory failed"
3889         touch foo || error "'touch foo' failed after recreating cwd"
3890         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
3891         touch .foo || error "'touch .foo' failed after recreating cwd"
3892         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3893         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3894         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3895         cd . || error "'cd .' failed after recreating cwd"
3896         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3897         rmdir . && error "'rmdir .' worked after recreating cwd"
3898         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3899         cd .. || error "'cd ..' failed after recreating cwd"
3900 }
3901 run_test 48a "Access renamed working dir (should return errors)="
3902
3903 test_48b() { # bug 2399
3904         rm -rf $DIR/$tdir
3905         test_mkdir $DIR/$tdir
3906         cd $DIR/$tdir
3907         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3908         touch foo && error "'touch foo' worked after removing cwd"
3909         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3910         touch .foo && error "'touch .foo' worked after removing cwd"
3911         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3912         ls . > /dev/null && error "'ls .' worked after removing cwd"
3913         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3914         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3915         test_mkdir . && error "'mkdir .' worked after removing cwd"
3916         rmdir . && error "'rmdir .' worked after removing cwd"
3917         ln -s . foo && error "'ln -s .' worked after removing cwd"
3918         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3919 }
3920 run_test 48b "Access removed working dir (should return errors)="
3921
3922 test_48c() { # bug 2350
3923         #lctl set_param debug=-1
3924         #set -vx
3925         rm -rf $DIR/$tdir
3926         test_mkdir -p $DIR/$tdir/dir
3927         cd $DIR/$tdir/dir
3928         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3929         $TRACE touch foo && error "touch foo worked after removing cwd"
3930         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3931         touch .foo && error "touch .foo worked after removing cwd"
3932         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3933         $TRACE ls . && error "'ls .' worked after removing cwd"
3934         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3935         is_patchless || ( $TRACE cd . &&
3936                         error "'cd .' worked after removing cwd" )
3937         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3938         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3939         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3940         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3941 }
3942 run_test 48c "Access removed working subdir (should return errors)"
3943
3944 test_48d() { # bug 2350
3945         #lctl set_param debug=-1
3946         #set -vx
3947         rm -rf $DIR/$tdir
3948         test_mkdir -p $DIR/$tdir/dir
3949         cd $DIR/$tdir/dir
3950         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3951         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3952         $TRACE touch foo && error "'touch foo' worked after removing parent"
3953         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3954         touch .foo && error "'touch .foo' worked after removing parent"
3955         test_mkdir .foo && error "mkdir .foo worked after removing parent"
3956         $TRACE ls . && error "'ls .' worked after removing parent"
3957         $TRACE ls .. && error "'ls ..' worked after removing parent"
3958         is_patchless || ( $TRACE cd . &&
3959                         error "'cd .' worked after recreate parent" )
3960         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3961         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3962         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3963         is_patchless || ( $TRACE cd .. &&
3964                         error "'cd ..' worked after removing parent" || true )
3965 }
3966 run_test 48d "Access removed parent subdir (should return errors)"
3967
3968 test_48e() { # bug 4134
3969         #lctl set_param debug=-1
3970         #set -vx
3971         rm -rf $DIR/$tdir
3972         test_mkdir -p $DIR/$tdir/dir
3973         cd $DIR/$tdir/dir
3974         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3975         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3976         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3977         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3978         # On a buggy kernel addition of "touch foo" after cd .. will
3979         # produce kernel oops in lookup_hash_it
3980         touch ../foo && error "'cd ..' worked after recreate parent"
3981         cd $DIR
3982         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3983 }
3984 run_test 48e "Access to recreated parent subdir (should return errors)"
3985
3986 test_49() { # LU-1030
3987         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3988         # get ost1 size - lustre-OST0000
3989         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3990                 awk '{ print $4 }')
3991         # write 800M at maximum
3992         [[ $ost1_size -lt 2 ]] && ost1_size=2
3993         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3994
3995         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3996         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3997         local dd_pid=$!
3998
3999         # change max_pages_per_rpc while writing the file
4000         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4001         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4002         # loop until dd process exits
4003         while ps ax -opid | grep -wq $dd_pid; do
4004                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4005                 sleep $((RANDOM % 5 + 1))
4006         done
4007         # restore original max_pages_per_rpc
4008         $LCTL set_param $osc1_mppc=$orig_mppc
4009         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4010 }
4011 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4012
4013 test_50() {
4014         # bug 1485
4015         test_mkdir $DIR/$tdir
4016         cd $DIR/$tdir
4017         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4018 }
4019 run_test 50 "special situations: /proc symlinks  ==============="
4020
4021 test_51a() {    # was test_51
4022         # bug 1516 - create an empty entry right after ".." then split dir
4023         test_mkdir -c1 $DIR/$tdir
4024         touch $DIR/$tdir/foo
4025         $MCREATE $DIR/$tdir/bar
4026         rm $DIR/$tdir/foo
4027         createmany -m $DIR/$tdir/longfile 201
4028         FNUM=202
4029         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4030                 $MCREATE $DIR/$tdir/longfile$FNUM
4031                 FNUM=$(($FNUM + 1))
4032                 echo -n "+"
4033         done
4034         echo
4035         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4036 }
4037 run_test 51a "special situations: split htree with empty entry =="
4038
4039 export NUMTEST=70000
4040 test_51b() {
4041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4042         local BASE=$DIR/d${base}.${TESTSUITE}
4043
4044         # cleanup the directory
4045         rm -fr $BASE
4046
4047         test_mkdir -p -c1 $BASE
4048
4049         $LFS df
4050         $LFS df -i
4051         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4052         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4053         [[ $numfree -lt 21000 ]] &&
4054                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4055                 return
4056
4057         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4058                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4059
4060         # need to check free space for the directories as well
4061         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4062         numfree=$((blkfree / 4))
4063         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4064                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4065
4066         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4067         {
4068                 $LFS df
4069                 $LFS df -i
4070                 echo "failed" > $BASE/fnum
4071                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4072         }
4073 }
4074 run_test 51b "exceed 64k subdirectory nlink limit"
4075
4076 test_51ba() { # LU-993
4077         local BASE=$DIR/d${base}.${TESTSUITE}
4078         # unlink all but 100 subdirectories, then check it still works
4079         local LEFT=100
4080         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4081
4082         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4083         local DELETE=$((NUMTEST - LEFT))
4084
4085         # continue on to run this test even if 51b didn't finish,
4086         # just to delete the many subdirectories created.
4087         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4088
4089         # for ldiskfs the nlink count should be 1, but this is OSD specific
4090         # and so this is listed for informational purposes only
4091         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4092         unlinkmany -d $BASE/d $DELETE
4093         RC=$?
4094
4095         if [ $RC -ne 0 ]; then
4096                 if [ "$NUMPREV" == "failed" ]; then
4097                         skip "previous setup failed"
4098                         return 0
4099                 else
4100                         error "unlink of first $DELETE subdirs failed"
4101                         return $RC
4102                 fi
4103         fi
4104
4105         echo "nlink between: $(stat -c %h $BASE)"
4106         # trim the first line of ls output
4107         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4108         [ $FOUND -ne $LEFT ] &&
4109                 error "can't find subdirs: found only $FOUND/$LEFT"
4110
4111         unlinkmany -d $BASE/d $DELETE $LEFT ||
4112                 error "unlink of second $LEFT subdirs failed"
4113         # regardless of whether the backing filesystem tracks nlink accurately
4114         # or not, the nlink count shouldn't be more than "." and ".." here
4115         local AFTER=$(stat -c %h $BASE)
4116         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4117                 echo "nlink after: $AFTER"
4118 }
4119 run_test 51ba "verify nlink for many subdirectory cleanup"
4120
4121 test_51d() {
4122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4123         [[ $OSTCOUNT -lt 3 ]] &&
4124                 skip_env "skipping test with few OSTs" && return
4125         test_mkdir -p $DIR/$tdir
4126         createmany -o $DIR/$tdir/t- 1000
4127         $GETSTRIPE $DIR/$tdir > $TMP/files
4128         for N in $(seq 0 $((OSTCOUNT - 1))); do
4129                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4130                         END { printf("%0.0f", objs) }' $TMP/files)
4131                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4132                         '($1 == '$N') { objs += 1 } \
4133                         END { printf("%0.0f", objs) }')
4134                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4135         done
4136         unlinkmany $DIR/$tdir/t- 1000
4137
4138         NLAST=0
4139         for N in $(seq 1 $((OSTCOUNT - 1))); do
4140                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4141                         error "OST $N has less objects vs OST $NLAST" \
4142                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4143                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4144                         error "OST $N has less objects vs OST $NLAST" \
4145                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4146
4147                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4148                         error "OST $N has less #0 objects vs OST $NLAST" \
4149                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4150                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4151                         error "OST $N has less #0 objects vs OST $NLAST" \
4152                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4153                 NLAST=$N
4154         done
4155 }
4156 run_test 51d "check object distribution ===================="
4157
4158 test_51e() {
4159         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4160                 skip "Only applicable to ldiskfs-based MDTs"
4161                 return
4162         fi
4163
4164         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4165         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4166
4167         touch $DIR/$tdir/d0/foo
4168         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4169                 error "file exceed 65000 nlink limit!"
4170         unlinkmany $DIR/$tdir/d0/f- 65001
4171         return 0
4172 }
4173 run_test 51e "check file nlink limit"
4174
4175 test_52a() {
4176         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4177         test_mkdir -p $DIR/$tdir
4178         touch $DIR/$tdir/foo
4179         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4180         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4181         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4182         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4183         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4184                                         error "link worked"
4185         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4186         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4187         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4188                                                      error "lsattr"
4189         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4190         cp -r $DIR/$tdir /tmp/
4191         rm -fr $DIR/$tdir || error "cleanup rm failed"
4192 }
4193 run_test 52a "append-only flag test (should return errors) ====="
4194
4195 test_52b() {
4196         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4197         test_mkdir -p $DIR/$tdir
4198         touch $DIR/$tdir/foo
4199         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4200         cat test > $DIR/$tdir/foo && error "cat test worked"
4201         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4202         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4203         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4204                                         error "link worked"
4205         echo foo >> $DIR/$tdir/foo && error "echo worked"
4206         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4207         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4208         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4209         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4210                                                         error "lsattr"
4211         chattr -i $DIR/$tdir/foo || error "chattr failed"
4212
4213         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4214 }
4215 run_test 52b "immutable flag test (should return errors) ======="
4216
4217 test_53() {
4218         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4219         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4220         remote_ost_nodsh && skip "remote OST with nodsh" && return
4221
4222         local param
4223         local param_seq
4224         local ostname
4225         local mds_last
4226         local mds_last_seq
4227         local ost_last
4228         local ost_last_seq
4229         local ost_last_id
4230         local ostnum
4231         local node
4232         local found=false
4233         local support_last_seq=true
4234
4235         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4236                 support_last_seq=false
4237
4238         # only test MDT0000
4239         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4240         local value
4241         for value in $(do_facet $SINGLEMDS \
4242                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4243                 param=$(echo ${value[0]} | cut -d "=" -f1)
4244                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4245
4246                 if $support_last_seq; then
4247                         param_seq=$(echo $param |
4248                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4249                         mds_last_seq=$(do_facet $SINGLEMDS \
4250                                        $LCTL get_param -n $param_seq)
4251                 fi
4252                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4253
4254                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4255                 node=$(facet_active_host ost$((ostnum+1)))
4256                 param="obdfilter.$ostname.last_id"
4257                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4258                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4259                         ost_last_id=$ost_last
4260
4261                         if $support_last_seq; then
4262                                 ost_last_id=$(echo $ost_last |
4263                                               awk -F':' '{print $2}' |
4264                                               sed -e "s/^0x//g")
4265                                 ost_last_seq=$(echo $ost_last |
4266                                                awk -F':' '{print $1}')
4267                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4268                         fi
4269
4270                         if [[ $ost_last_id != $mds_last ]]; then
4271                                 error "$ost_last_id != $mds_last"
4272                         else
4273                                 found=true
4274                                 break
4275                         fi
4276                 done
4277         done
4278         $found || error "can not match last_seq/last_id for $mdtosc"
4279         return 0
4280 }
4281 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4282
4283 test_54a() {
4284         $SOCKETSERVER $DIR/socket ||
4285                 error "$SOCKETSERVER $DIR/socket failed: $?"
4286         $SOCKETCLIENT $DIR/socket ||
4287                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4288         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4289 }
4290 run_test 54a "unix domain socket test =========================="
4291
4292 test_54b() {
4293         f="$DIR/f54b"
4294         mknod $f c 1 3
4295         chmod 0666 $f
4296         dd if=/dev/zero of=$f bs=$(page_size) count=1
4297 }
4298 run_test 54b "char device works in lustre ======================"
4299
4300 find_loop_dev() {
4301         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4302         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4303         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4304
4305         for i in $(seq 3 7); do
4306                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4307                 LOOPDEV=$LOOPBASE$i
4308                 LOOPNUM=$i
4309                 break
4310         done
4311 }
4312
4313 cleanup_54c() {
4314         loopdev="$DIR/loop54c"
4315
4316         trap 0
4317         $UMOUNT -d $DIR/$tdir || rc=$?
4318         losetup -d $loopdev || true
4319         losetup -d $LOOPDEV || true
4320         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4321         return $rc
4322 }
4323
4324 test_54c() {
4325         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4326         loopdev="$DIR/loop54c"
4327
4328         find_loop_dev
4329         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4330         trap cleanup_54c EXIT
4331         mknod $loopdev b 7 $LOOPNUM
4332         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4333         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4334         losetup $loopdev $DIR/$tfile ||
4335                 error "can't set up $loopdev for $DIR/$tfile"
4336         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4337         test_mkdir -p $DIR/$tdir
4338         mount -t ext2 $loopdev $DIR/$tdir ||
4339                 error "error mounting $loopdev on $DIR/$tdir"
4340         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4341                 error "dd write"
4342         df $DIR/$tdir
4343         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4344                 error "dd read"
4345         cleanup_54c
4346 }
4347 run_test 54c "block device works in lustre ====================="
4348
4349 test_54d() {
4350         f="$DIR/f54d"
4351         string="aaaaaa"
4352         mknod $f p
4353         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4354 }
4355 run_test 54d "fifo device works in lustre ======================"
4356
4357 test_54e() {
4358         f="$DIR/f54e"
4359         string="aaaaaa"
4360         cp -aL /dev/console $f
4361         echo $string > $f || error "echo $string to $f failed"
4362 }
4363 run_test 54e "console/tty device works in lustre ======================"
4364
4365 #The test_55 used to be iopen test and it was removed by bz#24037.
4366 #run_test 55 "check iopen_connect_dentry() ======================"
4367
4368 test_56a() {    # was test_56
4369         rm -rf $DIR/$tdir
4370         $SETSTRIPE -d $DIR
4371         test_mkdir -p $DIR/$tdir/dir
4372         NUMFILES=3
4373         NUMFILESx2=$(($NUMFILES * 2))
4374         for i in $(seq 1 $NUMFILES); do
4375                 touch $DIR/$tdir/file$i
4376                 touch $DIR/$tdir/dir/file$i
4377         done
4378
4379         # test lfs getstripe with --recursive
4380         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4381         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4382                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4383         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4384         [[ $FILENUM -eq $NUMFILES ]] ||
4385                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4386         echo "$GETSTRIPE --recursive passed."
4387
4388         # test lfs getstripe with file instead of dir
4389         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4390         [[ $FILENUM -eq 1 ]] ||
4391                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4392         echo "$GETSTRIPE file1 passed."
4393
4394         #test lfs getstripe with --verbose
4395         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4396                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4397                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4398         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4399                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4400         echo "$GETSTRIPE --verbose passed."
4401
4402         #test lfs getstripe with --obd
4403         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4404                 grep -q "unknown obduuid" ||
4405                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4406
4407         [[ $OSTCOUNT -lt 2 ]] &&
4408                 skip_env "skipping other $GETSTRIPE --obd test" && return
4409
4410         OSTIDX=1
4411         OBDUUID=$(ostuuid_from_index $OSTIDX)
4412         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4413         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4414         [[ $FOUND -eq $FILENUM ]] ||
4415                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4416         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4417                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4418                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4419                 error "$GETSTRIPE --obd: should not show file on other obd"
4420         echo "$GETSTRIPE --obd passed"
4421 }
4422 run_test 56a "check $GETSTRIPE"
4423
4424 NUMFILES=3
4425 NUMDIRS=3
4426 setup_56() {
4427         local LOCAL_NUMFILES="$1"
4428         local LOCAL_NUMDIRS="$2"
4429         local MKDIR_PARAMS="$3"
4430         local DIR_STRIPE_PARAMS="$4"
4431
4432         if [ ! -d "$TDIR" ] ; then
4433                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4434                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4435                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4436                         touch $TDIR/file$i
4437                 done
4438                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4439                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4440                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4441                                 touch $TDIR/dir$i/file$j
4442                         done
4443                 done
4444         fi
4445 }
4446
4447 setup_56_special() {
4448         LOCAL_NUMFILES=$1
4449         LOCAL_NUMDIRS=$2
4450         setup_56 $1 $2
4451         if [ ! -e "$TDIR/loop1b" ] ; then
4452                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4453                         mknod $TDIR/loop${i}b b 7 $i
4454                         mknod $TDIR/null${i}c c 1 3
4455                         ln -s $TDIR/file1 $TDIR/link${i}l
4456                 done
4457                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4458                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4459                         mknod $TDIR/dir$i/null${i}c c 1 3
4460                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4461                 done
4462         fi
4463 }
4464
4465 test_56g() {
4466         $SETSTRIPE -d $DIR
4467
4468         TDIR=$DIR/${tdir}g
4469         setup_56 $NUMFILES $NUMDIRS
4470
4471         EXPECTED=$(($NUMDIRS + 2))
4472         # test lfs find with -name
4473         for i in $(seq 1 $NUMFILES) ; do
4474                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4475                 [ $NUMS -eq $EXPECTED ] ||
4476                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4477                               "found $NUMS, expected $EXPECTED"
4478         done
4479 }
4480 run_test 56g "check lfs find -name ============================="
4481
4482 test_56h() {
4483         $SETSTRIPE -d $DIR
4484
4485         TDIR=$DIR/${tdir}g
4486         setup_56 $NUMFILES $NUMDIRS
4487
4488         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4489         # test lfs find with ! -name
4490         for i in $(seq 1 $NUMFILES) ; do
4491                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4492                 [ $NUMS -eq $EXPECTED ] ||
4493                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4494                               "found $NUMS, expected $EXPECTED"
4495         done
4496 }
4497 run_test 56h "check lfs find ! -name ============================="
4498
4499 test_56i() {
4500        tdir=${tdir}i
4501        test_mkdir -p $DIR/$tdir
4502        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4503        CMD="$LFIND -ost $UUID $DIR/$tdir"
4504        OUT=$($CMD)
4505        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4506 }
4507 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4508
4509 test_56j() {
4510         TDIR=$DIR/${tdir}g
4511         setup_56_special $NUMFILES $NUMDIRS
4512
4513         EXPECTED=$((NUMDIRS + 1))
4514         CMD="$LFIND -type d $TDIR"
4515         NUMS=$($CMD | wc -l)
4516         [ $NUMS -eq $EXPECTED ] ||
4517                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4518 }
4519 run_test 56j "check lfs find -type d ============================="
4520
4521 test_56k() {
4522         TDIR=$DIR/${tdir}g
4523         setup_56_special $NUMFILES $NUMDIRS
4524
4525         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4526         CMD="$LFIND -type f $TDIR"
4527         NUMS=$($CMD | wc -l)
4528         [ $NUMS -eq $EXPECTED ] ||
4529                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4530 }
4531 run_test 56k "check lfs find -type f ============================="
4532
4533 test_56l() {
4534         TDIR=$DIR/${tdir}g
4535         setup_56_special $NUMFILES $NUMDIRS
4536
4537         EXPECTED=$((NUMDIRS + NUMFILES))
4538         CMD="$LFIND -type b $TDIR"
4539         NUMS=$($CMD | wc -l)
4540         [ $NUMS -eq $EXPECTED ] ||
4541                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4542 }
4543 run_test 56l "check lfs find -type b ============================="
4544
4545 test_56m() {
4546         TDIR=$DIR/${tdir}g
4547         setup_56_special $NUMFILES $NUMDIRS
4548
4549         EXPECTED=$((NUMDIRS + NUMFILES))
4550         CMD="$LFIND -type c $TDIR"
4551         NUMS=$($CMD | wc -l)
4552         [ $NUMS -eq $EXPECTED ] ||
4553                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4554 }
4555 run_test 56m "check lfs find -type c ============================="
4556
4557 test_56n() {
4558         TDIR=$DIR/${tdir}g
4559         setup_56_special $NUMFILES $NUMDIRS
4560
4561         EXPECTED=$((NUMDIRS + NUMFILES))
4562         CMD="$LFIND -type l $TDIR"
4563         NUMS=$($CMD | wc -l)
4564         [ $NUMS -eq $EXPECTED ] ||
4565                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4566 }
4567 run_test 56n "check lfs find -type l ============================="
4568
4569 test_56o() {
4570         TDIR=$DIR/${tdir}o
4571         setup_56 $NUMFILES $NUMDIRS
4572         utime $TDIR/file1 > /dev/null || error "utime (1)"
4573         utime $TDIR/file2 > /dev/null || error "utime (2)"
4574         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4575         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4576         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4577         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4578
4579         EXPECTED=4
4580         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4581         [ $NUMS -eq $EXPECTED ] || \
4582                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4583
4584         EXPECTED=12
4585         CMD="$LFIND -mtime 0 $TDIR"
4586         NUMS=$($CMD | wc -l)
4587         [ $NUMS -eq $EXPECTED ] ||
4588                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4589 }
4590 run_test 56o "check lfs find -mtime for old files =========================="
4591
4592 test_56p() {
4593         [ $RUNAS_ID -eq $UID ] &&
4594                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4595
4596         TDIR=$DIR/${tdir}p
4597         setup_56 $NUMFILES $NUMDIRS
4598
4599         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4600         EXPECTED=$NUMFILES
4601         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4602         NUMS=$($CMD | wc -l)
4603         [ $NUMS -eq $EXPECTED ] || \
4604                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4605
4606         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4607         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4608         NUMS=$($CMD | wc -l)
4609         [ $NUMS -eq $EXPECTED ] || \
4610                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4611 }
4612 run_test 56p "check lfs find -uid and ! -uid ==============================="
4613
4614 test_56q() {
4615         [ $RUNAS_ID -eq $UID ] &&
4616                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4617
4618         TDIR=$DIR/${tdir}q
4619         setup_56 $NUMFILES $NUMDIRS
4620
4621         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4622
4623         EXPECTED=$NUMFILES
4624         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4625         NUMS=$($CMD | wc -l)
4626         [ $NUMS -eq $EXPECTED ] ||
4627                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4628
4629         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4630         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4631         NUMS=$($CMD | wc -l)
4632         [ $NUMS -eq $EXPECTED ] ||
4633                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4634 }
4635 run_test 56q "check lfs find -gid and ! -gid ==============================="
4636
4637 test_56r() {
4638         TDIR=$DIR/${tdir}r
4639         setup_56 $NUMFILES $NUMDIRS
4640
4641         EXPECTED=12
4642         CMD="$LFIND -size 0 -type f $TDIR"
4643         NUMS=$($CMD | wc -l)
4644         [ $NUMS -eq $EXPECTED ] ||
4645                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4646         EXPECTED=0
4647         CMD="$LFIND ! -size 0 -type f $TDIR"
4648         NUMS=$($CMD | wc -l)
4649         [ $NUMS -eq $EXPECTED ] ||
4650                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4651         echo "test" > $TDIR/$tfile
4652         echo "test2" > $TDIR/$tfile.2 && sync
4653         EXPECTED=1
4654         CMD="$LFIND -size 5 -type f $TDIR"
4655         NUMS=$($CMD | wc -l)
4656         [ $NUMS -eq $EXPECTED ] ||
4657                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4658         EXPECTED=1
4659         CMD="$LFIND -size +5 -type f $TDIR"
4660         NUMS=$($CMD | wc -l)
4661         [ $NUMS -eq $EXPECTED ] ||
4662                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4663         EXPECTED=2
4664         CMD="$LFIND -size +0 -type f $TDIR"
4665         NUMS=$($CMD | wc -l)
4666         [ $NUMS -eq $EXPECTED ] ||
4667                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4668         EXPECTED=2
4669         CMD="$LFIND ! -size -5 -type f $TDIR"
4670         NUMS=$($CMD | wc -l)
4671         [ $NUMS -eq $EXPECTED ] ||
4672                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4673         EXPECTED=12
4674         CMD="$LFIND -size -5 -type f $TDIR"
4675         NUMS=$($CMD | wc -l)
4676         [ $NUMS -eq $EXPECTED ] ||
4677                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4678 }
4679 run_test 56r "check lfs find -size works =========================="
4680
4681 test_56s() { # LU-611
4682         TDIR=$DIR/${tdir}s
4683         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4684
4685         if [[ $OSTCOUNT -gt 1 ]]; then
4686                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4687                 ONESTRIPE=4
4688                 EXTRA=4
4689         else
4690                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4691                 EXTRA=0
4692         fi
4693
4694         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4695         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4696         NUMS=$($CMD | wc -l)
4697         [ $NUMS -eq $EXPECTED ] || {
4698                 $GETSTRIPE -R $TDIR
4699                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4700         }
4701
4702         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4703         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4704         NUMS=$($CMD | wc -l)
4705         [ $NUMS -eq $EXPECTED ] || {
4706                 $GETSTRIPE -R $TDIR
4707                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4708         }
4709
4710         EXPECTED=$ONESTRIPE
4711         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4712         NUMS=$($CMD | wc -l)
4713         [ $NUMS -eq $EXPECTED ] || {
4714                 $GETSTRIPE -R $TDIR
4715                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4716         }
4717
4718         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4719         NUMS=$($CMD | wc -l)
4720         [ $NUMS -eq $EXPECTED ] || {
4721                 $GETSTRIPE -R $TDIR
4722                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4723         }
4724
4725         EXPECTED=0
4726         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4727         NUMS=$($CMD | wc -l)
4728         [ $NUMS -eq $EXPECTED ] || {
4729                 $GETSTRIPE -R $TDIR
4730                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4731         }
4732 }
4733 run_test 56s "check lfs find -stripe-count works"
4734
4735 test_56t() { # LU-611
4736         TDIR=$DIR/${tdir}t
4737         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4738
4739         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4740
4741         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4742         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4743         NUMS=$($CMD | wc -l)
4744         [ $NUMS -eq $EXPECTED ] ||
4745                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4746
4747         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4748         NUMS=$($CMD | wc -l)
4749         [ $NUMS -eq $EXPECTED ] ||
4750                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4751
4752         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4753         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4754         NUMS=$($CMD | wc -l)
4755         [ $NUMS -eq $EXPECTED ] ||
4756                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4757
4758         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4759         NUMS=$($CMD | wc -l)
4760         [ $NUMS -eq $EXPECTED ] ||
4761                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4762
4763         EXPECTED=4
4764         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4765         NUMS=$($CMD | wc -l)
4766         [ $NUMS -eq $EXPECTED ] ||
4767                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4768
4769         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4770         NUMS=$($CMD | wc -l)
4771         [ $NUMS -eq $EXPECTED ] ||
4772                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4773
4774         EXPECTED=0
4775         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4776         NUMS=$($CMD | wc -l)
4777         [ $NUMS -eq $EXPECTED ] ||
4778                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4779 }
4780 run_test 56t "check lfs find -stripe-size works"
4781
4782 test_56u() { # LU-611
4783         TDIR=$DIR/${tdir}u
4784         setup_56 $NUMFILES $NUMDIRS "-i 0"
4785
4786         if [[ $OSTCOUNT -gt 1 ]]; then
4787                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4788                 ONESTRIPE=4
4789         else
4790                 ONESTRIPE=0
4791         fi
4792
4793         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4794         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4795         NUMS=$($CMD | wc -l)
4796         [ $NUMS -eq $EXPECTED ] ||
4797                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4798
4799         EXPECTED=$ONESTRIPE
4800         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4801         NUMS=$($CMD | wc -l)
4802         [ $NUMS -eq $EXPECTED ] ||
4803                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4804
4805         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4806         NUMS=$($CMD | wc -l)
4807         [ $NUMS -eq $EXPECTED ] ||
4808                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4809
4810         EXPECTED=0
4811         # This should produce an error and not return any files
4812         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4813         NUMS=$($CMD 2>/dev/null | wc -l)
4814         [ $NUMS -eq $EXPECTED ] ||
4815                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4816
4817         if [[ $OSTCOUNT -gt 1 ]]; then
4818                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4819                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4820                 NUMS=$($CMD | wc -l)
4821                 [ $NUMS -eq $EXPECTED ] ||
4822                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4823         fi
4824 }
4825 run_test 56u "check lfs find -stripe-index works"
4826
4827 test_56v() {
4828     local MDT_IDX=0
4829
4830     TDIR=$DIR/${tdir}v
4831     rm -rf $TDIR
4832     setup_56 $NUMFILES $NUMDIRS
4833
4834     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4835     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4836
4837     for file in $($LFIND -mdt $UUID $TDIR); do
4838         file_mdt_idx=$($GETSTRIPE -M $file)
4839         [ $file_mdt_idx -eq $MDT_IDX ] ||
4840             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4841     done
4842 }
4843 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4844
4845 test_56w() {
4846         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4847                 return
4848         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4849         TDIR=$DIR/${tdir}w
4850
4851     rm -rf $TDIR || error "remove $TDIR failed"
4852     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4853
4854     local stripe_size
4855     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4856         error "$GETSTRIPE -S -d $TDIR failed"
4857     stripe_size=${stripe_size%% *}
4858
4859     local file_size=$((stripe_size * OSTCOUNT))
4860     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4861     local required_space=$((file_num * file_size))
4862     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4863     [[ $free_space -le $((required_space / 1024)) ]] &&
4864         skip_env "need at least $required_space bytes free space," \
4865                  "have $free_space kbytes" && return
4866
4867     local dd_bs=65536
4868     local dd_count=$((file_size / dd_bs))
4869
4870     # write data into the files
4871     local i
4872     local j
4873     local file
4874     for i in $(seq 1 $NUMFILES); do
4875         file=$TDIR/file$i
4876         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4877             error "write data into $file failed"
4878     done
4879     for i in $(seq 1 $NUMDIRS); do
4880         for j in $(seq 1 $NUMFILES); do
4881             file=$TDIR/dir$i/file$j
4882             yes | dd bs=$dd_bs count=$dd_count of=$file \
4883                 >/dev/null 2>&1 ||
4884                 error "write data into $file failed"
4885         done
4886     done
4887
4888     local expected=-1
4889     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4890
4891     # lfs_migrate file
4892     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4893     echo "$cmd"
4894     eval $cmd || error "$cmd failed"
4895
4896     check_stripe_count $TDIR/file1 $expected
4897
4898         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4899         then
4900                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4901                 # OST 1 if it is on OST 0. This file is small enough to
4902                 # be on only one stripe.
4903                 file=$TDIR/migr_1_ost
4904                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4905                         error "write data into $file failed"
4906                 local obdidx=$($LFS getstripe -i $file)
4907                 local oldmd5=$(md5sum $file)
4908                 local newobdidx=0
4909                 [[ $obdidx -eq 0 ]] && newobdidx=1
4910                 cmd="$LFS migrate -i $newobdidx $file"
4911                 echo $cmd
4912                 eval $cmd || error "$cmd failed"
4913                 local realobdix=$($LFS getstripe -i $file)
4914                 local newmd5=$(md5sum $file)
4915                 [[ $newobdidx -ne $realobdix ]] &&
4916                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4917                 [[ "$oldmd5" != "$newmd5" ]] &&
4918                         error "md5sum differ: $oldmd5, $newmd5"
4919         fi
4920
4921     # lfs_migrate dir
4922     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4923     echo "$cmd"
4924     eval $cmd || error "$cmd failed"
4925
4926     for j in $(seq 1 $NUMFILES); do
4927         check_stripe_count $TDIR/dir1/file$j $expected
4928     done
4929
4930     # lfs_migrate works with lfs find
4931     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4932          $LFS_MIGRATE -y -c $expected"
4933     echo "$cmd"
4934     eval $cmd || error "$cmd failed"
4935
4936     for i in $(seq 2 $NUMFILES); do
4937         check_stripe_count $TDIR/file$i $expected
4938     done
4939     for i in $(seq 2 $NUMDIRS); do
4940         for j in $(seq 1 $NUMFILES); do
4941             check_stripe_count $TDIR/dir$i/file$j $expected
4942         done
4943     done
4944 }
4945 run_test 56w "check lfs_migrate -c stripe_count works"
4946
4947 test_56x() {
4948         check_swap_layouts_support && return 0
4949         [[ $OSTCOUNT -lt 2 ]] &&
4950                 skip_env "need 2 OST, skipping test" && return
4951
4952         local dir0=$DIR/$tdir/$testnum
4953         test_mkdir -p $dir0 || error "creating dir $dir0"
4954
4955         local ref1=/etc/passwd
4956         local file1=$dir0/file1
4957
4958         $SETSTRIPE -c 2 $file1
4959         cp $ref1 $file1
4960         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4961         stripe=$($GETSTRIPE -c $file1)
4962         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4963         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4964
4965         # clean up
4966         rm -f $file1
4967 }
4968 run_test 56x "lfs migration support"
4969
4970 test_56y() {
4971         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4972                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
4973                 return
4974
4975         local res=""
4976         local dir0=$DIR/$tdir/$testnum
4977         test_mkdir -p $dir0 || error "creating dir $dir0"
4978         local f1=$dir0/file1
4979         local f2=$dir0/file2
4980
4981         touch $f1 || error "creating std file $f1"
4982         $MULTIOP $f2 H2c || error "creating released file $f2"
4983
4984         # a directory can be raid0, so ask only for files
4985         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4986         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4987
4988         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4989         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4990
4991         # only files can be released, so no need to force file search
4992         res=$($LFIND $dir0 -L released)
4993         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4994
4995         res=$($LFIND $dir0 \! -L released)
4996         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4997
4998 }
4999 run_test 56y "lfs find -L raid0|released"
5000
5001 test_56z() { # LU-4824
5002         # This checks to make sure 'lfs find' continues after errors
5003         # There are two classes of errors that should be caught:
5004         # - If multiple paths are provided, all should be searched even if one
5005         #   errors out
5006         # - If errors are encountered during the search, it should not terminate
5007         #   early
5008         local i
5009         test_mkdir $DIR/$tdir
5010         for i in d{0..9}; do
5011                 test_mkdir $DIR/$tdir/$i
5012         done
5013         touch $DIR/$tdir/d{0..9}/$tfile
5014         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5015                 error "$LFS find did not return an error"
5016         # Make a directory unsearchable. This should NOT be the last entry in
5017         # directory order.  Arbitrarily pick the 6th entry
5018         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5019         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5020         # The user should be able to see 10 directories and 9 files
5021         [ $count == 19 ] || error "$LFS find did not continue after error"
5022 }
5023 run_test 56z "lfs find should continue after an error"
5024
5025 test_56aa() { # LU-5937
5026         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5027
5028         mkdir $DIR/$tdir
5029         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5030
5031         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5032         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5033
5034         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5035 }
5036 run_test 56aa "lfs find --size under striped dir"
5037
5038 test_57a() {
5039         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5040         # note test will not do anything if MDS is not local
5041         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5042                 skip "Only applicable to ldiskfs-based MDTs"
5043                 return
5044         fi
5045
5046         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5047         local MNTDEV="osd*.*MDT*.mntdev"
5048         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5049         [ -z "$DEV" ] && error "can't access $MNTDEV"
5050         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5051                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5052                         error "can't access $DEV"
5053                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5054                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5055                 rm $TMP/t57a.dump
5056         done
5057 }
5058 run_test 57a "verify MDS filesystem created with large inodes =="
5059
5060 test_57b() {
5061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5062         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5063                 skip "Only applicable to ldiskfs-based MDTs"
5064                 return
5065         fi
5066
5067         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5068         local dir=$DIR/$tdir
5069
5070         local FILECOUNT=100
5071         local FILE1=$dir/f1
5072         local FILEN=$dir/f$FILECOUNT
5073
5074         rm -rf $dir || error "removing $dir"
5075         test_mkdir -p -c1 $dir || error "creating $dir"
5076         local mdtidx=$($LFS getstripe -M $dir)
5077         local mdtname=MDT$(printf %04x $mdtidx)
5078         local facet=mds$((mdtidx + 1))
5079
5080         echo "mcreating $FILECOUNT files"
5081         createmany -m $dir/f 1 $FILECOUNT || \
5082                 error "creating files in $dir"
5083
5084         # verify that files do not have EAs yet
5085         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5086         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5087
5088         sync
5089         sleep 1
5090         df $dir  #make sure we get new statfs data
5091         local MDSFREE=$(do_facet $facet \
5092                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5093         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5094         echo "opening files to create objects/EAs"
5095         local FILE
5096         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5097                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5098         done
5099
5100         # verify that files have EAs now
5101         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5102         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5103
5104         sleep 1  #make sure we get new statfs data
5105         df $dir
5106         local MDSFREE2=$(do_facet $facet \
5107                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5108         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5109         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5110                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5111                         error "MDC before $MDCFREE != after $MDCFREE2"
5112                 else
5113                         echo "MDC before $MDCFREE != after $MDCFREE2"
5114                         echo "unable to confirm if MDS has large inodes"
5115                 fi
5116         fi
5117         rm -rf $dir
5118 }
5119 run_test 57b "default LOV EAs are stored inside large inodes ==="
5120
5121 test_58() {
5122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5123         [ -z "$(which wiretest 2>/dev/null)" ] &&
5124                         skip_env "could not find wiretest" && return
5125         wiretest
5126 }
5127 run_test 58 "verify cross-platform wire constants =============="
5128
5129 test_59() {
5130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5131         echo "touch 130 files"
5132         createmany -o $DIR/f59- 130
5133         echo "rm 130 files"
5134         unlinkmany $DIR/f59- 130
5135         sync
5136         # wait for commitment of removal
5137         wait_delete_completed
5138 }
5139 run_test 59 "verify cancellation of llog records async ========="
5140
5141 TEST60_HEAD="test_60 run $RANDOM"
5142 test_60a() {
5143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5144         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5145         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5146                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5147                         skip_env "missing subtest run-llog.sh" && return
5148
5149         log "$TEST60_HEAD - from kernel mode"
5150         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5151         do_facet mgs sh run-llog.sh
5152         do_facet mgs $LCTL dk > $TMP/$tfile
5153
5154         # LU-6388: test llog_reader
5155         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5156         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5157         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5158                         skip_env "missing llog_reader" && return
5159         local fstype=$(facet_fstype mgs)
5160         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5161                 skip_env "Only for ldiskfs or zfs type mgs" && return
5162
5163         local mntpt=$(facet_mntpt mgs)
5164         local mgsdev=$(mgsdevname 1)
5165         local fid_list
5166         local fid
5167         local rec_list
5168         local rec
5169         local rec_type
5170         local obj_file
5171         local path
5172         local seq
5173         local oid
5174         local pass=true
5175
5176         #get fid and record list
5177         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5178                 tail -n 4))
5179         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5180                 tail -n 4))
5181         #remount mgs as ldiskfs or zfs type
5182         stop mgs || error "stop mgs failed"
5183         mount_fstype mgs || error "remount mgs failed"
5184         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5185                 fid=${fid_list[i]}
5186                 rec=${rec_list[i]}
5187                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5188                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5189                 oid=$((16#$oid))
5190
5191                 case $fstype in
5192                         ldiskfs )
5193                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5194                         zfs )
5195                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5196                 esac
5197                 echo "obj_file is $obj_file"
5198                 do_facet mgs $llog_reader $obj_file
5199
5200                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5201                         awk '{ print $3 }' | sed -e "s/^type=//g")
5202                 if [ $rec_type != $rec ]; then
5203                         echo "FAILED test_60a wrong record type $rec_type," \
5204                               "should be $rec"
5205                         pass=false
5206                         break
5207                 fi
5208
5209                 #check obj path if record type is LLOG_LOGID_MAGIC
5210                 if [ "$rec" == "1064553b" ]; then
5211                         path=$(do_facet mgs $llog_reader $obj_file |
5212                                 grep "path=" | awk '{ print $NF }' |
5213                                 sed -e "s/^path=//g")
5214                         if [ $obj_file != $mntpt/$path ]; then
5215                                 echo "FAILED test_60a wrong obj path" \
5216                                       "$montpt/$path, should be $obj_file"
5217                                 pass=false
5218                                 break
5219                         fi
5220                 fi
5221         done
5222         rm -f $TMP/$tfile
5223         #restart mgs before "error", otherwise it will block the next test
5224         stop mgs || error "stop mgs failed"
5225         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5226         $pass || error "test failed, see FAILED test_60a messages for specifics"
5227 }
5228 run_test 60a "llog_test run from kernel module and test llog_reader =========="
5229
5230 test_60b() { # bug 6411
5231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5232         dmesg > $DIR/$tfile
5233         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5234                                  /llog.test/ {
5235                                          if (marker)
5236                                                  from_marker++
5237                                          from_begin++
5238                                  }
5239                                  END {
5240                                          if (marker)
5241                                                  print from_marker
5242                                          else
5243                                                  print from_begin
5244                                  }"`
5245         [[ $LLOG_COUNT -gt 50 ]] &&
5246                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5247 }
5248 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5249
5250 test_60c() {
5251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5252         echo "create 5000 files"
5253         createmany -o $DIR/f60c- 5000
5254 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5255         lctl set_param fail_loc=0x80000137
5256         unlinkmany $DIR/f60c- 5000
5257         lctl set_param fail_loc=0
5258 }
5259 run_test 60c "unlink file when mds full"
5260
5261 test_60d() {
5262         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5263         SAVEPRINTK=$(lctl get_param -n printk)
5264
5265         # verify "lctl mark" is even working"
5266         MESSAGE="test message ID $RANDOM $$"
5267         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5268         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5269
5270         lctl set_param printk=0 || error "set lnet.printk failed"
5271         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5272         MESSAGE="new test message ID $RANDOM $$"
5273         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5274         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5275         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5276
5277         lctl set_param -n printk="$SAVEPRINTK"
5278 }
5279 run_test 60d "test printk console message masking"
5280
5281 test_61() {
5282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5283         f="$DIR/f61"
5284         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5285         cancel_lru_locks osc
5286         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5287         sync
5288 }
5289 run_test 61 "mmap() writes don't make sync hang ================"
5290
5291 # bug 2330 - insufficient obd_match error checking causes LBUG
5292 test_62() {
5293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5294         f="$DIR/f62"
5295         echo foo > $f
5296         cancel_lru_locks osc
5297         lctl set_param fail_loc=0x405
5298         cat $f && error "cat succeeded, expect -EIO"
5299         lctl set_param fail_loc=0
5300 }
5301 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5302 # match every page all of the time.
5303 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5304
5305 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5306 # Though this test is irrelevant anymore, it helped to reveal some
5307 # other grant bugs (LU-4482), let's keep it.
5308 test_63a() {   # was test_63
5309         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5310         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5311         for i in `seq 10` ; do
5312                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5313                 sleep 5
5314                 kill $!
5315                 sleep 1
5316         done
5317
5318         rm -f $DIR/f63 || true
5319 }
5320 run_test 63a "Verify oig_wait interruption does not crash ======="
5321
5322 # bug 2248 - async write errors didn't return to application on sync
5323 # bug 3677 - async write errors left page locked
5324 test_63b() {
5325         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5326         debugsave
5327         lctl set_param debug=-1
5328
5329         # ensure we have a grant to do async writes
5330         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5331         rm $DIR/$tfile
5332
5333         sync    # sync lest earlier test intercept the fail_loc
5334
5335         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5336         lctl set_param fail_loc=0x80000406
5337         $MULTIOP $DIR/$tfile Owy && \
5338                 error "sync didn't return ENOMEM"
5339         sync; sleep 2; sync     # do a real sync this time to flush page
5340         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5341                 error "locked page left in cache after async error" || true
5342         debugrestore
5343 }
5344 run_test 63b "async write errors should be returned to fsync ==="
5345
5346 test_64a () {
5347         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5348         df $DIR
5349         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5350 }
5351 run_test 64a "verify filter grant calculations (in kernel) ====="
5352
5353 test_64b () {
5354         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5355         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5356 }
5357 run_test 64b "check out-of-space detection on client ==========="
5358
5359 test_64c() {
5360         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5361 }
5362 run_test 64c "verify grant shrink ========================------"
5363
5364 # bug 1414 - set/get directories' stripe info
5365 test_65a() {
5366         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5367         test_mkdir -p $DIR/$tdir
5368         touch $DIR/$tdir/f1
5369         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5370 }
5371 run_test 65a "directory with no stripe info ===================="
5372
5373 test_65b() {
5374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5375         test_mkdir -p $DIR/$tdir
5376         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5377                                                 error "setstripe"
5378         touch $DIR/$tdir/f2
5379         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5380 }
5381 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5382
5383 test_65c() {
5384         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5385         if [[ $OSTCOUNT -gt 1 ]]; then
5386                 test_mkdir -p $DIR/$tdir
5387                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5388                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5389                 touch $DIR/$tdir/f3
5390                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5391         fi
5392 }
5393 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5394
5395 test_65d() {
5396         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5397         test_mkdir -p $DIR/$tdir
5398         if [[ $STRIPECOUNT -le 0 ]]; then
5399                 sc=1
5400         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5401 #LOV_MAX_STRIPE_COUNT is 2000
5402                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5403         else
5404                 sc=$(($STRIPECOUNT - 1))
5405         fi
5406         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5407         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5408         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5409                 error "lverify failed"
5410 }
5411 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5412
5413 test_65e() {
5414         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5415         test_mkdir -p $DIR/$tdir
5416
5417         $SETSTRIPE $DIR/$tdir || error "setstripe"
5418         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5419                                         error "no stripe info failed"
5420         touch $DIR/$tdir/f6
5421         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5422 }
5423 run_test 65e "directory setstripe defaults ======================="
5424
5425 test_65f() {
5426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5427         test_mkdir -p $DIR/${tdir}f
5428         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5429 }
5430 run_test 65f "dir setstripe permission (should return error) ==="
5431
5432 test_65g() {
5433         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5434         test_mkdir -p $DIR/$tdir
5435         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5436                                                         error "setstripe"
5437         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5438         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5439                 error "delete default stripe failed"
5440 }
5441 run_test 65g "directory setstripe -d ==========================="
5442
5443 test_65h() {
5444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5445         test_mkdir -p $DIR/$tdir
5446         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5447                                                         error "setstripe"
5448         test_mkdir -p $DIR/$tdir/dd1
5449         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5450                 error "stripe info inherit failed"
5451 }
5452 run_test 65h "directory stripe info inherit ===================="
5453
5454 test_65i() { # bug6367
5455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5456         $SETSTRIPE -S 65536 -c -1 $MOUNT
5457 }
5458 run_test 65i "set non-default striping on root directory (bug 6367)="
5459
5460 test_65ia() { # bug12836
5461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5462         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5463 }
5464 run_test 65ia "getstripe on -1 default directory striping"
5465
5466 test_65ib() { # bug12836
5467         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5468         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5469 }
5470 run_test 65ib "getstripe -v on -1 default directory striping"
5471
5472 test_65ic() { # bug12836
5473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5474         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5475 }
5476 run_test 65ic "new find on -1 default directory striping"
5477
5478 test_65j() { # bug6367
5479         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5480         sync; sleep 1
5481         # if we aren't already remounting for each test, do so for this test
5482         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5483                 cleanup || error "failed to unmount"
5484                 setup
5485         fi
5486         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5487 }
5488 run_test 65j "set default striping on root directory (bug 6367)="
5489
5490 test_65k() { # bug11679
5491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5492         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5493         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5494
5495     echo "Check OST status: "
5496     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5497               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5498
5499     for OSC in $MDS_OSCS; do
5500         echo $OSC "is activate"
5501         do_facet $SINGLEMDS lctl --device %$OSC activate
5502     done
5503
5504     mkdir -p $DIR/$tdir
5505     for INACTIVE_OSC in $MDS_OSCS; do
5506         echo "Deactivate: " $INACTIVE_OSC
5507         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5508         for STRIPE_OSC in $MDS_OSCS; do
5509             OST=`osc_to_ost $STRIPE_OSC`
5510             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5511                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5512
5513             [ -f $DIR/$tdir/$IDX ] && continue
5514             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5515             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5516             RC=$?
5517             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5518         done
5519         rm -f $DIR/$tdir/*
5520         echo $INACTIVE_OSC "is Activate."
5521         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5522     done
5523 }
5524 run_test 65k "validate manual striping works properly with deactivated OSCs"
5525
5526 test_65l() { # bug 12836
5527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5528         test_mkdir -p $DIR/$tdir/test_dir
5529         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5530         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5531 }
5532 run_test 65l "lfs find on -1 stripe dir ========================"
5533
5534 # bug 2543 - update blocks count on client
5535 test_66() {
5536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5537         COUNT=${COUNT:-8}
5538         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5539         sync; sync_all_data; sync; sync_all_data
5540         cancel_lru_locks osc
5541         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5542         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5543 }
5544 run_test 66 "update inode blocks count on client ==============="
5545
5546 LLOOP=
5547 LLITELOOPLOAD=
5548 cleanup_68() {
5549         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5550         trap 0
5551         if [ ! -z "$LLOOP" ]; then
5552                 if swapon -s | grep -q $LLOOP; then
5553                         swapoff $LLOOP || error "swapoff failed"
5554                 fi
5555
5556                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5557                 rm -f $LLOOP
5558                 unset LLOOP
5559         fi
5560         if [ ! -z "$LLITELOOPLOAD" ]; then
5561                 rmmod llite_lloop
5562                 unset LLITELOOPLOAD
5563         fi
5564         rm -f $DIR/f68*
5565 }
5566
5567 meminfo() {
5568         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5569 }
5570
5571 swap_used() {
5572         swapon -s | awk '($1 == "'$1'") { print $4 }'
5573 }
5574
5575 # test case for lloop driver, basic function
5576 test_68a() {
5577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5578         [ "$UID" != 0 ] && skip_env "must run as root" && return
5579         llite_lloop_enabled || \
5580                 { skip_env "llite_lloop module disabled" && return; }
5581
5582         trap cleanup_68 EXIT
5583
5584         if ! module_loaded llite_lloop; then
5585                 if load_module llite/llite_lloop; then
5586                         LLITELOOPLOAD=yes
5587                 else
5588                         skip_env "can't find module llite_lloop"
5589                         return
5590                 fi
5591         fi
5592
5593         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5594         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5595         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5596
5597         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5598         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5599
5600         cleanup_68
5601 }
5602 run_test 68a "lloop driver - basic test ========================"
5603
5604 # excercise swapping to lustre by adding a high priority swapfile entry
5605 # and then consuming memory until it is used.
5606 test_68b() {  # was test_68
5607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5608         [ "$UID" != 0 ] && skip_env "must run as root" && return
5609         lctl get_param -n devices | grep -q obdfilter && \
5610                 skip "local OST" && return
5611
5612         grep -q llite_lloop /proc/modules
5613         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5614
5615         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5616                 skip "can't reliably test swap with TCP" && return
5617
5618         MEMTOTAL=`meminfo MemTotal`
5619         NR_BLOCKS=$((MEMTOTAL>>8))
5620         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5621
5622         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5623         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5624         mkswap $DIR/f68b
5625
5626         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5627
5628         trap cleanup_68 EXIT
5629
5630         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5631
5632         echo "before: `swapon -s | grep $LLOOP`"
5633         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5634         echo "after: `swapon -s | grep $LLOOP`"
5635         SWAPUSED=`swap_used $LLOOP`
5636
5637         cleanup_68
5638
5639         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5640 }
5641 run_test 68b "support swapping to Lustre ========================"
5642
5643 # bug5265, obdfilter oa2dentry return -ENOENT
5644 # #define OBD_FAIL_OST_ENOENT 0x217
5645 test_69() {
5646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5647         remote_ost_nodsh && skip "remote OST with nodsh" && return
5648
5649         f="$DIR/$tfile"
5650         $SETSTRIPE -c 1 -i 0 $f
5651
5652         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5653
5654         do_facet ost1 lctl set_param fail_loc=0x217
5655         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5656         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5657
5658         do_facet ost1 lctl set_param fail_loc=0
5659         $DIRECTIO write $f 0 2 || error "write error"
5660
5661         cancel_lru_locks osc
5662         $DIRECTIO read $f 0 1 || error "read error"
5663
5664         do_facet ost1 lctl set_param fail_loc=0x217
5665         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5666
5667         do_facet ost1 lctl set_param fail_loc=0
5668         rm -f $f
5669 }
5670 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5671
5672 test_71() {
5673         test_mkdir -p $DIR/$tdir
5674         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
5675         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5676 }
5677 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5678
5679 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5680         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5681         [ "$RUNAS_ID" = "$UID" ] &&
5682                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5683
5684         # Check that testing environment is properly set up. Skip if not
5685         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5686                 skip_env "User $RUNAS_ID does not exist - skipping"
5687                 return 0
5688         }
5689         touch $DIR/$tfile
5690         chmod 777 $DIR/$tfile
5691         chmod ug+s $DIR/$tfile
5692         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5693                 error "$RUNAS dd $DIR/$tfile failed"
5694         # See if we are still setuid/sgid
5695         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5696                 error "S/gid is not dropped on write"
5697         # Now test that MDS is updated too
5698         cancel_lru_locks mdc
5699         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5700                 error "S/gid is not dropped on MDS"
5701         rm -f $DIR/$tfile
5702 }
5703 run_test 72a "Test that remove suid works properly (bug5695) ===="
5704
5705 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5706         local perm
5707
5708         [ "$RUNAS_ID" = "$UID" ] && \
5709                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5710         [ "$RUNAS_ID" -eq 0 ] && \
5711                 skip_env "RUNAS_ID = 0 -- skipping" && return
5712
5713         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5714         # Check that testing environment is properly set up. Skip if not
5715         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5716                 skip_env "User $RUNAS_ID does not exist - skipping"
5717                 return 0
5718         }
5719         touch $DIR/${tfile}-f{g,u}
5720         test_mkdir $DIR/${tfile}-dg
5721         test_mkdir $DIR/${tfile}-du
5722         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5723         chmod g+s $DIR/${tfile}-{f,d}g
5724         chmod u+s $DIR/${tfile}-{f,d}u
5725         for perm in 777 2777 4777; do
5726                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5727                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5728                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5729                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5730         done
5731         true
5732 }
5733 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5734
5735 # bug 3462 - multiple simultaneous MDC requests
5736 test_73() {
5737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5738         test_mkdir $DIR/d73-1
5739         test_mkdir $DIR/d73-2
5740         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5741         pid1=$!
5742
5743         lctl set_param fail_loc=0x80000129
5744         $MULTIOP $DIR/d73-1/f73-2 Oc &
5745         sleep 1
5746         lctl set_param fail_loc=0
5747
5748         $MULTIOP $DIR/d73-2/f73-3 Oc &
5749         pid3=$!
5750
5751         kill -USR1 $pid1
5752         wait $pid1 || return 1
5753
5754         sleep 25
5755
5756         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5757         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5758         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5759
5760         rm -rf $DIR/d73-*
5761 }
5762 run_test 73 "multiple MDC requests (should not deadlock)"
5763
5764 test_74a() { # bug 6149, 6184
5765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5766         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5767         #
5768         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5769         # will spin in a tight reconnection loop
5770         touch $DIR/f74a
5771         $LCTL set_param fail_loc=0x8000030e
5772         # get any lock that won't be difficult - lookup works.
5773         ls $DIR/f74a
5774         $LCTL set_param fail_loc=0
5775         rm -f $DIR/f74a
5776         true
5777 }
5778 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5779
5780 test_74b() { # bug 13310
5781         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5782         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5783         #
5784         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5785         # will spin in a tight reconnection loop
5786         $LCTL set_param fail_loc=0x8000030e
5787         # get a "difficult" lock
5788         touch $DIR/f74b
5789         $LCTL set_param fail_loc=0
5790         rm -f $DIR/f74b
5791         true
5792 }
5793 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5794
5795 test_74c() {
5796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5797         #define OBD_FAIL_LDLM_NEW_LOCK
5798         $LCTL set_param fail_loc=0x319
5799         touch $DIR/$tfile && error "touch successful"
5800         $LCTL set_param fail_loc=0
5801         true
5802 }
5803 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5804
5805 num_inodes() {
5806         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5807 }
5808
5809 get_inode_slab_tunables() {
5810         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5811 }
5812
5813 set_inode_slab_tunables() {
5814         echo "lustre_inode_cache $1" > /proc/slabinfo
5815 }
5816
5817 test_76() { # Now for bug 20433, added originally in bug 1443
5818         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5819         local SLAB_SETTINGS=`get_inode_slab_tunables`
5820         local CPUS=`getconf _NPROCESSORS_ONLN`
5821         # we cannot set limit below 1 which means 1 inode in each
5822         # per-cpu cache is still allowed
5823         set_inode_slab_tunables "1 1 0"
5824         cancel_lru_locks osc
5825         BEFORE_INODES=$(num_inodes)
5826         echo "before inodes: $BEFORE_INODES"
5827         local COUNT=1000
5828         [ "$SLOW" = "no" ] && COUNT=100
5829         for i in $(seq $COUNT); do
5830                 touch $DIR/$tfile
5831                 rm -f $DIR/$tfile
5832         done
5833         cancel_lru_locks osc
5834         AFTER_INODES=$(num_inodes)
5835         echo "after inodes: $AFTER_INODES"
5836         local wait=0
5837         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5838                 sleep 2
5839                 AFTER_INODES=$(num_inodes)
5840                 wait=$((wait+2))
5841                 echo "wait $wait seconds inodes: $AFTER_INODES"
5842                 if [ $wait -gt 30 ]; then
5843                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5844                 fi
5845         done
5846         set_inode_slab_tunables "$SLAB_SETTINGS"
5847 }
5848 run_test 76 "confirm clients recycle inodes properly ===="
5849
5850
5851 export ORIG_CSUM=""
5852 set_checksums()
5853 {
5854         # Note: in sptlrpc modes which enable its own bulk checksum, the
5855         # original crc32_le bulk checksum will be automatically disabled,
5856         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5857         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5858         # In this case set_checksums() will not be no-op, because sptlrpc
5859         # bulk checksum will be enabled all through the test.
5860
5861         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5862         lctl set_param -n osc.*.checksums $1
5863         return 0
5864 }
5865
5866 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5867                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5868 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5869 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5870 set_checksum_type()
5871 {
5872         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5873         log "set checksum type to $1"
5874         return 0
5875 }
5876 F77_TMP=$TMP/f77-temp
5877 F77SZ=8
5878 setup_f77() {
5879         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5880                 error "error writing to $F77_TMP"
5881 }
5882
5883 test_77a() { # bug 10889
5884         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5885         $GSS && skip "could not run with gss" && return
5886         [ ! -f $F77_TMP ] && setup_f77
5887         set_checksums 1
5888         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5889         set_checksums 0
5890         rm -f $DIR/$tfile
5891 }
5892 run_test 77a "normal checksum read/write operation"
5893
5894 test_77b() { # bug 10889
5895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5896         $GSS && skip "could not run with gss" && return
5897         [ ! -f $F77_TMP ] && setup_f77
5898         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5899         $LCTL set_param fail_loc=0x80000409
5900         set_checksums 1
5901
5902         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5903                 error "dd error: $?"
5904         $LCTL set_param fail_loc=0
5905
5906         for algo in $CKSUM_TYPES; do
5907                 cancel_lru_locks osc
5908                 set_checksum_type $algo
5909                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5910                 $LCTL set_param fail_loc=0x80000408
5911                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5912                 $LCTL set_param fail_loc=0
5913         done
5914         set_checksums 0
5915         set_checksum_type $ORIG_CSUM_TYPE
5916         rm -f $DIR/$tfile
5917 }
5918 run_test 77b "checksum error on client write, read"
5919
5920 test_77d() { # bug 10889
5921         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5922         $GSS && skip "could not run with gss" && return
5923         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5924         $LCTL set_param fail_loc=0x80000409
5925         set_checksums 1
5926         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5927                 error "direct write: rc=$?"
5928         $LCTL set_param fail_loc=0
5929         set_checksums 0
5930
5931         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5932         $LCTL set_param fail_loc=0x80000408
5933         set_checksums 1
5934         cancel_lru_locks osc
5935         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5936                 error "direct read: rc=$?"
5937         $LCTL set_param fail_loc=0
5938         set_checksums 0
5939 }
5940 run_test 77d "checksum error on OST direct write, read"
5941
5942 test_77f() { # bug 10889
5943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5944         $GSS && skip "could not run with gss" && return
5945         set_checksums 1
5946         for algo in $CKSUM_TYPES; do
5947                 cancel_lru_locks osc
5948                 set_checksum_type $algo
5949                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5950                 $LCTL set_param fail_loc=0x409
5951                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5952                         error "direct write succeeded"
5953                 $LCTL set_param fail_loc=0
5954         done
5955         set_checksum_type $ORIG_CSUM_TYPE
5956         set_checksums 0
5957 }
5958 run_test 77f "repeat checksum error on write (expect error)"
5959
5960 test_77g() { # bug 10889
5961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5962         $GSS && skip "could not run with gss" && return
5963         remote_ost_nodsh && skip "remote OST with nodsh" && return
5964
5965         [ ! -f $F77_TMP ] && setup_f77
5966
5967         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5968         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5969         do_facet ost1 lctl set_param fail_loc=0x8000021a
5970         set_checksums 1
5971         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5972                 error "write error: rc=$?"
5973         do_facet ost1 lctl set_param fail_loc=0
5974         set_checksums 0
5975
5976         cancel_lru_locks osc
5977         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5978         do_facet ost1 lctl set_param fail_loc=0x8000021b
5979         set_checksums 1
5980         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5981         do_facet ost1 lctl set_param fail_loc=0
5982         set_checksums 0
5983 }
5984 run_test 77g "checksum error on OST write, read"
5985
5986 test_77i() { # bug 13805
5987         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5988         $GSS && skip "could not run with gss" && return
5989         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5990         lctl set_param fail_loc=0x40b
5991         remount_client $MOUNT
5992         lctl set_param fail_loc=0
5993         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5994                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5995                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5996                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5997         done
5998         remount_client $MOUNT
5999 }
6000 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
6001
6002 test_77j() { # bug 13805
6003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6004         $GSS && skip "could not run with gss" && return
6005         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6006         lctl set_param fail_loc=0x40c
6007         remount_client $MOUNT
6008         lctl set_param fail_loc=0
6009         sleep 2 # wait async osc connect to finish
6010         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
6011                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6012                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
6013                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6014         done
6015         remount_client $MOUNT
6016 }
6017 run_test 77j "client only supporting ADLER32"
6018
6019 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6020 rm -f $F77_TMP
6021 unset F77_TMP
6022
6023 test_78() { # bug 10901
6024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6025         remote_ost || { skip_env "local OST" && return; }
6026
6027         NSEQ=5
6028         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6029         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6030         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6031         echo "MemTotal: $MEMTOTAL"
6032
6033         # reserve 256MB of memory for the kernel and other running processes,
6034         # and then take 1/2 of the remaining memory for the read/write buffers.
6035         if [ $MEMTOTAL -gt 512 ] ;then
6036                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6037         else
6038                 # for those poor memory-starved high-end clusters...
6039                 MEMTOTAL=$((MEMTOTAL / 2))
6040         fi
6041         echo "Mem to use for directio: $MEMTOTAL"
6042
6043         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6044         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6045         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6046         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6047                 head -n1)
6048         echo "Smallest OST: $SMALLESTOST"
6049         [[ $SMALLESTOST -lt 10240 ]] &&
6050                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6051
6052         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6053                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6054
6055         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6056         echo "File size: $F78SIZE"
6057         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6058         for i in $(seq 1 $NSEQ); do
6059                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6060                 echo directIO rdwr round $i of $NSEQ
6061                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6062         done
6063
6064         rm -f $DIR/$tfile
6065 }
6066 run_test 78 "handle large O_DIRECT writes correctly ============"
6067
6068 test_79() { # bug 12743
6069         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6070         wait_delete_completed
6071
6072         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6073         BKFREE=$(calc_osc_kbytes kbytesfree)
6074         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6075
6076         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6077         DFTOTAL=`echo $STRING | cut -d, -f1`
6078         DFUSED=`echo $STRING  | cut -d, -f2`
6079         DFAVAIL=`echo $STRING | cut -d, -f3`
6080         DFFREE=$(($DFTOTAL - $DFUSED))
6081
6082         ALLOWANCE=$((64 * $OSTCOUNT))
6083
6084         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6085            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6086                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6087         fi
6088         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6089            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6090                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6091         fi
6092         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6093            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6094                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6095         fi
6096 }
6097 run_test 79 "df report consistency check ======================="
6098
6099 test_80() { # bug 10718
6100         remote_ost_nodsh && skip "remote OST with nodsh" && return
6101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6102         # relax strong synchronous semantics for slow backends like ZFS
6103         local soc="obdfilter.*.sync_on_lock_cancel"
6104         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6105         local hosts=
6106         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6107                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6108                           facet_active_host $host; done | sort -u)
6109                 do_nodes $hosts lctl set_param $soc=never
6110         fi
6111
6112         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6113         sync; sleep 1; sync
6114         local BEFORE=`date +%s`
6115         cancel_lru_locks osc
6116         local AFTER=`date +%s`
6117         local DIFF=$((AFTER-BEFORE))
6118         if [ $DIFF -gt 1 ] ; then
6119                 error "elapsed for 1M@1T = $DIFF"
6120         fi
6121
6122         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6123
6124         rm -f $DIR/$tfile
6125 }
6126 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6127
6128 test_81a() { # LU-456
6129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6130         remote_ost_nodsh && skip "remote OST with nodsh" && return
6131         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6132         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6133         do_facet ost1 lctl set_param fail_loc=0x80000228
6134
6135         # write should trigger a retry and success
6136         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6137         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6138         RC=$?
6139         if [ $RC -ne 0 ] ; then
6140                 error "write should success, but failed for $RC"
6141         fi
6142 }
6143 run_test 81a "OST should retry write when get -ENOSPC ==============="
6144
6145 test_81b() { # LU-456
6146         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6147         remote_ost_nodsh && skip "remote OST with nodsh" && return
6148         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6149         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6150         do_facet ost1 lctl set_param fail_loc=0x228
6151
6152         # write should retry several times and return -ENOSPC finally
6153         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6154         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6155         RC=$?
6156         ENOSPC=28
6157         if [ $RC -ne $ENOSPC ] ; then
6158                 error "dd should fail for -ENOSPC, but succeed."
6159         fi
6160 }
6161 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6162
6163 test_82() { # LU-1031
6164         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6165         local gid1=14091995
6166         local gid2=16022000
6167
6168         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6169         local MULTIPID1=$!
6170         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6171         local MULTIPID2=$!
6172         kill -USR1 $MULTIPID2
6173         sleep 2
6174         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6175                 error "First grouplock does not block second one"
6176         else
6177                 echo "Second grouplock blocks first one"
6178         fi
6179         kill -USR1 $MULTIPID1
6180         wait $MULTIPID1
6181         wait $MULTIPID2
6182 }
6183 run_test 82 "Basic grouplock test ==============================="
6184
6185 test_99a() {
6186         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6187                 return
6188         test_mkdir -p $DIR/d99cvsroot
6189         chown $RUNAS_ID $DIR/d99cvsroot
6190         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6191         cd $TMP
6192
6193         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6194         cd $oldPWD
6195 }
6196 run_test 99a "cvs init ========================================="
6197
6198 test_99b() {
6199         [ -z "$(which cvs 2>/dev/null)" ] &&
6200                 skip_env "could not find cvs" && return
6201         [ ! -d $DIR/d99cvsroot ] && test_99a
6202         cd /etc/init.d
6203         # some versions of cvs import exit(1) when asked to import links or
6204         # files they can't read.  ignore those files.
6205         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6206                         ! -perm +4 -printf '-I %f\n')
6207         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6208                 d99reposname vtag rtag
6209 }
6210 run_test 99b "cvs import ======================================="
6211
6212 test_99c() {
6213         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6214         [ ! -d $DIR/d99cvsroot ] && test_99b
6215         cd $DIR
6216         test_mkdir -p $DIR/d99reposname
6217         chown $RUNAS_ID $DIR/d99reposname
6218         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6219 }
6220 run_test 99c "cvs checkout ====================================="
6221
6222 test_99d() {
6223         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6224         [ ! -d $DIR/d99cvsroot ] && test_99c
6225         cd $DIR/d99reposname
6226         $RUNAS touch foo99
6227         $RUNAS cvs add -m 'addmsg' foo99
6228 }
6229 run_test 99d "cvs add =========================================="
6230
6231 test_99e() {
6232         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6233         [ ! -d $DIR/d99cvsroot ] && test_99c
6234         cd $DIR/d99reposname
6235         $RUNAS cvs update
6236 }
6237 run_test 99e "cvs update ======================================="
6238
6239 test_99f() {
6240         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6241         [ ! -d $DIR/d99cvsroot ] && test_99d
6242         cd $DIR/d99reposname
6243         $RUNAS cvs commit -m 'nomsg' foo99
6244     rm -fr $DIR/d99cvsroot
6245 }
6246 run_test 99f "cvs commit ======================================="
6247
6248 test_100() {
6249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6250         [ "$NETTYPE" = tcp ] || \
6251                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6252                         return ; }
6253
6254         remote_ost_nodsh && skip "remote OST with nodsh" && return
6255         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6256         remote_servers || \
6257                 { skip "useless for local single node setup" && return; }
6258
6259         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6260                 [ "$PROT" != "tcp" ] && continue
6261                 RPORT=$(echo $REMOTE | cut -d: -f2)
6262                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6263
6264                 rc=0
6265                 LPORT=`echo $LOCAL | cut -d: -f2`
6266                 if [ $LPORT -ge 1024 ]; then
6267                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6268                         netstat -tna
6269                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6270                 fi
6271         done
6272         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6273 }
6274 run_test 100 "check local port using privileged port ==========="
6275
6276 function get_named_value()
6277 {
6278     local tag
6279
6280     tag=$1
6281     while read ;do
6282         line=$REPLY
6283         case $line in
6284         $tag*)
6285             echo $line | sed "s/^$tag[ ]*//"
6286             break
6287             ;;
6288         esac
6289     done
6290 }
6291
6292 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6293                    awk '/^max_cached_mb/ { print $2 }')
6294
6295 cleanup_101a() {
6296         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6297         trap 0
6298 }
6299
6300 test_101a() {
6301         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6302         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6303         local s
6304         local discard
6305         local nreads=10000
6306         local cache_limit=32
6307
6308         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6309         trap cleanup_101a EXIT
6310         $LCTL set_param -n llite.*.read_ahead_stats 0
6311         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6312
6313         #
6314         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6315         #
6316         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6317         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6318
6319         discard=0
6320         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6321                 get_named_value 'read but discarded' | cut -d" " -f1); do
6322                         discard=$(($discard + $s))
6323         done
6324         cleanup_101a
6325
6326         if [[ $(($discard * 10)) -gt $nreads ]]; then
6327                 $LCTL get_param osc.*-osc*.rpc_stats
6328                 $LCTL get_param llite.*.read_ahead_stats
6329                 error "too many ($discard) discarded pages"
6330         fi
6331         rm -f $DIR/$tfile || true
6332 }
6333 run_test 101a "check read-ahead for random reads ================"
6334
6335 setup_test101bc() {
6336         test_mkdir -p $DIR/$tdir
6337         local STRIPE_SIZE=$1
6338         local FILE_LENGTH=$2
6339         STRIPE_OFFSET=0
6340
6341         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6342
6343         local list=$(comma_list $(osts_nodes))
6344         set_osd_param $list '' read_cache_enable 0
6345         set_osd_param $list '' writethrough_cache_enable 0
6346
6347         trap cleanup_test101bc EXIT
6348         # prepare the read-ahead file
6349         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6350
6351         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6352                                 count=$FILE_SIZE_MB 2> /dev/null
6353
6354 }
6355
6356 cleanup_test101bc() {
6357         trap 0
6358         rm -rf $DIR/$tdir
6359         rm -f $DIR/$tfile
6360
6361         local list=$(comma_list $(osts_nodes))
6362         set_osd_param $list '' read_cache_enable 1
6363         set_osd_param $list '' writethrough_cache_enable 1
6364 }
6365
6366 calc_total() {
6367         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6368 }
6369
6370 ra_check_101() {
6371         local READ_SIZE=$1
6372         local STRIPE_SIZE=$2
6373         local FILE_LENGTH=$3
6374         local RA_INC=1048576
6375         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6376         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6377                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6378         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6379                         get_named_value 'read but discarded' |
6380                         cut -d" " -f1 | calc_total)
6381         if [[ $DISCARD -gt $discard_limit ]]; then
6382                 $LCTL get_param llite.*.read_ahead_stats
6383                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6384         else
6385                 echo "Read-ahead success for size ${READ_SIZE}"
6386         fi
6387 }
6388
6389 test_101b() {
6390         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6391         [[ $OSTCOUNT -lt 2 ]] &&
6392                 skip_env "skipping stride IO stride-ahead test" && return
6393         local STRIPE_SIZE=1048576
6394         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6395         if [ $SLOW == "yes" ]; then
6396                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6397         else
6398                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6399         fi
6400
6401         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6402
6403         # prepare the read-ahead file
6404         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6405         cancel_lru_locks osc
6406         for BIDX in 2 4 8 16 32 64 128 256
6407         do
6408                 local BSIZE=$((BIDX*4096))
6409                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6410                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6411                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6412                 $LCTL set_param -n llite.*.read_ahead_stats 0
6413                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6414                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6415                 cancel_lru_locks osc
6416                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6417         done
6418         cleanup_test101bc
6419         true
6420 }
6421 run_test 101b "check stride-io mode read-ahead ================="
6422
6423 test_101c() {
6424         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6425         local STRIPE_SIZE=1048576
6426         local FILE_LENGTH=$((STRIPE_SIZE*100))
6427         local nreads=10000
6428         local osc_rpc_stats
6429
6430         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6431
6432         cancel_lru_locks osc
6433         $LCTL set_param osc.*.rpc_stats 0
6434         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6435         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6436                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6437                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6438                 local size
6439
6440                 if [ $lines -le 20 ]; then
6441                         continue
6442                 fi
6443                 for size in 1 2 4 8; do
6444                         local rpc=$(echo "$stats" |
6445                                     awk '($1 == "'$size':") {print $2; exit; }')
6446                         [ $rpc != 0 ] &&
6447                                 error "Small $((size*4))k read IO $rpc !"
6448                 done
6449                 echo "$osc_rpc_stats check passed!"
6450         done
6451         cleanup_test101bc
6452         true
6453 }
6454 run_test 101c "check stripe_size aligned read-ahead ================="
6455
6456 set_read_ahead() {
6457         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6458         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6459 }
6460
6461 test_101d() {
6462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6463         local file=$DIR/$tfile
6464         local sz_MB=${FILESIZE_101d:-500}
6465         local ra_MB=${READAHEAD_MB:-40}
6466
6467         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6468         [ $free_MB -lt $sz_MB ] &&
6469                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6470
6471         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6472         $SETSTRIPE -c -1 $file || error "setstripe failed"
6473
6474         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6475         echo Cancel LRU locks on lustre client to flush the client cache
6476         cancel_lru_locks osc
6477
6478         echo Disable read-ahead
6479         local old_READAHEAD=$(set_read_ahead 0)
6480
6481         echo Reading the test file $file with read-ahead disabled
6482         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6483
6484         echo Cancel LRU locks on lustre client to flush the client cache
6485         cancel_lru_locks osc
6486         echo Enable read-ahead with ${ra_MB}MB
6487         set_read_ahead $ra_MB
6488
6489         echo Reading the test file $file with read-ahead enabled
6490         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6491
6492         echo "read-ahead disabled time read $raOFF"
6493         echo "read-ahead enabled  time read $raON"
6494
6495         set_read_ahead $old_READAHEAD
6496         rm -f $file
6497         wait_delete_completed
6498
6499         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6500                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6501 }
6502 run_test 101d "file read with and without read-ahead enabled"
6503
6504 test_101e() {
6505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6506         local file=$DIR/$tfile
6507         local size_KB=500  #KB
6508         local count=100
6509         local bsize=1024
6510
6511         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6512         local need_KB=$((count * size_KB))
6513         [[ $free_KB -le $need_KB ]] &&
6514                 skip_env "Need free space $need_KB, have $free_KB" && return
6515
6516         echo "Creating $count ${size_KB}K test files"
6517         for ((i = 0; i < $count; i++)); do
6518                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6519         done
6520
6521         echo "Cancel LRU locks on lustre client to flush the client cache"
6522         cancel_lru_locks osc
6523
6524         echo "Reset readahead stats"
6525         $LCTL set_param -n llite.*.read_ahead_stats 0
6526
6527         for ((i = 0; i < $count; i++)); do
6528                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6529         done
6530
6531         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6532                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6533
6534         for ((i = 0; i < $count; i++)); do
6535                 rm -rf $file.$i 2>/dev/null
6536         done
6537
6538         #10000 means 20% reads are missing in readahead
6539         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6540 }
6541 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6542
6543 cleanup_test101f() {
6544     trap 0
6545     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6546     rm -rf $DIR/$tfile 2>/dev/null
6547 }
6548
6549 test_101f() {
6550         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6551     local file=$DIR/$tfile
6552     local nreads=1000
6553
6554     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6555     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6556     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6557     trap cleanup_test101f EXIT
6558
6559     echo Cancel LRU locks on lustre client to flush the client cache
6560     cancel_lru_locks osc
6561
6562     echo Reset readahead stats
6563     $LCTL set_param -n llite.*.read_ahead_stats 0
6564     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6565     # readahead should read in 2M file on second read, so only miss
6566     # 2 pages.
6567     echo Random 4K reads on 2M file for 1000 times
6568     $READS -f $file -s 2097152 -b 4096 -n $nreads
6569
6570     echo checking missing pages
6571     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6572           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6573
6574     [ $miss -lt 3 ] || error "misses too much pages!"
6575     cleanup_test101f
6576 }
6577 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6578
6579 setup_test102() {
6580         test_mkdir -p $DIR/$tdir
6581         chown $RUNAS_ID $DIR/$tdir
6582         STRIPE_SIZE=65536
6583         STRIPE_OFFSET=1
6584         STRIPE_COUNT=$OSTCOUNT
6585         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6586
6587         trap cleanup_test102 EXIT
6588         cd $DIR
6589         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6590         cd $DIR/$tdir
6591         for num in 1 2 3 4; do
6592                 for count in $(seq 1 $STRIPE_COUNT); do
6593                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6594                                 local size=`expr $STRIPE_SIZE \* $num`
6595                                 local file=file"$num-$idx-$count"
6596                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6597                         done
6598                 done
6599         done
6600
6601         cd $DIR
6602         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6603 }
6604
6605 cleanup_test102() {
6606         trap 0
6607         rm -f $TMP/f102.tar
6608         rm -rf $DIR/d0.sanity/d102
6609 }
6610
6611 test_102a() {
6612         local testfile=$DIR/$tfile
6613
6614         touch $testfile
6615
6616         [ "$UID" != 0 ] && skip_env "must run as root" && return
6617         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6618                 skip_env "must have user_xattr" && return
6619
6620         [ -z "$(which setfattr 2>/dev/null)" ] &&
6621                 skip_env "could not find setfattr" && return
6622
6623         echo "set/get xattr..."
6624         setfattr -n trusted.name1 -v value1 $testfile ||
6625                 error "setfattr -n trusted.name1=value1 $testfile failed"
6626         getfattr -n trusted.name1 $testfile 2> /dev/null |
6627           grep "trusted.name1=.value1" ||
6628                 error "$testfile missing trusted.name1=value1"
6629
6630         setfattr -n user.author1 -v author1 $testfile ||
6631                 error "setfattr -n user.author1=author1 $testfile failed"
6632         getfattr -n user.author1 $testfile 2> /dev/null |
6633           grep "user.author1=.author1" ||
6634                 error "$testfile missing trusted.author1=author1"
6635
6636         echo "listxattr..."
6637         setfattr -n trusted.name2 -v value2 $testfile ||
6638                 error "$testfile unable to set trusted.name2"
6639         setfattr -n trusted.name3 -v value3 $testfile ||
6640                 error "$testfile unable to set trusted.name3"
6641         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6642             grep "trusted.name" | wc -l) -eq 3 ] ||
6643                 error "$testfile missing 3 trusted.name xattrs"
6644
6645         setfattr -n user.author2 -v author2 $testfile ||
6646                 error "$testfile unable to set user.author2"
6647         setfattr -n user.author3 -v author3 $testfile ||
6648                 error "$testfile unable to set user.author3"
6649         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6650             grep "user.author" | wc -l) -eq 3 ] ||
6651                 error "$testfile missing 3 user.author xattrs"
6652
6653         echo "remove xattr..."
6654         setfattr -x trusted.name1 $testfile ||
6655                 error "$testfile error deleting trusted.name1"
6656         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6657                 error "$testfile did not delete trusted.name1 xattr"
6658
6659         setfattr -x user.author1 $testfile ||
6660                 error "$testfile error deleting user.author1"
6661         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6662                 error "$testfile did not delete trusted.name1 xattr"
6663
6664         # b10667: setting lustre special xattr be silently discarded
6665         echo "set lustre special xattr ..."
6666         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6667                 error "$testfile allowed setting trusted.lov"
6668 }
6669 run_test 102a "user xattr test =================================="
6670
6671 test_102b() {
6672         [ -z "$(which setfattr 2>/dev/null)" ] &&
6673                 skip_env "could not find setfattr" && return
6674
6675         # b10930: get/set/list trusted.lov xattr
6676         echo "get/set/list trusted.lov xattr ..."
6677         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6678         local testfile=$DIR/$tfile
6679         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6680                 error "setstripe failed"
6681         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6682                 error "getstripe failed"
6683         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6684                 error "can't get trusted.lov from $testfile"
6685
6686         local testfile2=${testfile}2
6687         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6688                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6689
6690         $MCREATE $testfile2
6691         setfattr -n trusted.lov -v $value $testfile2
6692         local stripe_size=$($GETSTRIPE -S $testfile2)
6693         local stripe_count=$($GETSTRIPE -c $testfile2)
6694         [[ $stripe_size -eq 65536 ]] ||
6695                 error "stripe size $stripe_size != 65536"
6696         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6697                 error "stripe count $stripe_count != $STRIPECOUNT"
6698         rm -f $DIR/$tfile
6699 }
6700 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6701
6702 test_102c() {
6703         [ -z "$(which setfattr 2>/dev/null)" ] &&
6704                 skip_env "could not find setfattr" && return
6705
6706         # b10930: get/set/list lustre.lov xattr
6707         echo "get/set/list lustre.lov xattr ..."
6708         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6709         test_mkdir -p $DIR/$tdir
6710         chown $RUNAS_ID $DIR/$tdir
6711         local testfile=$DIR/$tdir/$tfile
6712         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6713                 error "setstripe failed"
6714         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6715                 error "getstripe failed"
6716         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6717         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6718
6719         local testfile2=${testfile}2
6720         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6721                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6722
6723         $RUNAS $MCREATE $testfile2
6724         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6725         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6726         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6727         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6728         [ $stripe_count -eq $STRIPECOUNT ] ||
6729                 error "stripe count $stripe_count != $STRIPECOUNT"
6730 }
6731 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6732
6733 compare_stripe_info1() {
6734         local stripe_index_all_zero=true
6735
6736         for num in 1 2 3 4; do
6737                 for count in $(seq 1 $STRIPE_COUNT); do
6738                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6739                                 local size=$((STRIPE_SIZE * num))
6740                                 local file=file"$num-$offset-$count"
6741                                 stripe_size=$($LFS getstripe -S $PWD/$file)
6742                                 [[ $stripe_size -ne $size ]] &&
6743                                     error "$file: size $stripe_size != $size"
6744                                 stripe_count=$($LFS getstripe -c $PWD/$file)
6745                                 # allow fewer stripes to be created, ORI-601
6746                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6747                                     error "$file: count $stripe_count != $count"
6748                                 stripe_index=$($LFS getstripe -i $PWD/$file)
6749                                 [[ $stripe_index -ne 0 ]] &&
6750                                         stripe_index_all_zero=false
6751                         done
6752                 done
6753         done
6754         $stripe_index_all_zero &&
6755                 error "all files are being extracted starting from OST index 0"
6756         return 0
6757 }
6758
6759 find_lustre_tar() {
6760         [ -n "$(which tar 2>/dev/null)" ] &&
6761                 strings $(which tar) | grep -q "lustre" && echo tar
6762 }
6763
6764 test_102d() {
6765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6766         # b10930: tar test for trusted.lov xattr
6767         TAR=$(find_lustre_tar)
6768         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6769         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6770         setup_test102
6771         test_mkdir -p $DIR/d102d
6772         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6773         cd $DIR/d102d/$tdir
6774         compare_stripe_info1
6775 }
6776 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6777
6778 test_102f() {
6779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6780         # b10930: tar test for trusted.lov xattr
6781         TAR=$(find_lustre_tar)
6782         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6783         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6784         setup_test102
6785         test_mkdir -p $DIR/d102f
6786         cd $DIR
6787         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6788         cd $DIR/d102f/$tdir
6789         compare_stripe_info1
6790 }
6791 run_test 102f "tar copy files, not keep osts ==========="
6792
6793 grow_xattr() {
6794         local xsize=${1:-1024}  # in bytes
6795         local file=$DIR/$tfile
6796
6797         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
6798                 skip "must have user_xattr" && return 0
6799         [ -z "$(which setfattr 2>/dev/null)" ] &&
6800                 skip_env "could not find setfattr" && return 0
6801         [ -z "$(which getfattr 2>/dev/null)" ] &&
6802                 skip_env "could not find getfattr" && return 0
6803
6804         touch $file
6805
6806         local value="$(generate_string $xsize)"
6807
6808         local xbig=trusted.big
6809         log "save $xbig on $file"
6810         setfattr -n $xbig -v $value $file ||
6811                 error "saving $xbig on $file failed"
6812
6813         local orig=$(get_xattr_value $xbig $file)
6814         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6815
6816         local xsml=trusted.sml
6817         log "save $xsml on $file"
6818         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6819
6820         local new=$(get_xattr_value $xbig $file)
6821         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6822
6823         log "grow $xsml on $file"
6824         setfattr -n $xsml -v "$value" $file ||
6825                 error "growing $xsml on $file failed"
6826
6827         new=$(get_xattr_value $xbig $file)
6828         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6829         log "$xbig still valid after growing $xsml"
6830
6831         rm -f $file
6832 }
6833
6834 test_102h() { # bug 15777
6835         grow_xattr 1024
6836 }
6837 run_test 102h "grow xattr from inside inode to external block"
6838
6839 test_102ha() {
6840         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6841         grow_xattr $(max_xattr_size)
6842 }
6843 run_test 102ha "grow xattr from inside inode to external inode"
6844
6845 test_102i() { # bug 17038
6846         [ -z "$(which getfattr 2>/dev/null)" ] &&
6847                 skip "could not find getfattr" && return
6848         touch $DIR/$tfile
6849         ln -s $DIR/$tfile $DIR/${tfile}link
6850         getfattr -n trusted.lov $DIR/$tfile ||
6851                 error "lgetxattr on $DIR/$tfile failed"
6852         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6853                 grep -i "no such attr" ||
6854                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6855         rm -f $DIR/$tfile $DIR/${tfile}link
6856 }
6857 run_test 102i "lgetxattr test on symbolic link ============"
6858
6859 test_102j() {
6860         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6861         TAR=$(find_lustre_tar)
6862         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6863         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6864         setup_test102 "$RUNAS"
6865         test_mkdir -p $DIR/d102j
6866         chown $RUNAS_ID $DIR/d102j
6867         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6868         cd $DIR/d102j/$tdir
6869         compare_stripe_info1 "$RUNAS"
6870 }
6871 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6872
6873 test_102k() {
6874         [ -z "$(which setfattr 2>/dev/null)" ] &&
6875                 skip "could not find setfattr" && return
6876         touch $DIR/$tfile
6877         # b22187 just check that does not crash for regular file.
6878         setfattr -n trusted.lov $DIR/$tfile
6879         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6880         local test_kdir=$DIR/d102k
6881         test_mkdir $test_kdir
6882         local default_size=`$GETSTRIPE -S $test_kdir`
6883         local default_count=`$GETSTRIPE -c $test_kdir`
6884         local default_offset=`$GETSTRIPE -i $test_kdir`
6885         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6886                 error 'dir setstripe failed'
6887         setfattr -n trusted.lov $test_kdir
6888         local stripe_size=`$GETSTRIPE -S $test_kdir`
6889         local stripe_count=`$GETSTRIPE -c $test_kdir`
6890         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6891         [ $stripe_size -eq $default_size ] ||
6892                 error "stripe size $stripe_size != $default_size"
6893         [ $stripe_count -eq $default_count ] ||
6894                 error "stripe count $stripe_count != $default_count"
6895         [ $stripe_offset -eq $default_offset ] ||
6896                 error "stripe offset $stripe_offset != $default_offset"
6897         rm -rf $DIR/$tfile $test_kdir
6898 }
6899 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6900
6901 test_102l() {
6902         [ -z "$(which getfattr 2>/dev/null)" ] &&
6903                 skip "could not find getfattr" && return
6904
6905         # LU-532 trusted. xattr is invisible to non-root
6906         local testfile=$DIR/$tfile
6907
6908         touch $testfile
6909
6910         echo "listxattr as user..."
6911         chown $RUNAS_ID $testfile
6912         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6913             grep -q "trusted" &&
6914                 error "$testfile trusted xattrs are user visible"
6915
6916         return 0;
6917 }
6918 run_test 102l "listxattr size test =================================="
6919
6920 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6921         local path=$DIR/$tfile
6922         touch $path
6923
6924         listxattr_size_check $path || error "listattr_size_check $path failed"
6925 }
6926 run_test 102m "Ensure listxattr fails on small bufffer ========"
6927
6928 cleanup_test102
6929
6930 getxattr() { # getxattr path name
6931         # Return the base64 encoding of the value of xattr name on path.
6932         local path=$1
6933         local name=$2
6934
6935         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6936         # file: $path
6937         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6938         #
6939         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6940
6941         getfattr --absolute-names --encoding=base64 --name=$name $path |
6942                 awk -F= -v name=$name '$1 == name {
6943                         print substr($0, index($0, "=") + 1);
6944         }'
6945 }
6946
6947 test_102n() { # LU-4101 mdt: protect internal xattrs
6948         local file0=$DIR/$tfile.0
6949         local file1=$DIR/$tfile.1
6950         local xattr0=$TMP/$tfile.0
6951         local xattr1=$TMP/$tfile.1
6952         local name
6953         local value
6954
6955         [ -z "$(which setfattr 2>/dev/null)" ] &&
6956                 skip "could not find setfattr" && return
6957
6958         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6959         then
6960                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6961                 return
6962         fi
6963
6964         rm -rf $file0 $file1 $xattr0 $xattr1
6965         touch $file0 $file1
6966
6967         # Get 'before' xattrs of $file1.
6968         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6969
6970         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6971                 # Try to copy xattr from $file0 to $file1.
6972                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6973
6974                 setfattr --name=trusted.$name --value="$value" $file1 ||
6975                         error "setxattr 'trusted.$name' failed"
6976
6977                 # Try to set a garbage xattr.
6978                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6979
6980                 setfattr --name=trusted.$name --value="$value" $file1 ||
6981                         error "setxattr 'trusted.$name' failed"
6982
6983                 # Try to remove the xattr from $file1. We don't care if this
6984                 # appears to succeed or fail, we just don't want there to be
6985                 # any changes or crashes.
6986                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6987         done
6988
6989         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6990         then
6991                 name="lfsck_ns"
6992                 # Try to copy xattr from $file0 to $file1.
6993                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6994
6995                 setfattr --name=trusted.$name --value="$value" $file1 ||
6996                         error "setxattr 'trusted.$name' failed"
6997
6998                 # Try to set a garbage xattr.
6999                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7000
7001                 setfattr --name=trusted.$name --value="$value" $file1 ||
7002                         error "setxattr 'trusted.$name' failed"
7003
7004                 # Try to remove the xattr from $file1. We don't care if this
7005                 # appears to succeed or fail, we just don't want there to be
7006                 # any changes or crashes.
7007                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7008         fi
7009
7010         # Get 'after' xattrs of file1.
7011         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7012
7013         if ! diff $xattr0 $xattr1; then
7014                 error "before and after xattrs of '$file1' differ"
7015         fi
7016
7017         rm -rf $file0 $file1 $xattr0 $xattr1
7018
7019         return 0
7020 }
7021 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7022
7023 test_102p() { # LU-4703 setxattr did not check ownership
7024         local testfile=$DIR/$tfile
7025
7026         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7027                 skip "MDS needs to be at least 2.5.56" && return
7028
7029         touch $testfile
7030
7031         echo "setfacl as user..."
7032         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7033         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7034
7035         echo "setfattr as user..."
7036         setfacl -m "u:$RUNAS_ID:---" $testfile
7037         $RUNAS setfattr -x system.posix_acl_access $testfile
7038         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7039 }
7040 run_test 102p "check setxattr(2) correctly fails without permission"
7041
7042 test_102q() {
7043         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7044                 skip "MDS needs to be at least 2.6.92" && return
7045         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7046 }
7047 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7048
7049 test_102r() {
7050         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7051                 skip "MDS needs to be at least 2.6.93" && return
7052         touch $DIR/$tfile || error "touch"
7053         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7054         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7055         rm $DIR/$tfile || error "rm"
7056
7057         #normal directory
7058         mkdir -p $DIR/$tdir || error "mkdir"
7059         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7060         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7061         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7062                 error "$testfile error deleting user.author1"
7063         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7064                 grep "user.$(basename $tdir)" &&
7065                 error "$tdir did not delete user.$(basename $tdir)"
7066         rmdir $DIR/$tdir || error "rmdir"
7067
7068         #striped directory
7069         test_mkdir -p $DIR/$tdir || error "make striped dir"
7070         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7071         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7072         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7073                 error "$testfile error deleting user.author1"
7074         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7075                 grep "user.$(basename $tdir)" &&
7076                 error "$tdir did not delete user.$(basename $tdir)"
7077         rmdir $DIR/$tdir || error "rm striped dir"
7078 }
7079 run_test 102r "set EAs with empty values"
7080
7081 run_acl_subtest()
7082 {
7083     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7084     return $?
7085 }
7086
7087 test_103a() {
7088         [ "$UID" != 0 ] && skip_env "must run as root" && return
7089         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7090                 skip "must have acl enabled" && return
7091         [ -z "$(which setfacl 2>/dev/null)" ] &&
7092                 skip_env "could not find setfacl" && return
7093         $GSS && skip "could not run under gss" && return
7094
7095         gpasswd -a daemon bin                           # LU-5641
7096         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7097
7098         declare -a identity_old
7099
7100         for num in $(seq $MDSCOUNT); do
7101                 switch_identity $num true || identity_old[$num]=$?
7102         done
7103
7104         SAVE_UMASK=$(umask)
7105         umask 0022
7106         cd $DIR
7107
7108         echo "performing cp ..."
7109         run_acl_subtest cp || error "run_acl_subtest cp failed"
7110         echo "performing getfacl-noacl..."
7111         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7112         echo "performing misc..."
7113         run_acl_subtest misc || error  "misc test failed"
7114         echo "performing permissions..."
7115         run_acl_subtest permissions || error "permissions failed"
7116         echo "performing setfacl..."
7117         run_acl_subtest setfacl || error  "setfacl test failed"
7118
7119         # inheritance test got from HP
7120         echo "performing inheritance..."
7121         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7122         chmod +x make-tree || error "chmod +x failed"
7123         run_acl_subtest inheritance || error "inheritance test failed"
7124         rm -f make-tree
7125
7126         echo "LU-974 ignore umask when acl is enabled..."
7127         run_acl_subtest 974 || error "LU-974 umask test failed"
7128         if [ $MDSCOUNT -ge 2 ]; then
7129                 run_acl_subtest 974_remote ||
7130                         error "LU-974 umask test failed under remote dir"
7131         fi
7132
7133         echo "LU-2561 newly created file is same size as directory..."
7134         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7135                 run_acl_subtest 2561 || error "LU-2561 test failed"
7136         else
7137                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7138         fi
7139
7140         run_acl_subtest 4924 || error "LU-4924 test failed"
7141
7142         cd $SAVE_PWD
7143         umask $SAVE_UMASK
7144
7145         for num in $(seq $MDSCOUNT); do
7146                 if [ "${identity_old[$num]}" = 1 ]; then
7147                         switch_identity $num false || identity_old[$num]=$?
7148                 fi
7149         done
7150 }
7151 run_test 103a "acl test ========================================="
7152
7153 test_103b() {
7154         local noacl=false
7155         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7156         local mountopts=$MDS_MOUNT_OPTS
7157
7158         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7159                 noacl=true
7160         else
7161                 # stop the MDT
7162                 stop $SINGLEMDS || error "failed to stop MDT."
7163                 # remount the MDT
7164                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7165                         MDS_MOUNT_OPTS="-o noacl"
7166                 else
7167                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7168                 fi
7169                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7170                         error "failed to start MDT."
7171                 MDS_MOUNT_OPTS=$mountopts
7172         fi
7173
7174         touch $DIR/$tfile
7175         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7176
7177         if ! $noacl; then
7178                 # stop the MDT
7179                 stop $SINGLEMDS || error "failed to stop MDT."
7180                 # remount the MDT
7181                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7182                         error "failed to start MDT."
7183         fi
7184
7185         true
7186 }
7187 run_test 103b "MDS mount option 'noacl'"
7188
7189 test_103c() {
7190         mkdir -p $DIR/$tdir
7191         cp -rp $DIR/$tdir $DIR/$tdir.bak
7192
7193         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7194                 error "$DIR/$tdir shouldn't contain default ACL"
7195         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7196                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7197         true
7198 }
7199 run_test 103c "'cp -rp' won't set empty acl"
7200
7201 test_104a() {
7202         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7203         touch $DIR/$tfile
7204         lfs df || error "lfs df failed"
7205         lfs df -ih || error "lfs df -ih failed"
7206         lfs df -h $DIR || error "lfs df -h $DIR failed"
7207         lfs df -i $DIR || error "lfs df -i $DIR failed"
7208         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7209         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7210
7211         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7212         lctl --device %$OSC deactivate
7213         lfs df || error "lfs df with deactivated OSC failed"
7214         lctl --device %$OSC activate
7215         # wait the osc back to normal
7216         wait_osc_import_state client ost FULL
7217
7218         lfs df || error "lfs df with reactivated OSC failed"
7219         rm -f $DIR/$tfile
7220 }
7221 run_test 104a "lfs df [-ih] [path] test ========================="
7222
7223 test_104b() {
7224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7225         [ $RUNAS_ID -eq $UID ] &&
7226                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7227         chmod 666 /dev/obd
7228         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7229                         grep "Permission denied" | wc -l)))
7230         if [ $denied_cnt -ne 0 ]; then
7231                 error "lfs check servers test failed"
7232         fi
7233 }
7234 run_test 104b "$RUNAS lfs check servers test ===================="
7235
7236 test_105a() {
7237         # doesn't work on 2.4 kernels
7238         touch $DIR/$tfile
7239         if $(flock_is_enabled); then
7240                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7241         else
7242                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7243         fi
7244         rm -f $DIR/$tfile
7245 }
7246 run_test 105a "flock when mounted without -o flock test ========"
7247
7248 test_105b() {
7249         touch $DIR/$tfile
7250         if $(flock_is_enabled); then
7251                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7252         else
7253                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7254         fi
7255         rm -f $DIR/$tfile
7256 }
7257 run_test 105b "fcntl when mounted without -o flock test ========"
7258
7259 test_105c() {
7260         touch $DIR/$tfile
7261         if $(flock_is_enabled); then
7262                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7263         else
7264                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7265         fi
7266         rm -f $DIR/$tfile
7267 }
7268 run_test 105c "lockf when mounted without -o flock test ========"
7269
7270 test_105d() { # bug 15924
7271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7272         test_mkdir -p $DIR/$tdir
7273         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7274         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7275         $LCTL set_param fail_loc=0x80000315
7276         flocks_test 2 $DIR/$tdir
7277 }
7278 run_test 105d "flock race (should not freeze) ========"
7279
7280 test_105e() { # bug 22660 && 22040
7281         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7282         touch $DIR/$tfile
7283         flocks_test 3 $DIR/$tfile
7284 }
7285 run_test 105e "Two conflicting flocks from same process ======="
7286
7287 test_106() { #bug 10921
7288         test_mkdir -p $DIR/$tdir
7289         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7290         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7291 }
7292 run_test 106 "attempt exec of dir followed by chown of that dir"
7293
7294 test_107() {
7295         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7296         CDIR=`pwd`
7297         cd $DIR
7298
7299         local file=core
7300         rm -f $file
7301
7302         local save_pattern=$(sysctl -n kernel.core_pattern)
7303         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7304         sysctl -w kernel.core_pattern=$file
7305         sysctl -w kernel.core_uses_pid=0
7306
7307         ulimit -c unlimited
7308         sleep 60 &
7309         SLEEPPID=$!
7310
7311         sleep 1
7312
7313         kill -s 11 $SLEEPPID
7314         wait $SLEEPPID
7315         if [ -e $file ]; then
7316                 size=`stat -c%s $file`
7317                 [ $size -eq 0 ] && error "Fail to create core file $file"
7318         else
7319                 error "Fail to create core file $file"
7320         fi
7321         rm -f $file
7322         sysctl -w kernel.core_pattern=$save_pattern
7323         sysctl -w kernel.core_uses_pid=$save_uses_pid
7324         cd $CDIR
7325 }
7326 run_test 107 "Coredump on SIG"
7327
7328 test_110() {
7329         test_mkdir -p $DIR/$tdir
7330         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7331                 error "mkdir with 255 char failed"
7332         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7333                 error "mkdir with 256 char should fail, but did not"
7334         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7335                 error "create with 255 char failed"
7336         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7337                 error "create with 256 char should fail, but did not"
7338
7339         ls -l $DIR/$tdir
7340         rm -rf $DIR/$tdir
7341 }
7342 run_test 110 "filename length checking"
7343
7344 test_115() {
7345         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7346         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7347                 tail -1 | cut -c11-20)
7348         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7349         echo "Starting with $OSTIO_pre threads"
7350
7351         NUMTEST=20000
7352         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7353         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7354         echo "$NUMTEST creates/unlinks"
7355         test_mkdir -p $DIR/$tdir
7356         createmany -o $DIR/$tdir/$tfile $NUMTEST
7357         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7358
7359         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7360                 tail -1 | cut -c11-20)
7361
7362         # don't return an error
7363         [ $OSTIO_post == $OSTIO_pre ] && echo \
7364             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7365             echo "This may be fine, depending on what ran before this test" &&
7366             echo "and how fast this system is." && return
7367
7368         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7369 }
7370 run_test 115 "verify dynamic thread creation===================="
7371
7372 free_min_max () {
7373         wait_delete_completed
7374         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7375         echo OST kbytes available: ${AVAIL[@]}
7376         MAXI=0; MAXV=${AVAIL[0]}
7377         MINI=0; MINV=${AVAIL[0]}
7378         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7379                 #echo OST $i: ${AVAIL[i]}kb
7380                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7381                         MAXV=${AVAIL[i]}; MAXI=$i
7382                 fi
7383                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7384                         MINV=${AVAIL[i]}; MINI=$i
7385                 fi
7386         done
7387         echo Min free space: OST $MINI: $MINV
7388         echo Max free space: OST $MAXI: $MAXV
7389 }
7390
7391 test_116a() { # was previously test_116()
7392         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7393         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7394
7395         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7396
7397         echo -n "Free space priority "
7398         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7399                 head -n1
7400         declare -a AVAIL
7401         free_min_max
7402
7403         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7404         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7405                 && return
7406         trap simple_cleanup_common EXIT
7407
7408
7409         # Check if we need to generate uneven OSTs
7410         test_mkdir -p $DIR/$tdir/OST${MINI}
7411         local FILL=$(($MINV / 4))
7412         local DIFF=$(($MAXV - $MINV))
7413         local DIFF2=$(($DIFF * 100 / $MINV))
7414
7415         local threshold=$(do_facet $SINGLEMDS \
7416                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7417         threshold=${threshold%%%}
7418         echo -n "Check for uneven OSTs: "
7419         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7420
7421         if [[ $DIFF2 -gt $threshold ]]; then
7422                 echo "ok"
7423                 echo "Don't need to fill OST$MINI"
7424         else
7425                 # generate uneven OSTs. Write 2% over the QOS threshold value
7426                 echo "no"
7427                 DIFF=$(($threshold - $DIFF2 + 2))
7428                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7429                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7430                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7431                         error "setstripe failed"
7432                 DIFF=$(($DIFF2 / 2048))
7433                 i=0
7434                 while [ $i -lt $DIFF ]; do
7435                         i=$(($i + 1))
7436                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7437                                 bs=2M count=1 2>/dev/null
7438                         echo -n .
7439                 done
7440                 echo .
7441                 sync
7442                 sleep_maxage
7443                 free_min_max
7444         fi
7445
7446         DIFF=$(($MAXV - $MINV))
7447         DIFF2=$(($DIFF * 100 / $MINV))
7448         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7449         if [[ $DIFF2 -gt $threshold ]]; then
7450                 echo "ok"
7451         else
7452                 echo "failed - QOS mode won't be used"
7453                 skip "QOS imbalance criteria not met"
7454                 simple_cleanup_common
7455                 return
7456         fi
7457
7458         MINI1=$MINI; MINV1=$MINV
7459         MAXI1=$MAXI; MAXV1=$MAXV
7460
7461         # now fill using QOS
7462         $SETSTRIPE -c 1 $DIR/$tdir
7463         FILL=$(($FILL / 200))
7464         if [ $FILL -gt 600 ]; then
7465                 FILL=600
7466         fi
7467         echo "writing $FILL files to QOS-assigned OSTs"
7468         i=0
7469         while [ $i -lt $FILL ]; do
7470                 i=$((i + 1))
7471                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7472                         count=1 2>/dev/null
7473                 echo -n .
7474         done
7475         echo "wrote $i 200k files"
7476         sync
7477         sleep_maxage
7478
7479         echo "Note: free space may not be updated, so measurements might be off"
7480         free_min_max
7481         DIFF2=$(($MAXV - $MINV))
7482         echo "free space delta: orig $DIFF final $DIFF2"
7483         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7484         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7485         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7486         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7487         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7488         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7489         [ $DIFF -gt 0 ] &&
7490                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7491
7492         # Figure out which files were written where
7493         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7494                   awk '/'$MINI1': / {print $2; exit}')
7495         echo $UUID
7496         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7497         echo "$MINC files created on smaller OST $MINI1"
7498         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7499                   awk '/'$MAXI1': / {print $2; exit}')
7500         echo $UUID
7501         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7502         echo "$MAXC files created on larger OST $MAXI1"
7503         FILL=$(($MAXC * 100 / $MINC - 100))
7504         [[ $MINC -gt 0 ]] &&
7505                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7506         [[ $MAXC -gt $MINC ]] ||
7507                 error_ignore LU-9 "stripe QOS didn't balance free space"
7508         simple_cleanup_common
7509 }
7510 run_test 116a "stripe QOS: free space balance ==================="
7511
7512 test_116b() { # LU-2093
7513         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7514         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7515
7516 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7517         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7518                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7519         [ -z "$old_rr" ] && skip "no QOS" && return 0
7520         do_facet $SINGLEMDS lctl set_param \
7521                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7522         mkdir -p $DIR/$tdir
7523         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7524         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7525         do_facet $SINGLEMDS lctl set_param fail_loc=0
7526         rm -rf $DIR/$tdir
7527         do_facet $SINGLEMDS lctl set_param \
7528                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7529 }
7530 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7531
7532 test_117() # bug 10891
7533 {
7534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7535         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7536         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7537         lctl set_param fail_loc=0x21e
7538         > $DIR/$tfile || error "truncate failed"
7539         lctl set_param fail_loc=0
7540         echo "Truncate succeeded."
7541         rm -f $DIR/$tfile
7542 }
7543 run_test 117 "verify osd extend =========="
7544
7545 NO_SLOW_RESENDCOUNT=4
7546 export OLD_RESENDCOUNT=""
7547 set_resend_count () {
7548         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7549         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7550         lctl set_param -n $PROC_RESENDCOUNT $1
7551         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7552 }
7553
7554 # for reduce test_118* time (b=14842)
7555 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7556
7557 # Reset async IO behavior after error case
7558 reset_async() {
7559         FILE=$DIR/reset_async
7560
7561         # Ensure all OSCs are cleared
7562         $SETSTRIPE -c -1 $FILE
7563         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7564         sync
7565         rm $FILE
7566 }
7567
7568 test_118a() #bug 11710
7569 {
7570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7571         reset_async
7572
7573         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7574         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7575         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7576
7577         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7578                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7579                 return 1;
7580         fi
7581         rm -f $DIR/$tfile
7582 }
7583 run_test 118a "verify O_SYNC works =========="
7584
7585 test_118b()
7586 {
7587         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7588         remote_ost_nodsh && skip "remote OST with nodsh" && return
7589
7590         reset_async
7591
7592         #define OBD_FAIL_OST_ENOENT 0x217
7593         set_nodes_failloc "$(osts_nodes)" 0x217
7594         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7595         RC=$?
7596         set_nodes_failloc "$(osts_nodes)" 0
7597         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7598         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7599                     grep -c writeback)
7600
7601         if [[ $RC -eq 0 ]]; then
7602                 error "Must return error due to dropped pages, rc=$RC"
7603                 return 1;
7604         fi
7605
7606         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7607                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7608                 return 1;
7609         fi
7610
7611         echo "Dirty pages not leaked on ENOENT"
7612
7613         # Due to the above error the OSC will issue all RPCs syncronously
7614         # until a subsequent RPC completes successfully without error.
7615         $MULTIOP $DIR/$tfile Ow4096yc
7616         rm -f $DIR/$tfile
7617
7618         return 0
7619 }
7620 run_test 118b "Reclaim dirty pages on fatal error =========="
7621
7622 test_118c()
7623 {
7624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7625
7626         # for 118c, restore the original resend count, LU-1940
7627         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7628                                 set_resend_count $OLD_RESENDCOUNT
7629         remote_ost_nodsh && skip "remote OST with nodsh" && return
7630
7631         reset_async
7632
7633         #define OBD_FAIL_OST_EROFS               0x216
7634         set_nodes_failloc "$(osts_nodes)" 0x216
7635
7636         # multiop should block due to fsync until pages are written
7637         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7638         MULTIPID=$!
7639         sleep 1
7640
7641         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7642                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7643         fi
7644
7645         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7646                     grep -c writeback)
7647         if [[ $WRITEBACK -eq 0 ]]; then
7648                 error "No page in writeback, writeback=$WRITEBACK"
7649         fi
7650
7651         set_nodes_failloc "$(osts_nodes)" 0
7652         wait $MULTIPID
7653         RC=$?
7654         if [[ $RC -ne 0 ]]; then
7655                 error "Multiop fsync failed, rc=$RC"
7656         fi
7657
7658         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7659         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7660                     grep -c writeback)
7661         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7662                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7663         fi
7664
7665         rm -f $DIR/$tfile
7666         echo "Dirty pages flushed via fsync on EROFS"
7667         return 0
7668 }
7669 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7670
7671 # continue to use small resend count to reduce test_118* time (b=14842)
7672 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7673
7674 test_118d()
7675 {
7676         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7677         remote_ost_nodsh && skip "remote OST with nodsh" && return
7678
7679         reset_async
7680
7681         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7682         set_nodes_failloc "$(osts_nodes)" 0x214
7683         # multiop should block due to fsync until pages are written
7684         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7685         MULTIPID=$!
7686         sleep 1
7687
7688         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7689                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7690         fi
7691
7692         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7693                     grep -c writeback)
7694         if [[ $WRITEBACK -eq 0 ]]; then
7695                 error "No page in writeback, writeback=$WRITEBACK"
7696         fi
7697
7698         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7699         set_nodes_failloc "$(osts_nodes)" 0
7700
7701         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7702         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7703                     grep -c writeback)
7704         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7705                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7706         fi
7707
7708         rm -f $DIR/$tfile
7709         echo "Dirty pages gaurenteed flushed via fsync"
7710         return 0
7711 }
7712 run_test 118d "Fsync validation inject a delay of the bulk =========="
7713
7714 test_118f() {
7715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7716         reset_async
7717
7718         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7719         lctl set_param fail_loc=0x8000040a
7720
7721         # Should simulate EINVAL error which is fatal
7722         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7723         RC=$?
7724         if [[ $RC -eq 0 ]]; then
7725                 error "Must return error due to dropped pages, rc=$RC"
7726         fi
7727
7728         lctl set_param fail_loc=0x0
7729
7730         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7731         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7732         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7733                     grep -c writeback)
7734         if [[ $LOCKED -ne 0 ]]; then
7735                 error "Locked pages remain in cache, locked=$LOCKED"
7736         fi
7737
7738         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7739                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7740         fi
7741
7742         rm -f $DIR/$tfile
7743         echo "No pages locked after fsync"
7744
7745         reset_async
7746         return 0
7747 }
7748 run_test 118f "Simulate unrecoverable OSC side error =========="
7749
7750 test_118g() {
7751         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7752         reset_async
7753
7754         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7755         lctl set_param fail_loc=0x406
7756
7757         # simulate local -ENOMEM
7758         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7759         RC=$?
7760
7761         lctl set_param fail_loc=0
7762         if [[ $RC -eq 0 ]]; then
7763                 error "Must return error due to dropped pages, rc=$RC"
7764         fi
7765
7766         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7767         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7768         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7769                         grep -c writeback)
7770         if [[ $LOCKED -ne 0 ]]; then
7771                 error "Locked pages remain in cache, locked=$LOCKED"
7772         fi
7773
7774         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7775                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7776         fi
7777
7778         rm -f $DIR/$tfile
7779         echo "No pages locked after fsync"
7780
7781         reset_async
7782         return 0
7783 }
7784 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7785
7786 test_118h() {
7787         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7788         remote_ost_nodsh && skip "remote OST with nodsh" && return
7789
7790         reset_async
7791
7792         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7793         set_nodes_failloc "$(osts_nodes)" 0x20e
7794         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7795         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7796         RC=$?
7797
7798         set_nodes_failloc "$(osts_nodes)" 0
7799         if [[ $RC -eq 0 ]]; then
7800                 error "Must return error due to dropped pages, rc=$RC"
7801         fi
7802
7803         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7804         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7805         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7806                     grep -c writeback)
7807         if [[ $LOCKED -ne 0 ]]; then
7808                 error "Locked pages remain in cache, locked=$LOCKED"
7809         fi
7810
7811         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7812                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7813         fi
7814
7815         rm -f $DIR/$tfile
7816         echo "No pages locked after fsync"
7817
7818         return 0
7819 }
7820 run_test 118h "Verify timeout in handling recoverables errors  =========="
7821
7822 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7823
7824 test_118i() {
7825         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7826         remote_ost_nodsh && skip "remote OST with nodsh" && return
7827
7828         reset_async
7829
7830         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7831         set_nodes_failloc "$(osts_nodes)" 0x20e
7832
7833         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7834         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7835         PID=$!
7836         sleep 5
7837         set_nodes_failloc "$(osts_nodes)" 0
7838
7839         wait $PID
7840         RC=$?
7841         if [[ $RC -ne 0 ]]; then
7842                 error "got error, but should be not, rc=$RC"
7843         fi
7844
7845         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7846         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7847         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7848         if [[ $LOCKED -ne 0 ]]; then
7849                 error "Locked pages remain in cache, locked=$LOCKED"
7850         fi
7851
7852         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7853                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7854         fi
7855
7856         rm -f $DIR/$tfile
7857         echo "No pages locked after fsync"
7858
7859         return 0
7860 }
7861 run_test 118i "Fix error before timeout in recoverable error  =========="
7862
7863 [ "$SLOW" = "no" ] && set_resend_count 4
7864
7865 test_118j() {
7866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7867         remote_ost_nodsh && skip "remote OST with nodsh" && return
7868
7869         reset_async
7870
7871         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7872         set_nodes_failloc "$(osts_nodes)" 0x220
7873
7874         # return -EIO from OST
7875         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7876         RC=$?
7877         set_nodes_failloc "$(osts_nodes)" 0x0
7878         if [[ $RC -eq 0 ]]; then
7879                 error "Must return error due to dropped pages, rc=$RC"
7880         fi
7881
7882         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7883         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7884         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7885         if [[ $LOCKED -ne 0 ]]; then
7886                 error "Locked pages remain in cache, locked=$LOCKED"
7887         fi
7888
7889         # in recoverable error on OST we want resend and stay until it finished
7890         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7891                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7892         fi
7893
7894         rm -f $DIR/$tfile
7895         echo "No pages locked after fsync"
7896
7897         return 0
7898 }
7899 run_test 118j "Simulate unrecoverable OST side error =========="
7900
7901 test_118k()
7902 {
7903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7904         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7905
7906         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7907         set_nodes_failloc "$(osts_nodes)" 0x20e
7908         test_mkdir -p $DIR/$tdir
7909
7910         for ((i=0;i<10;i++)); do
7911                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7912                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7913                 SLEEPPID=$!
7914                 sleep 0.500s
7915                 kill $SLEEPPID
7916                 wait $SLEEPPID
7917         done
7918
7919         set_nodes_failloc "$(osts_nodes)" 0
7920         rm -rf $DIR/$tdir
7921 }
7922 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7923
7924 test_118l()
7925 {
7926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7927         # LU-646
7928         test_mkdir -p $DIR/$tdir
7929         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7930         rm -rf $DIR/$tdir
7931 }
7932 run_test 118l "fsync dir ========="
7933
7934 test_118m() # LU-3066
7935 {
7936         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7937         test_mkdir -p $DIR/$tdir
7938         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7939         rm -rf $DIR/$tdir
7940 }
7941 run_test 118m "fdatasync dir ========="
7942
7943 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7944
7945 test_119a() # bug 11737
7946 {
7947         BSIZE=$((512 * 1024))
7948         directio write $DIR/$tfile 0 1 $BSIZE
7949         # We ask to read two blocks, which is more than a file size.
7950         # directio will indicate an error when requested and actual
7951         # sizes aren't equeal (a normal situation in this case) and
7952         # print actual read amount.
7953         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7954         if [ "$NOB" != "$BSIZE" ]; then
7955                 error "read $NOB bytes instead of $BSIZE"
7956         fi
7957         rm -f $DIR/$tfile
7958 }
7959 run_test 119a "Short directIO read must return actual read amount"
7960
7961 test_119b() # bug 11737
7962 {
7963         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7964
7965         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7966         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7967         sync
7968         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7969                 error "direct read failed"
7970         rm -f $DIR/$tfile
7971 }
7972 run_test 119b "Sparse directIO read must return actual read amount"
7973
7974 test_119c() # bug 13099
7975 {
7976         BSIZE=1048576
7977         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7978         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7979         rm -f $DIR/$tfile
7980 }
7981 run_test 119c "Testing for direct read hitting hole"
7982
7983 test_119d() # bug 15950
7984 {
7985         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7986         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7987         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7988         BSIZE=1048576
7989         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7990         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7991         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7992         lctl set_param fail_loc=0x40d
7993         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7994         pid_dio=$!
7995         sleep 1
7996         cat $DIR/$tfile > /dev/null &
7997         lctl set_param fail_loc=0
7998         pid_reads=$!
7999         wait $pid_dio
8000         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8001         sleep 2
8002         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8003         error "the read rpcs have not completed in 2s"
8004         rm -f $DIR/$tfile
8005         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8006 }
8007 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8008
8009 test_120a() {
8010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8011         test_mkdir -p $DIR/$tdir
8012         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8013                skip "no early lock cancel on server" && return 0
8014
8015         lru_resize_disable mdc
8016         lru_resize_disable osc
8017         cancel_lru_locks mdc
8018         # asynchronous object destroy at MDT could cause bl ast to client
8019         cancel_lru_locks osc
8020
8021         stat $DIR/$tdir > /dev/null
8022         can1=$(do_facet $SINGLEMDS \
8023                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8024                awk '/ldlm_cancel/ {print $2}')
8025         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8026                awk '/ldlm_bl_callback/ {print $2}')
8027         test_mkdir -c1 $DIR/$tdir/d1
8028         can2=$(do_facet $SINGLEMDS \
8029                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8030                awk '/ldlm_cancel/ {print $2}')
8031         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8032                awk '/ldlm_bl_callback/ {print $2}')
8033         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8034         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8035         lru_resize_enable mdc
8036         lru_resize_enable osc
8037 }
8038 run_test 120a "Early Lock Cancel: mkdir test"
8039
8040 test_120b() {
8041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8042         test_mkdir $DIR/$tdir
8043         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8044                skip "no early lock cancel on server" && return 0
8045         lru_resize_disable mdc
8046         lru_resize_disable osc
8047         cancel_lru_locks mdc
8048         stat $DIR/$tdir > /dev/null
8049         can1=$(do_facet $SINGLEMDS \
8050                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8051                awk '/ldlm_cancel/ {print $2}')
8052         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8053                awk '/ldlm_bl_callback/ {print $2}')
8054         touch $DIR/$tdir/f1
8055         can2=$(do_facet $SINGLEMDS \
8056                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8057                awk '/ldlm_cancel/ {print $2}')
8058         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8059                awk '/ldlm_bl_callback/ {print $2}')
8060         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8061         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8062         lru_resize_enable mdc
8063         lru_resize_enable osc
8064 }
8065 run_test 120b "Early Lock Cancel: create test"
8066
8067 test_120c() {
8068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8069         test_mkdir -c1 $DIR/$tdir
8070         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8071                skip "no early lock cancel on server" && return 0
8072         lru_resize_disable mdc
8073         lru_resize_disable osc
8074         test_mkdir -p -c1 $DIR/$tdir/d1
8075         test_mkdir -p -c1 $DIR/$tdir/d2
8076         touch $DIR/$tdir/d1/f1
8077         cancel_lru_locks mdc
8078         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8079         can1=$(do_facet $SINGLEMDS \
8080                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8081                awk '/ldlm_cancel/ {print $2}')
8082         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8083                awk '/ldlm_bl_callback/ {print $2}')
8084         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8085         can2=$(do_facet $SINGLEMDS \
8086                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8087                awk '/ldlm_cancel/ {print $2}')
8088         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8089                awk '/ldlm_bl_callback/ {print $2}')
8090         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8091         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8092         lru_resize_enable mdc
8093         lru_resize_enable osc
8094 }
8095 run_test 120c "Early Lock Cancel: link test"
8096
8097 test_120d() {
8098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8099         test_mkdir -p -c1 $DIR/$tdir
8100         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8101                skip "no early lock cancel on server" && return 0
8102         lru_resize_disable mdc
8103         lru_resize_disable osc
8104         touch $DIR/$tdir
8105         cancel_lru_locks mdc
8106         stat $DIR/$tdir > /dev/null
8107         can1=$(do_facet $SINGLEMDS \
8108                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8109                awk '/ldlm_cancel/ {print $2}')
8110         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8111                awk '/ldlm_bl_callback/ {print $2}')
8112         chmod a+x $DIR/$tdir
8113         can2=$(do_facet $SINGLEMDS \
8114                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8115                awk '/ldlm_cancel/ {print $2}')
8116         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8117                awk '/ldlm_bl_callback/ {print $2}')
8118         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8119         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8120         lru_resize_enable mdc
8121         lru_resize_enable osc
8122 }
8123 run_test 120d "Early Lock Cancel: setattr test"
8124
8125 test_120e() {
8126         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8127         test_mkdir -p -c1 $DIR/$tdir
8128         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8129                skip "no early lock cancel on server" && return 0
8130         lru_resize_disable mdc
8131         lru_resize_disable osc
8132         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8133         cancel_lru_locks mdc
8134         cancel_lru_locks osc
8135         dd if=$DIR/$tdir/f1 of=/dev/null
8136         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8137         # XXX client can not do early lock cancel of OST lock
8138         # during unlink (LU-4206), so cancel osc lock now.
8139         cancel_lru_locks osc
8140         can1=$(do_facet $SINGLEMDS \
8141                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8142                awk '/ldlm_cancel/ {print $2}')
8143         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8144                awk '/ldlm_bl_callback/ {print $2}')
8145         unlink $DIR/$tdir/f1
8146         sleep 5
8147         can2=$(do_facet $SINGLEMDS \
8148                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8149                awk '/ldlm_cancel/ {print $2}')
8150         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8151                awk '/ldlm_bl_callback/ {print $2}')
8152         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8153         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8154         lru_resize_enable mdc
8155         lru_resize_enable osc
8156 }
8157 run_test 120e "Early Lock Cancel: unlink test"
8158
8159 test_120f() {
8160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8161         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8162                skip "no early lock cancel on server" && return 0
8163         test_mkdir -p -c1 $DIR/$tdir
8164         lru_resize_disable mdc
8165         lru_resize_disable osc
8166         test_mkdir -p -c1 $DIR/$tdir/d1
8167         test_mkdir -p -c1 $DIR/$tdir/d2
8168         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8169         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8170         cancel_lru_locks mdc
8171         cancel_lru_locks osc
8172         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8173         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8174         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8175         # XXX client can not do early lock cancel of OST lock
8176         # during rename (LU-4206), so cancel osc lock now.
8177         cancel_lru_locks osc
8178         can1=$(do_facet $SINGLEMDS \
8179                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8180                awk '/ldlm_cancel/ {print $2}')
8181         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8182                awk '/ldlm_bl_callback/ {print $2}')
8183         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8184         sleep 5
8185         can2=$(do_facet $SINGLEMDS \
8186                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8187                awk '/ldlm_cancel/ {print $2}')
8188         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8189                awk '/ldlm_bl_callback/ {print $2}')
8190         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8191         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8192         lru_resize_enable mdc
8193         lru_resize_enable osc
8194 }
8195 run_test 120f "Early Lock Cancel: rename test"
8196
8197 test_120g() {
8198         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8199         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8200                skip "no early lock cancel on server" && return 0
8201         lru_resize_disable mdc
8202         lru_resize_disable osc
8203         count=10000
8204         echo create $count files
8205         test_mkdir -p $DIR/$tdir
8206         cancel_lru_locks mdc
8207         cancel_lru_locks osc
8208         t0=`date +%s`
8209
8210         can0=$(do_facet $SINGLEMDS \
8211                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8212                awk '/ldlm_cancel/ {print $2}')
8213         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8214                awk '/ldlm_bl_callback/ {print $2}')
8215         createmany -o $DIR/$tdir/f $count
8216         sync
8217         can1=$(do_facet $SINGLEMDS \
8218                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8219                awk '/ldlm_cancel/ {print $2}')
8220         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8221                awk '/ldlm_bl_callback/ {print $2}')
8222         t1=$(date +%s)
8223         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8224         echo rm $count files
8225         rm -r $DIR/$tdir
8226         sync
8227         can2=$(do_facet $SINGLEMDS \
8228                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8229                awk '/ldlm_cancel/ {print $2}')
8230         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8231                awk '/ldlm_bl_callback/ {print $2}')
8232         t2=$(date +%s)
8233         echo total: $count removes in $((t2-t1))
8234         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8235         sleep 2
8236         # wait for commitment of removal
8237         lru_resize_enable mdc
8238         lru_resize_enable osc
8239 }
8240 run_test 120g "Early Lock Cancel: performance test"
8241
8242 test_121() { #bug #10589
8243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8244         rm -rf $DIR/$tfile
8245         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8246 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8247         lctl set_param fail_loc=0x310
8248         cancel_lru_locks osc > /dev/null
8249         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8250         lctl set_param fail_loc=0
8251         [[ $reads -eq $writes ]] ||
8252                 error "read $reads blocks, must be $writes blocks"
8253 }
8254 run_test 121 "read cancel race ========="
8255
8256 test_123a() { # was test 123, statahead(bug 11401)
8257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8258         SLOWOK=0
8259         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8260             log "testing on UP system. Performance may be not as good as expected."
8261                         SLOWOK=1
8262         fi
8263
8264         rm -rf $DIR/$tdir
8265         test_mkdir -p $DIR/$tdir
8266         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8267         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8268         MULT=10
8269         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8270                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8271
8272                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8273                 lctl set_param -n llite.*.statahead_max 0
8274                 lctl get_param llite.*.statahead_max
8275                 cancel_lru_locks mdc
8276                 cancel_lru_locks osc
8277                 stime=`date +%s`
8278                 time ls -l $DIR/$tdir | wc -l
8279                 etime=`date +%s`
8280                 delta=$((etime - stime))
8281                 log "ls $i files without statahead: $delta sec"
8282                 lctl set_param llite.*.statahead_max=$max
8283
8284                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8285                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8286                 cancel_lru_locks mdc
8287                 cancel_lru_locks osc
8288                 stime=`date +%s`
8289                 time ls -l $DIR/$tdir | wc -l
8290                 etime=`date +%s`
8291                 delta_sa=$((etime - stime))
8292                 log "ls $i files with statahead: $delta_sa sec"
8293                 lctl get_param -n llite.*.statahead_stats
8294                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8295
8296                 [[ $swrong -lt $ewrong ]] &&
8297                         log "statahead was stopped, maybe too many locks held!"
8298                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8299
8300                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8301                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8302                     lctl set_param -n llite.*.statahead_max 0
8303                     lctl get_param llite.*.statahead_max
8304                     cancel_lru_locks mdc
8305                     cancel_lru_locks osc
8306                     stime=`date +%s`
8307                     time ls -l $DIR/$tdir | wc -l
8308                     etime=`date +%s`
8309                     delta=$((etime - stime))
8310                     log "ls $i files again without statahead: $delta sec"
8311                     lctl set_param llite.*.statahead_max=$max
8312                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8313                         if [  $SLOWOK -eq 0 ]; then
8314                                 error "ls $i files is slower with statahead!"
8315                         else
8316                                 log "ls $i files is slower with statahead!"
8317                         fi
8318                         break
8319                     fi
8320                 fi
8321
8322                 [ $delta -gt 20 ] && break
8323                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8324                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8325         done
8326         log "ls done"
8327
8328         stime=`date +%s`
8329         rm -r $DIR/$tdir
8330         sync
8331         etime=`date +%s`
8332         delta=$((etime - stime))
8333         log "rm -r $DIR/$tdir/: $delta seconds"
8334         log "rm done"
8335         lctl get_param -n llite.*.statahead_stats
8336 }
8337 run_test 123a "verify statahead work"
8338
8339 test_123b () { # statahead(bug 15027)
8340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8341         test_mkdir -p $DIR/$tdir
8342         createmany -o $DIR/$tdir/$tfile-%d 1000
8343
8344         cancel_lru_locks mdc
8345         cancel_lru_locks osc
8346
8347 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8348         lctl set_param fail_loc=0x80000803
8349         ls -lR $DIR/$tdir > /dev/null
8350         log "ls done"
8351         lctl set_param fail_loc=0x0
8352         lctl get_param -n llite.*.statahead_stats
8353         rm -r $DIR/$tdir
8354         sync
8355
8356 }
8357 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8358
8359 test_124a() {
8360         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8361         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8362                 skip "no lru resize on server" && return 0
8363         local NR=2000
8364         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8365
8366         log "create $NR files at $DIR/$tdir"
8367         createmany -o $DIR/$tdir/f $NR ||
8368                 error "failed to create $NR files in $DIR/$tdir"
8369
8370         cancel_lru_locks mdc
8371         ls -l $DIR/$tdir > /dev/null
8372
8373         local NSDIR=""
8374         local LRU_SIZE=0
8375         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8376                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8377                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8378                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8379                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8380                         log "NSDIR=$NSDIR"
8381                         log "NS=$(basename $NSDIR)"
8382                         break
8383                 fi
8384         done
8385
8386         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8387                 skip "Not enough cached locks created!"
8388                 return 0
8389         fi
8390         log "LRU=$LRU_SIZE"
8391
8392         local SLEEP=30
8393
8394         # We know that lru resize allows one client to hold $LIMIT locks
8395         # for 10h. After that locks begin to be killed by client.
8396         local MAX_HRS=10
8397         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8398         log "LIMIT=$LIMIT"
8399         if [ $LIMIT -lt $LRU_SIZE ]; then
8400             skip "Limit is too small $LIMIT"
8401             return 0
8402         fi
8403
8404         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8405         # killing locks. Some time was spent for creating locks. This means
8406         # that up to the moment of sleep finish we must have killed some of
8407         # them (10-100 locks). This depends on how fast ther were created.
8408         # Many of them were touched in almost the same moment and thus will
8409         # be killed in groups.
8410         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8411
8412         # Use $LRU_SIZE_B here to take into account real number of locks
8413         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8414         local LRU_SIZE_B=$LRU_SIZE
8415         log "LVF=$LVF"
8416         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8417         log "OLD_LVF=$OLD_LVF"
8418         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8419
8420         # Let's make sure that we really have some margin. Client checks
8421         # cached locks every 10 sec.
8422         SLEEP=$((SLEEP+20))
8423         log "Sleep ${SLEEP} sec"
8424         local SEC=0
8425         while ((SEC<$SLEEP)); do
8426                 echo -n "..."
8427                 sleep 5
8428                 SEC=$((SEC+5))
8429                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8430                 echo -n "$LRU_SIZE"
8431         done
8432         echo ""
8433         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8434         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8435
8436         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8437                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8438                 unlinkmany $DIR/$tdir/f $NR
8439                 return
8440         }
8441
8442         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8443         log "unlink $NR files at $DIR/$tdir"
8444         unlinkmany $DIR/$tdir/f $NR
8445 }
8446 run_test 124a "lru resize ======================================="
8447
8448 get_max_pool_limit()
8449 {
8450         local limit=$($LCTL get_param \
8451                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8452         local max=0
8453         for l in $limit; do
8454                 if [[ $l -gt $max ]]; then
8455                         max=$l
8456                 fi
8457         done
8458         echo $max
8459 }
8460
8461 test_124b() {
8462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8463         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8464                 skip "no lru resize on server" && return 0
8465
8466         LIMIT=$(get_max_pool_limit)
8467
8468         NR=$(($(default_lru_size)*20))
8469         if [[ $NR -gt $LIMIT ]]; then
8470                 log "Limit lock number by $LIMIT locks"
8471                 NR=$LIMIT
8472         fi
8473         lru_resize_disable mdc
8474         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8475                 error "failed to create $DIR/$tdir/disable_lru_resize"
8476
8477         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8478         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8479         cancel_lru_locks mdc
8480         stime=`date +%s`
8481         PID=""
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         sleep 2
8488         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8489         PID="$PID $!"
8490         wait $PID
8491         etime=`date +%s`
8492         nolruresize_delta=$((etime-stime))
8493         log "ls -la time: $nolruresize_delta seconds"
8494         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8495         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8496
8497         lru_resize_enable mdc
8498         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8499                 error "failed to create $DIR/$tdir/enable_lru_resize"
8500
8501         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8502         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8503         cancel_lru_locks mdc
8504         stime=`date +%s`
8505         PID=""
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         sleep 2
8512         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8513         PID="$PID $!"
8514         wait $PID
8515         etime=`date +%s`
8516         lruresize_delta=$((etime-stime))
8517         log "ls -la time: $lruresize_delta seconds"
8518         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8519
8520         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8521                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8522         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8523                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8524         else
8525                 log "lru resize performs the same with no lru resize"
8526         fi
8527         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8528 }
8529 run_test 124b "lru resize (performance test) ======================="
8530
8531 test_124c() {
8532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8533         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8534                 skip "no lru resize on server" && return 0
8535
8536         # cache ununsed locks on client
8537         local nr=100
8538         cancel_lru_locks mdc
8539         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8540         createmany -o $DIR/$tdir/f $nr ||
8541                 error "failed to create $nr files in $DIR/$tdir"
8542         ls -l $DIR/$tdir > /dev/null
8543
8544         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
8545         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
8546         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
8547         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
8548         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
8549
8550         # set lru_max_age to 1 sec
8551         $LCTL set_param $nsdir.lru_max_age=1000 # jiffies
8552         echo "sleep $((recalc_p * 2)) seconds..."
8553         sleep $((recalc_p * 2))
8554
8555         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
8556         # restore lru_max_age
8557         $LCTL set_param -n $nsdir.lru_max_age $max_age
8558         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
8559         unlinkmany $DIR/$tdir/f $nr
8560 }
8561 run_test 124c "LRUR cancel very aged locks"
8562
8563 test_125() { # 13358
8564         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8565         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8566         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8567         test_mkdir -p $DIR/d125 || error "mkdir failed"
8568         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8569         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8570         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8571 }
8572 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8573
8574 test_126() { # bug 12829/13455
8575         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8576         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8577         $GSS && skip "must run as gss disabled" && return
8578
8579         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8580         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8581         rm -f $DIR/$tfile
8582         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8583 }
8584 run_test 126 "check that the fsgid provided by the client is taken into account"
8585
8586 test_127a() { # bug 15521
8587         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8588         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8589         $LCTL set_param osc.*.stats=0
8590         FSIZE=$((2048 * 1024))
8591         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8592         cancel_lru_locks osc
8593         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8594
8595         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8596         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8597                 echo "got $COUNT $NAME"
8598                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8599                 eval $NAME=$COUNT || error "Wrong proc format"
8600
8601                 case $NAME in
8602                         read_bytes|write_bytes)
8603                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8604                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8605                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8606                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8607                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8608                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8609                                 error "sumsquare is too small: $SUMSQ"
8610                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8611                                 error "sumsquare is too big: $SUMSQ"
8612                         ;;
8613                         *) ;;
8614                 esac
8615         done < $DIR/${tfile}.tmp
8616
8617         #check that we actually got some stats
8618         [ "$read_bytes" ] || error "Missing read_bytes stats"
8619         [ "$write_bytes" ] || error "Missing write_bytes stats"
8620         [ "$read_bytes" != 0 ] || error "no read done"
8621         [ "$write_bytes" != 0 ] || error "no write done"
8622 }
8623 run_test 127a "verify the client stats are sane"
8624
8625 test_127b() { # bug LU-333
8626         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8627         $LCTL set_param llite.*.stats=0
8628         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8629         # perform 2 reads and writes so MAX is different from SUM.
8630         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8631         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8632         cancel_lru_locks osc
8633         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8634         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8635
8636         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8637         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8638                 echo "got $COUNT $NAME"
8639                 eval $NAME=$COUNT || error "Wrong proc format"
8640
8641         case $NAME in
8642                 read_bytes)
8643                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8644                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8645                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8646                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8647                         ;;
8648                 write_bytes)
8649                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8650                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8651                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8652                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8653                         ;;
8654                         *) ;;
8655                 esac
8656         done < $TMP/${tfile}.tmp
8657
8658         #check that we actually got some stats
8659         [ "$read_bytes" ] || error "Missing read_bytes stats"
8660         [ "$write_bytes" ] || error "Missing write_bytes stats"
8661         [ "$read_bytes" != 0 ] || error "no read done"
8662         [ "$write_bytes" != 0 ] || error "no write done"
8663 }
8664 run_test 127b "verify the llite client stats are sane"
8665
8666 test_128() { # bug 15212
8667         touch $DIR/$tfile
8668         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8669                 find $DIR/$tfile
8670                 find $DIR/$tfile
8671         EOF
8672
8673         result=$(grep error $TMP/$tfile.log)
8674         rm -f $DIR/$tfile
8675         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8676 }
8677 run_test 128 "interactive lfs for 2 consecutive find's"
8678
8679 set_dir_limits () {
8680         local mntdev
8681         local canondev
8682         local node
8683
8684         local LDPROC=/proc/fs/ldiskfs
8685         local facets=$(get_facets MDS)
8686
8687         for facet in ${facets//,/ }; do
8688                 canondev=$(ldiskfs_canon \
8689                            *.$(convert_facet2label $facet).mntdev $facet)
8690                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8691                                                 LDPROC=/sys/fs/ldiskfs
8692                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8693         done
8694 }
8695
8696 test_129() {
8697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8698         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8699                 skip "Only applicable to ldiskfs-based MDTs"
8700                 return
8701         fi
8702         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8703         ENOSPC=28
8704         EFBIG=27
8705
8706         rm -rf $DIR/$tdir
8707         test_mkdir -p $DIR/$tdir
8708
8709         # block size of mds1
8710         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8711         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8712         local MAX=$((MDSBLOCKSIZE * 3))
8713         set_dir_limits $MAX
8714         local I=$(stat -c%s "$DIR/$tdir")
8715         local J=0
8716         local STRIPE_COUNT=1
8717         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8718         MAX=$((MAX*STRIPE_COUNT))
8719         while [[ $I -le $MAX ]]; do
8720                 $MULTIOP $DIR/$tdir/$J Oc
8721                 rc=$?
8722                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8723                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8724                 #and EFBIG for previous versions
8725                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8726                         set_dir_limits 0
8727                         echo "return code $rc received as expected"
8728                         multiop $DIR/$tdir/$J Oc ||
8729                                 error_exit "multiop failed w/o dir size limit"
8730
8731                         I=$(stat -c%s "$DIR/$tdir")
8732
8733                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8734                                         $(version_code 2.4.51) ]
8735                         then
8736                                 [[ $I -eq $MAX ]] && return 0
8737                         else
8738                                 [[ $I -gt $MAX ]] && return 0
8739                         fi
8740                         error_exit "current dir size $I, previous limit $MAX"
8741                 elif [ $rc -ne 0 ]; then
8742                         set_dir_limits 0
8743                         error_exit "return code $rc received instead of expected " \
8744                                    "$EFBIG or $ENOSPC, files in dir $I"
8745                 fi
8746                 J=$((J+1))
8747                 I=$(stat -c%s "$DIR/$tdir")
8748         done
8749
8750         set_dir_limits 0
8751         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8752 }
8753 run_test 129 "test directory size limit ========================"
8754
8755 OLDIFS="$IFS"
8756 cleanup_130() {
8757         trap 0
8758         IFS="$OLDIFS"
8759 }
8760
8761 test_130a() {
8762         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8763         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8764                 return
8765
8766         trap cleanup_130 EXIT RETURN
8767
8768         local fm_file=$DIR/$tfile
8769         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8770         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8771                 error "dd failed for $fm_file"
8772
8773         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8774         filefrag -ves $fm_file
8775         RC=$?
8776         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8777                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8778         [ $RC != 0 ] && error "filefrag $fm_file failed"
8779
8780         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8781                       grep -v "ext:" | grep -v "found")
8782         lun=$($GETSTRIPE -i $fm_file)
8783
8784         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8785         IFS=$'\n'
8786         tot_len=0
8787         for line in $filefrag_op
8788         do
8789                 frag_lun=`echo $line | cut -d: -f5`
8790                 ext_len=`echo $line | cut -d: -f4`
8791                 if (( $frag_lun != $lun )); then
8792                         cleanup_130
8793                         error "FIEMAP on 1-stripe file($fm_file) failed"
8794                         return
8795                 fi
8796                 (( tot_len += ext_len ))
8797         done
8798
8799         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8800                 cleanup_130
8801                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8802                 return
8803         fi
8804
8805         cleanup_130
8806
8807         echo "FIEMAP on single striped file succeeded"
8808 }
8809 run_test 130a "FIEMAP (1-stripe file)"
8810
8811 test_130b() {
8812         [ "$OSTCOUNT" -lt "2" ] &&
8813                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8814
8815         [ "$OSTCOUNT" -ge "10" ] &&
8816                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8817
8818         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8819         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8820                 return
8821
8822         trap cleanup_130 EXIT RETURN
8823
8824         local fm_file=$DIR/$tfile
8825         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8826         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8827                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8828
8829         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8830                 error "dd failed on $fm_file"
8831
8832         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8833         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8834                       grep -v "ext:" | grep -v "found")
8835
8836         last_lun=$(echo $filefrag_op | cut -d: -f5)
8837
8838         IFS=$'\n'
8839         tot_len=0
8840         num_luns=1
8841         for line in $filefrag_op
8842         do
8843                 frag_lun=`echo $line | cut -d: -f5`
8844                 ext_len=`echo $line | cut -d: -f4`
8845                 if (( $frag_lun != $last_lun )); then
8846                         if (( tot_len != 1024 )); then
8847                                 cleanup_130
8848                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8849                                 return
8850                         else
8851                                 (( num_luns += 1 ))
8852                                 tot_len=0
8853                         fi
8854                 fi
8855                 (( tot_len += ext_len ))
8856                 last_lun=$frag_lun
8857         done
8858         if (( num_luns != 2 || tot_len != 1024 )); then
8859                 cleanup_130
8860                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8861                 return
8862         fi
8863
8864         cleanup_130
8865
8866         echo "FIEMAP on 2-stripe file succeeded"
8867 }
8868 run_test 130b "FIEMAP (2-stripe file)"
8869
8870 test_130c() {
8871         [ "$OSTCOUNT" -lt "2" ] &&
8872                 skip_env "skipping FIEMAP on 2-stripe file" && return
8873
8874         [ "$OSTCOUNT" -ge "10" ] &&
8875                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8876
8877         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8878         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8879                 return
8880
8881         trap cleanup_130 EXIT RETURN
8882
8883         local fm_file=$DIR/$tfile
8884         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8885         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8886                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8887
8888         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8889
8890         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8891         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8892
8893         last_lun=`echo $filefrag_op | cut -d: -f5`
8894
8895         IFS=$'\n'
8896         tot_len=0
8897         num_luns=1
8898         for line in $filefrag_op
8899         do
8900                 frag_lun=`echo $line | cut -d: -f5`
8901                 ext_len=`echo $line | cut -d: -f4`
8902                 if (( $frag_lun != $last_lun )); then
8903                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8904                         if (( logical != 512 )); then
8905                                 cleanup_130
8906                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8907                                 return
8908                         fi
8909                         if (( tot_len != 512 )); then
8910                                 cleanup_130
8911                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8912                                 return
8913                         else
8914                                 (( num_luns += 1 ))
8915                                 tot_len=0
8916                         fi
8917                 fi
8918                 (( tot_len += ext_len ))
8919                 last_lun=$frag_lun
8920         done
8921         if (( num_luns != 2 || tot_len != 512 )); then
8922                 cleanup_130
8923                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8924                 return
8925         fi
8926
8927         cleanup_130
8928
8929         echo "FIEMAP on 2-stripe file with hole succeeded"
8930 }
8931 run_test 130c "FIEMAP (2-stripe file with hole)"
8932
8933 test_130d() {
8934         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8935
8936         [ "$OSTCOUNT" -ge "10" ] &&
8937                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8938
8939         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8940         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8941
8942         trap cleanup_130 EXIT RETURN
8943
8944         local fm_file=$DIR/$tfile
8945         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8946         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8947                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8948
8949         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8950         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8951                 error "dd failed on $fm_file"
8952
8953         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8954         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8955                 grep -v "ext:" | grep -v "found"`
8956
8957         last_lun=`echo $filefrag_op | cut -d: -f5`
8958
8959         IFS=$'\n'
8960         tot_len=0
8961         num_luns=1
8962         for line in $filefrag_op
8963         do
8964                 frag_lun=`echo $line | cut -d: -f5`
8965                 ext_len=`echo $line | cut -d: -f4`
8966                 if (( $frag_lun != $last_lun )); then
8967                         if (( tot_len != 1024 )); then
8968                                 cleanup_130
8969                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8970                                 return
8971                         else
8972                                 (( num_luns += 1 ))
8973                                 tot_len=0
8974                         fi
8975                 fi
8976                 (( tot_len += ext_len ))
8977                 last_lun=$frag_lun
8978         done
8979         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8980                 cleanup_130
8981                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8982                 return
8983         fi
8984
8985         cleanup_130
8986
8987         echo "FIEMAP on N-stripe file succeeded"
8988 }
8989 run_test 130d "FIEMAP (N-stripe file)"
8990
8991 test_130e() {
8992         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8993
8994         [ "$OSTCOUNT" -ge "10" ] &&
8995                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8996
8997         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8998         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8999
9000         trap cleanup_130 EXIT RETURN
9001
9002         local fm_file=$DIR/$tfile
9003         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9004         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9005                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9006
9007         NUM_BLKS=512
9008         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9009         for ((i = 0; i < $NUM_BLKS; i++))
9010         do
9011                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9012         done
9013
9014         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9015         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
9016
9017         last_lun=`echo $filefrag_op | cut -d: -f5`
9018
9019         IFS=$'\n'
9020         tot_len=0
9021         num_luns=1
9022         for line in $filefrag_op
9023         do
9024                 frag_lun=`echo $line | cut -d: -f5`
9025                 ext_len=`echo $line | cut -d: -f4`
9026                 if (( $frag_lun != $last_lun )); then
9027                         if (( tot_len != $EXPECTED_LEN )); then
9028                                 cleanup_130
9029                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
9030                                 return
9031                         else
9032                                 (( num_luns += 1 ))
9033                                 tot_len=0
9034                         fi
9035                 fi
9036                 (( tot_len += ext_len ))
9037                 last_lun=$frag_lun
9038         done
9039         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9040                 cleanup_130
9041                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
9042                 return
9043         fi
9044
9045         cleanup_130
9046
9047         echo "FIEMAP with continuation calls succeeded"
9048 }
9049 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9050
9051 # Test for writev/readv
9052 test_131a() {
9053         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
9054         error "writev test failed"
9055         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
9056         error "readv failed"
9057         rm -f $DIR/$tfile
9058 }
9059 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9060
9061 test_131b() {
9062         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
9063         error "append writev test failed"
9064         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
9065         error "append writev test failed"
9066         rm -f $DIR/$tfile
9067 }
9068 run_test 131b "test append writev"
9069
9070 test_131c() {
9071         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9072         error "NOT PASS"
9073 }
9074 run_test 131c "test read/write on file w/o objects"
9075
9076 test_131d() {
9077         rwv -f $DIR/$tfile -w -n 1 1572864
9078         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9079         if [ "$NOB" != 1572864 ]; then
9080                 error "Short read filed: read $NOB bytes instead of 1572864"
9081         fi
9082         rm -f $DIR/$tfile
9083 }
9084 run_test 131d "test short read"
9085
9086 test_131e() {
9087         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9088         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9089         error "read hitting hole failed"
9090         rm -f $DIR/$tfile
9091 }
9092 run_test 131e "test read hitting hole"
9093
9094 check_stats() {
9095         local res
9096         local count
9097         case $1 in
9098         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9099                  ;;
9100         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9101                  ;;
9102         *) error "Wrong argument $1" ;;
9103         esac
9104         echo $res
9105         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9106         # if the argument $3 is zero, it means any stat increment is ok.
9107         if [[ $3 -gt 0 ]]; then
9108                 count=$(echo $res | awk '{ print $2 }')
9109                 [[ $count -ne $3 ]] &&
9110                         error "The $2 counter on $1 is wrong - expected $3"
9111         fi
9112 }
9113
9114 test_133a() {
9115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9116         remote_ost_nodsh && skip "remote OST with nodsh" && return
9117         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9118
9119         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9120                 { skip "MDS doesn't support rename stats"; return; }
9121         local testdir=$DIR/${tdir}/stats_testdir
9122         mkdir -p $DIR/${tdir}
9123
9124         # clear stats.
9125         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9126         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9127
9128         # verify mdt stats first.
9129         mkdir ${testdir} || error "mkdir failed"
9130         check_stats $SINGLEMDS "mkdir" 1
9131         touch ${testdir}/${tfile} || "touch failed"
9132         check_stats $SINGLEMDS "open" 1
9133         check_stats $SINGLEMDS "close" 1
9134         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9135         check_stats $SINGLEMDS "mknod" 1
9136         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9137         check_stats $SINGLEMDS "unlink" 1
9138         rm -f ${testdir}/${tfile} || error "file remove failed"
9139         check_stats $SINGLEMDS "unlink" 2
9140
9141         # remove working dir and check mdt stats again.
9142         rmdir ${testdir} || error "rmdir failed"
9143         check_stats $SINGLEMDS "rmdir" 1
9144
9145         local testdir1=$DIR/${tdir}/stats_testdir1
9146         mkdir -p ${testdir}
9147         mkdir -p ${testdir1}
9148         touch ${testdir1}/test1
9149         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9150         check_stats $SINGLEMDS "crossdir_rename" 1
9151
9152         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9153         check_stats $SINGLEMDS "samedir_rename" 1
9154
9155         rm -rf $DIR/${tdir}
9156 }
9157 run_test 133a "Verifying MDT stats ========================================"
9158
9159 test_133b() {
9160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9161         remote_ost_nodsh && skip "remote OST with nodsh" && return
9162         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9163         local testdir=$DIR/${tdir}/stats_testdir
9164         mkdir -p ${testdir} || error "mkdir failed"
9165         touch ${testdir}/${tfile} || "touch failed"
9166         cancel_lru_locks mdc
9167
9168         # clear stats.
9169         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9170         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9171
9172         # extra mdt stats verification.
9173         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9174         check_stats $SINGLEMDS "setattr" 1
9175         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9176         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9177         then            # LU-1740
9178                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9179                 check_stats $SINGLEMDS "getattr" 1
9180         fi
9181         $LFS df || error "lfs failed"
9182         check_stats $SINGLEMDS "statfs" 1
9183
9184         rm -rf $DIR/${tdir}
9185 }
9186 run_test 133b "Verifying extra MDT stats =================================="
9187
9188 test_133c() {
9189         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9190         remote_ost_nodsh && skip "remote OST with nodsh" && return
9191         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9192         local testdir=$DIR/${tdir}/stats_testdir
9193         test_mkdir -p ${testdir} || error "mkdir failed"
9194
9195         # verify obdfilter stats.
9196         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9197         sync
9198         cancel_lru_locks osc
9199         wait_delete_completed
9200
9201         # clear stats.
9202         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9203         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9204
9205         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9206         sync
9207         cancel_lru_locks osc
9208         check_stats ost "write" 1
9209
9210         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9211         check_stats ost "read" 1
9212
9213         > ${testdir}/${tfile} || error "truncate failed"
9214         check_stats ost "punch" 1
9215
9216         rm -f ${testdir}/${tfile} || error "file remove failed"
9217         wait_delete_completed
9218         check_stats ost "destroy" 1
9219
9220         rm -rf $DIR/${tdir}
9221 }
9222 run_test 133c "Verifying OST stats ========================================"
9223
9224 order_2() {
9225         local value=$1
9226         local orig=$value
9227         local order=1
9228
9229         while [ $value -ge 2 ]; do
9230                 order=$((order*2))
9231                 value=$((value/2))
9232         done
9233
9234         if [ $orig -gt $order ]; then
9235                 order=$((order*2))
9236         fi
9237         echo $order
9238 }
9239
9240 size_in_KMGT() {
9241     local value=$1
9242     local size=('K' 'M' 'G' 'T');
9243     local i=0
9244     local size_string=$value
9245
9246     while [ $value -ge 1024 ]; do
9247         if [ $i -gt 3 ]; then
9248             #T is the biggest unit we get here, if that is bigger,
9249             #just return XXXT
9250             size_string=${value}T
9251             break
9252         fi
9253         value=$((value >> 10))
9254         if [ $value -lt 1024 ]; then
9255             size_string=${value}${size[$i]}
9256             break
9257         fi
9258         i=$((i + 1))
9259     done
9260
9261     echo $size_string
9262 }
9263
9264 get_rename_size() {
9265     local size=$1
9266     local context=${2:-.}
9267     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9268                 grep -A1 $context |
9269                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9270     echo $sample
9271 }
9272
9273 test_133d() {
9274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9275         remote_ost_nodsh && skip "remote OST with nodsh" && return
9276         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9277         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9278         { skip "MDS doesn't support rename stats"; return; }
9279
9280         local testdir1=$DIR/${tdir}/stats_testdir1
9281         local testdir2=$DIR/${tdir}/stats_testdir2
9282
9283         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9284
9285         mkdir -p ${testdir1} || error "mkdir failed"
9286         mkdir -p ${testdir2} || error "mkdir failed"
9287
9288         createmany -o $testdir1/test 512 || error "createmany failed"
9289
9290         # check samedir rename size
9291         mv ${testdir1}/test0 ${testdir1}/test_0
9292
9293         local testdir1_size=$(ls -l $DIR/${tdir} |
9294                 awk '/stats_testdir1/ {print $5}')
9295         local testdir2_size=$(ls -l $DIR/${tdir} |
9296                 awk '/stats_testdir2/ {print $5}')
9297
9298         testdir1_size=$(order_2 $testdir1_size)
9299         testdir2_size=$(order_2 $testdir2_size)
9300
9301         testdir1_size=$(size_in_KMGT $testdir1_size)
9302         testdir2_size=$(size_in_KMGT $testdir2_size)
9303
9304         echo "source rename dir size: ${testdir1_size}"
9305         echo "target rename dir size: ${testdir2_size}"
9306
9307         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9308         eval $cmd || error "$cmd failed"
9309         local samedir=$($cmd | grep 'same_dir')
9310         local same_sample=$(get_rename_size $testdir1_size)
9311         [ -z "$samedir" ] && error "samedir_rename_size count error"
9312         [[ $same_sample -eq 1 ]] ||
9313                 error "samedir_rename_size error $same_sample"
9314         echo "Check same dir rename stats success"
9315
9316         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9317
9318         # check crossdir rename size
9319         mv ${testdir1}/test_0 ${testdir2}/test_0
9320
9321         testdir1_size=$(ls -l $DIR/${tdir} |
9322                 awk '/stats_testdir1/ {print $5}')
9323         testdir2_size=$(ls -l $DIR/${tdir} |
9324                 awk '/stats_testdir2/ {print $5}')
9325
9326         testdir1_size=$(order_2 $testdir1_size)
9327         testdir2_size=$(order_2 $testdir2_size)
9328
9329         testdir1_size=$(size_in_KMGT $testdir1_size)
9330         testdir2_size=$(size_in_KMGT $testdir2_size)
9331
9332         echo "source rename dir size: ${testdir1_size}"
9333         echo "target rename dir size: ${testdir2_size}"
9334
9335         eval $cmd || error "$cmd failed"
9336         local crossdir=$($cmd | grep 'crossdir')
9337         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9338         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9339         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9340         [[ $src_sample -eq 1 ]] ||
9341                 error "crossdir_rename_size error $src_sample"
9342         [[ $tgt_sample -eq 1 ]] ||
9343                 error "crossdir_rename_size error $tgt_sample"
9344         echo "Check cross dir rename stats success"
9345         rm -rf $DIR/${tdir}
9346 }
9347 run_test 133d "Verifying rename_stats ========================================"
9348
9349 test_133e() {
9350         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9351         remote_ost_nodsh && skip "remote OST with nodsh" && return
9352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9353         local testdir=$DIR/${tdir}/stats_testdir
9354         local ctr f0 f1 bs=32768 count=42 sum
9355
9356         mkdir -p ${testdir} || error "mkdir failed"
9357
9358         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9359
9360         for ctr in {write,read}_bytes; do
9361                 sync
9362                 cancel_lru_locks osc
9363
9364                 do_facet ost1 $LCTL set_param -n \
9365                         "obdfilter.*.exports.clear=clear"
9366
9367                 if [ $ctr = write_bytes ]; then
9368                         f0=/dev/zero
9369                         f1=${testdir}/${tfile}
9370                 else
9371                         f0=${testdir}/${tfile}
9372                         f1=/dev/null
9373                 fi
9374
9375                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9376                         error "dd failed"
9377                 sync
9378                 cancel_lru_locks osc
9379
9380                 sum=$(do_facet ost1 $LCTL get_param \
9381                         "obdfilter.*.exports.*.stats" |
9382                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9383                                 $1 == ctr { sum += $7 }
9384                                 END { printf("%0.0f", sum) }')
9385
9386                 if ((sum != bs * count)); then
9387                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9388                 fi
9389         done
9390
9391         rm -rf $DIR/${tdir}
9392 }
9393 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9394
9395 test_133f() {
9396         local proc_dirs
9397
9398         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9399 /sys/fs/lustre/ /sys/fs/lnet/"
9400         local dir
9401         for dir in $dirs; do
9402                 if [ -d $dir ]; then
9403                         proc_dirs="$proc_dirs $dir"
9404                 fi
9405         done
9406
9407         local facet
9408
9409         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9410         remote_ost_nodsh && skip "remote OST with nodsh" && return
9411         # First without trusting modes.
9412         find $proc_dirs -exec cat '{}' \; &> /dev/null
9413
9414         # Second verifying readability.
9415         find $proc_dirs \
9416                 -type f \
9417                 -exec cat '{}' \; &> /dev/null ||
9418                         error "proc file read failed"
9419
9420         for facet in $SINGLEMDS ost1; do
9421                 do_facet $facet find $proc_dirs \
9422                         ! -name req_history \
9423                         -exec cat '{}' \\\; &> /dev/null
9424
9425                 do_facet $facet find $proc_dirs \
9426                         ! -name req_history \
9427                         -type f \
9428                         -exec cat '{}' \\\; &> /dev/null ||
9429                                 error "proc file read failed"
9430         done
9431 }
9432 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9433
9434 test_133g() {
9435         local proc_dirs
9436
9437         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9438 /sys/fs/lustre/ /sys/fs/lnet/"
9439         local dir
9440         for dir in $dirs; do
9441                 if [ -d $dir ]; then
9442                         proc_dirs="$proc_dirs $dir"
9443                 fi
9444         done
9445
9446         local facet
9447
9448         # Second verifying readability.
9449         find $proc_dirs \
9450                 -type f \
9451                 -not -name force_lbug \
9452                 -not -name changelog_mask \
9453                 -exec badarea_io '{}' \; &> /dev/null ||
9454                 error "find $proc_dirs failed"
9455
9456         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9457                 skip "Too old lustre on MDS" && return
9458
9459         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9460                 skip "Too old lustre on ost1" && return
9461
9462         for facet in $SINGLEMDS ost1; do
9463                 do_facet $facet find $proc_dirs \
9464                         -type f \
9465                         -not -name force_lbug \
9466                         -not -name changelog_mask \
9467                         -exec badarea_io '{}' \\\; &> /dev/null ||
9468                 error "$facet find $proc_dirs failed"
9469
9470         done
9471
9472         # remount the FS in case writes/reads /proc break the FS
9473         cleanup || error "failed to unmount"
9474         setup || error "failed to setup"
9475         true
9476 }
9477 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9478
9479 test_134a() {
9480         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9481                 skip "Need MDS version at least 2.7.54" && return
9482
9483         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9484         cancel_lru_locks mdc
9485
9486         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9487         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9488         [ $unused -eq 0 ] || "$unused locks are not cleared"
9489
9490         local nr=1000
9491         createmany -o $DIR/$tdir/f $nr ||
9492                 error "failed to create $nr files in $DIR/$tdir"
9493         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9494
9495         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
9496         do_facet mds1 $LCTL set_param fail_loc=0x327
9497         do_facet mds1 $LCTL set_param fail_val=500
9498         touch $DIR/$tdir/m
9499
9500         echo "sleep 10 seconds ..."
9501         sleep 10
9502         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
9503
9504         do_facet mds1 $LCTL set_param fail_loc=0
9505         do_facet mds1 $LCTL set_param fail_val=0
9506         [ $lck_cnt -lt $unused ] ||
9507                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
9508
9509         rm $DIR/$tdir/m
9510         unlinkmany $DIR/$tdir/f $nr
9511 }
9512 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
9513
9514 test_134b() {
9515         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9516                 skip "Need MDS version at least 2.7.54" && return
9517
9518         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9519         cancel_lru_locks mdc
9520
9521         local low_wm=$(do_facet mds1 $LCTL get_param -n \
9522                         ldlm.lock_reclaim_threshold_mb)
9523         # disable reclaim temporarily
9524         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
9525
9526         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
9527         do_facet mds1 $LCTL set_param fail_loc=0x328
9528         do_facet mds1 $LCTL set_param fail_val=500
9529
9530         $LCTL set_param debug=+trace
9531
9532         local nr=600
9533         createmany -o $DIR/$tdir/f $nr &
9534         local create_pid=$!
9535
9536         echo "Sleep $TIMEOUT seconds ..."
9537         sleep $TIMEOUT
9538         if ! ps -p $create_pid  > /dev/null 2>&1; then
9539                 do_facet mds1 $LCTL set_param fail_loc=0
9540                 do_facet mds1 $LCTL set_param fail_val=0
9541                 do_facet mds1 $LCTL set_param \
9542                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
9543                 error "createmany finished incorrectly!"
9544         fi
9545         do_facet mds1 $LCTL set_param fail_loc=0
9546         do_facet mds1 $LCTL set_param fail_val=0
9547         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
9548         wait $create_pid || return 1
9549
9550         unlinkmany $DIR/$tdir/f $nr
9551 }
9552 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
9553
9554 test_140() { #bug-17379
9555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9556         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9557         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9558         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9559
9560         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9561         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9562         local i=0
9563         while i=`expr $i + 1`; do
9564                 test_mkdir -p $i || error "Creating dir $i"
9565                 cd $i || error "Changing to $i"
9566                 ln -s ../stat stat || error "Creating stat symlink"
9567                 # Read the symlink until ELOOP present,
9568                 # not LBUGing the system is considered success,
9569                 # we didn't overrun the stack.
9570                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9571                 [ $ret -ne 0 ] && {
9572                         if [ $ret -eq 40 ]; then
9573                                 break  # -ELOOP
9574                         else
9575                                 error "Open stat symlink"
9576                                 return
9577                         fi
9578                 }
9579         done
9580         i=`expr $i - 1`
9581         echo "The symlink depth = $i"
9582         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9583                                         error "Invalid symlink depth"
9584
9585         # Test recursive symlink
9586         ln -s symlink_self symlink_self
9587         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9588         echo "open symlink_self returns $ret"
9589         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9590 }
9591 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9592
9593 test_150() {
9594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9595         local TF="$TMP/$tfile"
9596
9597         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9598         cp $TF $DIR/$tfile
9599         cancel_lru_locks osc
9600         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9601         remount_client $MOUNT
9602         df -P $MOUNT
9603         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9604
9605         $TRUNCATE $TF 6000
9606         $TRUNCATE $DIR/$tfile 6000
9607         cancel_lru_locks osc
9608         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9609
9610         echo "12345" >>$TF
9611         echo "12345" >>$DIR/$tfile
9612         cancel_lru_locks osc
9613         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9614
9615         echo "12345" >>$TF
9616         echo "12345" >>$DIR/$tfile
9617         cancel_lru_locks osc
9618         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9619
9620         rm -f $TF
9621         true
9622 }
9623 run_test 150 "truncate/append tests"
9624
9625 #LU-2902 roc_hit was not able to read all values from lproc
9626 function roc_hit_init() {
9627         local list=$(comma_list $(osts_nodes))
9628         local dir=$DIR/$tdir-check
9629         local file=$dir/file
9630         local BEFORE
9631         local AFTER
9632         local idx
9633
9634         test_mkdir -p $dir
9635         #use setstripe to do a write to every ost
9636         for i in $(seq 0 $((OSTCOUNT-1))); do
9637                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9638                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9639                 idx=$(printf %04x $i)
9640                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9641                         awk '$1 == "cache_access" {sum += $7}
9642                                 END { printf("%0.0f", sum) }')
9643
9644                 cancel_lru_locks osc
9645                 cat $file >/dev/null
9646
9647                 AFTER=$(get_osd_param $list *OST*$idx stats |
9648                         awk '$1 == "cache_access" {sum += $7}
9649                                 END { printf("%0.0f", sum) }')
9650
9651                 echo BEFORE:$BEFORE AFTER:$AFTER
9652                 if ! let "AFTER - BEFORE == 4"; then
9653                         rm -rf $dir
9654                         error "roc_hit is not safe to use"
9655                 fi
9656                 rm $file
9657         done
9658
9659         rm -rf $dir
9660 }
9661
9662 function roc_hit() {
9663         local list=$(comma_list $(osts_nodes))
9664         echo $(get_osd_param $list '' stats |
9665                 awk '$1 == "cache_hit" {sum += $7}
9666                         END { printf("%0.0f", sum) }')
9667 }
9668
9669 function set_cache() {
9670         local on=1
9671
9672         if [ "$2" == "off" ]; then
9673                 on=0;
9674         fi
9675         local list=$(comma_list $(osts_nodes))
9676         set_osd_param $list '' $1_cache_enable $on
9677
9678         cancel_lru_locks osc
9679 }
9680
9681 test_151() {
9682         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9683         remote_ost_nodsh && skip "remote OST with nodsh" && return
9684
9685         local CPAGES=3
9686         local list=$(comma_list $(osts_nodes))
9687
9688         # check whether obdfilter is cache capable at all
9689         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9690                 echo "not cache-capable obdfilter"
9691                 return 0
9692         fi
9693
9694         # check cache is enabled on all obdfilters
9695         if get_osd_param $list '' read_cache_enable | grep 0; then
9696                 echo "oss cache is disabled"
9697                 return 0
9698         fi
9699
9700         set_osd_param $list '' writethrough_cache_enable 1
9701
9702         # check write cache is enabled on all obdfilters
9703         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9704                 echo "oss write cache is NOT enabled"
9705                 return 0
9706         fi
9707
9708         roc_hit_init
9709
9710         #define OBD_FAIL_OBD_NO_LRU  0x609
9711         do_nodes $list $LCTL set_param fail_loc=0x609
9712
9713         # pages should be in the case right after write
9714         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9715                 error "dd failed"
9716
9717         local BEFORE=$(roc_hit)
9718         cancel_lru_locks osc
9719         cat $DIR/$tfile >/dev/null
9720         local AFTER=$(roc_hit)
9721
9722         do_nodes $list $LCTL set_param fail_loc=0
9723
9724         if ! let "AFTER - BEFORE == CPAGES"; then
9725                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9726         fi
9727
9728         # the following read invalidates the cache
9729         cancel_lru_locks osc
9730         set_osd_param $list '' read_cache_enable 0
9731         cat $DIR/$tfile >/dev/null
9732
9733         # now data shouldn't be found in the cache
9734         BEFORE=$(roc_hit)
9735         cancel_lru_locks osc
9736         cat $DIR/$tfile >/dev/null
9737         AFTER=$(roc_hit)
9738         if let "AFTER - BEFORE != 0"; then
9739                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9740         fi
9741
9742         set_osd_param $list '' read_cache_enable 1
9743         rm -f $DIR/$tfile
9744 }
9745 run_test 151 "test cache on oss and controls ==============================="
9746
9747 test_152() {
9748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9749         local TF="$TMP/$tfile"
9750
9751         # simulate ENOMEM during write
9752 #define OBD_FAIL_OST_NOMEM      0x226
9753         lctl set_param fail_loc=0x80000226
9754         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9755         cp $TF $DIR/$tfile
9756         sync || error "sync failed"
9757         lctl set_param fail_loc=0
9758
9759         # discard client's cache
9760         cancel_lru_locks osc
9761
9762         # simulate ENOMEM during read
9763         lctl set_param fail_loc=0x80000226
9764         cmp $TF $DIR/$tfile || error "cmp failed"
9765         lctl set_param fail_loc=0
9766
9767         rm -f $TF
9768 }
9769 run_test 152 "test read/write with enomem ============================"
9770
9771 test_153() {
9772         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9773 }
9774 run_test 153 "test if fdatasync does not crash ======================="
9775
9776 dot_lustre_fid_permission_check() {
9777         local fid=$1
9778         local ffid=$MOUNT/.lustre/fid/$fid
9779         local test_dir=$2
9780
9781         echo "stat fid $fid"
9782         stat $ffid > /dev/null || error "stat $ffid failed."
9783         echo "touch fid $fid"
9784         touch $ffid || error "touch $ffid failed."
9785         echo "write to fid $fid"
9786         cat /etc/hosts > $ffid || error "write $ffid failed."
9787         echo "read fid $fid"
9788         diff /etc/hosts $ffid || error "read $ffid failed."
9789         echo "append write to fid $fid"
9790         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9791         echo "rename fid $fid"
9792         mv $ffid $test_dir/$tfile.1 &&
9793                 error "rename $ffid to $tfile.1 should fail."
9794         touch $test_dir/$tfile.1
9795         mv $test_dir/$tfile.1 $ffid &&
9796                 error "rename $tfile.1 to $ffid should fail."
9797         rm -f $test_dir/$tfile.1
9798         echo "truncate fid $fid"
9799         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9800         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9801                 echo "link fid $fid"
9802                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9803         fi
9804         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9805                 echo "setfacl fid $fid"
9806                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9807                 echo "getfacl fid $fid"
9808                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9809         fi
9810         echo "unlink fid $fid"
9811         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9812         echo "mknod fid $fid"
9813         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9814
9815         fid=[0xf00000400:0x1:0x0]
9816         ffid=$MOUNT/.lustre/fid/$fid
9817
9818         echo "stat non-exist fid $fid"
9819         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9820         echo "write to non-exist fid $fid"
9821         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9822         echo "link new fid $fid"
9823         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9824
9825         mkdir -p $test_dir/$tdir
9826         touch $test_dir/$tdir/$tfile
9827         fid=$($LFS path2fid $test_dir/$tdir)
9828         rc=$?
9829         [ $rc -ne 0 ] &&
9830                 error "error: could not get fid for $test_dir/$dir/$tfile."
9831
9832         ffid=$MOUNT/.lustre/fid/$fid
9833
9834         echo "ls $fid"
9835         ls $ffid > /dev/null || error "ls $ffid failed."
9836         echo "touch $fid/$tfile.1"
9837         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9838
9839         echo "touch $MOUNT/.lustre/fid/$tfile"
9840         touch $MOUNT/.lustre/fid/$tfile && \
9841                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9842
9843         echo "setxattr to $MOUNT/.lustre/fid"
9844         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9845
9846         echo "listxattr for $MOUNT/.lustre/fid"
9847         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9848
9849         echo "delxattr from $MOUNT/.lustre/fid"
9850         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9851
9852         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9853         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9854                 error "touch invalid fid should fail."
9855
9856         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9857         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9858                 error "touch non-normal fid should fail."
9859
9860         echo "rename $tdir to $MOUNT/.lustre/fid"
9861         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9862                 error "rename to $MOUNT/.lustre/fid should fail."
9863
9864         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9865         then            # LU-3547
9866                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9867                 local new_obf_mode=777
9868
9869                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9870                 chmod $new_obf_mode $DIR/.lustre/fid ||
9871                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9872
9873                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9874                 [ $obf_mode -eq $new_obf_mode ] ||
9875                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9876
9877                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9878                 chmod $old_obf_mode $DIR/.lustre/fid ||
9879                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9880         fi
9881
9882         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9883         fid=$($LFS path2fid $test_dir/$tfile-2)
9884
9885         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9886         then # LU-5424
9887                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9888                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9889                         error "create lov data thru .lustre failed"
9890         fi
9891         echo "cp /etc/passwd $test_dir/$tfile-2"
9892         cp /etc/passwd $test_dir/$tfile-2 ||
9893                 error "copy to $test_dir/$tfile-2 failed."
9894         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9895         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9896                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9897
9898         rm -rf $test_dir/tfile.lnk
9899         rm -rf $test_dir/$tfile-2
9900 }
9901
9902 test_154A() {
9903         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9904                 skip "Need MDS version at least 2.4.1" && return
9905
9906         touch $DIR/$tfile
9907         local FID=$($LFS path2fid $DIR/$tfile)
9908         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9909
9910         # check that we get the same pathname back
9911         local FOUND=$($LFS fid2path $MOUNT "$FID")
9912         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9913         [ "$FOUND" != "$DIR/$tfile" ] &&
9914                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9915
9916         rm -rf $DIR/$tfile
9917 }
9918 run_test 154A "lfs path2fid and fid2path basic checks"
9919
9920 test_154a() {
9921         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9922         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9923                 { skip "Need MDS version at least 2.2.51"; return 0; }
9924         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9925
9926         cp /etc/hosts $DIR/$tfile
9927
9928         fid=$($LFS path2fid $DIR/$tfile)
9929         rc=$?
9930         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9931
9932         dot_lustre_fid_permission_check "$fid" $DIR ||
9933                 error "dot lustre permission check $fid failed"
9934
9935         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9936
9937         touch $MOUNT/.lustre/file &&
9938                 error "creation is not allowed under .lustre"
9939
9940         mkdir $MOUNT/.lustre/dir &&
9941                 error "mkdir is not allowed under .lustre"
9942
9943         rm -rf $DIR/$tfile
9944 }
9945 run_test 154a "Open-by-FID"
9946
9947 test_154b() {
9948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9949         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9950                 { skip "Need MDS version at least 2.2.51"; return 0; }
9951
9952         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9953
9954         local remote_dir=$DIR/$tdir/remote_dir
9955         local MDTIDX=1
9956         local rc=0
9957
9958         mkdir -p $DIR/$tdir
9959         $LFS mkdir -i $MDTIDX $remote_dir ||
9960                 error "create remote directory failed"
9961
9962         cp /etc/hosts $remote_dir/$tfile
9963
9964         fid=$($LFS path2fid $remote_dir/$tfile)
9965         rc=$?
9966         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9967
9968         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9969                 error "dot lustre permission check $fid failed"
9970         rm -rf $DIR/$tdir
9971 }
9972 run_test 154b "Open-by-FID for remote directory"
9973
9974 test_154c() {
9975         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9976                 skip "Need MDS version at least 2.4.1" && return
9977
9978         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9979         local FID1=$($LFS path2fid $DIR/$tfile.1)
9980         local FID2=$($LFS path2fid $DIR/$tfile.2)
9981         local FID3=$($LFS path2fid $DIR/$tfile.3)
9982
9983         local N=1
9984         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9985                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9986                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9987                 local want=FID$N
9988                 [ "$FID" = "${!want}" ] ||
9989                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9990                 N=$((N + 1))
9991         done
9992
9993         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
9994         do
9995                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9996                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9997                 N=$((N + 1))
9998         done
9999 }
10000 run_test 154c "lfs path2fid and fid2path multiple arguments"
10001
10002 test_154d() {
10003         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10004                 skip "Need MDS version at least 2.5.53" && return
10005
10006         if remote_mds; then
10007                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10008         else
10009                 nid="0@lo"
10010         fi
10011         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10012         local fd
10013         local cmd
10014
10015         rm -f $DIR/$tfile
10016         touch $DIR/$tfile
10017
10018         local fid=$($LFS path2fid $DIR/$tfile)
10019         # Open the file
10020         fd=$(free_fd)
10021         cmd="exec $fd<$DIR/$tfile"
10022         eval $cmd
10023         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10024         echo "$fid_list" | grep "$fid"
10025         rc=$?
10026
10027         cmd="exec $fd>/dev/null"
10028         eval $cmd
10029         if [ $rc -ne 0 ]; then
10030                 error "FID $fid not found in open files list $fid_list"
10031         fi
10032 }
10033 run_test 154d "Verify open file fid"
10034
10035 test_154e()
10036 {
10037         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10038                 skip "Need MDS version at least 2.6.50" && return
10039
10040         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10041                 error ".lustre returned by readdir"
10042         fi
10043 }
10044 run_test 154e ".lustre is not returned by readdir"
10045
10046 test_154f() {
10047         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10048         test_mkdir -p -c1 $DIR/$tdir/d
10049         # test dirs inherit from its stripe
10050         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10051         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10052         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10053         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10054         touch $DIR/f
10055
10056         # get fid of parents
10057         local FID0=$($LFS path2fid $DIR/$tdir/d)
10058         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10059         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10060         local FID3=$($LFS path2fid $DIR)
10061
10062         # check that path2fid --parents returns expected <parent_fid>/name
10063         # 1) test for a directory (single parent)
10064         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10065         [ "$parent" == "$FID0/foo1" ] ||
10066                 error "expected parent: $FID0/foo1, got: $parent"
10067
10068         # 2) test for a file with nlink > 1 (multiple parents)
10069         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10070         echo "$parent" | grep -F "$FID1/$tfile" ||
10071                 error "$FID1/$tfile not returned in parent list"
10072         echo "$parent" | grep -F "$FID2/link" ||
10073                 error "$FID2/link not returned in parent list"
10074
10075         # 3) get parent by fid
10076         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10077         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10078         echo "$parent" | grep -F "$FID1/$tfile" ||
10079                 error "$FID1/$tfile not returned in parent list (by fid)"
10080         echo "$parent" | grep -F "$FID2/link" ||
10081                 error "$FID2/link not returned in parent list (by fid)"
10082
10083         # 4) test for entry in root directory
10084         parent=$($LFS path2fid --parents $DIR/f)
10085         echo "$parent" | grep -F "$FID3/f" ||
10086                 error "$FID3/f not returned in parent list"
10087
10088         # 5) test it on root directory
10089         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10090                 error "$MOUNT should not have parents"
10091
10092         # enable xattr caching and check that linkea is correctly updated
10093         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10094         save_lustre_params client "llite.*.xattr_cache" > $save
10095         lctl set_param llite.*.xattr_cache 1
10096
10097         # 6.1) linkea update on rename
10098         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10099
10100         # get parents by fid
10101         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10102         # foo1 should no longer be returned in parent list
10103         echo "$parent" | grep -F "$FID1" &&
10104                 error "$FID1 should no longer be in parent list"
10105         # the new path should appear
10106         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10107                 error "$FID2/$tfile.moved is not in parent list"
10108
10109         # 6.2) linkea update on unlink
10110         rm -f $DIR/$tdir/d/foo2/link
10111         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10112         # foo2/link should no longer be returned in parent list
10113         echo "$parent" | grep -F "$FID2/link" &&
10114                 error "$FID2/link should no longer be in parent list"
10115         true
10116
10117         rm -f $DIR/f
10118         restore_lustre_params < $save
10119 }
10120 run_test 154f "get parent fids by reading link ea"
10121
10122 test_154g()
10123 {
10124         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) ]] ||
10125                 { skip "Need MDS version at least 2.6.92"; return 0; }
10126
10127         mkdir -p $DIR/$tdir
10128         llapi_fid_test -d $DIR/$tdir
10129 }
10130 run_test 154g "various llapi FID tests"
10131
10132 test_155_small_load() {
10133     local temp=$TMP/$tfile
10134     local file=$DIR/$tfile
10135
10136     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10137         error "dd of=$temp bs=6096 count=1 failed"
10138     cp $temp $file
10139     cancel_lru_locks osc
10140     cmp $temp $file || error "$temp $file differ"
10141
10142     $TRUNCATE $temp 6000
10143     $TRUNCATE $file 6000
10144     cmp $temp $file || error "$temp $file differ (truncate1)"
10145
10146     echo "12345" >>$temp
10147     echo "12345" >>$file
10148     cmp $temp $file || error "$temp $file differ (append1)"
10149
10150     echo "12345" >>$temp
10151     echo "12345" >>$file
10152     cmp $temp $file || error "$temp $file differ (append2)"
10153
10154     rm -f $temp $file
10155     true
10156 }
10157
10158 test_155_big_load() {
10159     remote_ost_nodsh && skip "remote OST with nodsh" && return
10160     local temp=$TMP/$tfile
10161     local file=$DIR/$tfile
10162
10163     free_min_max
10164     local cache_size=$(do_facet ost$((MAXI+1)) \
10165         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10166     local large_file_size=$((cache_size * 2))
10167
10168     echo "OSS cache size: $cache_size KB"
10169     echo "Large file size: $large_file_size KB"
10170
10171     [ $MAXV -le $large_file_size ] && \
10172         skip_env "max available OST size needs > $large_file_size KB" && \
10173         return 0
10174
10175     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10176
10177     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10178         error "dd of=$temp bs=$large_file_size count=1k failed"
10179     cp $temp $file
10180     ls -lh $temp $file
10181     cancel_lru_locks osc
10182     cmp $temp $file || error "$temp $file differ"
10183
10184     rm -f $temp $file
10185     true
10186 }
10187
10188 test_155a() {
10189         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10190         set_cache read on
10191         set_cache writethrough on
10192         test_155_small_load
10193 }
10194 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10195
10196 test_155b() {
10197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10198         set_cache read on
10199         set_cache writethrough off
10200         test_155_small_load
10201 }
10202 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10203
10204 test_155c() {
10205         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10206         set_cache read off
10207         set_cache writethrough on
10208         test_155_small_load
10209 }
10210 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10211
10212 test_155d() {
10213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10214         set_cache read off
10215         set_cache writethrough off
10216         test_155_small_load
10217 }
10218 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10219
10220 test_155e() {
10221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10222         set_cache read on
10223         set_cache writethrough on
10224         test_155_big_load
10225 }
10226 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10227
10228 test_155f() {
10229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10230         set_cache read on
10231         set_cache writethrough off
10232         test_155_big_load
10233 }
10234 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10235
10236 test_155g() {
10237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10238         set_cache read off
10239         set_cache writethrough on
10240         test_155_big_load
10241 }
10242 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10243
10244 test_155h() {
10245         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10246         set_cache read off
10247         set_cache writethrough off
10248         test_155_big_load
10249 }
10250 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10251
10252 test_156() {
10253         remote_ost_nodsh && skip "remote OST with nodsh" && return
10254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10255         local CPAGES=3
10256         local BEFORE
10257         local AFTER
10258         local file="$DIR/$tfile"
10259
10260         [ "$(facet_fstype ost1)" = "zfs" -a \
10261            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10262                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10263                 return
10264
10265         roc_hit_init
10266
10267         log "Turn on read and write cache"
10268         set_cache read on
10269         set_cache writethrough on
10270
10271         log "Write data and read it back."
10272         log "Read should be satisfied from the cache."
10273         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10274         BEFORE=$(roc_hit)
10275         cancel_lru_locks osc
10276         cat $file >/dev/null
10277         AFTER=$(roc_hit)
10278         if ! let "AFTER - BEFORE == CPAGES"; then
10279                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10280         else
10281                 log "cache hits:: before: $BEFORE, after: $AFTER"
10282         fi
10283
10284         log "Read again; it should be satisfied from the cache."
10285         BEFORE=$AFTER
10286         cancel_lru_locks osc
10287         cat $file >/dev/null
10288         AFTER=$(roc_hit)
10289         if ! let "AFTER - BEFORE == CPAGES"; then
10290                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10291         else
10292                 log "cache hits:: before: $BEFORE, after: $AFTER"
10293         fi
10294
10295         log "Turn off the read cache and turn on the write cache"
10296         set_cache read off
10297         set_cache writethrough on
10298
10299         log "Read again; it should be satisfied from the cache."
10300         BEFORE=$(roc_hit)
10301         cancel_lru_locks osc
10302         cat $file >/dev/null
10303         AFTER=$(roc_hit)
10304         if ! let "AFTER - BEFORE == CPAGES"; then
10305                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10306         else
10307                 log "cache hits:: before: $BEFORE, after: $AFTER"
10308         fi
10309
10310         log "Read again; it should not be satisfied from the cache."
10311         BEFORE=$AFTER
10312         cancel_lru_locks osc
10313         cat $file >/dev/null
10314         AFTER=$(roc_hit)
10315         if ! let "AFTER - BEFORE == 0"; then
10316                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10317         else
10318                 log "cache hits:: before: $BEFORE, after: $AFTER"
10319         fi
10320
10321         log "Write data and read it back."
10322         log "Read should be satisfied from the cache."
10323         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10324         BEFORE=$(roc_hit)
10325         cancel_lru_locks osc
10326         cat $file >/dev/null
10327         AFTER=$(roc_hit)
10328         if ! let "AFTER - BEFORE == CPAGES"; then
10329                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10330         else
10331                 log "cache hits:: before: $BEFORE, after: $AFTER"
10332         fi
10333
10334         log "Read again; it should not be satisfied from the cache."
10335         BEFORE=$AFTER
10336         cancel_lru_locks osc
10337         cat $file >/dev/null
10338         AFTER=$(roc_hit)
10339         if ! let "AFTER - BEFORE == 0"; then
10340                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10341         else
10342                 log "cache hits:: before: $BEFORE, after: $AFTER"
10343         fi
10344
10345         log "Turn off read and write cache"
10346         set_cache read off
10347         set_cache writethrough off
10348
10349         log "Write data and read it back"
10350         log "It should not be satisfied from the cache."
10351         rm -f $file
10352         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10353         cancel_lru_locks osc
10354         BEFORE=$(roc_hit)
10355         cat $file >/dev/null
10356         AFTER=$(roc_hit)
10357         if ! let "AFTER - BEFORE == 0"; then
10358                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10359         else
10360                 log "cache hits:: before: $BEFORE, after: $AFTER"
10361         fi
10362
10363         log "Turn on the read cache and turn off the write cache"
10364         set_cache read on
10365         set_cache writethrough off
10366
10367         log "Write data and read it back"
10368         log "It should not be satisfied from the cache."
10369         rm -f $file
10370         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10371         BEFORE=$(roc_hit)
10372         cancel_lru_locks osc
10373         cat $file >/dev/null
10374         AFTER=$(roc_hit)
10375         if ! let "AFTER - BEFORE == 0"; then
10376                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10377         else
10378                 log "cache hits:: before: $BEFORE, after: $AFTER"
10379         fi
10380
10381         log "Read again; it should be satisfied from the cache."
10382         BEFORE=$(roc_hit)
10383         cancel_lru_locks osc
10384         cat $file >/dev/null
10385         AFTER=$(roc_hit)
10386         if ! let "AFTER - BEFORE == CPAGES"; then
10387                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10388         else
10389                 log "cache hits:: before: $BEFORE, after: $AFTER"
10390         fi
10391
10392         rm -f $file
10393 }
10394 run_test 156 "Verification of tunables"
10395
10396 #Changelogs
10397 err17935 () {
10398         if [[ $MDSCOUNT -gt 1 ]]; then
10399                 error_ignore bz17935 $*
10400         else
10401                 error $*
10402         fi
10403 }
10404
10405 changelog_chmask()
10406 {
10407         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10408
10409         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10410
10411         if [ $MASK -eq 1 ]; then
10412                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10413         else
10414                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10415         fi
10416 }
10417
10418 changelog_extract_field() {
10419         local mdt=$1
10420         local cltype=$2
10421         local file=$3
10422         local identifier=$4
10423
10424         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10425                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10426                 tail -1
10427 }
10428
10429 test_160a() {
10430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10431         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10432         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10433                 { skip "Need MDS version at least 2.2.0"; return; }
10434
10435         local CL_USERS="mdd.$MDT0.changelog_users"
10436         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10437         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10438         echo "Registered as changelog user $USER"
10439         $GET_CL_USERS | grep -q $USER ||
10440                 error "User $USER not found in changelog_users"
10441
10442         # change something
10443         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10444         touch $DIR/$tdir/pics/2008/zachy/timestamp
10445         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10446         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10447         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10448         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10449         rm $DIR/$tdir/pics/desktop.jpg
10450
10451         $LFS changelog $MDT0 | tail -5
10452
10453         echo "verifying changelog mask"
10454         changelog_chmask "MKDIR"
10455         changelog_chmask "CLOSE"
10456
10457         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10458         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10459
10460         changelog_chmask "MKDIR"
10461         changelog_chmask "CLOSE"
10462
10463         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10464         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10465
10466         $LFS changelog $MDT0
10467         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10468         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10469         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10470         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10471
10472         # verify contents
10473         echo "verifying target fid"
10474         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10475         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10476         [ "$fidc" == "$fidf" ] ||
10477                 err17935 "fid in changelog $fidc != file fid $fidf"
10478         echo "verifying parent fid"
10479         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10480         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10481         [ "$fidc" == "$fidf" ] ||
10482                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10483
10484         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10485         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10486         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10487         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10488         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10489                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10490
10491         MIN_REC=$($GET_CL_USERS |
10492                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10493         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10494         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10495         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10496                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10497
10498         # LU-3446 changelog index reset on MDT restart
10499         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10500         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10501         $LFS changelog_clear $MDT0 $USER 0
10502         stop $SINGLEMDS || error "Fail to stop MDT."
10503         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10504         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10505         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10506         [ $CUR_REC1 == $CUR_REC2 ] ||
10507                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10508
10509         echo "verifying user deregister"
10510         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10511         $GET_CL_USERS | grep -q $USER &&
10512                 error "User $USER still in changelog_users"
10513
10514         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10515         if [ $USERS -eq 0 ]; then
10516                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10517                 touch $DIR/$tdir/chloe
10518                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10519                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10520                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10521         else
10522                 echo "$USERS other changelog users; can't verify off"
10523         fi
10524 }
10525 run_test 160a "changelog sanity"
10526
10527 test_160b() { # LU-3587
10528         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10529         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10530         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10531                 { skip "Need MDS version at least 2.2.0"; return; }
10532
10533         local CL_USERS="mdd.$MDT0.changelog_users"
10534         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10535         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10536         echo "Registered as changelog user $USER"
10537         $GET_CL_USERS | grep -q $USER ||
10538                 error "User $USER not found in changelog_users"
10539
10540         local LONGNAME1=$(str_repeat a 255)
10541         local LONGNAME2=$(str_repeat b 255)
10542
10543         cd $DIR
10544         echo "creating very long named file"
10545         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10546         echo "moving very long named file"
10547         mv $LONGNAME1 $LONGNAME2
10548
10549         $LFS changelog $MDT0 | grep RENME
10550
10551         echo "deregistering $USER"
10552         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10553
10554         rm -f $LONGNAME2
10555 }
10556 run_test 160b "Verify that very long rename doesn't crash in changelog"
10557
10558 test_160c() {
10559         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10560
10561         local rc=0
10562         local server_version=$(lustre_version_code $SINGLEMDS)
10563
10564         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10565                 [[ $server_version -gt $(version_code 2.5.1) &&
10566                    $server_version -lt $(version_code 2.5.50) ]] ||
10567                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10569
10570         # Registration step
10571         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10572                 changelog_register -n)
10573
10574         rm -rf $DIR/$tdir
10575         mkdir -p $DIR/$tdir
10576         $MCREATE $DIR/$tdir/foo_160c
10577         changelog_chmask "TRUNC"
10578         $TRUNCATE $DIR/$tdir/foo_160c 200
10579         changelog_chmask "TRUNC"
10580         $TRUNCATE $DIR/$tdir/foo_160c 199
10581         $LFS changelog $MDT0
10582         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10583         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10584         $LFS changelog_clear $MDT0 $USER 0
10585
10586         # Deregistration step
10587         echo "deregistering $USER"
10588         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10589 }
10590 run_test 160c "verify that changelog log catch the truncate event"
10591
10592 test_160d() {
10593         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10594         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10595
10596         local server_version=$(lustre_version_code mds1)
10597         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10598
10599         [[ $server_version -ge $(version_code 2.7.60) ]] ||
10600                 { skip "Need MDS version at least 2.7.60+"; return; }
10601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10602
10603         # Registration step
10604         local USER=$(do_facet mds1 $LCTL --device $MDT0 \
10605                 changelog_register -n)
10606
10607         mkdir -p $DIR/$tdir/migrate_dir
10608         $LFS changelog_clear $MDT0 $USER 0
10609
10610         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
10611         $LFS changelog $MDT0
10612         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
10613         $LFS changelog_clear $MDT0 $USER 0
10614         [ $MIGRATES -eq 1 ] ||
10615                 error "MIGRATE changelog mask count $MIGRATES != 1"
10616
10617         # Deregistration step
10618         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $USER
10619 }
10620 run_test 160d "verify that changelog log catch the migrate event"
10621
10622 test_161a() {
10623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10624         test_mkdir -p -c1 $DIR/$tdir
10625         cp /etc/hosts $DIR/$tdir/$tfile
10626         test_mkdir -c1 $DIR/$tdir/foo1
10627         test_mkdir -c1 $DIR/$tdir/foo2
10628         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10629         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10630         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10631         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10632         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10633         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10634                 $LFS fid2path $DIR $FID
10635                 err17935 "bad link ea"
10636         fi
10637     # middle
10638     rm $DIR/$tdir/foo2/zachary
10639     # last
10640     rm $DIR/$tdir/foo2/thor
10641     # first
10642     rm $DIR/$tdir/$tfile
10643     # rename
10644     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10645     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10646         then
10647         $LFS fid2path $DIR $FID
10648         err17935 "bad link rename"
10649     fi
10650     rm $DIR/$tdir/foo2/maggie
10651
10652         # overflow the EA
10653         local longname=filename_avg_len_is_thirty_two_
10654         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10655                 error "failed to hardlink many files"
10656         links=$($LFS fid2path $DIR $FID | wc -l)
10657         echo -n "${links}/1000 links in link EA"
10658         [[ $links -gt 60 ]] ||
10659                 err17935 "expected at least 60 links in link EA"
10660         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10661                 error "failed to unlink many hardlinks"
10662 }
10663 run_test 161a "link ea sanity"
10664
10665 test_161b() {
10666         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10667         [ $MDSCOUNT -lt 2 ] &&
10668                 skip "skipping remote directory test" && return
10669         local MDTIDX=1
10670         local remote_dir=$DIR/$tdir/remote_dir
10671
10672         mkdir -p $DIR/$tdir
10673         $LFS mkdir -i $MDTIDX $remote_dir ||
10674                 error "create remote directory failed"
10675
10676         cp /etc/hosts $remote_dir/$tfile
10677         mkdir -p $remote_dir/foo1
10678         mkdir -p $remote_dir/foo2
10679         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10680         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10681         ln $remote_dir/$tfile $remote_dir/foo1/luna
10682         ln $remote_dir/$tfile $remote_dir/foo2/thor
10683
10684         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10685                      tr -d ']')
10686         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10687                 $LFS fid2path $DIR $FID
10688                 err17935 "bad link ea"
10689         fi
10690         # middle
10691         rm $remote_dir/foo2/zachary
10692         # last
10693         rm $remote_dir/foo2/thor
10694         # first
10695         rm $remote_dir/$tfile
10696         # rename
10697         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10698         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10699         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10700                 $LFS fid2path $DIR $FID
10701                 err17935 "bad link rename"
10702         fi
10703         rm $remote_dir/foo2/maggie
10704
10705         # overflow the EA
10706         local longname=filename_avg_len_is_thirty_two_
10707         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10708                 error "failed to hardlink many files"
10709         links=$($LFS fid2path $DIR $FID | wc -l)
10710         echo -n "${links}/1000 links in link EA"
10711         [[ ${links} -gt 60 ]] ||
10712                 err17935 "expected at least 60 links in link EA"
10713         unlinkmany $remote_dir/foo2/$longname 1000 ||
10714         error "failed to unlink many hardlinks"
10715 }
10716 run_test 161b "link ea sanity under remote directory"
10717
10718 test_161c() {
10719         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10720         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10721         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10722                 skip "Need MDS version at least 2.1.5" && return
10723
10724         # define CLF_RENAME_LAST 0x0001
10725         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10726         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10727                 changelog_register -n)
10728         rm -rf $DIR/$tdir
10729         mkdir -p $DIR/$tdir
10730         touch $DIR/$tdir/foo_161c
10731         touch $DIR/$tdir/bar_161c
10732         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10733         $LFS changelog $MDT0 | grep RENME
10734         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10735                 cut -f5 -d' ')
10736         $LFS changelog_clear $MDT0 $USER 0
10737         if [ x$flags != "x0x1" ]; then
10738                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10739                         $USER
10740                 error "flag $flags is not 0x1"
10741         fi
10742         echo "rename overwrite a target having nlink = 1," \
10743                 "changelog record has flags of $flags"
10744
10745         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10746         touch $DIR/$tdir/foo_161c
10747         touch $DIR/$tdir/bar_161c
10748         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10749         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10750         $LFS changelog $MDT0 | grep RENME
10751         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10752         $LFS changelog_clear $MDT0 $USER 0
10753         if [ x$flags != "x0x0" ]; then
10754                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10755                         $USER
10756                 error "flag $flags is not 0x0"
10757         fi
10758         echo "rename overwrite a target having nlink > 1," \
10759                 "changelog record has flags of $flags"
10760
10761         # rename doesn't overwrite a target (changelog flag 0x0)
10762         touch $DIR/$tdir/foo_161c
10763         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10764         $LFS changelog $MDT0 | grep RENME
10765         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10766         $LFS changelog_clear $MDT0 $USER 0
10767         if [ x$flags != "x0x0" ]; then
10768                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10769                         $USER
10770                 error "flag $flags is not 0x0"
10771         fi
10772         echo "rename doesn't overwrite a target," \
10773                 "changelog record has flags of $flags"
10774
10775         # define CLF_UNLINK_LAST 0x0001
10776         # unlink a file having nlink = 1 (changelog flag 0x1)
10777         rm -f $DIR/$tdir/foo2_161c
10778         $LFS changelog $MDT0 | grep UNLNK
10779         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10780         $LFS changelog_clear $MDT0 $USER 0
10781         if [ x$flags != "x0x1" ]; then
10782                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10783                         $USER
10784                 error "flag $flags is not 0x1"
10785         fi
10786         echo "unlink a file having nlink = 1," \
10787                 "changelog record has flags of $flags"
10788
10789         # unlink a file having nlink > 1 (changelog flag 0x0)
10790         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10791         rm -f $DIR/$tdir/foobar_161c
10792         $LFS changelog $MDT0 | grep UNLNK
10793         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10794         $LFS changelog_clear $MDT0 $USER 0
10795         if [ x$flags != "x0x0" ]; then
10796                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10797                         $USER
10798                 error "flag $flags is not 0x0"
10799         fi
10800         echo "unlink a file having nlink > 1," \
10801                 "changelog record has flags of $flags"
10802         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10803 }
10804 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10805
10806 check_path() {
10807     local expected=$1
10808     shift
10809     local fid=$2
10810
10811     local path=$(${LFS} fid2path $*)
10812     # Remove the '//' indicating a remote directory
10813     path=$(echo $path | sed 's#//#/#g')
10814     RC=$?
10815
10816     if [ $RC -ne 0 ]; then
10817         err17935 "path looked up of $expected failed. Error $RC"
10818         return $RC
10819     elif [ "${path}" != "${expected}" ]; then
10820         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10821         return 2
10822     fi
10823     echo "fid $fid resolves to path $path (expected $expected)"
10824 }
10825
10826 test_162a() { # was test_162
10827         # Make changes to filesystem
10828         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10829         test_mkdir -p -c1 $DIR/$tdir/d2
10830         touch $DIR/$tdir/d2/$tfile
10831         touch $DIR/$tdir/d2/x1
10832         touch $DIR/$tdir/d2/x2
10833         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10834         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10835         # regular file
10836         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10837         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10838                 error "check path $tdir/d2/$tfile failed"
10839
10840         # softlink
10841         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10842         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10843         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10844                 error "check path $tdir/d2/p/q/r/slink failed"
10845
10846         # softlink to wrong file
10847         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10848         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10849         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10850                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10851
10852         # hardlink
10853         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10854         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10855         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10856         # fid2path dir/fsname should both work
10857         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10858                 error "check path $tdir/d2/a/b/c/new_file failed"
10859         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10860                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10861
10862         # hardlink count: check that there are 2 links
10863         # Doesnt work with CMD yet: 17935
10864         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10865                 err17935 "expected 2 links"
10866
10867         # hardlink indexing: remove the first link
10868         rm $DIR/$tdir/d2/p/q/r/hlink
10869         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10870                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10871
10872         return 0
10873 }
10874 run_test 162a "path lookup sanity"
10875
10876 test_162b() {
10877         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10878         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10879
10880         mkdir $DIR/$tdir
10881         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10882                                 error "create striped dir failed"
10883
10884         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10885                                         tail -n 1 | awk '{print $2}')
10886         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10887
10888         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10889         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10890
10891         # regular file
10892         for ((i=0;i<5;i++)); do
10893                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10894                         error "get fid for f$i failed"
10895                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10896                         error "check path $tdir/striped_dir/f$i failed"
10897
10898                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10899                         error "get fid for d$i failed"
10900                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10901                         error "check path $tdir/striped_dir/d$i failed"
10902         done
10903
10904         return 0
10905 }
10906 run_test 162b "striped directory path lookup sanity"
10907
10908 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10909 test_162c() {
10910         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
10911                 skip "Need MDS version at least 2.7.51" && return
10912         test_mkdir $DIR/$tdir.local
10913         test_mkdir $DIR/$tdir.remote
10914         local lpath=$tdir.local
10915         local rpath=$tdir.remote
10916
10917         for ((i = 0; i <= 101; i++)); do
10918                 lpath="$lpath/$i"
10919                 mkdir $DIR/$lpath
10920                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10921                         error "get fid for local directory $DIR/$lpath failed"
10922                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10923                         error "check path for local directory $DIR/$lpath failed"
10924
10925                 rpath="$rpath/$i"
10926                 test_mkdir $DIR/$rpath
10927                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10928                         error "get fid for remote directory $DIR/$rpath failed"
10929                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10930                         error "check path for remote directory $DIR/$rpath failed"
10931         done
10932
10933         return 0
10934 }
10935 run_test 162c "fid2path works with paths 100 or more directories deep"
10936
10937 test_169() {
10938         # do directio so as not to populate the page cache
10939         log "creating a 10 Mb file"
10940         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10941         log "starting reads"
10942         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10943         log "truncating the file"
10944         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10945         log "killing dd"
10946         kill %+ || true # reads might have finished
10947         echo "wait until dd is finished"
10948         wait
10949         log "removing the temporary file"
10950         rm -rf $DIR/$tfile || error "tmp file removal failed"
10951 }
10952 run_test 169 "parallel read and truncate should not deadlock"
10953
10954 test_170() {
10955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10956         $LCTL clear     # bug 18514
10957         $LCTL debug_daemon start $TMP/${tfile}_log_good
10958         touch $DIR/$tfile
10959         $LCTL debug_daemon stop
10960         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10961                error "sed failed to read log_good"
10962
10963         $LCTL debug_daemon start $TMP/${tfile}_log_good
10964         rm -rf $DIR/$tfile
10965         $LCTL debug_daemon stop
10966
10967         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10968                error "lctl df log_bad failed"
10969
10970         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10971         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10972
10973         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10974         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10975
10976         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10977                 error "bad_line good_line1 good_line2 are empty"
10978
10979         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10980         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10981         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10982
10983         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10984         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10985         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10986
10987         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10988                 error "bad_line_new good_line_new are empty"
10989
10990         local expected_good=$((good_line1 + good_line2*2))
10991
10992         rm -f $TMP/${tfile}*
10993         # LU-231, short malformed line may not be counted into bad lines
10994         if [ $bad_line -ne $bad_line_new ] &&
10995                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10996                 error "expected $bad_line bad lines, but got $bad_line_new"
10997                 return 1
10998         fi
10999
11000         if [ $expected_good -ne $good_line_new ]; then
11001                 error "expected $expected_good good lines, but got $good_line_new"
11002                 return 2
11003         fi
11004         true
11005 }
11006 run_test 170 "test lctl df to handle corrupted log ====================="
11007
11008 test_171() { # bug20592
11009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11010 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11011         $LCTL set_param fail_loc=0x50e
11012         $LCTL set_param fail_val=3000
11013         multiop_bg_pause $DIR/$tfile O_s || true
11014         local MULTIPID=$!
11015         kill -USR1 $MULTIPID
11016         # cause log dump
11017         sleep 3
11018         wait $MULTIPID
11019         if dmesg | grep "recursive fault"; then
11020                 error "caught a recursive fault"
11021         fi
11022         $LCTL set_param fail_loc=0
11023         true
11024 }
11025 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11026
11027 # it would be good to share it with obdfilter-survey/iokit-libecho code
11028 setup_obdecho_osc () {
11029         local rc=0
11030         local ost_nid=$1
11031         local obdfilter_name=$2
11032         echo "Creating new osc for $obdfilter_name on $ost_nid"
11033         # make sure we can find loopback nid
11034         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11035
11036         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11037                            ${obdfilter_name}_osc_UUID || rc=2; }
11038         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11039                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11040         return $rc
11041 }
11042
11043 cleanup_obdecho_osc () {
11044         local obdfilter_name=$1
11045         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11046         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11047         return 0
11048 }
11049
11050 obdecho_test() {
11051         local OBD=$1
11052         local node=$2
11053         local pages=${3:-64}
11054         local rc=0
11055         local id
11056
11057         local count=10
11058         local obd_size=$(get_obd_size $node $OBD)
11059         local page_size=$(get_page_size $node)
11060         if [[ -n "$obd_size" ]]; then
11061                 local new_count=$((obd_size / (pages * page_size / 1024)))
11062                 [[ $new_count -ge $count ]] || count=$new_count
11063         fi
11064
11065         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11066         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11067                            rc=2; }
11068         if [ $rc -eq 0 ]; then
11069             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11070             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11071         fi
11072         echo "New object id is $id"
11073         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11074                            rc=4; }
11075         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11076                            "test_brw $count w v $pages $id" || rc=4; }
11077         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11078                            rc=4; }
11079         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11080                                         "cleanup" || rc=5; }
11081         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11082                                         "detach" || rc=6; }
11083         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11084         return $rc
11085 }
11086
11087 test_180a() {
11088         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11089         remote_ost_nodsh && skip "remote OST with nodsh" && return
11090         local rc=0
11091         local rmmod_local=0
11092
11093         if ! module_loaded obdecho; then
11094             load_module obdecho/obdecho
11095             rmmod_local=1
11096         fi
11097
11098         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11099         local host=$(lctl get_param -n osc.$osc.import |
11100                              awk '/current_connection:/ {print $2}' )
11101         local target=$(lctl get_param -n osc.$osc.import |
11102                              awk '/target:/ {print $2}' )
11103         target=${target%_UUID}
11104
11105         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11106         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11107         [[ -n $target ]] && cleanup_obdecho_osc $target
11108         [ $rmmod_local -eq 1 ] && rmmod obdecho
11109         return $rc
11110 }
11111 run_test 180a "test obdecho on osc"
11112
11113 test_180b() {
11114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11115         remote_ost_nodsh && skip "remote OST with nodsh" && return
11116         local rc=0
11117         local rmmod_remote=0
11118
11119         do_facet ost1 "lsmod | grep -q obdecho || "                      \
11120                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
11121                       "modprobe obdecho; }" && rmmod_remote=1
11122         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11123         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11124         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
11125         return $rc
11126 }
11127 run_test 180b "test obdecho directly on obdfilter"
11128
11129 test_180c() { # LU-2598
11130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11131         remote_ost_nodsh && skip "remote OST with nodsh" && return
11132         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11133                 skip "Need MDS version at least 2.4.0" && return
11134
11135         local rc=0
11136         local rmmod_remote=false
11137         local pages=16384 # 64MB bulk I/O RPC size
11138         local target
11139
11140         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11141                 rmmod_remote=true || error "failed to load module obdecho"
11142
11143         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11144                 head -n1)
11145         if [ -n "$target" ]; then
11146                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11147         else
11148                 echo "there is no obdfilter target on ost1"
11149                 rc=2
11150         fi
11151         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11152         return $rc
11153 }
11154 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11155
11156 test_181() { # bug 22177
11157         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11158         # create enough files to index the directory
11159         createmany -o $DIR/$tdir/foobar 4000
11160         # print attributes for debug purpose
11161         lsattr -d .
11162         # open dir
11163         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11164         MULTIPID=$!
11165         # remove the files & current working dir
11166         unlinkmany $DIR/$tdir/foobar 4000
11167         rmdir $DIR/$tdir
11168         kill -USR1 $MULTIPID
11169         wait $MULTIPID
11170         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11171         return 0
11172 }
11173 run_test 181 "Test open-unlinked dir ========================"
11174
11175 test_182() {
11176         local fcount=1000
11177         local tcount=10
11178
11179         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11180
11181         $LCTL set_param mdc.*.rpc_stats=clear
11182
11183         for (( i = 0; i < $tcount; i++ )) ; do
11184                 mkdir $DIR/$tdir/$i
11185         done
11186
11187         for (( i = 0; i < $tcount; i++ )) ; do
11188                 createmany -o $DIR/$tdir/$i/f- $fcount &
11189         done
11190         wait
11191
11192         for (( i = 0; i < $tcount; i++ )) ; do
11193                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11194         done
11195         wait
11196
11197         $LCTL get_param mdc.*.rpc_stats
11198
11199         rm -rf $DIR/$tdir
11200 }
11201 run_test 182 "Test parallel modify metadata operations ================"
11202
11203 test_183() { # LU-2275
11204         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11205         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11206                 skip "Need MDS version at least 2.3.56" && return
11207
11208         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11209         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11210         echo aaa > $DIR/$tdir/$tfile
11211
11212 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11213         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11214
11215         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11216         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11217
11218         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11219
11220         # Flush negative dentry cache
11221         touch $DIR/$tdir/$tfile
11222
11223         # We are not checking for any leaked references here, they'll
11224         # become evident next time we do cleanup with module unload.
11225         rm -rf $DIR/$tdir
11226 }
11227 run_test 183 "No crash or request leak in case of strange dispositions ========"
11228
11229 # test suite 184 is for LU-2016, LU-2017
11230 test_184a() {
11231         check_swap_layouts_support && return 0
11232
11233         dir0=$DIR/$tdir/$testnum
11234         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11235         ref1=/etc/passwd
11236         ref2=/etc/group
11237         file1=$dir0/f1
11238         file2=$dir0/f2
11239         $SETSTRIPE -c1 $file1
11240         cp $ref1 $file1
11241         $SETSTRIPE -c2 $file2
11242         cp $ref2 $file2
11243         gen1=$($GETSTRIPE -g $file1)
11244         gen2=$($GETSTRIPE -g $file2)
11245
11246         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11247         gen=$($GETSTRIPE -g $file1)
11248         [[ $gen1 != $gen ]] ||
11249                 "Layout generation on $file1 does not change"
11250         gen=$($GETSTRIPE -g $file2)
11251         [[ $gen2 != $gen ]] ||
11252                 "Layout generation on $file2 does not change"
11253
11254         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11255         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11256 }
11257 run_test 184a "Basic layout swap"
11258
11259 test_184b() {
11260         check_swap_layouts_support && return 0
11261
11262         dir0=$DIR/$tdir/$testnum
11263         mkdir -p $dir0 || error "creating dir $dir0"
11264         file1=$dir0/f1
11265         file2=$dir0/f2
11266         file3=$dir0/f3
11267         dir1=$dir0/d1
11268         dir2=$dir0/d2
11269         mkdir $dir1 $dir2
11270         $SETSTRIPE -c1 $file1
11271         $SETSTRIPE -c2 $file2
11272         $SETSTRIPE -c1 $file3
11273         chown $RUNAS_ID $file3
11274         gen1=$($GETSTRIPE -g $file1)
11275         gen2=$($GETSTRIPE -g $file2)
11276
11277         $LFS swap_layouts $dir1 $dir2 &&
11278                 error "swap of directories layouts should fail"
11279         $LFS swap_layouts $dir1 $file1 &&
11280                 error "swap of directory and file layouts should fail"
11281         $RUNAS $LFS swap_layouts $file1 $file2 &&
11282                 error "swap of file we cannot write should fail"
11283         $LFS swap_layouts $file1 $file3 &&
11284                 error "swap of file with different owner should fail"
11285         /bin/true # to clear error code
11286 }
11287 run_test 184b "Forbidden layout swap (will generate errors)"
11288
11289 test_184c() {
11290         check_swap_layouts_support && return 0
11291
11292         local dir0=$DIR/$tdir/$testnum
11293         mkdir -p $dir0 || error "creating dir $dir0"
11294
11295         local ref1=$dir0/ref1
11296         local ref2=$dir0/ref2
11297         local file1=$dir0/file1
11298         local file2=$dir0/file2
11299         # create a file large enough for the concurrent test
11300         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11301         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11302         echo "ref file size: ref1($(stat -c %s $ref1))," \
11303              "ref2($(stat -c %s $ref2))"
11304
11305         cp $ref2 $file2
11306         dd if=$ref1 of=$file1 bs=16k &
11307         local DD_PID=$!
11308
11309         # Make sure dd starts to copy file
11310         while [ ! -f $file1 ]; do sleep 0.1; done
11311
11312         $LFS swap_layouts $file1 $file2
11313         local rc=$?
11314         wait $DD_PID
11315         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11316         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11317
11318         # how many bytes copied before swapping layout
11319         local copied=$(stat -c %s $file2)
11320         local remaining=$(stat -c %s $ref1)
11321         remaining=$((remaining - copied))
11322         echo "Copied $copied bytes before swapping layout..."
11323
11324         cmp -n $copied $file1 $ref2 | grep differ &&
11325                 error "Content mismatch [0, $copied) of ref2 and file1"
11326         cmp -n $copied $file2 $ref1 ||
11327                 error "Content mismatch [0, $copied) of ref1 and file2"
11328         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11329                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11330
11331         # clean up
11332         rm -f $ref1 $ref2 $file1 $file2
11333 }
11334 run_test 184c "Concurrent write and layout swap"
11335
11336 test_184d() {
11337         check_swap_layouts_support && return 0
11338         [ -z "$(which getfattr 2>/dev/null)" ] &&
11339                 skip "no getfattr command" && return 0
11340
11341         local file1=$DIR/$tdir/$tfile-1
11342         local file2=$DIR/$tdir/$tfile-2
11343         local file3=$DIR/$tdir/$tfile-3
11344         local lovea1
11345         local lovea2
11346
11347         mkdir -p $DIR/$tdir
11348         touch $file1 || error "create $file1 failed"
11349         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11350                 error "create $file2 failed"
11351         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11352                 error "create $file3 failed"
11353         lovea1=$($LFS getstripe $file1 | sed 1d)
11354
11355         $LFS swap_layouts $file2 $file3 ||
11356                 error "swap $file2 $file3 layouts failed"
11357         $LFS swap_layouts $file1 $file2 ||
11358                 error "swap $file1 $file2 layouts failed"
11359
11360         lovea2=$($LFS getstripe $file2 | sed 1d)
11361         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11362
11363         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11364         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11365 }
11366 run_test 184d "allow stripeless layouts swap"
11367
11368 test_184e() {
11369         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
11370                 { skip "Need MDS version at least 2.6.94"; return 0; }
11371         check_swap_layouts_support && return 0
11372         [ -z "$(which getfattr 2>/dev/null)" ] &&
11373                 skip "no getfattr command" && return 0
11374
11375         local file1=$DIR/$tdir/$tfile-1
11376         local file2=$DIR/$tdir/$tfile-2
11377         local file3=$DIR/$tdir/$tfile-3
11378         local lovea
11379
11380         mkdir -p $DIR/$tdir
11381         touch $file1 || error "create $file1 failed"
11382         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11383                 error "create $file2 failed"
11384         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11385                 error "create $file3 failed"
11386
11387         $LFS swap_layouts $file1 $file2 ||
11388                 error "swap $file1 $file2 layouts failed"
11389
11390         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11391         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11392
11393         echo 123 > $file1 || error "Should be able to write into $file1"
11394
11395         $LFS swap_layouts $file1 $file3 ||
11396                 error "swap $file1 $file3 layouts failed"
11397
11398         echo 123 > $file1 || error "Should be able to write into $file1"
11399
11400         rm -rf $file1 $file2 $file3
11401 }
11402 run_test 184e "Recreate layout after stripeless layout swaps"
11403
11404 test_185() { # LU-2441
11405         # LU-3553 - no volatile file support in old servers
11406         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11407                 { skip "Need MDS version at least 2.3.60"; return 0; }
11408
11409         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11410         touch $DIR/$tdir/spoo
11411         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11412         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11413                 error "cannot create/write a volatile file"
11414         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11415                 error "FID is still valid after close"
11416
11417         multiop_bg_pause $DIR/$tdir vVw4096_c
11418         local multi_pid=$!
11419
11420         local OLD_IFS=$IFS
11421         IFS=":"
11422         local fidv=($fid)
11423         IFS=$OLD_IFS
11424         # assume that the next FID for this client is sequential, since stdout
11425         # is unfortunately eaten by multiop_bg_pause
11426         local n=$((${fidv[1]} + 1))
11427         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11428         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11429                 error "FID is missing before close"
11430         kill -USR1 $multi_pid
11431         # 1 second delay, so if mtime change we will see it
11432         sleep 1
11433         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11434         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11435 }
11436 run_test 185 "Volatile file support"
11437
11438 test_187a() {
11439         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11440         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11441                 skip "Need MDS version at least 2.3.0" && return
11442
11443         local dir0=$DIR/$tdir/$testnum
11444         mkdir -p $dir0 || error "creating dir $dir0"
11445
11446         local file=$dir0/file1
11447         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11448         local dv1=$($LFS data_version $file)
11449         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11450         local dv2=$($LFS data_version $file)
11451         [[ $dv1 != $dv2 ]] ||
11452                 error "data version did not change on write $dv1 == $dv2"
11453
11454         # clean up
11455         rm -f $file1
11456 }
11457 run_test 187a "Test data version change"
11458
11459 test_187b() {
11460         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11461         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11462                 skip "Need MDS version at least 2.3.0" && return
11463
11464         local dir0=$DIR/$tdir/$testnum
11465         mkdir -p $dir0 || error "creating dir $dir0"
11466
11467         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11468         [[ ${DV[0]} != ${DV[1]} ]] ||
11469                 error "data version did not change on write"\
11470                       " ${DV[0]} == ${DV[1]}"
11471
11472         # clean up
11473         rm -f $file1
11474 }
11475 run_test 187b "Test data version change on volatile file"
11476
11477 test_200() {
11478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11479         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11480
11481         local POOL=${POOL:-cea1}
11482         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11483         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11484         # Pool OST targets
11485         local first_ost=0
11486         local last_ost=$(($OSTCOUNT - 1))
11487         local ost_step=2
11488         local ost_list=$(seq $first_ost $ost_step $last_ost)
11489         local ost_range="$first_ost $last_ost $ost_step"
11490         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11491         local file_dir=$POOL_ROOT/file_tst
11492         local subdir=$test_path/subdir
11493
11494         local rc=0
11495         while : ; do
11496                 # former test_200a test_200b
11497                 pool_add $POOL                          || { rc=$? ; break; }
11498                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11499                 # former test_200c test_200d
11500                 mkdir -p $test_path
11501                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11502                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11503                 mkdir -p $subdir
11504                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11505                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11506                                                         || { rc=$? ; break; }
11507                 # former test_200e test_200f
11508                 local files=$((OSTCOUNT*3))
11509                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11510                                                         || { rc=$? ; break; }
11511                 pool_create_files $POOL $file_dir $files "$ost_list" \
11512                                                         || { rc=$? ; break; }
11513                 # former test_200g test_200h
11514                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11515                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11516
11517                 # former test_201a test_201b test_201c
11518                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11519
11520                 local f=$test_path/$tfile
11521                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11522                 pool_remove $POOL $f                    || { rc=$? ; break; }
11523                 break
11524         done
11525
11526         cleanup_pools
11527         return $rc
11528 }
11529 run_test 200 "OST pools"
11530
11531 # usage: default_attr <count | size | offset>
11532 default_attr() {
11533         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11534 }
11535
11536 # usage: check_default_stripe_attr
11537 check_default_stripe_attr() {
11538         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11539         case $1 in
11540         --stripe-count|--count)
11541                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11542         --stripe-size|--size)
11543                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11544         --stripe-index|--index)
11545                 EXPECTED=-1;;
11546         *)
11547                 error "unknown getstripe attr '$1'"
11548         esac
11549
11550         [ $ACTUAL != $EXPECTED ] &&
11551                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11552 }
11553
11554 test_204a() {
11555         test_mkdir -p $DIR/$tdir
11556         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11557
11558         check_default_stripe_attr --stripe-count
11559         check_default_stripe_attr --stripe-size
11560         check_default_stripe_attr --stripe-index
11561
11562         return 0
11563 }
11564 run_test 204a "Print default stripe attributes ================="
11565
11566 test_204b() {
11567         test_mkdir -p $DIR/$tdir
11568         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11569
11570         check_default_stripe_attr --stripe-size
11571         check_default_stripe_attr --stripe-index
11572
11573         return 0
11574 }
11575 run_test 204b "Print default stripe size and offset  ==========="
11576
11577 test_204c() {
11578         test_mkdir -p $DIR/$tdir
11579         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11580
11581         check_default_stripe_attr --stripe-count
11582         check_default_stripe_attr --stripe-index
11583
11584         return 0
11585 }
11586 run_test 204c "Print default stripe count and offset ==========="
11587
11588 test_204d() {
11589         test_mkdir -p $DIR/$tdir
11590         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11591
11592         check_default_stripe_attr --stripe-count
11593         check_default_stripe_attr --stripe-size
11594
11595         return 0
11596 }
11597 run_test 204d "Print default stripe count and size ============="
11598
11599 test_204e() {
11600         test_mkdir -p $DIR/$tdir
11601         $SETSTRIPE -d $DIR/$tdir
11602
11603         check_default_stripe_attr --stripe-count --raw
11604         check_default_stripe_attr --stripe-size --raw
11605         check_default_stripe_attr --stripe-index --raw
11606
11607         return 0
11608 }
11609 run_test 204e "Print raw stripe attributes ================="
11610
11611 test_204f() {
11612         test_mkdir -p $DIR/$tdir
11613         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11614
11615         check_default_stripe_attr --stripe-size --raw
11616         check_default_stripe_attr --stripe-index --raw
11617
11618         return 0
11619 }
11620 run_test 204f "Print raw stripe size and offset  ==========="
11621
11622 test_204g() {
11623         test_mkdir -p $DIR/$tdir
11624         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11625
11626         check_default_stripe_attr --stripe-count --raw
11627         check_default_stripe_attr --stripe-index --raw
11628
11629         return 0
11630 }
11631 run_test 204g "Print raw stripe count and offset ==========="
11632
11633 test_204h() {
11634         test_mkdir -p $DIR/$tdir
11635         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11636
11637         check_default_stripe_attr --stripe-count --raw
11638         check_default_stripe_attr --stripe-size --raw
11639
11640         return 0
11641 }
11642 run_test 204h "Print raw stripe count and size ============="
11643
11644 # Figure out which job scheduler is being used, if any,
11645 # or use a fake one
11646 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11647         JOBENV=SLURM_JOB_ID
11648 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11649         JOBENV=LSB_JOBID
11650 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11651         JOBENV=PBS_JOBID
11652 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11653         JOBENV=LOADL_STEP_ID
11654 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11655         JOBENV=JOB_ID
11656 else
11657         $LCTL list_param jobid_name > /dev/null 2>&1
11658         if [ $? -eq 0 ]; then
11659                 JOBENV=nodelocal
11660         else
11661                 JOBENV=FAKE_JOBID
11662         fi
11663 fi
11664
11665 verify_jobstats() {
11666         local cmd=($1)
11667         shift
11668         local facets="$@"
11669
11670 # we don't really need to clear the stats for this test to work, since each
11671 # command has a unique jobid, but it makes debugging easier if needed.
11672 #       for facet in $facets; do
11673 #               local dev=$(convert_facet2label $facet)
11674 #               # clear old jobstats
11675 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
11676 #       done
11677
11678         # use a new JobID for each test, or we might see an old one
11679         [ "$JOBENV" = "FAKE_JOBID" ] &&
11680                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11681
11682         JOBVAL=${!JOBENV}
11683
11684         [ "$JOBENV" = "nodelocal" ] && {
11685                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11686                 $LCTL set_param jobid_name=$FAKE_JOBID
11687                 JOBVAL=$FAKE_JOBID
11688         }
11689
11690         log "Test: ${cmd[*]}"
11691         log "Using JobID environment variable $JOBENV=$JOBVAL"
11692
11693         if [ $JOBENV = "FAKE_JOBID" ]; then
11694                 FAKE_JOBID=$JOBVAL ${cmd[*]}
11695         else
11696                 ${cmd[*]}
11697         fi
11698
11699         # all files are created on OST0000
11700         for facet in $facets; do
11701                 local stats="*.$(convert_facet2label $facet).job_stats"
11702                 if [ $(do_facet $facet lctl get_param $stats |
11703                        grep -c $JOBVAL) -ne 1 ]; then
11704                         do_facet $facet lctl get_param $stats
11705                         error "No jobstats for $JOBVAL found on $facet::$stats"
11706                 fi
11707         done
11708 }
11709
11710 jobstats_set() {
11711         trap 0
11712         NEW_JOBENV=${1:-$OLD_JOBENV}
11713         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11714         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11715 }
11716
11717 cleanup_205() {
11718         do_facet $SINGLEMDS \
11719                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
11720         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11721         do_facet $SINGLEMDS lctl --device $MDT0 changelog_deregister $CL_USER
11722 }
11723
11724 test_205() { # Job stats
11725         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11726         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11727         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11728         remote_ost_nodsh && skip "remote OST with nodsh" && return
11729
11730         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11731                 skip "Server doesn't support jobstats" && return 0
11732         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11733
11734         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11735         if [ $OLD_JOBENV != $JOBENV ]; then
11736                 jobstats_set $JOBENV
11737                 trap cleanup_205 EXIT
11738         fi
11739
11740         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
11741         echo "Registered as changelog user $CL_USER"
11742
11743         OLD_INTERVAL=$(do_facet $SINGLEMDS \
11744                        lctl get_param -n mdt.*.job_cleanup_interval)
11745         local interval_new=5
11746         do_facet $SINGLEMDS \
11747                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
11748         local start=$SECONDS
11749
11750         local cmd
11751         # mkdir
11752         cmd="mkdir $DIR/$tdir"
11753         verify_jobstats "$cmd" "$SINGLEMDS"
11754         # rmdir
11755         cmd="rmdir $DIR/$tdir"
11756         verify_jobstats "$cmd" "$SINGLEMDS"
11757         # mkdir on secondary MDT
11758         if [ $MDSCOUNT -gt 1 ]; then
11759                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
11760                 verify_jobstats "$cmd" "mds2"
11761         fi
11762         # mknod
11763         cmd="mknod $DIR/$tfile c 1 3"
11764         verify_jobstats "$cmd" "$SINGLEMDS"
11765         # unlink
11766         cmd="rm -f $DIR/$tfile"
11767         verify_jobstats "$cmd" "$SINGLEMDS"
11768         # create all files on OST0000 so verify_jobstats can find OST stats
11769         # open & close
11770         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11771         verify_jobstats "$cmd" "$SINGLEMDS"
11772         # setattr
11773         cmd="touch $DIR/$tfile"
11774         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11775         # write
11776         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11777         verify_jobstats "$cmd" "ost1"
11778         # read
11779         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11780         verify_jobstats "$cmd" "ost1"
11781         # truncate
11782         cmd="$TRUNCATE $DIR/$tfile 0"
11783         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11784         # rename
11785         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
11786         verify_jobstats "$cmd" "$SINGLEMDS"
11787         # jobstats expiry - sleep until old stats should be expired
11788         local left=$((interval_new - (SECONDS - start)))
11789         [ $left -ge 0 ] && echo "sleep $left for expiry" && sleep $((left + 1))
11790         cmd="mkdir $DIR/$tdir.expire"
11791         verify_jobstats "$cmd" "$SINGLEMDS"
11792         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
11793             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
11794
11795         # Ensure that jobid are present in changelog (if supported by MDS)
11796         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11797         then
11798                 $LFS changelog $MDT0 | tail -9
11799                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11800                 [ $jobids -eq 9 ] ||
11801                         error "Wrong changelog jobid count $jobids != 9"
11802
11803                 # LU-5862
11804                 JOBENV="disable"
11805                 jobstats_set $JOBENV
11806                 touch $DIR/$tfile
11807                 $LFS changelog $MDT0 | tail -1
11808                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11809                 [ $jobids -eq 0 ] ||
11810                         error "Unexpected jobids when jobid_var=$JOBENV"
11811         fi
11812
11813         cleanup_205
11814 }
11815 run_test 205 "Verify job stats"
11816
11817 # LU-1480, LU-1773 and LU-1657
11818 test_206() {
11819         mkdir -p $DIR/$tdir
11820         $SETSTRIPE -c -1 $DIR/$tdir
11821 #define OBD_FAIL_LOV_INIT 0x1403
11822         $LCTL set_param fail_loc=0xa0001403
11823         $LCTL set_param fail_val=1
11824         touch $DIR/$tdir/$tfile || true
11825 }
11826 run_test 206 "fail lov_init_raid0() doesn't lbug"
11827
11828 test_207a() {
11829         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11830         local fsz=`stat -c %s $DIR/$tfile`
11831         cancel_lru_locks mdc
11832
11833         # do not return layout in getattr intent
11834 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11835         $LCTL set_param fail_loc=0x170
11836         local sz=`stat -c %s $DIR/$tfile`
11837
11838         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11839
11840         rm -rf $DIR/$tfile
11841 }
11842 run_test 207a "can refresh layout at glimpse"
11843
11844 test_207b() {
11845         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11846         local cksum=`md5sum $DIR/$tfile`
11847         local fsz=`stat -c %s $DIR/$tfile`
11848         cancel_lru_locks mdc
11849         cancel_lru_locks osc
11850
11851         # do not return layout in getattr intent
11852 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11853         $LCTL set_param fail_loc=0x171
11854
11855         # it will refresh layout after the file is opened but before read issues
11856         echo checksum is "$cksum"
11857         echo "$cksum" |md5sum -c --quiet || error "file differs"
11858
11859         rm -rf $DIR/$tfile
11860 }
11861 run_test 207b "can refresh layout at open"
11862
11863 test_208() {
11864         # FIXME: in this test suite, only RD lease is used. This is okay
11865         # for now as only exclusive open is supported. After generic lease
11866         # is done, this test suite should be revised. - Jinshan
11867
11868         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11869         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11870                 { skip "Need MDS version at least 2.4.52"; return 0; }
11871
11872         echo "==== test 1: verify get lease work"
11873         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11874
11875         echo "==== test 2: verify lease can be broken by upcoming open"
11876         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11877         local PID=$!
11878         sleep 1
11879
11880         $MULTIOP $DIR/$tfile oO_RDONLY:c
11881         kill -USR1 $PID && wait $PID || error "break lease error"
11882
11883         echo "==== test 3: verify lease can't be granted if an open already exists"
11884         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11885         local PID=$!
11886         sleep 1
11887
11888         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11889         kill -USR1 $PID && wait $PID || error "open file error"
11890
11891         echo "==== test 4: lease can sustain over recovery"
11892         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11893         PID=$!
11894         sleep 1
11895
11896         fail mds1
11897
11898         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11899
11900         echo "==== test 5: lease broken can't be regained by replay"
11901         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11902         PID=$!
11903         sleep 1
11904
11905         # open file to break lease and then recovery
11906         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11907         fail mds1
11908
11909         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11910
11911         rm -f $DIR/$tfile
11912 }
11913 run_test 208 "Exclusive open"
11914
11915 test_209() {
11916         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11917                 skip_env "must have disp_stripe" && return
11918
11919         touch $DIR/$tfile
11920         sync; sleep 5; sync;
11921
11922         echo 3 > /proc/sys/vm/drop_caches
11923         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11924
11925         # open/close 500 times
11926         for i in $(seq 500); do
11927                 cat $DIR/$tfile
11928         done
11929
11930         echo 3 > /proc/sys/vm/drop_caches
11931         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11932
11933         echo "before: $req_before, after: $req_after"
11934         [ $((req_after - req_before)) -ge 300 ] &&
11935                 error "open/close requests are not freed"
11936         return 0
11937 }
11938 run_test 209 "read-only open/close requests should be freed promptly"
11939
11940 test_212() {
11941         size=`date +%s`
11942         size=$((size % 8192 + 1))
11943         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11944         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11945         rm -f $DIR/f212 $DIR/f212.xyz
11946 }
11947 run_test 212 "Sendfile test ============================================"
11948
11949 test_213() {
11950         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11951         cancel_lru_locks osc
11952         lctl set_param fail_loc=0x8000040f
11953         # generate a read lock
11954         cat $DIR/$tfile > /dev/null
11955         # write to the file, it will try to cancel the above read lock.
11956         cat /etc/hosts >> $DIR/$tfile
11957 }
11958 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11959
11960 test_214() { # for bug 20133
11961         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11962         for (( i=0; i < 340; i++ )) ; do
11963                 touch $DIR/$tdir/d214c/a$i
11964         done
11965
11966         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11967         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11968         ls $DIR/d214c || error "ls $DIR/d214c failed"
11969         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11970         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11971 }
11972 run_test 214 "hash-indexed directory test - bug 20133"
11973
11974 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11975 create_lnet_proc_files() {
11976         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11977         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11978
11979         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11980         rm -f "$TMP/lnet_$1.sys_tmp"
11981 }
11982
11983 # counterpart of create_lnet_proc_files
11984 remove_lnet_proc_files() {
11985         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11986 }
11987
11988 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11989 # 3rd arg as regexp for body
11990 check_lnet_proc_stats() {
11991         local l=$(cat "$TMP/lnet_$1" |wc -l)
11992         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11993
11994         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11995 }
11996
11997 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11998 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11999 # optional and can be regexp for 2nd line (lnet.routes case)
12000 check_lnet_proc_entry() {
12001         local blp=2          # blp stands for 'position of 1st line of body'
12002         [ -z "$5" ] || blp=3 # lnet.routes case
12003
12004         local l=$(cat "$TMP/lnet_$1" |wc -l)
12005         # subtracting one from $blp because the body can be empty
12006         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12007
12008         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12009                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12010
12011         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12012                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12013
12014         # bail out if any unexpected line happened
12015         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12016         [ "$?" != 0 ] || error "$2 misformatted"
12017 }
12018
12019 test_215() { # for bugs 18102, 21079, 21517
12020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12021         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12022         local P='[1-9][0-9]*'           # positive numeric
12023         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12024         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12025         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12026         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12027
12028         local L1 # regexp for 1st line
12029         local L2 # regexp for 2nd line (optional)
12030         local BR # regexp for the rest (body)
12031
12032         # lnet.stats should look as 11 space-separated non-negative numerics
12033         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12034         create_lnet_proc_files "stats"
12035         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12036         remove_lnet_proc_files "stats"
12037
12038         # lnet.routes should look like this:
12039         # Routing disabled/enabled
12040         # net hops priority state router
12041         # where net is a string like tcp0, hops > 0, priority >= 0,
12042         # state is up/down,
12043         # router is a string like 192.168.1.1@tcp2
12044         L1="^Routing (disabled|enabled)$"
12045         L2="^net +hops +priority +state +router$"
12046         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12047         create_lnet_proc_files "routes"
12048         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12049         remove_lnet_proc_files "routes"
12050
12051         # lnet.routers should look like this:
12052         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12053         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12054         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12055         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12056         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12057         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12058         create_lnet_proc_files "routers"
12059         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12060         remove_lnet_proc_files "routers"
12061
12062         # lnet.peers should look like this:
12063         # nid refs state last max rtr min tx min queue
12064         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12065         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12066         # numeric (0 or >0 or <0), queue >= 0.
12067         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12068         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12069         create_lnet_proc_files "peers"
12070         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12071         remove_lnet_proc_files "peers"
12072
12073         # lnet.buffers  should look like this:
12074         # pages count credits min
12075         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12076         L1="^pages +count +credits +min$"
12077         BR="^ +$N +$N +$I +$I$"
12078         create_lnet_proc_files "buffers"
12079         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12080         remove_lnet_proc_files "buffers"
12081
12082         # lnet.nis should look like this:
12083         # nid status alive refs peer rtr max tx min
12084         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12085         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12086         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12087         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12088         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12089         create_lnet_proc_files "nis"
12090         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12091         remove_lnet_proc_files "nis"
12092
12093         # can we successfully write to lnet.stats?
12094         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12095         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12096 }
12097 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12098
12099 test_216() { # bug 20317
12100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12101         remote_ost_nodsh && skip "remote OST with nodsh" && return
12102
12103         local node
12104         local facets=$(get_facets OST)
12105         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12106
12107         save_lustre_params client "osc.*.contention_seconds" > $p
12108         save_lustre_params $facets \
12109                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12110         save_lustre_params $facets \
12111                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12112         save_lustre_params $facets \
12113                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12114         clear_osc_stats
12115
12116         # agressive lockless i/o settings
12117         for node in $(osts_nodes); do
12118                 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'
12119         done
12120         lctl set_param -n osc.*.contention_seconds 60
12121
12122         $DIRECTIO write $DIR/$tfile 0 10 4096
12123         $CHECKSTAT -s 40960 $DIR/$tfile
12124
12125         # disable lockless i/o
12126         for node in $(osts_nodes); do
12127                 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'
12128         done
12129         lctl set_param -n osc.*.contention_seconds 0
12130         clear_osc_stats
12131
12132         dd if=/dev/zero of=$DIR/$tfile count=0
12133         $CHECKSTAT -s 0 $DIR/$tfile
12134
12135         restore_lustre_params <$p
12136         rm -f $p
12137         rm $DIR/$tfile
12138 }
12139 run_test 216 "check lockless direct write works and updates file size and kms correctly"
12140
12141 test_217() { # bug 22430
12142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12143         local node
12144         local nid
12145
12146         for node in $(nodes_list); do
12147                 nid=$(host_nids_address $node $NETTYPE)
12148                 if [[ $nid = *-* ]] ; then
12149                         echo "lctl ping $nid@$NETTYPE"
12150                         lctl ping $nid@$NETTYPE
12151                 else
12152                         echo "skipping $node (no hyphen detected)"
12153                 fi
12154         done
12155 }
12156 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12157
12158 test_218() {
12159        # do directio so as not to populate the page cache
12160        log "creating a 10 Mb file"
12161        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12162        log "starting reads"
12163        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12164        log "truncating the file"
12165        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12166        log "killing dd"
12167        kill %+ || true # reads might have finished
12168        echo "wait until dd is finished"
12169        wait
12170        log "removing the temporary file"
12171        rm -rf $DIR/$tfile || error "tmp file removal failed"
12172 }
12173 run_test 218 "parallel read and truncate should not deadlock ======================="
12174
12175 test_219() {
12176         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12177         # write one partial page
12178         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12179         # set no grant so vvp_io_commit_write will do sync write
12180         $LCTL set_param fail_loc=0x411
12181         # write a full page at the end of file
12182         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12183
12184         $LCTL set_param fail_loc=0
12185         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12186         $LCTL set_param fail_loc=0x411
12187         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12188
12189         # LU-4201
12190         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12191         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12192 }
12193 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
12194
12195 test_220() { #LU-325
12196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12197         remote_ost_nodsh && skip "remote OST with nodsh" && return
12198         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12199         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12200         local OSTIDX=0
12201
12202         # create on MDT0000 so the last_id and next_id are correct
12203         mkdir $DIR/$tdir
12204         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
12205         OST=${OST%_UUID}
12206
12207         # on the mdt's osc
12208         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
12209         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
12210                         osc.$mdtosc_proc1.prealloc_last_id)
12211         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
12212                         osc.$mdtosc_proc1.prealloc_next_id)
12213
12214         $LFS df -i
12215
12216         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
12217         #define OBD_FAIL_OST_ENOINO              0x229
12218         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
12219         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
12220         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
12221
12222         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
12223
12224         MDSOBJS=$((last_id - next_id))
12225         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
12226
12227         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
12228         echo "OST still has $count kbytes free"
12229
12230         echo "create $MDSOBJS files @next_id..."
12231         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
12232
12233         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12234                         osc.$mdtosc_proc1.prealloc_last_id)
12235         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12236                         osc.$mdtosc_proc1.prealloc_next_id)
12237
12238         echo "after creation, last_id=$last_id2, next_id=$next_id2"
12239         $LFS df -i
12240
12241         echo "cleanup..."
12242
12243         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12244         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12245
12246         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12247         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12248         echo "unlink $MDSOBJS files @$next_id..."
12249         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12250 }
12251 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12252
12253 test_221() {
12254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12255         dd if=`which date` of=$MOUNT/date oflag=sync
12256         chmod +x $MOUNT/date
12257
12258         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12259         $LCTL set_param fail_loc=0x80001401
12260
12261         $MOUNT/date > /dev/null
12262         rm -f $MOUNT/date
12263 }
12264 run_test 221 "make sure fault and truncate race to not cause OOM"
12265
12266 test_222a () {
12267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12268        rm -rf $DIR/$tdir
12269        test_mkdir -p $DIR/$tdir
12270        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12271        createmany -o $DIR/$tdir/$tfile 10
12272        cancel_lru_locks mdc
12273        cancel_lru_locks osc
12274        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12275        $LCTL set_param fail_loc=0x31a
12276        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12277        $LCTL set_param fail_loc=0
12278        rm -r $DIR/$tdir
12279 }
12280 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12281
12282 test_222b () {
12283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12284        rm -rf $DIR/$tdir
12285        test_mkdir -p $DIR/$tdir
12286        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12287        createmany -o $DIR/$tdir/$tfile 10
12288        cancel_lru_locks mdc
12289        cancel_lru_locks osc
12290        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12291        $LCTL set_param fail_loc=0x31a
12292        rm -r $DIR/$tdir || "AGL for rmdir failed"
12293        $LCTL set_param fail_loc=0
12294 }
12295 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12296
12297 test_223 () {
12298         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12299        rm -rf $DIR/$tdir
12300        test_mkdir -p $DIR/$tdir
12301        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12302        createmany -o $DIR/$tdir/$tfile 10
12303        cancel_lru_locks mdc
12304        cancel_lru_locks osc
12305        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12306        $LCTL set_param fail_loc=0x31b
12307        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12308        $LCTL set_param fail_loc=0
12309        rm -r $DIR/$tdir
12310 }
12311 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12312
12313 test_224a() { # LU-1039, MRP-303
12314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12315         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12316         $LCTL set_param fail_loc=0x508
12317         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12318         $LCTL set_param fail_loc=0
12319         df $DIR
12320 }
12321 run_test 224a "Don't panic on bulk IO failure"
12322
12323 test_224b() { # LU-1039, MRP-303
12324         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12325         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12326         cancel_lru_locks osc
12327         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12328         $LCTL set_param fail_loc=0x515
12329         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12330         $LCTL set_param fail_loc=0
12331         df $DIR
12332 }
12333 run_test 224b "Don't panic on bulk IO failure"
12334
12335 test_224c() { # LU-6441
12336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12337         local pages_per_rpc=$($LCTL get_param \
12338                                 osc.*.max_pages_per_rpc)
12339         local at_max=$($LCTL get_param -n at_max)
12340         local timeout=$($LCTL get_param -n timeout)
12341         local test_at="$LCTL get_param -n at_max"
12342         local param_at="$FSNAME.sys.at_max"
12343         local test_timeout="$LCTL get_param -n timeout"
12344         local param_timeout="$FSNAME.sys.timeout"
12345
12346         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
12347
12348         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
12349                 error "conf_param at_max=0 failed"
12350         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
12351                 error "conf_param timeout=5 failed"
12352
12353         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
12354         $LCTL set_param fail_loc=0x520
12355         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
12356         sync
12357         $LCTL set_param fail_loc=0
12358
12359         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
12360                 error "conf_param at_max=$at_max failed"
12361         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
12362                 $timeout || error "conf_param timeout=$timeout failed"
12363
12364         $LCTL set_param -n $pages_per_rpc
12365 }
12366 run_test 224c "Don't hang if one of md lost during large bulk RPC"
12367
12368 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12369 test_225a () {
12370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12371         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12372         if [ -z ${MDSSURVEY} ]; then
12373               skip_env "mds-survey not found" && return
12374         fi
12375
12376         [ $MDSCOUNT -ge 2 ] &&
12377                 skip "skipping now for more than one MDT" && return
12378
12379        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12380             { skip "Need MDS version at least 2.2.51"; return; }
12381
12382        local mds=$(facet_host $SINGLEMDS)
12383        local target=$(do_nodes $mds 'lctl dl' | \
12384                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12385
12386        local cmd1="file_count=1000 thrhi=4"
12387        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12388        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12389        local cmd="$cmd1 $cmd2 $cmd3"
12390
12391        rm -f ${TMP}/mds_survey*
12392        echo + $cmd
12393        eval $cmd || error "mds-survey with zero-stripe failed"
12394        cat ${TMP}/mds_survey*
12395        rm -f ${TMP}/mds_survey*
12396 }
12397 run_test 225a "Metadata survey sanity with zero-stripe"
12398
12399 test_225b () {
12400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12401         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12402         if [ -z ${MDSSURVEY} ]; then
12403               skip_env "mds-survey not found" && return
12404         fi
12405         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12406             { skip "Need MDS version at least 2.2.51"; return; }
12407
12408         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12409               skip_env "Need to mount OST to test" && return
12410         fi
12411
12412         [ $MDSCOUNT -ge 2 ] &&
12413                 skip "skipping now for more than one MDT" && return
12414        local mds=$(facet_host $SINGLEMDS)
12415        local target=$(do_nodes $mds 'lctl dl' | \
12416                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12417
12418        local cmd1="file_count=1000 thrhi=4"
12419        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12420        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12421        local cmd="$cmd1 $cmd2 $cmd3"
12422
12423        rm -f ${TMP}/mds_survey*
12424        echo + $cmd
12425        eval $cmd || error "mds-survey with stripe_count failed"
12426        cat ${TMP}/mds_survey*
12427        rm -f ${TMP}/mds_survey*
12428 }
12429 run_test 225b "Metadata survey sanity with stripe_count = 1"
12430
12431 mcreate_path2fid () {
12432         local mode=$1
12433         local major=$2
12434         local minor=$3
12435         local name=$4
12436         local desc=$5
12437         local path=$DIR/$tdir/$name
12438         local fid
12439         local rc
12440         local fid_path
12441
12442         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12443                 error "cannot create $desc"
12444
12445         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12446         rc=$?
12447         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12448
12449         fid_path=$($LFS fid2path $MOUNT $fid)
12450         rc=$?
12451         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12452
12453         [ "$path" == "$fid_path" ] ||
12454                 error "fid2path returned $fid_path, expected $path"
12455
12456         echo "pass with $path and $fid"
12457 }
12458
12459 test_226a () {
12460         rm -rf $DIR/$tdir
12461         mkdir -p $DIR/$tdir
12462
12463         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12464         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12465         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12466         mcreate_path2fid 0040666 0 0 dir "directory"
12467         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12468         mcreate_path2fid 0100666 0 0 file "regular file"
12469         mcreate_path2fid 0120666 0 0 link "symbolic link"
12470         mcreate_path2fid 0140666 0 0 sock "socket"
12471 }
12472 run_test 226a "call path2fid and fid2path on files of all type"
12473
12474 test_226b () {
12475         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12476         rm -rf $DIR/$tdir
12477         local MDTIDX=1
12478
12479         mkdir -p $DIR/$tdir
12480         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12481                 error "create remote directory failed"
12482         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12483         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12484                                 "character special file (null)"
12485         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12486                                 "character special file (no device)"
12487         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12488         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12489                                 "block special file (loop)"
12490         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12491         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12492         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12493 }
12494 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12495
12496 # LU-1299 Executing or running ldd on a truncated executable does not
12497 # cause an out-of-memory condition.
12498 test_227() {
12499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12500         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12501         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12502         chmod +x $MOUNT/date
12503
12504         $MOUNT/date > /dev/null
12505         ldd $MOUNT/date > /dev/null
12506         rm -f $MOUNT/date
12507 }
12508 run_test 227 "running truncated executable does not cause OOM"
12509
12510 # LU-1512 try to reuse idle OI blocks
12511 test_228a() {
12512         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12513         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12514         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12515                 skip "non-ldiskfs backend" && return
12516
12517         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12518         local myDIR=$DIR/$tdir
12519
12520         mkdir -p $myDIR
12521         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12522         $LCTL set_param fail_loc=0x80001002
12523         createmany -o $myDIR/t- 10000
12524         $LCTL set_param fail_loc=0
12525         # The guard is current the largest FID holder
12526         touch $myDIR/guard
12527         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12528                     tr -d '[')
12529         local IDX=$(($SEQ % 64))
12530
12531         do_facet $SINGLEMDS sync
12532         # Make sure journal flushed.
12533         sleep 6
12534         local blk1=$(do_facet $SINGLEMDS \
12535                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12536                      grep Blockcount | awk '{print $4}')
12537
12538         # Remove old files, some OI blocks will become idle.
12539         unlinkmany $myDIR/t- 10000
12540         # Create new files, idle OI blocks should be reused.
12541         createmany -o $myDIR/t- 2000
12542         do_facet $SINGLEMDS sync
12543         # Make sure journal flushed.
12544         sleep 6
12545         local blk2=$(do_facet $SINGLEMDS \
12546                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12547                      grep Blockcount | awk '{print $4}')
12548
12549         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12550 }
12551 run_test 228a "try to reuse idle OI blocks"
12552
12553 test_228b() {
12554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12555         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12556         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12557                 skip "non-ldiskfs backend" && return
12558
12559         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12560         local myDIR=$DIR/$tdir
12561
12562         mkdir -p $myDIR
12563         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12564         $LCTL set_param fail_loc=0x80001002
12565         createmany -o $myDIR/t- 10000
12566         $LCTL set_param fail_loc=0
12567         # The guard is current the largest FID holder
12568         touch $myDIR/guard
12569         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12570                     tr -d '[')
12571         local IDX=$(($SEQ % 64))
12572
12573         do_facet $SINGLEMDS sync
12574         # Make sure journal flushed.
12575         sleep 6
12576         local blk1=$(do_facet $SINGLEMDS \
12577                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12578                      grep Blockcount | awk '{print $4}')
12579
12580         # Remove old files, some OI blocks will become idle.
12581         unlinkmany $myDIR/t- 10000
12582
12583         # stop the MDT
12584         stop $SINGLEMDS || error "Fail to stop MDT."
12585         # remount the MDT
12586         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12587
12588         df $MOUNT || error "Fail to df."
12589         # Create new files, idle OI blocks should be reused.
12590         createmany -o $myDIR/t- 2000
12591         do_facet $SINGLEMDS sync
12592         # Make sure journal flushed.
12593         sleep 6
12594         local blk2=$(do_facet $SINGLEMDS \
12595                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12596                      grep Blockcount | awk '{print $4}')
12597
12598         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12599 }
12600 run_test 228b "idle OI blocks can be reused after MDT restart"
12601
12602 #LU-1881
12603 test_228c() {
12604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12605         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12606         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12607                 skip "non-ldiskfs backend" && return
12608
12609         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12610         local myDIR=$DIR/$tdir
12611
12612         mkdir -p $myDIR
12613         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12614         $LCTL set_param fail_loc=0x80001002
12615         # 20000 files can guarantee there are index nodes in the OI file
12616         createmany -o $myDIR/t- 20000
12617         $LCTL set_param fail_loc=0
12618         # The guard is current the largest FID holder
12619         touch $myDIR/guard
12620         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12621                     tr -d '[')
12622         local IDX=$(($SEQ % 64))
12623
12624         do_facet $SINGLEMDS sync
12625         # Make sure journal flushed.
12626         sleep 6
12627         local blk1=$(do_facet $SINGLEMDS \
12628                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12629                      grep Blockcount | awk '{print $4}')
12630
12631         # Remove old files, some OI blocks will become idle.
12632         unlinkmany $myDIR/t- 20000
12633         rm -f $myDIR/guard
12634         # The OI file should become empty now
12635
12636         # Create new files, idle OI blocks should be reused.
12637         createmany -o $myDIR/t- 2000
12638         do_facet $SINGLEMDS sync
12639         # Make sure journal flushed.
12640         sleep 6
12641         local blk2=$(do_facet $SINGLEMDS \
12642                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12643                      grep Blockcount | awk '{print $4}')
12644
12645         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12646 }
12647 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12648
12649 test_229() { # LU-2482, LU-3448
12650         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12651                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
12652                 return
12653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12654         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12655
12656         rm -f $DIR/$tfile
12657
12658         # Create a file with a released layout and stripe count 2.
12659         $MULTIOP $DIR/$tfile H2c ||
12660                 error "failed to create file with released layout"
12661
12662         $GETSTRIPE -v $DIR/$tfile
12663
12664         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12665         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12666
12667         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12668         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12669         stat $DIR/$tfile || error "failed to stat released file"
12670
12671         chown $RUNAS_ID $DIR/$tfile ||
12672                 error "chown $RUNAS_ID $DIR/$tfile failed"
12673
12674         chgrp $RUNAS_ID $DIR/$tfile ||
12675                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12676
12677         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12678         rm $DIR/$tfile || error "failed to remove released file"
12679 }
12680 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12681
12682 test_230a() {
12683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12684         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12685         local MDTIDX=1
12686
12687         test_mkdir $DIR/$tdir
12688         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
12689         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12690         [ $mdt_idx -ne 0 ] &&
12691                 error "create local directory on wrong MDT $mdt_idx"
12692
12693         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12694                         error "create remote directory failed"
12695         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12696         [ $mdt_idx -ne $MDTIDX ] &&
12697                 error "create remote directory on wrong MDT $mdt_idx"
12698
12699         createmany -o $DIR/$tdir/test_230/t- 10 ||
12700                 error "create files on remote directory failed"
12701         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12702         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12703         rm -r $DIR/$tdir || error "unlink remote directory failed"
12704 }
12705 run_test 230a "Create remote directory and files under the remote directory"
12706
12707 test_230b() {
12708         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12709         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12710         local MDTIDX=1
12711         local mdt_index
12712         local i
12713         local file
12714         local pid
12715         local stripe_count
12716         local migrate_dir=$DIR/$tdir/migrate_dir
12717         local other_dir=$DIR/$tdir/other_dir
12718
12719         test_mkdir $DIR/$tdir
12720         test_mkdir -i0 -c1 $migrate_dir
12721         test_mkdir -i0 -c1 $other_dir
12722         for ((i=0; i<10; i++)); do
12723                 mkdir -p $migrate_dir/dir_${i}
12724                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12725                         error "create files under remote dir failed $i"
12726         done
12727
12728         cp /etc/passwd $migrate_dir/$tfile
12729         cp /etc/passwd $other_dir/$tfile
12730         chattr +SAD $migrate_dir
12731         chattr +SAD $migrate_dir/$tfile
12732
12733         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12734         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12735         local old_dir_mode=$(stat -c%f $migrate_dir)
12736         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12737
12738         mkdir -p $migrate_dir/dir_default_stripe2
12739         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12740         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12741
12742         mkdir -p $other_dir
12743         ln $migrate_dir/$tfile $other_dir/luna
12744         ln $migrate_dir/$tfile $migrate_dir/sofia
12745         ln $other_dir/$tfile $migrate_dir/david
12746         ln -s $migrate_dir/$tfile $other_dir/zachary
12747         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12748         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12749
12750         $LFS migrate -m $MDTIDX $migrate_dir ||
12751                 error "fails on migrating remote dir to MDT1"
12752
12753         echo "migratate to MDT1, then checking.."
12754         for ((i = 0; i < 10; i++)); do
12755                 for file in $(find $migrate_dir/dir_${i}); do
12756                         mdt_index=$($LFS getstripe -M $file)
12757                         [ $mdt_index == $MDTIDX ] ||
12758                                 error "$file is not on MDT${MDTIDX}"
12759                 done
12760         done
12761
12762         # the multiple link file should still in MDT0
12763         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12764         [ $mdt_index == 0 ] ||
12765                 error "$file is not on MDT${MDTIDX}"
12766
12767         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12768         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12769                 error " expect $old_dir_flag get $new_dir_flag"
12770
12771         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12772         [ "$old_file_flag" = "$new_file_flag" ] ||
12773                 error " expect $old_file_flag get $new_file_flag"
12774
12775         local new_dir_mode=$(stat -c%f $migrate_dir)
12776         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12777                 error "expect mode $old_dir_mode get $new_dir_mode"
12778
12779         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12780         [ "$old_file_mode" = "$new_file_mode" ] ||
12781                 error "expect mode $old_file_mode get $new_file_mode"
12782
12783         diff /etc/passwd $migrate_dir/$tfile ||
12784                 error "$tfile different after migration"
12785
12786         diff /etc/passwd $other_dir/luna ||
12787                 error "luna different after migration"
12788
12789         diff /etc/passwd $migrate_dir/sofia ||
12790                 error "sofia different after migration"
12791
12792         diff /etc/passwd $migrate_dir/david ||
12793                 error "david different after migration"
12794
12795         diff /etc/passwd $other_dir/zachary ||
12796                 error "zachary different after migration"
12797
12798         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12799                 error "${tfile}_ln different after migration"
12800
12801         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12802                 error "${tfile}_ln_other different after migration"
12803
12804         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12805         [ $stripe_count = 2 ] ||
12806                 error "dir strpe_count $d != 2 after migration."
12807
12808         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12809         [ $stripe_count = 2 ] ||
12810                 error "file strpe_count $d != 2 after migration."
12811
12812         #migrate back to MDT0
12813         MDTIDX=0
12814
12815         $LFS migrate -m $MDTIDX $migrate_dir ||
12816                 error "fails on migrating remote dir to MDT0"
12817
12818         echo "migrate back to MDT0, checking.."
12819         for file in $(find $migrate_dir); do
12820                 mdt_index=$($LFS getstripe -M $file)
12821                 [ $mdt_index == $MDTIDX ] ||
12822                         error "$file is not on MDT${MDTIDX}"
12823         done
12824
12825         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12826         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12827                 error " expect $old_dir_flag get $new_dir_flag"
12828
12829         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12830         [ "$old_file_flag" = "$new_file_flag" ] ||
12831                 error " expect $old_file_flag get $new_file_flag"
12832
12833         local new_dir_mode=$(stat -c%f $migrate_dir)
12834         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12835                 error "expect mode $old_dir_mode get $new_dir_mode"
12836
12837         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12838         [ "$old_file_mode" = "$new_file_mode" ] ||
12839                 error "expect mode $old_file_mode get $new_file_mode"
12840
12841         diff /etc/passwd ${migrate_dir}/$tfile ||
12842                 error "$tfile different after migration"
12843
12844         diff /etc/passwd ${other_dir}/luna ||
12845                 error "luna different after migration"
12846
12847         diff /etc/passwd ${migrate_dir}/sofia ||
12848                 error "sofia different after migration"
12849
12850         diff /etc/passwd ${other_dir}/zachary ||
12851                 error "zachary different after migration"
12852
12853         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12854                 error "${tfile}_ln different after migration"
12855
12856         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12857                 error "${tfile}_ln_other different after migration"
12858
12859         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12860         [ $stripe_count = 2 ] ||
12861                 error "dir strpe_count $d != 2 after migration."
12862
12863         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12864         [ $stripe_count = 2 ] ||
12865                 error "file strpe_count $d != 2 after migration."
12866
12867         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12868 }
12869 run_test 230b "migrate directory"
12870
12871 test_230c() {
12872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12873         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12874         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12875         local MDTIDX=1
12876         local mdt_index
12877         local file
12878         local migrate_dir=$DIR/$tdir/migrate_dir
12879
12880         #If migrating directory fails in the middle, all entries of
12881         #the directory is still accessiable.
12882         test_mkdir $DIR/$tdir
12883         test_mkdir -i0 -c1 $migrate_dir
12884         stat $migrate_dir
12885         createmany -o $migrate_dir/f 10 ||
12886                 error "create files under ${migrate_dir} failed"
12887
12888         #failed after migrating 5 entries
12889         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12890         do_facet mds1 lctl set_param fail_loc=0x20001801
12891         do_facet mds1 lctl  set_param fail_val=5
12892         local t=$(ls $migrate_dir | wc -l)
12893         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
12894                 error "migrate should fail after 5 entries"
12895
12896         mkdir $migrate_dir/dir &&
12897                 error "mkdir succeeds under migrating directory"
12898         touch $migrate_dir/file &&
12899                 error "touch file succeeds under migrating directory"
12900
12901         local u=$(ls $migrate_dir | wc -l)
12902         [ "$u" == "$t" ] || error "$u != $t during migration"
12903
12904         for file in $(find $migrate_dir); do
12905                 stat $file || error "stat $file failed"
12906         done
12907
12908         do_facet mds1 lctl set_param fail_loc=0
12909         do_facet mds1 lctl set_param fail_val=0
12910
12911         $LFS migrate -m $MDTIDX $migrate_dir ||
12912                 error "migrate open files should failed with open files"
12913
12914         echo "Finish migration, then checking.."
12915         for file in $(find $migrate_dir); do
12916                 mdt_index=$($LFS getstripe -M $file)
12917                 [ $mdt_index == $MDTIDX ] ||
12918                         error "$file is not on MDT${MDTIDX}"
12919         done
12920
12921         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12922 }
12923 run_test 230c "check directory accessiblity if migration is failed"
12924
12925 test_230d() {
12926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12927         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12928         local MDTIDX=1
12929         local mdt_index
12930         local migrate_dir=$DIR/$tdir/migrate_dir
12931         local i
12932         local j
12933
12934         test_mkdir $DIR/$tdir
12935         test_mkdir -i0 -c1 $migrate_dir
12936
12937         for ((i=0; i<100; i++)); do
12938                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
12939                 createmany -o $migrate_dir/dir_${i}/f 100 ||
12940                         error "create files under remote dir failed $i"
12941         done
12942
12943         $LFS migrate -m $MDTIDX $migrate_dir ||
12944                 error "migrate remote dir error"
12945
12946         echo "Finish migration, then checking.."
12947         for file in $(find $migrate_dir); do
12948                 mdt_index=$($LFS getstripe -M $file)
12949                 [ $mdt_index == $MDTIDX ] ||
12950                         error "$file is not on MDT${MDTIDX}"
12951         done
12952
12953         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12954 }
12955 run_test 230d "check migrate big directory"
12956
12957 test_230e() {
12958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12959         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12960         local i
12961         local j
12962         local a_fid
12963         local b_fid
12964
12965         mkdir -p $DIR/$tdir
12966         mkdir $DIR/$tdir/migrate_dir
12967         mkdir $DIR/$tdir/other_dir
12968         touch $DIR/$tdir/migrate_dir/a
12969         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
12970
12971         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
12972                 error "migrate dir fails"
12973
12974         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
12975         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
12976
12977         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12978         [ $mdt_index == 0 ] || error "a is not on MDT0"
12979
12980         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
12981                 error "migrate dir fails"
12982
12983         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
12984         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
12985
12986         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12987         [ $mdt_index == 1 ] || error "a is not on MDT1"
12988
12989         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
12990         [ $mdt_index == 1 ] || error "b is not on MDT1"
12991
12992         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
12993         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
12994
12995         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
12996
12997         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12998 }
12999 run_test 230e "migrate mulitple link files"
13000
13001 test_231a()
13002 {
13003         # For simplicity this test assumes that max_pages_per_rpc
13004         # is the same across all OSCs
13005         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
13006         local bulk_size=$((max_pages * 4096))
13007
13008         mkdir -p $DIR/$tdir
13009
13010         # clear the OSC stats
13011         $LCTL set_param osc.*.stats=0 &>/dev/null
13012
13013         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
13014         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
13015                 oflag=direct &>/dev/null || error "dd failed"
13016
13017         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
13018         if [ x$nrpcs != "x1" ]; then
13019                 error "found $nrpc ost_write RPCs, not 1 as expected"
13020         fi
13021
13022         # Drop the OSC cache, otherwise we will read from it
13023         cancel_lru_locks osc
13024
13025         # clear the OSC stats
13026         $LCTL set_param osc.*.stats=0 &>/dev/null
13027
13028         # Client reads $bulk_size.
13029         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
13030                 iflag=direct &>/dev/null || error "dd failed"
13031
13032         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
13033         if [ x$nrpcs != "x1" ]; then
13034                 error "found $nrpc ost_read RPCs, not 1 as expected"
13035         fi
13036 }
13037 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
13038
13039 test_231b() {
13040         mkdir -p $DIR/$tdir
13041         local i
13042         for i in {0..1023}; do
13043                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
13044                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
13045                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
13046         done
13047         sync
13048 }
13049 run_test 231b "must not assert on fully utilized OST request buffer"
13050
13051 test_232() {
13052         mkdir -p $DIR/$tdir
13053         #define OBD_FAIL_LDLM_OST_LVB            0x31c
13054         $LCTL set_param fail_loc=0x31c
13055
13056         # ignore dd failure
13057         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
13058
13059         $LCTL set_param fail_loc=0
13060         umount_client $MOUNT || error "umount failed"
13061         mount_client $MOUNT || error "mount failed"
13062 }
13063 run_test 232 "failed lock should not block umount"
13064
13065 test_233a() {
13066         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
13067         { skip "Need MDS version at least 2.3.64"; return; }
13068
13069         local fid=$($LFS path2fid $MOUNT)
13070         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13071                 error "cannot access $MOUNT using its FID '$fid'"
13072 }
13073 run_test 233a "checking that OBF of the FS root succeeds"
13074
13075 test_233b() {
13076         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
13077         { skip "Need MDS version at least 2.5.90"; return; }
13078
13079         local fid=$($LFS path2fid $MOUNT/.lustre)
13080         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13081                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
13082
13083         fid=$($LFS path2fid $MOUNT/.lustre/fid)
13084         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13085                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
13086 }
13087 run_test 233b "checking that OBF of the FS .lustre succeeds"
13088
13089 test_234() {
13090         local p="$TMP/sanityN-$TESTNAME.parameters"
13091         save_lustre_params client "llite.*.xattr_cache" > $p
13092         lctl set_param llite.*.xattr_cache 1 ||
13093                 { skip "xattr cache is not supported"; return 0; }
13094
13095         mkdir -p $DIR/$tdir || error "mkdir failed"
13096         touch $DIR/$tdir/$tfile || error "touch failed"
13097         # OBD_FAIL_LLITE_XATTR_ENOMEM
13098         $LCTL set_param fail_loc=0x1405
13099         # output of the form: attr 2 4 44 3 fc13 x86_64
13100         V=($(IFS=".-" rpm -q attr))
13101         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
13102               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
13103                 # attr pre-2.4.44-7 had a bug with rc
13104                 # LU-3703 - SLES 11 and FC13 clients have older attr
13105                 getfattr -n user.attr $DIR/$tdir/$tfile &&
13106                         error "getfattr should have failed with ENOMEM"
13107         else
13108                 skip "LU-3703: attr version $(getfattr --version) too old"
13109         fi
13110         $LCTL set_param fail_loc=0x0
13111         rm -rf $DIR/$tdir
13112
13113         restore_lustre_params < $p
13114         rm -f $p
13115 }
13116 run_test 234 "xattr cache should not crash on ENOMEM"
13117
13118 test_235() {
13119         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
13120                 skip "Need MDS version at least 2.4.52" && return
13121         flock_deadlock $DIR/$tfile
13122         local RC=$?
13123         case $RC in
13124                 0)
13125                 ;;
13126                 124) error "process hangs on a deadlock"
13127                 ;;
13128                 *) error "error executing flock_deadlock $DIR/$tfile"
13129                 ;;
13130         esac
13131 }
13132 run_test 235 "LU-1715: flock deadlock detection does not work properly"
13133
13134 #LU-2935
13135 test_236() {
13136         check_swap_layouts_support && return 0
13137         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
13138
13139         local ref1=/etc/passwd
13140         local ref2=/etc/group
13141         local file1=$DIR/$tdir/f1
13142         local file2=$DIR/$tdir/f2
13143
13144         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
13145         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
13146         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
13147         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
13148         local fd=$(free_fd)
13149         local cmd="exec $fd<>$file2"
13150         eval $cmd
13151         rm $file2
13152         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
13153                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
13154         cmd="exec $fd>&-"
13155         eval $cmd
13156         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13157
13158         #cleanup
13159         rm -rf $DIR/$tdir
13160 }
13161 run_test 236 "Layout swap on open unlinked file"
13162
13163 # test to verify file handle related system calls
13164 # (name_to_handle_at/open_by_handle_at)
13165 # The new system calls are supported in glibc >= 2.14.
13166
13167 test_237() {
13168         echo "Test file_handle syscalls" > $DIR/$tfile ||
13169                 error "write failed"
13170         check_fhandle_syscalls $DIR/$tfile ||
13171                 error "check_fhandle_syscalls failed"
13172 }
13173 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
13174
13175 # LU-4659 linkea consistency
13176 test_238() {
13177         local server_version=$(lustre_version_code $SINGLEMDS)
13178
13179         [[ $server_version -gt $(version_code 2.5.57) ]] ||
13180                 [[ $server_version -gt $(version_code 2.5.1) &&
13181                    $server_version -lt $(version_code 2.5.50) ]] ||
13182                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
13183
13184         touch $DIR/$tfile
13185         ln $DIR/$tfile $DIR/$tfile.lnk
13186         touch $DIR/$tfile.new
13187         mv $DIR/$tfile.new $DIR/$tfile
13188         local fid1=$($LFS path2fid $DIR/$tfile)
13189         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
13190         local path1=$($LFS fid2path $FSNAME "$fid1")
13191         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
13192         local path2=$($LFS fid2path $FSNAME "$fid2")
13193         [ $tfile.lnk == $path2 ] ||
13194                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
13195         rm -f $DIR/$tfile*
13196 }
13197 run_test 238 "Verify linkea consistency"
13198
13199 test_239() {
13200         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
13201                 skip "Need MDS version at least 2.5.60" && return
13202         local list=$(comma_list $(mdts_nodes))
13203
13204         mkdir -p $DIR/$tdir
13205         createmany -o $DIR/$tdir/f- 5000
13206         unlinkmany $DIR/$tdir/f- 5000
13207         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
13208         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
13209                         osc.*MDT*.sync_in_flight" | calc_sum)
13210         [ "$changes" -eq 0 ] || error "$changes not synced"
13211 }
13212 run_test 239 "osp_sync test"
13213
13214 test_239a() { #LU-5297
13215         touch $DIR/$tfile
13216         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
13217         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
13218         chgrp $RUNAS_GID $DIR/$tfile
13219         wait_delete_completed
13220 }
13221 run_test 239a "process invalid osp sync record correctly"
13222
13223 test_239b() { #LU-5297
13224         touch $DIR/$tfile1
13225         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
13226         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
13227         chgrp $RUNAS_GID $DIR/$tfile1
13228         wait_delete_completed
13229         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13230         touch $DIR/$tfile2
13231         chgrp $RUNAS_GID $DIR/$tfile2
13232         wait_delete_completed
13233 }
13234 run_test 239b "process osp sync record with ENOMEM error correctly"
13235
13236 test_240() {
13237         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13238
13239         mkdir -p $DIR/$tdir
13240
13241         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
13242                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
13243         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
13244                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
13245
13246         umount_client $MOUNT || error "umount failed"
13247         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
13248         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
13249         mount_client $MOUNT || error "failed to mount client"
13250
13251         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
13252         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
13253 }
13254 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
13255
13256 test_241_bio() {
13257         for LOOP in $(seq $1); do
13258                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
13259                 cancel_lru_locks osc
13260         done
13261 }
13262
13263 test_241_dio() {
13264         for LOOP in $(seq $1); do
13265                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
13266                                                 iflag=direct 2>/dev/null
13267         done
13268 }
13269
13270 test_241() {
13271         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13272         ls -la $DIR/$tfile
13273         cancel_lru_locks osc
13274         test_241_bio 1000 &
13275         PID=$!
13276         test_241_dio 1000
13277         wait $PID
13278 }
13279 run_test 241 "bio vs dio"
13280
13281 test_241b() {
13282         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13283         ls -la $DIR/$tfile
13284         test_241_dio 1000 &
13285         PID=$!
13286         test_241_dio 1000
13287         wait $PID
13288 }
13289 run_test 241b "dio vs dio"
13290
13291 test_242() {
13292         mkdir -p $DIR/$tdir
13293         touch $DIR/$tdir/$tfile
13294
13295         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
13296         do_facet mds1 lctl set_param fail_loc=0x105
13297         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
13298
13299         do_facet mds1 lctl set_param fail_loc=0
13300         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
13301 }
13302 run_test 242 "mdt_readpage failure should not cause directory unreadable"
13303
13304 test_243()
13305 {
13306         test_mkdir -p $DIR/$tdir
13307         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
13308 }
13309 run_test 243 "various group lock tests"
13310
13311 test_244()
13312 {
13313         test_mkdir -p $DIR/$tdir
13314         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
13315         sendfile_grouplock $DIR/$tdir/$tfile || \
13316                 error "sendfile+grouplock failed"
13317         rm -rf $DIR/$tdir
13318 }
13319 run_test 244 "sendfile with group lock tests"
13320
13321 test_245() {
13322         local flagname="multi_mod_rpcs"
13323         local connect_data_name="max_mod_rpcs"
13324         local out
13325
13326         # check if multiple modify RPCs flag is set
13327         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
13328                 grep "connect_flags:")
13329         echo "$out"
13330
13331         echo "$out" | grep -qw $flagname
13332         if [ $? -ne 0 ]; then
13333                 echo "connect flag $flagname is not set"
13334                 return
13335         fi
13336
13337         # check if multiple modify RPCs data is set
13338         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
13339         echo "$out"
13340
13341         echo "$out" | grep -qw $connect_data_name ||
13342                 error "import should have connect data $connect_data_name"
13343 }
13344 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
13345
13346 test_250() {
13347         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
13348          && skip "no 16TB file size limit on ZFS" && return
13349
13350         $SETSTRIPE -c 1 $DIR/$tfile
13351         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
13352         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
13353         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
13354         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
13355                 conv=notrunc,fsync && error "append succeeded"
13356         return 0
13357 }
13358 run_test 250 "Write above 16T limit"
13359
13360 test_251() {
13361         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
13362
13363         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
13364         #Skip once - writing the first stripe will succeed
13365         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13366         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
13367                 error "short write happened"
13368
13369         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13370         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
13371                 error "short read happened"
13372
13373         rm -f $DIR/$tfile
13374 }
13375 run_test 251 "Handling short read and write correctly"
13376
13377 test_252() {
13378         local tgt
13379         local dev
13380         local out
13381         local uuid
13382         local num
13383         local gen
13384
13385         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
13386              "$(facet_fstype mds1)" != "ldiskfs" ]; then
13387                 skip "can only run lr_reader on ldiskfs target"
13388                 return
13389         fi
13390
13391         # check lr_reader on OST0000
13392         tgt=ost1
13393         dev=$(facet_device $tgt)
13394         out=$(do_facet $tgt $LR_READER $dev)
13395         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13396         echo "$out"
13397         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
13398         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
13399                 error "Invalid uuid returned by $LR_READER on target $tgt"
13400         echo -e "uuid returned by $LR_READER is '$uuid'\n"
13401
13402         # check lr_reader -c on MDT0000
13403         tgt=mds1
13404         dev=$(facet_device $tgt)
13405         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
13406                 echo "$LR_READER does not support additional options"
13407                 return 0
13408         fi
13409         out=$(do_facet $tgt $LR_READER -c $dev)
13410         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13411         echo "$out"
13412         num=$(echo "$out" | grep -c "mdtlov")
13413         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
13414                 error "Invalid number of mdtlov clients returned by $LR_READER"
13415         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
13416
13417         # check lr_reader -cr on MDT0000
13418         out=$(do_facet $tgt $LR_READER -cr $dev)
13419         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13420         echo "$out"
13421         echo "$out" | grep -q "^reply_data:$" ||
13422                 error "$LR_READER should have returned 'reply_data' section"
13423         num=$(echo "$out" | grep -c "client_generation")
13424         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
13425 }
13426 run_test 252 "check lr_reader tool"
13427
13428
13429 cleanup_test_300() {
13430         trap 0
13431         umask $SAVE_UMASK
13432 }
13433 test_striped_dir() {
13434         local mdt_index=$1
13435         local stripe_count
13436         local stripe_index
13437
13438         mkdir -p $DIR/$tdir
13439
13440         SAVE_UMASK=$(umask)
13441         trap cleanup_test_300 RETURN EXIT
13442
13443         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
13444                                                 $DIR/$tdir/striped_dir ||
13445                 error "set striped dir error"
13446
13447         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
13448         [ "$mode" = "755" ] || error "expect 755 got $mode"
13449
13450         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
13451                 error "getdirstripe failed"
13452         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
13453         if [ "$stripe_count" != "2" ]; then
13454                 error "stripe_count is $stripe_count, expect 2"
13455         fi
13456
13457         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
13458         if [ "$stripe_index" != "$mdt_index" ]; then
13459                 error "stripe_index is $stripe_index, expect $mdt_index"
13460         fi
13461
13462         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13463                 error "nlink error after create striped dir"
13464
13465         mkdir $DIR/$tdir/striped_dir/a
13466         mkdir $DIR/$tdir/striped_dir/b
13467
13468         stat $DIR/$tdir/striped_dir/a ||
13469                 error "create dir under striped dir failed"
13470         stat $DIR/$tdir/striped_dir/b ||
13471                 error "create dir under striped dir failed"
13472
13473         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
13474                 error "nlink error after mkdir"
13475
13476         rmdir $DIR/$tdir/striped_dir/a
13477         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
13478                 error "nlink error after rmdir"
13479
13480         rmdir $DIR/$tdir/striped_dir/b
13481         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13482                 error "nlink error after rmdir"
13483
13484         chattr +i $DIR/$tdir/striped_dir
13485         createmany -o $DIR/$tdir/striped_dir/f 10 &&
13486                 error "immutable flags not working under striped dir!"
13487         chattr -i $DIR/$tdir/striped_dir
13488
13489         rmdir $DIR/$tdir/striped_dir ||
13490                 error "rmdir striped dir error"
13491
13492         cleanup_test_300
13493
13494         true
13495 }
13496
13497 test_300a() {
13498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13499         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13500
13501         test_striped_dir 0 || error "failed on striped dir on MDT0"
13502         test_striped_dir 1 || error "failed on striped dir on MDT0"
13503 }
13504 run_test 300a "basic striped dir sanity test"
13505
13506 test_300b() {
13507         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13508         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13509         local i
13510         local mtime1
13511         local mtime2
13512         local mtime3
13513
13514         test_mkdir $DIR/$tdir || error "mkdir fail"
13515         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13516                 error "set striped dir error"
13517         for ((i=0; i<10; i++)); do
13518                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
13519                 sleep 1
13520                 touch $DIR/$tdir/striped_dir/file_$i ||
13521                                         error "touch error $i"
13522                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
13523                 [ $mtime1 -eq $mtime2 ] &&
13524                         error "mtime not change after create"
13525                 sleep 1
13526                 rm -f $DIR/$tdir/striped_dir/file_$i ||
13527                                         error "unlink error $i"
13528                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
13529                 [ $mtime2 -eq $mtime3 ] &&
13530                         error "mtime did not change after unlink"
13531         done
13532         true
13533 }
13534 run_test 300b "check ctime/mtime for striped dir"
13535
13536 test_300c() {
13537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13538         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13539         local file_count
13540
13541         mkdir -p $DIR/$tdir
13542         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
13543                 error "set striped dir error"
13544
13545         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
13546                 error "chown striped dir failed"
13547
13548         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
13549                 error "create 5k files failed"
13550
13551         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
13552
13553         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
13554
13555         rm -rf $DIR/$tdir
13556 }
13557 run_test 300c "chown && check ls under striped directory"
13558
13559 test_300d() {
13560         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13561         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13562         local stripe_count
13563         local file
13564
13565         mkdir -p $DIR/$tdir
13566         $SETSTRIPE -c 2 $DIR/$tdir
13567
13568         #local striped directory
13569         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13570                 error "set striped dir error"
13571         createmany -o $DIR/$tdir/striped_dir/f 10 ||
13572                 error "create 10 files failed"
13573
13574         #remote striped directory
13575         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
13576                 error "set striped dir error"
13577         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
13578                 error "create 10 files failed"
13579
13580         for file in $(find $DIR/$tdir); do
13581                 stripe_count=$($GETSTRIPE -c $file)
13582                 [ $stripe_count -eq 2 ] ||
13583                         error "wrong stripe $stripe_count for $file"
13584         done
13585
13586         rm -rf $DIR/$tdir
13587 }
13588 run_test 300d "check default stripe under striped directory"
13589
13590 test_300e() {
13591         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13592                 skip "Need MDS version at least 2.7.55" && return
13593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13594         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13595         local stripe_count
13596         local file
13597
13598         mkdir -p $DIR/$tdir
13599
13600         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13601                 error "set striped dir error"
13602
13603         touch $DIR/$tdir/striped_dir/a
13604         touch $DIR/$tdir/striped_dir/b
13605         touch $DIR/$tdir/striped_dir/c
13606
13607         mkdir $DIR/$tdir/striped_dir/dir_a
13608         mkdir $DIR/$tdir/striped_dir/dir_b
13609         mkdir $DIR/$tdir/striped_dir/dir_c
13610
13611         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13612                 error "set striped dir under striped dir error"
13613
13614         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13615                 error "set striped dir under striped dir error"
13616
13617         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13618                 error "set striped dir under striped dir error"
13619
13620         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
13621                 error "rename dir under striped dir fails"
13622
13623         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
13624                 error "rename dir under different stripes fails"
13625
13626         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13627                 error "rename file under striped dir should succeed"
13628
13629         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
13630                 error "rename dir under striped dir should succeed"
13631
13632         rm -rf $DIR/$tdir
13633 }
13634 run_test 300e "check rename under striped directory"
13635
13636 test_300f() {
13637         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13638                 skip "Need MDS version at least 2.7.55" && return
13639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13640         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13641         local stripe_count
13642         local file
13643
13644         rm -rf $DIR/$tdir
13645         mkdir -p $DIR/$tdir
13646
13647         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13648                 error "set striped dir error"
13649
13650         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13651                 error "set striped dir error"
13652
13653         touch $DIR/$tdir/striped_dir/a
13654         mkdir $DIR/$tdir/striped_dir/dir_a
13655         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13656                 error "create striped dir under striped dir fails"
13657
13658         touch $DIR/$tdir/striped_dir1/b
13659         mkdir $DIR/$tdir/striped_dir1/dir_b
13660         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13661                 error "create striped dir under striped dir fails"
13662
13663         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
13664                 error "rename dir under different striped dir should fail"
13665
13666         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
13667                 error "rename striped dir under diff striped dir should fail"
13668
13669         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13670                 error "rename file under diff striped dirs fails"
13671
13672         rm -rf $DIR/$tdir
13673 }
13674 run_test 300f "check rename cross striped directory"
13675
13676 test_300_check_default_striped_dir()
13677 {
13678         local dirname=$1
13679         local default_count=$2
13680         local default_index=$3
13681         local stripe_count
13682         local stripe_index
13683         local dir_stripe_index
13684         local dir
13685
13686         echo "checking $dirname $default_count $default_index"
13687         $LFS setdirstripe -D -c $default_count -i $default_index \
13688                                 -t all_char $DIR/$tdir/$dirname ||
13689                 error "set default stripe on striped dir error"
13690         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
13691         [ $stripe_count -eq $default_count ] ||
13692                 error "expect $default_count get $stripe_count for $dirname"
13693
13694         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
13695         [ $stripe_index -eq $default_index ] ||
13696                 error "expect $default_index get $stripe_index for $dirname"
13697
13698         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
13699                                                 error "create dirs failed"
13700
13701         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
13702         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
13703         for dir in $(find $DIR/$tdir/$dirname/*); do
13704                 stripe_count=$($LFS getdirstripe -c $dir)
13705                 [ $stripe_count -eq $default_count ] ||
13706                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
13707                 error "stripe count $default_count != $stripe_count for $dir"
13708
13709                 stripe_index=$($LFS getdirstripe -i $dir)
13710                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
13711                         error "$stripe_index != $default_index for $dir"
13712
13713                 #check default stripe
13714                 stripe_count=$($LFS getdirstripe -D -c $dir)
13715                 [ $stripe_count -eq $default_count ] ||
13716                 error "default count $default_count != $stripe_count for $dir"
13717
13718                 stripe_index=$($LFS getdirstripe -D -i $dir)
13719                 [ $stripe_index -eq $default_index ] ||
13720                 error "default index $default_index != $stripe_index for $dir"
13721         done
13722         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
13723 }
13724
13725 test_300g() {
13726         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13727                 skip "Need MDS version at least 2.7.55" && return
13728         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13729         local dir
13730         local stripe_count
13731         local stripe_index
13732
13733         mkdir $DIR/$tdir
13734         mkdir $DIR/$tdir/normal_dir
13735
13736         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
13737         test_300_check_default_striped_dir normal_dir 1 0
13738         test_300_check_default_striped_dir normal_dir 2 1
13739         test_300_check_default_striped_dir normal_dir 2 -1
13740
13741         #delete default stripe information
13742         echo "delete default stripeEA"
13743         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
13744                 error "set default stripe on striped dir error"
13745
13746         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
13747         for dir in $(find $DIR/$tdir/normal_dir/*); do
13748                 stripe_count=$($LFS getdirstripe -c $dir)
13749                 [ $stripe_count -eq 0 ] ||
13750                         error "expect 1 get $stripe_count for $dir"
13751                 stripe_index=$($LFS getdirstripe -i $dir)
13752                 [ $stripe_index -eq 0 ] ||
13753                         error "expect 0 get $stripe_index for $dir"
13754         done
13755 }
13756 run_test 300g "check default striped directory for normal directory"
13757
13758 test_300h() {
13759         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13760                 skip "Need MDS version at least 2.7.55" && return
13761         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13762         local dir
13763         local stripe_count
13764
13765         mkdir $DIR/$tdir
13766         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13767                                         $DIR/$tdir/striped_dir ||
13768                 error "set striped dir error"
13769
13770         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
13771         test_300_check_default_striped_dir striped_dir 1 0
13772         test_300_check_default_striped_dir striped_dir 2 1
13773         test_300_check_default_striped_dir striped_dir 2 -1
13774
13775         #delete default stripe information
13776         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
13777                 error "set default stripe on striped dir error"
13778
13779         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13780         for dir in $(find $DIR/$tdir/striped_dir/*); do
13781                 stripe_count=$($LFS getdirstripe -c $dir)
13782                 [ $stripe_count -eq 0 ] ||
13783                         error "expect 1 get $stripe_count for $dir"
13784         done
13785 }
13786 run_test 300h "check default striped directory for striped directory"
13787
13788 test_300i() {
13789         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13790                 skip "Need MDS version at least 2.7.55" && return
13791         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13792         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13793         local stripe_count
13794         local file
13795
13796         mkdir $DIR/$tdir
13797
13798         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13799                 error "set striped dir error"
13800
13801         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13802                 error "create files under striped dir failed"
13803
13804         # unfortunately, we need to umount to clear dir layout cache for now
13805         # once we fully implement dir layout, we can drop this
13806         umount_client $MOUNT || error "umount failed"
13807         mount_client $MOUNT || error "mount failed"
13808
13809         #set the stripe to be unknown hash type
13810         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13811         $LCTL set_param fail_loc=0x1901
13812         for ((i = 0; i < 10; i++)); do
13813                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13814                         error "stat f-$i failed"
13815                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13816         done
13817
13818         touch $DIR/$tdir/striped_dir/f0 &&
13819                 error "create under striped dir with unknown hash should fail"
13820
13821         $LCTL set_param fail_loc=0
13822
13823         umount_client $MOUNT || error "umount failed"
13824         mount_client $MOUNT || error "mount failed"
13825
13826         return 0
13827 }
13828 run_test 300i "client handle unknown hash type striped directory"
13829
13830 test_300j() {
13831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13832         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13833                 skip "Need MDS version at least 2.7.55" && return
13834         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13835         local stripe_count
13836         local file
13837
13838         mkdir $DIR/$tdir
13839
13840         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
13841         $LCTL set_param fail_loc=0x1702
13842         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13843                 error "set striped dir error"
13844
13845         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13846                 error "create files under striped dir failed"
13847
13848         $LCTL set_param fail_loc=0
13849
13850         rm -rf $DIR/$tdir || error "unlink striped dir fails"
13851
13852         return 0
13853 }
13854 run_test 300j "test large update record"
13855
13856 test_300k() {
13857         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13858                 skip "Need MDS version at least 2.7.55" && return
13859         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13860         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13861         local stripe_count
13862         local file
13863
13864         mkdir $DIR/$tdir
13865
13866         #define OBD_FAIL_LARGE_STRIPE   0x1703
13867         $LCTL set_param fail_loc=0x1703
13868         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
13869                 error "set striped dir error"
13870         $LCTL set_param fail_loc=0
13871
13872         $LFS getdirstripe $DIR/$tdir/striped_dir ||
13873                 error "getstripeddir fails"
13874         rm -rf $DIR/$tdir/striped_dir ||
13875                 error "unlink striped dir fails"
13876
13877         return 0
13878 }
13879 run_test 300k "test large striped directory"
13880
13881 test_300l() {
13882         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13883                 skip "Need MDS version at least 2.7.55" && return
13884         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13885         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13886         local stripe_index
13887
13888         test_mkdir -p $DIR/$tdir/striped_dir
13889         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
13890                         error "chown $RUNAS_ID failed"
13891         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
13892                 error "set default striped dir failed"
13893
13894         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
13895         $LCTL set_param fail_loc=0x80000158
13896         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
13897
13898         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
13899         [ $stripe_index -eq 1 ] ||
13900                 error "expect 1 get $stripe_index for $dir"
13901 }
13902 run_test 300l "non-root user to create dir under striped dir with stale layout"
13903
13904 test_300m() {
13905         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13906                 skip "Need MDS version at least 2.7.55" && return
13907         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13908         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
13909
13910         mkdir -p $DIR/$tdir/striped_dir
13911         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
13912                 error "set default stripes dir error"
13913
13914         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
13915
13916         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
13917         [ $stripe_count -eq 0 ] ||
13918                         error "expect 0 get $stripe_count for a"
13919
13920         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
13921                 error "set default stripes dir error"
13922
13923         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
13924
13925         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
13926         [ $stripe_count -eq 0 ] ||
13927                         error "expect 0 get $stripe_count for b"
13928
13929         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
13930                 error "set default stripes dir error"
13931
13932         mkdir $DIR/$tdir/striped_dir/c &&
13933                 error "default stripe_index is invalid, mkdir c should fails"
13934
13935         rm -rf $DIR/$tdir || error "rmdir fails"
13936 }
13937 run_test 300m "setstriped directory on single MDT FS"
13938
13939 cleanup_300n() {
13940         local list=$(comma_list $(mdts_nodes))
13941
13942         trap 0
13943         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13944 }
13945
13946 test_300n() {
13947         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13948                 skip "Need MDS version at least 2.7.55" && return
13949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13950         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13951         local stripe_index
13952         local list=$(comma_list $(mdts_nodes))
13953
13954         trap cleanup_300n RETURN EXIT
13955         mkdir -p $DIR/$tdir
13956         chmod 777 $DIR/$tdir
13957         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
13958                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
13959                 error "create striped dir succeeds with gid=0"
13960
13961         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
13962         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
13963                 error "create striped dir fails with gid=-1"
13964
13965         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13966         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
13967                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
13968                 error "set default striped dir succeeds with gid=0"
13969
13970
13971         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
13972         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
13973                 error "set default striped dir fails with gid=-1"
13974
13975
13976         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
13977         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
13978                                         error "create test_dir fails"
13979         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
13980                                         error "create test_dir1 fails"
13981         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
13982                                         error "create test_dir2 fails"
13983         cleanup_300n
13984 }
13985 run_test 300n "non-root user to create dir under striped dir with default EA"
13986
13987 test_300o() {
13988         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13989                 skip "Need MDS version at least 2.7.55" && return
13990         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13991         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13992         local numfree1
13993         local numfree2
13994
13995         mkdir -p $DIR/$tdir
13996
13997         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
13998         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
13999         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
14000                 skip "not enough free inodes $numfree1 $numfree2"
14001                 return
14002         fi
14003
14004         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
14005         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
14006         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
14007                 skip "not enough free space $numfree1 $numfree2"
14008                 return
14009         fi
14010
14011         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
14012                 error "setdirstripe fails"
14013
14014         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
14015                 error "create dirs fails"
14016
14017         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
14018         ls $DIR/$tdir/striped_dir > /dev/null ||
14019                 error "ls striped dir fails"
14020         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
14021                 error "unlink big striped dir fails"
14022 }
14023 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
14024
14025 prepare_remote_file() {
14026         mkdir $DIR/$tdir/src_dir ||
14027                 error "create remote source failed"
14028
14029         cp /etc/hosts $DIR/$tdir/src_dir/a || error
14030         touch $DIR/$tdir/src_dir/a
14031
14032         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
14033                 error "create remote target dir failed"
14034
14035         touch $DIR/$tdir/tgt_dir/b
14036
14037         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
14038                 error "rename dir cross MDT failed!"
14039
14040         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
14041                 error "src_child still exists after rename"
14042
14043         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
14044                 error "missing file(a) after rename"
14045
14046         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
14047                 error "diff after rename"
14048 }
14049
14050 test_310a() {
14051         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
14052         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14053         local remote_file=$DIR/$tdir/tgt_dir/b
14054
14055         mkdir -p $DIR/$tdir
14056
14057         prepare_remote_file || error "prepare remote file failed"
14058
14059         #open-unlink file
14060         $OPENUNLINK $remote_file $remote_file || error
14061         $CHECKSTAT -a $remote_file || error
14062 }
14063 run_test 310a "open unlink remote file"
14064
14065 test_310b() {
14066         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
14067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14068         local remote_file=$DIR/$tdir/tgt_dir/b
14069
14070         mkdir -p $DIR/$tdir
14071
14072         prepare_remote_file || error "prepare remote file failed"
14073
14074         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14075         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
14076         $CHECKSTAT -t file $remote_file || error "check file failed"
14077 }
14078 run_test 310b "unlink remote file with multiple links while open"
14079
14080 test_310c() {
14081         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
14082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14083         local remote_file=$DIR/$tdir/tgt_dir/b
14084
14085         mkdir -p $DIR/$tdir
14086
14087         prepare_remote_file || error "prepare remote file failed"
14088
14089         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14090         multiop_bg_pause $remote_file O_uc ||
14091                         error "mulitop failed for remote file"
14092         MULTIPID=$!
14093         $MULTIOP $DIR/$tfile Ouc
14094         kill -USR1 $MULTIPID
14095         wait $MULTIPID
14096 }
14097 run_test 310c "open-unlink remote file with multiple links"
14098
14099 test_400a() { # LU-1606, was conf-sanity test_74
14100         local extra_flags=''
14101         local out=$TMP/$tfile
14102         local prefix=/usr/include/lustre
14103         local prog
14104
14105         if ! which $CC > /dev/null 2>&1; then
14106                 skip_env "$CC is not installed"
14107                 return 0
14108         fi
14109
14110         if ! [[ -d $prefix ]]; then
14111                 # Assume we're running in tree and fixup the include path.
14112                 extra_flags+=" -I$LUSTRE/../libcfs/include"
14113                 extra_flags+=" -I$LUSTRE/include"
14114                 extra_flags+=" -L$LUSTRE/utils"
14115         fi
14116
14117         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
14118                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
14119                         error "client api broken"
14120         done
14121 }
14122 run_test 400a "Lustre client api program can compile and link"
14123
14124 test_400b() { # LU-1606, LU-5011
14125         local header
14126         local out=$TMP/$tfile
14127         local prefix=/usr/include/lustre
14128
14129         # We use a hard coded prefix so that this test will not fail
14130         # when run in tree. There are headers in lustre/include/lustre/
14131         # that are not packaged (like lustre_idl.h) and have more
14132         # complicated include dependencies (like config.h and lnet/types.h).
14133         # Since this test about correct packaging we just skip them when
14134         # they don't exist (see below) rather than try to fixup cppflags.
14135
14136         if ! which $CC > /dev/null 2>&1; then
14137                 skip_env "$CC is not installed"
14138                 return 0
14139         fi
14140
14141         for header in $prefix/*.h; do
14142                 if ! [[ -f "$header" ]]; then
14143                         continue
14144                 fi
14145
14146                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
14147                         continue # liblustreapi.h is deprecated.
14148                 fi
14149
14150                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
14151                         error "cannot compile '$header'"
14152         done
14153 }
14154 run_test 400b "packaged headers can be compiled"
14155
14156 #
14157 # tests that do cleanup/setup should be run at the end
14158 #
14159
14160 test_900() {
14161         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14162         local ls
14163         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
14164         $LCTL set_param fail_loc=0x903
14165
14166         cancel_lru_locks MGC
14167
14168         FAIL_ON_ERROR=true cleanup
14169         FAIL_ON_ERROR=true setup
14170 }
14171 run_test 900 "umount should not race with any mgc requeue thread"
14172
14173 complete $SECONDS
14174 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
14175 check_and_cleanup_lustre
14176 if [ "$I_MOUNTED" != "yes" ]; then
14177         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
14178 fi
14179 exit_status