Whamcloud - gitweb
86ea6d05b95ce7a567d80143daa9ff5311b6bd21
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19 is_sles11()                                             # LU-4341
20 {
21         if [ -r /etc/SuSE-release ]
22         then
23                 local vers=$(grep VERSION /etc/SuSE-release | awk '{print $3}')
24                 local patchlev=$(grep PATCHLEVEL /etc/SuSE-release |
25                         awk '{ print $3 }')
26                 if [ $vers -eq 11 ] && [ $patchlev -ge 3 ]; then
27                         return 0
28                 fi
29         fi
30         return 1
31 }
32
33 if is_sles11; then                                      # LU-4341
34         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 170"
35 fi
36
37 SRCDIR=$(cd $(dirname $0); echo $PWD)
38 export PATH=$PATH:/sbin
39
40 TMP=${TMP:-/tmp}
41
42 CC=${CC:-cc}
43 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
44 CREATETEST=${CREATETEST:-createtest}
45 LFS=${LFS:-lfs}
46 LFIND=${LFIND:-"$LFS find"}
47 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
48 LCTL=${LCTL:-lctl}
49 OPENFILE=${OPENFILE:-openfile}
50 OPENUNLINK=${OPENUNLINK:-openunlink}
51 export MULTIOP=${MULTIOP:-multiop}
52 READS=${READS:-"reads"}
53 MUNLINK=${MUNLINK:-munlink}
54 SOCKETSERVER=${SOCKETSERVER:-socketserver}
55 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
56 MEMHOG=${MEMHOG:-memhog}
57 DIRECTIO=${DIRECTIO:-directio}
58 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
59 STRIPES_PER_OBJ=-1
60 CHECK_GRANT=${CHECK_GRANT:-"yes"}
61 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
62 export PARALLEL=${PARALLEL:-"no"}
63
64 export NAME=${NAME:-local}
65
66 SAVE_PWD=$PWD
67
68 CLEANUP=${CLEANUP:-:}
69 SETUP=${SETUP:-:}
70 TRACE=${TRACE:-""}
71 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
72 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
73 . $LUSTRE/tests/test-framework.sh
74 init_test_env $@
75 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
76 init_logging
77
78 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24D 27m 64b 68 71 77f 78 115 124b 300o"
79
80 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
81         # bug number for skipped test: LU-4536 LU-1957 LU-2805
82         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  65ic    180     184c"
83         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b 51ba"
84 fi
85
86 FAIL_ON_ERROR=false
87
88 cleanup() {
89         echo -n "cln.."
90         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
91         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
92 }
93 setup() {
94         echo -n "mnt.."
95         load_modules
96         setupall || exit 10
97         echo "done"
98 }
99
100 check_swap_layouts_support()
101 {
102         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
103                 { skip "Does not support layout lock."; return 0; }
104         return 1
105 }
106
107 check_and_setup_lustre
108
109 DIR=${DIR:-$MOUNT}
110 assert_DIR
111
112 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
113         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
114 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
115 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
116 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
117 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
118 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
119 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
120
121 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
122 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
123 rm -rf $DIR/[Rdfs][0-9]*
124
125 # $RUNAS_ID may get set incorrectly somewhere else
126 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
127
128 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
129
130 build_test_filter
131
132 if [ "${ONLY}" = "MOUNT" ] ; then
133         echo "Lustre is up, please go on"
134         exit
135 fi
136
137 echo "preparing for tests involving mounts"
138 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
139 touch $EXT2_DEV
140 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
141 echo # add a newline after mke2fs.
142
143 umask 077
144
145 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
146 lctl set_param debug=-1 2> /dev/null || true
147 test_0a() {
148         touch $DIR/$tfile
149         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
150         rm $DIR/$tfile
151         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
152 }
153 run_test 0a "touch; rm ====================="
154
155 test_0b() {
156         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
157         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
158 }
159 run_test 0b "chmod 0755 $DIR ============================="
160
161 test_0c() {
162         $LCTL get_param mdc.*.import | grep "state: FULL" ||
163                 error "import not FULL"
164         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
165                 error "bad target"
166 }
167 run_test 0c "check import proc ============================="
168
169 test_1() {
170         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
171         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
172         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
173         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
174         rmdir $DIR/$tdir/d2
175         rmdir $DIR/$tdir
176         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
177 }
178 run_test 1 "mkdir; remkdir; rmdir =============================="
179
180 test_2() {
181         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
182         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
183         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
184         rm -r $DIR/$tdir
185         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
186 }
187 run_test 2 "mkdir; touch; rmdir; check file ===================="
188
189 test_3() {
190         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
191         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
192         touch $DIR/$tdir/$tfile
193         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
194         rm -r $DIR/$tdir
195         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
196 }
197 run_test 3 "mkdir; touch; rmdir; check dir ====================="
198
199 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
200 test_4() {
201         local MDTIDX=1
202
203         test_mkdir $DIR/$tdir ||
204                 error "Create remote directory failed"
205
206         touch $DIR/$tdir/$tfile ||
207                 error "Create file under remote directory failed"
208
209         rmdir $DIR/$tdir &&
210                 error "Expect error removing in-use dir $DIR/$tdir"
211
212         test -d $DIR/$tdir || error "Remote directory disappeared"
213
214         rm -rf $DIR/$tdir || error "remove remote dir error"
215 }
216 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
217
218 test_5() {
219         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
220         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
221         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
222         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
223         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
224 }
225 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
226
227 test_6a() {
228         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
229         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
230         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
231                 error "$tfile does not have perm 0666 or UID $UID"
232         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
233         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
234                 error "$tfile should be 0666 and owned by UID $UID"
235 }
236 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
237
238 test_6c() {
239         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
240         touch $DIR/$tfile
241         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
242         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
243                 error "$tfile should be owned by UID $RUNAS_ID"
244         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
245         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
246                 error "$tfile should be owned by UID $RUNAS_ID"
247 }
248 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
249
250 test_6e() {
251         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
252         touch $DIR/$tfile
253         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
254         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
255                 error "$tfile should be owned by GID $UID"
256         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
257         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
258                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
259 }
260 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
261
262 test_6g() {
263         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
264         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
265         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
266         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
267         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
268         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
269         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
270                 error "$tdir/d/subdir should be GID $RUNAS_GID"
271 }
272 run_test 6g "Is new dir in sgid dir inheriting group?"
273
274 test_6h() { # bug 7331
275         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
276         touch $DIR/$tfile || error "touch failed"
277         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
278         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
279                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
280         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
281                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
282 }
283 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
284
285 test_7a() {
286         test_mkdir $DIR/$tdir
287         $MCREATE $DIR/$tdir/$tfile
288         chmod 0666 $DIR/$tdir/$tfile
289         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
290                 error "$tdir/$tfile should be mode 0666"
291 }
292 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
293
294 test_7b() {
295         if [ ! -d $DIR/$tdir ]; then
296                 test_mkdir $DIR/$tdir
297         fi
298         $MCREATE $DIR/$tdir/$tfile
299         echo -n foo > $DIR/$tdir/$tfile
300         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
301         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
302 }
303 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
304
305 test_8() {
306         test_mkdir $DIR/$tdir
307         touch $DIR/$tdir/$tfile
308         chmod 0666 $DIR/$tdir/$tfile
309         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
310                 error "$tfile mode not 0666"
311 }
312 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
313
314 test_9() {
315         test_mkdir $DIR/$tdir
316         test_mkdir $DIR/$tdir/d2
317         test_mkdir $DIR/$tdir/d2/d3
318         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
319 }
320 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
321
322 test_10() {
323         test_mkdir $DIR/$tdir
324         test_mkdir $DIR/$tdir/d2
325         touch $DIR/$tdir/d2/$tfile
326         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
327                 error "$tdir/d2/$tfile not a file"
328 }
329 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
330
331 test_11() {
332         test_mkdir $DIR/$tdir
333         test_mkdir $DIR/$tdir/d2
334         chmod 0666 $DIR/$tdir/d2
335         chmod 0705 $DIR/$tdir/d2
336         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
337                 error "$tdir/d2 mode not 0705"
338 }
339 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
340
341 test_12() {
342         test_mkdir $DIR/$tdir
343         touch $DIR/$tdir/$tfile
344         chmod 0666 $DIR/$tdir/$tfile
345         chmod 0654 $DIR/$tdir/$tfile
346         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
347                 error "$tdir/d2 mode not 0654"
348 }
349 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
350
351 test_13() {
352         test_mkdir $DIR/$tdir
353         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
354         >  $DIR/$tdir/$tfile
355         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
356                 error "$tdir/$tfile size not 0 after truncate"
357 }
358 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
359
360 test_14() {
361         test_mkdir $DIR/$tdir
362         touch $DIR/$tdir/$tfile
363         rm $DIR/$tdir/$tfile
364         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
365 }
366 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
367
368 test_15() {
369         test_mkdir $DIR/$tdir
370         touch $DIR/$tdir/$tfile
371         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
372         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
373                 error "$tdir/${tfile_2} not a file after rename"
374         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
375 }
376 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
377
378 test_16() {
379         test_mkdir $DIR/$tdir
380         touch $DIR/$tdir/$tfile
381         rm -rf $DIR/$tdir/$tfile
382         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
383 }
384 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
385
386 test_17a() {
387         test_mkdir -p $DIR/$tdir
388         touch $DIR/$tdir/$tfile
389         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
390         ls -l $DIR/$tdir
391         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
392                 error "$tdir/l-exist not a symlink"
393         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
394                 error "$tdir/l-exist not referencing a file"
395         rm -f $DIR/$tdir/l-exist
396         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
397 }
398 run_test 17a "symlinks: create, remove (real) =================="
399
400 test_17b() {
401         test_mkdir -p $DIR/$tdir
402         ln -s no-such-file $DIR/$tdir/l-dangle
403         ls -l $DIR/$tdir
404         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
405                 error "$tdir/l-dangle not referencing no-such-file"
406         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
407                 error "$tdir/l-dangle not referencing non-existent file"
408         rm -f $DIR/$tdir/l-dangle
409         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
410 }
411 run_test 17b "symlinks: create, remove (dangling) =============="
412
413 test_17c() { # bug 3440 - don't save failed open RPC for replay
414         test_mkdir -p $DIR/$tdir
415         ln -s foo $DIR/$tdir/$tfile
416         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
417 }
418 run_test 17c "symlinks: open dangling (should return error) ===="
419
420 test_17d() {
421         test_mkdir -p $DIR/$tdir
422         ln -s foo $DIR/$tdir/$tfile
423         touch $DIR/$tdir/$tfile || error "creating to new symlink"
424 }
425 run_test 17d "symlinks: create dangling ========================"
426
427 test_17e() {
428         test_mkdir -p $DIR/$tdir
429         local foo=$DIR/$tdir/$tfile
430         ln -s $foo $foo || error "create symlink failed"
431         ls -l $foo || error "ls -l failed"
432         ls $foo && error "ls not failed" || true
433 }
434 run_test 17e "symlinks: create recursive symlink (should return error) ===="
435
436 test_17f() {
437         test_mkdir -p $DIR/$tdir
438         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
439         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
440         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
441         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
442         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
443         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
444         ls -l  $DIR/$tdir
445 }
446 run_test 17f "symlinks: long and very long symlink name ========================"
447
448 # str_repeat(S, N) generate a string that is string S repeated N times
449 str_repeat() {
450         local s=$1
451         local n=$2
452         local ret=''
453         while [ $((n -= 1)) -ge 0 ]; do
454                 ret=$ret$s
455         done
456         echo $ret
457 }
458
459 # Long symlinks and LU-2241
460 test_17g() {
461         test_mkdir -p $DIR/$tdir
462         local TESTS="59 60 61 4094 4095"
463
464         # Fix for inode size boundary in 2.1.4
465         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
466                 TESTS="4094 4095"
467
468         # Patch not applied to 2.2 or 2.3 branches
469         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
470         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
471                 TESTS="4094 4095"
472
473         # skip long symlink name for rhel6.5.
474         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
475         grep -q '6.5' /etc/redhat-release &>/dev/null &&
476                 TESTS="59 60 61 4062 4063"
477
478         for i in $TESTS; do
479                 local SYMNAME=$(str_repeat 'x' $i)
480                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
481                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
482         done
483 }
484 run_test 17g "symlinks: really long symlink name and inode boundaries"
485
486 test_17h() { #bug 17378
487         remote_mds_nodsh && skip "remote MDS with nodsh" && return
488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
489         local mdt_idx
490         test_mkdir -p $DIR/$tdir
491         if [[ $MDSCOUNT -gt 1 ]]; then
492                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
493         else
494                 mdt_idx=0
495         fi
496         $SETSTRIPE -c -1 $DIR/$tdir
497 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
498         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
499         touch $DIR/$tdir/$tfile || true
500 }
501 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
502
503 test_17i() { #bug 20018
504         remote_mds_nodsh && skip "remote MDS with nodsh" && return
505         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
506         test_mkdir -c1 $DIR/$tdir
507         local foo=$DIR/$tdir/$tfile
508         local mdt_idx
509         if [[ $MDSCOUNT -gt 1 ]]; then
510                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
511         else
512                 mdt_idx=0
513         fi
514         ln -s $foo $foo || error "create symlink failed"
515 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
516         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
517         ls -l $foo && error "error not detected"
518         return 0
519 }
520 run_test 17i "don't panic on short symlink"
521
522 test_17k() { #bug 22301
523         [[ -z "$(which rsync 2>/dev/null)" ]] &&
524                 skip "no rsync command" && return 0
525         rsync --help | grep -q xattr ||
526                 skip_env "$(rsync --version | head -n1) does not support xattrs"
527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
528         test_mkdir -p $DIR/$tdir
529         test_mkdir -p $DIR/$tdir.new
530         touch $DIR/$tdir/$tfile
531         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
532         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
533                 error "rsync failed with xattrs enabled"
534 }
535 run_test 17k "symlinks: rsync with xattrs enabled ========================="
536
537 test_17l() { # LU-279
538         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
539                 skip "no getfattr command" && return 0
540         test_mkdir -p $DIR/$tdir
541         touch $DIR/$tdir/$tfile
542         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
543         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
544                 # -h to not follow symlinks. -m '' to list all the xattrs.
545                 # grep to remove first line: '# file: $path'.
546                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
547                 do
548                         lgetxattr_size_check $path $xattr ||
549                                 error "lgetxattr_size_check $path $xattr failed"
550                 done
551         done
552 }
553 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
554
555 # LU-1540
556 test_17m() {
557         local short_sym="0123456789"
558         local WDIR=$DIR/${tdir}m
559         local i
560
561         remote_mds_nodsh && skip "remote MDS with nodsh" && return
562         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
563         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
564                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
565
566         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
567                 skip "only for ldiskfs MDT" && return 0
568
569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
570
571         test_mkdir -p $WDIR
572         long_sym=$short_sym
573         # create a long symlink file
574         for ((i = 0; i < 4; ++i)); do
575                 long_sym=${long_sym}${long_sym}
576         done
577
578         echo "create 512 short and long symlink files under $WDIR"
579         for ((i = 0; i < 256; ++i)); do
580                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
581                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
582         done
583
584         echo "erase them"
585         rm -f $WDIR/*
586         sync
587         wait_delete_completed
588
589         echo "recreate the 512 symlink files with a shorter string"
590         for ((i = 0; i < 512; ++i)); do
591                 # rewrite the symlink file with a shorter string
592                 ln -sf ${long_sym} $WDIR/long-$i || error "long_sym failed"
593                 ln -sf ${short_sym} $WDIR/short-$i || error "short_sym failed"
594         done
595
596         local mds_index=$(($($LFS getstripe -M $WDIR) + 1))
597         local devname=$(mdsdevname $mds_index)
598
599         echo "stop and checking mds${mds_index}:"
600         # e2fsck should not return error
601         stop mds${mds_index}
602         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
603         rc=$?
604
605         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
606         df $MOUNT > /dev/null 2>&1
607         [ $rc -eq 0 ] ||
608                 error "e2fsck detected error for short/long symlink: rc=$rc"
609 }
610 run_test 17m "run e2fsck against MDT which contains short/long symlink"
611
612 check_fs_consistency_17n() {
613         local mdt_index
614         local rc=0
615
616         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
617         # so it only check MDT1/MDT2 instead of all of MDTs.
618         for mdt_index in 1 2; do
619                 local devname=$(mdsdevname $mdt_index)
620                 # e2fsck should not return error
621                 stop mds${mdt_index}
622                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
623                         rc=$((rc + $?))
624
625                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
626                         error "mount mds$mdt_index failed"
627                 df $MOUNT > /dev/null 2>&1
628         done
629         return $rc
630 }
631
632 test_17n() {
633         local i
634
635         remote_mds_nodsh && skip "remote MDS with nodsh" && return
636         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
637         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
638                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
639
640         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
641                 skip "only for ldiskfs MDT" && return 0
642
643         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
644
645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
646
647         test_mkdir $DIR/$tdir
648         for ((i=0; i<10; i++)); do
649                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
650                         error "create remote dir error $i"
651                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
652                         error "create files under remote dir failed $i"
653         done
654
655         check_fs_consistency_17n ||
656                 error "e2fsck report error after create files under remote dir"
657
658         for ((i = 0; i < 10; i++)); do
659                 rm -rf $DIR/$tdir/remote_dir_${i} ||
660                         error "destroy remote dir error $i"
661         done
662
663         check_fs_consistency_17n ||
664                 error "e2fsck report error after unlink files under remote dir"
665
666         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
667                 skip "lustre < 2.4.50 does not support migrate mv " && return
668
669         for ((i = 0; i < 10; i++)); do
670                 mkdir -p $DIR/$tdir/remote_dir_${i}
671                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
672                         error "create files under remote dir failed $i"
673                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
674                         error "migrate remote dir error $i"
675         done
676         check_fs_consistency_17n || error "e2fsck report error after migration"
677
678         for ((i = 0; i < 10; i++)); do
679                 rm -rf $DIR/$tdir/remote_dir_${i} ||
680                         error "destroy remote dir error $i"
681         done
682
683         check_fs_consistency_17n || error "e2fsck report error after unlink"
684 }
685 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
686
687 test_17o() {
688         remote_mds_nodsh && skip "remote MDS with nodsh" && return
689         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
690                 skip "Need MDS version at least 2.3.64" && return
691
692         local WDIR=$DIR/${tdir}o
693         local mdt_index
694         local rc=0
695
696         test_mkdir -p $WDIR
697         mdt_index=$($LFS getstripe -M $WDIR)
698         mdt_index=$((mdt_index+1))
699
700         touch $WDIR/$tfile
701
702         #fail mds will wait the failover finish then set
703         #following fail_loc to avoid interfer the recovery process.
704         fail mds${mdt_index}
705
706         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
707         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
708         ls -l $WDIR/$tfile && rc=1
709         do_facet mds${mdt_index} lctl set_param fail_loc=0
710         [[ $rc -ne 0 ]] && error "stat file should fail"
711         true
712 }
713 run_test 17o "stat file with incompat LMA feature"
714
715 test_18() {
716         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
717         ls $DIR || error "Failed to ls $DIR: $?"
718 }
719 run_test 18 "touch .../f ; ls ... =============================="
720
721 test_19a() {
722         touch $DIR/$tfile
723         ls -l $DIR
724         rm $DIR/$tfile
725         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
726 }
727 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
728
729 test_19b() {
730         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
731 }
732 run_test 19b "ls -l .../f19 (should return error) =============="
733
734 test_19c() {
735         [ $RUNAS_ID -eq $UID ] &&
736                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
737         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
738 }
739 run_test 19c "$RUNAS touch .../f19 (should return error) =="
740
741 test_19d() {
742         cat $DIR/f19 && error || true
743 }
744 run_test 19d "cat .../f19 (should return error) =============="
745
746 test_20() {
747         touch $DIR/$tfile
748         rm $DIR/$tfile
749         log "1 done"
750         touch $DIR/$tfile
751         rm $DIR/$tfile
752         log "2 done"
753         touch $DIR/$tfile
754         rm $DIR/$tfile
755         log "3 done"
756         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
757 }
758 run_test 20 "touch .../f ; ls -l ... ==========================="
759
760 test_21() {
761         test_mkdir -p $DIR/$tdir
762         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
763         ln -s dangle $DIR/$tdir/link
764         echo foo >> $DIR/$tdir/link
765         cat $DIR/$tdir/dangle
766         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
767         $CHECKSTAT -f -t file $DIR/$tdir/link ||
768                 error "$tdir/link not linked to a file"
769 }
770 run_test 21 "write to dangling link ============================"
771
772 test_22() {
773         WDIR=$DIR/$tdir
774         test_mkdir -p $DIR/$tdir
775         chown $RUNAS_ID:$RUNAS_GID $WDIR
776         (cd $WDIR || error "cd $WDIR failed";
777         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
778         $RUNAS tar xf -)
779         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
780         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
781         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
782 }
783 run_test 22 "unpack tar archive as non-root user ==============="
784
785 # was test_23
786 test_23a() {
787         test_mkdir -p $DIR/$tdir
788         local file=$DIR/$tdir/$tfile
789
790         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
791         openfile -f O_CREAT:O_EXCL $file &&
792                 error "$file recreate succeeded" || true
793 }
794 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
795
796 test_23b() { # bug 18988
797         test_mkdir -p $DIR/$tdir
798         local file=$DIR/$tdir/$tfile
799
800         rm -f $file
801         echo foo > $file || error "write filed"
802         echo bar >> $file || error "append filed"
803         $CHECKSTAT -s 8 $file || error "wrong size"
804         rm $file
805 }
806 run_test 23b "O_APPEND check =========================="
807
808 # rename sanity
809 test_24a() {
810         echo '-- same directory rename'
811         test_mkdir $DIR/$tdir
812         touch $DIR/$tdir/$tfile.1
813         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
814         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
815 }
816 run_test 24a "rename file to non-existent target"
817
818 test_24b() {
819         test_mkdir $DIR/$tdir
820         touch $DIR/$tdir/$tfile.{1,2}
821         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
822         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
823         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
824 }
825 run_test 24b "rename file to existing target"
826
827 test_24c() {
828         test_mkdir $DIR/$tdir
829         test_mkdir $DIR/$tdir/d$testnum.1
830         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
831         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
832         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
833 }
834 run_test 24c "rename directory to non-existent target"
835
836 test_24d() {
837         test_mkdir -c1 $DIR/$tdir
838         test_mkdir -c1 $DIR/$tdir/d$testnum.1
839         test_mkdir -c1 $DIR/$tdir/d$testnum.2
840         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
841         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
842         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
843 }
844 run_test 24d "rename directory to existing target"
845
846 test_24e() {
847         echo '-- cross directory renames --'
848         test_mkdir $DIR/R5a
849         test_mkdir $DIR/R5b
850         touch $DIR/R5a/f
851         mv $DIR/R5a/f $DIR/R5b/g
852         $CHECKSTAT -a $DIR/R5a/f || error
853         $CHECKSTAT -t file $DIR/R5b/g || error
854 }
855 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
856
857 test_24f() {
858         test_mkdir $DIR/R6a
859         test_mkdir $DIR/R6b
860         touch $DIR/R6a/f $DIR/R6b/g
861         mv $DIR/R6a/f $DIR/R6b/g
862         $CHECKSTAT -a $DIR/R6a/f || error
863         $CHECKSTAT -t file $DIR/R6b/g || error
864 }
865 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
866
867 test_24g() {
868         test_mkdir $DIR/R7a
869         test_mkdir $DIR/R7b
870         test_mkdir $DIR/R7a/d
871         mv $DIR/R7a/d $DIR/R7b/e
872         $CHECKSTAT -a $DIR/R7a/d || error
873         $CHECKSTAT -t dir $DIR/R7b/e || error
874 }
875 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
876
877 test_24h() {
878         test_mkdir -c1 $DIR/R8a
879         test_mkdir -c1 $DIR/R8b
880         test_mkdir -c1 $DIR/R8a/d
881         test_mkdir -c1 $DIR/R8b/e
882         mrename $DIR/R8a/d $DIR/R8b/e
883         $CHECKSTAT -a $DIR/R8a/d || error
884         $CHECKSTAT -t dir $DIR/R8b/e || error
885 }
886 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
887
888 test_24i() {
889         echo "-- rename error cases"
890         test_mkdir $DIR/R9
891         test_mkdir $DIR/R9/a
892         touch $DIR/R9/f
893         mrename $DIR/R9/f $DIR/R9/a
894         $CHECKSTAT -t file $DIR/R9/f || error
895         $CHECKSTAT -t dir  $DIR/R9/a || error
896         $CHECKSTAT -a $DIR/R9/a/f || error
897 }
898 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
899
900 test_24j() {
901         test_mkdir $DIR/R10
902         mrename $DIR/R10/f $DIR/R10/g
903         $CHECKSTAT -t dir $DIR/R10 || error
904         $CHECKSTAT -a $DIR/R10/f || error
905         $CHECKSTAT -a $DIR/R10/g || error
906 }
907 run_test 24j "source does not exist ============================"
908
909 test_24k() {
910         test_mkdir $DIR/R11a
911         test_mkdir $DIR/R11a/d
912         touch $DIR/R11a/f
913         mv $DIR/R11a/f $DIR/R11a/d
914         $CHECKSTAT -a $DIR/R11a/f || error
915         $CHECKSTAT -t file $DIR/R11a/d/f || error
916 }
917 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
918
919 # bug 2429 - rename foo foo foo creates invalid file
920 test_24l() {
921         f="$DIR/f24l"
922         $MULTIOP $f OcNs || error
923 }
924 run_test 24l "Renaming a file to itself ========================"
925
926 test_24m() {
927         f="$DIR/f24m"
928         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
929         # on ext3 this does not remove either the source or target files
930         # though the "expected" operation would be to remove the source
931         $CHECKSTAT -t file ${f} || error "${f} missing"
932         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
933 }
934 run_test 24m "Renaming a file to a hard link to itself ========="
935
936 test_24n() {
937     f="$DIR/f24n"
938     # this stats the old file after it was renamed, so it should fail
939     touch ${f}
940     $CHECKSTAT ${f}
941     mv ${f} ${f}.rename
942     $CHECKSTAT ${f}.rename
943     $CHECKSTAT -a ${f}
944 }
945 run_test 24n "Statting the old file after renaming (Posix rename 2)"
946
947 test_24o() {
948         test_mkdir -p $DIR/d24o
949         rename_many -s random -v -n 10 $DIR/d24o
950 }
951 run_test 24o "rename of files during htree split ==============="
952
953 test_24p() {
954         test_mkdir $DIR/R12a
955         test_mkdir $DIR/R12b
956         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
957         mrename $DIR/R12a $DIR/R12b
958         $CHECKSTAT -a $DIR/R12a || error
959         $CHECKSTAT -t dir $DIR/R12b || error
960         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
961         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
962 }
963 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
964
965 cleanup_multiop_pause() {
966         trap 0
967         kill -USR1 $MULTIPID
968 }
969
970 test_24q() {
971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
972         test_mkdir $DIR/R13a
973         test_mkdir $DIR/R13b
974         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
975         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
976         MULTIPID=$!
977
978         trap cleanup_multiop_pause EXIT
979         mrename $DIR/R13a $DIR/R13b
980         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
981         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
982         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
983         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
984         cleanup_multiop_pause
985         wait $MULTIPID || error "multiop close failed"
986 }
987 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
988
989 test_24r() { #bug 3789
990         test_mkdir $DIR/R14a
991         test_mkdir $DIR/R14a/b
992         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
993         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
994         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
995 }
996 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
997
998 test_24s() {
999         test_mkdir $DIR/R15a
1000         test_mkdir $DIR/R15a/b
1001         test_mkdir $DIR/R15a/b/c
1002         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1003         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1004         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1005 }
1006 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1007 test_24t() {
1008         test_mkdir $DIR/R16a
1009         test_mkdir $DIR/R16a/b
1010         test_mkdir $DIR/R16a/b/c
1011         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1012         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1013         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1014 }
1015 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1016
1017 test_24u() { # bug12192
1018         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1019         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1020 }
1021 run_test 24u "create stripe file"
1022
1023 page_size() {
1024         getconf PAGE_SIZE
1025 }
1026
1027 simple_cleanup_common() {
1028         trap 0
1029         rm -rf $DIR/$tdir
1030         wait_delete_completed
1031 }
1032
1033 max_pages_per_rpc() {
1034         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1035 }
1036
1037 test_24v() {
1038         local NRFILES=100000
1039         local FREE_INODES=$(mdt_free_inodes 0)
1040         [[ $FREE_INODES -lt $NRFILES ]] &&
1041                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1042                 return
1043
1044         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1045         trap simple_cleanup_common EXIT
1046
1047         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1048         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1049
1050         test_mkdir -p $DIR/$tdir
1051         createmany -m $DIR/$tdir/$tfile $NRFILES
1052
1053         cancel_lru_locks mdc
1054         lctl set_param mdc.*.stats clear
1055
1056         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1057
1058         # LU-5 large readdir
1059         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1060         #               8 bytes for name(filename is mostly 5 in this test) +
1061         #               8 bytes for luda_type
1062         # take into account of overhead in lu_dirpage header and end mark in
1063         # each page, plus one in RPC_NUM calculation.
1064         DIRENT_SIZE=48
1065         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1066         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1067         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1068                                 awk '/^mds_readpage/ {print $2}')
1069         [[ $mds_readpage -gt $RPC_NUM ]] &&
1070                 error "large readdir doesn't take effect"
1071
1072         simple_cleanup_common
1073 }
1074 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1075
1076 test_24w() { # bug21506
1077         SZ1=234852
1078         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1079         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1080         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1081         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1082         [[ "$SZ1" -eq "$SZ2" ]] ||
1083                 error "Error reading at the end of the file $tfile"
1084 }
1085 run_test 24w "Reading a file larger than 4Gb"
1086
1087 test_24x() {
1088         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1090         local MDTIDX=1
1091         local remote_dir=$DIR/$tdir/remote_dir
1092
1093         test_mkdir -p $DIR/$tdir
1094         $LFS mkdir -i $MDTIDX $remote_dir ||
1095                 error "create remote directory failed"
1096
1097         test_mkdir -p $DIR/$tdir/src_dir
1098         touch $DIR/$tdir/src_file
1099         test_mkdir -p $remote_dir/tgt_dir
1100         touch $remote_dir/tgt_file
1101
1102         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1103                 error "rename dir cross MDT failed!"
1104
1105         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1106                 error "rename file cross MDT failed!"
1107
1108         touch $DIR/$tdir/ln_file
1109         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1110                 error "ln file cross MDT failed"
1111
1112         rm -rf $DIR/$tdir || error "Can not delete directories"
1113 }
1114 run_test 24x "cross MDT rename/link"
1115
1116 test_24y() {
1117         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1119         local MDTIDX=1
1120         local remote_dir=$DIR/$tdir/remote_dir
1121
1122         test_mkdir -p $DIR/$tdir
1123         $LFS mkdir -i $MDTIDX $remote_dir ||
1124                    error "create remote directory failed"
1125
1126         test_mkdir -p $remote_dir/src_dir
1127         touch $remote_dir/src_file
1128         test_mkdir -p $remote_dir/tgt_dir
1129         touch $remote_dir/tgt_file
1130
1131         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1132                 error "rename subdir in the same remote dir failed!"
1133
1134         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1135                 error "rename files in the same remote dir failed!"
1136
1137         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1138                 error "link files in the same remote dir failed!"
1139
1140         rm -rf $DIR/$tdir || error "Can not delete directories"
1141 }
1142 run_test 24y "rename/link on the same dir should succeed"
1143
1144 test_24A() { # LU-3182
1145         local NFILES=5000
1146
1147         rm -rf $DIR/$tdir
1148         test_mkdir -p $DIR/$tdir
1149         createmany -m $DIR/$tdir/$tfile $NFILES
1150         local t=$(ls $DIR/$tdir | wc -l)
1151         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1152         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1153         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1154                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1155         fi
1156
1157         rm -rf $DIR/$tdir || error "Can not delete directories"
1158 }
1159 run_test 24A "readdir() returns correct number of entries."
1160
1161 test_24B() { # LU-4805
1162         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1163         local count
1164
1165         test_mkdir $DIR/$tdir
1166         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1167                 error "create striped dir failed"
1168
1169         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1170         [ $count -eq 2 ] || error "Expected 2, got $count"
1171
1172         touch $DIR/$tdir/striped_dir/a
1173
1174         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1175         [ $count -eq 3 ] || error "Expected 3, got $count"
1176
1177         touch $DIR/$tdir/striped_dir/.f
1178
1179         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1180         [ $count -eq 4 ] || error "Expected 4, got $count"
1181
1182         rm -rf $DIR/$tdir || error "Can not delete directories"
1183 }
1184 run_test 24B "readdir for striped dir return correct number of entries"
1185
1186 test_24C() {
1187         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1188
1189         mkdir $DIR/$tdir
1190         mkdir $DIR/$tdir/d0
1191         mkdir $DIR/$tdir/d1
1192
1193         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1194                 error "create striped dir failed"
1195
1196         cd $DIR/$tdir/d0/striped_dir
1197
1198         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1199         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1200         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1201
1202         [ "$d0_ino" = "$parent_ino" ] ||
1203                 error ".. wrong, expect $d0_ino, get $parent_ino"
1204
1205         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1206                 error "mv striped dir failed"
1207
1208         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1209
1210         [ "$d1_ino" = "$parent_ino" ] ||
1211                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1212 }
1213 run_test 24C "check .. in striped dir"
1214
1215 test_24D() { # LU-6101
1216         local NFILES=50000
1217
1218         rm -rf $DIR/$tdir
1219         mkdir -p $DIR/$tdir
1220         createmany -m $DIR/$tdir/$tfile $NFILES
1221         local t=$(ls $DIR/$tdir | wc -l)
1222         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1223         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1224         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1225                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1226         fi
1227
1228         rm -rf $DIR/$tdir || error "Can not delete directories"
1229 }
1230 run_test 24D "readdir() returns correct number of entries after cursor reload"
1231
1232 test_24E() {
1233         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1234         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1235
1236         mkdir -p $DIR/$tdir
1237         mkdir $DIR/$tdir/src_dir
1238         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1239                 error "create remote source failed"
1240
1241         touch $DIR/$tdir/src_dir/src_child/a
1242
1243         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1244                 error "create remote target dir failed"
1245
1246         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1247                 error "create remote target child failed"
1248
1249         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1250                 error "rename dir cross MDT failed!"
1251
1252         find $DIR/$tdir
1253
1254         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1255                 error "src_child still exists after rename"
1256
1257         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1258                 error "missing file(a) after rename"
1259
1260         rm -rf $DIR/$tdir || error "Can not delete directories"
1261 }
1262 run_test 24E "cross MDT rename/link"
1263
1264 test_25a() {
1265         echo '== symlink sanity ============================================='
1266
1267         test_mkdir $DIR/d25
1268         ln -s d25 $DIR/s25
1269         touch $DIR/s25/foo || error
1270 }
1271 run_test 25a "create file in symlinked directory ==============="
1272
1273 test_25b() {
1274         [ ! -d $DIR/d25 ] && test_25a
1275         $CHECKSTAT -t file $DIR/s25/foo || error
1276 }
1277 run_test 25b "lookup file in symlinked directory ==============="
1278
1279 test_26a() {
1280         test_mkdir $DIR/d26
1281         test_mkdir $DIR/d26/d26-2
1282         ln -s d26/d26-2 $DIR/s26
1283         touch $DIR/s26/foo || error
1284 }
1285 run_test 26a "multiple component symlink ======================="
1286
1287 test_26b() {
1288         test_mkdir -p $DIR/d26b/d26-2
1289         ln -s d26b/d26-2/foo $DIR/s26-2
1290         touch $DIR/s26-2 || error
1291 }
1292 run_test 26b "multiple component symlink at end of lookup ======"
1293
1294 test_26c() {
1295         test_mkdir $DIR/d26.2
1296         touch $DIR/d26.2/foo
1297         ln -s d26.2 $DIR/s26.2-1
1298         ln -s s26.2-1 $DIR/s26.2-2
1299         ln -s s26.2-2 $DIR/s26.2-3
1300         chmod 0666 $DIR/s26.2-3/foo
1301 }
1302 run_test 26c "chain of symlinks ================================"
1303
1304 # recursive symlinks (bug 439)
1305 test_26d() {
1306         ln -s d26-3/foo $DIR/d26-3
1307 }
1308 run_test 26d "create multiple component recursive symlink ======"
1309
1310 test_26e() {
1311         [ ! -h $DIR/d26-3 ] && test_26d
1312         rm $DIR/d26-3
1313 }
1314 run_test 26e "unlink multiple component recursive symlink ======"
1315
1316 # recursive symlinks (bug 7022)
1317 test_26f() {
1318         test_mkdir -p $DIR/$tdir
1319         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1320         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1321         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1322         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1323         cd $tfile                || error "cd $tfile failed"
1324         ln -s .. dotdot          || error "ln dotdot failed"
1325         ln -s dotdot/lndir lndir || error "ln lndir failed"
1326         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1327         output=`ls $tfile/$tfile/lndir/bar1`
1328         [ "$output" = bar1 ] && error "unexpected output"
1329         rm -r $tfile             || error "rm $tfile failed"
1330         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1331 }
1332 run_test 26f "rm -r of a directory which has recursive symlink ="
1333
1334 test_27a() {
1335         echo '== stripe sanity =============================================='
1336         test_mkdir -p $DIR/d27 || error "mkdir failed"
1337         $GETSTRIPE $DIR/d27
1338         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1339         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1340         log "== test_27a: write to one stripe file ========================="
1341         cp /etc/hosts $DIR/d27/f0 || error
1342 }
1343 run_test 27a "one stripe file =================================="
1344
1345 test_27b() {
1346         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1347         test_mkdir -p $DIR/d27
1348         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1349         $GETSTRIPE -c $DIR/d27/f01
1350         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1351                 error "two-stripe file doesn't have two stripes"
1352
1353         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1354 }
1355 run_test 27b "create and write to two stripe file"
1356
1357 test_27d() {
1358         test_mkdir -p $DIR/d27
1359         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1360         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1361         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1362 }
1363 run_test 27d "create file with default settings ================"
1364
1365 test_27e() {
1366         # LU-5839 adds check for existed layout before setting it
1367         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1368                 skip "Need MDS version at least 2.7.56" && return
1369         test_mkdir -p $DIR/d27
1370         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1371         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1372         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1373 }
1374 run_test 27e "setstripe existing file (should return error) ======"
1375
1376 test_27f() {
1377         test_mkdir $DIR/$tdir
1378         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1379                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1380         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1381                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1382         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1383         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1384 }
1385 run_test 27f "setstripe with bad stripe size (should return error)"
1386
1387 test_27g() {
1388         test_mkdir -p $DIR/d27
1389         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1390         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1391                 error "$DIR/d27/fnone has object"
1392 }
1393 run_test 27g "$GETSTRIPE with no objects"
1394
1395 test_27i() {
1396         test_mkdir $DIR/$tdir
1397         touch $DIR/$tdir/$tfile || error "touch failed"
1398         [[ $($GETSTRIPE -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1399                 error "missing objects"
1400 }
1401 run_test 27i "$GETSTRIPE with some objects"
1402
1403 test_27j() {
1404         test_mkdir -p $DIR/d27
1405         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1406 }
1407 run_test 27j "setstripe with bad stripe offset (should return error)"
1408
1409 test_27k() { # bug 2844
1410         test_mkdir -p $DIR/d27
1411         FILE=$DIR/d27/f27k
1412         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1413         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1414         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1415         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1416         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1417         dd if=/dev/zero of=$FILE bs=4k count=1
1418         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1419         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1420 }
1421 run_test 27k "limit i_blksize for broken user apps ============="
1422
1423 test_27l() {
1424         test_mkdir -p $DIR/d27
1425         mcreate $DIR/f27l || error "creating file"
1426         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1427                 error "setstripe should have failed" || true
1428 }
1429 run_test 27l "check setstripe permissions (should return error)"
1430
1431 test_27m() {
1432         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1433                 return
1434         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1435                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1436                 return
1437         fi
1438         trap simple_cleanup_common EXIT
1439         test_mkdir -p $DIR/$tdir
1440         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1441         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1442                 error "dd should fill OST0"
1443         i=2
1444         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1445                 i=$((i + 1))
1446                 [ $i -gt 256 ] && break
1447         done
1448         i=$((i + 1))
1449         touch $DIR/$tdir/f27m_$i
1450         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1451                 error "OST0 was full but new created file still use it"
1452         i=$((i + 1))
1453         touch $DIR/$tdir/f27m_$i
1454         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1455                 error "OST0 was full but new created file still use it"
1456         simple_cleanup_common
1457 }
1458 run_test 27m "create file while OST0 was full =================="
1459
1460 sleep_maxage() {
1461         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1462         sleep $DELAY
1463 }
1464
1465 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1466 # if the OST isn't full anymore.
1467 reset_enospc() {
1468         local OSTIDX=${1:-""}
1469
1470         local list=$(comma_list $(osts_nodes))
1471         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1472
1473         do_nodes $list lctl set_param fail_loc=0
1474         sync    # initiate all OST_DESTROYs from MDS to OST
1475         sleep_maxage
1476 }
1477
1478 exhaust_precreations() {
1479         local OSTIDX=$1
1480         local FAILLOC=$2
1481         local FAILIDX=${3:-$OSTIDX}
1482         local ofacet=ost$((OSTIDX + 1))
1483
1484         test_mkdir -p -c1 $DIR/$tdir
1485         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1486         local mfacet=mds$((mdtidx + 1))
1487         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1488
1489         local OST=$(ostname_from_index $OSTIDX)
1490
1491         # on the mdt's osc
1492         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1493         local last_id=$(do_facet $mfacet lctl get_param -n \
1494                         osc.$mdtosc_proc1.prealloc_last_id)
1495         local next_id=$(do_facet $mfacet lctl get_param -n \
1496                         osc.$mdtosc_proc1.prealloc_next_id)
1497
1498         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1499         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1500
1501         test_mkdir -p $DIR/$tdir/${OST}
1502         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1503 #define OBD_FAIL_OST_ENOSPC              0x215
1504         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1505         echo "Creating to objid $last_id on ost $OST..."
1506         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1507         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1508         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1509         sleep_maxage
1510 }
1511
1512 exhaust_all_precreations() {
1513         local i
1514         for (( i=0; i < OSTCOUNT; i++ )) ; do
1515                 exhaust_precreations $i $1 -1
1516         done
1517 }
1518
1519 test_27n() {
1520         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1522         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1523         remote_ost_nodsh && skip "remote OST with nodsh" && return
1524
1525         reset_enospc
1526         rm -f $DIR/$tdir/$tfile
1527         exhaust_precreations 0 0x80000215
1528         $SETSTRIPE -c -1 $DIR/$tdir
1529         touch $DIR/$tdir/$tfile || error
1530         $GETSTRIPE $DIR/$tdir/$tfile
1531         reset_enospc
1532 }
1533 run_test 27n "create file with some full OSTs =================="
1534
1535 test_27o() {
1536         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1538         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1539         remote_ost_nodsh && skip "remote OST with nodsh" && return
1540
1541         reset_enospc
1542         rm -f $DIR/$tdir/$tfile
1543         exhaust_all_precreations 0x215
1544
1545         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1546
1547         reset_enospc
1548         rm -rf $DIR/$tdir/*
1549 }
1550 run_test 27o "create file with all full OSTs (should error) ===="
1551
1552 test_27p() {
1553         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1555         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1556         remote_ost_nodsh && skip "remote OST with nodsh" && return
1557
1558         reset_enospc
1559         rm -f $DIR/$tdir/$tfile
1560         test_mkdir -p $DIR/$tdir
1561
1562         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1563         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1564         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1565
1566         exhaust_precreations 0 0x80000215
1567         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1568         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1569         $GETSTRIPE $DIR/$tdir/$tfile
1570
1571         reset_enospc
1572 }
1573 run_test 27p "append to a truncated file with some full OSTs ==="
1574
1575 test_27q() {
1576         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1578         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1579         remote_ost_nodsh && skip "remote OST with nodsh" && return
1580
1581         reset_enospc
1582         rm -f $DIR/$tdir/$tfile
1583
1584         test_mkdir -p $DIR/$tdir
1585         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1586         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1587         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1588
1589         exhaust_all_precreations 0x215
1590
1591         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1592         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1593
1594         reset_enospc
1595 }
1596 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1597
1598 test_27r() {
1599         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1601         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1602         remote_ost_nodsh && skip "remote OST with nodsh" && return
1603
1604         reset_enospc
1605         rm -f $DIR/$tdir/$tfile
1606         exhaust_precreations 0 0x80000215
1607
1608         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1609
1610         reset_enospc
1611 }
1612 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1613
1614 test_27s() { # bug 10725
1615         test_mkdir -p $DIR/$tdir
1616         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1617         local stripe_count=0
1618         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1619         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1620                 error "stripe width >= 2^32 succeeded" || true
1621
1622 }
1623 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1624
1625 test_27t() { # bug 10864
1626         WDIR=$(pwd)
1627         WLFS=$(which lfs)
1628         cd $DIR
1629         touch $tfile
1630         $WLFS getstripe $tfile
1631         cd $WDIR
1632 }
1633 run_test 27t "check that utils parse path correctly"
1634
1635 test_27u() { # bug 4900
1636         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1637         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1638         local index
1639         local list=$(comma_list $(mdts_nodes))
1640
1641 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1642         do_nodes $list $LCTL set_param fail_loc=0x139
1643         test_mkdir -p $DIR/$tdir
1644         rm -rf $DIR/$tdir/*
1645         createmany -o $DIR/$tdir/t- 1000
1646         do_nodes $list $LCTL set_param fail_loc=0
1647
1648         TLOG=$DIR/$tfile.getstripe
1649         $GETSTRIPE $DIR/$tdir > $TLOG
1650         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1651         unlinkmany $DIR/$tdir/t- 1000
1652         [[ $OBJS -gt 0 ]] &&
1653                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1654 }
1655 run_test 27u "skip object creation on OSC w/o objects =========="
1656
1657 test_27v() { # bug 4900
1658         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1660         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1661         remote_ost_nodsh && skip "remote OST with nodsh" && return
1662
1663         exhaust_all_precreations 0x215
1664         reset_enospc
1665
1666         test_mkdir -p $DIR/$tdir
1667         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1668
1669         touch $DIR/$tdir/$tfile
1670         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1671         # all except ost1
1672         for (( i=1; i < OSTCOUNT; i++ )); do
1673                 do_facet ost$i lctl set_param fail_loc=0x705
1674         done
1675         local START=`date +%s`
1676         createmany -o $DIR/$tdir/$tfile 32
1677
1678         local FINISH=`date +%s`
1679         local TIMEOUT=`lctl get_param -n timeout`
1680         local PROCESS=$((FINISH - START))
1681         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1682                error "$FINISH - $START >= $TIMEOUT / 2"
1683         sleep $((TIMEOUT / 2 - PROCESS))
1684         reset_enospc
1685 }
1686 run_test 27v "skip object creation on slow OST ================="
1687
1688 test_27w() { # bug 10997
1689         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1690         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1691         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1692                 error "stripe size $size != 65536" || true
1693         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1694                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1695 }
1696 run_test 27w "check $SETSTRIPE -S option"
1697
1698 test_27wa() {
1699         [[ $OSTCOUNT -lt 2 ]] &&
1700                 skip_env "skipping multiple stripe count/offset test" && return
1701
1702         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1703         for i in $(seq 1 $OSTCOUNT); do
1704                 offset=$((i - 1))
1705                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1706                         error "setstripe -c $i -i $offset failed"
1707                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1708                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1709                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1710                 [ $index -ne $offset ] &&
1711                         error "stripe offset $index != $offset" || true
1712         done
1713 }
1714 run_test 27wa "check $SETSTRIPE -c -i options"
1715
1716 test_27x() {
1717         remote_ost_nodsh && skip "remote OST with nodsh" && return
1718         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1720         OFFSET=$(($OSTCOUNT - 1))
1721         OSTIDX=0
1722         local OST=$(ostname_from_index $OSTIDX)
1723
1724         test_mkdir -p $DIR/$tdir
1725         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1726         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1727         sleep_maxage
1728         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1729         for i in `seq 0 $OFFSET`; do
1730                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1731                 error "OST0 was degraded but new created file still use it"
1732         done
1733         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1734 }
1735 run_test 27x "create files while OST0 is degraded"
1736
1737 test_27y() {
1738         [[ $OSTCOUNT -lt 2 ]] &&
1739                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1740         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1741         remote_ost_nodsh && skip "remote OST with nodsh" && return
1742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1743
1744         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1745         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1746             osc.$mdtosc.prealloc_last_id)
1747         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1748             osc.$mdtosc.prealloc_next_id)
1749         local fcount=$((last_id - next_id))
1750         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1751         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1752
1753         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1754                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1755         local OST_DEACTIVE_IDX=-1
1756         local OSC
1757         local OSTIDX
1758         local OST
1759
1760         for OSC in $MDS_OSCS; do
1761                 OST=$(osc_to_ost $OSC)
1762                 OSTIDX=$(index_from_ostuuid $OST)
1763                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1764                         OST_DEACTIVE_IDX=$OSTIDX
1765                 fi
1766                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1767                         echo $OSC "is Deactivated:"
1768                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1769                 fi
1770         done
1771
1772         OSTIDX=$(index_from_ostuuid $OST)
1773         test_mkdir -p $DIR/$tdir
1774         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1775
1776         for OSC in $MDS_OSCS; do
1777                 OST=$(osc_to_ost $OSC)
1778                 OSTIDX=$(index_from_ostuuid $OST)
1779                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1780                         echo $OST "is degraded:"
1781                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1782                                                 obdfilter.$OST.degraded=1
1783                 fi
1784         done
1785
1786         sleep_maxage
1787         createmany -o $DIR/$tdir/$tfile $fcount
1788
1789         for OSC in $MDS_OSCS; do
1790                 OST=$(osc_to_ost $OSC)
1791                 OSTIDX=$(index_from_ostuuid $OST)
1792                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1793                         echo $OST "is recovered from degraded:"
1794                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1795                                                 obdfilter.$OST.degraded=0
1796                 else
1797                         do_facet $SINGLEMDS lctl --device %$OSC activate
1798                 fi
1799         done
1800
1801         # all osp devices get activated, hence -1 stripe count restored
1802         local stripecnt=0
1803
1804         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1805         # devices get activated.
1806         sleep_maxage
1807         $SETSTRIPE -c -1 $DIR/$tfile
1808         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1809         rm -f $DIR/$tfile
1810         [ $stripecnt -ne $OSTCOUNT ] &&
1811                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1812         return 0
1813 }
1814 run_test 27y "create files while OST0 is degraded and the rest inactive"
1815
1816 check_seq_oid()
1817 {
1818         log "check file $1"
1819
1820         lmm_count=$($GETSTRIPE -c $1)
1821         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1822         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1823
1824         local old_ifs="$IFS"
1825         IFS=$'[:]'
1826         fid=($($LFS path2fid $1))
1827         IFS="$old_ifs"
1828
1829         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1830         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1831
1832         # compare lmm_seq and lu_fid->f_seq
1833         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1834         # compare lmm_object_id and lu_fid->oid
1835         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1836
1837         # check the trusted.fid attribute of the OST objects of the file
1838         local have_obdidx=false
1839         local stripe_nr=0
1840         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1841                 # skip lines up to and including "obdidx"
1842                 [ -z "$obdidx" ] && break
1843                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1844                 $have_obdidx || continue
1845
1846                 local ost=$((obdidx + 1))
1847                 local dev=$(ostdevname $ost)
1848                 local oid_hex
1849
1850                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1851
1852                 seq=$(echo $seq | sed -e "s/^0x//g")
1853                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1854                         oid_hex=$(echo $oid)
1855                 else
1856                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1857                 fi
1858                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1859
1860                 local ff
1861                 #
1862                 # Don't unmount/remount the OSTs if we don't need to do that.
1863                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1864                 # update too, until that use mount/ll_decode_filter_fid/mount.
1865                 # Re-enable when debugfs will understand new filter_fid.
1866                 #
1867                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1868                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1869                                 $dev 2>/dev/null" | grep "parent=")
1870                 else
1871                         stop ost$ost
1872                         mount_fstype ost$ost
1873                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1874                                 $(facet_mntpt ost$ost)/$obj_file)
1875                         unmount_fstype ost$ost
1876                         start ost$ost $dev $OST_MOUNT_OPTS
1877                         clients_up
1878                 fi
1879
1880                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1881
1882                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1883
1884                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1885                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1886                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1887                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1888                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1889                 local ff_pstripe
1890                 if echo $ff_parent | grep -q 'stripe='; then
1891                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1892                 else
1893                         #
1894                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1895                         # into f_ver in this case.  See the comment on
1896                         # ff_parent.
1897                         #
1898                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1899                                 sed -e 's/\]//')
1900                 fi
1901
1902                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1903                 [ $ff_pseq = $lmm_seq ] ||
1904                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1905                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1906                 [ $ff_poid = $lmm_oid ] ||
1907                         error "FF parent OID $ff_poid != $lmm_oid"
1908                 (($ff_pstripe == $stripe_nr)) ||
1909                         error "FF stripe $ff_pstripe != $stripe_nr"
1910
1911                 stripe_nr=$((stripe_nr + 1))
1912         done
1913 }
1914
1915 test_27z() {
1916         remote_ost_nodsh && skip "remote OST with nodsh" && return
1917         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1918         test_mkdir -p $DIR/$tdir
1919
1920         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1921                 { error "setstripe -c -1 failed"; return 1; }
1922         # We need to send a write to every object to get parent FID info set.
1923         # This _should_ also work for setattr, but does not currently.
1924         # touch $DIR/$tdir/$tfile-1 ||
1925         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1926                 { error "dd $tfile-1 failed"; return 2; }
1927         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1928                 { error "setstripe -c -1 failed"; return 3; }
1929         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1930                 { error "dd $tfile-2 failed"; return 4; }
1931
1932         # make sure write RPCs have been sent to OSTs
1933         sync; sleep 5; sync
1934
1935         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1936         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1937 }
1938 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1939
1940 test_27A() { # b=19102
1941         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1942         local restore_size=$($GETSTRIPE -S $MOUNT)
1943         local restore_count=$($GETSTRIPE -c $MOUNT)
1944         local restore_offset=$($GETSTRIPE -i $MOUNT)
1945         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1946         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1947                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1948         local default_size=$($GETSTRIPE -S $MOUNT)
1949         local default_offset=$($GETSTRIPE -i $MOUNT)
1950         local dsize=$((1024 * 1024))
1951         [ $default_size -eq $dsize ] ||
1952                 error "stripe size $default_size != $dsize"
1953         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1954         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1955 }
1956 run_test 27A "check filesystem-wide default LOV EA values"
1957
1958 test_27B() { # LU-2523
1959         test_mkdir -p $DIR/$tdir
1960         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1961         touch $DIR/$tdir/f0
1962         # open f1 with O_LOV_DELAY_CREATE
1963         # rename f0 onto f1
1964         # call setstripe ioctl on open file descriptor for f1
1965         # close
1966         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1967                 $DIR/$tdir/f0
1968
1969         rm -f $DIR/$tdir/f1
1970         # open f1 with O_LOV_DELAY_CREATE
1971         # unlink f1
1972         # call setstripe ioctl on open file descriptor for f1
1973         # close
1974         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1975
1976         # Allow multiop to fail in imitation of NFS's busted semantics.
1977         true
1978 }
1979 run_test 27B "call setstripe on open unlinked file/rename victim"
1980
1981 test_27C() { #LU-2871
1982         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1983
1984         declare -a ost_idx
1985         local index
1986         local found
1987         local i
1988         local j
1989
1990         test_mkdir -p $DIR/$tdir
1991         cd $DIR/$tdir
1992         for i in $(seq 0 $((OSTCOUNT - 1))); do
1993                 # set stripe across all OSTs starting from OST$i
1994                 $SETSTRIPE -i $i -c -1 $tfile$i
1995                 # get striping information
1996                 ost_idx=($($GETSTRIPE $tfile$i |
1997                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1998                 echo ${ost_idx[@]}
1999
2000                 # check the layout
2001                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2002                         error "${#ost_idx[@]} != $OSTCOUNT"
2003
2004                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2005                         found=0
2006                         for j in $(echo ${ost_idx[@]}); do
2007                                 if [ $index -eq $j ]; then
2008                                         found=1
2009                                         break
2010                                 fi
2011                         done
2012                         [ $found = 1 ] ||
2013                                 error "Can not find $index in ${ost_idx[@]}"
2014                 done
2015         done
2016 }
2017 run_test 27C "check full striping across all OSTs"
2018
2019 test_27D() {
2020         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2021         local POOL=${POOL:-testpool}
2022         local first_ost=0
2023         local last_ost=$(($OSTCOUNT - 1))
2024         local ost_step=1
2025         local ost_list=$(seq $first_ost $ost_step $last_ost)
2026         local ost_range="$first_ost $last_ost $ost_step"
2027
2028         test_mkdir -p $DIR/$tdir
2029         pool_add $POOL || error "pool_add failed"
2030         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2031         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2032                 error "llapi_layout_test failed"
2033         cleanup_pools || error "cleanup_pools failed"
2034 }
2035 run_test 27D "validate llapi_layout API"
2036
2037 # Verify that default_easize is increased from its initial value after
2038 # accessing a widely striped file.
2039 test_27E() {
2040         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2041
2042         # 72 bytes is the minimum space required to store striping
2043         # information for a file striped across one OST:
2044         # (sizeof(struct lov_user_md_v3) +
2045         #  sizeof(struct lov_user_ost_data_v1))
2046         local min_easize=72
2047         $LCTL set_param -n llite.*.default_easize $min_easize ||
2048                 error "lctl set_param failed"
2049         local easize=$($LCTL get_param -n llite.*.default_easize)
2050
2051         [ $easize -eq $min_easize ] ||
2052                 error "failed to set default_easize"
2053
2054         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2055                 error "setstripe failed"
2056         cat $DIR/$tfile
2057         rm $DIR/$tfile
2058
2059         easize=$($LCTL get_param -n llite.*.default_easize)
2060
2061         [ $easize -gt $min_easize ] ||
2062                 error "default_easize not updated"
2063 }
2064 run_test 27E "check that default extended attribute size properly increases"
2065
2066 # createtest also checks that device nodes are created and
2067 # then visible correctly (#2091)
2068 test_28() { # bug 2091
2069         test_mkdir $DIR/d28
2070         $CREATETEST $DIR/d28/ct || error
2071 }
2072 run_test 28 "create/mknod/mkdir with bad file types ============"
2073
2074 test_29() {
2075         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2076         cancel_lru_locks mdc
2077         test_mkdir $DIR/d29
2078         touch $DIR/d29/foo
2079         log 'first d29'
2080         ls -l $DIR/d29
2081
2082         declare -i LOCKCOUNTORIG=0
2083         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2084                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2085         done
2086         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2087
2088         declare -i LOCKUNUSEDCOUNTORIG=0
2089         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2090                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2091         done
2092
2093         log 'second d29'
2094         ls -l $DIR/d29
2095         log 'done'
2096
2097         declare -i LOCKCOUNTCURRENT=0
2098         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2099                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2100         done
2101
2102         declare -i LOCKUNUSEDCOUNTCURRENT=0
2103         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2104                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2105         done
2106
2107         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2108                 $LCTL set_param -n ldlm.dump_namespaces ""
2109                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2110                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2111                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2112                 return 2
2113         fi
2114         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2115                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2116                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2117                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2118                 return 3
2119         fi
2120 }
2121 run_test 29 "IT_GETATTR regression  ============================"
2122
2123 test_30a() { # was test_30
2124         cp $(which ls) $DIR || cp /bin/ls $DIR
2125         $DIR/ls / || error
2126         rm $DIR/ls
2127 }
2128 run_test 30a "execute binary from Lustre (execve) =============="
2129
2130 test_30b() {
2131         cp `which ls` $DIR || cp /bin/ls $DIR
2132         chmod go+rx $DIR/ls
2133         $RUNAS $DIR/ls / || error
2134         rm $DIR/ls
2135 }
2136 run_test 30b "execute binary from Lustre as non-root ==========="
2137
2138 test_30c() { # b=22376
2139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2140         cp `which ls` $DIR || cp /bin/ls $DIR
2141         chmod a-rw $DIR/ls
2142         cancel_lru_locks mdc
2143         cancel_lru_locks osc
2144         $RUNAS $DIR/ls / || error
2145         rm -f $DIR/ls
2146 }
2147 run_test 30c "execute binary from Lustre without read perms ===="
2148
2149 test_31a() {
2150         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2151         $CHECKSTAT -a $DIR/f31 || error
2152 }
2153 run_test 31a "open-unlink file =================================="
2154
2155 test_31b() {
2156         touch $DIR/f31 || error
2157         ln $DIR/f31 $DIR/f31b || error
2158         $MULTIOP $DIR/f31b Ouc || error
2159         $CHECKSTAT -t file $DIR/f31 || error
2160 }
2161 run_test 31b "unlink file with multiple links while open ======="
2162
2163 test_31c() {
2164         touch $DIR/f31 || error
2165         ln $DIR/f31 $DIR/f31c || error
2166         multiop_bg_pause $DIR/f31 O_uc || return 1
2167         MULTIPID=$!
2168         $MULTIOP $DIR/f31c Ouc
2169         kill -USR1 $MULTIPID
2170         wait $MULTIPID
2171 }
2172 run_test 31c "open-unlink file with multiple links ============="
2173
2174 test_31d() {
2175         opendirunlink $DIR/d31d $DIR/d31d || error
2176         $CHECKSTAT -a $DIR/d31d || error
2177 }
2178 run_test 31d "remove of open directory ========================="
2179
2180 test_31e() { # bug 2904
2181         openfilleddirunlink $DIR/d31e || error
2182 }
2183 run_test 31e "remove of open non-empty directory ==============="
2184
2185 test_31f() { # bug 4554
2186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2187         set -vx
2188         test_mkdir $DIR/d31f
2189         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2190         cp /etc/hosts $DIR/d31f
2191         ls -l $DIR/d31f
2192         $GETSTRIPE $DIR/d31f/hosts
2193         multiop_bg_pause $DIR/d31f D_c || return 1
2194         MULTIPID=$!
2195
2196         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2197         test_mkdir $DIR/d31f
2198         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2199         cp /etc/hosts $DIR/d31f
2200         ls -l $DIR/d31f
2201         $GETSTRIPE $DIR/d31f/hosts
2202         multiop_bg_pause $DIR/d31f D_c || return 1
2203         MULTIPID2=$!
2204
2205         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2206         wait $MULTIPID || error "first opendir $MULTIPID failed"
2207
2208         sleep 6
2209
2210         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2211         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2212         set +vx
2213 }
2214 run_test 31f "remove of open directory with open-unlink file ==="
2215
2216 test_31g() {
2217         echo "-- cross directory link --"
2218         test_mkdir -c1 $DIR/${tdir}ga
2219         test_mkdir -c1 $DIR/${tdir}gb
2220         touch $DIR/${tdir}ga/f
2221         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2222         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2223         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2224         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2225         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2226 }
2227 run_test 31g "cross directory link==============="
2228
2229 test_31h() {
2230         echo "-- cross directory link --"
2231         test_mkdir -c1 $DIR/${tdir}
2232         test_mkdir -c1 $DIR/${tdir}/dir
2233         touch $DIR/${tdir}/f
2234         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2235         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2236         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2237         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2238         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2239 }
2240 run_test 31h "cross directory link under child==============="
2241
2242 test_31i() {
2243         echo "-- cross directory link --"
2244         test_mkdir -c1 $DIR/$tdir
2245         test_mkdir -c1 $DIR/$tdir/dir
2246         touch $DIR/$tdir/dir/f
2247         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2248         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2249         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2250         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2251         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2252 }
2253 run_test 31i "cross directory link under parent==============="
2254
2255 test_31j() {
2256         test_mkdir -c1 -p $DIR/$tdir
2257         test_mkdir -c1 -p $DIR/$tdir/dir1
2258         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2259         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2260         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2261         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2262         return 0
2263 }
2264 run_test 31j "link for directory==============="
2265
2266 test_31k() {
2267         test_mkdir -c1 -p $DIR/$tdir
2268         touch $DIR/$tdir/s
2269         touch $DIR/$tdir/exist
2270         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2271         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2272         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2273         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2274         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2275         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2276         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2277         return 0
2278 }
2279 run_test 31k "link to file: the same, non-existing, dir==============="
2280
2281 test_31m() {
2282         mkdir $DIR/d31m
2283         touch $DIR/d31m/s
2284         mkdir $DIR/d31m2
2285         touch $DIR/d31m2/exist
2286         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2287         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2288         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2289         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2290         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2291         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2292         return 0
2293 }
2294 run_test 31m "link to file: the same, non-existing, dir==============="
2295
2296 test_31n() {
2297         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2298         nlink=$(stat --format=%h $DIR/$tfile)
2299         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2300         local fd=$(free_fd)
2301         local cmd="exec $fd<$DIR/$tfile"
2302         eval $cmd
2303         cmd="exec $fd<&-"
2304         trap "eval $cmd" EXIT
2305         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2306         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2307         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2308         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2309         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2310         eval $cmd
2311 }
2312 run_test 31n "check link count of unlinked file"
2313
2314 link_one() {
2315         local TEMPNAME=$(mktemp $1_XXXXXX)
2316         mlink $TEMPNAME $1 2> /dev/null &&
2317                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2318         munlink $TEMPNAME
2319 }
2320
2321 test_31o() { # LU-2901
2322         test_mkdir -p $DIR/$tdir
2323         for LOOP in $(seq 100); do
2324                 rm -f $DIR/$tdir/$tfile*
2325                 for THREAD in $(seq 8); do
2326                         link_one $DIR/$tdir/$tfile.$LOOP &
2327                 done
2328                 wait
2329                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2330                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2331                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2332                         break || true
2333         done
2334 }
2335 run_test 31o "duplicate hard links with same filename"
2336
2337 test_31p() {
2338         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2339
2340         test_mkdir $DIR/$tdir
2341         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2342         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2343
2344         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2345                 error "open unlink test1 failed"
2346         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2347                 error "open unlink test2 failed"
2348
2349         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2350                 error "test1 still exists"
2351         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2352                 error "test2 still exists"
2353 }
2354 run_test 31p "remove of open striped directory"
2355
2356 cleanup_test32_mount() {
2357         trap 0
2358         $UMOUNT $DIR/$tdir/ext2-mountpoint
2359 }
2360
2361 test_32a() {
2362         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2363         echo "== more mountpoints and symlinks ================="
2364         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2365         trap cleanup_test32_mount EXIT
2366         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2367         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2368         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2369         cleanup_test32_mount
2370 }
2371 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2372
2373 test_32b() {
2374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
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         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2380         cleanup_test32_mount
2381 }
2382 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2383
2384 test_32c() {
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         test_mkdir -p $DIR/$tdir/d2/test_dir
2391         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2392         cleanup_test32_mount
2393 }
2394 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2395
2396 test_32d() {
2397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2398         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2399         trap cleanup_test32_mount EXIT
2400         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2401         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2402         test_mkdir -p $DIR/$tdir/d2/test_dir
2403         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2404         cleanup_test32_mount
2405 }
2406 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2407
2408 test_32e() {
2409         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2410         test_mkdir -p $DIR/d32e/tmp
2411         TMP_DIR=$DIR/d32e/tmp
2412         ln -s $DIR/d32e $TMP_DIR/symlink11
2413         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2414         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2415         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2416 }
2417 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2418
2419 test_32f() {
2420         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2421         test_mkdir -p $DIR/d32f/tmp
2422         TMP_DIR=$DIR/d32f/tmp
2423         ln -s $DIR/d32f $TMP_DIR/symlink11
2424         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2425         ls $DIR/d32f/tmp/symlink11  || error
2426         ls $DIR/d32f/symlink01 || error
2427 }
2428 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2429
2430 test_32g() {
2431         TMP_DIR=$DIR/$tdir/tmp
2432         test_mkdir -p $DIR/$tdir/tmp
2433         test_mkdir $DIR/${tdir}2
2434         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2435         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2436         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2437         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2438         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2439         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2440 }
2441 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2442
2443 test_32h() {
2444         rm -fr $DIR/$tdir $DIR/${tdir}2
2445         TMP_DIR=$DIR/$tdir/tmp
2446         test_mkdir -p $DIR/$tdir/tmp
2447         test_mkdir $DIR/${tdir}2
2448         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2449         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2450         ls $TMP_DIR/symlink12 || error
2451         ls $DIR/$tdir/symlink02  || error
2452 }
2453 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2454
2455 test_32i() {
2456         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2457         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2458         trap cleanup_test32_mount EXIT
2459         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2460         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2461         touch $DIR/$tdir/test_file
2462         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2463         cleanup_test32_mount
2464 }
2465 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2466
2467 test_32j() {
2468         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2469         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2470         trap cleanup_test32_mount EXIT
2471         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2472         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2473         touch $DIR/$tdir/test_file
2474         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2475         cleanup_test32_mount
2476 }
2477 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2478
2479 test_32k() {
2480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2481         rm -fr $DIR/$tdir
2482         trap cleanup_test32_mount EXIT
2483         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2484         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2485         test_mkdir -p $DIR/$tdir/d2
2486         touch $DIR/$tdir/d2/test_file || error
2487         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2488         cleanup_test32_mount
2489 }
2490 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2491
2492 test_32l() {
2493         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2494         rm -fr $DIR/$tdir
2495         trap cleanup_test32_mount EXIT
2496         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2497         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2498         test_mkdir -p $DIR/$tdir/d2
2499         touch $DIR/$tdir/d2/test_file
2500         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2501         cleanup_test32_mount
2502 }
2503 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2504
2505 test_32m() {
2506         rm -fr $DIR/d32m
2507         test_mkdir -p $DIR/d32m/tmp
2508         TMP_DIR=$DIR/d32m/tmp
2509         ln -s $DIR $TMP_DIR/symlink11
2510         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2511         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2512         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2513 }
2514 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2515
2516 test_32n() {
2517         rm -fr $DIR/d32n
2518         test_mkdir -p $DIR/d32n/tmp
2519         TMP_DIR=$DIR/d32n/tmp
2520         ln -s $DIR $TMP_DIR/symlink11
2521         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2522         ls -l $DIR/d32n/tmp/symlink11  || error
2523         ls -l $DIR/d32n/symlink01 || error
2524 }
2525 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2526
2527 test_32o() {
2528         touch $DIR/$tfile
2529         test_mkdir -p $DIR/d32o/tmp
2530         TMP_DIR=$DIR/d32o/tmp
2531         ln -s $DIR/$tfile $TMP_DIR/symlink12
2532         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2533         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2534         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2535         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2536         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2537 }
2538 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2539
2540 test_32p() {
2541     log 32p_1
2542         rm -fr $DIR/d32p
2543     log 32p_2
2544         rm -f $DIR/$tfile
2545     log 32p_3
2546         touch $DIR/$tfile
2547     log 32p_4
2548         test_mkdir -p $DIR/d32p/tmp
2549     log 32p_5
2550         TMP_DIR=$DIR/d32p/tmp
2551     log 32p_6
2552         ln -s $DIR/$tfile $TMP_DIR/symlink12
2553     log 32p_7
2554         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2555     log 32p_8
2556         cat $DIR/d32p/tmp/symlink12 || error
2557     log 32p_9
2558         cat $DIR/d32p/symlink02 || error
2559     log 32p_10
2560 }
2561 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2562
2563 cleanup_testdir_mount() {
2564         trap 0
2565         $UMOUNT $DIR/$tdir
2566 }
2567
2568 test_32q() {
2569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2570         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2571         trap cleanup_testdir_mount EXIT
2572         test_mkdir -p $DIR/$tdir
2573         touch $DIR/$tdir/under_the_mount
2574         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2575         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2576         cleanup_testdir_mount
2577 }
2578 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2579
2580 test_32r() {
2581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2582         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2583         trap cleanup_testdir_mount EXIT
2584         test_mkdir -p $DIR/$tdir
2585         touch $DIR/$tdir/under_the_mount
2586         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2587         ls $DIR/$tdir | grep -q under_the_mount && error || true
2588         cleanup_testdir_mount
2589 }
2590 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2591
2592 test_33aa() {
2593         rm -f $DIR/$tfile
2594         touch $DIR/$tfile
2595         chmod 444 $DIR/$tfile
2596         chown $RUNAS_ID $DIR/$tfile
2597         log 33_1
2598         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2599         log 33_2
2600 }
2601 run_test 33aa "write file with mode 444 (should return error) ===="
2602
2603 test_33a() {
2604         rm -fr $DIR/d33
2605         test_mkdir -p $DIR/d33
2606         chown $RUNAS_ID $DIR/d33
2607         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2608         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2609                 error "open RDWR" || true
2610 }
2611 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2612
2613 test_33b() {
2614         rm -fr $DIR/d33
2615         test_mkdir -p $DIR/d33
2616         chown $RUNAS_ID $DIR/d33
2617         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2618 }
2619 run_test 33b "test open file with malformed flags (No panic)"
2620
2621 test_33c() {
2622         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2623         local ostnum
2624         local ostname
2625         local write_bytes
2626         local all_zeros
2627
2628         remote_ost_nodsh && skip "remote OST with nodsh" && return
2629         all_zeros=:
2630         rm -fr $DIR/d33
2631         test_mkdir -p $DIR/d33
2632         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2633
2634         sync
2635         for ostnum in $(seq $OSTCOUNT); do
2636                 # test-framework's OST numbering is one-based, while Lustre's
2637                 # is zero-based
2638                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2639                 # Parsing llobdstat's output sucks; we could grep the /proc
2640                 # path, but that's likely to not be as portable as using the
2641                 # llobdstat utility.  So we parse lctl output instead.
2642                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2643                         obdfilter/$ostname/stats |
2644                         awk '/^write_bytes/ {print $7}' )
2645                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2646                 if (( ${write_bytes:-0} > 0 ))
2647                 then
2648                         all_zeros=false
2649                         break;
2650                 fi
2651         done
2652
2653         $all_zeros || return 0
2654
2655         # Write four bytes
2656         echo foo > $DIR/d33/bar
2657         # Really write them
2658         sync
2659
2660         # Total up write_bytes after writing.  We'd better find non-zeros.
2661         for ostnum in $(seq $OSTCOUNT); do
2662                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2663                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2664                         obdfilter/$ostname/stats |
2665                         awk '/^write_bytes/ {print $7}' )
2666                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2667                 if (( ${write_bytes:-0} > 0 ))
2668                 then
2669                         all_zeros=false
2670                         break;
2671                 fi
2672         done
2673
2674         if $all_zeros
2675         then
2676                 for ostnum in $(seq $OSTCOUNT); do
2677                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2678                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2679                         do_facet ost$ostnum lctl get_param -n \
2680                                 obdfilter/$ostname/stats
2681                 done
2682                 error "OST not keeping write_bytes stats (b22312)"
2683         fi
2684 }
2685 run_test 33c "test llobdstat and write_bytes"
2686
2687 test_33d() {
2688         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2690         local MDTIDX=1
2691         local remote_dir=$DIR/$tdir/remote_dir
2692
2693         test_mkdir -p $DIR/$tdir
2694         $LFS mkdir -i $MDTIDX $remote_dir ||
2695                 error "create remote directory failed"
2696
2697         touch $remote_dir/$tfile
2698         chmod 444 $remote_dir/$tfile
2699         chown $RUNAS_ID $remote_dir/$tfile
2700
2701         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2702
2703         chown $RUNAS_ID $remote_dir
2704         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2705                                         error "create" || true
2706         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2707                                     error "open RDWR" || true
2708         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2709 }
2710 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2711
2712 test_33e() {
2713         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2714
2715         mkdir $DIR/$tdir
2716
2717         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2718         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2719         mkdir $DIR/$tdir/local_dir
2720
2721         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2722         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2723         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2724
2725         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2726                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2727
2728         rmdir $DIR/$tdir/* || error "rmdir failed"
2729
2730         umask 777
2731         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2732         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2733         mkdir $DIR/$tdir/local_dir
2734
2735         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2736         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2737         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2738
2739         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2740                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2741
2742         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2743
2744         umask 000
2745         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2746         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2747         mkdir $DIR/$tdir/local_dir
2748
2749         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2750         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2751         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2752
2753         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2754                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2755 }
2756 run_test 33e "mkdir and striped directory should have same mode"
2757
2758 cleanup_33f() {
2759         trap 0
2760         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2761 }
2762
2763 test_33f() {
2764         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2765
2766         mkdir $DIR/$tdir
2767         chmod go+rwx $DIR/$tdir
2768         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2769         trap cleanup_33f EXIT
2770
2771         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2772                 error "cannot create striped directory"
2773
2774         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2775                 error "cannot create files in striped directory"
2776
2777         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2778                 error "cannot remove files in striped directory"
2779
2780         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2781                 error "cannot remove striped directory"
2782
2783         cleanup_33f
2784 }
2785 run_test 33f "nonroot user can create, access, and remove a striped directory"
2786
2787 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2788 test_34a() {
2789         rm -f $DIR/f34
2790         $MCREATE $DIR/f34 || error
2791         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2792         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2793         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2794         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2795 }
2796 run_test 34a "truncate file that has not been opened ==========="
2797
2798 test_34b() {
2799         [ ! -f $DIR/f34 ] && test_34a
2800         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2801         $OPENFILE -f O_RDONLY $DIR/f34
2802         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2803         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2804 }
2805 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2806
2807 test_34c() {
2808         [ ! -f $DIR/f34 ] && test_34a
2809         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2810         $OPENFILE -f O_RDWR $DIR/f34
2811         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2812         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2813 }
2814 run_test 34c "O_RDWR opening file-with-size works =============="
2815
2816 test_34d() {
2817         [ ! -f $DIR/f34 ] && test_34a
2818         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2819         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2820         rm $DIR/f34
2821 }
2822 run_test 34d "write to sparse file ============================="
2823
2824 test_34e() {
2825         rm -f $DIR/f34e
2826         $MCREATE $DIR/f34e || error
2827         $TRUNCATE $DIR/f34e 1000 || error
2828         $CHECKSTAT -s 1000 $DIR/f34e || error
2829         $OPENFILE -f O_RDWR $DIR/f34e
2830         $CHECKSTAT -s 1000 $DIR/f34e || error
2831 }
2832 run_test 34e "create objects, some with size and some without =="
2833
2834 test_34f() { # bug 6242, 6243
2835         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2836         SIZE34F=48000
2837         rm -f $DIR/f34f
2838         $MCREATE $DIR/f34f || error
2839         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2840         dd if=$DIR/f34f of=$TMP/f34f
2841         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2842         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2843         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2844         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2845         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2846 }
2847 run_test 34f "read from a file with no objects until EOF ======="
2848
2849 test_34g() {
2850         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2851         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2852         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2853         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2854         cancel_lru_locks osc
2855         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2856                 error "wrong size after lock cancel"
2857
2858         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2859         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2860                 error "expanding truncate failed"
2861         cancel_lru_locks osc
2862         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2863                 error "wrong expanded size after lock cancel"
2864 }
2865 run_test 34g "truncate long file ==============================="
2866
2867 test_34h() {
2868         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2869         local gid=10
2870         local sz=1000
2871
2872         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2873         sync # Flush the cache so that multiop below does not block on cache
2874              # flush when getting the group lock
2875         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2876         MULTIPID=$!
2877
2878         # Since just timed wait is not good enough, let's do a sync write
2879         # that way we are sure enough time for a roundtrip + processing
2880         # passed + 2 seconds of extra margin.
2881         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2882         rm $DIR/${tfile}-1
2883         sleep 2
2884
2885         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2886                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2887                 kill -9 $MULTIPID
2888         fi
2889         wait $MULTIPID
2890         local nsz=`stat -c %s $DIR/$tfile`
2891         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2892 }
2893 run_test 34h "ftruncate file under grouplock should not block"
2894
2895 test_35a() {
2896         cp /bin/sh $DIR/f35a
2897         chmod 444 $DIR/f35a
2898         chown $RUNAS_ID $DIR/f35a
2899         $RUNAS $DIR/f35a && error || true
2900         rm $DIR/f35a
2901 }
2902 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2903
2904 test_36a() {
2905         rm -f $DIR/f36
2906         utime $DIR/f36 || error
2907 }
2908 run_test 36a "MDS utime check (mknod, utime) ==================="
2909
2910 test_36b() {
2911         echo "" > $DIR/f36
2912         utime $DIR/f36 || error
2913 }
2914 run_test 36b "OST utime check (open, utime) ===================="
2915
2916 test_36c() {
2917         rm -f $DIR/d36/f36
2918         test_mkdir $DIR/d36
2919         chown $RUNAS_ID $DIR/d36
2920         $RUNAS utime $DIR/d36/f36 || error
2921 }
2922 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2923
2924 test_36d() {
2925         [ ! -d $DIR/d36 ] && test_36c
2926         echo "" > $DIR/d36/f36
2927         $RUNAS utime $DIR/d36/f36 || error
2928 }
2929 run_test 36d "non-root OST utime check (open, utime) ==========="
2930
2931 test_36e() {
2932         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2933         test_mkdir -p $DIR/$tdir
2934         touch $DIR/$tdir/$tfile
2935         $RUNAS utime $DIR/$tdir/$tfile && \
2936                 error "utime worked, expected failure" || true
2937 }
2938 run_test 36e "utime on non-owned file (should return error) ===="
2939
2940 subr_36fh() {
2941         local fl="$1"
2942         local LANG_SAVE=$LANG
2943         local LC_LANG_SAVE=$LC_LANG
2944         export LANG=C LC_LANG=C # for date language
2945
2946         DATESTR="Dec 20  2000"
2947         test_mkdir -p $DIR/$tdir
2948         lctl set_param fail_loc=$fl
2949         date; date +%s
2950         cp /etc/hosts $DIR/$tdir/$tfile
2951         sync & # write RPC generated with "current" inode timestamp, but delayed
2952         sleep 1
2953         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2954         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2955         cancel_lru_locks osc
2956         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2957         date; date +%s
2958         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2959                 echo "BEFORE: $LS_BEFORE" && \
2960                 echo "AFTER : $LS_AFTER" && \
2961                 echo "WANT  : $DATESTR" && \
2962                 error "$DIR/$tdir/$tfile timestamps changed" || true
2963
2964         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2965 }
2966
2967 test_36f() {
2968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2969         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2970         subr_36fh "0x80000214"
2971 }
2972 run_test 36f "utime on file racing with OST BRW write =========="
2973
2974 test_36g() {
2975         remote_ost_nodsh && skip "remote OST with nodsh" && return
2976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2977         local fmd_max_age
2978         local fmd_before
2979         local fmd_after
2980
2981         test_mkdir -p $DIR/$tdir
2982         fmd_max_age=$(do_facet ost1 \
2983                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2984                 head -n 1")
2985
2986         fmd_before=$(do_facet ost1 \
2987                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2988         touch $DIR/$tdir/$tfile
2989         sleep $((fmd_max_age + 12))
2990         fmd_after=$(do_facet ost1 \
2991                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2992
2993         echo "fmd_before: $fmd_before"
2994         echo "fmd_after: $fmd_after"
2995         [[ $fmd_after -gt $fmd_before ]] &&
2996                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2997                 error "fmd didn't expire after ping" || true
2998 }
2999 run_test 36g "filter mod data cache expiry ====================="
3000
3001 test_36h() {
3002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3003         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3004         subr_36fh "0x80000227"
3005 }
3006 run_test 36h "utime on file racing with OST BRW write =========="
3007
3008 test_36i() {
3009         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3010
3011         test_mkdir $DIR/$tdir
3012         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3013
3014         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3015         local new_mtime=$((mtime + 200))
3016
3017         #change Modify time of striped dir
3018         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3019                         error "change mtime failed"
3020
3021         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3022
3023         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3024 }
3025 run_test 36i "change mtime on striped directory"
3026
3027 # test_37 - duplicate with tests 32q 32r
3028
3029 test_38() {
3030         local file=$DIR/$tfile
3031         touch $file
3032         openfile -f O_DIRECTORY $file
3033         local RC=$?
3034         local ENOTDIR=20
3035         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3036         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3037 }
3038 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3039
3040 test_39() {
3041         touch $DIR/$tfile
3042         touch $DIR/${tfile}2
3043 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3044 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3045 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3046         sleep 2
3047         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3048         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3049                 echo "mtime"
3050                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3051                 echo "atime"
3052                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3053                 echo "ctime"
3054                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3055                 error "O_TRUNC didn't change timestamps"
3056         fi
3057 }
3058 run_test 39 "mtime changed on create ==========================="
3059
3060 test_39b() {
3061         test_mkdir -p -c1 $DIR/$tdir
3062         cp -p /etc/passwd $DIR/$tdir/fopen
3063         cp -p /etc/passwd $DIR/$tdir/flink
3064         cp -p /etc/passwd $DIR/$tdir/funlink
3065         cp -p /etc/passwd $DIR/$tdir/frename
3066         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3067
3068         sleep 1
3069         echo "aaaaaa" >> $DIR/$tdir/fopen
3070         echo "aaaaaa" >> $DIR/$tdir/flink
3071         echo "aaaaaa" >> $DIR/$tdir/funlink
3072         echo "aaaaaa" >> $DIR/$tdir/frename
3073
3074         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3075         local link_new=`stat -c %Y $DIR/$tdir/flink`
3076         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3077         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3078
3079         cat $DIR/$tdir/fopen > /dev/null
3080         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3081         rm -f $DIR/$tdir/funlink2
3082         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3083
3084         for (( i=0; i < 2; i++ )) ; do
3085                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3086                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3087                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3088                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3089
3090                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3091                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3092                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3093                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3094
3095                 cancel_lru_locks osc
3096                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3097         done
3098 }
3099 run_test 39b "mtime change on open, link, unlink, rename  ======"
3100
3101 # this should be set to past
3102 TEST_39_MTIME=`date -d "1 year ago" +%s`
3103
3104 # bug 11063
3105 test_39c() {
3106         touch $DIR1/$tfile
3107         sleep 2
3108         local mtime0=`stat -c %Y $DIR1/$tfile`
3109
3110         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3111         local mtime1=`stat -c %Y $DIR1/$tfile`
3112         [ "$mtime1" = $TEST_39_MTIME ] || \
3113                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3114
3115         local d1=`date +%s`
3116         echo hello >> $DIR1/$tfile
3117         local d2=`date +%s`
3118         local mtime2=`stat -c %Y $DIR1/$tfile`
3119         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3120                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3121
3122         mv $DIR1/$tfile $DIR1/$tfile-1
3123
3124         for (( i=0; i < 2; i++ )) ; do
3125                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3126                 [ "$mtime2" = "$mtime3" ] || \
3127                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3128
3129                 cancel_lru_locks osc
3130                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3131         done
3132 }
3133 run_test 39c "mtime change on rename ==========================="
3134
3135 # bug 21114
3136 test_39d() {
3137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3138         touch $DIR1/$tfile
3139
3140         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3141
3142         for (( i=0; i < 2; i++ )) ; do
3143                 local mtime=`stat -c %Y $DIR1/$tfile`
3144                 [ $mtime = $TEST_39_MTIME ] || \
3145                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3146
3147                 cancel_lru_locks osc
3148                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3149         done
3150 }
3151 run_test 39d "create, utime, stat =============================="
3152
3153 # bug 21114
3154 test_39e() {
3155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3156         touch $DIR1/$tfile
3157         local mtime1=`stat -c %Y $DIR1/$tfile`
3158
3159         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3160
3161         for (( i=0; i < 2; i++ )) ; do
3162                 local mtime2=`stat -c %Y $DIR1/$tfile`
3163                 [ $mtime2 = $TEST_39_MTIME ] || \
3164                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3165
3166                 cancel_lru_locks osc
3167                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3168         done
3169 }
3170 run_test 39e "create, stat, utime, stat ========================"
3171
3172 # bug 21114
3173 test_39f() {
3174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3175         touch $DIR1/$tfile
3176         mtime1=`stat -c %Y $DIR1/$tfile`
3177
3178         sleep 2
3179         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3180
3181         for (( i=0; i < 2; i++ )) ; do
3182                 local mtime2=`stat -c %Y $DIR1/$tfile`
3183                 [ $mtime2 = $TEST_39_MTIME ] || \
3184                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3185
3186                 cancel_lru_locks osc
3187                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3188         done
3189 }
3190 run_test 39f "create, stat, sleep, utime, stat ================="
3191
3192 # bug 11063
3193 test_39g() {
3194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3195         echo hello >> $DIR1/$tfile
3196         local mtime1=`stat -c %Y $DIR1/$tfile`
3197
3198         sleep 2
3199         chmod o+r $DIR1/$tfile
3200
3201         for (( i=0; i < 2; i++ )) ; do
3202                 local mtime2=`stat -c %Y $DIR1/$tfile`
3203                 [ "$mtime1" = "$mtime2" ] || \
3204                         error "lost mtime: $mtime2, should be $mtime1"
3205
3206                 cancel_lru_locks osc
3207                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3208         done
3209 }
3210 run_test 39g "write, chmod, stat ==============================="
3211
3212 # bug 11063
3213 test_39h() {
3214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3215         touch $DIR1/$tfile
3216         sleep 1
3217
3218         local d1=`date`
3219         echo hello >> $DIR1/$tfile
3220         local mtime1=`stat -c %Y $DIR1/$tfile`
3221
3222         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3223         local d2=`date`
3224         if [ "$d1" != "$d2" ]; then
3225                 echo "write and touch not within one second"
3226         else
3227                 for (( i=0; i < 2; i++ )) ; do
3228                         local mtime2=`stat -c %Y $DIR1/$tfile`
3229                         [ "$mtime2" = $TEST_39_MTIME ] || \
3230                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3231
3232                         cancel_lru_locks osc
3233                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3234                 done
3235         fi
3236 }
3237 run_test 39h "write, utime within one second, stat ============="
3238
3239 test_39i() {
3240         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3241         touch $DIR1/$tfile
3242         sleep 1
3243
3244         echo hello >> $DIR1/$tfile
3245         local mtime1=`stat -c %Y $DIR1/$tfile`
3246
3247         mv $DIR1/$tfile $DIR1/$tfile-1
3248
3249         for (( i=0; i < 2; i++ )) ; do
3250                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3251
3252                 [ "$mtime1" = "$mtime2" ] || \
3253                         error "lost mtime: $mtime2, should be $mtime1"
3254
3255                 cancel_lru_locks osc
3256                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3257         done
3258 }
3259 run_test 39i "write, rename, stat =============================="
3260
3261 test_39j() {
3262         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3263         start_full_debug_logging
3264         touch $DIR1/$tfile
3265         sleep 1
3266
3267         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3268         lctl set_param fail_loc=0x80000412
3269         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3270                 error "multiop failed"
3271         local multipid=$!
3272         local mtime1=`stat -c %Y $DIR1/$tfile`
3273
3274         mv $DIR1/$tfile $DIR1/$tfile-1
3275
3276         kill -USR1 $multipid
3277         wait $multipid || error "multiop close failed"
3278
3279         for (( i=0; i < 2; i++ )) ; do
3280                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3281                 [ "$mtime1" = "$mtime2" ] ||
3282                         error "mtime is lost on close: $mtime2, " \
3283                               "should be $mtime1"
3284
3285                 cancel_lru_locks osc
3286                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3287         done
3288         lctl set_param fail_loc=0
3289         stop_full_debug_logging
3290 }
3291 run_test 39j "write, rename, close, stat ======================="
3292
3293 test_39k() {
3294         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3295         touch $DIR1/$tfile
3296         sleep 1
3297
3298         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3299         local multipid=$!
3300         local mtime1=`stat -c %Y $DIR1/$tfile`
3301
3302         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3303
3304         kill -USR1 $multipid
3305         wait $multipid || error "multiop close failed"
3306
3307         for (( i=0; i < 2; i++ )) ; do
3308                 local mtime2=`stat -c %Y $DIR1/$tfile`
3309
3310                 [ "$mtime2" = $TEST_39_MTIME ] || \
3311                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3312
3313                 cancel_lru_locks osc
3314                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3315         done
3316 }
3317 run_test 39k "write, utime, close, stat ========================"
3318
3319 # this should be set to future
3320 TEST_39_ATIME=`date -d "1 year" +%s`
3321
3322 test_39l() {
3323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3324         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3325         local atime_diff=$(do_facet $SINGLEMDS \
3326                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3327         rm -rf $DIR/$tdir
3328         mkdir -p $DIR/$tdir
3329
3330         # test setting directory atime to future
3331         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3332         local atime=$(stat -c %X $DIR/$tdir)
3333         [ "$atime" = $TEST_39_ATIME ] || \
3334                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3335
3336         # test setting directory atime from future to now
3337         local d1=$(date +%s)
3338         ls $DIR/$tdir
3339         local d2=$(date +%s)
3340
3341         cancel_lru_locks mdc
3342         atime=$(stat -c %X $DIR/$tdir)
3343         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3344                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3345
3346         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3347         sleep 3
3348
3349         # test setting directory atime when now > dir atime + atime_diff
3350         d1=$(date +%s)
3351         ls $DIR/$tdir
3352         d2=$(date +%s)
3353         cancel_lru_locks mdc
3354         atime=$(stat -c %X $DIR/$tdir)
3355         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3356                 error "atime is not updated  : $atime, should be $d2"
3357
3358         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3359         sleep 3
3360
3361         # test not setting directory atime when now < dir atime + atime_diff
3362         ls $DIR/$tdir
3363         cancel_lru_locks mdc
3364         atime=$(stat -c %X $DIR/$tdir)
3365         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3366                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3367
3368         do_facet $SINGLEMDS \
3369                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3370 }
3371 run_test 39l "directory atime update ==========================="
3372
3373 test_39m() {
3374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3375         touch $DIR1/$tfile
3376         sleep 2
3377         local far_past_mtime=$(date -d "May 29 1953" +%s)
3378         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3379
3380         touch -m -d @$far_past_mtime $DIR1/$tfile
3381         touch -a -d @$far_past_atime $DIR1/$tfile
3382
3383         for (( i=0; i < 2; i++ )) ; do
3384                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3385                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3386                         error "atime or mtime set incorrectly"
3387
3388                 cancel_lru_locks osc
3389                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3390         done
3391 }
3392 run_test 39m "test atime and mtime before 1970"
3393
3394 test_39n() { # LU-3832
3395         local atime_diff=$(do_facet $SINGLEMDS \
3396                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3397         local atime0
3398         local atime1
3399         local atime2
3400
3401         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3402
3403         rm -rf $DIR/$tfile
3404         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3405         atime0=$(stat -c %X $DIR/$tfile)
3406
3407         sleep 5
3408         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3409         atime1=$(stat -c %X $DIR/$tfile)
3410
3411         sleep 5
3412         cancel_lru_locks mdc
3413         cancel_lru_locks osc
3414         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3415         atime2=$(stat -c %X $DIR/$tfile)
3416
3417         do_facet $SINGLEMDS \
3418                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3419
3420         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3421         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3422 }
3423 run_test 39n "check that O_NOATIME is honored"
3424
3425 test_39o() {
3426         TESTDIR=$DIR/$tdir/$tfile
3427         [ -e $TESTDIR ] && rm -rf $TESTDIR
3428         mkdir -p $TESTDIR
3429         cd $TESTDIR
3430         links1=2
3431         ls
3432         mkdir a b
3433         ls
3434         links2=$(stat -c %h .)
3435         [ $(($links1 + 2)) != $links2 ] &&
3436                 error "wrong links count $(($links1 + 2)) != $links2"
3437         rmdir b
3438         links3=$(stat -c %h .)
3439         [ $(($links1 + 1)) != $links3 ] &&
3440                 error "wrong links count $links1 != $links3"
3441         return 0
3442 }
3443 run_test 39o "directory cached attributes updated after create ========"
3444
3445 test_39p() {
3446         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3447         local MDTIDX=1
3448         TESTDIR=$DIR/$tdir/$tfile
3449         [ -e $TESTDIR ] && rm -rf $TESTDIR
3450         test_mkdir -p $TESTDIR
3451         cd $TESTDIR
3452         links1=2
3453         ls
3454         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3455         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3456         ls
3457         links2=$(stat -c %h .)
3458         [ $(($links1 + 2)) != $links2 ] &&
3459                 error "wrong links count $(($links1 + 2)) != $links2"
3460         rmdir remote_dir2
3461         links3=$(stat -c %h .)
3462         [ $(($links1 + 1)) != $links3 ] &&
3463                 error "wrong links count $links1 != $links3"
3464         return 0
3465 }
3466 run_test 39p "remote directory cached attributes updated after create ========"
3467
3468
3469 test_40() {
3470         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3471         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3472                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3473         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3474                 error "$tfile is not 4096 bytes in size"
3475 }
3476 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3477
3478 test_41() {
3479         # bug 1553
3480         small_write $DIR/f41 18
3481 }
3482 run_test 41 "test small file write + fstat ====================="
3483
3484 count_ost_writes() {
3485         lctl get_param -n osc.*.stats |
3486                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3487                         END { printf("%0.0f", writes) }'
3488 }
3489
3490 # decent default
3491 WRITEBACK_SAVE=500
3492 DIRTY_RATIO_SAVE=40
3493 MAX_DIRTY_RATIO=50
3494 BG_DIRTY_RATIO_SAVE=10
3495 MAX_BG_DIRTY_RATIO=25
3496
3497 start_writeback() {
3498         trap 0
3499         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3500         # dirty_ratio, dirty_background_ratio
3501         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3502                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3503                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3504                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3505         else
3506                 # if file not here, we are a 2.4 kernel
3507                 kill -CONT `pidof kupdated`
3508         fi
3509 }
3510
3511 stop_writeback() {
3512         # setup the trap first, so someone cannot exit the test at the
3513         # exact wrong time and mess up a machine
3514         trap start_writeback EXIT
3515         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3516         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3517                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3518                 sysctl -w vm.dirty_writeback_centisecs=0
3519                 sysctl -w vm.dirty_writeback_centisecs=0
3520                 # save and increase /proc/sys/vm/dirty_ratio
3521                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3522                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3523                 # save and increase /proc/sys/vm/dirty_background_ratio
3524                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3525                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3526         else
3527                 # if file not here, we are a 2.4 kernel
3528                 kill -STOP `pidof kupdated`
3529         fi
3530 }
3531
3532 # ensure that all stripes have some grant before we test client-side cache
3533 setup_test42() {
3534         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3535                 dd if=/dev/zero of=$i bs=4k count=1
3536                 rm $i
3537         done
3538 }
3539
3540 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3541 # file truncation, and file removal.
3542 test_42a() {
3543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3544         setup_test42
3545         cancel_lru_locks osc
3546         stop_writeback
3547         sync; sleep 1; sync # just to be safe
3548         BEFOREWRITES=`count_ost_writes`
3549         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3550         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3551         AFTERWRITES=`count_ost_writes`
3552         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3553                 error "$BEFOREWRITES < $AFTERWRITES"
3554         start_writeback
3555 }
3556 run_test 42a "ensure that we don't flush on close =============="
3557
3558 test_42b() {
3559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3560         setup_test42
3561         cancel_lru_locks osc
3562         stop_writeback
3563         sync
3564         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3565         BEFOREWRITES=$(count_ost_writes)
3566         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3567         AFTERWRITES=$(count_ost_writes)
3568         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3569                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3570         fi
3571         BEFOREWRITES=$(count_ost_writes)
3572         sync || error "sync: $?"
3573         AFTERWRITES=$(count_ost_writes)
3574         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3575                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3576         fi
3577         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3578         start_writeback
3579         return 0
3580 }
3581 run_test 42b "test destroy of file with cached dirty data ======"
3582
3583 # if these tests just want to test the effect of truncation,
3584 # they have to be very careful.  consider:
3585 # - the first open gets a {0,EOF}PR lock
3586 # - the first write conflicts and gets a {0, count-1}PW
3587 # - the rest of the writes are under {count,EOF}PW
3588 # - the open for truncate tries to match a {0,EOF}PR
3589 #   for the filesize and cancels the PWs.
3590 # any number of fixes (don't get {0,EOF} on open, match
3591 # composite locks, do smarter file size management) fix
3592 # this, but for now we want these tests to verify that
3593 # the cancellation with truncate intent works, so we
3594 # start the file with a full-file pw lock to match against
3595 # until the truncate.
3596 trunc_test() {
3597         test=$1
3598         file=$DIR/$test
3599         offset=$2
3600         cancel_lru_locks osc
3601         stop_writeback
3602         # prime the file with 0,EOF PW to match
3603         touch $file
3604         $TRUNCATE $file 0
3605         sync; sync
3606         # now the real test..
3607         dd if=/dev/zero of=$file bs=1024 count=100
3608         BEFOREWRITES=`count_ost_writes`
3609         $TRUNCATE $file $offset
3610         cancel_lru_locks osc
3611         AFTERWRITES=`count_ost_writes`
3612         start_writeback
3613 }
3614
3615 test_42c() {
3616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3617         trunc_test 42c 1024
3618         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3619             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3620         rm $file
3621 }
3622 run_test 42c "test partial truncate of file with cached dirty data"
3623
3624 test_42d() {
3625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3626         trunc_test 42d 0
3627         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3628             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3629         rm $file
3630 }
3631 run_test 42d "test complete truncate of file with cached dirty data"
3632
3633 test_42e() { # bug22074
3634         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3635         local TDIR=$DIR/${tdir}e
3636         local pagesz=$(page_size)
3637         local pages=16 # hardcoded 16 pages, don't change it.
3638         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3639         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3640         local max_dirty_mb
3641         local warmup_files
3642
3643         test_mkdir -p $DIR/${tdir}e
3644         $SETSTRIPE -c 1 $TDIR
3645         createmany -o $TDIR/f $files
3646
3647         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3648
3649         # we assume that with $OSTCOUNT files, at least one of them will
3650         # be allocated on OST0.
3651         warmup_files=$((OSTCOUNT * max_dirty_mb))
3652         createmany -o $TDIR/w $warmup_files
3653
3654         # write a large amount of data into one file and sync, to get good
3655         # avail_grant number from OST.
3656         for ((i=0; i<$warmup_files; i++)); do
3657                 idx=$($GETSTRIPE -i $TDIR/w$i)
3658                 [ $idx -ne 0 ] && continue
3659                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3660                 break
3661         done
3662         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3663         sync
3664         $LCTL get_param $proc_osc0/cur_dirty_bytes
3665         $LCTL get_param $proc_osc0/cur_grant_bytes
3666
3667         # create as much dirty pages as we can while not to trigger the actual
3668         # RPCs directly. but depends on the env, VFS may trigger flush during this
3669         # period, hopefully we are good.
3670         for ((i=0; i<$warmup_files; i++)); do
3671                 idx=$($GETSTRIPE -i $TDIR/w$i)
3672                 [ $idx -ne 0 ] && continue
3673                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3674         done
3675         $LCTL get_param $proc_osc0/cur_dirty_bytes
3676         $LCTL get_param $proc_osc0/cur_grant_bytes
3677
3678         # perform the real test
3679         $LCTL set_param $proc_osc0/rpc_stats 0
3680         for ((;i<$files; i++)); do
3681                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3682                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3683         done
3684         sync
3685         $LCTL get_param $proc_osc0/rpc_stats
3686
3687         local percent=0
3688         local have_ppr=false
3689         $LCTL get_param $proc_osc0/rpc_stats |
3690                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3691                         # skip lines until we are at the RPC histogram data
3692                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3693                         $have_ppr || continue
3694
3695                         # we only want the percent stat for < 16 pages
3696                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3697
3698                         percent=$((percent + WPCT))
3699                         if [[ $percent -gt 15 ]]; then
3700                                 error "less than 16-pages write RPCs" \
3701                                       "$percent% > 15%"
3702                                 break
3703                         fi
3704                 done
3705         rm -rf $TDIR
3706 }
3707 run_test 42e "verify sub-RPC writes are not done synchronously"
3708
3709 test_43() {
3710         test_mkdir -p $DIR/$tdir
3711         cp -p /bin/ls $DIR/$tdir/$tfile
3712         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3713         pid=$!
3714         # give multiop a chance to open
3715         sleep 1
3716
3717         $DIR/$tdir/$tfile && error || true
3718         kill -USR1 $pid
3719 }
3720 run_test 43 "execution of file opened for write should return -ETXTBSY"
3721
3722 test_43a() {
3723         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3724         test_mkdir -p $DIR/$tdir
3725         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3726                         cp -p multiop $DIR/$tdir/multiop
3727         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3728                         return 1
3729         MULTIOP_PID=$!
3730         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3731         kill -USR1 $MULTIOP_PID || return 2
3732         wait $MULTIOP_PID || return 3
3733         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3734 }
3735 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3736
3737 test_43b() {
3738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3739         test_mkdir -p $DIR/$tdir
3740         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3741                         cp -p multiop $DIR/$tdir/multiop
3742         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3743                         return 1
3744         MULTIOP_PID=$!
3745         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3746         kill -USR1 $MULTIOP_PID || return 2
3747         wait $MULTIOP_PID || return 3
3748         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3749 }
3750 run_test 43b "truncate of file being executed should return -ETXTBSY"
3751
3752 test_43c() {
3753         local testdir="$DIR/$tdir"
3754         test_mkdir -p $DIR/$tdir
3755         cp $SHELL $testdir/
3756         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3757                 ( cd $testdir && md5sum -c)
3758 }
3759 run_test 43c "md5sum of copy into lustre========================"
3760
3761 test_44() {
3762         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3763         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3764         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3765 }
3766 run_test 44 "zero length read from a sparse stripe ============="
3767
3768 test_44a() {
3769         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3770                 awk '{ print $2 }')
3771         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3772         [[ $nstripe -gt $OSTCOUNT ]] &&
3773             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3774             return
3775         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3776                 awk '{ print $2 }')
3777         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3778                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3779                         awk '{ print $2 }')
3780         fi
3781
3782         OFFSETS="0 $((stride/2)) $((stride-1))"
3783         for offset in $OFFSETS; do
3784                 for i in $(seq 0 $((nstripe-1))); do
3785                         local GLOBALOFFSETS=""
3786                         # size in Bytes
3787                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3788                         local myfn=$DIR/d44a-$size
3789                         echo "--------writing $myfn at $size"
3790                         ll_sparseness_write $myfn $size ||
3791                                 error "ll_sparseness_write"
3792                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3793                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3794                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3795
3796                         for j in $(seq 0 $((nstripe-1))); do
3797                                 # size in Bytes
3798                                 size=$((((j + $nstripe )*$stride + $offset)))
3799                                 ll_sparseness_write $myfn $size ||
3800                                         error "ll_sparseness_write"
3801                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3802                         done
3803                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3804                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3805                         rm -f $myfn
3806                 done
3807         done
3808 }
3809 run_test 44a "test sparse pwrite ==============================="
3810
3811 dirty_osc_total() {
3812         tot=0
3813         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3814                 tot=$(($tot + $d))
3815         done
3816         echo $tot
3817 }
3818 do_dirty_record() {
3819         before=`dirty_osc_total`
3820         echo executing "\"$*\""
3821         eval $*
3822         after=`dirty_osc_total`
3823         echo before $before, after $after
3824 }
3825 test_45() {
3826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3827         f="$DIR/f45"
3828         # Obtain grants from OST if it supports it
3829         echo blah > ${f}_grant
3830         stop_writeback
3831         sync
3832         do_dirty_record "echo blah > $f"
3833         [[ $before -eq $after ]] && error "write wasn't cached"
3834         do_dirty_record "> $f"
3835         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3836         do_dirty_record "echo blah > $f"
3837         [[ $before -eq $after ]] && error "write wasn't cached"
3838         do_dirty_record "sync"
3839         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3840         do_dirty_record "echo blah > $f"
3841         [[ $before -eq $after ]] && error "write wasn't cached"
3842         do_dirty_record "cancel_lru_locks osc"
3843         [[ $before -gt $after ]] ||
3844                 error "lock cancellation didn't lower dirty count"
3845         start_writeback
3846 }
3847 run_test 45 "osc io page accounting ============================"
3848
3849 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3850 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3851 # objects offset and an assert hit when an rpc was built with 1023's mapped
3852 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3853 test_46() {
3854         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3855         f="$DIR/f46"
3856         stop_writeback
3857         sync
3858         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3859         sync
3860         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3861         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3862         sync
3863         start_writeback
3864 }
3865 run_test 46 "dirtying a previously written page ================"
3866
3867 # test_47 is removed "Device nodes check" is moved to test_28
3868
3869 test_48a() { # bug 2399
3870         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3871         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3872                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3873                 return
3874         test_mkdir $DIR/$tdir
3875         cd $DIR/$tdir
3876         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3877         test_mkdir $DIR/$tdir || error "recreate directory failed"
3878         touch foo || error "'touch foo' failed after recreating cwd"
3879         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
3880         touch .foo || error "'touch .foo' failed after recreating cwd"
3881         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3882         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3883         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3884         cd . || error "'cd .' failed after recreating cwd"
3885         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3886         rmdir . && error "'rmdir .' worked after recreating cwd"
3887         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3888         cd .. || error "'cd ..' failed after recreating cwd"
3889 }
3890 run_test 48a "Access renamed working dir (should return errors)="
3891
3892 test_48b() { # bug 2399
3893         rm -rf $DIR/$tdir
3894         test_mkdir $DIR/$tdir
3895         cd $DIR/$tdir
3896         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3897         touch foo && error "'touch foo' worked after removing cwd"
3898         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3899         touch .foo && error "'touch .foo' worked after removing cwd"
3900         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3901         ls . > /dev/null && error "'ls .' worked after removing cwd"
3902         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3903         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3904         test_mkdir . && error "'mkdir .' worked after removing cwd"
3905         rmdir . && error "'rmdir .' worked after removing cwd"
3906         ln -s . foo && error "'ln -s .' worked after removing cwd"
3907         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3908 }
3909 run_test 48b "Access removed working dir (should return errors)="
3910
3911 test_48c() { # bug 2350
3912         #lctl set_param debug=-1
3913         #set -vx
3914         rm -rf $DIR/$tdir
3915         test_mkdir -p $DIR/$tdir/dir
3916         cd $DIR/$tdir/dir
3917         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3918         $TRACE touch foo && error "touch foo worked after removing cwd"
3919         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3920         touch .foo && error "touch .foo worked after removing cwd"
3921         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3922         $TRACE ls . && error "'ls .' worked after removing cwd"
3923         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3924         is_patchless || ( $TRACE cd . &&
3925                         error "'cd .' worked after removing cwd" )
3926         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3927         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3928         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3929         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3930 }
3931 run_test 48c "Access removed working subdir (should return errors)"
3932
3933 test_48d() { # bug 2350
3934         #lctl set_param debug=-1
3935         #set -vx
3936         rm -rf $DIR/$tdir
3937         test_mkdir -p $DIR/$tdir/dir
3938         cd $DIR/$tdir/dir
3939         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3940         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3941         $TRACE touch foo && error "'touch foo' worked after removing parent"
3942         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3943         touch .foo && error "'touch .foo' worked after removing parent"
3944         test_mkdir .foo && error "mkdir .foo worked after removing parent"
3945         $TRACE ls . && error "'ls .' worked after removing parent"
3946         $TRACE ls .. && error "'ls ..' worked after removing parent"
3947         is_patchless || ( $TRACE cd . &&
3948                         error "'cd .' worked after recreate parent" )
3949         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3950         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3951         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3952         is_patchless || ( $TRACE cd .. &&
3953                         error "'cd ..' worked after removing parent" || true )
3954 }
3955 run_test 48d "Access removed parent subdir (should return errors)"
3956
3957 test_48e() { # bug 4134
3958         #lctl set_param debug=-1
3959         #set -vx
3960         rm -rf $DIR/$tdir
3961         test_mkdir -p $DIR/$tdir/dir
3962         cd $DIR/$tdir/dir
3963         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3964         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3965         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3966         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3967         # On a buggy kernel addition of "touch foo" after cd .. will
3968         # produce kernel oops in lookup_hash_it
3969         touch ../foo && error "'cd ..' worked after recreate parent"
3970         cd $DIR
3971         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3972 }
3973 run_test 48e "Access to recreated parent subdir (should return errors)"
3974
3975 test_49() { # LU-1030
3976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3977         # get ost1 size - lustre-OST0000
3978         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3979                 awk '{ print $4 }')
3980         # write 800M at maximum
3981         [[ $ost1_size -lt 2 ]] && ost1_size=2
3982         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3983
3984         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3985         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3986         local dd_pid=$!
3987
3988         # change max_pages_per_rpc while writing the file
3989         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3990         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3991         # loop until dd process exits
3992         while ps ax -opid | grep -wq $dd_pid; do
3993                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3994                 sleep $((RANDOM % 5 + 1))
3995         done
3996         # restore original max_pages_per_rpc
3997         $LCTL set_param $osc1_mppc=$orig_mppc
3998         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3999 }
4000 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4001
4002 test_50() {
4003         # bug 1485
4004         test_mkdir $DIR/$tdir
4005         cd $DIR/$tdir
4006         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4007 }
4008 run_test 50 "special situations: /proc symlinks  ==============="
4009
4010 test_51a() {    # was test_51
4011         # bug 1516 - create an empty entry right after ".." then split dir
4012         test_mkdir -c1 $DIR/$tdir
4013         touch $DIR/$tdir/foo
4014         $MCREATE $DIR/$tdir/bar
4015         rm $DIR/$tdir/foo
4016         createmany -m $DIR/$tdir/longfile 201
4017         FNUM=202
4018         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4019                 $MCREATE $DIR/$tdir/longfile$FNUM
4020                 FNUM=$(($FNUM + 1))
4021                 echo -n "+"
4022         done
4023         echo
4024         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4025 }
4026 run_test 51a "special situations: split htree with empty entry =="
4027
4028 export NUMTEST=70000
4029 test_51b() {
4030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4031         local BASE=$DIR/d${base}.${TESTSUITE}
4032
4033         # cleanup the directory
4034         rm -fr $BASE
4035
4036         test_mkdir -p -c1 $BASE
4037
4038         $LFS df
4039         $LFS df -i
4040         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4041         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4042         [[ $numfree -lt 21000 ]] &&
4043                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4044                 return
4045
4046         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4047                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4048
4049         # need to check free space for the directories as well
4050         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4051         numfree=$((blkfree / 4))
4052         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4053                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4054
4055         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4056         {
4057                 $LFS df
4058                 $LFS df -i
4059                 echo "failed" > $BASE/fnum
4060                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4061         }
4062 }
4063 run_test 51b "exceed 64k subdirectory nlink limit"
4064
4065 test_51ba() { # LU-993
4066         local BASE=$DIR/d${base}.${TESTSUITE}
4067         # unlink all but 100 subdirectories, then check it still works
4068         local LEFT=100
4069         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4070
4071         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4072         local DELETE=$((NUMTEST - LEFT))
4073
4074         # continue on to run this test even if 51b didn't finish,
4075         # just to delete the many subdirectories created.
4076         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4077
4078         # for ldiskfs the nlink count should be 1, but this is OSD specific
4079         # and so this is listed for informational purposes only
4080         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4081         unlinkmany -d $BASE/d $DELETE
4082         RC=$?
4083
4084         if [ $RC -ne 0 ]; then
4085                 if [ "$NUMPREV" == "failed" ]; then
4086                         skip "previous setup failed"
4087                         return 0
4088                 else
4089                         error "unlink of first $DELETE subdirs failed"
4090                         return $RC
4091                 fi
4092         fi
4093
4094         echo "nlink between: $(stat -c %h $BASE)"
4095         # trim the first line of ls output
4096         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4097         [ $FOUND -ne $LEFT ] &&
4098                 error "can't find subdirs: found only $FOUND/$LEFT"
4099
4100         unlinkmany -d $BASE/d $DELETE $LEFT ||
4101                 error "unlink of second $LEFT subdirs failed"
4102         # regardless of whether the backing filesystem tracks nlink accurately
4103         # or not, the nlink count shouldn't be more than "." and ".." here
4104         local AFTER=$(stat -c %h $BASE)
4105         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4106                 echo "nlink after: $AFTER"
4107 }
4108 run_test 51ba "verify nlink for many subdirectory cleanup"
4109
4110 test_51d() {
4111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4112         [[ $OSTCOUNT -lt 3 ]] &&
4113                 skip_env "skipping test with few OSTs" && return
4114         test_mkdir -p $DIR/$tdir
4115         createmany -o $DIR/$tdir/t- 1000
4116         $GETSTRIPE $DIR/$tdir > $TMP/files
4117         for N in $(seq 0 $((OSTCOUNT - 1))); do
4118                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4119                         END { printf("%0.0f", objs) }' $TMP/files)
4120                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4121                         '($1 == '$N') { objs += 1 } \
4122                         END { printf("%0.0f", objs) }')
4123                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4124         done
4125         unlinkmany $DIR/$tdir/t- 1000
4126
4127         NLAST=0
4128         for N in $(seq 1 $((OSTCOUNT - 1))); do
4129                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4130                         error "OST $N has less objects vs OST $NLAST" \
4131                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4132                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4133                         error "OST $N has less objects vs OST $NLAST" \
4134                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4135
4136                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4137                         error "OST $N has less #0 objects vs OST $NLAST" \
4138                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4139                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4140                         error "OST $N has less #0 objects vs OST $NLAST" \
4141                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4142                 NLAST=$N
4143         done
4144 }
4145 run_test 51d "check object distribution ===================="
4146
4147 test_51e() {
4148         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4149                 skip "Only applicable to ldiskfs-based MDTs"
4150                 return
4151         fi
4152
4153         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4154         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4155
4156         touch $DIR/$tdir/d0/foo
4157         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4158                 error "file exceed 65000 nlink limit!"
4159         unlinkmany $DIR/$tdir/d0/f- 65001
4160         return 0
4161 }
4162 run_test 51e "check file nlink limit"
4163
4164 test_52a() {
4165         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4166         test_mkdir -p $DIR/$tdir
4167         touch $DIR/$tdir/foo
4168         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4169         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4170         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4171         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4172         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4173                                         error "link worked"
4174         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4175         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4176         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4177                                                      error "lsattr"
4178         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4179         cp -r $DIR/$tdir /tmp/
4180         rm -fr $DIR/$tdir || error "cleanup rm failed"
4181 }
4182 run_test 52a "append-only flag test (should return errors) ====="
4183
4184 test_52b() {
4185         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4186         test_mkdir -p $DIR/$tdir
4187         touch $DIR/$tdir/foo
4188         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4189         cat test > $DIR/$tdir/foo && error "cat test worked"
4190         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4191         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4192         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4193                                         error "link worked"
4194         echo foo >> $DIR/$tdir/foo && error "echo worked"
4195         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4196         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4197         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4198         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4199                                                         error "lsattr"
4200         chattr -i $DIR/$tdir/foo || error "chattr failed"
4201
4202         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4203 }
4204 run_test 52b "immutable flag test (should return errors) ======="
4205
4206 test_53() {
4207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4208         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4209         remote_ost_nodsh && skip "remote OST with nodsh" && return
4210
4211         local param
4212         local param_seq
4213         local ostname
4214         local mds_last
4215         local mds_last_seq
4216         local ost_last
4217         local ost_last_seq
4218         local ost_last_id
4219         local ostnum
4220         local node
4221         local found=false
4222         local support_last_seq=true
4223
4224         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4225                 support_last_seq=false
4226
4227         # only test MDT0000
4228         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4229         local value
4230         for value in $(do_facet $SINGLEMDS \
4231                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4232                 param=$(echo ${value[0]} | cut -d "=" -f1)
4233                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4234
4235                 if $support_last_seq; then
4236                         param_seq=$(echo $param |
4237                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4238                         mds_last_seq=$(do_facet $SINGLEMDS \
4239                                        $LCTL get_param -n $param_seq)
4240                 fi
4241                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4242
4243                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4244                 node=$(facet_active_host ost$((ostnum+1)))
4245                 param="obdfilter.$ostname.last_id"
4246                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4247                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4248                         ost_last_id=$ost_last
4249
4250                         if $support_last_seq; then
4251                                 ost_last_id=$(echo $ost_last |
4252                                               awk -F':' '{print $2}' |
4253                                               sed -e "s/^0x//g")
4254                                 ost_last_seq=$(echo $ost_last |
4255                                                awk -F':' '{print $1}')
4256                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4257                         fi
4258
4259                         if [[ $ost_last_id != $mds_last ]]; then
4260                                 error "$ost_last_id != $mds_last"
4261                         else
4262                                 found=true
4263                                 break
4264                         fi
4265                 done
4266         done
4267         $found || error "can not match last_seq/last_id for $mdtosc"
4268         return 0
4269 }
4270 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4271
4272 test_54a() {
4273         $SOCKETSERVER $DIR/socket ||
4274                 error "$SOCKETSERVER $DIR/socket failed: $?"
4275         $SOCKETCLIENT $DIR/socket ||
4276                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4277         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4278 }
4279 run_test 54a "unix domain socket test =========================="
4280
4281 test_54b() {
4282         f="$DIR/f54b"
4283         mknod $f c 1 3
4284         chmod 0666 $f
4285         dd if=/dev/zero of=$f bs=$(page_size) count=1
4286 }
4287 run_test 54b "char device works in lustre ======================"
4288
4289 find_loop_dev() {
4290         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4291         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4292         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4293
4294         for i in $(seq 3 7); do
4295                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4296                 LOOPDEV=$LOOPBASE$i
4297                 LOOPNUM=$i
4298                 break
4299         done
4300 }
4301
4302 cleanup_54c() {
4303         loopdev="$DIR/loop54c"
4304
4305         trap 0
4306         $UMOUNT $DIR/$tdir || rc=$?
4307         losetup -d $loopdev || true
4308         losetup -d $LOOPDEV || true
4309         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4310         return $rc
4311 }
4312
4313 test_54c() {
4314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4315         loopdev="$DIR/loop54c"
4316
4317         find_loop_dev
4318         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4319         trap cleanup_54c EXIT
4320         mknod $loopdev b 7 $LOOPNUM
4321         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4322         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4323         losetup $loopdev $DIR/$tfile ||
4324                 error "can't set up $loopdev for $DIR/$tfile"
4325         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4326         test_mkdir -p $DIR/$tdir
4327         mount -t ext2 $loopdev $DIR/$tdir ||
4328                 error "error mounting $loopdev on $DIR/$tdir"
4329         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4330                 error "dd write"
4331         df $DIR/$tdir
4332         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4333                 error "dd read"
4334         cleanup_54c
4335 }
4336 run_test 54c "block device works in lustre ====================="
4337
4338 test_54d() {
4339         f="$DIR/f54d"
4340         string="aaaaaa"
4341         mknod $f p
4342         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4343 }
4344 run_test 54d "fifo device works in lustre ======================"
4345
4346 test_54e() {
4347         f="$DIR/f54e"
4348         string="aaaaaa"
4349         cp -aL /dev/console $f
4350         echo $string > $f || error "echo $string to $f failed"
4351 }
4352 run_test 54e "console/tty device works in lustre ======================"
4353
4354 #The test_55 used to be iopen test and it was removed by bz#24037.
4355 #run_test 55 "check iopen_connect_dentry() ======================"
4356
4357 test_56a() {    # was test_56
4358         rm -rf $DIR/$tdir
4359         $SETSTRIPE -d $DIR
4360         test_mkdir -p $DIR/$tdir/dir
4361         NUMFILES=3
4362         NUMFILESx2=$(($NUMFILES * 2))
4363         for i in $(seq 1 $NUMFILES); do
4364                 touch $DIR/$tdir/file$i
4365                 touch $DIR/$tdir/dir/file$i
4366         done
4367
4368         # test lfs getstripe with --recursive
4369         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4370         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4371                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4372         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4373         [[ $FILENUM -eq $NUMFILES ]] ||
4374                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4375         echo "$GETSTRIPE --recursive passed."
4376
4377         # test lfs getstripe with file instead of dir
4378         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4379         [[ $FILENUM -eq 1 ]] ||
4380                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4381         echo "$GETSTRIPE file1 passed."
4382
4383         #test lfs getstripe with --verbose
4384         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4385                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4386                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4387         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4388                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4389         echo "$GETSTRIPE --verbose passed."
4390
4391         #test lfs getstripe with --obd
4392         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4393                 grep -q "unknown obduuid" ||
4394                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4395
4396         [[ $OSTCOUNT -lt 2 ]] &&
4397                 skip_env "skipping other $GETSTRIPE --obd test" && return
4398
4399         OSTIDX=1
4400         OBDUUID=$(ostuuid_from_index $OSTIDX)
4401         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4402         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4403         [[ $FOUND -eq $FILENUM ]] ||
4404                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4405         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4406                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4407                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4408                 error "$GETSTRIPE --obd: should not show file on other obd"
4409         echo "$GETSTRIPE --obd passed"
4410 }
4411 run_test 56a "check $GETSTRIPE"
4412
4413 NUMFILES=3
4414 NUMDIRS=3
4415 setup_56() {
4416         local LOCAL_NUMFILES="$1"
4417         local LOCAL_NUMDIRS="$2"
4418         local MKDIR_PARAMS="$3"
4419         local DIR_STRIPE_PARAMS="$4"
4420
4421         if [ ! -d "$TDIR" ] ; then
4422                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4423                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4424                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4425                         touch $TDIR/file$i
4426                 done
4427                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4428                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4429                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4430                                 touch $TDIR/dir$i/file$j
4431                         done
4432                 done
4433         fi
4434 }
4435
4436 setup_56_special() {
4437         LOCAL_NUMFILES=$1
4438         LOCAL_NUMDIRS=$2
4439         setup_56 $1 $2
4440         if [ ! -e "$TDIR/loop1b" ] ; then
4441                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4442                         mknod $TDIR/loop${i}b b 7 $i
4443                         mknod $TDIR/null${i}c c 1 3
4444                         ln -s $TDIR/file1 $TDIR/link${i}l
4445                 done
4446                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4447                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4448                         mknod $TDIR/dir$i/null${i}c c 1 3
4449                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4450                 done
4451         fi
4452 }
4453
4454 test_56g() {
4455         $SETSTRIPE -d $DIR
4456
4457         TDIR=$DIR/${tdir}g
4458         setup_56 $NUMFILES $NUMDIRS
4459
4460         EXPECTED=$(($NUMDIRS + 2))
4461         # test lfs find with -name
4462         for i in $(seq 1 $NUMFILES) ; do
4463                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4464                 [ $NUMS -eq $EXPECTED ] ||
4465                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4466                               "found $NUMS, expected $EXPECTED"
4467         done
4468 }
4469 run_test 56g "check lfs find -name ============================="
4470
4471 test_56h() {
4472         $SETSTRIPE -d $DIR
4473
4474         TDIR=$DIR/${tdir}g
4475         setup_56 $NUMFILES $NUMDIRS
4476
4477         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4478         # test lfs find with ! -name
4479         for i in $(seq 1 $NUMFILES) ; do
4480                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4481                 [ $NUMS -eq $EXPECTED ] ||
4482                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4483                               "found $NUMS, expected $EXPECTED"
4484         done
4485 }
4486 run_test 56h "check lfs find ! -name ============================="
4487
4488 test_56i() {
4489        tdir=${tdir}i
4490        test_mkdir -p $DIR/$tdir
4491        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4492        CMD="$LFIND -ost $UUID $DIR/$tdir"
4493        OUT=$($CMD)
4494        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4495 }
4496 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4497
4498 test_56j() {
4499         TDIR=$DIR/${tdir}g
4500         setup_56_special $NUMFILES $NUMDIRS
4501
4502         EXPECTED=$((NUMDIRS + 1))
4503         CMD="$LFIND -type d $TDIR"
4504         NUMS=$($CMD | wc -l)
4505         [ $NUMS -eq $EXPECTED ] ||
4506                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4507 }
4508 run_test 56j "check lfs find -type d ============================="
4509
4510 test_56k() {
4511         TDIR=$DIR/${tdir}g
4512         setup_56_special $NUMFILES $NUMDIRS
4513
4514         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4515         CMD="$LFIND -type f $TDIR"
4516         NUMS=$($CMD | wc -l)
4517         [ $NUMS -eq $EXPECTED ] ||
4518                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4519 }
4520 run_test 56k "check lfs find -type f ============================="
4521
4522 test_56l() {
4523         TDIR=$DIR/${tdir}g
4524         setup_56_special $NUMFILES $NUMDIRS
4525
4526         EXPECTED=$((NUMDIRS + NUMFILES))
4527         CMD="$LFIND -type b $TDIR"
4528         NUMS=$($CMD | wc -l)
4529         [ $NUMS -eq $EXPECTED ] ||
4530                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4531 }
4532 run_test 56l "check lfs find -type b ============================="
4533
4534 test_56m() {
4535         TDIR=$DIR/${tdir}g
4536         setup_56_special $NUMFILES $NUMDIRS
4537
4538         EXPECTED=$((NUMDIRS + NUMFILES))
4539         CMD="$LFIND -type c $TDIR"
4540         NUMS=$($CMD | wc -l)
4541         [ $NUMS -eq $EXPECTED ] ||
4542                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4543 }
4544 run_test 56m "check lfs find -type c ============================="
4545
4546 test_56n() {
4547         TDIR=$DIR/${tdir}g
4548         setup_56_special $NUMFILES $NUMDIRS
4549
4550         EXPECTED=$((NUMDIRS + NUMFILES))
4551         CMD="$LFIND -type l $TDIR"
4552         NUMS=$($CMD | wc -l)
4553         [ $NUMS -eq $EXPECTED ] ||
4554                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4555 }
4556 run_test 56n "check lfs find -type l ============================="
4557
4558 test_56o() {
4559         TDIR=$DIR/${tdir}o
4560         setup_56 $NUMFILES $NUMDIRS
4561         utime $TDIR/file1 > /dev/null || error "utime (1)"
4562         utime $TDIR/file2 > /dev/null || error "utime (2)"
4563         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4564         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4565         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4566         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4567
4568         EXPECTED=4
4569         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4570         [ $NUMS -eq $EXPECTED ] || \
4571                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4572
4573         EXPECTED=12
4574         CMD="$LFIND -mtime 0 $TDIR"
4575         NUMS=$($CMD | wc -l)
4576         [ $NUMS -eq $EXPECTED ] ||
4577                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4578 }
4579 run_test 56o "check lfs find -mtime for old files =========================="
4580
4581 test_56p() {
4582         [ $RUNAS_ID -eq $UID ] &&
4583                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4584
4585         TDIR=$DIR/${tdir}p
4586         setup_56 $NUMFILES $NUMDIRS
4587
4588         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4589         EXPECTED=$NUMFILES
4590         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4591         NUMS=$($CMD | wc -l)
4592         [ $NUMS -eq $EXPECTED ] || \
4593                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4594
4595         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4596         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4597         NUMS=$($CMD | wc -l)
4598         [ $NUMS -eq $EXPECTED ] || \
4599                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4600 }
4601 run_test 56p "check lfs find -uid and ! -uid ==============================="
4602
4603 test_56q() {
4604         [ $RUNAS_ID -eq $UID ] &&
4605                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4606
4607         TDIR=$DIR/${tdir}q
4608         setup_56 $NUMFILES $NUMDIRS
4609
4610         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4611
4612         EXPECTED=$NUMFILES
4613         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4614         NUMS=$($CMD | wc -l)
4615         [ $NUMS -eq $EXPECTED ] ||
4616                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4617
4618         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4619         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4620         NUMS=$($CMD | wc -l)
4621         [ $NUMS -eq $EXPECTED ] ||
4622                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4623 }
4624 run_test 56q "check lfs find -gid and ! -gid ==============================="
4625
4626 test_56r() {
4627         TDIR=$DIR/${tdir}r
4628         setup_56 $NUMFILES $NUMDIRS
4629
4630         EXPECTED=12
4631         CMD="$LFIND -size 0 -type f $TDIR"
4632         NUMS=$($CMD | wc -l)
4633         [ $NUMS -eq $EXPECTED ] ||
4634                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4635         EXPECTED=0
4636         CMD="$LFIND ! -size 0 -type f $TDIR"
4637         NUMS=$($CMD | wc -l)
4638         [ $NUMS -eq $EXPECTED ] ||
4639                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4640         echo "test" > $TDIR/$tfile
4641         echo "test2" > $TDIR/$tfile.2 && sync
4642         EXPECTED=1
4643         CMD="$LFIND -size 5 -type f $TDIR"
4644         NUMS=$($CMD | wc -l)
4645         [ $NUMS -eq $EXPECTED ] ||
4646                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4647         EXPECTED=1
4648         CMD="$LFIND -size +5 -type f $TDIR"
4649         NUMS=$($CMD | wc -l)
4650         [ $NUMS -eq $EXPECTED ] ||
4651                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4652         EXPECTED=2
4653         CMD="$LFIND -size +0 -type f $TDIR"
4654         NUMS=$($CMD | wc -l)
4655         [ $NUMS -eq $EXPECTED ] ||
4656                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4657         EXPECTED=2
4658         CMD="$LFIND ! -size -5 -type f $TDIR"
4659         NUMS=$($CMD | wc -l)
4660         [ $NUMS -eq $EXPECTED ] ||
4661                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4662         EXPECTED=12
4663         CMD="$LFIND -size -5 -type f $TDIR"
4664         NUMS=$($CMD | wc -l)
4665         [ $NUMS -eq $EXPECTED ] ||
4666                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4667 }
4668 run_test 56r "check lfs find -size works =========================="
4669
4670 test_56s() { # LU-611
4671         TDIR=$DIR/${tdir}s
4672         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4673
4674         if [[ $OSTCOUNT -gt 1 ]]; then
4675                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4676                 ONESTRIPE=4
4677                 EXTRA=4
4678         else
4679                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4680                 EXTRA=0
4681         fi
4682
4683         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4684         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4685         NUMS=$($CMD | wc -l)
4686         [ $NUMS -eq $EXPECTED ] || {
4687                 $GETSTRIPE -R $TDIR
4688                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4689         }
4690
4691         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4692         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4693         NUMS=$($CMD | wc -l)
4694         [ $NUMS -eq $EXPECTED ] || {
4695                 $GETSTRIPE -R $TDIR
4696                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4697         }
4698
4699         EXPECTED=$ONESTRIPE
4700         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4701         NUMS=$($CMD | wc -l)
4702         [ $NUMS -eq $EXPECTED ] || {
4703                 $GETSTRIPE -R $TDIR
4704                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4705         }
4706
4707         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4708         NUMS=$($CMD | wc -l)
4709         [ $NUMS -eq $EXPECTED ] || {
4710                 $GETSTRIPE -R $TDIR
4711                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4712         }
4713
4714         EXPECTED=0
4715         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4716         NUMS=$($CMD | wc -l)
4717         [ $NUMS -eq $EXPECTED ] || {
4718                 $GETSTRIPE -R $TDIR
4719                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4720         }
4721 }
4722 run_test 56s "check lfs find -stripe-count works"
4723
4724 test_56t() { # LU-611
4725         TDIR=$DIR/${tdir}t
4726         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4727
4728         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4729
4730         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4731         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4732         NUMS=$($CMD | wc -l)
4733         [ $NUMS -eq $EXPECTED ] ||
4734                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4735
4736         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4737         NUMS=$($CMD | wc -l)
4738         [ $NUMS -eq $EXPECTED ] ||
4739                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4740
4741         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4742         CMD="$LFIND -stripe-size +200k -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 -640k -type f $TDIR"
4748         NUMS=$($CMD | wc -l)
4749         [ $NUMS -eq $EXPECTED ] ||
4750                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4751
4752         EXPECTED=4
4753         CMD="$LFIND -stripe-size 256k -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 -320k -type f $TDIR"
4759         NUMS=$($CMD | wc -l)
4760         [ $NUMS -eq $EXPECTED ] ||
4761                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4762
4763         EXPECTED=0
4764         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4765         NUMS=$($CMD | wc -l)
4766         [ $NUMS -eq $EXPECTED ] ||
4767                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4768 }
4769 run_test 56t "check lfs find -stripe-size works"
4770
4771 test_56u() { # LU-611
4772         TDIR=$DIR/${tdir}u
4773         setup_56 $NUMFILES $NUMDIRS "-i 0"
4774
4775         if [[ $OSTCOUNT -gt 1 ]]; then
4776                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4777                 ONESTRIPE=4
4778         else
4779                 ONESTRIPE=0
4780         fi
4781
4782         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4783         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4784         NUMS=$($CMD | wc -l)
4785         [ $NUMS -eq $EXPECTED ] ||
4786                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4787
4788         EXPECTED=$ONESTRIPE
4789         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4790         NUMS=$($CMD | wc -l)
4791         [ $NUMS -eq $EXPECTED ] ||
4792                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4793
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=0
4800         # This should produce an error and not return any files
4801         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4802         NUMS=$($CMD 2>/dev/null | wc -l)
4803         [ $NUMS -eq $EXPECTED ] ||
4804                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4805
4806         if [[ $OSTCOUNT -gt 1 ]]; then
4807                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4808                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4809                 NUMS=$($CMD | wc -l)
4810                 [ $NUMS -eq $EXPECTED ] ||
4811                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4812         fi
4813 }
4814 run_test 56u "check lfs find -stripe-index works"
4815
4816 test_56v() {
4817     local MDT_IDX=0
4818
4819     TDIR=$DIR/${tdir}v
4820     rm -rf $TDIR
4821     setup_56 $NUMFILES $NUMDIRS
4822
4823     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4824     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4825
4826     for file in $($LFIND -mdt $UUID $TDIR); do
4827         file_mdt_idx=$($GETSTRIPE -M $file)
4828         [ $file_mdt_idx -eq $MDT_IDX ] ||
4829             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4830     done
4831 }
4832 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4833
4834 test_56w() {
4835         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4836                 return
4837         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4838         TDIR=$DIR/${tdir}w
4839
4840     rm -rf $TDIR || error "remove $TDIR failed"
4841     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4842
4843     local stripe_size
4844     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4845         error "$GETSTRIPE -S -d $TDIR failed"
4846     stripe_size=${stripe_size%% *}
4847
4848     local file_size=$((stripe_size * OSTCOUNT))
4849     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4850     local required_space=$((file_num * file_size))
4851     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4852     [[ $free_space -le $((required_space / 1024)) ]] &&
4853         skip_env "need at least $required_space bytes free space," \
4854                  "have $free_space kbytes" && return
4855
4856     local dd_bs=65536
4857     local dd_count=$((file_size / dd_bs))
4858
4859     # write data into the files
4860     local i
4861     local j
4862     local file
4863     for i in $(seq 1 $NUMFILES); do
4864         file=$TDIR/file$i
4865         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4866             error "write data into $file failed"
4867     done
4868     for i in $(seq 1 $NUMDIRS); do
4869         for j in $(seq 1 $NUMFILES); do
4870             file=$TDIR/dir$i/file$j
4871             yes | dd bs=$dd_bs count=$dd_count of=$file \
4872                 >/dev/null 2>&1 ||
4873                 error "write data into $file failed"
4874         done
4875     done
4876
4877     local expected=-1
4878     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4879
4880     # lfs_migrate file
4881     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4882     echo "$cmd"
4883     eval $cmd || error "$cmd failed"
4884
4885     check_stripe_count $TDIR/file1 $expected
4886
4887         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4888         then
4889                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4890                 # OST 1 if it is on OST 0. This file is small enough to
4891                 # be on only one stripe.
4892                 file=$TDIR/migr_1_ost
4893                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4894                         error "write data into $file failed"
4895                 local obdidx=$($LFS getstripe -i $file)
4896                 local oldmd5=$(md5sum $file)
4897                 local newobdidx=0
4898                 [[ $obdidx -eq 0 ]] && newobdidx=1
4899                 cmd="$LFS migrate -i $newobdidx $file"
4900                 echo $cmd
4901                 eval $cmd || error "$cmd failed"
4902                 local realobdix=$($LFS getstripe -i $file)
4903                 local newmd5=$(md5sum $file)
4904                 [[ $newobdidx -ne $realobdix ]] &&
4905                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4906                 [[ "$oldmd5" != "$newmd5" ]] &&
4907                         error "md5sum differ: $oldmd5, $newmd5"
4908         fi
4909
4910     # lfs_migrate dir
4911     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4912     echo "$cmd"
4913     eval $cmd || error "$cmd failed"
4914
4915     for j in $(seq 1 $NUMFILES); do
4916         check_stripe_count $TDIR/dir1/file$j $expected
4917     done
4918
4919     # lfs_migrate works with lfs find
4920     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4921          $LFS_MIGRATE -y -c $expected"
4922     echo "$cmd"
4923     eval $cmd || error "$cmd failed"
4924
4925     for i in $(seq 2 $NUMFILES); do
4926         check_stripe_count $TDIR/file$i $expected
4927     done
4928     for i in $(seq 2 $NUMDIRS); do
4929         for j in $(seq 1 $NUMFILES); do
4930             check_stripe_count $TDIR/dir$i/file$j $expected
4931         done
4932     done
4933 }
4934 run_test 56w "check lfs_migrate -c stripe_count works"
4935
4936 test_56x() {
4937         check_swap_layouts_support && return 0
4938         [[ $OSTCOUNT -lt 2 ]] &&
4939                 skip_env "need 2 OST, skipping test" && return
4940
4941         local dir0=$DIR/$tdir/$testnum
4942         test_mkdir -p $dir0 || error "creating dir $dir0"
4943
4944         local ref1=/etc/passwd
4945         local file1=$dir0/file1
4946
4947         $SETSTRIPE -c 2 $file1
4948         cp $ref1 $file1
4949         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4950         stripe=$($GETSTRIPE -c $file1)
4951         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4952         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4953
4954         # clean up
4955         rm -f $file1
4956 }
4957 run_test 56x "lfs migration support"
4958
4959 test_56y() {
4960         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4961                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
4962                 return
4963
4964         local res=""
4965         local dir0=$DIR/$tdir/$testnum
4966         test_mkdir -p $dir0 || error "creating dir $dir0"
4967         local f1=$dir0/file1
4968         local f2=$dir0/file2
4969
4970         touch $f1 || error "creating std file $f1"
4971         $MULTIOP $f2 H2c || error "creating released file $f2"
4972
4973         # a directory can be raid0, so ask only for files
4974         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4975         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4976
4977         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4978         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4979
4980         # only files can be released, so no need to force file search
4981         res=$($LFIND $dir0 -L released)
4982         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4983
4984         res=$($LFIND $dir0 \! -L released)
4985         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4986
4987 }
4988 run_test 56y "lfs find -L raid0|released"
4989
4990 test_56z() { # LU-4824
4991         # This checks to make sure 'lfs find' continues after errors
4992         # There are two classes of errors that should be caught:
4993         # - If multiple paths are provided, all should be searched even if one
4994         #   errors out
4995         # - If errors are encountered during the search, it should not terminate
4996         #   early
4997         local i
4998         test_mkdir $DIR/$tdir
4999         for i in d{0..9}; do
5000                 test_mkdir $DIR/$tdir/$i
5001         done
5002         touch $DIR/$tdir/d{0..9}/$tfile
5003         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5004                 error "$LFS find did not return an error"
5005         # Make a directory unsearchable. This should NOT be the last entry in
5006         # directory order.  Arbitrarily pick the 6th entry
5007         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5008         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5009         # The user should be able to see 10 directories and 9 files
5010         [ $count == 19 ] || error "$LFS find did not continue after error"
5011 }
5012 run_test 56z "lfs find should continue after an error"
5013
5014 test_56aa() { # LU-5937
5015         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5016
5017         mkdir $DIR/$tdir
5018         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5019
5020         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5021         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5022
5023         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5024 }
5025 run_test 56aa "lfs find --size under striped dir"
5026
5027 test_57a() {
5028         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5029         # note test will not do anything if MDS is not local
5030         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5031                 skip "Only applicable to ldiskfs-based MDTs"
5032                 return
5033         fi
5034
5035         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5036         local MNTDEV="osd*.*MDT*.mntdev"
5037         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5038         [ -z "$DEV" ] && error "can't access $MNTDEV"
5039         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5040                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5041                         error "can't access $DEV"
5042                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5043                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5044                 rm $TMP/t57a.dump
5045         done
5046 }
5047 run_test 57a "verify MDS filesystem created with large inodes =="
5048
5049 test_57b() {
5050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5051         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5052                 skip "Only applicable to ldiskfs-based MDTs"
5053                 return
5054         fi
5055
5056         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5057         local dir=$DIR/$tdir
5058
5059         local FILECOUNT=100
5060         local FILE1=$dir/f1
5061         local FILEN=$dir/f$FILECOUNT
5062
5063         rm -rf $dir || error "removing $dir"
5064         test_mkdir -p -c1 $dir || error "creating $dir"
5065         local mdtidx=$($LFS getstripe -M $dir)
5066         local mdtname=MDT$(printf %04x $mdtidx)
5067         local facet=mds$((mdtidx + 1))
5068
5069         echo "mcreating $FILECOUNT files"
5070         createmany -m $dir/f 1 $FILECOUNT || \
5071                 error "creating files in $dir"
5072
5073         # verify that files do not have EAs yet
5074         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5075         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5076
5077         sync
5078         sleep 1
5079         df $dir  #make sure we get new statfs data
5080         local MDSFREE=$(do_facet $facet \
5081                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5082         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5083         echo "opening files to create objects/EAs"
5084         local FILE
5085         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5086                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5087         done
5088
5089         # verify that files have EAs now
5090         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5091         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5092
5093         sleep 1  #make sure we get new statfs data
5094         df $dir
5095         local MDSFREE2=$(do_facet $facet \
5096                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5097         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5098         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5099                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5100                         error "MDC before $MDCFREE != after $MDCFREE2"
5101                 else
5102                         echo "MDC before $MDCFREE != after $MDCFREE2"
5103                         echo "unable to confirm if MDS has large inodes"
5104                 fi
5105         fi
5106         rm -rf $dir
5107 }
5108 run_test 57b "default LOV EAs are stored inside large inodes ==="
5109
5110 test_58() {
5111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5112         [ -z "$(which wiretest 2>/dev/null)" ] &&
5113                         skip_env "could not find wiretest" && return
5114         wiretest
5115 }
5116 run_test 58 "verify cross-platform wire constants =============="
5117
5118 test_59() {
5119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5120         echo "touch 130 files"
5121         createmany -o $DIR/f59- 130
5122         echo "rm 130 files"
5123         unlinkmany $DIR/f59- 130
5124         sync
5125         # wait for commitment of removal
5126         wait_delete_completed
5127 }
5128 run_test 59 "verify cancellation of llog records async ========="
5129
5130 TEST60_HEAD="test_60 run $RANDOM"
5131 test_60a() {
5132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5133         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5134         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5135                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5136                         skip_env "missing subtest run-llog.sh" && return
5137
5138         log "$TEST60_HEAD - from kernel mode"
5139         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5140         do_facet mgs sh run-llog.sh
5141         do_facet mgs $LCTL dk > $TMP/$tfile
5142
5143         # LU-6388: test llog_reader
5144         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5145         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5146         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5147                         skip_env "missing llog_reader" && return
5148         local fstype=$(facet_fstype mgs)
5149         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5150                 skip_env "Only for ldiskfs or zfs type mgs" && return
5151
5152         local mntpt=$(facet_mntpt mgs)
5153         local mgsdev=$(mgsdevname 1)
5154         local fid_list
5155         local fid
5156         local rec_list
5157         local rec
5158         local rec_type
5159         local obj_file
5160         local path
5161         local seq
5162         local oid
5163         local pass=true
5164
5165         #get fid and record list
5166         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5167                 tail -n 4))
5168         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5169                 tail -n 4))
5170         #remount mgs as ldiskfs or zfs type
5171         stop mgs || error "stop mgs failed"
5172         mount_fstype mgs || error "remount mgs failed"
5173         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5174                 fid=${fid_list[i]}
5175                 rec=${rec_list[i]}
5176                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5177                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5178                 oid=$((16#$oid))
5179
5180                 case $fstype in
5181                         ldiskfs )
5182                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5183                         zfs )
5184                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5185                 esac
5186                 echo "obj_file is $obj_file"
5187                 do_facet mgs $llog_reader $obj_file
5188
5189                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5190                         awk '{ print $3 }' | sed -e "s/^type=//g")
5191                 if [ $rec_type != $rec ]; then
5192                         echo "FAILED test_60a wrong record type $rec_type," \
5193                               "should be $rec"
5194                         pass=false
5195                         break
5196                 fi
5197
5198                 #check obj path if record type is LLOG_LOGID_MAGIC
5199                 if [ "$rec" == "1064553b" ]; then
5200                         path=$(do_facet mgs $llog_reader $obj_file |
5201                                 grep "path=" | awk '{ print $NF }' |
5202                                 sed -e "s/^path=//g")
5203                         if [ $obj_file != $mntpt/$path ]; then
5204                                 echo "FAILED test_60a wrong obj path" \
5205                                       "$montpt/$path, should be $obj_file"
5206                                 pass=false
5207                                 break
5208                         fi
5209                 fi
5210         done
5211         rm -f $TMP/$tfile
5212         #restart mgs before "error", otherwise it will block the next test
5213         stop mgs || error "stop mgs failed"
5214         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5215         $pass || error "test failed, see FAILED test_60a messages for specifics"
5216 }
5217 run_test 60a "llog_test run from kernel module and test llog_reader =========="
5218
5219 test_60b() { # bug 6411
5220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5221         dmesg > $DIR/$tfile
5222         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5223                                  /llog.test/ {
5224                                          if (marker)
5225                                                  from_marker++
5226                                          from_begin++
5227                                  }
5228                                  END {
5229                                          if (marker)
5230                                                  print from_marker
5231                                          else
5232                                                  print from_begin
5233                                  }"`
5234         [[ $LLOG_COUNT -gt 50 ]] &&
5235                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5236 }
5237 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5238
5239 test_60c() {
5240         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5241         echo "create 5000 files"
5242         createmany -o $DIR/f60c- 5000
5243 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5244         lctl set_param fail_loc=0x80000137
5245         unlinkmany $DIR/f60c- 5000
5246         lctl set_param fail_loc=0
5247 }
5248 run_test 60c "unlink file when mds full"
5249
5250 test_60d() {
5251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5252         SAVEPRINTK=$(lctl get_param -n printk)
5253
5254         # verify "lctl mark" is even working"
5255         MESSAGE="test message ID $RANDOM $$"
5256         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5257         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5258
5259         lctl set_param printk=0 || error "set lnet.printk failed"
5260         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5261         MESSAGE="new test message ID $RANDOM $$"
5262         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5263         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5264         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5265
5266         lctl set_param -n printk="$SAVEPRINTK"
5267 }
5268 run_test 60d "test printk console message masking"
5269
5270 test_60e() {
5271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5272         touch $DIR/$tfile
5273 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5274         do_facet mds1 lctl set_param fail_loc=0x15b
5275         rm $DIR/$tfile
5276 }
5277 run_test 60e "no space while new llog is being created"
5278
5279 test_61() {
5280         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5281         f="$DIR/f61"
5282         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5283         cancel_lru_locks osc
5284         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5285         sync
5286 }
5287 run_test 61 "mmap() writes don't make sync hang ================"
5288
5289 # bug 2330 - insufficient obd_match error checking causes LBUG
5290 test_62() {
5291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5292         f="$DIR/f62"
5293         echo foo > $f
5294         cancel_lru_locks osc
5295         lctl set_param fail_loc=0x405
5296         cat $f && error "cat succeeded, expect -EIO"
5297         lctl set_param fail_loc=0
5298 }
5299 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5300 # match every page all of the time.
5301 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5302
5303 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5304 # Though this test is irrelevant anymore, it helped to reveal some
5305 # other grant bugs (LU-4482), let's keep it.
5306 test_63a() {   # was test_63
5307         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5308         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5309         for i in `seq 10` ; do
5310                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5311                 sleep 5
5312                 kill $!
5313                 sleep 1
5314         done
5315
5316         rm -f $DIR/f63 || true
5317 }
5318 run_test 63a "Verify oig_wait interruption does not crash ======="
5319
5320 # bug 2248 - async write errors didn't return to application on sync
5321 # bug 3677 - async write errors left page locked
5322 test_63b() {
5323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5324         debugsave
5325         lctl set_param debug=-1
5326
5327         # ensure we have a grant to do async writes
5328         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5329         rm $DIR/$tfile
5330
5331         sync    # sync lest earlier test intercept the fail_loc
5332
5333         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5334         lctl set_param fail_loc=0x80000406
5335         $MULTIOP $DIR/$tfile Owy && \
5336                 error "sync didn't return ENOMEM"
5337         sync; sleep 2; sync     # do a real sync this time to flush page
5338         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5339                 error "locked page left in cache after async error" || true
5340         debugrestore
5341 }
5342 run_test 63b "async write errors should be returned to fsync ==="
5343
5344 test_64a () {
5345         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5346         df $DIR
5347         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5348 }
5349 run_test 64a "verify filter grant calculations (in kernel) ====="
5350
5351 test_64b () {
5352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5353         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5354 }
5355 run_test 64b "check out-of-space detection on client ==========="
5356
5357 test_64c() {
5358         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5359 }
5360 run_test 64c "verify grant shrink ========================------"
5361
5362 # bug 1414 - set/get directories' stripe info
5363 test_65a() {
5364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5365         test_mkdir -p $DIR/$tdir
5366         touch $DIR/$tdir/f1
5367         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5368 }
5369 run_test 65a "directory with no stripe info ===================="
5370
5371 test_65b() {
5372         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5373         test_mkdir -p $DIR/$tdir
5374         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5375                                                 error "setstripe"
5376         touch $DIR/$tdir/f2
5377         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5378 }
5379 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5380
5381 test_65c() {
5382         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5383         if [[ $OSTCOUNT -gt 1 ]]; then
5384                 test_mkdir -p $DIR/$tdir
5385                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5386                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5387                 touch $DIR/$tdir/f3
5388                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5389         fi
5390 }
5391 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5392
5393 test_65d() {
5394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5395         test_mkdir -p $DIR/$tdir
5396         if [[ $STRIPECOUNT -le 0 ]]; then
5397                 sc=1
5398         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5399 #LOV_MAX_STRIPE_COUNT is 2000
5400                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5401         else
5402                 sc=$(($STRIPECOUNT - 1))
5403         fi
5404         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5405         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5406         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5407                 error "lverify failed"
5408 }
5409 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5410
5411 test_65e() {
5412         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5413         test_mkdir -p $DIR/$tdir
5414
5415         $SETSTRIPE $DIR/$tdir || error "setstripe"
5416         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5417                                         error "no stripe info failed"
5418         touch $DIR/$tdir/f6
5419         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5420 }
5421 run_test 65e "directory setstripe defaults ======================="
5422
5423 test_65f() {
5424         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5425         test_mkdir -p $DIR/${tdir}f
5426         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5427 }
5428 run_test 65f "dir setstripe permission (should return error) ==="
5429
5430 test_65g() {
5431         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5432         test_mkdir -p $DIR/$tdir
5433         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5434                                                         error "setstripe"
5435         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5436         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5437                 error "delete default stripe failed"
5438 }
5439 run_test 65g "directory setstripe -d ==========================="
5440
5441 test_65h() {
5442         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5443         test_mkdir -p $DIR/$tdir
5444         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5445                                                         error "setstripe"
5446         test_mkdir -p $DIR/$tdir/dd1
5447         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5448                 error "stripe info inherit failed"
5449 }
5450 run_test 65h "directory stripe info inherit ===================="
5451
5452 test_65i() { # bug6367
5453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5454         $SETSTRIPE -S 65536 -c -1 $MOUNT
5455 }
5456 run_test 65i "set non-default striping on root directory (bug 6367)="
5457
5458 test_65ia() { # bug12836
5459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5460         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5461 }
5462 run_test 65ia "getstripe on -1 default directory striping"
5463
5464 test_65ib() { # bug12836
5465         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5466         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5467 }
5468 run_test 65ib "getstripe -v on -1 default directory striping"
5469
5470 test_65ic() { # bug12836
5471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5472         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5473 }
5474 run_test 65ic "new find on -1 default directory striping"
5475
5476 test_65j() { # bug6367
5477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5478         sync; sleep 1
5479         # if we aren't already remounting for each test, do so for this test
5480         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5481                 cleanup || error "failed to unmount"
5482                 setup
5483         fi
5484         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5485 }
5486 run_test 65j "set default striping on root directory (bug 6367)="
5487
5488 test_65k() { # bug11679
5489         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5490         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5491         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5492
5493     echo "Check OST status: "
5494     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5495               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5496
5497     for OSC in $MDS_OSCS; do
5498         echo $OSC "is activate"
5499         do_facet $SINGLEMDS lctl --device %$OSC activate
5500     done
5501
5502     mkdir -p $DIR/$tdir
5503     for INACTIVE_OSC in $MDS_OSCS; do
5504         echo "Deactivate: " $INACTIVE_OSC
5505         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5506         for STRIPE_OSC in $MDS_OSCS; do
5507             OST=`osc_to_ost $STRIPE_OSC`
5508             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5509                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5510
5511             [ -f $DIR/$tdir/$IDX ] && continue
5512             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5513             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5514             RC=$?
5515             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5516         done
5517         rm -f $DIR/$tdir/*
5518         echo $INACTIVE_OSC "is Activate."
5519         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5520     done
5521 }
5522 run_test 65k "validate manual striping works properly with deactivated OSCs"
5523
5524 test_65l() { # bug 12836
5525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5526         test_mkdir -p $DIR/$tdir/test_dir
5527         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5528         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5529 }
5530 run_test 65l "lfs find on -1 stripe dir ========================"
5531
5532 # bug 2543 - update blocks count on client
5533 test_66() {
5534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5535         COUNT=${COUNT:-8}
5536         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5537         sync; sync_all_data; sync; sync_all_data
5538         cancel_lru_locks osc
5539         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5540         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5541 }
5542 run_test 66 "update inode blocks count on client ==============="
5543
5544 LLOOP=
5545 LLITELOOPLOAD=
5546 cleanup_68() {
5547         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5548         trap 0
5549         if [ ! -z "$LLOOP" ]; then
5550                 if swapon -s | grep -q $LLOOP; then
5551                         swapoff $LLOOP || error "swapoff failed"
5552                 fi
5553
5554                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5555                 rm -f $LLOOP
5556                 unset LLOOP
5557         fi
5558         if [ ! -z "$LLITELOOPLOAD" ]; then
5559                 rmmod llite_lloop
5560                 unset LLITELOOPLOAD
5561         fi
5562         rm -f $DIR/f68*
5563 }
5564
5565 meminfo() {
5566         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5567 }
5568
5569 swap_used() {
5570         swapon -s | awk '($1 == "'$1'") { print $4 }'
5571 }
5572
5573 # test case for lloop driver, basic function
5574 test_68a() {
5575         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5576         [ "$UID" != 0 ] && skip_env "must run as root" && return
5577         llite_lloop_enabled || \
5578                 { skip_env "llite_lloop module disabled" && return; }
5579
5580         trap cleanup_68 EXIT
5581
5582         if ! module_loaded llite_lloop; then
5583                 if load_module llite/llite_lloop; then
5584                         LLITELOOPLOAD=yes
5585                 else
5586                         skip_env "can't find module llite_lloop"
5587                         return
5588                 fi
5589         fi
5590
5591         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5592         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5593         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5594
5595         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5596         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5597
5598         cleanup_68
5599 }
5600 run_test 68a "lloop driver - basic test ========================"
5601
5602 # excercise swapping to lustre by adding a high priority swapfile entry
5603 # and then consuming memory until it is used.
5604 test_68b() {  # was test_68
5605         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5606         [ "$UID" != 0 ] && skip_env "must run as root" && return
5607         lctl get_param -n devices | grep -q obdfilter && \
5608                 skip "local OST" && return
5609
5610         grep -q llite_lloop /proc/modules
5611         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5612
5613         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5614                 skip "can't reliably test swap with TCP" && return
5615
5616         MEMTOTAL=`meminfo MemTotal`
5617         NR_BLOCKS=$((MEMTOTAL>>8))
5618         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5619
5620         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5621         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5622         mkswap $DIR/f68b
5623
5624         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5625
5626         trap cleanup_68 EXIT
5627
5628         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5629
5630         echo "before: `swapon -s | grep $LLOOP`"
5631         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5632         echo "after: `swapon -s | grep $LLOOP`"
5633         SWAPUSED=`swap_used $LLOOP`
5634
5635         cleanup_68
5636
5637         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5638 }
5639 run_test 68b "support swapping to Lustre ========================"
5640
5641 # bug5265, obdfilter oa2dentry return -ENOENT
5642 # #define OBD_FAIL_SRV_ENOENT 0x217
5643 test_69() {
5644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5645         remote_ost_nodsh && skip "remote OST with nodsh" && return
5646
5647         f="$DIR/$tfile"
5648         $SETSTRIPE -c 1 -i 0 $f
5649
5650         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5651
5652         do_facet ost1 lctl set_param fail_loc=0x217
5653         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5654         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5655
5656         do_facet ost1 lctl set_param fail_loc=0
5657         $DIRECTIO write $f 0 2 || error "write error"
5658
5659         cancel_lru_locks osc
5660         $DIRECTIO read $f 0 1 || error "read error"
5661
5662         do_facet ost1 lctl set_param fail_loc=0x217
5663         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5664
5665         do_facet ost1 lctl set_param fail_loc=0
5666         rm -f $f
5667 }
5668 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5669
5670 test_71() {
5671         test_mkdir -p $DIR/$tdir
5672         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
5673         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5674 }
5675 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5676
5677 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5678         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5679         [ "$RUNAS_ID" = "$UID" ] &&
5680                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5681
5682         # Check that testing environment is properly set up. Skip if not
5683         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5684                 skip_env "User $RUNAS_ID does not exist - skipping"
5685                 return 0
5686         }
5687         touch $DIR/$tfile
5688         chmod 777 $DIR/$tfile
5689         chmod ug+s $DIR/$tfile
5690         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5691                 error "$RUNAS dd $DIR/$tfile failed"
5692         # See if we are still setuid/sgid
5693         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5694                 error "S/gid is not dropped on write"
5695         # Now test that MDS is updated too
5696         cancel_lru_locks mdc
5697         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5698                 error "S/gid is not dropped on MDS"
5699         rm -f $DIR/$tfile
5700 }
5701 run_test 72a "Test that remove suid works properly (bug5695) ===="
5702
5703 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5704         local perm
5705
5706         [ "$RUNAS_ID" = "$UID" ] && \
5707                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5708         [ "$RUNAS_ID" -eq 0 ] && \
5709                 skip_env "RUNAS_ID = 0 -- skipping" && return
5710
5711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5712         # Check that testing environment is properly set up. Skip if not
5713         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5714                 skip_env "User $RUNAS_ID does not exist - skipping"
5715                 return 0
5716         }
5717         touch $DIR/${tfile}-f{g,u}
5718         test_mkdir $DIR/${tfile}-dg
5719         test_mkdir $DIR/${tfile}-du
5720         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5721         chmod g+s $DIR/${tfile}-{f,d}g
5722         chmod u+s $DIR/${tfile}-{f,d}u
5723         for perm in 777 2777 4777; do
5724                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5725                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5726                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5727                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5728         done
5729         true
5730 }
5731 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5732
5733 # bug 3462 - multiple simultaneous MDC requests
5734 test_73() {
5735         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5736         test_mkdir $DIR/d73-1
5737         test_mkdir $DIR/d73-2
5738         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5739         pid1=$!
5740
5741         lctl set_param fail_loc=0x80000129
5742         $MULTIOP $DIR/d73-1/f73-2 Oc &
5743         sleep 1
5744         lctl set_param fail_loc=0
5745
5746         $MULTIOP $DIR/d73-2/f73-3 Oc &
5747         pid3=$!
5748
5749         kill -USR1 $pid1
5750         wait $pid1 || return 1
5751
5752         sleep 25
5753
5754         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5755         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5756         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5757
5758         rm -rf $DIR/d73-*
5759 }
5760 run_test 73 "multiple MDC requests (should not deadlock)"
5761
5762 test_74a() { # bug 6149, 6184
5763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5764         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5765         #
5766         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5767         # will spin in a tight reconnection loop
5768         touch $DIR/f74a
5769         $LCTL set_param fail_loc=0x8000030e
5770         # get any lock that won't be difficult - lookup works.
5771         ls $DIR/f74a
5772         $LCTL set_param fail_loc=0
5773         rm -f $DIR/f74a
5774         true
5775 }
5776 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5777
5778 test_74b() { # bug 13310
5779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5780         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5781         #
5782         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5783         # will spin in a tight reconnection loop
5784         $LCTL set_param fail_loc=0x8000030e
5785         # get a "difficult" lock
5786         touch $DIR/f74b
5787         $LCTL set_param fail_loc=0
5788         rm -f $DIR/f74b
5789         true
5790 }
5791 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5792
5793 test_74c() {
5794         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5795         #define OBD_FAIL_LDLM_NEW_LOCK
5796         $LCTL set_param fail_loc=0x319
5797         touch $DIR/$tfile && error "touch successful"
5798         $LCTL set_param fail_loc=0
5799         true
5800 }
5801 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5802
5803 num_inodes() {
5804         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5805 }
5806
5807 get_inode_slab_tunables() {
5808         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5809 }
5810
5811 set_inode_slab_tunables() {
5812         echo "lustre_inode_cache $1" > /proc/slabinfo
5813 }
5814
5815 test_76() { # Now for bug 20433, added originally in bug 1443
5816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5817         local SLAB_SETTINGS=`get_inode_slab_tunables`
5818         local CPUS=`getconf _NPROCESSORS_ONLN`
5819         # we cannot set limit below 1 which means 1 inode in each
5820         # per-cpu cache is still allowed
5821         set_inode_slab_tunables "1 1 0"
5822         cancel_lru_locks osc
5823         BEFORE_INODES=$(num_inodes)
5824         echo "before inodes: $BEFORE_INODES"
5825         local COUNT=1000
5826         [ "$SLOW" = "no" ] && COUNT=100
5827         for i in $(seq $COUNT); do
5828                 touch $DIR/$tfile
5829                 rm -f $DIR/$tfile
5830         done
5831         cancel_lru_locks osc
5832         AFTER_INODES=$(num_inodes)
5833         echo "after inodes: $AFTER_INODES"
5834         local wait=0
5835         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5836                 sleep 2
5837                 AFTER_INODES=$(num_inodes)
5838                 wait=$((wait+2))
5839                 echo "wait $wait seconds inodes: $AFTER_INODES"
5840                 if [ $wait -gt 30 ]; then
5841                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5842                 fi
5843         done
5844         set_inode_slab_tunables "$SLAB_SETTINGS"
5845 }
5846 run_test 76 "confirm clients recycle inodes properly ===="
5847
5848
5849 export ORIG_CSUM=""
5850 set_checksums()
5851 {
5852         # Note: in sptlrpc modes which enable its own bulk checksum, the
5853         # original crc32_le bulk checksum will be automatically disabled,
5854         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5855         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5856         # In this case set_checksums() will not be no-op, because sptlrpc
5857         # bulk checksum will be enabled all through the test.
5858
5859         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5860         lctl set_param -n osc.*.checksums $1
5861         return 0
5862 }
5863
5864 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5865                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5866 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5867 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5868 set_checksum_type()
5869 {
5870         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5871         log "set checksum type to $1"
5872         return 0
5873 }
5874 F77_TMP=$TMP/f77-temp
5875 F77SZ=8
5876 setup_f77() {
5877         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5878                 error "error writing to $F77_TMP"
5879 }
5880
5881 test_77a() { # bug 10889
5882         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5883         $GSS && skip "could not run with gss" && return
5884         [ ! -f $F77_TMP ] && setup_f77
5885         set_checksums 1
5886         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5887         set_checksums 0
5888         rm -f $DIR/$tfile
5889 }
5890 run_test 77a "normal checksum read/write operation"
5891
5892 test_77b() { # bug 10889
5893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5894         $GSS && skip "could not run with gss" && return
5895         [ ! -f $F77_TMP ] && setup_f77
5896         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5897         $LCTL set_param fail_loc=0x80000409
5898         set_checksums 1
5899
5900         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5901                 error "dd error: $?"
5902         $LCTL set_param fail_loc=0
5903
5904         for algo in $CKSUM_TYPES; do
5905                 cancel_lru_locks osc
5906                 set_checksum_type $algo
5907                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5908                 $LCTL set_param fail_loc=0x80000408
5909                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5910                 $LCTL set_param fail_loc=0
5911         done
5912         set_checksums 0
5913         set_checksum_type $ORIG_CSUM_TYPE
5914         rm -f $DIR/$tfile
5915 }
5916 run_test 77b "checksum error on client write, read"
5917
5918 test_77d() { # bug 10889
5919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5920         $GSS && skip "could not run with gss" && return
5921         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5922         $LCTL set_param fail_loc=0x80000409
5923         set_checksums 1
5924         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5925                 error "direct write: rc=$?"
5926         $LCTL set_param fail_loc=0
5927         set_checksums 0
5928
5929         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5930         $LCTL set_param fail_loc=0x80000408
5931         set_checksums 1
5932         cancel_lru_locks osc
5933         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5934                 error "direct read: rc=$?"
5935         $LCTL set_param fail_loc=0
5936         set_checksums 0
5937 }
5938 run_test 77d "checksum error on OST direct write, read"
5939
5940 test_77f() { # bug 10889
5941         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5942         $GSS && skip "could not run with gss" && return
5943         set_checksums 1
5944         for algo in $CKSUM_TYPES; do
5945                 cancel_lru_locks osc
5946                 set_checksum_type $algo
5947                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5948                 $LCTL set_param fail_loc=0x409
5949                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5950                         error "direct write succeeded"
5951                 $LCTL set_param fail_loc=0
5952         done
5953         set_checksum_type $ORIG_CSUM_TYPE
5954         set_checksums 0
5955 }
5956 run_test 77f "repeat checksum error on write (expect error)"
5957
5958 test_77g() { # bug 10889
5959         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5960         $GSS && skip "could not run with gss" && return
5961         remote_ost_nodsh && skip "remote OST with nodsh" && return
5962
5963         [ ! -f $F77_TMP ] && setup_f77
5964
5965         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5966         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5967         do_facet ost1 lctl set_param fail_loc=0x8000021a
5968         set_checksums 1
5969         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5970                 error "write error: rc=$?"
5971         do_facet ost1 lctl set_param fail_loc=0
5972         set_checksums 0
5973
5974         cancel_lru_locks osc
5975         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5976         do_facet ost1 lctl set_param fail_loc=0x8000021b
5977         set_checksums 1
5978         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5979         do_facet ost1 lctl set_param fail_loc=0
5980         set_checksums 0
5981 }
5982 run_test 77g "checksum error on OST write, read"
5983
5984 test_77i() { # bug 13805
5985         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5986         $GSS && skip "could not run with gss" && return
5987         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5988         lctl set_param fail_loc=0x40b
5989         remount_client $MOUNT
5990         lctl set_param fail_loc=0
5991         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5992                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5993                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5994                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5995         done
5996         remount_client $MOUNT
5997 }
5998 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5999
6000 test_77j() { # bug 13805
6001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6002         $GSS && skip "could not run with gss" && return
6003         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6004         lctl set_param fail_loc=0x40c
6005         remount_client $MOUNT
6006         lctl set_param fail_loc=0
6007         sleep 2 # wait async osc connect to finish
6008         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
6009                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6010                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
6011                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6012         done
6013         remount_client $MOUNT
6014 }
6015 run_test 77j "client only supporting ADLER32"
6016
6017 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6018 rm -f $F77_TMP
6019 unset F77_TMP
6020
6021 test_78() { # bug 10901
6022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6023         remote_ost || { skip_env "local OST" && return; }
6024
6025         NSEQ=5
6026         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6027         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6028         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6029         echo "MemTotal: $MEMTOTAL"
6030
6031         # reserve 256MB of memory for the kernel and other running processes,
6032         # and then take 1/2 of the remaining memory for the read/write buffers.
6033         if [ $MEMTOTAL -gt 512 ] ;then
6034                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6035         else
6036                 # for those poor memory-starved high-end clusters...
6037                 MEMTOTAL=$((MEMTOTAL / 2))
6038         fi
6039         echo "Mem to use for directio: $MEMTOTAL"
6040
6041         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6042         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6043         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6044         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6045                 head -n1)
6046         echo "Smallest OST: $SMALLESTOST"
6047         [[ $SMALLESTOST -lt 10240 ]] &&
6048                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6049
6050         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6051                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6052
6053         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6054         echo "File size: $F78SIZE"
6055         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6056         for i in $(seq 1 $NSEQ); do
6057                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6058                 echo directIO rdwr round $i of $NSEQ
6059                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6060         done
6061
6062         rm -f $DIR/$tfile
6063 }
6064 run_test 78 "handle large O_DIRECT writes correctly ============"
6065
6066 test_79() { # bug 12743
6067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6068         wait_delete_completed
6069
6070         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6071         BKFREE=$(calc_osc_kbytes kbytesfree)
6072         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6073
6074         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6075         DFTOTAL=`echo $STRING | cut -d, -f1`
6076         DFUSED=`echo $STRING  | cut -d, -f2`
6077         DFAVAIL=`echo $STRING | cut -d, -f3`
6078         DFFREE=$(($DFTOTAL - $DFUSED))
6079
6080         ALLOWANCE=$((64 * $OSTCOUNT))
6081
6082         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6083            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6084                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6085         fi
6086         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6087            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6088                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6089         fi
6090         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6091            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6092                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6093         fi
6094 }
6095 run_test 79 "df report consistency check ======================="
6096
6097 test_80() { # bug 10718
6098         remote_ost_nodsh && skip "remote OST with nodsh" && return
6099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6100         # relax strong synchronous semantics for slow backends like ZFS
6101         local soc="obdfilter.*.sync_on_lock_cancel"
6102         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6103         local hosts=
6104         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6105                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6106                           facet_active_host $host; done | sort -u)
6107                 do_nodes $hosts lctl set_param $soc=never
6108         fi
6109
6110         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6111         sync; sleep 1; sync
6112         local BEFORE=`date +%s`
6113         cancel_lru_locks osc
6114         local AFTER=`date +%s`
6115         local DIFF=$((AFTER-BEFORE))
6116         if [ $DIFF -gt 1 ] ; then
6117                 error "elapsed for 1M@1T = $DIFF"
6118         fi
6119
6120         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6121
6122         rm -f $DIR/$tfile
6123 }
6124 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6125
6126 test_81a() { # LU-456
6127         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6128         remote_ost_nodsh && skip "remote OST with nodsh" && return
6129         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6130         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6131         do_facet ost1 lctl set_param fail_loc=0x80000228
6132
6133         # write should trigger a retry and success
6134         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6135         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6136         RC=$?
6137         if [ $RC -ne 0 ] ; then
6138                 error "write should success, but failed for $RC"
6139         fi
6140 }
6141 run_test 81a "OST should retry write when get -ENOSPC ==============="
6142
6143 test_81b() { # LU-456
6144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6145         remote_ost_nodsh && skip "remote OST with nodsh" && return
6146         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6147         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6148         do_facet ost1 lctl set_param fail_loc=0x228
6149
6150         # write should retry several times and return -ENOSPC finally
6151         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6152         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6153         RC=$?
6154         ENOSPC=28
6155         if [ $RC -ne $ENOSPC ] ; then
6156                 error "dd should fail for -ENOSPC, but succeed."
6157         fi
6158 }
6159 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6160
6161 test_82() { # LU-1031
6162         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6163         local gid1=14091995
6164         local gid2=16022000
6165
6166         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6167         local MULTIPID1=$!
6168         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6169         local MULTIPID2=$!
6170         kill -USR1 $MULTIPID2
6171         sleep 2
6172         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6173                 error "First grouplock does not block second one"
6174         else
6175                 echo "Second grouplock blocks first one"
6176         fi
6177         kill -USR1 $MULTIPID1
6178         wait $MULTIPID1
6179         wait $MULTIPID2
6180 }
6181 run_test 82 "Basic grouplock test ==============================="
6182
6183 test_99a() {
6184         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6185                 return
6186         test_mkdir -p $DIR/d99cvsroot
6187         chown $RUNAS_ID $DIR/d99cvsroot
6188         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6189         cd $TMP
6190
6191         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6192         cd $oldPWD
6193 }
6194 run_test 99a "cvs init ========================================="
6195
6196 test_99b() {
6197         [ -z "$(which cvs 2>/dev/null)" ] &&
6198                 skip_env "could not find cvs" && return
6199         [ ! -d $DIR/d99cvsroot ] && test_99a
6200         cd /etc/init.d
6201         # some versions of cvs import exit(1) when asked to import links or
6202         # files they can't read.  ignore those files.
6203         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6204                         ! -perm +4 -printf '-I %f\n')
6205         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6206                 d99reposname vtag rtag
6207 }
6208 run_test 99b "cvs import ======================================="
6209
6210 test_99c() {
6211         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6212         [ ! -d $DIR/d99cvsroot ] && test_99b
6213         cd $DIR
6214         test_mkdir -p $DIR/d99reposname
6215         chown $RUNAS_ID $DIR/d99reposname
6216         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6217 }
6218 run_test 99c "cvs checkout ====================================="
6219
6220 test_99d() {
6221         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6222         [ ! -d $DIR/d99cvsroot ] && test_99c
6223         cd $DIR/d99reposname
6224         $RUNAS touch foo99
6225         $RUNAS cvs add -m 'addmsg' foo99
6226 }
6227 run_test 99d "cvs add =========================================="
6228
6229 test_99e() {
6230         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6231         [ ! -d $DIR/d99cvsroot ] && test_99c
6232         cd $DIR/d99reposname
6233         $RUNAS cvs update
6234 }
6235 run_test 99e "cvs update ======================================="
6236
6237 test_99f() {
6238         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6239         [ ! -d $DIR/d99cvsroot ] && test_99d
6240         cd $DIR/d99reposname
6241         $RUNAS cvs commit -m 'nomsg' foo99
6242     rm -fr $DIR/d99cvsroot
6243 }
6244 run_test 99f "cvs commit ======================================="
6245
6246 test_100() {
6247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6248         [ "$NETTYPE" = tcp ] || \
6249                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6250                         return ; }
6251
6252         remote_ost_nodsh && skip "remote OST with nodsh" && return
6253         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6254         remote_servers || \
6255                 { skip "useless for local single node setup" && return; }
6256
6257         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6258                 [ "$PROT" != "tcp" ] && continue
6259                 RPORT=$(echo $REMOTE | cut -d: -f2)
6260                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6261
6262                 rc=0
6263                 LPORT=`echo $LOCAL | cut -d: -f2`
6264                 if [ $LPORT -ge 1024 ]; then
6265                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6266                         netstat -tna
6267                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6268                 fi
6269         done
6270         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6271 }
6272 run_test 100 "check local port using privileged port ==========="
6273
6274 function get_named_value()
6275 {
6276     local tag
6277
6278     tag=$1
6279     while read ;do
6280         line=$REPLY
6281         case $line in
6282         $tag*)
6283             echo $line | sed "s/^$tag[ ]*//"
6284             break
6285             ;;
6286         esac
6287     done
6288 }
6289
6290 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6291                    awk '/^max_cached_mb/ { print $2 }')
6292
6293 cleanup_101a() {
6294         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6295         trap 0
6296 }
6297
6298 test_101a() {
6299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6300         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6301         local s
6302         local discard
6303         local nreads=10000
6304         local cache_limit=32
6305
6306         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6307         trap cleanup_101a EXIT
6308         $LCTL set_param -n llite.*.read_ahead_stats 0
6309         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6310
6311         #
6312         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6313         #
6314         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6315         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6316
6317         discard=0
6318         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6319                 get_named_value 'read but discarded' | cut -d" " -f1); do
6320                         discard=$(($discard + $s))
6321         done
6322         cleanup_101a
6323
6324         if [[ $(($discard * 10)) -gt $nreads ]]; then
6325                 $LCTL get_param osc.*-osc*.rpc_stats
6326                 $LCTL get_param llite.*.read_ahead_stats
6327                 error "too many ($discard) discarded pages"
6328         fi
6329         rm -f $DIR/$tfile || true
6330 }
6331 run_test 101a "check read-ahead for random reads ================"
6332
6333 setup_test101bc() {
6334         test_mkdir -p $DIR/$tdir
6335         local STRIPE_SIZE=$1
6336         local FILE_LENGTH=$2
6337         STRIPE_OFFSET=0
6338
6339         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6340
6341         local list=$(comma_list $(osts_nodes))
6342         set_osd_param $list '' read_cache_enable 0
6343         set_osd_param $list '' writethrough_cache_enable 0
6344
6345         trap cleanup_test101bc EXIT
6346         # prepare the read-ahead file
6347         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6348
6349         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6350                                 count=$FILE_SIZE_MB 2> /dev/null
6351
6352 }
6353
6354 cleanup_test101bc() {
6355         trap 0
6356         rm -rf $DIR/$tdir
6357         rm -f $DIR/$tfile
6358
6359         local list=$(comma_list $(osts_nodes))
6360         set_osd_param $list '' read_cache_enable 1
6361         set_osd_param $list '' writethrough_cache_enable 1
6362 }
6363
6364 calc_total() {
6365         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6366 }
6367
6368 ra_check_101() {
6369         local READ_SIZE=$1
6370         local STRIPE_SIZE=$2
6371         local FILE_LENGTH=$3
6372         local RA_INC=1048576
6373         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6374         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6375                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6376         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6377                         get_named_value 'read but discarded' |
6378                         cut -d" " -f1 | calc_total)
6379         if [[ $DISCARD -gt $discard_limit ]]; then
6380                 $LCTL get_param llite.*.read_ahead_stats
6381                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6382         else
6383                 echo "Read-ahead success for size ${READ_SIZE}"
6384         fi
6385 }
6386
6387 test_101b() {
6388         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6389         [[ $OSTCOUNT -lt 2 ]] &&
6390                 skip_env "skipping stride IO stride-ahead test" && return
6391         local STRIPE_SIZE=1048576
6392         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6393         if [ $SLOW == "yes" ]; then
6394                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6395         else
6396                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6397         fi
6398
6399         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6400
6401         # prepare the read-ahead file
6402         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6403         cancel_lru_locks osc
6404         for BIDX in 2 4 8 16 32 64 128 256
6405         do
6406                 local BSIZE=$((BIDX*4096))
6407                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6408                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6409                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6410                 $LCTL set_param -n llite.*.read_ahead_stats 0
6411                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6412                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6413                 cancel_lru_locks osc
6414                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6415         done
6416         cleanup_test101bc
6417         true
6418 }
6419 run_test 101b "check stride-io mode read-ahead ================="
6420
6421 test_101c() {
6422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6423         local STRIPE_SIZE=1048576
6424         local FILE_LENGTH=$((STRIPE_SIZE*100))
6425         local nreads=10000
6426         local osc_rpc_stats
6427
6428         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6429
6430         cancel_lru_locks osc
6431         $LCTL set_param osc.*.rpc_stats 0
6432         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6433         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6434                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6435                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6436                 local size
6437
6438                 if [ $lines -le 20 ]; then
6439                         continue
6440                 fi
6441                 for size in 1 2 4 8; do
6442                         local rpc=$(echo "$stats" |
6443                                     awk '($1 == "'$size':") {print $2; exit; }')
6444                         [ $rpc != 0 ] &&
6445                                 error "Small $((size*4))k read IO $rpc !"
6446                 done
6447                 echo "$osc_rpc_stats check passed!"
6448         done
6449         cleanup_test101bc
6450         true
6451 }
6452 run_test 101c "check stripe_size aligned read-ahead ================="
6453
6454 set_read_ahead() {
6455         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6456         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6457 }
6458
6459 test_101d() {
6460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6461         local file=$DIR/$tfile
6462         local sz_MB=${FILESIZE_101d:-500}
6463         local ra_MB=${READAHEAD_MB:-40}
6464
6465         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6466         [ $free_MB -lt $sz_MB ] &&
6467                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6468
6469         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6470         $SETSTRIPE -c -1 $file || error "setstripe failed"
6471
6472         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6473         echo Cancel LRU locks on lustre client to flush the client cache
6474         cancel_lru_locks osc
6475
6476         echo Disable read-ahead
6477         local old_READAHEAD=$(set_read_ahead 0)
6478
6479         echo Reading the test file $file with read-ahead disabled
6480         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6481
6482         echo Cancel LRU locks on lustre client to flush the client cache
6483         cancel_lru_locks osc
6484         echo Enable read-ahead with ${ra_MB}MB
6485         set_read_ahead $ra_MB
6486
6487         echo Reading the test file $file with read-ahead enabled
6488         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6489
6490         echo "read-ahead disabled time read $raOFF"
6491         echo "read-ahead enabled  time read $raON"
6492
6493         set_read_ahead $old_READAHEAD
6494         rm -f $file
6495         wait_delete_completed
6496
6497         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6498                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6499 }
6500 run_test 101d "file read with and without read-ahead enabled"
6501
6502 test_101e() {
6503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6504         local file=$DIR/$tfile
6505         local size_KB=500  #KB
6506         local count=100
6507         local bsize=1024
6508
6509         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6510         local need_KB=$((count * size_KB))
6511         [[ $free_KB -le $need_KB ]] &&
6512                 skip_env "Need free space $need_KB, have $free_KB" && return
6513
6514         echo "Creating $count ${size_KB}K test files"
6515         for ((i = 0; i < $count; i++)); do
6516                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6517         done
6518
6519         echo "Cancel LRU locks on lustre client to flush the client cache"
6520         cancel_lru_locks osc
6521
6522         echo "Reset readahead stats"
6523         $LCTL set_param -n llite.*.read_ahead_stats 0
6524
6525         for ((i = 0; i < $count; i++)); do
6526                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6527         done
6528
6529         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6530                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6531
6532         for ((i = 0; i < $count; i++)); do
6533                 rm -rf $file.$i 2>/dev/null
6534         done
6535
6536         #10000 means 20% reads are missing in readahead
6537         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6538 }
6539 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6540
6541 cleanup_test101f() {
6542     trap 0
6543     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6544     rm -rf $DIR/$tfile 2>/dev/null
6545 }
6546
6547 test_101f() {
6548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6549     local file=$DIR/$tfile
6550     local nreads=1000
6551
6552     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6553     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6554     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6555     trap cleanup_test101f EXIT
6556
6557     echo Cancel LRU locks on lustre client to flush the client cache
6558     cancel_lru_locks osc
6559
6560     echo Reset readahead stats
6561     $LCTL set_param -n llite.*.read_ahead_stats 0
6562     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6563     # readahead should read in 2M file on second read, so only miss
6564     # 2 pages.
6565     echo Random 4K reads on 2M file for 1000 times
6566     $READS -f $file -s 2097152 -b 4096 -n $nreads
6567
6568     echo checking missing pages
6569     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6570           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6571
6572     [ $miss -lt 3 ] || error "misses too much pages!"
6573     cleanup_test101f
6574 }
6575 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6576
6577 setup_test102() {
6578         test_mkdir -p $DIR/$tdir
6579         chown $RUNAS_ID $DIR/$tdir
6580         STRIPE_SIZE=65536
6581         STRIPE_OFFSET=1
6582         STRIPE_COUNT=$OSTCOUNT
6583         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6584
6585         trap cleanup_test102 EXIT
6586         cd $DIR
6587         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6588         cd $DIR/$tdir
6589         for num in 1 2 3 4; do
6590                 for count in $(seq 1 $STRIPE_COUNT); do
6591                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6592                                 local size=`expr $STRIPE_SIZE \* $num`
6593                                 local file=file"$num-$idx-$count"
6594                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6595                         done
6596                 done
6597         done
6598
6599         cd $DIR
6600         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6601 }
6602
6603 cleanup_test102() {
6604         trap 0
6605         rm -f $TMP/f102.tar
6606         rm -rf $DIR/d0.sanity/d102
6607 }
6608
6609 test_102a() {
6610         local testfile=$DIR/$tfile
6611
6612         touch $testfile
6613
6614         [ "$UID" != 0 ] && skip_env "must run as root" && return
6615         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6616                 skip_env "must have user_xattr" && return
6617
6618         [ -z "$(which setfattr 2>/dev/null)" ] &&
6619                 skip_env "could not find setfattr" && return
6620
6621         echo "set/get xattr..."
6622         setfattr -n trusted.name1 -v value1 $testfile ||
6623                 error "setfattr -n trusted.name1=value1 $testfile failed"
6624         getfattr -n trusted.name1 $testfile 2> /dev/null |
6625           grep "trusted.name1=.value1" ||
6626                 error "$testfile missing trusted.name1=value1"
6627
6628         setfattr -n user.author1 -v author1 $testfile ||
6629                 error "setfattr -n user.author1=author1 $testfile failed"
6630         getfattr -n user.author1 $testfile 2> /dev/null |
6631           grep "user.author1=.author1" ||
6632                 error "$testfile missing trusted.author1=author1"
6633
6634         echo "listxattr..."
6635         setfattr -n trusted.name2 -v value2 $testfile ||
6636                 error "$testfile unable to set trusted.name2"
6637         setfattr -n trusted.name3 -v value3 $testfile ||
6638                 error "$testfile unable to set trusted.name3"
6639         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6640             grep "trusted.name" | wc -l) -eq 3 ] ||
6641                 error "$testfile missing 3 trusted.name xattrs"
6642
6643         setfattr -n user.author2 -v author2 $testfile ||
6644                 error "$testfile unable to set user.author2"
6645         setfattr -n user.author3 -v author3 $testfile ||
6646                 error "$testfile unable to set user.author3"
6647         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6648             grep "user.author" | wc -l) -eq 3 ] ||
6649                 error "$testfile missing 3 user.author xattrs"
6650
6651         echo "remove xattr..."
6652         setfattr -x trusted.name1 $testfile ||
6653                 error "$testfile error deleting trusted.name1"
6654         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6655                 error "$testfile did not delete trusted.name1 xattr"
6656
6657         setfattr -x user.author1 $testfile ||
6658                 error "$testfile error deleting user.author1"
6659         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6660                 error "$testfile did not delete trusted.name1 xattr"
6661
6662         # b10667: setting lustre special xattr be silently discarded
6663         echo "set lustre special xattr ..."
6664         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6665                 error "$testfile allowed setting trusted.lov"
6666 }
6667 run_test 102a "user xattr test =================================="
6668
6669 test_102b() {
6670         [ -z "$(which setfattr 2>/dev/null)" ] &&
6671                 skip_env "could not find setfattr" && return
6672
6673         # b10930: get/set/list trusted.lov xattr
6674         echo "get/set/list trusted.lov xattr ..."
6675         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6676         local testfile=$DIR/$tfile
6677         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6678                 error "setstripe failed"
6679         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6680                 error "getstripe failed"
6681         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6682                 error "can't get trusted.lov from $testfile"
6683
6684         local testfile2=${testfile}2
6685         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6686                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6687
6688         $MCREATE $testfile2
6689         setfattr -n trusted.lov -v $value $testfile2
6690         local stripe_size=$($GETSTRIPE -S $testfile2)
6691         local stripe_count=$($GETSTRIPE -c $testfile2)
6692         [[ $stripe_size -eq 65536 ]] ||
6693                 error "stripe size $stripe_size != 65536"
6694         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6695                 error "stripe count $stripe_count != $STRIPECOUNT"
6696         rm -f $DIR/$tfile
6697 }
6698 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6699
6700 test_102c() {
6701         [ -z "$(which setfattr 2>/dev/null)" ] &&
6702                 skip_env "could not find setfattr" && return
6703
6704         # b10930: get/set/list lustre.lov xattr
6705         echo "get/set/list lustre.lov xattr ..."
6706         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6707         test_mkdir -p $DIR/$tdir
6708         chown $RUNAS_ID $DIR/$tdir
6709         local testfile=$DIR/$tdir/$tfile
6710         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6711                 error "setstripe failed"
6712         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6713                 error "getstripe failed"
6714         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6715         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6716
6717         local testfile2=${testfile}2
6718         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6719                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6720
6721         $RUNAS $MCREATE $testfile2
6722         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6723         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6724         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6725         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6726         [ $stripe_count -eq $STRIPECOUNT ] ||
6727                 error "stripe count $stripe_count != $STRIPECOUNT"
6728 }
6729 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6730
6731 compare_stripe_info1() {
6732         local stripe_index_all_zero=true
6733
6734         for num in 1 2 3 4; do
6735                 for count in $(seq 1 $STRIPE_COUNT); do
6736                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6737                                 local size=$((STRIPE_SIZE * num))
6738                                 local file=file"$num-$offset-$count"
6739                                 stripe_size=$($LFS getstripe -S $PWD/$file)
6740                                 [[ $stripe_size -ne $size ]] &&
6741                                     error "$file: size $stripe_size != $size"
6742                                 stripe_count=$($LFS getstripe -c $PWD/$file)
6743                                 # allow fewer stripes to be created, ORI-601
6744                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6745                                     error "$file: count $stripe_count != $count"
6746                                 stripe_index=$($LFS getstripe -i $PWD/$file)
6747                                 [[ $stripe_index -ne 0 ]] &&
6748                                         stripe_index_all_zero=false
6749                         done
6750                 done
6751         done
6752         $stripe_index_all_zero &&
6753                 error "all files are being extracted starting from OST index 0"
6754         return 0
6755 }
6756
6757 find_lustre_tar() {
6758         [ -n "$(which tar 2>/dev/null)" ] &&
6759                 strings $(which tar) | grep -q "lustre" && echo tar
6760 }
6761
6762 test_102d() {
6763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6764         # b10930: tar test for trusted.lov xattr
6765         TAR=$(find_lustre_tar)
6766         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6767         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6768         setup_test102
6769         test_mkdir -p $DIR/d102d
6770         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6771         cd $DIR/d102d/$tdir
6772         compare_stripe_info1
6773 }
6774 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6775
6776 test_102f() {
6777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6778         # b10930: tar test for trusted.lov xattr
6779         TAR=$(find_lustre_tar)
6780         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6781         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6782         setup_test102
6783         test_mkdir -p $DIR/d102f
6784         cd $DIR
6785         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6786         cd $DIR/d102f/$tdir
6787         compare_stripe_info1
6788 }
6789 run_test 102f "tar copy files, not keep osts ==========="
6790
6791 grow_xattr() {
6792         local xsize=${1:-1024}  # in bytes
6793         local file=$DIR/$tfile
6794
6795         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
6796                 skip "must have user_xattr" && return 0
6797         [ -z "$(which setfattr 2>/dev/null)" ] &&
6798                 skip_env "could not find setfattr" && return 0
6799         [ -z "$(which getfattr 2>/dev/null)" ] &&
6800                 skip_env "could not find getfattr" && return 0
6801
6802         touch $file
6803
6804         local value="$(generate_string $xsize)"
6805
6806         local xbig=trusted.big
6807         log "save $xbig on $file"
6808         setfattr -n $xbig -v $value $file ||
6809                 error "saving $xbig on $file failed"
6810
6811         local orig=$(get_xattr_value $xbig $file)
6812         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6813
6814         local xsml=trusted.sml
6815         log "save $xsml on $file"
6816         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6817
6818         local new=$(get_xattr_value $xbig $file)
6819         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6820
6821         log "grow $xsml on $file"
6822         setfattr -n $xsml -v "$value" $file ||
6823                 error "growing $xsml on $file failed"
6824
6825         new=$(get_xattr_value $xbig $file)
6826         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6827         log "$xbig still valid after growing $xsml"
6828
6829         rm -f $file
6830 }
6831
6832 test_102h() { # bug 15777
6833         grow_xattr 1024
6834 }
6835 run_test 102h "grow xattr from inside inode to external block"
6836
6837 test_102ha() {
6838         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6839         grow_xattr $(max_xattr_size)
6840 }
6841 run_test 102ha "grow xattr from inside inode to external inode"
6842
6843 test_102i() { # bug 17038
6844         [ -z "$(which getfattr 2>/dev/null)" ] &&
6845                 skip "could not find getfattr" && return
6846         touch $DIR/$tfile
6847         ln -s $DIR/$tfile $DIR/${tfile}link
6848         getfattr -n trusted.lov $DIR/$tfile ||
6849                 error "lgetxattr on $DIR/$tfile failed"
6850         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6851                 grep -i "no such attr" ||
6852                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6853         rm -f $DIR/$tfile $DIR/${tfile}link
6854 }
6855 run_test 102i "lgetxattr test on symbolic link ============"
6856
6857 test_102j() {
6858         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6859         TAR=$(find_lustre_tar)
6860         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6861         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6862         setup_test102 "$RUNAS"
6863         test_mkdir -p $DIR/d102j
6864         chown $RUNAS_ID $DIR/d102j
6865         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6866         cd $DIR/d102j/$tdir
6867         compare_stripe_info1 "$RUNAS"
6868 }
6869 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6870
6871 test_102k() {
6872         [ -z "$(which setfattr 2>/dev/null)" ] &&
6873                 skip "could not find setfattr" && return
6874         touch $DIR/$tfile
6875         # b22187 just check that does not crash for regular file.
6876         setfattr -n trusted.lov $DIR/$tfile
6877         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6878         local test_kdir=$DIR/d102k
6879         test_mkdir $test_kdir
6880         local default_size=`$GETSTRIPE -S $test_kdir`
6881         local default_count=`$GETSTRIPE -c $test_kdir`
6882         local default_offset=`$GETSTRIPE -i $test_kdir`
6883         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6884                 error 'dir setstripe failed'
6885         setfattr -n trusted.lov $test_kdir
6886         local stripe_size=`$GETSTRIPE -S $test_kdir`
6887         local stripe_count=`$GETSTRIPE -c $test_kdir`
6888         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6889         [ $stripe_size -eq $default_size ] ||
6890                 error "stripe size $stripe_size != $default_size"
6891         [ $stripe_count -eq $default_count ] ||
6892                 error "stripe count $stripe_count != $default_count"
6893         [ $stripe_offset -eq $default_offset ] ||
6894                 error "stripe offset $stripe_offset != $default_offset"
6895         rm -rf $DIR/$tfile $test_kdir
6896 }
6897 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6898
6899 test_102l() {
6900         [ -z "$(which getfattr 2>/dev/null)" ] &&
6901                 skip "could not find getfattr" && return
6902
6903         # LU-532 trusted. xattr is invisible to non-root
6904         local testfile=$DIR/$tfile
6905
6906         touch $testfile
6907
6908         echo "listxattr as user..."
6909         chown $RUNAS_ID $testfile
6910         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6911             grep -q "trusted" &&
6912                 error "$testfile trusted xattrs are user visible"
6913
6914         return 0;
6915 }
6916 run_test 102l "listxattr size test =================================="
6917
6918 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6919         local path=$DIR/$tfile
6920         touch $path
6921
6922         listxattr_size_check $path || error "listattr_size_check $path failed"
6923 }
6924 run_test 102m "Ensure listxattr fails on small bufffer ========"
6925
6926 cleanup_test102
6927
6928 getxattr() { # getxattr path name
6929         # Return the base64 encoding of the value of xattr name on path.
6930         local path=$1
6931         local name=$2
6932
6933         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6934         # file: $path
6935         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6936         #
6937         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6938
6939         getfattr --absolute-names --encoding=base64 --name=$name $path |
6940                 awk -F= -v name=$name '$1 == name {
6941                         print substr($0, index($0, "=") + 1);
6942         }'
6943 }
6944
6945 test_102n() { # LU-4101 mdt: protect internal xattrs
6946         local file0=$DIR/$tfile.0
6947         local file1=$DIR/$tfile.1
6948         local xattr0=$TMP/$tfile.0
6949         local xattr1=$TMP/$tfile.1
6950         local name
6951         local value
6952
6953         [ -z "$(which setfattr 2>/dev/null)" ] &&
6954                 skip "could not find setfattr" && return
6955
6956         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6957         then
6958                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6959                 return
6960         fi
6961
6962         rm -rf $file0 $file1 $xattr0 $xattr1
6963         touch $file0 $file1
6964
6965         # Get 'before' xattrs of $file1.
6966         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6967
6968         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6969                 # Try to copy xattr from $file0 to $file1.
6970                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6971
6972                 setfattr --name=trusted.$name --value="$value" $file1 ||
6973                         error "setxattr 'trusted.$name' failed"
6974
6975                 # Try to set a garbage xattr.
6976                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6977
6978                 setfattr --name=trusted.$name --value="$value" $file1 ||
6979                         error "setxattr 'trusted.$name' failed"
6980
6981                 # Try to remove the xattr from $file1. We don't care if this
6982                 # appears to succeed or fail, we just don't want there to be
6983                 # any changes or crashes.
6984                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6985         done
6986
6987         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6988         then
6989                 name="lfsck_ns"
6990                 # Try to copy xattr from $file0 to $file1.
6991                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6992
6993                 setfattr --name=trusted.$name --value="$value" $file1 ||
6994                         error "setxattr 'trusted.$name' failed"
6995
6996                 # Try to set a garbage xattr.
6997                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6998
6999                 setfattr --name=trusted.$name --value="$value" $file1 ||
7000                         error "setxattr 'trusted.$name' failed"
7001
7002                 # Try to remove the xattr from $file1. We don't care if this
7003                 # appears to succeed or fail, we just don't want there to be
7004                 # any changes or crashes.
7005                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7006         fi
7007
7008         # Get 'after' xattrs of file1.
7009         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7010
7011         if ! diff $xattr0 $xattr1; then
7012                 error "before and after xattrs of '$file1' differ"
7013         fi
7014
7015         rm -rf $file0 $file1 $xattr0 $xattr1
7016
7017         return 0
7018 }
7019 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7020
7021 test_102p() { # LU-4703 setxattr did not check ownership
7022         local testfile=$DIR/$tfile
7023
7024         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7025                 skip "MDS needs to be at least 2.5.56" && return
7026
7027         touch $testfile
7028
7029         echo "setfacl as user..."
7030         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7031         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7032
7033         echo "setfattr as user..."
7034         setfacl -m "u:$RUNAS_ID:---" $testfile
7035         $RUNAS setfattr -x system.posix_acl_access $testfile
7036         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7037 }
7038 run_test 102p "check setxattr(2) correctly fails without permission"
7039
7040 test_102q() {
7041         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7042                 skip "MDS needs to be at least 2.6.92" && return
7043         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7044 }
7045 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7046
7047 test_102r() {
7048         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7049                 skip "MDS needs to be at least 2.6.93" && return
7050         touch $DIR/$tfile || error "touch"
7051         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7052         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7053         rm $DIR/$tfile || error "rm"
7054
7055         #normal directory
7056         mkdir -p $DIR/$tdir || error "mkdir"
7057         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7058         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7059         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7060                 error "$testfile error deleting user.author1"
7061         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7062                 grep "user.$(basename $tdir)" &&
7063                 error "$tdir did not delete user.$(basename $tdir)"
7064         rmdir $DIR/$tdir || error "rmdir"
7065
7066         #striped directory
7067         test_mkdir -p $DIR/$tdir || error "make striped dir"
7068         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7069         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7070         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7071                 error "$testfile error deleting user.author1"
7072         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7073                 grep "user.$(basename $tdir)" &&
7074                 error "$tdir did not delete user.$(basename $tdir)"
7075         rmdir $DIR/$tdir || error "rm striped dir"
7076 }
7077 run_test 102r "set EAs with empty values"
7078
7079 run_acl_subtest()
7080 {
7081     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7082     return $?
7083 }
7084
7085 test_103a() {
7086         [ "$UID" != 0 ] && skip_env "must run as root" && return
7087         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7088                 skip "must have acl enabled" && return
7089         [ -z "$(which setfacl 2>/dev/null)" ] &&
7090                 skip_env "could not find setfacl" && return
7091         $GSS && skip "could not run under gss" && return
7092
7093         gpasswd -a daemon bin                           # LU-5641
7094         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7095
7096         declare -a identity_old
7097
7098         for num in $(seq $MDSCOUNT); do
7099                 switch_identity $num true || identity_old[$num]=$?
7100         done
7101
7102         SAVE_UMASK=$(umask)
7103         umask 0022
7104         cd $DIR
7105
7106         echo "performing cp ..."
7107         run_acl_subtest cp || error "run_acl_subtest cp failed"
7108         echo "performing getfacl-noacl..."
7109         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7110         echo "performing misc..."
7111         run_acl_subtest misc || error  "misc test failed"
7112         echo "performing permissions..."
7113         run_acl_subtest permissions || error "permissions failed"
7114         echo "performing setfacl..."
7115         run_acl_subtest setfacl || error  "setfacl test failed"
7116
7117         # inheritance test got from HP
7118         echo "performing inheritance..."
7119         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7120         chmod +x make-tree || error "chmod +x failed"
7121         run_acl_subtest inheritance || error "inheritance test failed"
7122         rm -f make-tree
7123
7124         echo "LU-974 ignore umask when acl is enabled..."
7125         run_acl_subtest 974 || error "LU-974 umask test failed"
7126         if [ $MDSCOUNT -ge 2 ]; then
7127                 run_acl_subtest 974_remote ||
7128                         error "LU-974 umask test failed under remote dir"
7129         fi
7130
7131         echo "LU-2561 newly created file is same size as directory..."
7132         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7133                 run_acl_subtest 2561 || error "LU-2561 test failed"
7134         else
7135                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7136         fi
7137
7138         run_acl_subtest 4924 || error "LU-4924 test failed"
7139
7140         cd $SAVE_PWD
7141         umask $SAVE_UMASK
7142
7143         for num in $(seq $MDSCOUNT); do
7144                 if [ "${identity_old[$num]}" = 1 ]; then
7145                         switch_identity $num false || identity_old[$num]=$?
7146                 fi
7147         done
7148 }
7149 run_test 103a "acl test ========================================="
7150
7151 test_103b() {
7152         local noacl=false
7153         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7154         local mountopts=$MDS_MOUNT_OPTS
7155
7156         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7157                 noacl=true
7158         else
7159                 # stop the MDT
7160                 stop $SINGLEMDS || error "failed to stop MDT."
7161                 # remount the MDT
7162                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7163                         MDS_MOUNT_OPTS="-o noacl"
7164                 else
7165                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7166                 fi
7167                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7168                         error "failed to start MDT."
7169                 MDS_MOUNT_OPTS=$mountopts
7170         fi
7171
7172         touch $DIR/$tfile
7173         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7174
7175         if ! $noacl; then
7176                 # stop the MDT
7177                 stop $SINGLEMDS || error "failed to stop MDT."
7178                 # remount the MDT
7179                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7180                         error "failed to start MDT."
7181         fi
7182
7183         true
7184 }
7185 run_test 103b "MDS mount option 'noacl'"
7186
7187 test_103c() {
7188         mkdir -p $DIR/$tdir
7189         cp -rp $DIR/$tdir $DIR/$tdir.bak
7190
7191         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7192                 error "$DIR/$tdir shouldn't contain default ACL"
7193         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7194                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7195         true
7196 }
7197 run_test 103c "'cp -rp' won't set empty acl"
7198
7199 test_104a() {
7200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7201         touch $DIR/$tfile
7202         lfs df || error "lfs df failed"
7203         lfs df -ih || error "lfs df -ih failed"
7204         lfs df -h $DIR || error "lfs df -h $DIR failed"
7205         lfs df -i $DIR || error "lfs df -i $DIR failed"
7206         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7207         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7208
7209         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7210         lctl --device %$OSC deactivate
7211         lfs df || error "lfs df with deactivated OSC failed"
7212         lctl --device %$OSC activate
7213         # wait the osc back to normal
7214         wait_osc_import_state client ost FULL
7215
7216         lfs df || error "lfs df with reactivated OSC failed"
7217         rm -f $DIR/$tfile
7218 }
7219 run_test 104a "lfs df [-ih] [path] test ========================="
7220
7221 test_104b() {
7222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7223         [ $RUNAS_ID -eq $UID ] &&
7224                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7225         chmod 666 /dev/obd
7226         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7227                         grep "Permission denied" | wc -l)))
7228         if [ $denied_cnt -ne 0 ]; then
7229                 error "lfs check servers test failed"
7230         fi
7231 }
7232 run_test 104b "$RUNAS lfs check servers test ===================="
7233
7234 test_105a() {
7235         # doesn't work on 2.4 kernels
7236         touch $DIR/$tfile
7237         if $(flock_is_enabled); then
7238                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7239         else
7240                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7241         fi
7242         rm -f $DIR/$tfile
7243 }
7244 run_test 105a "flock when mounted without -o flock test ========"
7245
7246 test_105b() {
7247         touch $DIR/$tfile
7248         if $(flock_is_enabled); then
7249                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7250         else
7251                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7252         fi
7253         rm -f $DIR/$tfile
7254 }
7255 run_test 105b "fcntl when mounted without -o flock test ========"
7256
7257 test_105c() {
7258         touch $DIR/$tfile
7259         if $(flock_is_enabled); then
7260                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7261         else
7262                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7263         fi
7264         rm -f $DIR/$tfile
7265 }
7266 run_test 105c "lockf when mounted without -o flock test ========"
7267
7268 test_105d() { # bug 15924
7269         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7270         test_mkdir -p $DIR/$tdir
7271         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7272         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7273         $LCTL set_param fail_loc=0x80000315
7274         flocks_test 2 $DIR/$tdir
7275 }
7276 run_test 105d "flock race (should not freeze) ========"
7277
7278 test_105e() { # bug 22660 && 22040
7279         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7280         touch $DIR/$tfile
7281         flocks_test 3 $DIR/$tfile
7282 }
7283 run_test 105e "Two conflicting flocks from same process ======="
7284
7285 test_106() { #bug 10921
7286         test_mkdir -p $DIR/$tdir
7287         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7288         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7289 }
7290 run_test 106 "attempt exec of dir followed by chown of that dir"
7291
7292 test_107() {
7293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7294         CDIR=`pwd`
7295         cd $DIR
7296
7297         local file=core
7298         rm -f $file
7299
7300         local save_pattern=$(sysctl -n kernel.core_pattern)
7301         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7302         sysctl -w kernel.core_pattern=$file
7303         sysctl -w kernel.core_uses_pid=0
7304
7305         ulimit -c unlimited
7306         sleep 60 &
7307         SLEEPPID=$!
7308
7309         sleep 1
7310
7311         kill -s 11 $SLEEPPID
7312         wait $SLEEPPID
7313         if [ -e $file ]; then
7314                 size=`stat -c%s $file`
7315                 [ $size -eq 0 ] && error "Fail to create core file $file"
7316         else
7317                 error "Fail to create core file $file"
7318         fi
7319         rm -f $file
7320         sysctl -w kernel.core_pattern=$save_pattern
7321         sysctl -w kernel.core_uses_pid=$save_uses_pid
7322         cd $CDIR
7323 }
7324 run_test 107 "Coredump on SIG"
7325
7326 test_110() {
7327         test_mkdir -p $DIR/$tdir
7328         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7329                 error "mkdir with 255 char failed"
7330         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7331                 error "mkdir with 256 char should fail, but did not"
7332         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7333                 error "create with 255 char failed"
7334         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7335                 error "create with 256 char should fail, but did not"
7336
7337         ls -l $DIR/$tdir
7338         rm -rf $DIR/$tdir
7339 }
7340 run_test 110 "filename length checking"
7341
7342 test_115() {
7343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7344         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7345                 tail -1 | cut -c11-20)
7346         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7347         echo "Starting with $OSTIO_pre threads"
7348
7349         NUMTEST=20000
7350         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7351         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7352         echo "$NUMTEST creates/unlinks"
7353         test_mkdir -p $DIR/$tdir
7354         createmany -o $DIR/$tdir/$tfile $NUMTEST
7355         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7356
7357         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7358                 tail -1 | cut -c11-20)
7359
7360         # don't return an error
7361         [ $OSTIO_post == $OSTIO_pre ] && echo \
7362             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7363             echo "This may be fine, depending on what ran before this test" &&
7364             echo "and how fast this system is." && return
7365
7366         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7367 }
7368 run_test 115 "verify dynamic thread creation===================="
7369
7370 free_min_max () {
7371         wait_delete_completed
7372         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7373         echo OST kbytes available: ${AVAIL[@]}
7374         MAXI=0; MAXV=${AVAIL[0]}
7375         MINI=0; MINV=${AVAIL[0]}
7376         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7377                 #echo OST $i: ${AVAIL[i]}kb
7378                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7379                         MAXV=${AVAIL[i]}; MAXI=$i
7380                 fi
7381                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7382                         MINV=${AVAIL[i]}; MINI=$i
7383                 fi
7384         done
7385         echo Min free space: OST $MINI: $MINV
7386         echo Max free space: OST $MAXI: $MAXV
7387 }
7388
7389 test_116a() { # was previously test_116()
7390         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7391         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7392
7393         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7394
7395         echo -n "Free space priority "
7396         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7397                 head -n1
7398         declare -a AVAIL
7399         free_min_max
7400
7401         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7402         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7403                 && return
7404         trap simple_cleanup_common EXIT
7405
7406
7407         # Check if we need to generate uneven OSTs
7408         test_mkdir -p $DIR/$tdir/OST${MINI}
7409         local FILL=$(($MINV / 4))
7410         local DIFF=$(($MAXV - $MINV))
7411         local DIFF2=$(($DIFF * 100 / $MINV))
7412
7413         local threshold=$(do_facet $SINGLEMDS \
7414                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7415         threshold=${threshold%%%}
7416         echo -n "Check for uneven OSTs: "
7417         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7418
7419         if [[ $DIFF2 -gt $threshold ]]; then
7420                 echo "ok"
7421                 echo "Don't need to fill OST$MINI"
7422         else
7423                 # generate uneven OSTs. Write 2% over the QOS threshold value
7424                 echo "no"
7425                 DIFF=$(($threshold - $DIFF2 + 2))
7426                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7427                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7428                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7429                         error "setstripe failed"
7430                 DIFF=$(($DIFF2 / 2048))
7431                 i=0
7432                 while [ $i -lt $DIFF ]; do
7433                         i=$(($i + 1))
7434                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7435                                 bs=2M count=1 2>/dev/null
7436                         echo -n .
7437                 done
7438                 echo .
7439                 sync
7440                 sleep_maxage
7441                 free_min_max
7442         fi
7443
7444         DIFF=$(($MAXV - $MINV))
7445         DIFF2=$(($DIFF * 100 / $MINV))
7446         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7447         if [[ $DIFF2 -gt $threshold ]]; then
7448                 echo "ok"
7449         else
7450                 echo "failed - QOS mode won't be used"
7451                 skip "QOS imbalance criteria not met"
7452                 simple_cleanup_common
7453                 return
7454         fi
7455
7456         MINI1=$MINI; MINV1=$MINV
7457         MAXI1=$MAXI; MAXV1=$MAXV
7458
7459         # now fill using QOS
7460         $SETSTRIPE -c 1 $DIR/$tdir
7461         FILL=$(($FILL / 200))
7462         if [ $FILL -gt 600 ]; then
7463                 FILL=600
7464         fi
7465         echo "writing $FILL files to QOS-assigned OSTs"
7466         i=0
7467         while [ $i -lt $FILL ]; do
7468                 i=$((i + 1))
7469                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7470                         count=1 2>/dev/null
7471                 echo -n .
7472         done
7473         echo "wrote $i 200k files"
7474         sync
7475         sleep_maxage
7476
7477         echo "Note: free space may not be updated, so measurements might be off"
7478         free_min_max
7479         DIFF2=$(($MAXV - $MINV))
7480         echo "free space delta: orig $DIFF final $DIFF2"
7481         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7482         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7483         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7484         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7485         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7486         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7487         [ $DIFF -gt 0 ] &&
7488                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7489
7490         # Figure out which files were written where
7491         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7492                   awk '/'$MINI1': / {print $2; exit}')
7493         echo $UUID
7494         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7495         echo "$MINC files created on smaller OST $MINI1"
7496         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7497                   awk '/'$MAXI1': / {print $2; exit}')
7498         echo $UUID
7499         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7500         echo "$MAXC files created on larger OST $MAXI1"
7501         FILL=$(($MAXC * 100 / $MINC - 100))
7502         [[ $MINC -gt 0 ]] &&
7503                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7504         [[ $MAXC -gt $MINC ]] ||
7505                 error_ignore LU-9 "stripe QOS didn't balance free space"
7506         simple_cleanup_common
7507 }
7508 run_test 116a "stripe QOS: free space balance ==================="
7509
7510 test_116b() { # LU-2093
7511         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7512         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7513
7514 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7515         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7516                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7517         [ -z "$old_rr" ] && skip "no QOS" && return 0
7518         do_facet $SINGLEMDS lctl set_param \
7519                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7520         mkdir -p $DIR/$tdir
7521         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7522         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7523         do_facet $SINGLEMDS lctl set_param fail_loc=0
7524         rm -rf $DIR/$tdir
7525         do_facet $SINGLEMDS lctl set_param \
7526                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7527 }
7528 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7529
7530 test_117() # bug 10891
7531 {
7532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7533         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7534         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7535         lctl set_param fail_loc=0x21e
7536         > $DIR/$tfile || error "truncate failed"
7537         lctl set_param fail_loc=0
7538         echo "Truncate succeeded."
7539         rm -f $DIR/$tfile
7540 }
7541 run_test 117 "verify osd extend =========="
7542
7543 NO_SLOW_RESENDCOUNT=4
7544 export OLD_RESENDCOUNT=""
7545 set_resend_count () {
7546         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7547         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7548         lctl set_param -n $PROC_RESENDCOUNT $1
7549         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7550 }
7551
7552 # for reduce test_118* time (b=14842)
7553 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7554
7555 # Reset async IO behavior after error case
7556 reset_async() {
7557         FILE=$DIR/reset_async
7558
7559         # Ensure all OSCs are cleared
7560         $SETSTRIPE -c -1 $FILE
7561         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7562         sync
7563         rm $FILE
7564 }
7565
7566 test_118a() #bug 11710
7567 {
7568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7569         reset_async
7570
7571         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7572         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7573         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7574
7575         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7576                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7577                 return 1;
7578         fi
7579         rm -f $DIR/$tfile
7580 }
7581 run_test 118a "verify O_SYNC works =========="
7582
7583 test_118b()
7584 {
7585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7586         remote_ost_nodsh && skip "remote OST with nodsh" && return
7587
7588         reset_async
7589
7590         #define OBD_FAIL_SRV_ENOENT 0x217
7591         set_nodes_failloc "$(osts_nodes)" 0x217
7592         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7593         RC=$?
7594         set_nodes_failloc "$(osts_nodes)" 0
7595         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7596         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7597                     grep -c writeback)
7598
7599         if [[ $RC -eq 0 ]]; then
7600                 error "Must return error due to dropped pages, rc=$RC"
7601                 return 1;
7602         fi
7603
7604         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7605                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7606                 return 1;
7607         fi
7608
7609         echo "Dirty pages not leaked on ENOENT"
7610
7611         # Due to the above error the OSC will issue all RPCs syncronously
7612         # until a subsequent RPC completes successfully without error.
7613         $MULTIOP $DIR/$tfile Ow4096yc
7614         rm -f $DIR/$tfile
7615
7616         return 0
7617 }
7618 run_test 118b "Reclaim dirty pages on fatal error =========="
7619
7620 test_118c()
7621 {
7622         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7623
7624         # for 118c, restore the original resend count, LU-1940
7625         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7626                                 set_resend_count $OLD_RESENDCOUNT
7627         remote_ost_nodsh && skip "remote OST with nodsh" && return
7628
7629         reset_async
7630
7631         #define OBD_FAIL_OST_EROFS               0x216
7632         set_nodes_failloc "$(osts_nodes)" 0x216
7633
7634         # multiop should block due to fsync until pages are written
7635         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7636         MULTIPID=$!
7637         sleep 1
7638
7639         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7640                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7641         fi
7642
7643         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7644                     grep -c writeback)
7645         if [[ $WRITEBACK -eq 0 ]]; then
7646                 error "No page in writeback, writeback=$WRITEBACK"
7647         fi
7648
7649         set_nodes_failloc "$(osts_nodes)" 0
7650         wait $MULTIPID
7651         RC=$?
7652         if [[ $RC -ne 0 ]]; then
7653                 error "Multiop fsync failed, rc=$RC"
7654         fi
7655
7656         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7657         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7658                     grep -c writeback)
7659         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7660                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7661         fi
7662
7663         rm -f $DIR/$tfile
7664         echo "Dirty pages flushed via fsync on EROFS"
7665         return 0
7666 }
7667 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7668
7669 # continue to use small resend count to reduce test_118* time (b=14842)
7670 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7671
7672 test_118d()
7673 {
7674         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7675         remote_ost_nodsh && skip "remote OST with nodsh" && return
7676
7677         reset_async
7678
7679         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7680         set_nodes_failloc "$(osts_nodes)" 0x214
7681         # multiop should block due to fsync until pages are written
7682         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7683         MULTIPID=$!
7684         sleep 1
7685
7686         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7687                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7688         fi
7689
7690         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7691                     grep -c writeback)
7692         if [[ $WRITEBACK -eq 0 ]]; then
7693                 error "No page in writeback, writeback=$WRITEBACK"
7694         fi
7695
7696         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7697         set_nodes_failloc "$(osts_nodes)" 0
7698
7699         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7700         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7701                     grep -c writeback)
7702         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7703                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7704         fi
7705
7706         rm -f $DIR/$tfile
7707         echo "Dirty pages gaurenteed flushed via fsync"
7708         return 0
7709 }
7710 run_test 118d "Fsync validation inject a delay of the bulk =========="
7711
7712 test_118f() {
7713         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7714         reset_async
7715
7716         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7717         lctl set_param fail_loc=0x8000040a
7718
7719         # Should simulate EINVAL error which is fatal
7720         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7721         RC=$?
7722         if [[ $RC -eq 0 ]]; then
7723                 error "Must return error due to dropped pages, rc=$RC"
7724         fi
7725
7726         lctl set_param fail_loc=0x0
7727
7728         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7729         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7730         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7731                     grep -c writeback)
7732         if [[ $LOCKED -ne 0 ]]; then
7733                 error "Locked pages remain in cache, locked=$LOCKED"
7734         fi
7735
7736         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7737                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7738         fi
7739
7740         rm -f $DIR/$tfile
7741         echo "No pages locked after fsync"
7742
7743         reset_async
7744         return 0
7745 }
7746 run_test 118f "Simulate unrecoverable OSC side error =========="
7747
7748 test_118g() {
7749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7750         reset_async
7751
7752         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7753         lctl set_param fail_loc=0x406
7754
7755         # simulate local -ENOMEM
7756         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7757         RC=$?
7758
7759         lctl set_param fail_loc=0
7760         if [[ $RC -eq 0 ]]; then
7761                 error "Must return error due to dropped pages, rc=$RC"
7762         fi
7763
7764         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7765         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7766         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7767                         grep -c writeback)
7768         if [[ $LOCKED -ne 0 ]]; then
7769                 error "Locked pages remain in cache, locked=$LOCKED"
7770         fi
7771
7772         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7773                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7774         fi
7775
7776         rm -f $DIR/$tfile
7777         echo "No pages locked after fsync"
7778
7779         reset_async
7780         return 0
7781 }
7782 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7783
7784 test_118h() {
7785         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7786         remote_ost_nodsh && skip "remote OST with nodsh" && return
7787
7788         reset_async
7789
7790         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7791         set_nodes_failloc "$(osts_nodes)" 0x20e
7792         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7793         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7794         RC=$?
7795
7796         set_nodes_failloc "$(osts_nodes)" 0
7797         if [[ $RC -eq 0 ]]; then
7798                 error "Must return error due to dropped pages, rc=$RC"
7799         fi
7800
7801         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7802         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7803         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7804                     grep -c writeback)
7805         if [[ $LOCKED -ne 0 ]]; then
7806                 error "Locked pages remain in cache, locked=$LOCKED"
7807         fi
7808
7809         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7810                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7811         fi
7812
7813         rm -f $DIR/$tfile
7814         echo "No pages locked after fsync"
7815
7816         return 0
7817 }
7818 run_test 118h "Verify timeout in handling recoverables errors  =========="
7819
7820 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7821
7822 test_118i() {
7823         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7824         remote_ost_nodsh && skip "remote OST with nodsh" && return
7825
7826         reset_async
7827
7828         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7829         set_nodes_failloc "$(osts_nodes)" 0x20e
7830
7831         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7832         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7833         PID=$!
7834         sleep 5
7835         set_nodes_failloc "$(osts_nodes)" 0
7836
7837         wait $PID
7838         RC=$?
7839         if [[ $RC -ne 0 ]]; then
7840                 error "got error, but should be not, rc=$RC"
7841         fi
7842
7843         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7844         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7845         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7846         if [[ $LOCKED -ne 0 ]]; then
7847                 error "Locked pages remain in cache, locked=$LOCKED"
7848         fi
7849
7850         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7851                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7852         fi
7853
7854         rm -f $DIR/$tfile
7855         echo "No pages locked after fsync"
7856
7857         return 0
7858 }
7859 run_test 118i "Fix error before timeout in recoverable error  =========="
7860
7861 [ "$SLOW" = "no" ] && set_resend_count 4
7862
7863 test_118j() {
7864         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7865         remote_ost_nodsh && skip "remote OST with nodsh" && return
7866
7867         reset_async
7868
7869         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7870         set_nodes_failloc "$(osts_nodes)" 0x220
7871
7872         # return -EIO from OST
7873         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7874         RC=$?
7875         set_nodes_failloc "$(osts_nodes)" 0x0
7876         if [[ $RC -eq 0 ]]; then
7877                 error "Must return error due to dropped pages, rc=$RC"
7878         fi
7879
7880         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7881         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7882         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7883         if [[ $LOCKED -ne 0 ]]; then
7884                 error "Locked pages remain in cache, locked=$LOCKED"
7885         fi
7886
7887         # in recoverable error on OST we want resend and stay until it finished
7888         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7889                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7890         fi
7891
7892         rm -f $DIR/$tfile
7893         echo "No pages locked after fsync"
7894
7895         return 0
7896 }
7897 run_test 118j "Simulate unrecoverable OST side error =========="
7898
7899 test_118k()
7900 {
7901         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7902         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7903
7904         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7905         set_nodes_failloc "$(osts_nodes)" 0x20e
7906         test_mkdir -p $DIR/$tdir
7907
7908         for ((i=0;i<10;i++)); do
7909                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7910                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7911                 SLEEPPID=$!
7912                 sleep 0.500s
7913                 kill $SLEEPPID
7914                 wait $SLEEPPID
7915         done
7916
7917         set_nodes_failloc "$(osts_nodes)" 0
7918         rm -rf $DIR/$tdir
7919 }
7920 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7921
7922 test_118l()
7923 {
7924         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7925         # LU-646
7926         test_mkdir -p $DIR/$tdir
7927         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7928         rm -rf $DIR/$tdir
7929 }
7930 run_test 118l "fsync dir ========="
7931
7932 test_118m() # LU-3066
7933 {
7934         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7935         test_mkdir -p $DIR/$tdir
7936         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7937         rm -rf $DIR/$tdir
7938 }
7939 run_test 118m "fdatasync dir ========="
7940
7941 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7942
7943 test_119a() # bug 11737
7944 {
7945         BSIZE=$((512 * 1024))
7946         directio write $DIR/$tfile 0 1 $BSIZE
7947         # We ask to read two blocks, which is more than a file size.
7948         # directio will indicate an error when requested and actual
7949         # sizes aren't equeal (a normal situation in this case) and
7950         # print actual read amount.
7951         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7952         if [ "$NOB" != "$BSIZE" ]; then
7953                 error "read $NOB bytes instead of $BSIZE"
7954         fi
7955         rm -f $DIR/$tfile
7956 }
7957 run_test 119a "Short directIO read must return actual read amount"
7958
7959 test_119b() # bug 11737
7960 {
7961         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7962
7963         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7964         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7965         sync
7966         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7967                 error "direct read failed"
7968         rm -f $DIR/$tfile
7969 }
7970 run_test 119b "Sparse directIO read must return actual read amount"
7971
7972 test_119c() # bug 13099
7973 {
7974         BSIZE=1048576
7975         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7976         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7977         rm -f $DIR/$tfile
7978 }
7979 run_test 119c "Testing for direct read hitting hole"
7980
7981 test_119d() # bug 15950
7982 {
7983         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7984         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7985         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7986         BSIZE=1048576
7987         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7988         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7989         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7990         lctl set_param fail_loc=0x40d
7991         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7992         pid_dio=$!
7993         sleep 1
7994         cat $DIR/$tfile > /dev/null &
7995         lctl set_param fail_loc=0
7996         pid_reads=$!
7997         wait $pid_dio
7998         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7999         sleep 2
8000         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8001         error "the read rpcs have not completed in 2s"
8002         rm -f $DIR/$tfile
8003         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8004 }
8005 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8006
8007 test_120a() {
8008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8009         test_mkdir -p $DIR/$tdir
8010         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8011                skip "no early lock cancel on server" && return 0
8012
8013         lru_resize_disable mdc
8014         lru_resize_disable osc
8015         cancel_lru_locks mdc
8016         # asynchronous object destroy at MDT could cause bl ast to client
8017         cancel_lru_locks osc
8018
8019         stat $DIR/$tdir > /dev/null
8020         can1=$(do_facet $SINGLEMDS \
8021                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8022                awk '/ldlm_cancel/ {print $2}')
8023         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8024                awk '/ldlm_bl_callback/ {print $2}')
8025         test_mkdir -c1 $DIR/$tdir/d1
8026         can2=$(do_facet $SINGLEMDS \
8027                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8028                awk '/ldlm_cancel/ {print $2}')
8029         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8030                awk '/ldlm_bl_callback/ {print $2}')
8031         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8032         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8033         lru_resize_enable mdc
8034         lru_resize_enable osc
8035 }
8036 run_test 120a "Early Lock Cancel: mkdir test"
8037
8038 test_120b() {
8039         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8040         test_mkdir $DIR/$tdir
8041         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8042                skip "no early lock cancel on server" && return 0
8043         lru_resize_disable mdc
8044         lru_resize_disable osc
8045         cancel_lru_locks mdc
8046         stat $DIR/$tdir > /dev/null
8047         can1=$(do_facet $SINGLEMDS \
8048                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8049                awk '/ldlm_cancel/ {print $2}')
8050         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8051                awk '/ldlm_bl_callback/ {print $2}')
8052         touch $DIR/$tdir/f1
8053         can2=$(do_facet $SINGLEMDS \
8054                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8055                awk '/ldlm_cancel/ {print $2}')
8056         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8057                awk '/ldlm_bl_callback/ {print $2}')
8058         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8059         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8060         lru_resize_enable mdc
8061         lru_resize_enable osc
8062 }
8063 run_test 120b "Early Lock Cancel: create test"
8064
8065 test_120c() {
8066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8067         test_mkdir -c1 $DIR/$tdir
8068         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8069                skip "no early lock cancel on server" && return 0
8070         lru_resize_disable mdc
8071         lru_resize_disable osc
8072         test_mkdir -p -c1 $DIR/$tdir/d1
8073         test_mkdir -p -c1 $DIR/$tdir/d2
8074         touch $DIR/$tdir/d1/f1
8075         cancel_lru_locks mdc
8076         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8077         can1=$(do_facet $SINGLEMDS \
8078                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8079                awk '/ldlm_cancel/ {print $2}')
8080         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8081                awk '/ldlm_bl_callback/ {print $2}')
8082         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8083         can2=$(do_facet $SINGLEMDS \
8084                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8085                awk '/ldlm_cancel/ {print $2}')
8086         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8087                awk '/ldlm_bl_callback/ {print $2}')
8088         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8089         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8090         lru_resize_enable mdc
8091         lru_resize_enable osc
8092 }
8093 run_test 120c "Early Lock Cancel: link test"
8094
8095 test_120d() {
8096         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8097         test_mkdir -p -c1 $DIR/$tdir
8098         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8099                skip "no early lock cancel on server" && return 0
8100         lru_resize_disable mdc
8101         lru_resize_disable osc
8102         touch $DIR/$tdir
8103         cancel_lru_locks mdc
8104         stat $DIR/$tdir > /dev/null
8105         can1=$(do_facet $SINGLEMDS \
8106                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8107                awk '/ldlm_cancel/ {print $2}')
8108         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8109                awk '/ldlm_bl_callback/ {print $2}')
8110         chmod a+x $DIR/$tdir
8111         can2=$(do_facet $SINGLEMDS \
8112                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8113                awk '/ldlm_cancel/ {print $2}')
8114         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8115                awk '/ldlm_bl_callback/ {print $2}')
8116         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8117         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8118         lru_resize_enable mdc
8119         lru_resize_enable osc
8120 }
8121 run_test 120d "Early Lock Cancel: setattr test"
8122
8123 test_120e() {
8124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8125         test_mkdir -p -c1 $DIR/$tdir
8126         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8127                skip "no early lock cancel on server" && return 0
8128         lru_resize_disable mdc
8129         lru_resize_disable osc
8130         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8131         cancel_lru_locks mdc
8132         cancel_lru_locks osc
8133         dd if=$DIR/$tdir/f1 of=/dev/null
8134         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8135         # XXX client can not do early lock cancel of OST lock
8136         # during unlink (LU-4206), so cancel osc lock now.
8137         cancel_lru_locks osc
8138         can1=$(do_facet $SINGLEMDS \
8139                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8140                awk '/ldlm_cancel/ {print $2}')
8141         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8142                awk '/ldlm_bl_callback/ {print $2}')
8143         unlink $DIR/$tdir/f1
8144         sleep 5
8145         can2=$(do_facet $SINGLEMDS \
8146                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8147                awk '/ldlm_cancel/ {print $2}')
8148         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8149                awk '/ldlm_bl_callback/ {print $2}')
8150         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8151         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8152         lru_resize_enable mdc
8153         lru_resize_enable osc
8154 }
8155 run_test 120e "Early Lock Cancel: unlink test"
8156
8157 test_120f() {
8158         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8159         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8160                skip "no early lock cancel on server" && return 0
8161         test_mkdir -p -c1 $DIR/$tdir
8162         lru_resize_disable mdc
8163         lru_resize_disable osc
8164         test_mkdir -p -c1 $DIR/$tdir/d1
8165         test_mkdir -p -c1 $DIR/$tdir/d2
8166         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8167         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8168         cancel_lru_locks mdc
8169         cancel_lru_locks osc
8170         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8171         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8172         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8173         # XXX client can not do early lock cancel of OST lock
8174         # during rename (LU-4206), so cancel osc lock now.
8175         cancel_lru_locks osc
8176         can1=$(do_facet $SINGLEMDS \
8177                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8178                awk '/ldlm_cancel/ {print $2}')
8179         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8180                awk '/ldlm_bl_callback/ {print $2}')
8181         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8182         sleep 5
8183         can2=$(do_facet $SINGLEMDS \
8184                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8185                awk '/ldlm_cancel/ {print $2}')
8186         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8187                awk '/ldlm_bl_callback/ {print $2}')
8188         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8189         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8190         lru_resize_enable mdc
8191         lru_resize_enable osc
8192 }
8193 run_test 120f "Early Lock Cancel: rename test"
8194
8195 test_120g() {
8196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8197         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8198                skip "no early lock cancel on server" && return 0
8199         lru_resize_disable mdc
8200         lru_resize_disable osc
8201         count=10000
8202         echo create $count files
8203         test_mkdir -p $DIR/$tdir
8204         cancel_lru_locks mdc
8205         cancel_lru_locks osc
8206         t0=`date +%s`
8207
8208         can0=$(do_facet $SINGLEMDS \
8209                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8210                awk '/ldlm_cancel/ {print $2}')
8211         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8212                awk '/ldlm_bl_callback/ {print $2}')
8213         createmany -o $DIR/$tdir/f $count
8214         sync
8215         can1=$(do_facet $SINGLEMDS \
8216                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8217                awk '/ldlm_cancel/ {print $2}')
8218         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8219                awk '/ldlm_bl_callback/ {print $2}')
8220         t1=$(date +%s)
8221         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8222         echo rm $count files
8223         rm -r $DIR/$tdir
8224         sync
8225         can2=$(do_facet $SINGLEMDS \
8226                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8227                awk '/ldlm_cancel/ {print $2}')
8228         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8229                awk '/ldlm_bl_callback/ {print $2}')
8230         t2=$(date +%s)
8231         echo total: $count removes in $((t2-t1))
8232         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8233         sleep 2
8234         # wait for commitment of removal
8235         lru_resize_enable mdc
8236         lru_resize_enable osc
8237 }
8238 run_test 120g "Early Lock Cancel: performance test"
8239
8240 test_121() { #bug #10589
8241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8242         rm -rf $DIR/$tfile
8243         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8244 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8245         lctl set_param fail_loc=0x310
8246         cancel_lru_locks osc > /dev/null
8247         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8248         lctl set_param fail_loc=0
8249         [[ $reads -eq $writes ]] ||
8250                 error "read $reads blocks, must be $writes blocks"
8251 }
8252 run_test 121 "read cancel race ========="
8253
8254 test_123a() { # was test 123, statahead(bug 11401)
8255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8256         SLOWOK=0
8257         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8258             log "testing on UP system. Performance may be not as good as expected."
8259                         SLOWOK=1
8260         fi
8261
8262         rm -rf $DIR/$tdir
8263         test_mkdir -p $DIR/$tdir
8264         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8265         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8266         MULT=10
8267         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8268                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8269
8270                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8271                 lctl set_param -n llite.*.statahead_max 0
8272                 lctl get_param llite.*.statahead_max
8273                 cancel_lru_locks mdc
8274                 cancel_lru_locks osc
8275                 stime=`date +%s`
8276                 time ls -l $DIR/$tdir | wc -l
8277                 etime=`date +%s`
8278                 delta=$((etime - stime))
8279                 log "ls $i files without statahead: $delta sec"
8280                 lctl set_param llite.*.statahead_max=$max
8281
8282                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8283                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8284                 cancel_lru_locks mdc
8285                 cancel_lru_locks osc
8286                 stime=`date +%s`
8287                 time ls -l $DIR/$tdir | wc -l
8288                 etime=`date +%s`
8289                 delta_sa=$((etime - stime))
8290                 log "ls $i files with statahead: $delta_sa sec"
8291                 lctl get_param -n llite.*.statahead_stats
8292                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8293
8294                 [[ $swrong -lt $ewrong ]] &&
8295                         log "statahead was stopped, maybe too many locks held!"
8296                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8297
8298                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8299                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8300                     lctl set_param -n llite.*.statahead_max 0
8301                     lctl get_param llite.*.statahead_max
8302                     cancel_lru_locks mdc
8303                     cancel_lru_locks osc
8304                     stime=`date +%s`
8305                     time ls -l $DIR/$tdir | wc -l
8306                     etime=`date +%s`
8307                     delta=$((etime - stime))
8308                     log "ls $i files again without statahead: $delta sec"
8309                     lctl set_param llite.*.statahead_max=$max
8310                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8311                         if [  $SLOWOK -eq 0 ]; then
8312                                 error "ls $i files is slower with statahead!"
8313                         else
8314                                 log "ls $i files is slower with statahead!"
8315                         fi
8316                         break
8317                     fi
8318                 fi
8319
8320                 [ $delta -gt 20 ] && break
8321                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8322                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8323         done
8324         log "ls done"
8325
8326         stime=`date +%s`
8327         rm -r $DIR/$tdir
8328         sync
8329         etime=`date +%s`
8330         delta=$((etime - stime))
8331         log "rm -r $DIR/$tdir/: $delta seconds"
8332         log "rm done"
8333         lctl get_param -n llite.*.statahead_stats
8334 }
8335 run_test 123a "verify statahead work"
8336
8337 test_123b () { # statahead(bug 15027)
8338         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8339         test_mkdir -p $DIR/$tdir
8340         createmany -o $DIR/$tdir/$tfile-%d 1000
8341
8342         cancel_lru_locks mdc
8343         cancel_lru_locks osc
8344
8345 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8346         lctl set_param fail_loc=0x80000803
8347         ls -lR $DIR/$tdir > /dev/null
8348         log "ls done"
8349         lctl set_param fail_loc=0x0
8350         lctl get_param -n llite.*.statahead_stats
8351         rm -r $DIR/$tdir
8352         sync
8353
8354 }
8355 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8356
8357 test_124a() {
8358         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8359         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8360                 skip "no lru resize on server" && return 0
8361         local NR=2000
8362         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8363
8364         log "create $NR files at $DIR/$tdir"
8365         createmany -o $DIR/$tdir/f $NR ||
8366                 error "failed to create $NR files in $DIR/$tdir"
8367
8368         cancel_lru_locks mdc
8369         ls -l $DIR/$tdir > /dev/null
8370
8371         local NSDIR=""
8372         local LRU_SIZE=0
8373         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8374                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8375                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8376                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8377                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8378                         log "NSDIR=$NSDIR"
8379                         log "NS=$(basename $NSDIR)"
8380                         break
8381                 fi
8382         done
8383
8384         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8385                 skip "Not enough cached locks created!"
8386                 return 0
8387         fi
8388         log "LRU=$LRU_SIZE"
8389
8390         local SLEEP=30
8391
8392         # We know that lru resize allows one client to hold $LIMIT locks
8393         # for 10h. After that locks begin to be killed by client.
8394         local MAX_HRS=10
8395         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8396         log "LIMIT=$LIMIT"
8397         if [ $LIMIT -lt $LRU_SIZE ]; then
8398             skip "Limit is too small $LIMIT"
8399             return 0
8400         fi
8401
8402         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8403         # killing locks. Some time was spent for creating locks. This means
8404         # that up to the moment of sleep finish we must have killed some of
8405         # them (10-100 locks). This depends on how fast ther were created.
8406         # Many of them were touched in almost the same moment and thus will
8407         # be killed in groups.
8408         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8409
8410         # Use $LRU_SIZE_B here to take into account real number of locks
8411         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8412         local LRU_SIZE_B=$LRU_SIZE
8413         log "LVF=$LVF"
8414         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8415         log "OLD_LVF=$OLD_LVF"
8416         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8417
8418         # Let's make sure that we really have some margin. Client checks
8419         # cached locks every 10 sec.
8420         SLEEP=$((SLEEP+20))
8421         log "Sleep ${SLEEP} sec"
8422         local SEC=0
8423         while ((SEC<$SLEEP)); do
8424                 echo -n "..."
8425                 sleep 5
8426                 SEC=$((SEC+5))
8427                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8428                 echo -n "$LRU_SIZE"
8429         done
8430         echo ""
8431         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8432         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8433
8434         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8435                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8436                 unlinkmany $DIR/$tdir/f $NR
8437                 return
8438         }
8439
8440         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8441         log "unlink $NR files at $DIR/$tdir"
8442         unlinkmany $DIR/$tdir/f $NR
8443 }
8444 run_test 124a "lru resize ======================================="
8445
8446 get_max_pool_limit()
8447 {
8448         local limit=$($LCTL get_param \
8449                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8450         local max=0
8451         for l in $limit; do
8452                 if [[ $l -gt $max ]]; then
8453                         max=$l
8454                 fi
8455         done
8456         echo $max
8457 }
8458
8459 test_124b() {
8460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8461         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8462                 skip "no lru resize on server" && return 0
8463
8464         LIMIT=$(get_max_pool_limit)
8465
8466         NR=$(($(default_lru_size)*20))
8467         if [[ $NR -gt $LIMIT ]]; then
8468                 log "Limit lock number by $LIMIT locks"
8469                 NR=$LIMIT
8470         fi
8471         lru_resize_disable mdc
8472         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8473                 error "failed to create $DIR/$tdir/disable_lru_resize"
8474
8475         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8476         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8477         cancel_lru_locks mdc
8478         stime=`date +%s`
8479         PID=""
8480         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8481         PID="$PID $!"
8482         sleep 2
8483         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8484         PID="$PID $!"
8485         sleep 2
8486         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8487         PID="$PID $!"
8488         wait $PID
8489         etime=`date +%s`
8490         nolruresize_delta=$((etime-stime))
8491         log "ls -la time: $nolruresize_delta seconds"
8492         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8493         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8494
8495         lru_resize_enable mdc
8496         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8497                 error "failed to create $DIR/$tdir/enable_lru_resize"
8498
8499         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8500         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8501         cancel_lru_locks mdc
8502         stime=`date +%s`
8503         PID=""
8504         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8505         PID="$PID $!"
8506         sleep 2
8507         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8508         PID="$PID $!"
8509         sleep 2
8510         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8511         PID="$PID $!"
8512         wait $PID
8513         etime=`date +%s`
8514         lruresize_delta=$((etime-stime))
8515         log "ls -la time: $lruresize_delta seconds"
8516         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8517
8518         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8519                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8520         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8521                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8522         else
8523                 log "lru resize performs the same with no lru resize"
8524         fi
8525         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8526 }
8527 run_test 124b "lru resize (performance test) ======================="
8528
8529 test_124c() {
8530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8531         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8532                 skip "no lru resize on server" && return 0
8533
8534         # cache ununsed locks on client
8535         local nr=100
8536         cancel_lru_locks mdc
8537         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8538         createmany -o $DIR/$tdir/f $nr ||
8539                 error "failed to create $nr files in $DIR/$tdir"
8540         ls -l $DIR/$tdir > /dev/null
8541
8542         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
8543         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
8544         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
8545         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
8546         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
8547
8548         # set lru_max_age to 1 sec
8549         $LCTL set_param $nsdir.lru_max_age=1000 # jiffies
8550         echo "sleep $((recalc_p * 2)) seconds..."
8551         sleep $((recalc_p * 2))
8552
8553         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
8554         # restore lru_max_age
8555         $LCTL set_param -n $nsdir.lru_max_age $max_age
8556         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
8557         unlinkmany $DIR/$tdir/f $nr
8558 }
8559 run_test 124c "LRUR cancel very aged locks"
8560
8561 test_125() { # 13358
8562         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8563         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8564         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8565         test_mkdir -p $DIR/d125 || error "mkdir failed"
8566         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8567         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8568         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8569 }
8570 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8571
8572 test_126() { # bug 12829/13455
8573         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8574         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8575         $GSS && skip "must run as gss disabled" && return
8576
8577         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8578         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8579         rm -f $DIR/$tfile
8580         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8581 }
8582 run_test 126 "check that the fsgid provided by the client is taken into account"
8583
8584 test_127a() { # bug 15521
8585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8586         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8587         $LCTL set_param osc.*.stats=0
8588         FSIZE=$((2048 * 1024))
8589         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8590         cancel_lru_locks osc
8591         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8592
8593         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8594         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8595                 echo "got $COUNT $NAME"
8596                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8597                 eval $NAME=$COUNT || error "Wrong proc format"
8598
8599                 case $NAME in
8600                         read_bytes|write_bytes)
8601                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8602                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8603                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8604                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8605                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8606                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8607                                 error "sumsquare is too small: $SUMSQ"
8608                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8609                                 error "sumsquare is too big: $SUMSQ"
8610                         ;;
8611                         *) ;;
8612                 esac
8613         done < $DIR/${tfile}.tmp
8614
8615         #check that we actually got some stats
8616         [ "$read_bytes" ] || error "Missing read_bytes stats"
8617         [ "$write_bytes" ] || error "Missing write_bytes stats"
8618         [ "$read_bytes" != 0 ] || error "no read done"
8619         [ "$write_bytes" != 0 ] || error "no write done"
8620 }
8621 run_test 127a "verify the client stats are sane"
8622
8623 test_127b() { # bug LU-333
8624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8625         $LCTL set_param llite.*.stats=0
8626         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8627         # perform 2 reads and writes so MAX is different from SUM.
8628         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8629         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8630         cancel_lru_locks osc
8631         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8632         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8633
8634         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8635         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8636                 echo "got $COUNT $NAME"
8637                 eval $NAME=$COUNT || error "Wrong proc format"
8638
8639         case $NAME in
8640                 read_bytes)
8641                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8642                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8643                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8644                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8645                         ;;
8646                 write_bytes)
8647                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8648                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8649                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8650                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8651                         ;;
8652                         *) ;;
8653                 esac
8654         done < $TMP/${tfile}.tmp
8655
8656         #check that we actually got some stats
8657         [ "$read_bytes" ] || error "Missing read_bytes stats"
8658         [ "$write_bytes" ] || error "Missing write_bytes stats"
8659         [ "$read_bytes" != 0 ] || error "no read done"
8660         [ "$write_bytes" != 0 ] || error "no write done"
8661 }
8662 run_test 127b "verify the llite client stats are sane"
8663
8664 test_128() { # bug 15212
8665         touch $DIR/$tfile
8666         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8667                 find $DIR/$tfile
8668                 find $DIR/$tfile
8669         EOF
8670
8671         result=$(grep error $TMP/$tfile.log)
8672         rm -f $DIR/$tfile
8673         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8674 }
8675 run_test 128 "interactive lfs for 2 consecutive find's"
8676
8677 set_dir_limits () {
8678         local mntdev
8679         local canondev
8680         local node
8681
8682         local LDPROC=/proc/fs/ldiskfs
8683         local facets=$(get_facets MDS)
8684
8685         for facet in ${facets//,/ }; do
8686                 canondev=$(ldiskfs_canon \
8687                            *.$(convert_facet2label $facet).mntdev $facet)
8688                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8689                                                 LDPROC=/sys/fs/ldiskfs
8690                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8691         done
8692 }
8693
8694 test_129() {
8695         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8696         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8697                 skip "Only applicable to ldiskfs-based MDTs"
8698                 return
8699         fi
8700         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8701         ENOSPC=28
8702         EFBIG=27
8703
8704         rm -rf $DIR/$tdir
8705         test_mkdir -p $DIR/$tdir
8706
8707         # block size of mds1
8708         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8709         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8710         local MAX=$((MDSBLOCKSIZE * 3))
8711         set_dir_limits $MAX
8712         local I=$(stat -c%s "$DIR/$tdir")
8713         local J=0
8714         local STRIPE_COUNT=1
8715         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8716         MAX=$((MAX*STRIPE_COUNT))
8717         while [[ $I -le $MAX ]]; do
8718                 $MULTIOP $DIR/$tdir/$J Oc
8719                 rc=$?
8720                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8721                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8722                 #and EFBIG for previous versions
8723                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8724                         set_dir_limits 0
8725                         echo "return code $rc received as expected"
8726                         multiop $DIR/$tdir/$J Oc ||
8727                                 error_exit "multiop failed w/o dir size limit"
8728
8729                         I=$(stat -c%s "$DIR/$tdir")
8730
8731                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8732                                         $(version_code 2.4.51) ]
8733                         then
8734                                 [[ $I -eq $MAX ]] && return 0
8735                         else
8736                                 [[ $I -gt $MAX ]] && return 0
8737                         fi
8738                         error_exit "current dir size $I, previous limit $MAX"
8739                 elif [ $rc -ne 0 ]; then
8740                         set_dir_limits 0
8741                         error_exit "return code $rc received instead of expected " \
8742                                    "$EFBIG or $ENOSPC, files in dir $I"
8743                 fi
8744                 J=$((J+1))
8745                 I=$(stat -c%s "$DIR/$tdir")
8746         done
8747
8748         set_dir_limits 0
8749         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8750 }
8751 run_test 129 "test directory size limit ========================"
8752
8753 OLDIFS="$IFS"
8754 cleanup_130() {
8755         trap 0
8756         IFS="$OLDIFS"
8757 }
8758
8759 test_130a() {
8760         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8761         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8762                 return
8763
8764         trap cleanup_130 EXIT RETURN
8765
8766         local fm_file=$DIR/$tfile
8767         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8768         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8769                 error "dd failed for $fm_file"
8770
8771         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8772         filefrag -ves $fm_file
8773         RC=$?
8774         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8775                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8776         [ $RC != 0 ] && error "filefrag $fm_file failed"
8777
8778         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8779                       grep -v "ext:" | grep -v "found")
8780         lun=$($GETSTRIPE -i $fm_file)
8781
8782         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8783         IFS=$'\n'
8784         tot_len=0
8785         for line in $filefrag_op
8786         do
8787                 frag_lun=`echo $line | cut -d: -f5`
8788                 ext_len=`echo $line | cut -d: -f4`
8789                 if (( $frag_lun != $lun )); then
8790                         cleanup_130
8791                         error "FIEMAP on 1-stripe file($fm_file) failed"
8792                         return
8793                 fi
8794                 (( tot_len += ext_len ))
8795         done
8796
8797         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8798                 cleanup_130
8799                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8800                 return
8801         fi
8802
8803         cleanup_130
8804
8805         echo "FIEMAP on single striped file succeeded"
8806 }
8807 run_test 130a "FIEMAP (1-stripe file)"
8808
8809 test_130b() {
8810         [ "$OSTCOUNT" -lt "2" ] &&
8811                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8812
8813         [ "$OSTCOUNT" -ge "10" ] &&
8814                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8815
8816         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8817         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8818                 return
8819
8820         trap cleanup_130 EXIT RETURN
8821
8822         local fm_file=$DIR/$tfile
8823         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8824         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8825                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8826
8827         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8828                 error "dd failed on $fm_file"
8829
8830         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8831         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8832                       grep -v "ext:" | grep -v "found")
8833
8834         last_lun=$(echo $filefrag_op | cut -d: -f5)
8835
8836         IFS=$'\n'
8837         tot_len=0
8838         num_luns=1
8839         for line in $filefrag_op
8840         do
8841                 frag_lun=`echo $line | cut -d: -f5`
8842                 ext_len=`echo $line | cut -d: -f4`
8843                 if (( $frag_lun != $last_lun )); then
8844                         if (( tot_len != 1024 )); then
8845                                 cleanup_130
8846                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8847                                 return
8848                         else
8849                                 (( num_luns += 1 ))
8850                                 tot_len=0
8851                         fi
8852                 fi
8853                 (( tot_len += ext_len ))
8854                 last_lun=$frag_lun
8855         done
8856         if (( num_luns != 2 || tot_len != 1024 )); then
8857                 cleanup_130
8858                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8859                 return
8860         fi
8861
8862         cleanup_130
8863
8864         echo "FIEMAP on 2-stripe file succeeded"
8865 }
8866 run_test 130b "FIEMAP (2-stripe file)"
8867
8868 test_130c() {
8869         [ "$OSTCOUNT" -lt "2" ] &&
8870                 skip_env "skipping FIEMAP on 2-stripe file" && return
8871
8872         [ "$OSTCOUNT" -ge "10" ] &&
8873                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8874
8875         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8876         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8877                 return
8878
8879         trap cleanup_130 EXIT RETURN
8880
8881         local fm_file=$DIR/$tfile
8882         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8883         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8884                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8885
8886         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8887
8888         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8889         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8890
8891         last_lun=`echo $filefrag_op | cut -d: -f5`
8892
8893         IFS=$'\n'
8894         tot_len=0
8895         num_luns=1
8896         for line in $filefrag_op
8897         do
8898                 frag_lun=`echo $line | cut -d: -f5`
8899                 ext_len=`echo $line | cut -d: -f4`
8900                 if (( $frag_lun != $last_lun )); then
8901                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8902                         if (( logical != 512 )); then
8903                                 cleanup_130
8904                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8905                                 return
8906                         fi
8907                         if (( tot_len != 512 )); then
8908                                 cleanup_130
8909                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8910                                 return
8911                         else
8912                                 (( num_luns += 1 ))
8913                                 tot_len=0
8914                         fi
8915                 fi
8916                 (( tot_len += ext_len ))
8917                 last_lun=$frag_lun
8918         done
8919         if (( num_luns != 2 || tot_len != 512 )); then
8920                 cleanup_130
8921                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8922                 return
8923         fi
8924
8925         cleanup_130
8926
8927         echo "FIEMAP on 2-stripe file with hole succeeded"
8928 }
8929 run_test 130c "FIEMAP (2-stripe file with hole)"
8930
8931 test_130d() {
8932         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8933
8934         [ "$OSTCOUNT" -ge "10" ] &&
8935                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8936
8937         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8938         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8939
8940         trap cleanup_130 EXIT RETURN
8941
8942         local fm_file=$DIR/$tfile
8943         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8944         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8945                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8946
8947         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8948         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8949                 error "dd failed on $fm_file"
8950
8951         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8952         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8953                 grep -v "ext:" | grep -v "found"`
8954
8955         last_lun=`echo $filefrag_op | cut -d: -f5`
8956
8957         IFS=$'\n'
8958         tot_len=0
8959         num_luns=1
8960         for line in $filefrag_op
8961         do
8962                 frag_lun=`echo $line | cut -d: -f5`
8963                 ext_len=`echo $line | cut -d: -f4`
8964                 if (( $frag_lun != $last_lun )); then
8965                         if (( tot_len != 1024 )); then
8966                                 cleanup_130
8967                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8968                                 return
8969                         else
8970                                 (( num_luns += 1 ))
8971                                 tot_len=0
8972                         fi
8973                 fi
8974                 (( tot_len += ext_len ))
8975                 last_lun=$frag_lun
8976         done
8977         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8978                 cleanup_130
8979                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8980                 return
8981         fi
8982
8983         cleanup_130
8984
8985         echo "FIEMAP on N-stripe file succeeded"
8986 }
8987 run_test 130d "FIEMAP (N-stripe file)"
8988
8989 test_130e() {
8990         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8991
8992         [ "$OSTCOUNT" -ge "10" ] &&
8993                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8994
8995         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8996         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8997
8998         trap cleanup_130 EXIT RETURN
8999
9000         local fm_file=$DIR/$tfile
9001         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9002         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9003                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9004
9005         NUM_BLKS=512
9006         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9007         for ((i = 0; i < $NUM_BLKS; i++))
9008         do
9009                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9010         done
9011
9012         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9013         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
9014
9015         last_lun=`echo $filefrag_op | cut -d: -f5`
9016
9017         IFS=$'\n'
9018         tot_len=0
9019         num_luns=1
9020         for line in $filefrag_op
9021         do
9022                 frag_lun=`echo $line | cut -d: -f5`
9023                 ext_len=`echo $line | cut -d: -f4`
9024                 if (( $frag_lun != $last_lun )); then
9025                         if (( tot_len != $EXPECTED_LEN )); then
9026                                 cleanup_130
9027                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
9028                                 return
9029                         else
9030                                 (( num_luns += 1 ))
9031                                 tot_len=0
9032                         fi
9033                 fi
9034                 (( tot_len += ext_len ))
9035                 last_lun=$frag_lun
9036         done
9037         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9038                 cleanup_130
9039                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
9040                 return
9041         fi
9042
9043         cleanup_130
9044
9045         echo "FIEMAP with continuation calls succeeded"
9046 }
9047 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9048
9049 # Test for writev/readv
9050 test_131a() {
9051         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
9052         error "writev test failed"
9053         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
9054         error "readv failed"
9055         rm -f $DIR/$tfile
9056 }
9057 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9058
9059 test_131b() {
9060         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
9061         error "append writev test failed"
9062         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
9063         error "append writev test failed"
9064         rm -f $DIR/$tfile
9065 }
9066 run_test 131b "test append writev"
9067
9068 test_131c() {
9069         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9070         error "NOT PASS"
9071 }
9072 run_test 131c "test read/write on file w/o objects"
9073
9074 test_131d() {
9075         rwv -f $DIR/$tfile -w -n 1 1572864
9076         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9077         if [ "$NOB" != 1572864 ]; then
9078                 error "Short read filed: read $NOB bytes instead of 1572864"
9079         fi
9080         rm -f $DIR/$tfile
9081 }
9082 run_test 131d "test short read"
9083
9084 test_131e() {
9085         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9086         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9087         error "read hitting hole failed"
9088         rm -f $DIR/$tfile
9089 }
9090 run_test 131e "test read hitting hole"
9091
9092 check_stats() {
9093         local res
9094         local count
9095         case $1 in
9096         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9097                  ;;
9098         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9099                  ;;
9100         *) error "Wrong argument $1" ;;
9101         esac
9102         echo $res
9103         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9104         # if the argument $3 is zero, it means any stat increment is ok.
9105         if [[ $3 -gt 0 ]]; then
9106                 count=$(echo $res | awk '{ print $2 }')
9107                 [[ $count -ne $3 ]] &&
9108                         error "The $2 counter on $1 is wrong - expected $3"
9109         fi
9110 }
9111
9112 test_133a() {
9113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9114         remote_ost_nodsh && skip "remote OST with nodsh" && return
9115         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9116
9117         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9118                 { skip "MDS doesn't support rename stats"; return; }
9119         local testdir=$DIR/${tdir}/stats_testdir
9120         mkdir -p $DIR/${tdir}
9121
9122         # clear stats.
9123         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9124         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9125
9126         # verify mdt stats first.
9127         mkdir ${testdir} || error "mkdir failed"
9128         check_stats $SINGLEMDS "mkdir" 1
9129         touch ${testdir}/${tfile} || "touch failed"
9130         check_stats $SINGLEMDS "open" 1
9131         check_stats $SINGLEMDS "close" 1
9132         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9133         check_stats $SINGLEMDS "mknod" 1
9134         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9135         check_stats $SINGLEMDS "unlink" 1
9136         rm -f ${testdir}/${tfile} || error "file remove failed"
9137         check_stats $SINGLEMDS "unlink" 2
9138
9139         # remove working dir and check mdt stats again.
9140         rmdir ${testdir} || error "rmdir failed"
9141         check_stats $SINGLEMDS "rmdir" 1
9142
9143         local testdir1=$DIR/${tdir}/stats_testdir1
9144         mkdir -p ${testdir}
9145         mkdir -p ${testdir1}
9146         touch ${testdir1}/test1
9147         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9148         check_stats $SINGLEMDS "crossdir_rename" 1
9149
9150         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9151         check_stats $SINGLEMDS "samedir_rename" 1
9152
9153         rm -rf $DIR/${tdir}
9154 }
9155 run_test 133a "Verifying MDT stats ========================================"
9156
9157 test_133b() {
9158         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9159         remote_ost_nodsh && skip "remote OST with nodsh" && return
9160         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9161         local testdir=$DIR/${tdir}/stats_testdir
9162         mkdir -p ${testdir} || error "mkdir failed"
9163         touch ${testdir}/${tfile} || "touch failed"
9164         cancel_lru_locks mdc
9165
9166         # clear stats.
9167         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9168         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9169
9170         # extra mdt stats verification.
9171         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9172         check_stats $SINGLEMDS "setattr" 1
9173         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9174         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9175         then            # LU-1740
9176                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9177                 check_stats $SINGLEMDS "getattr" 1
9178         fi
9179         $LFS df || error "lfs failed"
9180         check_stats $SINGLEMDS "statfs" 1
9181
9182         rm -rf $DIR/${tdir}
9183 }
9184 run_test 133b "Verifying extra MDT stats =================================="
9185
9186 test_133c() {
9187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9188         remote_ost_nodsh && skip "remote OST with nodsh" && return
9189         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9190         local testdir=$DIR/${tdir}/stats_testdir
9191         test_mkdir -p ${testdir} || error "mkdir failed"
9192
9193         # verify obdfilter stats.
9194         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9195         sync
9196         cancel_lru_locks osc
9197         wait_delete_completed
9198
9199         # clear stats.
9200         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9201         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9202
9203         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9204         sync
9205         cancel_lru_locks osc
9206         check_stats ost "write" 1
9207
9208         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9209         check_stats ost "read" 1
9210
9211         > ${testdir}/${tfile} || error "truncate failed"
9212         check_stats ost "punch" 1
9213
9214         rm -f ${testdir}/${tfile} || error "file remove failed"
9215         wait_delete_completed
9216         check_stats ost "destroy" 1
9217
9218         rm -rf $DIR/${tdir}
9219 }
9220 run_test 133c "Verifying OST stats ========================================"
9221
9222 order_2() {
9223         local value=$1
9224         local orig=$value
9225         local order=1
9226
9227         while [ $value -ge 2 ]; do
9228                 order=$((order*2))
9229                 value=$((value/2))
9230         done
9231
9232         if [ $orig -gt $order ]; then
9233                 order=$((order*2))
9234         fi
9235         echo $order
9236 }
9237
9238 size_in_KMGT() {
9239     local value=$1
9240     local size=('K' 'M' 'G' 'T');
9241     local i=0
9242     local size_string=$value
9243
9244     while [ $value -ge 1024 ]; do
9245         if [ $i -gt 3 ]; then
9246             #T is the biggest unit we get here, if that is bigger,
9247             #just return XXXT
9248             size_string=${value}T
9249             break
9250         fi
9251         value=$((value >> 10))
9252         if [ $value -lt 1024 ]; then
9253             size_string=${value}${size[$i]}
9254             break
9255         fi
9256         i=$((i + 1))
9257     done
9258
9259     echo $size_string
9260 }
9261
9262 get_rename_size() {
9263     local size=$1
9264     local context=${2:-.}
9265     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9266                 grep -A1 $context |
9267                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9268     echo $sample
9269 }
9270
9271 test_133d() {
9272         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9273         remote_ost_nodsh && skip "remote OST with nodsh" && return
9274         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9275         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9276         { skip "MDS doesn't support rename stats"; return; }
9277
9278         local testdir1=$DIR/${tdir}/stats_testdir1
9279         local testdir2=$DIR/${tdir}/stats_testdir2
9280
9281         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9282
9283         mkdir -p ${testdir1} || error "mkdir failed"
9284         mkdir -p ${testdir2} || error "mkdir failed"
9285
9286         createmany -o $testdir1/test 512 || error "createmany failed"
9287
9288         # check samedir rename size
9289         mv ${testdir1}/test0 ${testdir1}/test_0
9290
9291         local testdir1_size=$(ls -l $DIR/${tdir} |
9292                 awk '/stats_testdir1/ {print $5}')
9293         local testdir2_size=$(ls -l $DIR/${tdir} |
9294                 awk '/stats_testdir2/ {print $5}')
9295
9296         testdir1_size=$(order_2 $testdir1_size)
9297         testdir2_size=$(order_2 $testdir2_size)
9298
9299         testdir1_size=$(size_in_KMGT $testdir1_size)
9300         testdir2_size=$(size_in_KMGT $testdir2_size)
9301
9302         echo "source rename dir size: ${testdir1_size}"
9303         echo "target rename dir size: ${testdir2_size}"
9304
9305         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9306         eval $cmd || error "$cmd failed"
9307         local samedir=$($cmd | grep 'same_dir')
9308         local same_sample=$(get_rename_size $testdir1_size)
9309         [ -z "$samedir" ] && error "samedir_rename_size count error"
9310         [[ $same_sample -eq 1 ]] ||
9311                 error "samedir_rename_size error $same_sample"
9312         echo "Check same dir rename stats success"
9313
9314         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9315
9316         # check crossdir rename size
9317         mv ${testdir1}/test_0 ${testdir2}/test_0
9318
9319         testdir1_size=$(ls -l $DIR/${tdir} |
9320                 awk '/stats_testdir1/ {print $5}')
9321         testdir2_size=$(ls -l $DIR/${tdir} |
9322                 awk '/stats_testdir2/ {print $5}')
9323
9324         testdir1_size=$(order_2 $testdir1_size)
9325         testdir2_size=$(order_2 $testdir2_size)
9326
9327         testdir1_size=$(size_in_KMGT $testdir1_size)
9328         testdir2_size=$(size_in_KMGT $testdir2_size)
9329
9330         echo "source rename dir size: ${testdir1_size}"
9331         echo "target rename dir size: ${testdir2_size}"
9332
9333         eval $cmd || error "$cmd failed"
9334         local crossdir=$($cmd | grep 'crossdir')
9335         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9336         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9337         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9338         [[ $src_sample -eq 1 ]] ||
9339                 error "crossdir_rename_size error $src_sample"
9340         [[ $tgt_sample -eq 1 ]] ||
9341                 error "crossdir_rename_size error $tgt_sample"
9342         echo "Check cross dir rename stats success"
9343         rm -rf $DIR/${tdir}
9344 }
9345 run_test 133d "Verifying rename_stats ========================================"
9346
9347 test_133e() {
9348         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9349         remote_ost_nodsh && skip "remote OST with nodsh" && return
9350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9351         local testdir=$DIR/${tdir}/stats_testdir
9352         local ctr f0 f1 bs=32768 count=42 sum
9353
9354         mkdir -p ${testdir} || error "mkdir failed"
9355
9356         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9357
9358         for ctr in {write,read}_bytes; do
9359                 sync
9360                 cancel_lru_locks osc
9361
9362                 do_facet ost1 $LCTL set_param -n \
9363                         "obdfilter.*.exports.clear=clear"
9364
9365                 if [ $ctr = write_bytes ]; then
9366                         f0=/dev/zero
9367                         f1=${testdir}/${tfile}
9368                 else
9369                         f0=${testdir}/${tfile}
9370                         f1=/dev/null
9371                 fi
9372
9373                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9374                         error "dd failed"
9375                 sync
9376                 cancel_lru_locks osc
9377
9378                 sum=$(do_facet ost1 $LCTL get_param \
9379                         "obdfilter.*.exports.*.stats" |
9380                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9381                                 $1 == ctr { sum += $7 }
9382                                 END { printf("%0.0f", sum) }')
9383
9384                 if ((sum != bs * count)); then
9385                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9386                 fi
9387         done
9388
9389         rm -rf $DIR/${tdir}
9390 }
9391 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9392
9393 test_133f() {
9394         local proc_dirs
9395
9396         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9397 /sys/fs/lustre/ /sys/fs/lnet/"
9398         local dir
9399         for dir in $dirs; do
9400                 if [ -d $dir ]; then
9401                         proc_dirs="$proc_dirs $dir"
9402                 fi
9403         done
9404
9405         local facet
9406
9407         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9408         remote_ost_nodsh && skip "remote OST with nodsh" && return
9409         # First without trusting modes.
9410         find $proc_dirs -exec cat '{}' \; &> /dev/null
9411
9412         # Second verifying readability.
9413         find $proc_dirs \
9414                 -type f \
9415                 -exec cat '{}' \; &> /dev/null ||
9416                         error "proc file read failed"
9417
9418         for facet in $SINGLEMDS ost1; do
9419                 do_facet $facet find $proc_dirs \
9420                         ! -name req_history \
9421                         -exec cat '{}' \\\; &> /dev/null
9422
9423                 do_facet $facet find $proc_dirs \
9424                         ! -name req_history \
9425                         -type f \
9426                         -exec cat '{}' \\\; &> /dev/null ||
9427                                 error "proc file read failed"
9428         done
9429 }
9430 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9431
9432 test_133g() {
9433         local proc_dirs
9434
9435         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9436 /sys/fs/lustre/ /sys/fs/lnet/"
9437         local dir
9438         for dir in $dirs; do
9439                 if [ -d $dir ]; then
9440                         proc_dirs="$proc_dirs $dir"
9441                 fi
9442         done
9443
9444         local facet
9445
9446         # Second verifying readability.
9447         find $proc_dirs \
9448                 -type f \
9449                 -not -name force_lbug \
9450                 -not -name changelog_mask \
9451                 -exec badarea_io '{}' \; &> /dev/null ||
9452                 error "find $proc_dirs failed"
9453
9454         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9455                 skip "Too old lustre on MDS" && return
9456
9457         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9458                 skip "Too old lustre on ost1" && return
9459
9460         for facet in $SINGLEMDS ost1; do
9461                 do_facet $facet find $proc_dirs \
9462                         -type f \
9463                         -not -name force_lbug \
9464                         -not -name changelog_mask \
9465                         -exec badarea_io '{}' \\\; &> /dev/null ||
9466                 error "$facet find $proc_dirs failed"
9467
9468         done
9469
9470         # remount the FS in case writes/reads /proc break the FS
9471         cleanup || error "failed to unmount"
9472         setup || error "failed to setup"
9473         true
9474 }
9475 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9476
9477 test_134a() {
9478         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9479                 skip "Need MDS version at least 2.7.54" && return
9480
9481         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9482         cancel_lru_locks mdc
9483
9484         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9485         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9486         [ $unused -eq 0 ] || "$unused locks are not cleared"
9487
9488         local nr=1000
9489         createmany -o $DIR/$tdir/f $nr ||
9490                 error "failed to create $nr files in $DIR/$tdir"
9491         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9492
9493         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
9494         do_facet mds1 $LCTL set_param fail_loc=0x327
9495         do_facet mds1 $LCTL set_param fail_val=500
9496         touch $DIR/$tdir/m
9497
9498         echo "sleep 10 seconds ..."
9499         sleep 10
9500         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
9501
9502         do_facet mds1 $LCTL set_param fail_loc=0
9503         do_facet mds1 $LCTL set_param fail_val=0
9504         [ $lck_cnt -lt $unused ] ||
9505                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
9506
9507         rm $DIR/$tdir/m
9508         unlinkmany $DIR/$tdir/f $nr
9509 }
9510 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
9511
9512 test_134b() {
9513         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
9514                 skip "Need MDS version at least 2.7.54" && return
9515
9516         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9517         cancel_lru_locks mdc
9518
9519         local low_wm=$(do_facet mds1 $LCTL get_param -n \
9520                         ldlm.lock_reclaim_threshold_mb)
9521         # disable reclaim temporarily
9522         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
9523
9524         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
9525         do_facet mds1 $LCTL set_param fail_loc=0x328
9526         do_facet mds1 $LCTL set_param fail_val=500
9527
9528         $LCTL set_param debug=+trace
9529
9530         local nr=600
9531         createmany -o $DIR/$tdir/f $nr &
9532         local create_pid=$!
9533
9534         echo "Sleep $TIMEOUT seconds ..."
9535         sleep $TIMEOUT
9536         if ! ps -p $create_pid  > /dev/null 2>&1; then
9537                 do_facet mds1 $LCTL set_param fail_loc=0
9538                 do_facet mds1 $LCTL set_param fail_val=0
9539                 do_facet mds1 $LCTL set_param \
9540                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
9541                 error "createmany finished incorrectly!"
9542         fi
9543         do_facet mds1 $LCTL set_param fail_loc=0
9544         do_facet mds1 $LCTL set_param fail_val=0
9545         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
9546         wait $create_pid || return 1
9547
9548         unlinkmany $DIR/$tdir/f $nr
9549 }
9550 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
9551
9552 test_140() { #bug-17379
9553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9554         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9555         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9556         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9557
9558         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9559         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9560         local i=0
9561         while i=`expr $i + 1`; do
9562                 test_mkdir -p $i || error "Creating dir $i"
9563                 cd $i || error "Changing to $i"
9564                 ln -s ../stat stat || error "Creating stat symlink"
9565                 # Read the symlink until ELOOP present,
9566                 # not LBUGing the system is considered success,
9567                 # we didn't overrun the stack.
9568                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9569                 [ $ret -ne 0 ] && {
9570                         if [ $ret -eq 40 ]; then
9571                                 break  # -ELOOP
9572                         else
9573                                 error "Open stat symlink"
9574                                 return
9575                         fi
9576                 }
9577         done
9578         i=`expr $i - 1`
9579         echo "The symlink depth = $i"
9580         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9581                                         error "Invalid symlink depth"
9582
9583         # Test recursive symlink
9584         ln -s symlink_self symlink_self
9585         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9586         echo "open symlink_self returns $ret"
9587         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9588 }
9589 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9590
9591 test_150() {
9592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9593         local TF="$TMP/$tfile"
9594
9595         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9596         cp $TF $DIR/$tfile
9597         cancel_lru_locks osc
9598         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9599         remount_client $MOUNT
9600         df -P $MOUNT
9601         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9602
9603         $TRUNCATE $TF 6000
9604         $TRUNCATE $DIR/$tfile 6000
9605         cancel_lru_locks osc
9606         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9607
9608         echo "12345" >>$TF
9609         echo "12345" >>$DIR/$tfile
9610         cancel_lru_locks osc
9611         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9612
9613         echo "12345" >>$TF
9614         echo "12345" >>$DIR/$tfile
9615         cancel_lru_locks osc
9616         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9617
9618         rm -f $TF
9619         true
9620 }
9621 run_test 150 "truncate/append tests"
9622
9623 #LU-2902 roc_hit was not able to read all values from lproc
9624 function roc_hit_init() {
9625         local list=$(comma_list $(osts_nodes))
9626         local dir=$DIR/$tdir-check
9627         local file=$dir/file
9628         local BEFORE
9629         local AFTER
9630         local idx
9631
9632         test_mkdir -p $dir
9633         #use setstripe to do a write to every ost
9634         for i in $(seq 0 $((OSTCOUNT-1))); do
9635                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9636                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9637                 idx=$(printf %04x $i)
9638                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9639                         awk '$1 == "cache_access" {sum += $7}
9640                                 END { printf("%0.0f", sum) }')
9641
9642                 cancel_lru_locks osc
9643                 cat $file >/dev/null
9644
9645                 AFTER=$(get_osd_param $list *OST*$idx stats |
9646                         awk '$1 == "cache_access" {sum += $7}
9647                                 END { printf("%0.0f", sum) }')
9648
9649                 echo BEFORE:$BEFORE AFTER:$AFTER
9650                 if ! let "AFTER - BEFORE == 4"; then
9651                         rm -rf $dir
9652                         error "roc_hit is not safe to use"
9653                 fi
9654                 rm $file
9655         done
9656
9657         rm -rf $dir
9658 }
9659
9660 function roc_hit() {
9661         local list=$(comma_list $(osts_nodes))
9662         echo $(get_osd_param $list '' stats |
9663                 awk '$1 == "cache_hit" {sum += $7}
9664                         END { printf("%0.0f", sum) }')
9665 }
9666
9667 function set_cache() {
9668         local on=1
9669
9670         if [ "$2" == "off" ]; then
9671                 on=0;
9672         fi
9673         local list=$(comma_list $(osts_nodes))
9674         set_osd_param $list '' $1_cache_enable $on
9675
9676         cancel_lru_locks osc
9677 }
9678
9679 test_151() {
9680         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9681         remote_ost_nodsh && skip "remote OST with nodsh" && return
9682
9683         local CPAGES=3
9684         local list=$(comma_list $(osts_nodes))
9685
9686         # check whether obdfilter is cache capable at all
9687         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9688                 echo "not cache-capable obdfilter"
9689                 return 0
9690         fi
9691
9692         # check cache is enabled on all obdfilters
9693         if get_osd_param $list '' read_cache_enable | grep 0; then
9694                 echo "oss cache is disabled"
9695                 return 0
9696         fi
9697
9698         set_osd_param $list '' writethrough_cache_enable 1
9699
9700         # check write cache is enabled on all obdfilters
9701         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9702                 echo "oss write cache is NOT enabled"
9703                 return 0
9704         fi
9705
9706         roc_hit_init
9707
9708         #define OBD_FAIL_OBD_NO_LRU  0x609
9709         do_nodes $list $LCTL set_param fail_loc=0x609
9710
9711         # pages should be in the case right after write
9712         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9713                 error "dd failed"
9714
9715         local BEFORE=$(roc_hit)
9716         cancel_lru_locks osc
9717         cat $DIR/$tfile >/dev/null
9718         local AFTER=$(roc_hit)
9719
9720         do_nodes $list $LCTL set_param fail_loc=0
9721
9722         if ! let "AFTER - BEFORE == CPAGES"; then
9723                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9724         fi
9725
9726         # the following read invalidates the cache
9727         cancel_lru_locks osc
9728         set_osd_param $list '' read_cache_enable 0
9729         cat $DIR/$tfile >/dev/null
9730
9731         # now data shouldn't be found in the cache
9732         BEFORE=$(roc_hit)
9733         cancel_lru_locks osc
9734         cat $DIR/$tfile >/dev/null
9735         AFTER=$(roc_hit)
9736         if let "AFTER - BEFORE != 0"; then
9737                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9738         fi
9739
9740         set_osd_param $list '' read_cache_enable 1
9741         rm -f $DIR/$tfile
9742 }
9743 run_test 151 "test cache on oss and controls ==============================="
9744
9745 test_152() {
9746         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9747         local TF="$TMP/$tfile"
9748
9749         # simulate ENOMEM during write
9750 #define OBD_FAIL_OST_NOMEM      0x226
9751         lctl set_param fail_loc=0x80000226
9752         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9753         cp $TF $DIR/$tfile
9754         sync || error "sync failed"
9755         lctl set_param fail_loc=0
9756
9757         # discard client's cache
9758         cancel_lru_locks osc
9759
9760         # simulate ENOMEM during read
9761         lctl set_param fail_loc=0x80000226
9762         cmp $TF $DIR/$tfile || error "cmp failed"
9763         lctl set_param fail_loc=0
9764
9765         rm -f $TF
9766 }
9767 run_test 152 "test read/write with enomem ============================"
9768
9769 test_153() {
9770         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9771 }
9772 run_test 153 "test if fdatasync does not crash ======================="
9773
9774 dot_lustre_fid_permission_check() {
9775         local fid=$1
9776         local ffid=$MOUNT/.lustre/fid/$fid
9777         local test_dir=$2
9778
9779         echo "stat fid $fid"
9780         stat $ffid > /dev/null || error "stat $ffid failed."
9781         echo "touch fid $fid"
9782         touch $ffid || error "touch $ffid failed."
9783         echo "write to fid $fid"
9784         cat /etc/hosts > $ffid || error "write $ffid failed."
9785         echo "read fid $fid"
9786         diff /etc/hosts $ffid || error "read $ffid failed."
9787         echo "append write to fid $fid"
9788         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9789         echo "rename fid $fid"
9790         mv $ffid $test_dir/$tfile.1 &&
9791                 error "rename $ffid to $tfile.1 should fail."
9792         touch $test_dir/$tfile.1
9793         mv $test_dir/$tfile.1 $ffid &&
9794                 error "rename $tfile.1 to $ffid should fail."
9795         rm -f $test_dir/$tfile.1
9796         echo "truncate fid $fid"
9797         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9798         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9799                 echo "link fid $fid"
9800                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9801         fi
9802         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9803                 echo "setfacl fid $fid"
9804                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9805                 echo "getfacl fid $fid"
9806                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9807         fi
9808         echo "unlink fid $fid"
9809         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9810         echo "mknod fid $fid"
9811         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9812
9813         fid=[0xf00000400:0x1:0x0]
9814         ffid=$MOUNT/.lustre/fid/$fid
9815
9816         echo "stat non-exist fid $fid"
9817         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9818         echo "write to non-exist fid $fid"
9819         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9820         echo "link new fid $fid"
9821         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9822
9823         mkdir -p $test_dir/$tdir
9824         touch $test_dir/$tdir/$tfile
9825         fid=$($LFS path2fid $test_dir/$tdir)
9826         rc=$?
9827         [ $rc -ne 0 ] &&
9828                 error "error: could not get fid for $test_dir/$dir/$tfile."
9829
9830         ffid=$MOUNT/.lustre/fid/$fid
9831
9832         echo "ls $fid"
9833         ls $ffid > /dev/null || error "ls $ffid failed."
9834         echo "touch $fid/$tfile.1"
9835         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9836
9837         echo "touch $MOUNT/.lustre/fid/$tfile"
9838         touch $MOUNT/.lustre/fid/$tfile && \
9839                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9840
9841         echo "setxattr to $MOUNT/.lustre/fid"
9842         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9843
9844         echo "listxattr for $MOUNT/.lustre/fid"
9845         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9846
9847         echo "delxattr from $MOUNT/.lustre/fid"
9848         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9849
9850         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9851         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9852                 error "touch invalid fid should fail."
9853
9854         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9855         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9856                 error "touch non-normal fid should fail."
9857
9858         echo "rename $tdir to $MOUNT/.lustre/fid"
9859         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9860                 error "rename to $MOUNT/.lustre/fid should fail."
9861
9862         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9863         then            # LU-3547
9864                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9865                 local new_obf_mode=777
9866
9867                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9868                 chmod $new_obf_mode $DIR/.lustre/fid ||
9869                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9870
9871                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9872                 [ $obf_mode -eq $new_obf_mode ] ||
9873                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9874
9875                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9876                 chmod $old_obf_mode $DIR/.lustre/fid ||
9877                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9878         fi
9879
9880         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9881         fid=$($LFS path2fid $test_dir/$tfile-2)
9882
9883         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9884         then # LU-5424
9885                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9886                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9887                         error "create lov data thru .lustre failed"
9888         fi
9889         echo "cp /etc/passwd $test_dir/$tfile-2"
9890         cp /etc/passwd $test_dir/$tfile-2 ||
9891                 error "copy to $test_dir/$tfile-2 failed."
9892         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9893         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9894                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9895
9896         rm -rf $test_dir/tfile.lnk
9897         rm -rf $test_dir/$tfile-2
9898 }
9899
9900 test_154A() {
9901         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9902                 skip "Need MDS version at least 2.4.1" && return
9903
9904         touch $DIR/$tfile
9905         local FID=$($LFS path2fid $DIR/$tfile)
9906         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9907
9908         # check that we get the same pathname back
9909         local FOUND=$($LFS fid2path $MOUNT "$FID")
9910         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9911         [ "$FOUND" != "$DIR/$tfile" ] &&
9912                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9913
9914         rm -rf $DIR/$tfile
9915 }
9916 run_test 154A "lfs path2fid and fid2path basic checks"
9917
9918 test_154a() {
9919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9920         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9921                 { skip "Need MDS version at least 2.2.51"; return 0; }
9922         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9923
9924         cp /etc/hosts $DIR/$tfile
9925
9926         fid=$($LFS path2fid $DIR/$tfile)
9927         rc=$?
9928         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9929
9930         dot_lustre_fid_permission_check "$fid" $DIR ||
9931                 error "dot lustre permission check $fid failed"
9932
9933         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9934
9935         touch $MOUNT/.lustre/file &&
9936                 error "creation is not allowed under .lustre"
9937
9938         mkdir $MOUNT/.lustre/dir &&
9939                 error "mkdir is not allowed under .lustre"
9940
9941         rm -rf $DIR/$tfile
9942 }
9943 run_test 154a "Open-by-FID"
9944
9945 test_154b() {
9946         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9947         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9948                 { skip "Need MDS version at least 2.2.51"; return 0; }
9949
9950         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9951
9952         local remote_dir=$DIR/$tdir/remote_dir
9953         local MDTIDX=1
9954         local rc=0
9955
9956         mkdir -p $DIR/$tdir
9957         $LFS mkdir -i $MDTIDX $remote_dir ||
9958                 error "create remote directory failed"
9959
9960         cp /etc/hosts $remote_dir/$tfile
9961
9962         fid=$($LFS path2fid $remote_dir/$tfile)
9963         rc=$?
9964         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9965
9966         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9967                 error "dot lustre permission check $fid failed"
9968         rm -rf $DIR/$tdir
9969 }
9970 run_test 154b "Open-by-FID for remote directory"
9971
9972 test_154c() {
9973         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9974                 skip "Need MDS version at least 2.4.1" && return
9975
9976         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9977         local FID1=$($LFS path2fid $DIR/$tfile.1)
9978         local FID2=$($LFS path2fid $DIR/$tfile.2)
9979         local FID3=$($LFS path2fid $DIR/$tfile.3)
9980
9981         local N=1
9982         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9983                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9984                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9985                 local want=FID$N
9986                 [ "$FID" = "${!want}" ] ||
9987                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9988                 N=$((N + 1))
9989         done
9990
9991         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
9992         do
9993                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9994                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9995                 N=$((N + 1))
9996         done
9997 }
9998 run_test 154c "lfs path2fid and fid2path multiple arguments"
9999
10000 test_154d() {
10001         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10002                 skip "Need MDS version at least 2.5.53" && return
10003
10004         if remote_mds; then
10005                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10006         else
10007                 nid="0@lo"
10008         fi
10009         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10010         local fd
10011         local cmd
10012
10013         rm -f $DIR/$tfile
10014         touch $DIR/$tfile
10015
10016         local fid=$($LFS path2fid $DIR/$tfile)
10017         # Open the file
10018         fd=$(free_fd)
10019         cmd="exec $fd<$DIR/$tfile"
10020         eval $cmd
10021         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10022         echo "$fid_list" | grep "$fid"
10023         rc=$?
10024
10025         cmd="exec $fd>/dev/null"
10026         eval $cmd
10027         if [ $rc -ne 0 ]; then
10028                 error "FID $fid not found in open files list $fid_list"
10029         fi
10030 }
10031 run_test 154d "Verify open file fid"
10032
10033 test_154e()
10034 {
10035         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10036                 skip "Need MDS version at least 2.6.50" && return
10037
10038         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10039                 error ".lustre returned by readdir"
10040         fi
10041 }
10042 run_test 154e ".lustre is not returned by readdir"
10043
10044 test_154f() {
10045         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10046         test_mkdir -p -c1 $DIR/$tdir/d
10047         # test dirs inherit from its stripe
10048         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10049         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10050         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10051         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10052         touch $DIR/f
10053
10054         # get fid of parents
10055         local FID0=$($LFS path2fid $DIR/$tdir/d)
10056         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10057         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10058         local FID3=$($LFS path2fid $DIR)
10059
10060         # check that path2fid --parents returns expected <parent_fid>/name
10061         # 1) test for a directory (single parent)
10062         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10063         [ "$parent" == "$FID0/foo1" ] ||
10064                 error "expected parent: $FID0/foo1, got: $parent"
10065
10066         # 2) test for a file with nlink > 1 (multiple parents)
10067         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10068         echo "$parent" | grep -F "$FID1/$tfile" ||
10069                 error "$FID1/$tfile not returned in parent list"
10070         echo "$parent" | grep -F "$FID2/link" ||
10071                 error "$FID2/link not returned in parent list"
10072
10073         # 3) get parent by fid
10074         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10075         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10076         echo "$parent" | grep -F "$FID1/$tfile" ||
10077                 error "$FID1/$tfile not returned in parent list (by fid)"
10078         echo "$parent" | grep -F "$FID2/link" ||
10079                 error "$FID2/link not returned in parent list (by fid)"
10080
10081         # 4) test for entry in root directory
10082         parent=$($LFS path2fid --parents $DIR/f)
10083         echo "$parent" | grep -F "$FID3/f" ||
10084                 error "$FID3/f not returned in parent list"
10085
10086         # 5) test it on root directory
10087         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10088                 error "$MOUNT should not have parents"
10089
10090         # enable xattr caching and check that linkea is correctly updated
10091         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10092         save_lustre_params client "llite.*.xattr_cache" > $save
10093         lctl set_param llite.*.xattr_cache 1
10094
10095         # 6.1) linkea update on rename
10096         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10097
10098         # get parents by fid
10099         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10100         # foo1 should no longer be returned in parent list
10101         echo "$parent" | grep -F "$FID1" &&
10102                 error "$FID1 should no longer be in parent list"
10103         # the new path should appear
10104         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10105                 error "$FID2/$tfile.moved is not in parent list"
10106
10107         # 6.2) linkea update on unlink
10108         rm -f $DIR/$tdir/d/foo2/link
10109         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10110         # foo2/link should no longer be returned in parent list
10111         echo "$parent" | grep -F "$FID2/link" &&
10112                 error "$FID2/link should no longer be in parent list"
10113         true
10114
10115         rm -f $DIR/f
10116         restore_lustre_params < $save
10117 }
10118 run_test 154f "get parent fids by reading link ea"
10119
10120 test_154g()
10121 {
10122         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) ]] ||
10123                 { skip "Need MDS version at least 2.6.92"; return 0; }
10124
10125         mkdir -p $DIR/$tdir
10126         llapi_fid_test -d $DIR/$tdir
10127 }
10128 run_test 154g "various llapi FID tests"
10129
10130 test_155_small_load() {
10131     local temp=$TMP/$tfile
10132     local file=$DIR/$tfile
10133
10134     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10135         error "dd of=$temp bs=6096 count=1 failed"
10136     cp $temp $file
10137     cancel_lru_locks osc
10138     cmp $temp $file || error "$temp $file differ"
10139
10140     $TRUNCATE $temp 6000
10141     $TRUNCATE $file 6000
10142     cmp $temp $file || error "$temp $file differ (truncate1)"
10143
10144     echo "12345" >>$temp
10145     echo "12345" >>$file
10146     cmp $temp $file || error "$temp $file differ (append1)"
10147
10148     echo "12345" >>$temp
10149     echo "12345" >>$file
10150     cmp $temp $file || error "$temp $file differ (append2)"
10151
10152     rm -f $temp $file
10153     true
10154 }
10155
10156 test_155_big_load() {
10157     remote_ost_nodsh && skip "remote OST with nodsh" && return
10158     local temp=$TMP/$tfile
10159     local file=$DIR/$tfile
10160
10161     free_min_max
10162     local cache_size=$(do_facet ost$((MAXI+1)) \
10163         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10164     local large_file_size=$((cache_size * 2))
10165
10166     echo "OSS cache size: $cache_size KB"
10167     echo "Large file size: $large_file_size KB"
10168
10169     [ $MAXV -le $large_file_size ] && \
10170         skip_env "max available OST size needs > $large_file_size KB" && \
10171         return 0
10172
10173     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10174
10175     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10176         error "dd of=$temp bs=$large_file_size count=1k failed"
10177     cp $temp $file
10178     ls -lh $temp $file
10179     cancel_lru_locks osc
10180     cmp $temp $file || error "$temp $file differ"
10181
10182     rm -f $temp $file
10183     true
10184 }
10185
10186 test_155a() {
10187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10188         set_cache read on
10189         set_cache writethrough on
10190         test_155_small_load
10191 }
10192 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10193
10194 test_155b() {
10195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10196         set_cache read on
10197         set_cache writethrough off
10198         test_155_small_load
10199 }
10200 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10201
10202 test_155c() {
10203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10204         set_cache read off
10205         set_cache writethrough on
10206         test_155_small_load
10207 }
10208 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10209
10210 test_155d() {
10211         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10212         set_cache read off
10213         set_cache writethrough off
10214         test_155_small_load
10215 }
10216 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10217
10218 test_155e() {
10219         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10220         set_cache read on
10221         set_cache writethrough on
10222         test_155_big_load
10223 }
10224 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10225
10226 test_155f() {
10227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10228         set_cache read on
10229         set_cache writethrough off
10230         test_155_big_load
10231 }
10232 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10233
10234 test_155g() {
10235         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10236         set_cache read off
10237         set_cache writethrough on
10238         test_155_big_load
10239 }
10240 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10241
10242 test_155h() {
10243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10244         set_cache read off
10245         set_cache writethrough off
10246         test_155_big_load
10247 }
10248 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10249
10250 test_156() {
10251         remote_ost_nodsh && skip "remote OST with nodsh" && return
10252         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10253         local CPAGES=3
10254         local BEFORE
10255         local AFTER
10256         local file="$DIR/$tfile"
10257
10258         [ "$(facet_fstype ost1)" = "zfs" -a \
10259            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10260                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10261                 return
10262
10263         roc_hit_init
10264
10265         log "Turn on read and write cache"
10266         set_cache read on
10267         set_cache writethrough on
10268
10269         log "Write data and read it back."
10270         log "Read should be satisfied from the cache."
10271         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10272         BEFORE=$(roc_hit)
10273         cancel_lru_locks osc
10274         cat $file >/dev/null
10275         AFTER=$(roc_hit)
10276         if ! let "AFTER - BEFORE == CPAGES"; then
10277                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10278         else
10279                 log "cache hits:: before: $BEFORE, after: $AFTER"
10280         fi
10281
10282         log "Read again; it should be satisfied from the cache."
10283         BEFORE=$AFTER
10284         cancel_lru_locks osc
10285         cat $file >/dev/null
10286         AFTER=$(roc_hit)
10287         if ! let "AFTER - BEFORE == CPAGES"; then
10288                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10289         else
10290                 log "cache hits:: before: $BEFORE, after: $AFTER"
10291         fi
10292
10293         log "Turn off the read cache and turn on the write cache"
10294         set_cache read off
10295         set_cache writethrough on
10296
10297         log "Read again; it should be satisfied from the cache."
10298         BEFORE=$(roc_hit)
10299         cancel_lru_locks osc
10300         cat $file >/dev/null
10301         AFTER=$(roc_hit)
10302         if ! let "AFTER - BEFORE == CPAGES"; then
10303                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10304         else
10305                 log "cache hits:: before: $BEFORE, after: $AFTER"
10306         fi
10307
10308         log "Read again; it should not be satisfied from the cache."
10309         BEFORE=$AFTER
10310         cancel_lru_locks osc
10311         cat $file >/dev/null
10312         AFTER=$(roc_hit)
10313         if ! let "AFTER - BEFORE == 0"; then
10314                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10315         else
10316                 log "cache hits:: before: $BEFORE, after: $AFTER"
10317         fi
10318
10319         log "Write data and read it back."
10320         log "Read should be satisfied from the cache."
10321         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10322         BEFORE=$(roc_hit)
10323         cancel_lru_locks osc
10324         cat $file >/dev/null
10325         AFTER=$(roc_hit)
10326         if ! let "AFTER - BEFORE == CPAGES"; then
10327                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10328         else
10329                 log "cache hits:: before: $BEFORE, after: $AFTER"
10330         fi
10331
10332         log "Read again; it should not be satisfied from the cache."
10333         BEFORE=$AFTER
10334         cancel_lru_locks osc
10335         cat $file >/dev/null
10336         AFTER=$(roc_hit)
10337         if ! let "AFTER - BEFORE == 0"; then
10338                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10339         else
10340                 log "cache hits:: before: $BEFORE, after: $AFTER"
10341         fi
10342
10343         log "Turn off read and write cache"
10344         set_cache read off
10345         set_cache writethrough off
10346
10347         log "Write data and read it back"
10348         log "It should not be satisfied from the cache."
10349         rm -f $file
10350         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10351         cancel_lru_locks osc
10352         BEFORE=$(roc_hit)
10353         cat $file >/dev/null
10354         AFTER=$(roc_hit)
10355         if ! let "AFTER - BEFORE == 0"; then
10356                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10357         else
10358                 log "cache hits:: before: $BEFORE, after: $AFTER"
10359         fi
10360
10361         log "Turn on the read cache and turn off the write cache"
10362         set_cache read on
10363         set_cache writethrough off
10364
10365         log "Write data and read it back"
10366         log "It should not be satisfied from the cache."
10367         rm -f $file
10368         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10369         BEFORE=$(roc_hit)
10370         cancel_lru_locks osc
10371         cat $file >/dev/null
10372         AFTER=$(roc_hit)
10373         if ! let "AFTER - BEFORE == 0"; then
10374                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10375         else
10376                 log "cache hits:: before: $BEFORE, after: $AFTER"
10377         fi
10378
10379         log "Read again; it should be satisfied from the cache."
10380         BEFORE=$(roc_hit)
10381         cancel_lru_locks osc
10382         cat $file >/dev/null
10383         AFTER=$(roc_hit)
10384         if ! let "AFTER - BEFORE == CPAGES"; then
10385                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10386         else
10387                 log "cache hits:: before: $BEFORE, after: $AFTER"
10388         fi
10389
10390         rm -f $file
10391 }
10392 run_test 156 "Verification of tunables"
10393
10394 #Changelogs
10395 err17935 () {
10396         if [[ $MDSCOUNT -gt 1 ]]; then
10397                 error_ignore bz17935 $*
10398         else
10399                 error $*
10400         fi
10401 }
10402
10403 changelog_chmask()
10404 {
10405         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10406
10407         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10408
10409         if [ $MASK -eq 1 ]; then
10410                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10411         else
10412                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10413         fi
10414 }
10415
10416 changelog_extract_field() {
10417         local mdt=$1
10418         local cltype=$2
10419         local file=$3
10420         local identifier=$4
10421
10422         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10423                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10424                 tail -1
10425 }
10426
10427 test_160a() {
10428         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10429         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10430         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10431                 { skip "Need MDS version at least 2.2.0"; return; }
10432
10433         local CL_USERS="mdd.$MDT0.changelog_users"
10434         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10435         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10436         echo "Registered as changelog user $USER"
10437         $GET_CL_USERS | grep -q $USER ||
10438                 error "User $USER not found in changelog_users"
10439
10440         # change something
10441         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10442         touch $DIR/$tdir/pics/2008/zachy/timestamp
10443         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10444         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10445         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10446         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10447         rm $DIR/$tdir/pics/desktop.jpg
10448
10449         $LFS changelog $MDT0 | tail -5
10450
10451         echo "verifying changelog mask"
10452         changelog_chmask "MKDIR"
10453         changelog_chmask "CLOSE"
10454
10455         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10456         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10457
10458         changelog_chmask "MKDIR"
10459         changelog_chmask "CLOSE"
10460
10461         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10462         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10463
10464         $LFS changelog $MDT0
10465         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10466         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10467         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10468         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10469
10470         # verify contents
10471         echo "verifying target fid"
10472         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10473         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10474         [ "$fidc" == "$fidf" ] ||
10475                 err17935 "fid in changelog $fidc != file fid $fidf"
10476         echo "verifying parent fid"
10477         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10478         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10479         [ "$fidc" == "$fidf" ] ||
10480                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10481
10482         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10483         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10484         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10485         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10486         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10487                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10488
10489         MIN_REC=$($GET_CL_USERS |
10490                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10491         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10492         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10493         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10494                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10495
10496         # LU-3446 changelog index reset on MDT restart
10497         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10498         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10499         $LFS changelog_clear $MDT0 $USER 0
10500         stop $SINGLEMDS || error "Fail to stop MDT."
10501         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10502         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10503         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10504         [ $CUR_REC1 == $CUR_REC2 ] ||
10505                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10506
10507         echo "verifying user deregister"
10508         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10509         $GET_CL_USERS | grep -q $USER &&
10510                 error "User $USER still in changelog_users"
10511
10512         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10513         if [ $USERS -eq 0 ]; then
10514                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10515                 touch $DIR/$tdir/chloe
10516                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10517                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10518                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10519         else
10520                 echo "$USERS other changelog users; can't verify off"
10521         fi
10522 }
10523 run_test 160a "changelog sanity"
10524
10525 test_160b() { # LU-3587
10526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10527         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10528         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10529                 { skip "Need MDS version at least 2.2.0"; return; }
10530
10531         local CL_USERS="mdd.$MDT0.changelog_users"
10532         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10533         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10534         echo "Registered as changelog user $USER"
10535         $GET_CL_USERS | grep -q $USER ||
10536                 error "User $USER not found in changelog_users"
10537
10538         local LONGNAME1=$(str_repeat a 255)
10539         local LONGNAME2=$(str_repeat b 255)
10540
10541         cd $DIR
10542         echo "creating very long named file"
10543         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10544         echo "moving very long named file"
10545         mv $LONGNAME1 $LONGNAME2
10546
10547         $LFS changelog $MDT0 | grep RENME
10548
10549         echo "deregistering $USER"
10550         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10551
10552         rm -f $LONGNAME2
10553 }
10554 run_test 160b "Verify that very long rename doesn't crash in changelog"
10555
10556 test_160c() {
10557         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10558
10559         local rc=0
10560         local server_version=$(lustre_version_code $SINGLEMDS)
10561
10562         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10563                 [[ $server_version -gt $(version_code 2.5.1) &&
10564                    $server_version -lt $(version_code 2.5.50) ]] ||
10565                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10567
10568         # Registration step
10569         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10570                 changelog_register -n)
10571
10572         rm -rf $DIR/$tdir
10573         mkdir -p $DIR/$tdir
10574         $MCREATE $DIR/$tdir/foo_160c
10575         changelog_chmask "TRUNC"
10576         $TRUNCATE $DIR/$tdir/foo_160c 200
10577         changelog_chmask "TRUNC"
10578         $TRUNCATE $DIR/$tdir/foo_160c 199
10579         $LFS changelog $MDT0
10580         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10581         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10582         $LFS changelog_clear $MDT0 $USER 0
10583
10584         # Deregistration step
10585         echo "deregistering $USER"
10586         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10587 }
10588 run_test 160c "verify that changelog log catch the truncate event"
10589
10590 test_160d() {
10591         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10592         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10593
10594         local server_version=$(lustre_version_code mds1)
10595         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10596
10597         [[ $server_version -ge $(version_code 2.7.60) ]] ||
10598                 { skip "Need MDS version at least 2.7.60+"; return; }
10599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10600
10601         # Registration step
10602         local USER=$(do_facet mds1 $LCTL --device $MDT0 \
10603                 changelog_register -n)
10604
10605         mkdir -p $DIR/$tdir/migrate_dir
10606         $LFS changelog_clear $MDT0 $USER 0
10607
10608         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
10609         $LFS changelog $MDT0
10610         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
10611         $LFS changelog_clear $MDT0 $USER 0
10612         [ $MIGRATES -eq 1 ] ||
10613                 error "MIGRATE changelog mask count $MIGRATES != 1"
10614
10615         # Deregistration step
10616         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $USER
10617 }
10618 run_test 160d "verify that changelog log catch the migrate event"
10619
10620 test_161a() {
10621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10622         test_mkdir -p -c1 $DIR/$tdir
10623         cp /etc/hosts $DIR/$tdir/$tfile
10624         test_mkdir -c1 $DIR/$tdir/foo1
10625         test_mkdir -c1 $DIR/$tdir/foo2
10626         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10627         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10628         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10629         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10630         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10631         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10632                 $LFS fid2path $DIR $FID
10633                 err17935 "bad link ea"
10634         fi
10635     # middle
10636     rm $DIR/$tdir/foo2/zachary
10637     # last
10638     rm $DIR/$tdir/foo2/thor
10639     # first
10640     rm $DIR/$tdir/$tfile
10641     # rename
10642     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10643     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10644         then
10645         $LFS fid2path $DIR $FID
10646         err17935 "bad link rename"
10647     fi
10648     rm $DIR/$tdir/foo2/maggie
10649
10650         # overflow the EA
10651         local longname=filename_avg_len_is_thirty_two_
10652         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10653                 error "failed to hardlink many files"
10654         links=$($LFS fid2path $DIR $FID | wc -l)
10655         echo -n "${links}/1000 links in link EA"
10656         [[ $links -gt 60 ]] ||
10657                 err17935 "expected at least 60 links in link EA"
10658         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10659                 error "failed to unlink many hardlinks"
10660 }
10661 run_test 161a "link ea sanity"
10662
10663 test_161b() {
10664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10665         [ $MDSCOUNT -lt 2 ] &&
10666                 skip "skipping remote directory test" && return
10667         local MDTIDX=1
10668         local remote_dir=$DIR/$tdir/remote_dir
10669
10670         mkdir -p $DIR/$tdir
10671         $LFS mkdir -i $MDTIDX $remote_dir ||
10672                 error "create remote directory failed"
10673
10674         cp /etc/hosts $remote_dir/$tfile
10675         mkdir -p $remote_dir/foo1
10676         mkdir -p $remote_dir/foo2
10677         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10678         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10679         ln $remote_dir/$tfile $remote_dir/foo1/luna
10680         ln $remote_dir/$tfile $remote_dir/foo2/thor
10681
10682         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10683                      tr -d ']')
10684         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10685                 $LFS fid2path $DIR $FID
10686                 err17935 "bad link ea"
10687         fi
10688         # middle
10689         rm $remote_dir/foo2/zachary
10690         # last
10691         rm $remote_dir/foo2/thor
10692         # first
10693         rm $remote_dir/$tfile
10694         # rename
10695         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10696         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10697         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10698                 $LFS fid2path $DIR $FID
10699                 err17935 "bad link rename"
10700         fi
10701         rm $remote_dir/foo2/maggie
10702
10703         # overflow the EA
10704         local longname=filename_avg_len_is_thirty_two_
10705         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10706                 error "failed to hardlink many files"
10707         links=$($LFS fid2path $DIR $FID | wc -l)
10708         echo -n "${links}/1000 links in link EA"
10709         [[ ${links} -gt 60 ]] ||
10710                 err17935 "expected at least 60 links in link EA"
10711         unlinkmany $remote_dir/foo2/$longname 1000 ||
10712         error "failed to unlink many hardlinks"
10713 }
10714 run_test 161b "link ea sanity under remote directory"
10715
10716 test_161c() {
10717         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10718         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10719         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10720                 skip "Need MDS version at least 2.1.5" && return
10721
10722         # define CLF_RENAME_LAST 0x0001
10723         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10724         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10725                 changelog_register -n)
10726         rm -rf $DIR/$tdir
10727         mkdir -p $DIR/$tdir
10728         touch $DIR/$tdir/foo_161c
10729         touch $DIR/$tdir/bar_161c
10730         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10731         $LFS changelog $MDT0 | grep RENME
10732         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10733                 cut -f5 -d' ')
10734         $LFS changelog_clear $MDT0 $USER 0
10735         if [ x$flags != "x0x1" ]; then
10736                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10737                         $USER
10738                 error "flag $flags is not 0x1"
10739         fi
10740         echo "rename overwrite a target having nlink = 1," \
10741                 "changelog record has flags of $flags"
10742
10743         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10744         touch $DIR/$tdir/foo_161c
10745         touch $DIR/$tdir/bar_161c
10746         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10747         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10748         $LFS changelog $MDT0 | grep RENME
10749         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10750         $LFS changelog_clear $MDT0 $USER 0
10751         if [ x$flags != "x0x0" ]; then
10752                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10753                         $USER
10754                 error "flag $flags is not 0x0"
10755         fi
10756         echo "rename overwrite a target having nlink > 1," \
10757                 "changelog record has flags of $flags"
10758
10759         # rename doesn't overwrite a target (changelog flag 0x0)
10760         touch $DIR/$tdir/foo_161c
10761         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10762         $LFS changelog $MDT0 | grep RENME
10763         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10764         $LFS changelog_clear $MDT0 $USER 0
10765         if [ x$flags != "x0x0" ]; then
10766                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10767                         $USER
10768                 error "flag $flags is not 0x0"
10769         fi
10770         echo "rename doesn't overwrite a target," \
10771                 "changelog record has flags of $flags"
10772
10773         # define CLF_UNLINK_LAST 0x0001
10774         # unlink a file having nlink = 1 (changelog flag 0x1)
10775         rm -f $DIR/$tdir/foo2_161c
10776         $LFS changelog $MDT0 | grep UNLNK
10777         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10778         $LFS changelog_clear $MDT0 $USER 0
10779         if [ x$flags != "x0x1" ]; then
10780                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10781                         $USER
10782                 error "flag $flags is not 0x1"
10783         fi
10784         echo "unlink a file having nlink = 1," \
10785                 "changelog record has flags of $flags"
10786
10787         # unlink a file having nlink > 1 (changelog flag 0x0)
10788         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10789         rm -f $DIR/$tdir/foobar_161c
10790         $LFS changelog $MDT0 | grep UNLNK
10791         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10792         $LFS changelog_clear $MDT0 $USER 0
10793         if [ x$flags != "x0x0" ]; then
10794                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10795                         $USER
10796                 error "flag $flags is not 0x0"
10797         fi
10798         echo "unlink a file having nlink > 1," \
10799                 "changelog record has flags of $flags"
10800         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10801 }
10802 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10803
10804 check_path() {
10805     local expected=$1
10806     shift
10807     local fid=$2
10808
10809     local path=$(${LFS} fid2path $*)
10810     # Remove the '//' indicating a remote directory
10811     path=$(echo $path | sed 's#//#/#g')
10812     RC=$?
10813
10814     if [ $RC -ne 0 ]; then
10815         err17935 "path looked up of $expected failed. Error $RC"
10816         return $RC
10817     elif [ "${path}" != "${expected}" ]; then
10818         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10819         return 2
10820     fi
10821     echo "fid $fid resolves to path $path (expected $expected)"
10822 }
10823
10824 test_162a() { # was test_162
10825         # Make changes to filesystem
10826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10827         test_mkdir -p -c1 $DIR/$tdir/d2
10828         touch $DIR/$tdir/d2/$tfile
10829         touch $DIR/$tdir/d2/x1
10830         touch $DIR/$tdir/d2/x2
10831         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10832         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10833         # regular file
10834         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10835         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10836                 error "check path $tdir/d2/$tfile failed"
10837
10838         # softlink
10839         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10840         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10841         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10842                 error "check path $tdir/d2/p/q/r/slink failed"
10843
10844         # softlink to wrong file
10845         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10846         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10847         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10848                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10849
10850         # hardlink
10851         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10852         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10853         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10854         # fid2path dir/fsname should both work
10855         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10856                 error "check path $tdir/d2/a/b/c/new_file failed"
10857         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10858                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10859
10860         # hardlink count: check that there are 2 links
10861         # Doesnt work with CMD yet: 17935
10862         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10863                 err17935 "expected 2 links"
10864
10865         # hardlink indexing: remove the first link
10866         rm $DIR/$tdir/d2/p/q/r/hlink
10867         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10868                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10869
10870         return 0
10871 }
10872 run_test 162a "path lookup sanity"
10873
10874 test_162b() {
10875         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10876         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10877
10878         mkdir $DIR/$tdir
10879         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10880                                 error "create striped dir failed"
10881
10882         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10883                                         tail -n 1 | awk '{print $2}')
10884         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10885
10886         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10887         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10888
10889         # regular file
10890         for ((i=0;i<5;i++)); do
10891                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10892                         error "get fid for f$i failed"
10893                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10894                         error "check path $tdir/striped_dir/f$i failed"
10895
10896                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10897                         error "get fid for d$i failed"
10898                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10899                         error "check path $tdir/striped_dir/d$i failed"
10900         done
10901
10902         return 0
10903 }
10904 run_test 162b "striped directory path lookup sanity"
10905
10906 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10907 test_162c() {
10908         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
10909                 skip "Need MDS version at least 2.7.51" && return
10910         test_mkdir $DIR/$tdir.local
10911         test_mkdir $DIR/$tdir.remote
10912         local lpath=$tdir.local
10913         local rpath=$tdir.remote
10914
10915         for ((i = 0; i <= 101; i++)); do
10916                 lpath="$lpath/$i"
10917                 mkdir $DIR/$lpath
10918                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10919                         error "get fid for local directory $DIR/$lpath failed"
10920                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10921                         error "check path for local directory $DIR/$lpath failed"
10922
10923                 rpath="$rpath/$i"
10924                 test_mkdir $DIR/$rpath
10925                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10926                         error "get fid for remote directory $DIR/$rpath failed"
10927                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10928                         error "check path for remote directory $DIR/$rpath failed"
10929         done
10930
10931         return 0
10932 }
10933 run_test 162c "fid2path works with paths 100 or more directories deep"
10934
10935 test_169() {
10936         # do directio so as not to populate the page cache
10937         log "creating a 10 Mb file"
10938         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10939         log "starting reads"
10940         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10941         log "truncating the file"
10942         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10943         log "killing dd"
10944         kill %+ || true # reads might have finished
10945         echo "wait until dd is finished"
10946         wait
10947         log "removing the temporary file"
10948         rm -rf $DIR/$tfile || error "tmp file removal failed"
10949 }
10950 run_test 169 "parallel read and truncate should not deadlock"
10951
10952 test_170() {
10953         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10954         $LCTL clear     # bug 18514
10955         $LCTL debug_daemon start $TMP/${tfile}_log_good
10956         touch $DIR/$tfile
10957         $LCTL debug_daemon stop
10958         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10959                error "sed failed to read log_good"
10960
10961         $LCTL debug_daemon start $TMP/${tfile}_log_good
10962         rm -rf $DIR/$tfile
10963         $LCTL debug_daemon stop
10964
10965         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10966                error "lctl df log_bad failed"
10967
10968         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10969         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10970
10971         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10972         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10973
10974         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10975                 error "bad_line good_line1 good_line2 are empty"
10976
10977         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10978         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10979         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10980
10981         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10982         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10983         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10984
10985         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10986                 error "bad_line_new good_line_new are empty"
10987
10988         local expected_good=$((good_line1 + good_line2*2))
10989
10990         rm -f $TMP/${tfile}*
10991         # LU-231, short malformed line may not be counted into bad lines
10992         if [ $bad_line -ne $bad_line_new ] &&
10993                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10994                 error "expected $bad_line bad lines, but got $bad_line_new"
10995                 return 1
10996         fi
10997
10998         if [ $expected_good -ne $good_line_new ]; then
10999                 error "expected $expected_good good lines, but got $good_line_new"
11000                 return 2
11001         fi
11002         true
11003 }
11004 run_test 170 "test lctl df to handle corrupted log ====================="
11005
11006 test_171() { # bug20592
11007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11008 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11009         $LCTL set_param fail_loc=0x50e
11010         $LCTL set_param fail_val=3000
11011         multiop_bg_pause $DIR/$tfile O_s || true
11012         local MULTIPID=$!
11013         kill -USR1 $MULTIPID
11014         # cause log dump
11015         sleep 3
11016         wait $MULTIPID
11017         if dmesg | grep "recursive fault"; then
11018                 error "caught a recursive fault"
11019         fi
11020         $LCTL set_param fail_loc=0
11021         true
11022 }
11023 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11024
11025 # it would be good to share it with obdfilter-survey/iokit-libecho code
11026 setup_obdecho_osc () {
11027         local rc=0
11028         local ost_nid=$1
11029         local obdfilter_name=$2
11030         echo "Creating new osc for $obdfilter_name on $ost_nid"
11031         # make sure we can find loopback nid
11032         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11033
11034         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11035                            ${obdfilter_name}_osc_UUID || rc=2; }
11036         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11037                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11038         return $rc
11039 }
11040
11041 cleanup_obdecho_osc () {
11042         local obdfilter_name=$1
11043         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11044         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11045         return 0
11046 }
11047
11048 obdecho_test() {
11049         local OBD=$1
11050         local node=$2
11051         local pages=${3:-64}
11052         local rc=0
11053         local id
11054
11055         local count=10
11056         local obd_size=$(get_obd_size $node $OBD)
11057         local page_size=$(get_page_size $node)
11058         if [[ -n "$obd_size" ]]; then
11059                 local new_count=$((obd_size / (pages * page_size / 1024)))
11060                 [[ $new_count -ge $count ]] || count=$new_count
11061         fi
11062
11063         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11064         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11065                            rc=2; }
11066         if [ $rc -eq 0 ]; then
11067             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11068             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11069         fi
11070         echo "New object id is $id"
11071         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11072                            rc=4; }
11073         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11074                            "test_brw $count w v $pages $id" || rc=4; }
11075         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11076                            rc=4; }
11077         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11078                                         "cleanup" || rc=5; }
11079         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11080                                         "detach" || rc=6; }
11081         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11082         return $rc
11083 }
11084
11085 test_180a() {
11086         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11087         remote_ost_nodsh && skip "remote OST with nodsh" && return
11088         local rc=0
11089         local rmmod_local=0
11090
11091         if ! module_loaded obdecho; then
11092             load_module obdecho/obdecho
11093             rmmod_local=1
11094         fi
11095
11096         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11097         local host=$(lctl get_param -n osc.$osc.import |
11098                              awk '/current_connection:/ {print $2}' )
11099         local target=$(lctl get_param -n osc.$osc.import |
11100                              awk '/target:/ {print $2}' )
11101         target=${target%_UUID}
11102
11103         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11104         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11105         [[ -n $target ]] && cleanup_obdecho_osc $target
11106         [ $rmmod_local -eq 1 ] && rmmod obdecho
11107         return $rc
11108 }
11109 run_test 180a "test obdecho on osc"
11110
11111 test_180b() {
11112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11113         remote_ost_nodsh && skip "remote OST with nodsh" && return
11114         local rc=0
11115         local rmmod_remote=0
11116
11117         do_facet ost1 "lsmod | grep -q obdecho || "                      \
11118                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
11119                       "modprobe obdecho; }" && rmmod_remote=1
11120         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11121         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11122         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
11123         return $rc
11124 }
11125 run_test 180b "test obdecho directly on obdfilter"
11126
11127 test_180c() { # LU-2598
11128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11129         remote_ost_nodsh && skip "remote OST with nodsh" && return
11130         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11131                 skip "Need MDS version at least 2.4.0" && return
11132
11133         local rc=0
11134         local rmmod_remote=false
11135         local pages=16384 # 64MB bulk I/O RPC size
11136         local target
11137
11138         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11139                 rmmod_remote=true || error "failed to load module obdecho"
11140
11141         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11142                 head -n1)
11143         if [ -n "$target" ]; then
11144                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11145         else
11146                 echo "there is no obdfilter target on ost1"
11147                 rc=2
11148         fi
11149         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11150         return $rc
11151 }
11152 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11153
11154 test_181() { # bug 22177
11155         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11156         # create enough files to index the directory
11157         createmany -o $DIR/$tdir/foobar 4000
11158         # print attributes for debug purpose
11159         lsattr -d .
11160         # open dir
11161         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11162         MULTIPID=$!
11163         # remove the files & current working dir
11164         unlinkmany $DIR/$tdir/foobar 4000
11165         rmdir $DIR/$tdir
11166         kill -USR1 $MULTIPID
11167         wait $MULTIPID
11168         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11169         return 0
11170 }
11171 run_test 181 "Test open-unlinked dir ========================"
11172
11173 test_182() {
11174         local fcount=1000
11175         local tcount=10
11176
11177         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11178
11179         $LCTL set_param mdc.*.rpc_stats=clear
11180
11181         for (( i = 0; i < $tcount; i++ )) ; do
11182                 mkdir $DIR/$tdir/$i
11183         done
11184
11185         for (( i = 0; i < $tcount; i++ )) ; do
11186                 createmany -o $DIR/$tdir/$i/f- $fcount &
11187         done
11188         wait
11189
11190         for (( i = 0; i < $tcount; i++ )) ; do
11191                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11192         done
11193         wait
11194
11195         $LCTL get_param mdc.*.rpc_stats
11196
11197         rm -rf $DIR/$tdir
11198 }
11199 run_test 182 "Test parallel modify metadata operations ================"
11200
11201 test_183() { # LU-2275
11202         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11203         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
11204                 skip "Need MDS version at least 2.3.56" && return
11205
11206         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11207         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11208         echo aaa > $DIR/$tdir/$tfile
11209
11210 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
11211         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
11212
11213         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
11214         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11215
11216         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11217
11218         # Flush negative dentry cache
11219         touch $DIR/$tdir/$tfile
11220
11221         # We are not checking for any leaked references here, they'll
11222         # become evident next time we do cleanup with module unload.
11223         rm -rf $DIR/$tdir
11224 }
11225 run_test 183 "No crash or request leak in case of strange dispositions ========"
11226
11227 # test suite 184 is for LU-2016, LU-2017
11228 test_184a() {
11229         check_swap_layouts_support && return 0
11230
11231         dir0=$DIR/$tdir/$testnum
11232         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11233         ref1=/etc/passwd
11234         ref2=/etc/group
11235         file1=$dir0/f1
11236         file2=$dir0/f2
11237         $SETSTRIPE -c1 $file1
11238         cp $ref1 $file1
11239         $SETSTRIPE -c2 $file2
11240         cp $ref2 $file2
11241         gen1=$($GETSTRIPE -g $file1)
11242         gen2=$($GETSTRIPE -g $file2)
11243
11244         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11245         gen=$($GETSTRIPE -g $file1)
11246         [[ $gen1 != $gen ]] ||
11247                 "Layout generation on $file1 does not change"
11248         gen=$($GETSTRIPE -g $file2)
11249         [[ $gen2 != $gen ]] ||
11250                 "Layout generation on $file2 does not change"
11251
11252         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11253         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11254 }
11255 run_test 184a "Basic layout swap"
11256
11257 test_184b() {
11258         check_swap_layouts_support && return 0
11259
11260         dir0=$DIR/$tdir/$testnum
11261         mkdir -p $dir0 || error "creating dir $dir0"
11262         file1=$dir0/f1
11263         file2=$dir0/f2
11264         file3=$dir0/f3
11265         dir1=$dir0/d1
11266         dir2=$dir0/d2
11267         mkdir $dir1 $dir2
11268         $SETSTRIPE -c1 $file1
11269         $SETSTRIPE -c2 $file2
11270         $SETSTRIPE -c1 $file3
11271         chown $RUNAS_ID $file3
11272         gen1=$($GETSTRIPE -g $file1)
11273         gen2=$($GETSTRIPE -g $file2)
11274
11275         $LFS swap_layouts $dir1 $dir2 &&
11276                 error "swap of directories layouts should fail"
11277         $LFS swap_layouts $dir1 $file1 &&
11278                 error "swap of directory and file layouts should fail"
11279         $RUNAS $LFS swap_layouts $file1 $file2 &&
11280                 error "swap of file we cannot write should fail"
11281         $LFS swap_layouts $file1 $file3 &&
11282                 error "swap of file with different owner should fail"
11283         /bin/true # to clear error code
11284 }
11285 run_test 184b "Forbidden layout swap (will generate errors)"
11286
11287 test_184c() {
11288         check_swap_layouts_support && return 0
11289
11290         local dir0=$DIR/$tdir/$testnum
11291         mkdir -p $dir0 || error "creating dir $dir0"
11292
11293         local ref1=$dir0/ref1
11294         local ref2=$dir0/ref2
11295         local file1=$dir0/file1
11296         local file2=$dir0/file2
11297         # create a file large enough for the concurrent test
11298         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11299         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11300         echo "ref file size: ref1($(stat -c %s $ref1))," \
11301              "ref2($(stat -c %s $ref2))"
11302
11303         cp $ref2 $file2
11304         dd if=$ref1 of=$file1 bs=16k &
11305         local DD_PID=$!
11306
11307         # Make sure dd starts to copy file
11308         while [ ! -f $file1 ]; do sleep 0.1; done
11309
11310         $LFS swap_layouts $file1 $file2
11311         local rc=$?
11312         wait $DD_PID
11313         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11314         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11315
11316         # how many bytes copied before swapping layout
11317         local copied=$(stat -c %s $file2)
11318         local remaining=$(stat -c %s $ref1)
11319         remaining=$((remaining - copied))
11320         echo "Copied $copied bytes before swapping layout..."
11321
11322         cmp -n $copied $file1 $ref2 | grep differ &&
11323                 error "Content mismatch [0, $copied) of ref2 and file1"
11324         cmp -n $copied $file2 $ref1 ||
11325                 error "Content mismatch [0, $copied) of ref1 and file2"
11326         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11327                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11328
11329         # clean up
11330         rm -f $ref1 $ref2 $file1 $file2
11331 }
11332 run_test 184c "Concurrent write and layout swap"
11333
11334 test_184d() {
11335         check_swap_layouts_support && return 0
11336         [ -z "$(which getfattr 2>/dev/null)" ] &&
11337                 skip "no getfattr command" && return 0
11338
11339         local file1=$DIR/$tdir/$tfile-1
11340         local file2=$DIR/$tdir/$tfile-2
11341         local file3=$DIR/$tdir/$tfile-3
11342         local lovea1
11343         local lovea2
11344
11345         mkdir -p $DIR/$tdir
11346         touch $file1 || error "create $file1 failed"
11347         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11348                 error "create $file2 failed"
11349         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11350                 error "create $file3 failed"
11351         lovea1=$($LFS getstripe $file1 | sed 1d)
11352
11353         $LFS swap_layouts $file2 $file3 ||
11354                 error "swap $file2 $file3 layouts failed"
11355         $LFS swap_layouts $file1 $file2 ||
11356                 error "swap $file1 $file2 layouts failed"
11357
11358         lovea2=$($LFS getstripe $file2 | sed 1d)
11359         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11360
11361         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11362         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11363 }
11364 run_test 184d "allow stripeless layouts swap"
11365
11366 test_184e() {
11367         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
11368                 { skip "Need MDS version at least 2.6.94"; return 0; }
11369         check_swap_layouts_support && return 0
11370         [ -z "$(which getfattr 2>/dev/null)" ] &&
11371                 skip "no getfattr command" && return 0
11372
11373         local file1=$DIR/$tdir/$tfile-1
11374         local file2=$DIR/$tdir/$tfile-2
11375         local file3=$DIR/$tdir/$tfile-3
11376         local lovea
11377
11378         mkdir -p $DIR/$tdir
11379         touch $file1 || error "create $file1 failed"
11380         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11381                 error "create $file2 failed"
11382         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11383                 error "create $file3 failed"
11384
11385         $LFS swap_layouts $file1 $file2 ||
11386                 error "swap $file1 $file2 layouts failed"
11387
11388         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11389         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11390
11391         echo 123 > $file1 || error "Should be able to write into $file1"
11392
11393         $LFS swap_layouts $file1 $file3 ||
11394                 error "swap $file1 $file3 layouts failed"
11395
11396         echo 123 > $file1 || error "Should be able to write into $file1"
11397
11398         rm -rf $file1 $file2 $file3
11399 }
11400 run_test 184e "Recreate layout after stripeless layout swaps"
11401
11402 test_185() { # LU-2441
11403         # LU-3553 - no volatile file support in old servers
11404         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11405                 { skip "Need MDS version at least 2.3.60"; return 0; }
11406
11407         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11408         touch $DIR/$tdir/spoo
11409         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11410         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11411                 error "cannot create/write a volatile file"
11412         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11413                 error "FID is still valid after close"
11414
11415         multiop_bg_pause $DIR/$tdir vVw4096_c
11416         local multi_pid=$!
11417
11418         local OLD_IFS=$IFS
11419         IFS=":"
11420         local fidv=($fid)
11421         IFS=$OLD_IFS
11422         # assume that the next FID for this client is sequential, since stdout
11423         # is unfortunately eaten by multiop_bg_pause
11424         local n=$((${fidv[1]} + 1))
11425         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11426         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11427                 error "FID is missing before close"
11428         kill -USR1 $multi_pid
11429         # 1 second delay, so if mtime change we will see it
11430         sleep 1
11431         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11432         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11433 }
11434 run_test 185 "Volatile file support"
11435
11436 test_187a() {
11437         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11438         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11439                 skip "Need MDS version at least 2.3.0" && return
11440
11441         local dir0=$DIR/$tdir/$testnum
11442         mkdir -p $dir0 || error "creating dir $dir0"
11443
11444         local file=$dir0/file1
11445         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11446         local dv1=$($LFS data_version $file)
11447         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11448         local dv2=$($LFS data_version $file)
11449         [[ $dv1 != $dv2 ]] ||
11450                 error "data version did not change on write $dv1 == $dv2"
11451
11452         # clean up
11453         rm -f $file1
11454 }
11455 run_test 187a "Test data version change"
11456
11457 test_187b() {
11458         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11459         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
11460                 skip "Need MDS version at least 2.3.0" && return
11461
11462         local dir0=$DIR/$tdir/$testnum
11463         mkdir -p $dir0 || error "creating dir $dir0"
11464
11465         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11466         [[ ${DV[0]} != ${DV[1]} ]] ||
11467                 error "data version did not change on write"\
11468                       " ${DV[0]} == ${DV[1]}"
11469
11470         # clean up
11471         rm -f $file1
11472 }
11473 run_test 187b "Test data version change on volatile file"
11474
11475 test_200() {
11476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11477         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11478
11479         local POOL=${POOL:-cea1}
11480         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11481         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11482         # Pool OST targets
11483         local first_ost=0
11484         local last_ost=$(($OSTCOUNT - 1))
11485         local ost_step=2
11486         local ost_list=$(seq $first_ost $ost_step $last_ost)
11487         local ost_range="$first_ost $last_ost $ost_step"
11488         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11489         local file_dir=$POOL_ROOT/file_tst
11490         local subdir=$test_path/subdir
11491
11492         local rc=0
11493         while : ; do
11494                 # former test_200a test_200b
11495                 pool_add $POOL                          || { rc=$? ; break; }
11496                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11497                 # former test_200c test_200d
11498                 mkdir -p $test_path
11499                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11500                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11501                 mkdir -p $subdir
11502                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11503                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11504                                                         || { rc=$? ; break; }
11505                 # former test_200e test_200f
11506                 local files=$((OSTCOUNT*3))
11507                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11508                                                         || { rc=$? ; break; }
11509                 pool_create_files $POOL $file_dir $files "$ost_list" \
11510                                                         || { rc=$? ; break; }
11511                 # former test_200g test_200h
11512                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11513                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11514
11515                 # former test_201a test_201b test_201c
11516                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11517
11518                 local f=$test_path/$tfile
11519                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11520                 pool_remove $POOL $f                    || { rc=$? ; break; }
11521                 break
11522         done
11523
11524         cleanup_pools
11525         return $rc
11526 }
11527 run_test 200 "OST pools"
11528
11529 # usage: default_attr <count | size | offset>
11530 default_attr() {
11531         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11532 }
11533
11534 # usage: check_default_stripe_attr
11535 check_default_stripe_attr() {
11536         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11537         case $1 in
11538         --stripe-count|--count)
11539                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11540         --stripe-size|--size)
11541                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11542         --stripe-index|--index)
11543                 EXPECTED=-1;;
11544         *)
11545                 error "unknown getstripe attr '$1'"
11546         esac
11547
11548         [ $ACTUAL != $EXPECTED ] &&
11549                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11550 }
11551
11552 test_204a() {
11553         test_mkdir -p $DIR/$tdir
11554         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11555
11556         check_default_stripe_attr --stripe-count
11557         check_default_stripe_attr --stripe-size
11558         check_default_stripe_attr --stripe-index
11559
11560         return 0
11561 }
11562 run_test 204a "Print default stripe attributes ================="
11563
11564 test_204b() {
11565         test_mkdir -p $DIR/$tdir
11566         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11567
11568         check_default_stripe_attr --stripe-size
11569         check_default_stripe_attr --stripe-index
11570
11571         return 0
11572 }
11573 run_test 204b "Print default stripe size and offset  ==========="
11574
11575 test_204c() {
11576         test_mkdir -p $DIR/$tdir
11577         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11578
11579         check_default_stripe_attr --stripe-count
11580         check_default_stripe_attr --stripe-index
11581
11582         return 0
11583 }
11584 run_test 204c "Print default stripe count and offset ==========="
11585
11586 test_204d() {
11587         test_mkdir -p $DIR/$tdir
11588         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11589
11590         check_default_stripe_attr --stripe-count
11591         check_default_stripe_attr --stripe-size
11592
11593         return 0
11594 }
11595 run_test 204d "Print default stripe count and size ============="
11596
11597 test_204e() {
11598         test_mkdir -p $DIR/$tdir
11599         $SETSTRIPE -d $DIR/$tdir
11600
11601         check_default_stripe_attr --stripe-count --raw
11602         check_default_stripe_attr --stripe-size --raw
11603         check_default_stripe_attr --stripe-index --raw
11604
11605         return 0
11606 }
11607 run_test 204e "Print raw stripe attributes ================="
11608
11609 test_204f() {
11610         test_mkdir -p $DIR/$tdir
11611         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11612
11613         check_default_stripe_attr --stripe-size --raw
11614         check_default_stripe_attr --stripe-index --raw
11615
11616         return 0
11617 }
11618 run_test 204f "Print raw stripe size and offset  ==========="
11619
11620 test_204g() {
11621         test_mkdir -p $DIR/$tdir
11622         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11623
11624         check_default_stripe_attr --stripe-count --raw
11625         check_default_stripe_attr --stripe-index --raw
11626
11627         return 0
11628 }
11629 run_test 204g "Print raw stripe count and offset ==========="
11630
11631 test_204h() {
11632         test_mkdir -p $DIR/$tdir
11633         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11634
11635         check_default_stripe_attr --stripe-count --raw
11636         check_default_stripe_attr --stripe-size --raw
11637
11638         return 0
11639 }
11640 run_test 204h "Print raw stripe count and size ============="
11641
11642 # Figure out which job scheduler is being used, if any,
11643 # or use a fake one
11644 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11645         JOBENV=SLURM_JOB_ID
11646 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11647         JOBENV=LSB_JOBID
11648 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11649         JOBENV=PBS_JOBID
11650 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11651         JOBENV=LOADL_STEP_ID
11652 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11653         JOBENV=JOB_ID
11654 else
11655         $LCTL list_param jobid_name > /dev/null 2>&1
11656         if [ $? -eq 0 ]; then
11657                 JOBENV=nodelocal
11658         else
11659                 JOBENV=FAKE_JOBID
11660         fi
11661 fi
11662
11663 verify_jobstats() {
11664         local cmd=($1)
11665         shift
11666         local facets="$@"
11667
11668 # we don't really need to clear the stats for this test to work, since each
11669 # command has a unique jobid, but it makes debugging easier if needed.
11670 #       for facet in $facets; do
11671 #               local dev=$(convert_facet2label $facet)
11672 #               # clear old jobstats
11673 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
11674 #       done
11675
11676         # use a new JobID for each test, or we might see an old one
11677         [ "$JOBENV" = "FAKE_JOBID" ] &&
11678                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11679
11680         JOBVAL=${!JOBENV}
11681
11682         [ "$JOBENV" = "nodelocal" ] && {
11683                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
11684                 $LCTL set_param jobid_name=$FAKE_JOBID
11685                 JOBVAL=$FAKE_JOBID
11686         }
11687
11688         log "Test: ${cmd[*]}"
11689         log "Using JobID environment variable $JOBENV=$JOBVAL"
11690
11691         if [ $JOBENV = "FAKE_JOBID" ]; then
11692                 FAKE_JOBID=$JOBVAL ${cmd[*]}
11693         else
11694                 ${cmd[*]}
11695         fi
11696
11697         # all files are created on OST0000
11698         for facet in $facets; do
11699                 local stats="*.$(convert_facet2label $facet).job_stats"
11700                 if [ $(do_facet $facet lctl get_param $stats |
11701                        grep -c $JOBVAL) -ne 1 ]; then
11702                         do_facet $facet lctl get_param $stats
11703                         error "No jobstats for $JOBVAL found on $facet::$stats"
11704                 fi
11705         done
11706 }
11707
11708 jobstats_set() {
11709         trap 0
11710         NEW_JOBENV=${1:-$OLD_JOBENV}
11711         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11712         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11713 }
11714
11715 cleanup_205() {
11716         do_facet $SINGLEMDS \
11717                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
11718         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11719         do_facet $SINGLEMDS lctl --device $MDT0 changelog_deregister $CL_USER
11720 }
11721
11722 test_205() { # Job stats
11723         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11724         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11725         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11726         remote_ost_nodsh && skip "remote OST with nodsh" && return
11727
11728         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11729                 skip "Server doesn't support jobstats" && return 0
11730         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11731
11732         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11733         if [ $OLD_JOBENV != $JOBENV ]; then
11734                 jobstats_set $JOBENV
11735                 trap cleanup_205 EXIT
11736         fi
11737
11738         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
11739         echo "Registered as changelog user $CL_USER"
11740
11741         OLD_INTERVAL=$(do_facet $SINGLEMDS \
11742                        lctl get_param -n mdt.*.job_cleanup_interval)
11743         local interval_new=5
11744         do_facet $SINGLEMDS \
11745                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
11746         local start=$SECONDS
11747
11748         local cmd
11749         # mkdir
11750         cmd="mkdir $DIR/$tdir"
11751         verify_jobstats "$cmd" "$SINGLEMDS"
11752         # rmdir
11753         cmd="rmdir $DIR/$tdir"
11754         verify_jobstats "$cmd" "$SINGLEMDS"
11755         # mkdir on secondary MDT
11756         if [ $MDSCOUNT -gt 1 ]; then
11757                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
11758                 verify_jobstats "$cmd" "mds2"
11759         fi
11760         # mknod
11761         cmd="mknod $DIR/$tfile c 1 3"
11762         verify_jobstats "$cmd" "$SINGLEMDS"
11763         # unlink
11764         cmd="rm -f $DIR/$tfile"
11765         verify_jobstats "$cmd" "$SINGLEMDS"
11766         # create all files on OST0000 so verify_jobstats can find OST stats
11767         # open & close
11768         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11769         verify_jobstats "$cmd" "$SINGLEMDS"
11770         # setattr
11771         cmd="touch $DIR/$tfile"
11772         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11773         # write
11774         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11775         verify_jobstats "$cmd" "ost1"
11776         # read
11777         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11778         verify_jobstats "$cmd" "ost1"
11779         # truncate
11780         cmd="$TRUNCATE $DIR/$tfile 0"
11781         verify_jobstats "$cmd" "$SINGLEMDS ost1"
11782         # rename
11783         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
11784         verify_jobstats "$cmd" "$SINGLEMDS"
11785         # jobstats expiry - sleep until old stats should be expired
11786         local left=$((interval_new + 2 - (SECONDS - start)))
11787         [ $left -ge 0 ] && echo "sleep $left for expiry" && sleep $((left + 1))
11788         cmd="mkdir $DIR/$tdir.expire"
11789         verify_jobstats "$cmd" "$SINGLEMDS"
11790         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
11791             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
11792
11793         # Ensure that jobid are present in changelog (if supported by MDS)
11794         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11795         then
11796                 $LFS changelog $MDT0 | tail -9
11797                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11798                 [ $jobids -eq 9 ] ||
11799                         error "Wrong changelog jobid count $jobids != 9"
11800
11801                 # LU-5862
11802                 JOBENV="disable"
11803                 jobstats_set $JOBENV
11804                 touch $DIR/$tfile
11805                 $LFS changelog $MDT0 | tail -1
11806                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11807                 [ $jobids -eq 0 ] ||
11808                         error "Unexpected jobids when jobid_var=$JOBENV"
11809         fi
11810
11811         cleanup_205
11812 }
11813 run_test 205 "Verify job stats"
11814
11815 # LU-1480, LU-1773 and LU-1657
11816 test_206() {
11817         mkdir -p $DIR/$tdir
11818         $SETSTRIPE -c -1 $DIR/$tdir
11819 #define OBD_FAIL_LOV_INIT 0x1403
11820         $LCTL set_param fail_loc=0xa0001403
11821         $LCTL set_param fail_val=1
11822         touch $DIR/$tdir/$tfile || true
11823 }
11824 run_test 206 "fail lov_init_raid0() doesn't lbug"
11825
11826 test_207a() {
11827         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11828         local fsz=`stat -c %s $DIR/$tfile`
11829         cancel_lru_locks mdc
11830
11831         # do not return layout in getattr intent
11832 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11833         $LCTL set_param fail_loc=0x170
11834         local sz=`stat -c %s $DIR/$tfile`
11835
11836         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11837
11838         rm -rf $DIR/$tfile
11839 }
11840 run_test 207a "can refresh layout at glimpse"
11841
11842 test_207b() {
11843         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11844         local cksum=`md5sum $DIR/$tfile`
11845         local fsz=`stat -c %s $DIR/$tfile`
11846         cancel_lru_locks mdc
11847         cancel_lru_locks osc
11848
11849         # do not return layout in getattr intent
11850 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11851         $LCTL set_param fail_loc=0x171
11852
11853         # it will refresh layout after the file is opened but before read issues
11854         echo checksum is "$cksum"
11855         echo "$cksum" |md5sum -c --quiet || error "file differs"
11856
11857         rm -rf $DIR/$tfile
11858 }
11859 run_test 207b "can refresh layout at open"
11860
11861 test_208() {
11862         # FIXME: in this test suite, only RD lease is used. This is okay
11863         # for now as only exclusive open is supported. After generic lease
11864         # is done, this test suite should be revised. - Jinshan
11865
11866         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11867         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11868                 { skip "Need MDS version at least 2.4.52"; return 0; }
11869
11870         echo "==== test 1: verify get lease work"
11871         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11872
11873         echo "==== test 2: verify lease can be broken by upcoming open"
11874         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11875         local PID=$!
11876         sleep 1
11877
11878         $MULTIOP $DIR/$tfile oO_RDONLY:c
11879         kill -USR1 $PID && wait $PID || error "break lease error"
11880
11881         echo "==== test 3: verify lease can't be granted if an open already exists"
11882         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11883         local PID=$!
11884         sleep 1
11885
11886         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11887         kill -USR1 $PID && wait $PID || error "open file error"
11888
11889         echo "==== test 4: lease can sustain over recovery"
11890         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11891         PID=$!
11892         sleep 1
11893
11894         fail mds1
11895
11896         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11897
11898         echo "==== test 5: lease broken can't be regained by replay"
11899         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11900         PID=$!
11901         sleep 1
11902
11903         # open file to break lease and then recovery
11904         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11905         fail mds1
11906
11907         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11908
11909         rm -f $DIR/$tfile
11910 }
11911 run_test 208 "Exclusive open"
11912
11913 test_209() {
11914         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11915                 skip_env "must have disp_stripe" && return
11916
11917         touch $DIR/$tfile
11918         sync; sleep 5; sync;
11919
11920         echo 3 > /proc/sys/vm/drop_caches
11921         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11922
11923         # open/close 500 times
11924         for i in $(seq 500); do
11925                 cat $DIR/$tfile
11926         done
11927
11928         echo 3 > /proc/sys/vm/drop_caches
11929         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11930
11931         echo "before: $req_before, after: $req_after"
11932         [ $((req_after - req_before)) -ge 300 ] &&
11933                 error "open/close requests are not freed"
11934         return 0
11935 }
11936 run_test 209 "read-only open/close requests should be freed promptly"
11937
11938 test_212() {
11939         size=`date +%s`
11940         size=$((size % 8192 + 1))
11941         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11942         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11943         rm -f $DIR/f212 $DIR/f212.xyz
11944 }
11945 run_test 212 "Sendfile test ============================================"
11946
11947 test_213() {
11948         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11949         cancel_lru_locks osc
11950         lctl set_param fail_loc=0x8000040f
11951         # generate a read lock
11952         cat $DIR/$tfile > /dev/null
11953         # write to the file, it will try to cancel the above read lock.
11954         cat /etc/hosts >> $DIR/$tfile
11955 }
11956 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11957
11958 test_214() { # for bug 20133
11959         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11960         for (( i=0; i < 340; i++ )) ; do
11961                 touch $DIR/$tdir/d214c/a$i
11962         done
11963
11964         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11965         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11966         ls $DIR/d214c || error "ls $DIR/d214c failed"
11967         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11968         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11969 }
11970 run_test 214 "hash-indexed directory test - bug 20133"
11971
11972 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11973 create_lnet_proc_files() {
11974         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11975         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11976
11977         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11978         rm -f "$TMP/lnet_$1.sys_tmp"
11979 }
11980
11981 # counterpart of create_lnet_proc_files
11982 remove_lnet_proc_files() {
11983         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11984 }
11985
11986 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11987 # 3rd arg as regexp for body
11988 check_lnet_proc_stats() {
11989         local l=$(cat "$TMP/lnet_$1" |wc -l)
11990         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11991
11992         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11993 }
11994
11995 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11996 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11997 # optional and can be regexp for 2nd line (lnet.routes case)
11998 check_lnet_proc_entry() {
11999         local blp=2          # blp stands for 'position of 1st line of body'
12000         [ -z "$5" ] || blp=3 # lnet.routes case
12001
12002         local l=$(cat "$TMP/lnet_$1" |wc -l)
12003         # subtracting one from $blp because the body can be empty
12004         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12005
12006         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12007                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12008
12009         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12010                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12011
12012         # bail out if any unexpected line happened
12013         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12014         [ "$?" != 0 ] || error "$2 misformatted"
12015 }
12016
12017 test_215() { # for bugs 18102, 21079, 21517
12018         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12019         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12020         local P='[1-9][0-9]*'           # positive numeric
12021         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12022         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12023         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12024         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12025
12026         local L1 # regexp for 1st line
12027         local L2 # regexp for 2nd line (optional)
12028         local BR # regexp for the rest (body)
12029
12030         # lnet.stats should look as 11 space-separated non-negative numerics
12031         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12032         create_lnet_proc_files "stats"
12033         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12034         remove_lnet_proc_files "stats"
12035
12036         # lnet.routes should look like this:
12037         # Routing disabled/enabled
12038         # net hops priority state router
12039         # where net is a string like tcp0, hops > 0, priority >= 0,
12040         # state is up/down,
12041         # router is a string like 192.168.1.1@tcp2
12042         L1="^Routing (disabled|enabled)$"
12043         L2="^net +hops +priority +state +router$"
12044         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12045         create_lnet_proc_files "routes"
12046         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12047         remove_lnet_proc_files "routes"
12048
12049         # lnet.routers should look like this:
12050         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12051         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12052         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12053         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12054         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12055         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12056         create_lnet_proc_files "routers"
12057         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12058         remove_lnet_proc_files "routers"
12059
12060         # lnet.peers should look like this:
12061         # nid refs state last max rtr min tx min queue
12062         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12063         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12064         # numeric (0 or >0 or <0), queue >= 0.
12065         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12066         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12067         create_lnet_proc_files "peers"
12068         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12069         remove_lnet_proc_files "peers"
12070
12071         # lnet.buffers  should look like this:
12072         # pages count credits min
12073         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12074         L1="^pages +count +credits +min$"
12075         BR="^ +$N +$N +$I +$I$"
12076         create_lnet_proc_files "buffers"
12077         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12078         remove_lnet_proc_files "buffers"
12079
12080         # lnet.nis should look like this:
12081         # nid status alive refs peer rtr max tx min
12082         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12083         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12084         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12085         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12086         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12087         create_lnet_proc_files "nis"
12088         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12089         remove_lnet_proc_files "nis"
12090
12091         # can we successfully write to lnet.stats?
12092         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12093         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12094 }
12095 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12096
12097 test_216() { # bug 20317
12098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12099         remote_ost_nodsh && skip "remote OST with nodsh" && return
12100
12101         local node
12102         local facets=$(get_facets OST)
12103         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12104
12105         save_lustre_params client "osc.*.contention_seconds" > $p
12106         save_lustre_params $facets \
12107                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12108         save_lustre_params $facets \
12109                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12110         save_lustre_params $facets \
12111                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12112         clear_osc_stats
12113
12114         # agressive lockless i/o settings
12115         for node in $(osts_nodes); do
12116                 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'
12117         done
12118         lctl set_param -n osc.*.contention_seconds 60
12119
12120         $DIRECTIO write $DIR/$tfile 0 10 4096
12121         $CHECKSTAT -s 40960 $DIR/$tfile
12122
12123         # disable lockless i/o
12124         for node in $(osts_nodes); do
12125                 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'
12126         done
12127         lctl set_param -n osc.*.contention_seconds 0
12128         clear_osc_stats
12129
12130         dd if=/dev/zero of=$DIR/$tfile count=0
12131         $CHECKSTAT -s 0 $DIR/$tfile
12132
12133         restore_lustre_params <$p
12134         rm -f $p
12135         rm $DIR/$tfile
12136 }
12137 run_test 216 "check lockless direct write works and updates file size and kms correctly"
12138
12139 test_217() { # bug 22430
12140         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12141         local node
12142         local nid
12143
12144         for node in $(nodes_list); do
12145                 nid=$(host_nids_address $node $NETTYPE)
12146                 if [[ $nid = *-* ]] ; then
12147                         echo "lctl ping $nid@$NETTYPE"
12148                         lctl ping $nid@$NETTYPE
12149                 else
12150                         echo "skipping $node (no hyphen detected)"
12151                 fi
12152         done
12153 }
12154 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12155
12156 test_218() {
12157        # do directio so as not to populate the page cache
12158        log "creating a 10 Mb file"
12159        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12160        log "starting reads"
12161        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12162        log "truncating the file"
12163        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12164        log "killing dd"
12165        kill %+ || true # reads might have finished
12166        echo "wait until dd is finished"
12167        wait
12168        log "removing the temporary file"
12169        rm -rf $DIR/$tfile || error "tmp file removal failed"
12170 }
12171 run_test 218 "parallel read and truncate should not deadlock ======================="
12172
12173 test_219() {
12174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12175         # write one partial page
12176         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12177         # set no grant so vvp_io_commit_write will do sync write
12178         $LCTL set_param fail_loc=0x411
12179         # write a full page at the end of file
12180         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12181
12182         $LCTL set_param fail_loc=0
12183         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12184         $LCTL set_param fail_loc=0x411
12185         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12186
12187         # LU-4201
12188         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12189         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12190 }
12191 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
12192
12193 test_220() { #LU-325
12194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12195         remote_ost_nodsh && skip "remote OST with nodsh" && return
12196         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12197         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12198         local OSTIDX=0
12199
12200         # create on MDT0000 so the last_id and next_id are correct
12201         mkdir $DIR/$tdir
12202         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
12203         OST=${OST%_UUID}
12204
12205         # on the mdt's osc
12206         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
12207         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
12208                         osc.$mdtosc_proc1.prealloc_last_id)
12209         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
12210                         osc.$mdtosc_proc1.prealloc_next_id)
12211
12212         $LFS df -i
12213
12214         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
12215         #define OBD_FAIL_OST_ENOINO              0x229
12216         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
12217         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
12218         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
12219
12220         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
12221
12222         MDSOBJS=$((last_id - next_id))
12223         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
12224
12225         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
12226         echo "OST still has $count kbytes free"
12227
12228         echo "create $MDSOBJS files @next_id..."
12229         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
12230
12231         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12232                         osc.$mdtosc_proc1.prealloc_last_id)
12233         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
12234                         osc.$mdtosc_proc1.prealloc_next_id)
12235
12236         echo "after creation, last_id=$last_id2, next_id=$next_id2"
12237         $LFS df -i
12238
12239         echo "cleanup..."
12240
12241         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
12242         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
12243
12244         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
12245         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
12246         echo "unlink $MDSOBJS files @$next_id..."
12247         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
12248 }
12249 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
12250
12251 test_221() {
12252         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12253         dd if=`which date` of=$MOUNT/date oflag=sync
12254         chmod +x $MOUNT/date
12255
12256         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
12257         $LCTL set_param fail_loc=0x80001401
12258
12259         $MOUNT/date > /dev/null
12260         rm -f $MOUNT/date
12261 }
12262 run_test 221 "make sure fault and truncate race to not cause OOM"
12263
12264 test_222a () {
12265         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12266        rm -rf $DIR/$tdir
12267        test_mkdir -p $DIR/$tdir
12268        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12269        createmany -o $DIR/$tdir/$tfile 10
12270        cancel_lru_locks mdc
12271        cancel_lru_locks osc
12272        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12273        $LCTL set_param fail_loc=0x31a
12274        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12275        $LCTL set_param fail_loc=0
12276        rm -r $DIR/$tdir
12277 }
12278 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12279
12280 test_222b () {
12281         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12282        rm -rf $DIR/$tdir
12283        test_mkdir -p $DIR/$tdir
12284        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12285        createmany -o $DIR/$tdir/$tfile 10
12286        cancel_lru_locks mdc
12287        cancel_lru_locks osc
12288        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12289        $LCTL set_param fail_loc=0x31a
12290        rm -r $DIR/$tdir || "AGL for rmdir failed"
12291        $LCTL set_param fail_loc=0
12292 }
12293 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12294
12295 test_223 () {
12296         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12297        rm -rf $DIR/$tdir
12298        test_mkdir -p $DIR/$tdir
12299        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12300        createmany -o $DIR/$tdir/$tfile 10
12301        cancel_lru_locks mdc
12302        cancel_lru_locks osc
12303        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12304        $LCTL set_param fail_loc=0x31b
12305        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12306        $LCTL set_param fail_loc=0
12307        rm -r $DIR/$tdir
12308 }
12309 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12310
12311 test_224a() { # LU-1039, MRP-303
12312         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12313         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12314         $LCTL set_param fail_loc=0x508
12315         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12316         $LCTL set_param fail_loc=0
12317         df $DIR
12318 }
12319 run_test 224a "Don't panic on bulk IO failure"
12320
12321 test_224b() { # LU-1039, MRP-303
12322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12323         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12324         cancel_lru_locks osc
12325         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12326         $LCTL set_param fail_loc=0x515
12327         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12328         $LCTL set_param fail_loc=0
12329         df $DIR
12330 }
12331 run_test 224b "Don't panic on bulk IO failure"
12332
12333 test_224c() { # LU-6441
12334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12335         local pages_per_rpc=$($LCTL get_param \
12336                                 osc.*.max_pages_per_rpc)
12337         local at_max=$($LCTL get_param -n at_max)
12338         local timeout=$($LCTL get_param -n timeout)
12339         local test_at="$LCTL get_param -n at_max"
12340         local param_at="$FSNAME.sys.at_max"
12341         local test_timeout="$LCTL get_param -n timeout"
12342         local param_timeout="$FSNAME.sys.timeout"
12343
12344         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
12345
12346         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
12347                 error "conf_param at_max=0 failed"
12348         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
12349                 error "conf_param timeout=5 failed"
12350
12351         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
12352         $LCTL set_param fail_loc=0x520
12353         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
12354         sync
12355         $LCTL set_param fail_loc=0
12356
12357         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
12358                 error "conf_param at_max=$at_max failed"
12359         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
12360                 $timeout || error "conf_param timeout=$timeout failed"
12361
12362         $LCTL set_param -n $pages_per_rpc
12363 }
12364 run_test 224c "Don't hang if one of md lost during large bulk RPC"
12365
12366 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12367 test_225a () {
12368         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12369         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12370         if [ -z ${MDSSURVEY} ]; then
12371               skip_env "mds-survey not found" && return
12372         fi
12373
12374         [ $MDSCOUNT -ge 2 ] &&
12375                 skip "skipping now for more than one MDT" && return
12376
12377        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12378             { skip "Need MDS version at least 2.2.51"; return; }
12379
12380        local mds=$(facet_host $SINGLEMDS)
12381        local target=$(do_nodes $mds 'lctl dl' | \
12382                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12383
12384        local cmd1="file_count=1000 thrhi=4"
12385        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12386        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12387        local cmd="$cmd1 $cmd2 $cmd3"
12388
12389        rm -f ${TMP}/mds_survey*
12390        echo + $cmd
12391        eval $cmd || error "mds-survey with zero-stripe failed"
12392        cat ${TMP}/mds_survey*
12393        rm -f ${TMP}/mds_survey*
12394 }
12395 run_test 225a "Metadata survey sanity with zero-stripe"
12396
12397 test_225b () {
12398         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12399         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12400         if [ -z ${MDSSURVEY} ]; then
12401               skip_env "mds-survey not found" && return
12402         fi
12403         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12404             { skip "Need MDS version at least 2.2.51"; return; }
12405
12406         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12407               skip_env "Need to mount OST to test" && return
12408         fi
12409
12410         [ $MDSCOUNT -ge 2 ] &&
12411                 skip "skipping now for more than one MDT" && return
12412        local mds=$(facet_host $SINGLEMDS)
12413        local target=$(do_nodes $mds 'lctl dl' | \
12414                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12415
12416        local cmd1="file_count=1000 thrhi=4"
12417        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12418        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12419        local cmd="$cmd1 $cmd2 $cmd3"
12420
12421        rm -f ${TMP}/mds_survey*
12422        echo + $cmd
12423        eval $cmd || error "mds-survey with stripe_count failed"
12424        cat ${TMP}/mds_survey*
12425        rm -f ${TMP}/mds_survey*
12426 }
12427 run_test 225b "Metadata survey sanity with stripe_count = 1"
12428
12429 mcreate_path2fid () {
12430         local mode=$1
12431         local major=$2
12432         local minor=$3
12433         local name=$4
12434         local desc=$5
12435         local path=$DIR/$tdir/$name
12436         local fid
12437         local rc
12438         local fid_path
12439
12440         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12441                 error "cannot create $desc"
12442
12443         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12444         rc=$?
12445         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12446
12447         fid_path=$($LFS fid2path $MOUNT $fid)
12448         rc=$?
12449         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12450
12451         [ "$path" == "$fid_path" ] ||
12452                 error "fid2path returned $fid_path, expected $path"
12453
12454         echo "pass with $path and $fid"
12455 }
12456
12457 test_226a () {
12458         rm -rf $DIR/$tdir
12459         mkdir -p $DIR/$tdir
12460
12461         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12462         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12463         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12464         mcreate_path2fid 0040666 0 0 dir "directory"
12465         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12466         mcreate_path2fid 0100666 0 0 file "regular file"
12467         mcreate_path2fid 0120666 0 0 link "symbolic link"
12468         mcreate_path2fid 0140666 0 0 sock "socket"
12469 }
12470 run_test 226a "call path2fid and fid2path on files of all type"
12471
12472 test_226b () {
12473         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12474         rm -rf $DIR/$tdir
12475         local MDTIDX=1
12476
12477         mkdir -p $DIR/$tdir
12478         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12479                 error "create remote directory failed"
12480         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12481         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12482                                 "character special file (null)"
12483         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12484                                 "character special file (no device)"
12485         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12486         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12487                                 "block special file (loop)"
12488         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12489         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12490         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12491 }
12492 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12493
12494 # LU-1299 Executing or running ldd on a truncated executable does not
12495 # cause an out-of-memory condition.
12496 test_227() {
12497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12498         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12499         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12500         chmod +x $MOUNT/date
12501
12502         $MOUNT/date > /dev/null
12503         ldd $MOUNT/date > /dev/null
12504         rm -f $MOUNT/date
12505 }
12506 run_test 227 "running truncated executable does not cause OOM"
12507
12508 # LU-1512 try to reuse idle OI blocks
12509 test_228a() {
12510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12511         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12512         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12513                 skip "non-ldiskfs backend" && return
12514
12515         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12516         local myDIR=$DIR/$tdir
12517
12518         mkdir -p $myDIR
12519         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12520         $LCTL set_param fail_loc=0x80001002
12521         createmany -o $myDIR/t- 10000
12522         $LCTL set_param fail_loc=0
12523         # The guard is current the largest FID holder
12524         touch $myDIR/guard
12525         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12526                     tr -d '[')
12527         local IDX=$(($SEQ % 64))
12528
12529         do_facet $SINGLEMDS sync
12530         # Make sure journal flushed.
12531         sleep 6
12532         local blk1=$(do_facet $SINGLEMDS \
12533                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12534                      grep Blockcount | awk '{print $4}')
12535
12536         # Remove old files, some OI blocks will become idle.
12537         unlinkmany $myDIR/t- 10000
12538         # Create new files, idle OI blocks should be reused.
12539         createmany -o $myDIR/t- 2000
12540         do_facet $SINGLEMDS sync
12541         # Make sure journal flushed.
12542         sleep 6
12543         local blk2=$(do_facet $SINGLEMDS \
12544                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12545                      grep Blockcount | awk '{print $4}')
12546
12547         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12548 }
12549 run_test 228a "try to reuse idle OI blocks"
12550
12551 test_228b() {
12552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12553         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12554         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12555                 skip "non-ldiskfs backend" && return
12556
12557         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12558         local myDIR=$DIR/$tdir
12559
12560         mkdir -p $myDIR
12561         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12562         $LCTL set_param fail_loc=0x80001002
12563         createmany -o $myDIR/t- 10000
12564         $LCTL set_param fail_loc=0
12565         # The guard is current the largest FID holder
12566         touch $myDIR/guard
12567         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12568                     tr -d '[')
12569         local IDX=$(($SEQ % 64))
12570
12571         do_facet $SINGLEMDS sync
12572         # Make sure journal flushed.
12573         sleep 6
12574         local blk1=$(do_facet $SINGLEMDS \
12575                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12576                      grep Blockcount | awk '{print $4}')
12577
12578         # Remove old files, some OI blocks will become idle.
12579         unlinkmany $myDIR/t- 10000
12580
12581         # stop the MDT
12582         stop $SINGLEMDS || error "Fail to stop MDT."
12583         # remount the MDT
12584         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12585
12586         df $MOUNT || error "Fail to df."
12587         # Create new files, idle OI blocks should be reused.
12588         createmany -o $myDIR/t- 2000
12589         do_facet $SINGLEMDS sync
12590         # Make sure journal flushed.
12591         sleep 6
12592         local blk2=$(do_facet $SINGLEMDS \
12593                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12594                      grep Blockcount | awk '{print $4}')
12595
12596         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12597 }
12598 run_test 228b "idle OI blocks can be reused after MDT restart"
12599
12600 #LU-1881
12601 test_228c() {
12602         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12603         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12604         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12605                 skip "non-ldiskfs backend" && return
12606
12607         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12608         local myDIR=$DIR/$tdir
12609
12610         mkdir -p $myDIR
12611         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12612         $LCTL set_param fail_loc=0x80001002
12613         # 20000 files can guarantee there are index nodes in the OI file
12614         createmany -o $myDIR/t- 20000
12615         $LCTL set_param fail_loc=0
12616         # The guard is current the largest FID holder
12617         touch $myDIR/guard
12618         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12619                     tr -d '[')
12620         local IDX=$(($SEQ % 64))
12621
12622         do_facet $SINGLEMDS sync
12623         # Make sure journal flushed.
12624         sleep 6
12625         local blk1=$(do_facet $SINGLEMDS \
12626                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12627                      grep Blockcount | awk '{print $4}')
12628
12629         # Remove old files, some OI blocks will become idle.
12630         unlinkmany $myDIR/t- 20000
12631         rm -f $myDIR/guard
12632         # The OI file should become empty now
12633
12634         # Create new files, idle OI blocks should be reused.
12635         createmany -o $myDIR/t- 2000
12636         do_facet $SINGLEMDS sync
12637         # Make sure journal flushed.
12638         sleep 6
12639         local blk2=$(do_facet $SINGLEMDS \
12640                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12641                      grep Blockcount | awk '{print $4}')
12642
12643         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12644 }
12645 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12646
12647 test_229() { # LU-2482, LU-3448
12648         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12649                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
12650                 return
12651         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12652         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12653
12654         rm -f $DIR/$tfile
12655
12656         # Create a file with a released layout and stripe count 2.
12657         $MULTIOP $DIR/$tfile H2c ||
12658                 error "failed to create file with released layout"
12659
12660         $GETSTRIPE -v $DIR/$tfile
12661
12662         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12663         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12664
12665         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12666         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12667         stat $DIR/$tfile || error "failed to stat released file"
12668
12669         chown $RUNAS_ID $DIR/$tfile ||
12670                 error "chown $RUNAS_ID $DIR/$tfile failed"
12671
12672         chgrp $RUNAS_ID $DIR/$tfile ||
12673                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12674
12675         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12676         rm $DIR/$tfile || error "failed to remove released file"
12677 }
12678 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12679
12680 test_230a() {
12681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12682         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12683         local MDTIDX=1
12684
12685         test_mkdir $DIR/$tdir
12686         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
12687         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12688         [ $mdt_idx -ne 0 ] &&
12689                 error "create local directory on wrong MDT $mdt_idx"
12690
12691         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12692                         error "create remote directory failed"
12693         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12694         [ $mdt_idx -ne $MDTIDX ] &&
12695                 error "create remote directory on wrong MDT $mdt_idx"
12696
12697         createmany -o $DIR/$tdir/test_230/t- 10 ||
12698                 error "create files on remote directory failed"
12699         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12700         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12701         rm -r $DIR/$tdir || error "unlink remote directory failed"
12702 }
12703 run_test 230a "Create remote directory and files under the remote directory"
12704
12705 test_230b() {
12706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12707         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12708         local MDTIDX=1
12709         local mdt_index
12710         local i
12711         local file
12712         local pid
12713         local stripe_count
12714         local migrate_dir=$DIR/$tdir/migrate_dir
12715         local other_dir=$DIR/$tdir/other_dir
12716
12717         test_mkdir $DIR/$tdir
12718         test_mkdir -i0 -c1 $migrate_dir
12719         test_mkdir -i0 -c1 $other_dir
12720         for ((i=0; i<10; i++)); do
12721                 mkdir -p $migrate_dir/dir_${i}
12722                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12723                         error "create files under remote dir failed $i"
12724         done
12725
12726         cp /etc/passwd $migrate_dir/$tfile
12727         cp /etc/passwd $other_dir/$tfile
12728         chattr +SAD $migrate_dir
12729         chattr +SAD $migrate_dir/$tfile
12730
12731         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12732         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12733         local old_dir_mode=$(stat -c%f $migrate_dir)
12734         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12735
12736         mkdir -p $migrate_dir/dir_default_stripe2
12737         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12738         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12739
12740         mkdir -p $other_dir
12741         ln $migrate_dir/$tfile $other_dir/luna
12742         ln $migrate_dir/$tfile $migrate_dir/sofia
12743         ln $other_dir/$tfile $migrate_dir/david
12744         ln -s $migrate_dir/$tfile $other_dir/zachary
12745         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12746         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12747
12748         $LFS migrate -m $MDTIDX $migrate_dir ||
12749                 error "fails on migrating remote dir to MDT1"
12750
12751         echo "migratate to MDT1, then checking.."
12752         for ((i = 0; i < 10; i++)); do
12753                 for file in $(find $migrate_dir/dir_${i}); do
12754                         mdt_index=$($LFS getstripe -M $file)
12755                         [ $mdt_index == $MDTIDX ] ||
12756                                 error "$file is not on MDT${MDTIDX}"
12757                 done
12758         done
12759
12760         # the multiple link file should still in MDT0
12761         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12762         [ $mdt_index == 0 ] ||
12763                 error "$file is not on MDT${MDTIDX}"
12764
12765         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12766         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12767                 error " expect $old_dir_flag get $new_dir_flag"
12768
12769         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12770         [ "$old_file_flag" = "$new_file_flag" ] ||
12771                 error " expect $old_file_flag get $new_file_flag"
12772
12773         local new_dir_mode=$(stat -c%f $migrate_dir)
12774         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12775                 error "expect mode $old_dir_mode get $new_dir_mode"
12776
12777         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12778         [ "$old_file_mode" = "$new_file_mode" ] ||
12779                 error "expect mode $old_file_mode get $new_file_mode"
12780
12781         diff /etc/passwd $migrate_dir/$tfile ||
12782                 error "$tfile different after migration"
12783
12784         diff /etc/passwd $other_dir/luna ||
12785                 error "luna different after migration"
12786
12787         diff /etc/passwd $migrate_dir/sofia ||
12788                 error "sofia different after migration"
12789
12790         diff /etc/passwd $migrate_dir/david ||
12791                 error "david different after migration"
12792
12793         diff /etc/passwd $other_dir/zachary ||
12794                 error "zachary different after migration"
12795
12796         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12797                 error "${tfile}_ln different after migration"
12798
12799         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12800                 error "${tfile}_ln_other different after migration"
12801
12802         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12803         [ $stripe_count = 2 ] ||
12804                 error "dir strpe_count $d != 2 after migration."
12805
12806         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12807         [ $stripe_count = 2 ] ||
12808                 error "file strpe_count $d != 2 after migration."
12809
12810         #migrate back to MDT0
12811         MDTIDX=0
12812
12813         $LFS migrate -m $MDTIDX $migrate_dir ||
12814                 error "fails on migrating remote dir to MDT0"
12815
12816         echo "migrate back to MDT0, checking.."
12817         for file in $(find $migrate_dir); do
12818                 mdt_index=$($LFS getstripe -M $file)
12819                 [ $mdt_index == $MDTIDX ] ||
12820                         error "$file is not on MDT${MDTIDX}"
12821         done
12822
12823         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12824         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12825                 error " expect $old_dir_flag get $new_dir_flag"
12826
12827         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12828         [ "$old_file_flag" = "$new_file_flag" ] ||
12829                 error " expect $old_file_flag get $new_file_flag"
12830
12831         local new_dir_mode=$(stat -c%f $migrate_dir)
12832         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12833                 error "expect mode $old_dir_mode get $new_dir_mode"
12834
12835         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12836         [ "$old_file_mode" = "$new_file_mode" ] ||
12837                 error "expect mode $old_file_mode get $new_file_mode"
12838
12839         diff /etc/passwd ${migrate_dir}/$tfile ||
12840                 error "$tfile different after migration"
12841
12842         diff /etc/passwd ${other_dir}/luna ||
12843                 error "luna different after migration"
12844
12845         diff /etc/passwd ${migrate_dir}/sofia ||
12846                 error "sofia different after migration"
12847
12848         diff /etc/passwd ${other_dir}/zachary ||
12849                 error "zachary different after migration"
12850
12851         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12852                 error "${tfile}_ln different after migration"
12853
12854         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12855                 error "${tfile}_ln_other different after migration"
12856
12857         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12858         [ $stripe_count = 2 ] ||
12859                 error "dir strpe_count $d != 2 after migration."
12860
12861         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12862         [ $stripe_count = 2 ] ||
12863                 error "file strpe_count $d != 2 after migration."
12864
12865         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12866 }
12867 run_test 230b "migrate directory"
12868
12869 test_230c() {
12870         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12871         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12872         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12873         local MDTIDX=1
12874         local mdt_index
12875         local file
12876         local migrate_dir=$DIR/$tdir/migrate_dir
12877
12878         #If migrating directory fails in the middle, all entries of
12879         #the directory is still accessiable.
12880         test_mkdir $DIR/$tdir
12881         test_mkdir -i0 -c1 $migrate_dir
12882         stat $migrate_dir
12883         createmany -o $migrate_dir/f 10 ||
12884                 error "create files under ${migrate_dir} failed"
12885
12886         #failed after migrating 5 entries
12887         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12888         do_facet mds1 lctl set_param fail_loc=0x20001801
12889         do_facet mds1 lctl  set_param fail_val=5
12890         local t=$(ls $migrate_dir | wc -l)
12891         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
12892                 error "migrate should fail after 5 entries"
12893
12894         mkdir $migrate_dir/dir &&
12895                 error "mkdir succeeds under migrating directory"
12896         touch $migrate_dir/file &&
12897                 error "touch file succeeds under migrating directory"
12898
12899         local u=$(ls $migrate_dir | wc -l)
12900         [ "$u" == "$t" ] || error "$u != $t during migration"
12901
12902         for file in $(find $migrate_dir); do
12903                 stat $file || error "stat $file failed"
12904         done
12905
12906         do_facet mds1 lctl set_param fail_loc=0
12907         do_facet mds1 lctl set_param fail_val=0
12908
12909         $LFS migrate -m $MDTIDX $migrate_dir ||
12910                 error "migrate open files should failed with open files"
12911
12912         echo "Finish migration, then checking.."
12913         for file in $(find $migrate_dir); do
12914                 mdt_index=$($LFS getstripe -M $file)
12915                 [ $mdt_index == $MDTIDX ] ||
12916                         error "$file is not on MDT${MDTIDX}"
12917         done
12918
12919         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12920 }
12921 run_test 230c "check directory accessiblity if migration is failed"
12922
12923 test_230d() {
12924         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12925         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12926         local MDTIDX=1
12927         local mdt_index
12928         local migrate_dir=$DIR/$tdir/migrate_dir
12929         local i
12930         local j
12931
12932         test_mkdir $DIR/$tdir
12933         test_mkdir -i0 -c1 $migrate_dir
12934
12935         for ((i=0; i<100; i++)); do
12936                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
12937                 createmany -o $migrate_dir/dir_${i}/f 100 ||
12938                         error "create files under remote dir failed $i"
12939         done
12940
12941         $LFS migrate -m $MDTIDX $migrate_dir ||
12942                 error "migrate remote dir error"
12943
12944         echo "Finish migration, then checking.."
12945         for file in $(find $migrate_dir); do
12946                 mdt_index=$($LFS getstripe -M $file)
12947                 [ $mdt_index == $MDTIDX ] ||
12948                         error "$file is not on MDT${MDTIDX}"
12949         done
12950
12951         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12952 }
12953 run_test 230d "check migrate big directory"
12954
12955 test_230e() {
12956         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12957         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12958         local i
12959         local j
12960         local a_fid
12961         local b_fid
12962
12963         mkdir -p $DIR/$tdir
12964         mkdir $DIR/$tdir/migrate_dir
12965         mkdir $DIR/$tdir/other_dir
12966         touch $DIR/$tdir/migrate_dir/a
12967         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
12968         ls $DIR/$tdir/other_dir
12969
12970         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
12971                 error "migrate dir fails"
12972
12973         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
12974         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
12975
12976         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12977         [ $mdt_index == 0 ] || error "a is not on MDT0"
12978
12979         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
12980                 error "migrate dir fails"
12981
12982         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
12983         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
12984
12985         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
12986         [ $mdt_index == 1 ] || error "a is not on MDT1"
12987
12988         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
12989         [ $mdt_index == 1 ] || error "b is not on MDT1"
12990
12991         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
12992         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
12993
12994         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
12995
12996         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12997 }
12998 run_test 230e "migrate mulitple local link files"
12999
13000 test_230f() {
13001         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13002         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13003         local a_fid
13004         local ln_fid
13005
13006         mkdir -p $DIR/$tdir
13007         mkdir $DIR/$tdir/migrate_dir
13008         $LFS mkdir -i1 $DIR/$tdir/other_dir
13009         touch $DIR/$tdir/migrate_dir/a
13010         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
13011         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
13012         ls $DIR/$tdir/other_dir
13013
13014         # a should be migrated to MDT1, since no other links on MDT0
13015         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13016                 error "migrate dir fails"
13017         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13018         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13019         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13020         [ $mdt_index == 1 ] || error "a is not on MDT1"
13021
13022         # a should stay on MDT1, because it is a mulitple link file
13023         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13024                 error "migrate dir fails"
13025         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13026         [ $mdt_index == 1 ] || error "a is not on MDT1"
13027
13028         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13029                 error "migrate dir fails"
13030
13031         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13032         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
13033         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
13034
13035         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
13036         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
13037
13038         # a should be migrated to MDT0, since no other links on MDT1
13039         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13040                 error "migrate dir fails"
13041         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13042         [ $mdt_index == 0 ] || error "a is not on MDT0"
13043
13044         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13045 }
13046 run_test 230f "migrate mulitple remote link files"
13047
13048 test_230g() {
13049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13050         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13051
13052         mkdir -p $DIR/$tdir/migrate_dir
13053
13054         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
13055                 error "migrating dir to non-exist MDT succeeds"
13056         true
13057 }
13058 run_test 230g "migrate dir to non-exist MDT"
13059
13060 test_231a()
13061 {
13062         # For simplicity this test assumes that max_pages_per_rpc
13063         # is the same across all OSCs
13064         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
13065         local bulk_size=$((max_pages * 4096))
13066
13067         mkdir -p $DIR/$tdir
13068
13069         # clear the OSC stats
13070         $LCTL set_param osc.*.stats=0 &>/dev/null
13071
13072         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
13073         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
13074                 oflag=direct &>/dev/null || error "dd failed"
13075
13076         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
13077         if [ x$nrpcs != "x1" ]; then
13078                 error "found $nrpc ost_write RPCs, not 1 as expected"
13079         fi
13080
13081         # Drop the OSC cache, otherwise we will read from it
13082         cancel_lru_locks osc
13083
13084         # clear the OSC stats
13085         $LCTL set_param osc.*.stats=0 &>/dev/null
13086
13087         # Client reads $bulk_size.
13088         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
13089                 iflag=direct &>/dev/null || error "dd failed"
13090
13091         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
13092         if [ x$nrpcs != "x1" ]; then
13093                 error "found $nrpc ost_read RPCs, not 1 as expected"
13094         fi
13095 }
13096 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
13097
13098 test_231b() {
13099         mkdir -p $DIR/$tdir
13100         local i
13101         for i in {0..1023}; do
13102                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
13103                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
13104                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
13105         done
13106         sync
13107 }
13108 run_test 231b "must not assert on fully utilized OST request buffer"
13109
13110 test_232() {
13111         mkdir -p $DIR/$tdir
13112         #define OBD_FAIL_LDLM_OST_LVB            0x31c
13113         $LCTL set_param fail_loc=0x31c
13114
13115         # ignore dd failure
13116         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
13117
13118         $LCTL set_param fail_loc=0
13119         umount_client $MOUNT || error "umount failed"
13120         mount_client $MOUNT || error "mount failed"
13121 }
13122 run_test 232 "failed lock should not block umount"
13123
13124 test_233a() {
13125         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
13126         { skip "Need MDS version at least 2.3.64"; return; }
13127
13128         local fid=$($LFS path2fid $MOUNT)
13129         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13130                 error "cannot access $MOUNT using its FID '$fid'"
13131 }
13132 run_test 233a "checking that OBF of the FS root succeeds"
13133
13134 test_233b() {
13135         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
13136         { skip "Need MDS version at least 2.5.90"; return; }
13137
13138         local fid=$($LFS path2fid $MOUNT/.lustre)
13139         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13140                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
13141
13142         fid=$($LFS path2fid $MOUNT/.lustre/fid)
13143         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
13144                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
13145 }
13146 run_test 233b "checking that OBF of the FS .lustre succeeds"
13147
13148 test_234() {
13149         local p="$TMP/sanityN-$TESTNAME.parameters"
13150         save_lustre_params client "llite.*.xattr_cache" > $p
13151         lctl set_param llite.*.xattr_cache 1 ||
13152                 { skip "xattr cache is not supported"; return 0; }
13153
13154         mkdir -p $DIR/$tdir || error "mkdir failed"
13155         touch $DIR/$tdir/$tfile || error "touch failed"
13156         # OBD_FAIL_LLITE_XATTR_ENOMEM
13157         $LCTL set_param fail_loc=0x1405
13158         # output of the form: attr 2 4 44 3 fc13 x86_64
13159         V=($(IFS=".-" rpm -q attr))
13160         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
13161               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
13162                 # attr pre-2.4.44-7 had a bug with rc
13163                 # LU-3703 - SLES 11 and FC13 clients have older attr
13164                 getfattr -n user.attr $DIR/$tdir/$tfile &&
13165                         error "getfattr should have failed with ENOMEM"
13166         else
13167                 skip "LU-3703: attr version $(getfattr --version) too old"
13168         fi
13169         $LCTL set_param fail_loc=0x0
13170         rm -rf $DIR/$tdir
13171
13172         restore_lustre_params < $p
13173         rm -f $p
13174 }
13175 run_test 234 "xattr cache should not crash on ENOMEM"
13176
13177 test_235() {
13178         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
13179                 skip "Need MDS version at least 2.4.52" && return
13180         flock_deadlock $DIR/$tfile
13181         local RC=$?
13182         case $RC in
13183                 0)
13184                 ;;
13185                 124) error "process hangs on a deadlock"
13186                 ;;
13187                 *) error "error executing flock_deadlock $DIR/$tfile"
13188                 ;;
13189         esac
13190 }
13191 run_test 235 "LU-1715: flock deadlock detection does not work properly"
13192
13193 #LU-2935
13194 test_236() {
13195         check_swap_layouts_support && return 0
13196         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
13197
13198         local ref1=/etc/passwd
13199         local ref2=/etc/group
13200         local file1=$DIR/$tdir/f1
13201         local file2=$DIR/$tdir/f2
13202
13203         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
13204         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
13205         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
13206         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
13207         local fd=$(free_fd)
13208         local cmd="exec $fd<>$file2"
13209         eval $cmd
13210         rm $file2
13211         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
13212                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
13213         cmd="exec $fd>&-"
13214         eval $cmd
13215         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13216
13217         #cleanup
13218         rm -rf $DIR/$tdir
13219 }
13220 run_test 236 "Layout swap on open unlinked file"
13221
13222 # test to verify file handle related system calls
13223 # (name_to_handle_at/open_by_handle_at)
13224 # The new system calls are supported in glibc >= 2.14.
13225
13226 test_237() {
13227         echo "Test file_handle syscalls" > $DIR/$tfile ||
13228                 error "write failed"
13229         check_fhandle_syscalls $DIR/$tfile ||
13230                 error "check_fhandle_syscalls failed"
13231 }
13232 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
13233
13234 # LU-4659 linkea consistency
13235 test_238() {
13236         local server_version=$(lustre_version_code $SINGLEMDS)
13237
13238         [[ $server_version -gt $(version_code 2.5.57) ]] ||
13239                 [[ $server_version -gt $(version_code 2.5.1) &&
13240                    $server_version -lt $(version_code 2.5.50) ]] ||
13241                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
13242
13243         touch $DIR/$tfile
13244         ln $DIR/$tfile $DIR/$tfile.lnk
13245         touch $DIR/$tfile.new
13246         mv $DIR/$tfile.new $DIR/$tfile
13247         local fid1=$($LFS path2fid $DIR/$tfile)
13248         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
13249         local path1=$($LFS fid2path $FSNAME "$fid1")
13250         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
13251         local path2=$($LFS fid2path $FSNAME "$fid2")
13252         [ $tfile.lnk == $path2 ] ||
13253                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
13254         rm -f $DIR/$tfile*
13255 }
13256 run_test 238 "Verify linkea consistency"
13257
13258 test_239() {
13259         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
13260                 skip "Need MDS version at least 2.5.60" && return
13261         local list=$(comma_list $(mdts_nodes))
13262
13263         mkdir -p $DIR/$tdir
13264         createmany -o $DIR/$tdir/f- 5000
13265         unlinkmany $DIR/$tdir/f- 5000
13266         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
13267         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
13268                         osc.*MDT*.sync_in_flight" | calc_sum)
13269         [ "$changes" -eq 0 ] || error "$changes not synced"
13270 }
13271 run_test 239 "osp_sync test"
13272
13273 test_239a() { #LU-5297
13274         touch $DIR/$tfile
13275         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
13276         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
13277         chgrp $RUNAS_GID $DIR/$tfile
13278         wait_delete_completed
13279 }
13280 run_test 239a "process invalid osp sync record correctly"
13281
13282 test_239b() { #LU-5297
13283         touch $DIR/$tfile1
13284         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
13285         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
13286         chgrp $RUNAS_GID $DIR/$tfile1
13287         wait_delete_completed
13288         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13289         touch $DIR/$tfile2
13290         chgrp $RUNAS_GID $DIR/$tfile2
13291         wait_delete_completed
13292 }
13293 run_test 239b "process osp sync record with ENOMEM error correctly"
13294
13295 test_240() {
13296         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13297
13298         mkdir -p $DIR/$tdir
13299
13300         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
13301                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
13302         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
13303                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
13304
13305         umount_client $MOUNT || error "umount failed"
13306         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
13307         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
13308         mount_client $MOUNT || error "failed to mount client"
13309
13310         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
13311         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
13312 }
13313 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
13314
13315 test_241_bio() {
13316         for LOOP in $(seq $1); do
13317                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
13318                 cancel_lru_locks osc
13319         done
13320 }
13321
13322 test_241_dio() {
13323         for LOOP in $(seq $1); do
13324                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
13325                                                 iflag=direct 2>/dev/null
13326         done
13327 }
13328
13329 test_241() {
13330         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13331         ls -la $DIR/$tfile
13332         cancel_lru_locks osc
13333         test_241_bio 1000 &
13334         PID=$!
13335         test_241_dio 1000
13336         wait $PID
13337 }
13338 run_test 241 "bio vs dio"
13339
13340 test_241b() {
13341         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
13342         ls -la $DIR/$tfile
13343         test_241_dio 1000 &
13344         PID=$!
13345         test_241_dio 1000
13346         wait $PID
13347 }
13348 run_test 241b "dio vs dio"
13349
13350 test_242() {
13351         mkdir -p $DIR/$tdir
13352         touch $DIR/$tdir/$tfile
13353
13354         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
13355         do_facet mds1 lctl set_param fail_loc=0x105
13356         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
13357
13358         do_facet mds1 lctl set_param fail_loc=0
13359         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
13360 }
13361 run_test 242 "mdt_readpage failure should not cause directory unreadable"
13362
13363 test_243()
13364 {
13365         test_mkdir -p $DIR/$tdir
13366         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
13367 }
13368 run_test 243 "various group lock tests"
13369
13370 test_244()
13371 {
13372         test_mkdir -p $DIR/$tdir
13373         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
13374         sendfile_grouplock $DIR/$tdir/$tfile || \
13375                 error "sendfile+grouplock failed"
13376         rm -rf $DIR/$tdir
13377 }
13378 run_test 244 "sendfile with group lock tests"
13379
13380 test_245() {
13381         local flagname="multi_mod_rpcs"
13382         local connect_data_name="max_mod_rpcs"
13383         local out
13384
13385         # check if multiple modify RPCs flag is set
13386         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
13387                 grep "connect_flags:")
13388         echo "$out"
13389
13390         echo "$out" | grep -qw $flagname
13391         if [ $? -ne 0 ]; then
13392                 echo "connect flag $flagname is not set"
13393                 return
13394         fi
13395
13396         # check if multiple modify RPCs data is set
13397         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
13398         echo "$out"
13399
13400         echo "$out" | grep -qw $connect_data_name ||
13401                 error "import should have connect data $connect_data_name"
13402 }
13403 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
13404
13405 test_246() { # LU-7371
13406         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
13407                 skip "Need OST version >= 2.7.62" && return 0
13408         do_facet ost1 $LCTL set_param fail_val=4095
13409 #define OBD_FAIL_OST_READ_SIZE          0x234
13410         do_facet ost1 $LCTL set_param fail_loc=0x234
13411         $LFS setstripe $DIR/$tfile -i 0 -c 1
13412         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
13413         cancel_lru_locks $FSNAME-OST0000
13414         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
13415 }
13416 run_test 246 "Read file of size 4095 should return right length"
13417
13418 test_250() {
13419         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
13420          && skip "no 16TB file size limit on ZFS" && return
13421
13422         $SETSTRIPE -c 1 $DIR/$tfile
13423         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
13424         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
13425         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
13426         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
13427                 conv=notrunc,fsync && error "append succeeded"
13428         return 0
13429 }
13430 run_test 250 "Write above 16T limit"
13431
13432 test_251() {
13433         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
13434
13435         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
13436         #Skip once - writing the first stripe will succeed
13437         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13438         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
13439                 error "short write happened"
13440
13441         $LCTL set_param fail_loc=0xa0001407 fail_val=1
13442         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
13443                 error "short read happened"
13444
13445         rm -f $DIR/$tfile
13446 }
13447 run_test 251 "Handling short read and write correctly"
13448
13449 test_252() {
13450         local tgt
13451         local dev
13452         local out
13453         local uuid
13454         local num
13455         local gen
13456
13457         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
13458              "$(facet_fstype mds1)" != "ldiskfs" ]; then
13459                 skip "can only run lr_reader on ldiskfs target"
13460                 return
13461         fi
13462
13463         # check lr_reader on OST0000
13464         tgt=ost1
13465         dev=$(facet_device $tgt)
13466         out=$(do_facet $tgt $LR_READER $dev)
13467         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13468         echo "$out"
13469         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
13470         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
13471                 error "Invalid uuid returned by $LR_READER on target $tgt"
13472         echo -e "uuid returned by $LR_READER is '$uuid'\n"
13473
13474         # check lr_reader -c on MDT0000
13475         tgt=mds1
13476         dev=$(facet_device $tgt)
13477         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
13478                 echo "$LR_READER does not support additional options"
13479                 return 0
13480         fi
13481         out=$(do_facet $tgt $LR_READER -c $dev)
13482         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13483         echo "$out"
13484         num=$(echo "$out" | grep -c "mdtlov")
13485         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
13486                 error "Invalid number of mdtlov clients returned by $LR_READER"
13487         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
13488
13489         # check lr_reader -cr on MDT0000
13490         out=$(do_facet $tgt $LR_READER -cr $dev)
13491         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
13492         echo "$out"
13493         echo "$out" | grep -q "^reply_data:$" ||
13494                 error "$LR_READER should have returned 'reply_data' section"
13495         num=$(echo "$out" | grep -c "client_generation")
13496         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
13497 }
13498 run_test 252 "check lr_reader tool"
13499
13500
13501 cleanup_test_300() {
13502         trap 0
13503         umask $SAVE_UMASK
13504 }
13505 test_striped_dir() {
13506         local mdt_index=$1
13507         local stripe_count
13508         local stripe_index
13509
13510         mkdir -p $DIR/$tdir
13511
13512         SAVE_UMASK=$(umask)
13513         trap cleanup_test_300 RETURN EXIT
13514
13515         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
13516                                                 $DIR/$tdir/striped_dir ||
13517                 error "set striped dir error"
13518
13519         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
13520         [ "$mode" = "755" ] || error "expect 755 got $mode"
13521
13522         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
13523                 error "getdirstripe failed"
13524         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
13525         if [ "$stripe_count" != "2" ]; then
13526                 error "stripe_count is $stripe_count, expect 2"
13527         fi
13528
13529         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
13530         if [ "$stripe_index" != "$mdt_index" ]; then
13531                 error "stripe_index is $stripe_index, expect $mdt_index"
13532         fi
13533
13534         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13535                 error "nlink error after create striped dir"
13536
13537         mkdir $DIR/$tdir/striped_dir/a
13538         mkdir $DIR/$tdir/striped_dir/b
13539
13540         stat $DIR/$tdir/striped_dir/a ||
13541                 error "create dir under striped dir failed"
13542         stat $DIR/$tdir/striped_dir/b ||
13543                 error "create dir under striped dir failed"
13544
13545         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
13546                 error "nlink error after mkdir"
13547
13548         rmdir $DIR/$tdir/striped_dir/a
13549         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
13550                 error "nlink error after rmdir"
13551
13552         rmdir $DIR/$tdir/striped_dir/b
13553         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
13554                 error "nlink error after rmdir"
13555
13556         chattr +i $DIR/$tdir/striped_dir
13557         createmany -o $DIR/$tdir/striped_dir/f 10 &&
13558                 error "immutable flags not working under striped dir!"
13559         chattr -i $DIR/$tdir/striped_dir
13560
13561         rmdir $DIR/$tdir/striped_dir ||
13562                 error "rmdir striped dir error"
13563
13564         cleanup_test_300
13565
13566         true
13567 }
13568
13569 test_300a() {
13570         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13571         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13572
13573         test_striped_dir 0 || error "failed on striped dir on MDT0"
13574         test_striped_dir 1 || error "failed on striped dir on MDT0"
13575 }
13576 run_test 300a "basic striped dir sanity test"
13577
13578 test_300b() {
13579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13580         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13581         local i
13582         local mtime1
13583         local mtime2
13584         local mtime3
13585
13586         test_mkdir $DIR/$tdir || error "mkdir fail"
13587         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13588                 error "set striped dir error"
13589         for ((i=0; i<10; i++)); do
13590                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
13591                 sleep 1
13592                 touch $DIR/$tdir/striped_dir/file_$i ||
13593                                         error "touch error $i"
13594                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
13595                 [ $mtime1 -eq $mtime2 ] &&
13596                         error "mtime not change after create"
13597                 sleep 1
13598                 rm -f $DIR/$tdir/striped_dir/file_$i ||
13599                                         error "unlink error $i"
13600                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
13601                 [ $mtime2 -eq $mtime3 ] &&
13602                         error "mtime did not change after unlink"
13603         done
13604         true
13605 }
13606 run_test 300b "check ctime/mtime for striped dir"
13607
13608 test_300c() {
13609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13610         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13611         local file_count
13612
13613         mkdir -p $DIR/$tdir
13614         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
13615                 error "set striped dir error"
13616
13617         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
13618                 error "chown striped dir failed"
13619
13620         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
13621                 error "create 5k files failed"
13622
13623         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
13624
13625         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
13626
13627         rm -rf $DIR/$tdir
13628 }
13629 run_test 300c "chown && check ls under striped directory"
13630
13631 test_300d() {
13632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13633         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13634         local stripe_count
13635         local file
13636
13637         mkdir -p $DIR/$tdir
13638         $SETSTRIPE -c 2 $DIR/$tdir
13639
13640         #local striped directory
13641         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13642                 error "set striped dir error"
13643         createmany -o $DIR/$tdir/striped_dir/f 10 ||
13644                 error "create 10 files failed"
13645
13646         #remote striped directory
13647         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
13648                 error "set striped dir error"
13649         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
13650                 error "create 10 files failed"
13651
13652         for file in $(find $DIR/$tdir); do
13653                 stripe_count=$($GETSTRIPE -c $file)
13654                 [ $stripe_count -eq 2 ] ||
13655                         error "wrong stripe $stripe_count for $file"
13656         done
13657
13658         rm -rf $DIR/$tdir
13659 }
13660 run_test 300d "check default stripe under striped directory"
13661
13662 test_300e() {
13663         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13664                 skip "Need MDS version at least 2.7.55" && return
13665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13666         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13667         local stripe_count
13668         local file
13669
13670         mkdir -p $DIR/$tdir
13671
13672         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13673                 error "set striped dir error"
13674
13675         touch $DIR/$tdir/striped_dir/a
13676         touch $DIR/$tdir/striped_dir/b
13677         touch $DIR/$tdir/striped_dir/c
13678
13679         mkdir $DIR/$tdir/striped_dir/dir_a
13680         mkdir $DIR/$tdir/striped_dir/dir_b
13681         mkdir $DIR/$tdir/striped_dir/dir_c
13682
13683         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13684                 error "set striped dir under striped dir error"
13685
13686         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13687                 error "set striped dir under striped dir error"
13688
13689         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13690                 error "set striped dir under striped dir error"
13691
13692         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
13693                 error "rename dir under striped dir fails"
13694
13695         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
13696                 error "rename dir under different stripes fails"
13697
13698         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13699                 error "rename file under striped dir should succeed"
13700
13701         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
13702                 error "rename dir under striped dir should succeed"
13703
13704         rm -rf $DIR/$tdir
13705 }
13706 run_test 300e "check rename under striped directory"
13707
13708 test_300f() {
13709         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13710                 skip "Need MDS version at least 2.7.55" && return
13711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13712         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13713         local stripe_count
13714         local file
13715
13716         rm -rf $DIR/$tdir
13717         mkdir -p $DIR/$tdir
13718
13719         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13720                 error "set striped dir error"
13721
13722         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13723                 error "set striped dir error"
13724
13725         touch $DIR/$tdir/striped_dir/a
13726         mkdir $DIR/$tdir/striped_dir/dir_a
13727         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13728                 error "create striped dir under striped dir fails"
13729
13730         touch $DIR/$tdir/striped_dir1/b
13731         mkdir $DIR/$tdir/striped_dir1/dir_b
13732         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13733                 error "create striped dir under striped dir fails"
13734
13735         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
13736                 error "rename dir under different striped dir should fail"
13737
13738         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
13739                 error "rename striped dir under diff striped dir should fail"
13740
13741         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13742                 error "rename file under diff striped dirs fails"
13743
13744         rm -rf $DIR/$tdir
13745 }
13746 run_test 300f "check rename cross striped directory"
13747
13748 test_300_check_default_striped_dir()
13749 {
13750         local dirname=$1
13751         local default_count=$2
13752         local default_index=$3
13753         local stripe_count
13754         local stripe_index
13755         local dir_stripe_index
13756         local dir
13757
13758         echo "checking $dirname $default_count $default_index"
13759         $LFS setdirstripe -D -c $default_count -i $default_index \
13760                                 -t all_char $DIR/$tdir/$dirname ||
13761                 error "set default stripe on striped dir error"
13762         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
13763         [ $stripe_count -eq $default_count ] ||
13764                 error "expect $default_count get $stripe_count for $dirname"
13765
13766         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
13767         [ $stripe_index -eq $default_index ] ||
13768                 error "expect $default_index get $stripe_index for $dirname"
13769
13770         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
13771                                                 error "create dirs failed"
13772
13773         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
13774         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
13775         for dir in $(find $DIR/$tdir/$dirname/*); do
13776                 stripe_count=$($LFS getdirstripe -c $dir)
13777                 [ $stripe_count -eq $default_count ] ||
13778                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
13779                 error "stripe count $default_count != $stripe_count for $dir"
13780
13781                 stripe_index=$($LFS getdirstripe -i $dir)
13782                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
13783                         error "$stripe_index != $default_index for $dir"
13784
13785                 #check default stripe
13786                 stripe_count=$($LFS getdirstripe -D -c $dir)
13787                 [ $stripe_count -eq $default_count ] ||
13788                 error "default count $default_count != $stripe_count for $dir"
13789
13790                 stripe_index=$($LFS getdirstripe -D -i $dir)
13791                 [ $stripe_index -eq $default_index ] ||
13792                 error "default index $default_index != $stripe_index for $dir"
13793         done
13794         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
13795 }
13796
13797 test_300g() {
13798         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13799                 skip "Need MDS version at least 2.7.55" && return
13800         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13801         local dir
13802         local stripe_count
13803         local stripe_index
13804
13805         mkdir $DIR/$tdir
13806         mkdir $DIR/$tdir/normal_dir
13807
13808         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
13809         test_300_check_default_striped_dir normal_dir 1 0
13810         test_300_check_default_striped_dir normal_dir 2 1
13811         test_300_check_default_striped_dir normal_dir 2 -1
13812
13813         #delete default stripe information
13814         echo "delete default stripeEA"
13815         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
13816                 error "set default stripe on striped dir error"
13817
13818         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
13819         for dir in $(find $DIR/$tdir/normal_dir/*); do
13820                 stripe_count=$($LFS getdirstripe -c $dir)
13821                 [ $stripe_count -eq 0 ] ||
13822                         error "expect 1 get $stripe_count for $dir"
13823                 stripe_index=$($LFS getdirstripe -i $dir)
13824                 [ $stripe_index -eq 0 ] ||
13825                         error "expect 0 get $stripe_index for $dir"
13826         done
13827 }
13828 run_test 300g "check default striped directory for normal directory"
13829
13830 test_300h() {
13831         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13832                 skip "Need MDS version at least 2.7.55" && return
13833         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13834         local dir
13835         local stripe_count
13836
13837         mkdir $DIR/$tdir
13838         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13839                                         $DIR/$tdir/striped_dir ||
13840                 error "set striped dir error"
13841
13842         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
13843         test_300_check_default_striped_dir striped_dir 1 0
13844         test_300_check_default_striped_dir striped_dir 2 1
13845         test_300_check_default_striped_dir striped_dir 2 -1
13846
13847         #delete default stripe information
13848         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
13849                 error "set default stripe on striped dir error"
13850
13851         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13852         for dir in $(find $DIR/$tdir/striped_dir/*); do
13853                 stripe_count=$($LFS getdirstripe -c $dir)
13854                 [ $stripe_count -eq 0 ] ||
13855                         error "expect 1 get $stripe_count for $dir"
13856         done
13857 }
13858 run_test 300h "check default striped directory for striped directory"
13859
13860 test_300i() {
13861         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13862                 skip "Need MDS version at least 2.7.55" && return
13863         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13864         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13865         local stripe_count
13866         local file
13867
13868         mkdir $DIR/$tdir
13869
13870         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13871                 error "set striped dir error"
13872
13873         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13874                 error "create files under striped dir failed"
13875
13876         # unfortunately, we need to umount to clear dir layout cache for now
13877         # once we fully implement dir layout, we can drop this
13878         umount_client $MOUNT || error "umount failed"
13879         mount_client $MOUNT || error "mount failed"
13880
13881         #set the stripe to be unknown hash type
13882         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13883         $LCTL set_param fail_loc=0x1901
13884         for ((i = 0; i < 10; i++)); do
13885                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13886                         error "stat f-$i failed"
13887                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13888         done
13889
13890         touch $DIR/$tdir/striped_dir/f0 &&
13891                 error "create under striped dir with unknown hash should fail"
13892
13893         $LCTL set_param fail_loc=0
13894
13895         umount_client $MOUNT || error "umount failed"
13896         mount_client $MOUNT || error "mount failed"
13897
13898         return 0
13899 }
13900 run_test 300i "client handle unknown hash type striped directory"
13901
13902 test_300j() {
13903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13904         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13905                 skip "Need MDS version at least 2.7.55" && return
13906         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13907         local stripe_count
13908         local file
13909
13910         mkdir $DIR/$tdir
13911
13912         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
13913         $LCTL set_param fail_loc=0x1702
13914         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13915                 error "set striped dir error"
13916
13917         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13918                 error "create files under striped dir failed"
13919
13920         $LCTL set_param fail_loc=0
13921
13922         rm -rf $DIR/$tdir || error "unlink striped dir fails"
13923
13924         return 0
13925 }
13926 run_test 300j "test large update record"
13927
13928 test_300k() {
13929         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13930                 skip "Need MDS version at least 2.7.55" && return
13931         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13932         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13933         local stripe_count
13934         local file
13935
13936         mkdir $DIR/$tdir
13937
13938         #define OBD_FAIL_LARGE_STRIPE   0x1703
13939         $LCTL set_param fail_loc=0x1703
13940         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
13941                 error "set striped dir error"
13942         $LCTL set_param fail_loc=0
13943
13944         $LFS getdirstripe $DIR/$tdir/striped_dir ||
13945                 error "getstripeddir fails"
13946         rm -rf $DIR/$tdir/striped_dir ||
13947                 error "unlink striped dir fails"
13948
13949         return 0
13950 }
13951 run_test 300k "test large striped directory"
13952
13953 test_300l() {
13954         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13955                 skip "Need MDS version at least 2.7.55" && return
13956         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13957         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13958         local stripe_index
13959
13960         test_mkdir -p $DIR/$tdir/striped_dir
13961         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
13962                         error "chown $RUNAS_ID failed"
13963         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
13964                 error "set default striped dir failed"
13965
13966         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
13967         $LCTL set_param fail_loc=0x80000158
13968         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
13969
13970         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
13971         [ $stripe_index -eq 1 ] ||
13972                 error "expect 1 get $stripe_index for $dir"
13973 }
13974 run_test 300l "non-root user to create dir under striped dir with stale layout"
13975
13976 test_300m() {
13977         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
13978                 skip "Need MDS version at least 2.7.55" && return
13979         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13980         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
13981
13982         mkdir -p $DIR/$tdir/striped_dir
13983         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
13984                 error "set default stripes dir error"
13985
13986         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
13987
13988         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
13989         [ $stripe_count -eq 0 ] ||
13990                         error "expect 0 get $stripe_count for a"
13991
13992         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
13993                 error "set default stripes dir error"
13994
13995         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
13996
13997         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
13998         [ $stripe_count -eq 0 ] ||
13999                         error "expect 0 get $stripe_count for b"
14000
14001         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
14002                 error "set default stripes dir error"
14003
14004         mkdir $DIR/$tdir/striped_dir/c &&
14005                 error "default stripe_index is invalid, mkdir c should fails"
14006
14007         rm -rf $DIR/$tdir || error "rmdir fails"
14008 }
14009 run_test 300m "setstriped directory on single MDT FS"
14010
14011 cleanup_300n() {
14012         local list=$(comma_list $(mdts_nodes))
14013
14014         trap 0
14015         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
14016 }
14017
14018 test_300n() {
14019         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14020                 skip "Need MDS version at least 2.7.55" && return
14021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14022         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14023         local stripe_index
14024         local list=$(comma_list $(mdts_nodes))
14025
14026         trap cleanup_300n RETURN EXIT
14027         mkdir -p $DIR/$tdir
14028         chmod 777 $DIR/$tdir
14029         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
14030                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
14031                 error "create striped dir succeeds with gid=0"
14032
14033         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
14034         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
14035                 error "create striped dir fails with gid=-1"
14036
14037         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
14038         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
14039                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
14040                 error "set default striped dir succeeds with gid=0"
14041
14042
14043         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
14044         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
14045                 error "set default striped dir fails with gid=-1"
14046
14047
14048         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
14049         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
14050                                         error "create test_dir fails"
14051         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
14052                                         error "create test_dir1 fails"
14053         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
14054                                         error "create test_dir2 fails"
14055         cleanup_300n
14056 }
14057 run_test 300n "non-root user to create dir under striped dir with default EA"
14058
14059 test_300o() {
14060         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
14061                 skip "Need MDS version at least 2.7.55" && return
14062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14063         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14064         local numfree1
14065         local numfree2
14066
14067         mkdir -p $DIR/$tdir
14068
14069         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
14070         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
14071         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
14072                 skip "not enough free inodes $numfree1 $numfree2"
14073                 return
14074         fi
14075
14076         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
14077         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
14078         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
14079                 skip "not enough free space $numfree1 $numfree2"
14080                 return
14081         fi
14082
14083         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
14084                 error "setdirstripe fails"
14085
14086         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
14087                 error "create dirs fails"
14088
14089         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
14090         ls $DIR/$tdir/striped_dir > /dev/null ||
14091                 error "ls striped dir fails"
14092         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
14093                 error "unlink big striped dir fails"
14094 }
14095 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
14096
14097 test_300p() {
14098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14099         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14100
14101         mkdir -p $DIR/$tdir
14102
14103         #define OBD_FAIL_OUT_ENOSPC     0x1704
14104         do_facet mds2 lctl set_param fail_loc=0x80001704
14105         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
14106                         error "create striped directory should fail"
14107
14108         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
14109         true
14110 }
14111 run_test 300p "create striped directory without space"
14112
14113 prepare_remote_file() {
14114         mkdir $DIR/$tdir/src_dir ||
14115                 error "create remote source failed"
14116
14117         cp /etc/hosts $DIR/$tdir/src_dir/a || error
14118         touch $DIR/$tdir/src_dir/a
14119
14120         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
14121                 error "create remote target dir failed"
14122
14123         touch $DIR/$tdir/tgt_dir/b
14124
14125         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
14126                 error "rename dir cross MDT failed!"
14127
14128         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
14129                 error "src_child still exists after rename"
14130
14131         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
14132                 error "missing file(a) after rename"
14133
14134         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
14135                 error "diff after rename"
14136 }
14137
14138 test_310a() {
14139         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
14140         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14141         local remote_file=$DIR/$tdir/tgt_dir/b
14142
14143         mkdir -p $DIR/$tdir
14144
14145         prepare_remote_file || error "prepare remote file failed"
14146
14147         #open-unlink file
14148         $OPENUNLINK $remote_file $remote_file || error
14149         $CHECKSTAT -a $remote_file || error
14150 }
14151 run_test 310a "open unlink remote file"
14152
14153 test_310b() {
14154         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
14155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14156         local remote_file=$DIR/$tdir/tgt_dir/b
14157
14158         mkdir -p $DIR/$tdir
14159
14160         prepare_remote_file || error "prepare remote file failed"
14161
14162         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14163         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
14164         $CHECKSTAT -t file $remote_file || error "check file failed"
14165 }
14166 run_test 310b "unlink remote file with multiple links while open"
14167
14168 test_310c() {
14169         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
14170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14171         local remote_file=$DIR/$tdir/tgt_dir/b
14172
14173         mkdir -p $DIR/$tdir
14174
14175         prepare_remote_file || error "prepare remote file failed"
14176
14177         ln $remote_file $DIR/$tfile || error "link failed for remote file"
14178         multiop_bg_pause $remote_file O_uc ||
14179                         error "mulitop failed for remote file"
14180         MULTIPID=$!
14181         $MULTIOP $DIR/$tfile Ouc
14182         kill -USR1 $MULTIPID
14183         wait $MULTIPID
14184 }
14185 run_test 310c "open-unlink remote file with multiple links"
14186
14187 test_400a() { # LU-1606, was conf-sanity test_74
14188         local extra_flags=''
14189         local out=$TMP/$tfile
14190         local prefix=/usr/include/lustre
14191         local prog
14192
14193         if ! which $CC > /dev/null 2>&1; then
14194                 skip_env "$CC is not installed"
14195                 return 0
14196         fi
14197
14198         if ! [[ -d $prefix ]]; then
14199                 # Assume we're running in tree and fixup the include path.
14200                 extra_flags+=" -I$LUSTRE/../libcfs/include"
14201                 extra_flags+=" -I$LUSTRE/include"
14202                 extra_flags+=" -L$LUSTRE/utils"
14203         fi
14204
14205         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
14206                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
14207                         error "client api broken"
14208         done
14209 }
14210 run_test 400a "Lustre client api program can compile and link"
14211
14212 test_400b() { # LU-1606, LU-5011
14213         local header
14214         local out=$TMP/$tfile
14215         local prefix=/usr/include/lustre
14216
14217         # We use a hard coded prefix so that this test will not fail
14218         # when run in tree. There are headers in lustre/include/lustre/
14219         # that are not packaged (like lustre_idl.h) and have more
14220         # complicated include dependencies (like config.h and lnet/types.h).
14221         # Since this test about correct packaging we just skip them when
14222         # they don't exist (see below) rather than try to fixup cppflags.
14223
14224         if ! which $CC > /dev/null 2>&1; then
14225                 skip_env "$CC is not installed"
14226                 return 0
14227         fi
14228
14229         for header in $prefix/*.h; do
14230                 if ! [[ -f "$header" ]]; then
14231                         continue
14232                 fi
14233
14234                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
14235                         continue # liblustreapi.h is deprecated.
14236                 fi
14237
14238                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
14239                         error "cannot compile '$header'"
14240         done
14241 }
14242 run_test 400b "packaged headers can be compiled"
14243
14244 test_401() { #LU-7437
14245         local params
14246         local procs
14247
14248         #count the number of parameters by "list_param -R"
14249         params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
14250         #count the number of parameters by listing proc files
14251         ls -lRL /proc/{fs,sys}/{lnet,lustre} 2>/dev/null |
14252                 grep -v "^t" | grep -v "^d" > $TMP/$tfile
14253         #Since there is no /proc/fs/lnet, we need to remove other
14254         #3 directories, /proc/{fs,sys}/lustre and /proc/sys/lnet.
14255         procs=$(($(sed /^$/d $TMP/$tfile | wc -l)-3))
14256
14257         [ $params -eq $procs ] ||
14258                 error "found $params parameters vs. $procs proc files"
14259
14260         rm -f $TMP/$tfile
14261 }
14262 run_test 401 "Verify if 'lctl list_param -R' can list parameters recursively"
14263
14264 #
14265 # tests that do cleanup/setup should be run at the end
14266 #
14267
14268 test_900() {
14269         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14270         local ls
14271         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
14272         $LCTL set_param fail_loc=0x903
14273
14274         cancel_lru_locks MGC
14275
14276         FAIL_ON_ERROR=true cleanup
14277         FAIL_ON_ERROR=true setup
14278 }
14279 run_test 900 "umount should not race with any mgc requeue thread"
14280
14281 complete $SECONDS
14282 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
14283 check_and_cleanup_lustre
14284 if [ "$I_MOUNTED" != "yes" ]; then
14285         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
14286 fi
14287 exit_status