Whamcloud - gitweb
LU-2775 osp: enable fid-on-OST only for DNE.
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 4; 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="                27u   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
20 SRCDIR=$(cd $(dirname $0); echo $PWD)
21 export PATH=$PATH:/sbin
22
23 TMP=${TMP:-/tmp}
24
25 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
26 CREATETEST=${CREATETEST:-createtest}
27 LFS=${LFS:-lfs}
28 LFIND=${LFIND:-"$LFS find"}
29 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
30 LCTL=${LCTL:-lctl}
31 MCREATE=${MCREATE:-mcreate}
32 OPENFILE=${OPENFILE:-openfile}
33 OPENUNLINK=${OPENUNLINK:-openunlink}
34 export MULTIOP=${MULTIOP:-multiop}
35 READS=${READS:-"reads"}
36 MUNLINK=${MUNLINK:-munlink}
37 SOCKETSERVER=${SOCKETSERVER:-socketserver}
38 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
39 MEMHOG=${MEMHOG:-memhog}
40 DIRECTIO=${DIRECTIO:-directio}
41 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
42 UMOUNT=${UMOUNT:-"umount -d"}
43 STRIPES_PER_OBJ=-1
44 CHECK_GRANT=${CHECK_GRANT:-"yes"}
45 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
46 export PARALLEL=${PARALLEL:-"no"}
47
48 export NAME=${NAME:-local}
49
50 SAVE_PWD=$PWD
51
52 CLEANUP=${CLEANUP:-:}
53 SETUP=${SETUP:-:}
54 TRACE=${TRACE:-""}
55 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
56 . $LUSTRE/tests/test-framework.sh
57 init_test_env $@
58 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
59 init_logging
60
61 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 60c 63 64b 68 71 73 77f 78 101a 103 115 120g 124b"
62
63 FAIL_ON_ERROR=false
64
65 cleanup() {
66         echo -n "cln.."
67         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
68         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
69 }
70 setup() {
71         echo -n "mnt.."
72         load_modules
73         setupall || exit 10
74         echo "done"
75 }
76
77 check_kernel_version() {
78         WANT_VER=$1
79         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
80         case $GOT_VER in
81         patchless|patchless_client) return 0;;
82         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
83         esac
84         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
85         return 1
86 }
87
88 if [ "$ONLY" == "cleanup" ]; then
89        sh llmountcleanup.sh
90        exit 0
91 fi
92
93 check_and_setup_lustre
94
95 DIR=${DIR:-$MOUNT}
96 assert_DIR
97
98 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
99     awk '{gsub(/_UUID/,""); print $1}' | head -1)
100 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
101 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
102 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
103 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
104 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
105 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
106
107 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
108 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
109 rm -rf $DIR/[Rdfs][0-9]*
110
111 # $RUNAS_ID may get set incorrectly somewhere else
112 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
113
114 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
115
116 build_test_filter
117
118 if [ "${ONLY}" = "MOUNT" ] ; then
119         echo "Lustre is up, please go on"
120         exit
121 fi
122
123 echo "preparing for tests involving mounts"
124 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
125 touch $EXT2_DEV
126 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
127 echo # add a newline after mke2fs.
128
129 umask 077
130
131 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
132 lctl set_param debug=-1 2> /dev/null || true
133 test_0() {
134         touch $DIR/$tfile
135         $CHECKSTAT -t file $DIR/$tfile || error
136         rm $DIR/$tfile
137         $CHECKSTAT -a $DIR/$tfile || error
138 }
139 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
140
141 test_0b() {
142         chmod 0755 $DIR || error
143         $CHECKSTAT -p 0755 $DIR || error
144 }
145 run_test 0b "chmod 0755 $DIR ============================="
146
147 test_0c() {
148     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
149     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
150 }
151 run_test 0c "check import proc ============================="
152
153 test_1a() {
154         test_mkdir -p $DIR/$tdir
155         test_mkdir -p $DIR/$tdir/d2
156         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
157         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
158 }
159 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
160
161 test_1b() {
162         rmdir $DIR/$tdir/d2
163         rmdir $DIR/$tdir
164         $CHECKSTAT -a $DIR/$tdir || error
165 }
166 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
167
168 test_2a() {
169         test_mkdir $DIR/$tdir
170         touch $DIR/$tdir/$tfile
171         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
172 }
173 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
174
175 test_2b() {
176         rm -r $DIR/$tdir
177         $CHECKSTAT -a $DIR/$tdir || error
178 }
179 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
180
181 test_3a() {
182         test_mkdir -p $DIR/$tdir
183         $CHECKSTAT -t dir $DIR/$tdir || error
184 }
185 run_test 3a "mkdir .../d3 ======================================"
186
187 test_3b() {
188         if [ ! -d $DIR/$tdir ]; then
189                 mkdir $DIR/$tdir
190         fi
191         touch $DIR/$tdir/$tfile
192         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
193 }
194 run_test 3b "touch .../d3/f ===================================="
195
196 test_3c() {
197         rm -r $DIR/$tdir
198         $CHECKSTAT -a $DIR/$tdir || error
199 }
200 run_test 3c "rm -r .../d3 ======================================"
201
202 test_4a() {
203         test_mkdir -p $DIR/$tdir
204         $CHECKSTAT -t dir $DIR/$tdir || error
205 }
206 run_test 4a "mkdir .../d4 ======================================"
207
208 test_4b() {
209         if [ ! -d $DIR/$tdir ]; then
210                 test_mkdir $DIR/$tdir
211         fi
212         test_mkdir $DIR/$tdir/d2
213         mkdir $DIR/$tdir/d2
214         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
215 }
216 run_test 4b "mkdir .../d4/d2 ==================================="
217
218 test_5() {
219         test_mkdir $DIR/$tdir
220         test_mkdir $DIR/$tdir/d2
221         chmod 0707 $DIR/$tdir/d2
222         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error
223 }
224 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
225
226 test_6a() {
227         touch $DIR/$tfile
228         chmod 0666 $DIR/$tfile || error
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
230 }
231 run_test 6a "touch .../f6a; chmod .../f6a ======================"
232
233 test_6b() {
234         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
235         if [ ! -f $DIR/$tfile ]; then
236                 touch $DIR/$tfile
237                 chmod 0666 $DIR/$tfile
238         fi
239         $RUNAS chmod 0444 $DIR/$tfile && error
240         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
241 }
242 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
243
244 test_6c() {
245         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
246         touch $DIR/$tfile
247         chown $RUNAS_ID $DIR/$tfile || error
248         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
249 }
250 run_test 6c "touch .../f6c; chown .../f6c ======================"
251
252 test_6d() {
253         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
254         if [ ! -f $DIR/$tfile ]; then
255                 touch $DIR/$tfile
256                 chown $RUNAS_ID $DIR/$tfile
257         fi
258         $RUNAS chown $UID $DIR/$tfile && error
259         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
260 }
261 run_test 6d "$RUNAS chown .../f6c (should return error) =="
262
263 test_6e() {
264         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
265         touch $DIR/$tfile
266         chgrp $RUNAS_ID $DIR/$tfile || error
267         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
268 }
269 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
270
271 test_6f() {
272         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
273         if [ ! -f $DIR/$tfile ]; then
274                 touch $DIR/$tfile
275                 chgrp $RUNAS_ID $DIR/$tfile
276         fi
277         $RUNAS chgrp $UID $DIR/$tfile && error
278         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
279 }
280 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
281
282 test_6g() {
283         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
284         test_mkdir $DIR/$tdir || error
285         chmod 777 $DIR/$tdir || error
286         $RUNAS mkdir $DIR/$tdir/d || error
287         chmod g+s $DIR/$tdir/d || error
288         test_mkdir $DIR/$tdir/d/subdir
289         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir || error
290 }
291 run_test 6g "Is new dir in sgid dir inheriting group?"
292
293 test_6h() { # bug 7331
294         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
295         touch $DIR/$tfile || error "touch failed"
296         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
297         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
298                 error "chown worked"
299         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile || error
300 }
301 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
302
303 test_7a() {
304         test_mkdir $DIR/$tdir
305         $MCREATE $DIR/$tdir/$tfile
306         chmod 0666 $DIR/$tdir/$tfile
307         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
308 }
309 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
310
311 test_7b() {
312         if [ ! -d $DIR/$tdir ]; then
313                 mkdir $DIR/$tdir
314         fi
315         $MCREATE $DIR/$tdir/$tfile
316         echo -n foo > $DIR/$tdir/$tfile
317         [ "`cat $DIR/$tdir/$tfile`" = "foo" ] || error
318         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error
319 }
320 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
321
322 test_8() {
323         test_mkdir $DIR/$tdir
324         touch $DIR/$tdir/$tfile
325         chmod 0666 $DIR/$tdir/$tfile
326         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
327 }
328 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
329
330 test_9() {
331         test_mkdir $DIR/$tdir
332         test_mkdir $DIR/$tdir/d2
333         test_mkdir $DIR/$tdir/d2/d3
334         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error
335 }
336 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
337
338 test_10() {
339         test_mkdir $DIR/$tdir
340         test_mkdir $DIR/$tdir/d2
341         touch $DIR/$tdir/d2/$tfile
342         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile || error
343 }
344 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
345
346 test_11() {
347         test_mkdir $DIR/$tdir
348         test_mkdir $DIR/$tdir/d2
349         chmod 0666 $DIR/$tdir/d2
350         chmod 0705 $DIR/$tdir/d2
351         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 || error
352 }
353 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
354
355 test_12() {
356         test_mkdir $DIR/$tdir
357         touch $DIR/$tdir/$tfile
358         chmod 0666 $DIR/$tdir/$tfile
359         chmod 0654 $DIR/$tdir/$tfile
360         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile || error
361 }
362 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
363
364 test_13() {
365         test_mkdir $DIR/$tdir
366         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
367         >  $DIR/$tdir/$tfile
368         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile || error
369 }
370 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
371
372 test_14() {
373         test_mkdir $DIR/$tdir
374         touch $DIR/$tdir/$tfile
375         rm $DIR/$tdir/$tfile
376         $CHECKSTAT -a $DIR/$tdir/$tfile || error
377 }
378 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
379
380 test_15() {
381         test_mkdir $DIR/$tdir
382         touch $DIR/$tdir/$tfile
383         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
384         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 || error
385 }
386 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
387
388 test_16() {
389         test_mkdir $DIR/$tdir
390         touch $DIR/$tdir/$tfile
391         rm -rf $DIR/$tdir/$tfile
392         $CHECKSTAT -a $DIR/$tdir/$tfile || error
393 }
394 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
395
396 test_17a() {
397         test_mkdir -p $DIR/$tdir
398         touch $DIR/$tdir/$tfile
399         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
400         ls -l $DIR/$tdir
401         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist || error
402         $CHECKSTAT -f -t f $DIR/$tdir/l-exist || error
403         rm -f $DIR/$tdir/l-exist
404         $CHECKSTAT -a $DIR/$tdir/l-exist || error
405 }
406 run_test 17a "symlinks: create, remove (real) =================="
407
408 test_17b() {
409         test_mkdir -p $DIR/$tdir
410         ln -s no-such-file $DIR/$tdir/l-dangle
411         ls -l $DIR/$tdir
412         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle || error
413         $CHECKSTAT -fa $DIR/$tdir/l-dangle || error
414         rm -f $DIR/$tdir/l-dangle
415         $CHECKSTAT -a $DIR/$tdir/l-dangle || error
416 }
417 run_test 17b "symlinks: create, remove (dangling) =============="
418
419 test_17c() { # bug 3440 - don't save failed open RPC for replay
420         test_mkdir -p $DIR/$tdir
421         ln -s foo $DIR/$tdir/$tfile
422         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
423 }
424 run_test 17c "symlinks: open dangling (should return error) ===="
425
426 test_17d() {
427         test_mkdir -p $DIR/$tdir
428         ln -s foo $DIR/$tdir/$tfile
429         touch $DIR/$tdir/$tfile || error "creating to new symlink"
430 }
431 run_test 17d "symlinks: create dangling ========================"
432
433 test_17e() {
434         test_mkdir -p $DIR/$tdir
435         local foo=$DIR/$tdir/$tfile
436         ln -s $foo $foo || error "create symlink failed"
437         ls -l $foo || error "ls -l failed"
438         ls $foo && error "ls not failed" || true
439 }
440 run_test 17e "symlinks: create recursive symlink (should return error) ===="
441
442 test_17f() {
443         test_mkdir -p $DIR/d17f
444         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
445         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
446         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
447         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
448         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
449         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/d17f/666
450         ls -l  $DIR/d17f
451 }
452 run_test 17f "symlinks: long and very long symlink name ========================"
453
454 # str_repeat(S, N) generate a string that is string S repeated N times
455 str_repeat() {
456         local s=$1
457         local n=$2
458         local ret=''
459         while [ $((n -= 1)) -ge 0 ]; do
460                 ret=$ret$s
461         done
462         echo $ret
463 }
464
465 # Long symlinks and LU-2241
466 test_17g() {
467         test_mkdir -p $DIR/$tdir
468         local TESTS="59 60 61 4094 4095"
469
470         for i in $TESTS; do
471                 local SYMNAME=$(str_repeat 'x' $i)
472                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
473                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
474         done
475 }
476 run_test 17g "symlinks: really long symlink name and inode boundaries"
477
478 test_17h() { #bug 17378
479         remote_mds_nodsh && skip "remote MDS with nodsh" && return
480         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
481         local mdt_idx
482         test_mkdir -p $DIR/$tdir
483         if [ $MDSCOUNT -gt 1 ]; then
484                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
485         else
486                 mdt_idx=0
487         fi
488         $SETSTRIPE -c -1 $DIR/$tdir
489 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
490         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
491         touch $DIR/$tdir/$tfile || true
492 }
493 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
494
495 test_17i() { #bug 20018
496         remote_mds_nodsh && skip "remote MDS with nodsh" && return
497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
498         test_mkdir -p $DIR/$tdir
499         local foo=$DIR/$tdir/$tfile
500         local mdt_idx
501         if [ $MDSCOUNT -gt 1 ]; then
502                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
503         else
504                 mdt_idx=0
505         fi
506         ln -s $foo $foo || error "create symlink failed"
507 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
508         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
509         ls -l $foo && error "error not detected"
510         return 0
511 }
512 run_test 17i "don't panic on short symlink"
513
514 test_17k() { #bug 22301
515         rsync --help | grep -q xattr ||
516                 skip_env "$(rsync --version| head -1) does not support xattrs"
517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
518         test_mkdir -p $DIR/$tdir
519         test_mkdir -p $DIR/$tdir.new
520         touch $DIR/$tdir/$tfile
521         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
522         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
523                 error "rsync failed with xattrs enabled"
524 }
525 run_test 17k "symlinks: rsync with xattrs enabled ========================="
526
527 test_17l() { # LU-279
528         mkdir -p $DIR/$tdir
529         touch $DIR/$tdir/$tfile
530         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
531         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
532                 # -h to not follow symlinks. -m '' to list all the xattrs.
533                 # grep to remove first line: '# file: $path'.
534                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
535                 do
536                         lgetxattr_size_check $path $xattr ||
537                                 error "lgetxattr_size_check $path $xattr failed"
538                 done
539         done
540 }
541 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
542
543 # LU-1540
544 test_17m() {
545         local short_sym="0123456789"
546         local WDIR=$DIR/${tdir}m
547         local mds_index
548         local devname
549         local cmd
550         local i
551         local rc=0
552
553         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
554         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
555                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
556
557         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
558                 skip "only for ldiskfs MDT" && return 0
559
560         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
561
562         mkdir -p $WDIR
563         long_sym=$short_sym
564         # create a long symlink file
565         for ((i = 0; i < 4; ++i)); do
566                 long_sym=${long_sym}${long_sym}
567         done
568
569         echo "create 512 short and long symlink files under $WDIR"
570         for ((i = 0; i < 256; ++i)); do
571                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
572                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
573         done
574
575         echo "erase them"
576         rm -f $WDIR/*
577         sync
578         wait_delete_completed
579
580         echo "recreate the 512 symlink files with a shorter string"
581         for ((i = 0; i < 512; ++i)); do
582                 # rewrite the symlink file with a shorter string
583                 ln -sf ${long_sym} $WDIR/long-$i
584                 ln -sf ${short_sym} $WDIR/short-$i
585         done
586
587         mds_index=$($LFS getstripe -M $WDIR)
588         mds_index=$((mds_index+1))
589         devname=$(mdsdevname $mds_index)
590         cmd="$E2FSCK -fnvd $devname"
591
592         echo "stop and checking mds${mds_index}: $cmd"
593         # e2fsck should not return error
594         stop mds${mds_index} -f
595         do_facet mds${mds_index} $cmd || rc=$?
596
597         start mds${mds_index} $devname $MDS_MOUNT_OPTS
598         df $MOUNT > /dev/null 2>&1
599         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
600                 "short/long symlink MDT: rc=$rc"
601         return $rc
602 }
603 run_test 17m "run e2fsck against MDT which contains short/long symlink"
604
605 check_fs_consistency_17n() {
606         local mdt_index
607         local devname
608         local cmd
609         local rc=0
610
611         for mdt_index in $(seq 1 $MDSCOUNT); do
612                 devname=$(mdsdevname $mdt_index)
613                 cmd="$E2FSCK -fnvd $devname"
614
615                 echo "stop and checking mds${mdt_index}: $cmd"
616                 # e2fsck should not return error
617                 stop mds${mdt_index}
618                 do_facet mds${mdt_index} $cmd || rc=$?
619
620                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
621                 df $MOUNT > /dev/null 2>&1
622                 [ $rc -ne 0 ] && break
623         done
624         return $rc
625 }
626
627 test_17n() {
628         local i
629
630         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
631         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
632                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
633
634         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
635                 skip "only for ldiskfs MDT" && return 0
636
637         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
638
639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
640
641         mkdir -p $DIR/$tdir
642         for ((i=0; i<10; i++)); do
643                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
644                         error "create remote dir error $i"
645                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
646                         error "create files under remote dir failed $i"
647         done
648
649         check_fs_consistency_17n || error "e2fsck report error"
650
651         for ((i=0;i<10;i++)); do
652                 rm -rf $DIR/$tdir/remote_dir_${i} ||
653                         error "destroy remote dir error $i"
654         done
655
656         check_fs_consistency_17n || error "e2fsck report error"
657 }
658 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
659
660 test_18() {
661         touch $DIR/f
662         ls $DIR || error
663 }
664 run_test 18 "touch .../f ; ls ... =============================="
665
666 test_19a() {
667         touch $DIR/f19
668         ls -l $DIR
669         rm $DIR/f19
670         $CHECKSTAT -a $DIR/f19 || error
671 }
672 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
673
674 test_19b() {
675         ls -l $DIR/f19 && error || true
676 }
677 run_test 19b "ls -l .../f19 (should return error) =============="
678
679 test_19c() {
680         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
681         $RUNAS touch $DIR/f19 && error || true
682 }
683 run_test 19c "$RUNAS touch .../f19 (should return error) =="
684
685 test_19d() {
686         cat $DIR/f19 && error || true
687 }
688 run_test 19d "cat .../f19 (should return error) =============="
689
690 test_20() {
691         touch $DIR/f
692         rm $DIR/f
693         log "1 done"
694         touch $DIR/f
695         rm $DIR/f
696         log "2 done"
697         touch $DIR/f
698         rm $DIR/f
699         log "3 done"
700         $CHECKSTAT -a $DIR/f || error
701 }
702 run_test 20 "touch .../f ; ls -l ... ==========================="
703
704 test_21() {
705         test_mkdir $DIR/d21
706         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
707         ln -s dangle $DIR/d21/link
708         echo foo >> $DIR/d21/link
709         cat $DIR/d21/dangle
710         $CHECKSTAT -t link $DIR/d21/link || error
711         $CHECKSTAT -f -t file $DIR/d21/link || error
712 }
713 run_test 21 "write to dangling link ============================"
714
715 test_22() {
716         WDIR=$DIR/$tdir
717         test_mkdir -p $DIR/$tdir
718         chown $RUNAS_ID:$RUNAS_GID $WDIR
719         (cd $WDIR || error "cd $WDIR failed";
720         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
721         $RUNAS tar xf -)
722         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
723         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
724         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
725 }
726 run_test 22 "unpack tar archive as non-root user ==============="
727
728 # was test_23
729 test_23a() {
730         test_mkdir -p $DIR/$tdir
731         local file=$DIR/$tdir/$tfile
732
733         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
734         openfile -f O_CREAT:O_EXCL $file &&
735                 error "$file recreate succeeded" || true
736 }
737 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
738
739 test_23b() { # bug 18988
740         test_mkdir -p $DIR/$tdir
741         local file=$DIR/$tdir/$tfile
742
743         rm -f $file
744         echo foo > $file || error "write filed"
745         echo bar >> $file || error "append filed"
746         $CHECKSTAT -s 8 $file || error "wrong size"
747         rm $file
748 }
749 run_test 23b "O_APPEND check =========================="
750
751 test_24a() {
752         echo '== rename sanity =============================================='
753         echo '-- same directory rename'
754         test_mkdir $DIR/R1
755         touch $DIR/R1/f
756         mv $DIR/R1/f $DIR/R1/g
757         $CHECKSTAT -t file $DIR/R1/g || error
758 }
759 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
760
761 test_24b() {
762         test_mkdir $DIR/R2
763         touch $DIR/R2/{f,g}
764         mv $DIR/R2/f $DIR/R2/g
765         $CHECKSTAT -a $DIR/R2/f || error
766         $CHECKSTAT -t file $DIR/R2/g || error
767 }
768 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
769
770 test_24c() {
771         test_mkdir $DIR/R3
772         test_mkdir $DIR/R3/f
773         mv $DIR/R3/f $DIR/R3/g
774         $CHECKSTAT -a $DIR/R3/f || error
775         $CHECKSTAT -t dir $DIR/R3/g || error
776 }
777 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
778
779 test_24d() {
780         test_mkdir $DIR/R4
781         test_mkdir $DIR/R4/f
782         test_mkdir $DIR/R4/g
783         mrename $DIR/R4/f $DIR/R4/g
784         $CHECKSTAT -a $DIR/R4/f || error
785         $CHECKSTAT -t dir $DIR/R4/g || error
786 }
787 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
788
789 test_24e() {
790         echo '-- cross directory renames --'
791         test_mkdir $DIR/R5a
792         test_mkdir $DIR/R5b
793         touch $DIR/R5a/f
794         mv $DIR/R5a/f $DIR/R5b/g
795         $CHECKSTAT -a $DIR/R5a/f || error
796         $CHECKSTAT -t file $DIR/R5b/g || error
797 }
798 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
799
800 test_24f() {
801         test_mkdir $DIR/R6a
802         test_mkdir $DIR/R6b
803         touch $DIR/R6a/f $DIR/R6b/g
804         mv $DIR/R6a/f $DIR/R6b/g
805         $CHECKSTAT -a $DIR/R6a/f || error
806         $CHECKSTAT -t file $DIR/R6b/g || error
807 }
808 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
809
810 test_24g() {
811         test_mkdir $DIR/R7a
812         test_mkdir $DIR/R7b
813         test_mkdir $DIR/R7a/d
814         mv $DIR/R7a/d $DIR/R7b/e
815         $CHECKSTAT -a $DIR/R7a/d || error
816         $CHECKSTAT -t dir $DIR/R7b/e || error
817 }
818 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
819
820 test_24h() {
821         test_mkdir $DIR/R8a
822         test_mkdir $DIR/R8b
823         test_mkdir $DIR/R8a/d
824         test_mkdir $DIR/R8b/e
825         mrename $DIR/R8a/d $DIR/R8b/e
826         $CHECKSTAT -a $DIR/R8a/d || error
827         $CHECKSTAT -t dir $DIR/R8b/e || error
828 }
829 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
830
831 test_24i() {
832         echo "-- rename error cases"
833         test_mkdir $DIR/R9
834         test_mkdir $DIR/R9/a
835         touch $DIR/R9/f
836         mrename $DIR/R9/f $DIR/R9/a
837         $CHECKSTAT -t file $DIR/R9/f || error
838         $CHECKSTAT -t dir  $DIR/R9/a || error
839         $CHECKSTAT -a $DIR/R9/a/f || error
840 }
841 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
842
843 test_24j() {
844         test_mkdir $DIR/R10
845         mrename $DIR/R10/f $DIR/R10/g
846         $CHECKSTAT -t dir $DIR/R10 || error
847         $CHECKSTAT -a $DIR/R10/f || error
848         $CHECKSTAT -a $DIR/R10/g || error
849 }
850 run_test 24j "source does not exist ============================"
851
852 test_24k() {
853         test_mkdir $DIR/R11a
854         test_mkdir $DIR/R11a/d
855         touch $DIR/R11a/f
856         mv $DIR/R11a/f $DIR/R11a/d
857         $CHECKSTAT -a $DIR/R11a/f || error
858         $CHECKSTAT -t file $DIR/R11a/d/f || error
859 }
860 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
861
862 # bug 2429 - rename foo foo foo creates invalid file
863 test_24l() {
864         f="$DIR/f24l"
865         $MULTIOP $f OcNs || error
866 }
867 run_test 24l "Renaming a file to itself ========================"
868
869 test_24m() {
870         f="$DIR/f24m"
871         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
872         # on ext3 this does not remove either the source or target files
873         # though the "expected" operation would be to remove the source
874         $CHECKSTAT -t file ${f} || error "${f} missing"
875         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
876 }
877 run_test 24m "Renaming a file to a hard link to itself ========="
878
879 test_24n() {
880     f="$DIR/f24n"
881     # this stats the old file after it was renamed, so it should fail
882     touch ${f}
883     $CHECKSTAT ${f}
884     mv ${f} ${f}.rename
885     $CHECKSTAT ${f}.rename
886     $CHECKSTAT -a ${f}
887 }
888 run_test 24n "Statting the old file after renaming (Posix rename 2)"
889
890 test_24o() {
891         check_kernel_version 37 || return 0
892         test_mkdir -p $DIR/d24o
893         rename_many -s random -v -n 10 $DIR/d24o
894 }
895 run_test 24o "rename of files during htree split ==============="
896
897 test_24p() {
898         test_mkdir $DIR/R12a
899         test_mkdir $DIR/R12b
900         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
901         mrename $DIR/R12a $DIR/R12b
902         $CHECKSTAT -a $DIR/R12a || error
903         $CHECKSTAT -t dir $DIR/R12b || error
904         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
905         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
906 }
907 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
908
909 cleanup_multiop_pause() {
910         trap 0
911         kill -USR1 $MULTIPID
912 }
913
914 test_24q() {
915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
916         test_mkdir $DIR/R13a
917         test_mkdir $DIR/R13b
918         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
919         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
920         MULTIPID=$!
921
922         trap cleanup_multiop_pause EXIT
923         mrename $DIR/R13a $DIR/R13b
924         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
925         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
926         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
927         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
928         cleanup_multiop_pause
929         wait $MULTIPID || error "multiop close failed"
930 }
931 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
932
933 test_24r() { #bug 3789
934         test_mkdir $DIR/R14a
935         test_mkdir $DIR/R14a/b
936         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
937         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
938         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
939 }
940 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
941
942 test_24s() {
943         test_mkdir $DIR/R15a
944         test_mkdir $DIR/R15a/b
945         test_mkdir $DIR/R15a/b/c
946         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
947         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
948         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
949 }
950 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
951 test_24t() {
952         test_mkdir $DIR/R16a
953         test_mkdir $DIR/R16a/b
954         test_mkdir $DIR/R16a/b/c
955         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
956         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
957         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
958 }
959 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
960
961 test_24u() { # bug12192
962         rm -rf $DIR/$tfile
963         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
964         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
965 }
966 run_test 24u "create stripe file"
967
968 page_size() {
969         getconf PAGE_SIZE
970 }
971
972 simple_cleanup_common() {
973         trap 0
974         rm -rf $DIR/$tdir
975         wait_delete_completed
976 }
977
978 max_pages_per_rpc() {
979         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
980 }
981
982 test_24v() {
983         local NRFILES=100000
984         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
985         [ $FREE_INODES -lt $NRFILES ] && \
986                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
987                 return
988
989         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
990         trap simple_cleanup_common EXIT
991
992         mkdir -p $DIR/$tdir
993         createmany -m $DIR/$tdir/$tfile $NRFILES
994
995         cancel_lru_locks mdc
996         lctl set_param mdc.*.stats clear
997
998         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
999
1000         # LU-5 large readdir
1001         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1002         #               8 bytes for name(filename is mostly 5 in this test) +
1003         #               8 bytes for luda_type
1004         # take into account of overhead in lu_dirpage header and end mark in
1005         # each page, plus one in RPC_NUM calculation.
1006         DIRENT_SIZE=48
1007         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1008         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1009         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
1010                                 awk '/^mds_readpage/ {print $2}')
1011         [ $mds_readpage -gt $RPC_NUM ] && \
1012                 error "large readdir doesn't take effect"
1013
1014         simple_cleanup_common
1015 }
1016 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1017
1018 test_24w() { # bug21506
1019         SZ1=234852
1020         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1021         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1022         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1023         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1024         [ "$SZ1" = "$SZ2" ] || \
1025                 error "Error reading at the end of the file $tfile"
1026 }
1027 run_test 24w "Reading a file larger than 4Gb"
1028
1029 test_24x() {
1030         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1031         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1032         local MDTIDX=1
1033         local remote_dir=$DIR/$tdir/remote_dir
1034
1035         mkdir -p $DIR/$tdir
1036         $LFS mkdir -i $MDTIDX $remote_dir ||
1037                 error "create remote directory failed"
1038
1039         mkdir -p $DIR/$tdir/src_dir
1040         touch $DIR/$tdir/src_file
1041         mkdir -p $remote_dir/tgt_dir
1042         touch $remote_dir/tgt_file
1043
1044         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1045                 error "rename dir cross MDT works!"
1046
1047         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1048                 error "rename file cross MDT works!"
1049
1050         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1051                 error "ln file cross MDT should not work!"
1052
1053         rm -rf $DIR/$tdir || error "Can not delete directories"
1054 }
1055 run_test 24x "cross rename/link should be failed"
1056
1057 test_24y() {
1058         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1059         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1060         local MDTIDX=1
1061         local remote_dir=$DIR/$tdir/remote_dir
1062
1063         mkdir -p $DIR/$tdir
1064         $LFS mkdir -i $MDTIDX $remote_dir ||
1065                    error "create remote directory failed"
1066
1067         mkdir -p $remote_dir/src_dir
1068         touch $remote_dir/src_file
1069         mkdir -p $remote_dir/tgt_dir
1070         touch $remote_dir/tgt_file
1071
1072         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1073                 error "rename subdir in the same remote dir failed!"
1074
1075         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1076                 error "rename files in the same remote dir failed!"
1077
1078         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1079                 error "link files in the same remote dir failed!"
1080
1081         rm -rf $DIR/$tdir || error "Can not delete directories"
1082 }
1083 run_test 24y "rename/link on the same dir should succeed"
1084
1085 test_24z() {
1086         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1088         local MDTIDX=1
1089         local remote_src=$DIR/$tdir/remote_dir
1090         local remote_tgt=$DIR/$tdir/remote_tgt
1091
1092         mkdir -p $DIR/$tdir
1093         $LFS mkdir -i $MDTIDX $remote_src ||
1094                    error "create remote directory failed"
1095
1096         $LFS mkdir -i $MDTIDX $remote_tgt ||
1097                    error "create remote directory failed"
1098
1099         mrename $remote_src $remote_tgt &&
1100                 error "rename remote dirs should not work!"
1101
1102         rm -rf $DIR/$tdir || error "Can not delete directories"
1103 }
1104 run_test 24z "rename one remote dir to another remote dir should fail"
1105
1106 test_25a() {
1107         echo '== symlink sanity ============================================='
1108
1109         test_mkdir $DIR/d25
1110         ln -s d25 $DIR/s25
1111         touch $DIR/s25/foo || error
1112 }
1113 run_test 25a "create file in symlinked directory ==============="
1114
1115 test_25b() {
1116         [ ! -d $DIR/d25 ] && test_25a
1117         $CHECKSTAT -t file $DIR/s25/foo || error
1118 }
1119 run_test 25b "lookup file in symlinked directory ==============="
1120
1121 test_26a() {
1122         test_mkdir $DIR/d26
1123         test_mkdir $DIR/d26/d26-2
1124         ln -s d26/d26-2 $DIR/s26
1125         touch $DIR/s26/foo || error
1126 }
1127 run_test 26a "multiple component symlink ======================="
1128
1129 test_26b() {
1130         test_mkdir -p $DIR/d26b/d26-2
1131         ln -s d26b/d26-2/foo $DIR/s26-2
1132         touch $DIR/s26-2 || error
1133 }
1134 run_test 26b "multiple component symlink at end of lookup ======"
1135
1136 test_26c() {
1137         test_mkdir $DIR/d26.2
1138         touch $DIR/d26.2/foo
1139         ln -s d26.2 $DIR/s26.2-1
1140         ln -s s26.2-1 $DIR/s26.2-2
1141         ln -s s26.2-2 $DIR/s26.2-3
1142         chmod 0666 $DIR/s26.2-3/foo
1143 }
1144 run_test 26c "chain of symlinks ================================"
1145
1146 # recursive symlinks (bug 439)
1147 test_26d() {
1148         ln -s d26-3/foo $DIR/d26-3
1149 }
1150 run_test 26d "create multiple component recursive symlink ======"
1151
1152 test_26e() {
1153         [ ! -h $DIR/d26-3 ] && test_26d
1154         rm $DIR/d26-3
1155 }
1156 run_test 26e "unlink multiple component recursive symlink ======"
1157
1158 # recursive symlinks (bug 7022)
1159 test_26f() {
1160         test_mkdir -p $DIR/$tdir
1161         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1162         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1163         test_mkdir -p lndir bar1      || error "mkdir lndir/bar1 failed"
1164         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1165         cd $tfile                || error "cd $tfile failed"
1166         ln -s .. dotdot          || error "ln dotdot failed"
1167         ln -s dotdot/lndir lndir || error "ln lndir failed"
1168         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1169         output=`ls $tfile/$tfile/lndir/bar1`
1170         [ "$output" = bar1 ] && error "unexpected output"
1171         rm -r $tfile             || error "rm $tfile failed"
1172         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1173 }
1174 run_test 26f "rm -r of a directory which has recursive symlink ="
1175
1176 test_27a() {
1177         echo '== stripe sanity =============================================='
1178         test_mkdir -p $DIR/d27 || error "mkdir failed"
1179         $GETSTRIPE $DIR/d27
1180         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1181         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1182         pass
1183         log "== test_27a: write to one stripe file ========================="
1184         cp /etc/hosts $DIR/d27/f0 || error
1185 }
1186 run_test 27a "one stripe file =================================="
1187
1188 test_27b() {
1189         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1190         test_mkdir -p $DIR/d27
1191         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1192         $GETSTRIPE -c $DIR/d27/f01
1193         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1194                 error "two-stripe file doesn't have two stripes"
1195 }
1196 run_test 27b "create two stripe file"
1197
1198 test_27c() {
1199         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1200
1201         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1202 }
1203 run_test 27c "write to two stripe file"
1204
1205 test_27d() {
1206         test_mkdir -p $DIR/d27
1207         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1208         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1209         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1210 }
1211 run_test 27d "create file with default settings ================"
1212
1213 test_27e() {
1214         test_mkdir -p $DIR/d27
1215         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1216         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1217         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1218 }
1219 run_test 27e "setstripe existing file (should return error) ======"
1220
1221 test_27f() {
1222         test_mkdir -p $DIR/d27
1223         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1224         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1225         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1226 }
1227 run_test 27f "setstripe with bad stripe size (should return error)"
1228
1229 test_27g() {
1230         test_mkdir -p $DIR/d27
1231         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1232         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1233                 error "$DIR/d27/fnone has object"
1234 }
1235 run_test 27g "$GETSTRIPE with no objects"
1236
1237 test_27i() {
1238         touch $DIR/d27/fsome || error "touch failed"
1239         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1240 }
1241 run_test 27i "$GETSTRIPE with some objects"
1242
1243 test_27j() {
1244         test_mkdir -p $DIR/d27
1245         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1246 }
1247 run_test 27j "setstripe with bad stripe offset (should return error)"
1248
1249 test_27k() { # bug 2844
1250         test_mkdir -p $DIR/d27
1251         FILE=$DIR/d27/f27k
1252         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1253         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1254         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1255         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1256         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1257         dd if=/dev/zero of=$FILE bs=4k count=1
1258         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1259         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1260 }
1261 run_test 27k "limit i_blksize for broken user apps ============="
1262
1263 test_27l() {
1264         test_mkdir -p $DIR/d27
1265         mcreate $DIR/f27l || error "creating file"
1266         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1267                 error "setstripe should have failed" || true
1268 }
1269 run_test 27l "check setstripe permissions (should return error)"
1270
1271 test_27m() {
1272         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1273                 return
1274         if [ $ORIGFREE -gt $MAXFREE ]; then
1275                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1276                 return
1277         fi
1278         trap simple_cleanup_common EXIT
1279         test_mkdir -p $DIR/$tdir
1280         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1281         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1282                 error "dd should fill OST0"
1283         i=2
1284         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1285                 i=`expr $i + 1`
1286                 [ $i -gt 256 ] && break
1287         done
1288         i=`expr $i + 1`
1289         touch $DIR/$tdir/f27m_$i
1290         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1291                 error "OST0 was full but new created file still use it"
1292         i=`expr $i + 1`
1293         touch $DIR/$tdir/f27m_$i
1294         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1295                 error "OST0 was full but new created file still use it"
1296         simple_cleanup_common
1297 }
1298 run_test 27m "create file while OST0 was full =================="
1299
1300 sleep_maxage() {
1301         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1302         sleep $DELAY
1303 }
1304
1305 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1306 # if the OST isn't full anymore.
1307 reset_enospc() {
1308         local OSTIDX=${1:-""}
1309
1310         local list=$(comma_list $(osts_nodes))
1311         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1312
1313         do_nodes $list lctl set_param fail_loc=0
1314         sync    # initiate all OST_DESTROYs from MDS to OST
1315         sleep_maxage
1316 }
1317
1318 exhaust_precreations() {
1319         local OSTIDX=$1
1320         local FAILLOC=$2
1321         local FAILIDX=${3:-$OSTIDX}
1322
1323         test_mkdir -p $DIR/$tdir
1324         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1325         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1326
1327         local OST=$(ostname_from_index $OSTIDX)
1328         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1329                           sed -e 's/_UUID$//;s/^.*-//')
1330
1331         # on the mdt's osc
1332         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1333         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1334         osc.$mdtosc_proc1.prealloc_last_id)
1335         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1336         osc.$mdtosc_proc1.prealloc_next_id)
1337
1338         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1339         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1340
1341         test_mkdir -p $DIR/$tdir/${OST}
1342         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1343 #define OBD_FAIL_OST_ENOSPC              0x215
1344         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1345         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1346         echo "Creating to objid $last_id on ost $OST..."
1347         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1348         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1349         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1350         sleep_maxage
1351 }
1352
1353 exhaust_all_precreations() {
1354         local i
1355         for (( i=0; i < OSTCOUNT; i++ )) ; do
1356                 exhaust_precreations $i $1 -1
1357         done
1358 }
1359
1360 test_27n() {
1361         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1362         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1363         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1364         remote_ost_nodsh && skip "remote OST with nodsh" && return
1365
1366         reset_enospc
1367         rm -f $DIR/$tdir/$tfile
1368         exhaust_precreations 0 0x80000215
1369         $SETSTRIPE -c -1 $DIR/$tdir
1370         touch $DIR/$tdir/$tfile || error
1371         $GETSTRIPE $DIR/$tdir/$tfile
1372         reset_enospc
1373 }
1374 run_test 27n "create file with some full OSTs =================="
1375
1376 test_27o() {
1377         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1379         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1380         remote_ost_nodsh && skip "remote OST with nodsh" && return
1381
1382         reset_enospc
1383         rm -f $DIR/$tdir/$tfile
1384         exhaust_all_precreations 0x215
1385
1386         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1387
1388         reset_enospc
1389         rm -rf $DIR/$tdir/*
1390 }
1391 run_test 27o "create file with all full OSTs (should error) ===="
1392
1393 test_27p() {
1394         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1395         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1396         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1397         remote_ost_nodsh && skip "remote OST with nodsh" && return
1398
1399         reset_enospc
1400         rm -f $DIR/$tdir/$tfile
1401         test_mkdir -p $DIR/$tdir
1402
1403         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1404         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1405         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1406
1407         exhaust_precreations 0 0x80000215
1408         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1409         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1410         $GETSTRIPE $DIR/$tdir/$tfile
1411
1412         reset_enospc
1413 }
1414 run_test 27p "append to a truncated file with some full OSTs ==="
1415
1416 test_27q() {
1417         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1418         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1419         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1420         remote_ost_nodsh && skip "remote OST with nodsh" && return
1421
1422         reset_enospc
1423         rm -f $DIR/$tdir/$tfile
1424
1425         test_mkdir -p $DIR/$tdir
1426         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1427         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1428         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1429
1430         exhaust_all_precreations 0x215
1431
1432         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1433         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1434
1435         reset_enospc
1436 }
1437 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1438
1439 test_27r() {
1440         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1441         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1442         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1443         remote_ost_nodsh && skip "remote OST with nodsh" && return
1444
1445         reset_enospc
1446         rm -f $DIR/$tdir/$tfile
1447         exhaust_precreations 0 0x80000215
1448
1449         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1450
1451         reset_enospc
1452 }
1453 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1454
1455 test_27s() { # bug 10725
1456         test_mkdir -p $DIR/$tdir
1457         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1458         local stripe_count=0
1459         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1460         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1461                 error "stripe width >= 2^32 succeeded" || true
1462
1463 }
1464 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1465
1466 test_27t() { # bug 10864
1467         WDIR=`pwd`
1468         WLFS=`which lfs`
1469         cd $DIR
1470         touch $tfile
1471         $WLFS getstripe $tfile
1472         cd $WDIR
1473 }
1474 run_test 27t "check that utils parse path correctly"
1475
1476 test_27u() { # bug 4900
1477         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1478         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1479
1480 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1481         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1482         test_mkdir -p $DIR/$tdir
1483         createmany -o $DIR/$tdir/t- 1000
1484         do_facet $SINGLEMDS lctl set_param fail_loc=0
1485
1486         TLOG=$DIR/$tfile.getstripe
1487         $GETSTRIPE $DIR/$tdir > $TLOG
1488         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1489         unlinkmany $DIR/$tdir/t- 1000
1490         [ $OBJS -gt 0 ] && \
1491                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1492 }
1493 run_test 27u "skip object creation on OSC w/o objects =========="
1494
1495 test_27v() { # bug 4900
1496         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1498         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1499         remote_ost_nodsh && skip "remote OST with nodsh" && return
1500
1501         exhaust_all_precreations 0x215
1502         reset_enospc
1503
1504         test_mkdir -p $DIR/$tdir
1505         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1506
1507         touch $DIR/$tdir/$tfile
1508         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1509         # all except ost1
1510         for (( i=1; i < OSTCOUNT; i++ )); do
1511                 do_facet ost$i lctl set_param fail_loc=0x705
1512         done
1513         local START=`date +%s`
1514         createmany -o $DIR/$tdir/$tfile 32
1515
1516         local FINISH=`date +%s`
1517         local TIMEOUT=`lctl get_param -n timeout`
1518         local PROCESS=$((FINISH - START))
1519         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1520                error "$FINISH - $START >= $TIMEOUT / 2"
1521         sleep $((TIMEOUT / 2 - PROCESS))
1522         reset_enospc
1523 }
1524 run_test 27v "skip object creation on slow OST ================="
1525
1526 test_27w() { # bug 10997
1527         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1528         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1529         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1530                 error "stripe size $size != 65536" || true
1531         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1532                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1533 }
1534 run_test 27w "check $SETSTRIPE -S option"
1535
1536 test_27wa() {
1537         [ "$OSTCOUNT" -lt "2" ] &&
1538                 skip_env "skipping multiple stripe count/offset test" && return
1539
1540         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1541         for i in $(seq 1 $OSTCOUNT); do
1542                 offset=$((i - 1))
1543                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1544                         error "setstripe -c $i -i $offset failed"
1545                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1546                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1547                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1548                 [ $index -ne $offset ] &&
1549                         error "stripe offset $index != $offset" || true
1550         done
1551 }
1552 run_test 27wa "check $SETSTRIPE -c -i options"
1553
1554 test_27x() {
1555         remote_ost_nodsh && skip "remote OST with nodsh" && return
1556         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1557         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1558         OFFSET=$(($OSTCOUNT - 1))
1559         OSTIDX=0
1560         local OST=$(ostname_from_index $OSTIDX)
1561
1562         test_mkdir -p $DIR/$tdir
1563         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1564         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1565         sleep_maxage
1566         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1567         for i in `seq 0 $OFFSET`; do
1568                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1569                 error "OST0 was degraded but new created file still use it"
1570         done
1571         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1572 }
1573 run_test 27x "create files while OST0 is degraded"
1574
1575 test_27y() {
1576         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1577         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1578         remote_ost_nodsh && skip "remote OST with nodsh" && return
1579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1580
1581         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1582         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1583             osc.$mdtosc.prealloc_last_id)
1584         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1585             osc.$mdtosc.prealloc_next_id)
1586         local fcount=$((last_id - next_id))
1587         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1588         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1589
1590         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1591                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1592         local OST_DEACTIVE_IDX=-1
1593         local OSC
1594         local OSTIDX
1595         local OST
1596
1597         for OSC in $MDS_OSCS; do
1598                 OST=$(osc_to_ost $OSC)
1599                 OSTIDX=$(index_from_ostuuid $OST)
1600                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1601                         OST_DEACTIVE_IDX=$OSTIDX
1602                 fi
1603                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1604                         echo $OSC "is Deactivated:"
1605                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1606                 fi
1607         done
1608
1609         OSTIDX=$(index_from_ostuuid $OST)
1610         mkdir -p $DIR/$tdir
1611         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1612
1613         for OSC in $MDS_OSCS; do
1614                 OST=$(osc_to_ost $OSC)
1615                 OSTIDX=$(index_from_ostuuid $OST)
1616                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1617                         echo $OST "is degraded:"
1618                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1619                                                 obdfilter.$OST.degraded=1
1620                 fi
1621         done
1622
1623         sleep_maxage
1624         createmany -o $DIR/$tdir/$tfile $fcount
1625
1626         for OSC in $MDS_OSCS; do
1627                 OST=$(osc_to_ost $OSC)
1628                 OSTIDX=$(index_from_ostuuid $OST)
1629                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1630                         echo $OST "is recovered from degraded:"
1631                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1632                                                 obdfilter.$OST.degraded=0
1633                 else
1634                         do_facet $SINGLEMDS lctl --device %$OSC activate
1635                 fi
1636         done
1637 }
1638 run_test 27y "create files while OST0 is degraded and the rest inactive"
1639
1640 check_seq_oid()
1641 {
1642         log "check file $1"
1643
1644         lmm_count=$($GETSTRIPE -c $1)
1645         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1646         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1647
1648         local old_ifs="$IFS"
1649         IFS=$'[:]'
1650         fid=($($LFS path2fid $1))
1651         IFS="$old_ifs"
1652
1653         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1654         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1655
1656         # compare lmm_seq and lu_fid->f_seq
1657         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1658         # compare lmm_object_id and lu_fid->oid
1659         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1660
1661         # check the trusted.fid attribute of the OST objects of the file
1662         local have_obdidx=false
1663         local stripe_nr=0
1664         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1665                 # skip lines up to and including "obdidx"
1666                 [ -z "$obdidx" ] && break
1667                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1668                 $have_obdidx || continue
1669
1670                 local ost=$((obdidx + 1))
1671                 local dev=$(ostdevname $ost)
1672
1673                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1674                         echo "Currently only works with ldiskfs-based OSTs"
1675                         continue
1676                 fi
1677
1678                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1679
1680                 #don't unmount/remount the OSTs if we don't need to do that
1681                 #local dir=$(facet_mntpt ost$ost)
1682                 #stop ost$dev
1683                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1684                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1685                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1686                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1687                 seq=$(echo $seq | sed -e "s/^0x//g")
1688                 if [ $seq == 0 ]; then
1689                         oid_hex=$(echo $oid)
1690                 else
1691                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1692                 fi
1693                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1694                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1695                            $dev 2>/dev/null" | grep "parent=")
1696
1697                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1698
1699                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1700
1701                 #do_facet ost$ost umount -d $dir
1702                 #start ost$ost $dev $OST_MOUNT_OPTS
1703
1704                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1705                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1706                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1707                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1708                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1709                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1710
1711                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1712                 [ $ff_pseq = $lmm_seq ] ||
1713                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1714                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1715                 [ $ff_poid = $lmm_oid ] ||
1716                         error "FF parent OID $ff_poid != $lmm_oid"
1717                 [ $ff_pstripe = $stripe_nr ] ||
1718                         error "FF stripe $ff_pstripe != $stripe_nr"
1719
1720                 stripe_nr=$((stripe_nr + 1))
1721         done
1722 }
1723
1724 test_27z() {
1725         remote_ost_nodsh && skip "remote OST with nodsh" && return
1726         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1727         test_mkdir -p $DIR/$tdir
1728
1729         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1730                 { error "setstripe -c -1 failed"; return 1; }
1731         # We need to send a write to every object to get parent FID info set.
1732         # This _should_ also work for setattr, but does not currently.
1733         # touch $DIR/$tdir/$tfile-1 ||
1734         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1735                 { error "dd $tfile-1 failed"; return 2; }
1736         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1737                 { error "setstripe -c -1 failed"; return 3; }
1738         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1739                 { error "dd $tfile-2 failed"; return 4; }
1740
1741         # make sure write RPCs have been sent to OSTs
1742         sync; sleep 5; sync
1743
1744         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1745         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1746 }
1747 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1748
1749 test_27A() { # b=19102
1750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1751         local restore_size=$($GETSTRIPE -S $MOUNT)
1752         local restore_count=$($GETSTRIPE -c $MOUNT)
1753         local restore_offset=$($GETSTRIPE -i $MOUNT)
1754         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1755         local default_size=$($GETSTRIPE -S $MOUNT)
1756         local default_count=$($GETSTRIPE -c $MOUNT)
1757         local default_offset=$($GETSTRIPE -i $MOUNT)
1758         local dsize=$((1024 * 1024))
1759         [ $default_size -eq $dsize ] ||
1760                 error "stripe size $default_size != $dsize"
1761         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1762         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1763         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1764 }
1765 run_test 27A "check filesystem-wide default LOV EA values"
1766
1767 # createtest also checks that device nodes are created and
1768 # then visible correctly (#2091)
1769 test_28() { # bug 2091
1770         test_mkdir $DIR/d28
1771         $CREATETEST $DIR/d28/ct || error
1772 }
1773 run_test 28 "create/mknod/mkdir with bad file types ============"
1774
1775 test_29() {
1776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1777         cancel_lru_locks mdc
1778         test_mkdir $DIR/d29
1779         touch $DIR/d29/foo
1780         log 'first d29'
1781         ls -l $DIR/d29
1782
1783         declare -i LOCKCOUNTORIG=0
1784         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1785                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1786         done
1787         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1788
1789         declare -i LOCKUNUSEDCOUNTORIG=0
1790         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1791                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1792         done
1793
1794         log 'second d29'
1795         ls -l $DIR/d29
1796         log 'done'
1797
1798         declare -i LOCKCOUNTCURRENT=0
1799         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1800                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1801         done
1802
1803         declare -i LOCKUNUSEDCOUNTCURRENT=0
1804         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1805                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1806         done
1807
1808         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1809                 lctl set_param -n ldlm.dump_namespaces ""
1810                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1811                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1812                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1813                 return 2
1814         fi
1815         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1816                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1817                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1818                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1819                 return 3
1820         fi
1821 }
1822 run_test 29 "IT_GETATTR regression  ============================"
1823
1824 test_30a() { # was test_30
1825         cp `which ls` $DIR || cp /bin/ls $DIR
1826         $DIR/ls / || error
1827         rm $DIR/ls
1828 }
1829 run_test 30a "execute binary from Lustre (execve) =============="
1830
1831 test_30b() {
1832         cp `which ls` $DIR || cp /bin/ls $DIR
1833         chmod go+rx $DIR/ls
1834         $RUNAS $DIR/ls / || error
1835         rm $DIR/ls
1836 }
1837 run_test 30b "execute binary from Lustre as non-root ==========="
1838
1839 test_30c() { # b=22376
1840         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1841         cp `which ls` $DIR || cp /bin/ls $DIR
1842         chmod a-rw $DIR/ls
1843         cancel_lru_locks mdc
1844         cancel_lru_locks osc
1845         $RUNAS $DIR/ls / || error
1846         rm -f $DIR/ls
1847 }
1848 run_test 30c "execute binary from Lustre without read perms ===="
1849
1850 test_31a() {
1851         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1852         $CHECKSTAT -a $DIR/f31 || error
1853 }
1854 run_test 31a "open-unlink file =================================="
1855
1856 test_31b() {
1857         touch $DIR/f31 || error
1858         ln $DIR/f31 $DIR/f31b || error
1859         $MULTIOP $DIR/f31b Ouc || error
1860         $CHECKSTAT -t file $DIR/f31 || error
1861 }
1862 run_test 31b "unlink file with multiple links while open ======="
1863
1864 test_31c() {
1865         touch $DIR/f31 || error
1866         ln $DIR/f31 $DIR/f31c || error
1867         multiop_bg_pause $DIR/f31 O_uc || return 1
1868         MULTIPID=$!
1869         $MULTIOP $DIR/f31c Ouc
1870         kill -USR1 $MULTIPID
1871         wait $MULTIPID
1872 }
1873 run_test 31c "open-unlink file with multiple links ============="
1874
1875 test_31d() {
1876         opendirunlink $DIR/d31d $DIR/d31d || error
1877         $CHECKSTAT -a $DIR/d31d || error
1878 }
1879 run_test 31d "remove of open directory ========================="
1880
1881 test_31e() { # bug 2904
1882         check_kernel_version 34 || return 0
1883         openfilleddirunlink $DIR/d31e || error
1884 }
1885 run_test 31e "remove of open non-empty directory ==============="
1886
1887 test_31f() { # bug 4554
1888         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1889         set -vx
1890         test_mkdir $DIR/d31f
1891         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1892         cp /etc/hosts $DIR/d31f
1893         ls -l $DIR/d31f
1894         $GETSTRIPE $DIR/d31f/hosts
1895         multiop_bg_pause $DIR/d31f D_c || return 1
1896         MULTIPID=$!
1897
1898         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1899         test_mkdir $DIR/d31f
1900         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1901         cp /etc/hosts $DIR/d31f
1902         ls -l $DIR/d31f
1903         $GETSTRIPE $DIR/d31f/hosts
1904         multiop_bg_pause $DIR/d31f D_c || return 1
1905         MULTIPID2=$!
1906
1907         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1908         wait $MULTIPID || error "first opendir $MULTIPID failed"
1909
1910         sleep 6
1911
1912         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1913         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1914         set +vx
1915 }
1916 run_test 31f "remove of open directory with open-unlink file ==="
1917
1918 test_31g() {
1919         echo "-- cross directory link --"
1920         test_mkdir $DIR/d31ga
1921         test_mkdir $DIR/d31gb
1922         touch $DIR/d31ga/f
1923         ln $DIR/d31ga/f $DIR/d31gb/g
1924         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1925         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1926         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1927         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1928 }
1929 run_test 31g "cross directory link==============="
1930
1931 test_31h() {
1932         echo "-- cross directory link --"
1933         test_mkdir $DIR/d31h
1934         test_mkdir $DIR/d31h/dir
1935         touch $DIR/d31h/f
1936         ln $DIR/d31h/f $DIR/d31h/dir/g
1937         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1938         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1939         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1940         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1941 }
1942 run_test 31h "cross directory link under child==============="
1943
1944 test_31i() {
1945         echo "-- cross directory link --"
1946         test_mkdir $DIR/d31i
1947         test_mkdir $DIR/d31i/dir
1948         touch $DIR/d31i/dir/f
1949         ln $DIR/d31i/dir/f $DIR/d31i/g
1950         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1951         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1952         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1953         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1954 }
1955 run_test 31i "cross directory link under parent==============="
1956
1957
1958 test_31j() {
1959         test_mkdir $DIR/d31j
1960         test_mkdir $DIR/d31j/dir1
1961         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1962         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1963         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1964         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1965         return 0
1966 }
1967 run_test 31j "link for directory==============="
1968
1969
1970 test_31k() {
1971         test_mkdir $DIR/d31k
1972         touch $DIR/d31k/s
1973         touch $DIR/d31k/exist
1974         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1975         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1976         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1977         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1978         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1979         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1980         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1981         return 0
1982 }
1983 run_test 31k "link to file: the same, non-existing, dir==============="
1984
1985 test_31m() {
1986         test_mkdir $DIR/d31m
1987         touch $DIR/d31m/s
1988         test_mkdir $DIR/d31m2
1989         touch $DIR/d31m2/exist
1990         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1991         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1992         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1993         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1994         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1995         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1996         return 0
1997 }
1998 run_test 31m "link to file: the same, non-existing, dir==============="
1999
2000 cleanup_test32_mount() {
2001         trap 0
2002         $UMOUNT $DIR/$tdir/ext2-mountpoint
2003 }
2004
2005 test_32a() {
2006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2007         echo "== more mountpoints and symlinks ================="
2008         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2009         trap cleanup_test32_mount EXIT
2010         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2011         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2012         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2013         cleanup_test32_mount
2014 }
2015 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2016
2017 test_32b() {
2018         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2019         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2020         trap cleanup_test32_mount EXIT
2021         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2022         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2023         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2024         cleanup_test32_mount
2025 }
2026 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2027
2028 test_32c() {
2029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2030         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2031         trap cleanup_test32_mount EXIT
2032         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2033         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2034         test_mkdir -p $DIR/$tdir/d2/test_dir
2035         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2036         cleanup_test32_mount
2037 }
2038 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2039
2040 test_32d() {
2041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2042         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2043         trap cleanup_test32_mount EXIT
2044         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2045         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2046         test_mkdir -p $DIR/$tdir/d2/test_dir
2047         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2048         cleanup_test32_mount
2049 }
2050 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2051
2052 test_32e() {
2053         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2054         test_mkdir -p $DIR/d32e/tmp
2055         TMP_DIR=$DIR/d32e/tmp
2056         ln -s $DIR/d32e $TMP_DIR/symlink11
2057         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2058         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2059         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2060 }
2061 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2062
2063 test_32f() {
2064         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2065         test_mkdir -p $DIR/d32f/tmp
2066         TMP_DIR=$DIR/d32f/tmp
2067         ln -s $DIR/d32f $TMP_DIR/symlink11
2068         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2069         ls $DIR/d32f/tmp/symlink11  || error
2070         ls $DIR/d32f/symlink01 || error
2071 }
2072 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2073
2074 test_32g() {
2075         TMP_DIR=$DIR/$tdir/tmp
2076         test_mkdir -p $DIR/$tdir/tmp
2077         test_mkdir $DIR/${tdir}2
2078         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2079         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2080         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2081         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2082         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2083         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2084 }
2085 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2086
2087 test_32h() {
2088         rm -fr $DIR/$tdir $DIR/${tdir}2
2089         TMP_DIR=$DIR/$tdir/tmp
2090         test_mkdir -p $DIR/$tdir/tmp
2091         test_mkdir $DIR/${tdir}2
2092         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2093         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2094         ls $TMP_DIR/symlink12 || error
2095         ls $DIR/$tdir/symlink02  || error
2096 }
2097 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2098
2099 test_32i() {
2100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2101         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2102         trap cleanup_test32_mount EXIT
2103         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2104         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2105         touch $DIR/$tdir/test_file
2106         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2107         cleanup_test32_mount
2108 }
2109 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2110
2111 test_32j() {
2112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2113         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2114         trap cleanup_test32_mount EXIT
2115         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2116         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2117         touch $DIR/$tdir/test_file
2118         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2119         cleanup_test32_mount
2120 }
2121 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2122
2123 test_32k() {
2124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2125         rm -fr $DIR/$tdir
2126         trap cleanup_test32_mount EXIT
2127         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2128         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2129         test_mkdir -p $DIR/$tdir/d2
2130         touch $DIR/$tdir/d2/test_file || error
2131         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2132         cleanup_test32_mount
2133 }
2134 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2135
2136 test_32l() {
2137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2138         rm -fr $DIR/$tdir
2139         trap cleanup_test32_mount EXIT
2140         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2141         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2142         test_mkdir -p $DIR/$tdir/d2
2143         touch $DIR/$tdir/d2/test_file
2144         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2145         cleanup_test32_mount
2146 }
2147 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2148
2149 test_32m() {
2150         rm -fr $DIR/d32m
2151         test_mkdir -p $DIR/d32m/tmp
2152         TMP_DIR=$DIR/d32m/tmp
2153         ln -s $DIR $TMP_DIR/symlink11
2154         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2155         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2156         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2157 }
2158 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2159
2160 test_32n() {
2161         rm -fr $DIR/d32n
2162         test_mkdir -p $DIR/d32n/tmp
2163         TMP_DIR=$DIR/d32n/tmp
2164         ln -s $DIR $TMP_DIR/symlink11
2165         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2166         ls -l $DIR/d32n/tmp/symlink11  || error
2167         ls -l $DIR/d32n/symlink01 || error
2168 }
2169 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2170
2171 test_32o() {
2172         rm -fr $DIR/d32o $DIR/$tfile
2173         touch $DIR/$tfile
2174         test_mkdir -p $DIR/d32o/tmp
2175         TMP_DIR=$DIR/d32o/tmp
2176         ln -s $DIR/$tfile $TMP_DIR/symlink12
2177         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2178         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2179         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2180         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2181         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2182 }
2183 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2184
2185 test_32p() {
2186     log 32p_1
2187         rm -fr $DIR/d32p
2188     log 32p_2
2189         rm -f $DIR/$tfile
2190     log 32p_3
2191         touch $DIR/$tfile
2192     log 32p_4
2193         test_mkdir -p $DIR/d32p/tmp
2194     log 32p_5
2195         TMP_DIR=$DIR/d32p/tmp
2196     log 32p_6
2197         ln -s $DIR/$tfile $TMP_DIR/symlink12
2198     log 32p_7
2199         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2200     log 32p_8
2201         cat $DIR/d32p/tmp/symlink12 || error
2202     log 32p_9
2203         cat $DIR/d32p/symlink02 || error
2204     log 32p_10
2205 }
2206 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2207
2208 cleanup_testdir_mount() {
2209         trap 0
2210         $UMOUNT $DIR/$tdir
2211 }
2212
2213 test_32q() {
2214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2215         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2216         trap cleanup_testdir_mount EXIT
2217         test_mkdir -p $DIR/$tdir
2218         touch $DIR/$tdir/under_the_mount
2219         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2220         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2221         cleanup_testdir_mount
2222 }
2223 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2224
2225 test_32r() {
2226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2227         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2228         trap cleanup_testdir_mount EXIT
2229         test_mkdir -p $DIR/$tdir
2230         touch $DIR/$tdir/under_the_mount
2231         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2232         ls $DIR/$tdir | grep -q under_the_mount && error || true
2233         cleanup_testdir_mount
2234 }
2235 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2236
2237 test_33aa() {
2238         rm -f $DIR/$tfile
2239         touch $DIR/$tfile
2240         chmod 444 $DIR/$tfile
2241         chown $RUNAS_ID $DIR/$tfile
2242         log 33_1
2243         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2244         log 33_2
2245 }
2246 run_test 33aa "write file with mode 444 (should return error) ===="
2247
2248 test_33a() {
2249         rm -fr $DIR/d33
2250         test_mkdir -p $DIR/d33
2251         chown $RUNAS_ID $DIR/d33
2252         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2253         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2254                 error "open RDWR" || true
2255 }
2256 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2257
2258 test_33b() {
2259         rm -fr $DIR/d33
2260         test_mkdir -p $DIR/d33
2261         chown $RUNAS_ID $DIR/d33
2262         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2263 }
2264 run_test 33b "test open file with malformed flags (No panic and return error)"
2265
2266 test_33c() {
2267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2268         local ostnum
2269         local ostname
2270         local write_bytes
2271         local all_zeros
2272
2273         remote_ost_nodsh && skip "remote OST with nodsh" && return
2274         all_zeros=:
2275         rm -fr $DIR/d33
2276         test_mkdir -p $DIR/d33
2277         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2278
2279         sync
2280         for ostnum in $(seq $OSTCOUNT); do
2281                 # test-framework's OST numbering is one-based, while Lustre's
2282                 # is zero-based
2283                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2284                 # Parsing llobdstat's output sucks; we could grep the /proc
2285                 # path, but that's likely to not be as portable as using the
2286                 # llobdstat utility.  So we parse lctl output instead.
2287                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2288                         obdfilter/$ostname/stats |
2289                         awk '/^write_bytes/ {print $7}' )
2290                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2291                 if (( ${write_bytes:-0} > 0 ))
2292                 then
2293                         all_zeros=false
2294                         break;
2295                 fi
2296         done
2297
2298         $all_zeros || return 0
2299
2300         # Write four bytes
2301         echo foo > $DIR/d33/bar
2302         # Really write them
2303         sync
2304
2305         # Total up write_bytes after writing.  We'd better find non-zeros.
2306         for ostnum in $(seq $OSTCOUNT); do
2307                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2308                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2309                         obdfilter/$ostname/stats |
2310                         awk '/^write_bytes/ {print $7}' )
2311                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2312                 if (( ${write_bytes:-0} > 0 ))
2313                 then
2314                         all_zeros=false
2315                         break;
2316                 fi
2317         done
2318
2319         if $all_zeros
2320         then
2321                 for ostnum in $(seq $OSTCOUNT); do
2322                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2323                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2324                         do_facet ost$ostnum lctl get_param -n \
2325                                 obdfilter/$ostname/stats
2326                 done
2327                 error "OST not keeping write_bytes stats (b22312)"
2328         fi
2329 }
2330 run_test 33c "test llobdstat and write_bytes"
2331
2332 test_33d() {
2333         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2335         local MDTIDX=1
2336         local remote_dir=$DIR/$tdir/remote_dir
2337
2338         mkdir -p $DIR/$tdir
2339         $LFS mkdir -i $MDTIDX $remote_dir ||
2340                 error "create remote directory failed"
2341
2342         touch $remote_dir/$tfile
2343         chmod 444 $remote_dir/$tfile
2344         chown $RUNAS_ID $remote_dir/$tfile
2345
2346         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2347
2348         chown $RUNAS_ID $remote_dir
2349         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2350                                         error "create" || true
2351         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2352                                     error "open RDWR" || true
2353         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2354                                     error "create" || true
2355 }
2356 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2357
2358 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2359 test_34a() {
2360         rm -f $DIR/f34
2361         $MCREATE $DIR/f34 || error
2362         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2363         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2364         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2365         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2366 }
2367 run_test 34a "truncate file that has not been opened ==========="
2368
2369 test_34b() {
2370         [ ! -f $DIR/f34 ] && test_34a
2371         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2372         $OPENFILE -f O_RDONLY $DIR/f34
2373         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2374         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2375 }
2376 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2377
2378 test_34c() {
2379         [ ! -f $DIR/f34 ] && test_34a
2380         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2381         $OPENFILE -f O_RDWR $DIR/f34
2382         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2383         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2384 }
2385 run_test 34c "O_RDWR opening file-with-size works =============="
2386
2387 test_34d() {
2388         [ ! -f $DIR/f34 ] && test_34a
2389         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2390         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2391         rm $DIR/f34
2392 }
2393 run_test 34d "write to sparse file ============================="
2394
2395 test_34e() {
2396         rm -f $DIR/f34e
2397         $MCREATE $DIR/f34e || error
2398         $TRUNCATE $DIR/f34e 1000 || error
2399         $CHECKSTAT -s 1000 $DIR/f34e || error
2400         $OPENFILE -f O_RDWR $DIR/f34e
2401         $CHECKSTAT -s 1000 $DIR/f34e || error
2402 }
2403 run_test 34e "create objects, some with size and some without =="
2404
2405 test_34f() { # bug 6242, 6243
2406         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2407         SIZE34F=48000
2408         rm -f $DIR/f34f
2409         $MCREATE $DIR/f34f || error
2410         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2411         dd if=$DIR/f34f of=$TMP/f34f
2412         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2413         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2414         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2415         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2416         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2417 }
2418 run_test 34f "read from a file with no objects until EOF ======="
2419
2420 test_34g() {
2421         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2422         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2423         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2424         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2425         cancel_lru_locks osc
2426         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2427                 error "wrong size after lock cancel"
2428
2429         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2430         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2431                 error "expanding truncate failed"
2432         cancel_lru_locks osc
2433         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2434                 error "wrong expanded size after lock cancel"
2435 }
2436 run_test 34g "truncate long file ==============================="
2437
2438 test_34h() {
2439         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2440         local gid=10
2441         local sz=1000
2442
2443         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2444         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2445         MULTIPID=$!
2446         sleep 2
2447
2448         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2449                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2450                 kill -9 $MULTIPID
2451         fi
2452         wait $MULTIPID
2453         local nsz=`stat -c %s $DIR/$tfile`
2454         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2455 }
2456 run_test 34h "ftruncate file under grouplock should not block"
2457
2458 test_35a() {
2459         cp /bin/sh $DIR/f35a
2460         chmod 444 $DIR/f35a
2461         chown $RUNAS_ID $DIR/f35a
2462         $RUNAS $DIR/f35a && error || true
2463         rm $DIR/f35a
2464 }
2465 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2466
2467 test_36a() {
2468         rm -f $DIR/f36
2469         utime $DIR/f36 || error
2470 }
2471 run_test 36a "MDS utime check (mknod, utime) ==================="
2472
2473 test_36b() {
2474         echo "" > $DIR/f36
2475         utime $DIR/f36 || error
2476 }
2477 run_test 36b "OST utime check (open, utime) ===================="
2478
2479 test_36c() {
2480         rm -f $DIR/d36/f36
2481         test_mkdir $DIR/d36
2482         chown $RUNAS_ID $DIR/d36
2483         $RUNAS utime $DIR/d36/f36 || error
2484 }
2485 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2486
2487 test_36d() {
2488         [ ! -d $DIR/d36 ] && test_36c
2489         echo "" > $DIR/d36/f36
2490         $RUNAS utime $DIR/d36/f36 || error
2491 }
2492 run_test 36d "non-root OST utime check (open, utime) ==========="
2493
2494 test_36e() {
2495         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2496         test_mkdir -p $DIR/$tdir
2497         touch $DIR/$tdir/$tfile
2498         $RUNAS utime $DIR/$tdir/$tfile && \
2499                 error "utime worked, expected failure" || true
2500 }
2501 run_test 36e "utime on non-owned file (should return error) ===="
2502
2503 subr_36fh() {
2504         local fl="$1"
2505         local LANG_SAVE=$LANG
2506         local LC_LANG_SAVE=$LC_LANG
2507         export LANG=C LC_LANG=C # for date language
2508
2509         DATESTR="Dec 20  2000"
2510         test_mkdir -p $DIR/$tdir
2511         lctl set_param fail_loc=$fl
2512         date; date +%s
2513         cp /etc/hosts $DIR/$tdir/$tfile
2514         sync & # write RPC generated with "current" inode timestamp, but delayed
2515         sleep 1
2516         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2517         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2518         cancel_lru_locks osc
2519         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2520         date; date +%s
2521         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2522                 echo "BEFORE: $LS_BEFORE" && \
2523                 echo "AFTER : $LS_AFTER" && \
2524                 echo "WANT  : $DATESTR" && \
2525                 error "$DIR/$tdir/$tfile timestamps changed" || true
2526
2527         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2528 }
2529
2530 test_36f() {
2531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2532         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2533         subr_36fh "0x80000214"
2534 }
2535 run_test 36f "utime on file racing with OST BRW write =========="
2536
2537 test_36g() {
2538         remote_ost_nodsh && skip "remote OST with nodsh" && return
2539         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2540         local fmd_max_age
2541         local fmd_before
2542         local fmd_after
2543
2544         test_mkdir -p $DIR/$tdir
2545         fmd_max_age=$(do_facet ost1 \
2546                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2547                 head -n 1")
2548
2549         fmd_before=$(do_facet ost1 \
2550                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2551         touch $DIR/$tdir/$tfile
2552         sleep $((fmd_max_age + 12))
2553         fmd_after=$(do_facet ost1 \
2554                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2555
2556         echo "fmd_before: $fmd_before"
2557         echo "fmd_after: $fmd_after"
2558         [ "$fmd_after" -gt "$fmd_before" ] && \
2559                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2560                 error "fmd didn't expire after ping" || true
2561 }
2562 run_test 36g "filter mod data cache expiry ====================="
2563
2564 test_36h() {
2565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2566         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2567         subr_36fh "0x80000227"
2568 }
2569 run_test 36h "utime on file racing with OST BRW write =========="
2570
2571 # test_37 - duplicate with tests 32q 32r
2572
2573 test_38() {
2574         local file=$DIR/$tfile
2575         touch $file
2576         openfile -f O_DIRECTORY $file
2577         local RC=$?
2578         local ENOTDIR=20
2579         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2580         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2581 }
2582 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2583
2584 test_39() {
2585         touch $DIR/$tfile
2586         touch $DIR/${tfile}2
2587 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2588 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2589 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2590         sleep 2
2591         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2592         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2593                 echo "mtime"
2594                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2595                 echo "atime"
2596                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2597                 echo "ctime"
2598                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2599                 error "O_TRUNC didn't change timestamps"
2600         fi
2601 }
2602 run_test 39 "mtime changed on create ==========================="
2603
2604 test_39b() {
2605         test_mkdir -p $DIR/$tdir
2606         cp -p /etc/passwd $DIR/$tdir/fopen
2607         cp -p /etc/passwd $DIR/$tdir/flink
2608         cp -p /etc/passwd $DIR/$tdir/funlink
2609         cp -p /etc/passwd $DIR/$tdir/frename
2610         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2611
2612         sleep 1
2613         echo "aaaaaa" >> $DIR/$tdir/fopen
2614         echo "aaaaaa" >> $DIR/$tdir/flink
2615         echo "aaaaaa" >> $DIR/$tdir/funlink
2616         echo "aaaaaa" >> $DIR/$tdir/frename
2617
2618         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2619         local link_new=`stat -c %Y $DIR/$tdir/flink`
2620         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2621         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2622
2623         cat $DIR/$tdir/fopen > /dev/null
2624         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2625         rm -f $DIR/$tdir/funlink2
2626         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2627
2628         for (( i=0; i < 2; i++ )) ; do
2629                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2630                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2631                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2632                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2633
2634                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2635                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2636                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2637                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2638
2639                 cancel_lru_locks osc
2640                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2641         done
2642 }
2643 run_test 39b "mtime change on open, link, unlink, rename  ======"
2644
2645 # this should be set to past
2646 TEST_39_MTIME=`date -d "1 year ago" +%s`
2647
2648 # bug 11063
2649 test_39c() {
2650         touch $DIR1/$tfile
2651         sleep 2
2652         local mtime0=`stat -c %Y $DIR1/$tfile`
2653
2654         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2655         local mtime1=`stat -c %Y $DIR1/$tfile`
2656         [ "$mtime1" = $TEST_39_MTIME ] || \
2657                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2658
2659         local d1=`date +%s`
2660         echo hello >> $DIR1/$tfile
2661         local d2=`date +%s`
2662         local mtime2=`stat -c %Y $DIR1/$tfile`
2663         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2664                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2665
2666         mv $DIR1/$tfile $DIR1/$tfile-1
2667
2668         for (( i=0; i < 2; i++ )) ; do
2669                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2670                 [ "$mtime2" = "$mtime3" ] || \
2671                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2672
2673                 cancel_lru_locks osc
2674                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2675         done
2676 }
2677 run_test 39c "mtime change on rename ==========================="
2678
2679 # bug 21114
2680 test_39d() {
2681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2682         touch $DIR1/$tfile
2683
2684         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2685
2686         for (( i=0; i < 2; i++ )) ; do
2687                 local mtime=`stat -c %Y $DIR1/$tfile`
2688                 [ $mtime = $TEST_39_MTIME ] || \
2689                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2690
2691                 cancel_lru_locks osc
2692                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2693         done
2694 }
2695 run_test 39d "create, utime, stat =============================="
2696
2697 # bug 21114
2698 test_39e() {
2699         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2700         touch $DIR1/$tfile
2701         local mtime1=`stat -c %Y $DIR1/$tfile`
2702
2703         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2704
2705         for (( i=0; i < 2; i++ )) ; do
2706                 local mtime2=`stat -c %Y $DIR1/$tfile`
2707                 [ $mtime2 = $TEST_39_MTIME ] || \
2708                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2709
2710                 cancel_lru_locks osc
2711                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2712         done
2713 }
2714 run_test 39e "create, stat, utime, stat ========================"
2715
2716 # bug 21114
2717 test_39f() {
2718         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2719         touch $DIR1/$tfile
2720         mtime1=`stat -c %Y $DIR1/$tfile`
2721
2722         sleep 2
2723         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2724
2725         for (( i=0; i < 2; i++ )) ; do
2726                 local mtime2=`stat -c %Y $DIR1/$tfile`
2727                 [ $mtime2 = $TEST_39_MTIME ] || \
2728                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2729
2730                 cancel_lru_locks osc
2731                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2732         done
2733 }
2734 run_test 39f "create, stat, sleep, utime, stat ================="
2735
2736 # bug 11063
2737 test_39g() {
2738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2739         echo hello >> $DIR1/$tfile
2740         local mtime1=`stat -c %Y $DIR1/$tfile`
2741
2742         sleep 2
2743         chmod o+r $DIR1/$tfile
2744
2745         for (( i=0; i < 2; i++ )) ; do
2746                 local mtime2=`stat -c %Y $DIR1/$tfile`
2747                 [ "$mtime1" = "$mtime2" ] || \
2748                         error "lost mtime: $mtime2, should be $mtime1"
2749
2750                 cancel_lru_locks osc
2751                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2752         done
2753 }
2754 run_test 39g "write, chmod, stat ==============================="
2755
2756 # bug 11063
2757 test_39h() {
2758         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2759         touch $DIR1/$tfile
2760         sleep 1
2761
2762         local d1=`date`
2763         echo hello >> $DIR1/$tfile
2764         local mtime1=`stat -c %Y $DIR1/$tfile`
2765
2766         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2767         local d2=`date`
2768         if [ "$d1" != "$d2" ]; then
2769                 echo "write and touch not within one second"
2770         else
2771                 for (( i=0; i < 2; i++ )) ; do
2772                         local mtime2=`stat -c %Y $DIR1/$tfile`
2773                         [ "$mtime2" = $TEST_39_MTIME ] || \
2774                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2775
2776                         cancel_lru_locks osc
2777                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2778                 done
2779         fi
2780 }
2781 run_test 39h "write, utime within one second, stat ============="
2782
2783 test_39i() {
2784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2785         touch $DIR1/$tfile
2786         sleep 1
2787
2788         echo hello >> $DIR1/$tfile
2789         local mtime1=`stat -c %Y $DIR1/$tfile`
2790
2791         mv $DIR1/$tfile $DIR1/$tfile-1
2792
2793         for (( i=0; i < 2; i++ )) ; do
2794                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2795
2796                 [ "$mtime1" = "$mtime2" ] || \
2797                         error "lost mtime: $mtime2, should be $mtime1"
2798
2799                 cancel_lru_locks osc
2800                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2801         done
2802 }
2803 run_test 39i "write, rename, stat =============================="
2804
2805 test_39j() {
2806         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2807         start_full_debug_logging
2808         touch $DIR1/$tfile
2809         sleep 1
2810
2811         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2812         lctl set_param fail_loc=0x80000412
2813         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2814                 error "multiop failed"
2815         local multipid=$!
2816         local mtime1=`stat -c %Y $DIR1/$tfile`
2817
2818         mv $DIR1/$tfile $DIR1/$tfile-1
2819
2820         kill -USR1 $multipid
2821         wait $multipid || error "multiop close failed"
2822
2823         for (( i=0; i < 2; i++ )) ; do
2824                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2825                 [ "$mtime1" = "$mtime2" ] ||
2826                         error "mtime is lost on close: $mtime2, " \
2827                               "should be $mtime1"
2828
2829                 cancel_lru_locks osc
2830                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2831         done
2832         lctl set_param fail_loc=0
2833         stop_full_debug_logging
2834 }
2835 run_test 39j "write, rename, close, stat ======================="
2836
2837 test_39k() {
2838         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2839         touch $DIR1/$tfile
2840         sleep 1
2841
2842         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2843         local multipid=$!
2844         local mtime1=`stat -c %Y $DIR1/$tfile`
2845
2846         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2847
2848         kill -USR1 $multipid
2849         wait $multipid || error "multiop close failed"
2850
2851         for (( i=0; i < 2; i++ )) ; do
2852                 local mtime2=`stat -c %Y $DIR1/$tfile`
2853
2854                 [ "$mtime2" = $TEST_39_MTIME ] || \
2855                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2856
2857                 cancel_lru_locks osc
2858                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2859         done
2860 }
2861 run_test 39k "write, utime, close, stat ========================"
2862
2863 # this should be set to future
2864 TEST_39_ATIME=`date -d "1 year" +%s`
2865
2866 test_39l() {
2867         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2868         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2869         local atime_diff=$(do_facet $SINGLEMDS \
2870                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2871         rm -rf $DIR/$tdir
2872         mkdir -p $DIR/$tdir
2873
2874         # test setting directory atime to future
2875         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2876         local atime=$(stat -c %X $DIR/$tdir)
2877         [ "$atime" = $TEST_39_ATIME ] || \
2878                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2879
2880         # test setting directory atime from future to now
2881         local d1=$(date +%s)
2882         ls $DIR/$tdir
2883         local d2=$(date +%s)
2884
2885         cancel_lru_locks mdc
2886         atime=$(stat -c %X $DIR/$tdir)
2887         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2888                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2889
2890         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2891         sleep 3
2892
2893         # test setting directory atime when now > dir atime + atime_diff
2894         d1=$(date +%s)
2895         ls $DIR/$tdir
2896         d2=$(date +%s)
2897         cancel_lru_locks mdc
2898         atime=$(stat -c %X $DIR/$tdir)
2899         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2900                 error "atime is not updated  : $atime, should be $d2"
2901
2902         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
2903         sleep 3
2904
2905         # test not setting directory atime when now < dir atime + atime_diff
2906         ls $DIR/$tdir
2907         cancel_lru_locks mdc
2908         atime=$(stat -c %X $DIR/$tdir)
2909         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2910                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2911
2912         do_facet $SINGLEMDS \
2913                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
2914 }
2915 run_test 39l "directory atime update ==========================="
2916
2917 test_39m() {
2918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2919         touch $DIR1/$tfile
2920         sleep 2
2921         local far_past_mtime=$(date -d "May 29 1953" +%s)
2922         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2923
2924         touch -m -d @$far_past_mtime $DIR1/$tfile
2925         touch -a -d @$far_past_atime $DIR1/$tfile
2926
2927         for (( i=0; i < 2; i++ )) ; do
2928                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2929                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2930                         error "atime or mtime set incorrectly"
2931
2932                 cancel_lru_locks osc
2933                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2934         done
2935 }
2936 run_test 39m "test atime and mtime before 1970"
2937
2938 test_40() {
2939         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2940         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2941         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2942 }
2943 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2944
2945 test_41() {
2946         # bug 1553
2947         small_write $DIR/f41 18
2948 }
2949 run_test 41 "test small file write + fstat ====================="
2950
2951 count_ost_writes() {
2952         lctl get_param -n osc.*.stats |
2953             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2954 }
2955
2956 # decent default
2957 WRITEBACK_SAVE=500
2958 DIRTY_RATIO_SAVE=40
2959 MAX_DIRTY_RATIO=50
2960 BG_DIRTY_RATIO_SAVE=10
2961 MAX_BG_DIRTY_RATIO=25
2962
2963 start_writeback() {
2964         trap 0
2965         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2966         # dirty_ratio, dirty_background_ratio
2967         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2968                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2969                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2970                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2971         else
2972                 # if file not here, we are a 2.4 kernel
2973                 kill -CONT `pidof kupdated`
2974         fi
2975 }
2976
2977 stop_writeback() {
2978         # setup the trap first, so someone cannot exit the test at the
2979         # exact wrong time and mess up a machine
2980         trap start_writeback EXIT
2981         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2982         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2983                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2984                 sysctl -w vm.dirty_writeback_centisecs=0
2985                 sysctl -w vm.dirty_writeback_centisecs=0
2986                 # save and increase /proc/sys/vm/dirty_ratio
2987                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2988                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2989                 # save and increase /proc/sys/vm/dirty_background_ratio
2990                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2991                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2992         else
2993                 # if file not here, we are a 2.4 kernel
2994                 kill -STOP `pidof kupdated`
2995         fi
2996 }
2997
2998 # ensure that all stripes have some grant before we test client-side cache
2999 setup_test42() {
3000         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3001                 dd if=/dev/zero of=$i bs=4k count=1
3002                 rm $i
3003         done
3004 }
3005
3006 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3007 # file truncation, and file removal.
3008 test_42a() {
3009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3010         setup_test42
3011         cancel_lru_locks osc
3012         stop_writeback
3013         sync; sleep 1; sync # just to be safe
3014         BEFOREWRITES=`count_ost_writes`
3015         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3016         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3017         AFTERWRITES=`count_ost_writes`
3018         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3019                 error "$BEFOREWRITES < $AFTERWRITES"
3020         start_writeback
3021 }
3022 run_test 42a "ensure that we don't flush on close =============="
3023
3024 test_42b() {
3025         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3026         setup_test42
3027         cancel_lru_locks osc
3028         stop_writeback
3029         sync
3030         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3031         BEFOREWRITES=`count_ost_writes`
3032         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3033         AFTERWRITES=`count_ost_writes`
3034         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3035                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3036         fi
3037         BEFOREWRITES=`count_ost_writes`
3038         sync || error "sync: $?"
3039         AFTERWRITES=`count_ost_writes`
3040         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
3041                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3042         fi
3043         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3044         start_writeback
3045         return 0
3046 }
3047 run_test 42b "test destroy of file with cached dirty data ======"
3048
3049 # if these tests just want to test the effect of truncation,
3050 # they have to be very careful.  consider:
3051 # - the first open gets a {0,EOF}PR lock
3052 # - the first write conflicts and gets a {0, count-1}PW
3053 # - the rest of the writes are under {count,EOF}PW
3054 # - the open for truncate tries to match a {0,EOF}PR
3055 #   for the filesize and cancels the PWs.
3056 # any number of fixes (don't get {0,EOF} on open, match
3057 # composite locks, do smarter file size management) fix
3058 # this, but for now we want these tests to verify that
3059 # the cancellation with truncate intent works, so we
3060 # start the file with a full-file pw lock to match against
3061 # until the truncate.
3062 trunc_test() {
3063         test=$1
3064         file=$DIR/$test
3065         offset=$2
3066         cancel_lru_locks osc
3067         stop_writeback
3068         # prime the file with 0,EOF PW to match
3069         touch $file
3070         $TRUNCATE $file 0
3071         sync; sync
3072         # now the real test..
3073         dd if=/dev/zero of=$file bs=1024 count=100
3074         BEFOREWRITES=`count_ost_writes`
3075         $TRUNCATE $file $offset
3076         cancel_lru_locks osc
3077         AFTERWRITES=`count_ost_writes`
3078         start_writeback
3079 }
3080
3081 test_42c() {
3082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3083         trunc_test 42c 1024
3084         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3085             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3086         rm $file
3087 }
3088 run_test 42c "test partial truncate of file with cached dirty data"
3089
3090 test_42d() {
3091         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3092         trunc_test 42d 0
3093         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3094             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3095         rm $file
3096 }
3097 run_test 42d "test complete truncate of file with cached dirty data"
3098
3099 test_42e() { # bug22074
3100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3101         local TDIR=$DIR/${tdir}e
3102         local pagesz=$(page_size)
3103         local pages=16 # hardcoded 16 pages, don't change it.
3104         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3105         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3106         local max_dirty_mb
3107         local warmup_files
3108
3109         test_mkdir -p $DIR/${tdir}e
3110         $SETSTRIPE -c 1 $TDIR
3111         createmany -o $TDIR/f $files
3112
3113         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3114
3115         # we assume that with $OSTCOUNT files, at least one of them will
3116         # be allocated on OST0.
3117         warmup_files=$((OSTCOUNT * max_dirty_mb))
3118         createmany -o $TDIR/w $warmup_files
3119
3120         # write a large amount of data into one file and sync, to get good
3121         # avail_grant number from OST.
3122         for ((i=0; i<$warmup_files; i++)); do
3123                 idx=$($GETSTRIPE -i $TDIR/w$i)
3124                 [ $idx -ne 0 ] && continue
3125                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3126                 break
3127         done
3128         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3129         sync
3130         $LCTL get_param $proc_osc0/cur_dirty_bytes
3131         $LCTL get_param $proc_osc0/cur_grant_bytes
3132
3133         # create as much dirty pages as we can while not to trigger the actual
3134         # RPCs directly. but depends on the env, VFS may trigger flush during this
3135         # period, hopefully we are good.
3136         for ((i=0; i<$warmup_files; i++)); do
3137                 idx=$($GETSTRIPE -i $TDIR/w$i)
3138                 [ $idx -ne 0 ] && continue
3139                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3140         done
3141         $LCTL get_param $proc_osc0/cur_dirty_bytes
3142         $LCTL get_param $proc_osc0/cur_grant_bytes
3143
3144         # perform the real test
3145         $LCTL set_param $proc_osc0/rpc_stats 0
3146         for ((;i<$files; i++)); do
3147                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3148                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3149         done
3150         sync
3151         $LCTL get_param $proc_osc0/rpc_stats
3152
3153         local percent=0
3154         local have_ppr=false
3155         $LCTL get_param $proc_osc0/rpc_stats |
3156                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3157                         # skip lines until we are at the RPC histogram data
3158                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3159                         $have_ppr || continue
3160
3161                         # we only want the percent stat for < 16 pages
3162                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3163
3164                         percent=$((percent + WPCT))
3165                         if [ $percent -gt 15 ]; then
3166                                 error "less than 16-pages write RPCs" \
3167                                       "$percent% > 15%"
3168                                 break
3169                         fi
3170                 done
3171         rm -rf $TDIR
3172 }
3173 run_test 42e "verify sub-RPC writes are not done synchronously"
3174
3175 test_43() {
3176         test_mkdir -p $DIR/$tdir
3177         cp -p /bin/ls $DIR/$tdir/$tfile
3178         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3179         pid=$!
3180         # give multiop a chance to open
3181         sleep 1
3182
3183         $DIR/$tdir/$tfile && error || true
3184         kill -USR1 $pid
3185 }
3186 run_test 43 "execution of file opened for write should return -ETXTBSY"
3187
3188 test_43a() {
3189         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3190         test_mkdir -p $DIR/$tdir
3191         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3192                         cp -p multiop $DIR/$tdir/multiop
3193         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3194                         return 1
3195         MULTIOP_PID=$!
3196         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3197         kill -USR1 $MULTIOP_PID || return 2
3198         wait $MULTIOP_PID || return 3
3199         rm $TMP/test43.junk
3200 }
3201 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3202
3203 test_43b() {
3204         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3205         test_mkdir -p $DIR/$tdir
3206         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3207                         cp -p multiop $DIR/$tdir/multiop
3208         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3209                         return 1
3210         MULTIOP_PID=$!
3211         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3212         kill -USR1 $MULTIOP_PID || return 2
3213         wait $MULTIOP_PID || return 3
3214         rm $TMP/test43.junk
3215 }
3216 run_test 43b "truncate of file being executed should return -ETXTBSY"
3217
3218 test_43c() {
3219         local testdir="$DIR/$tdir"
3220         test_mkdir -p $DIR/$tdir
3221         cp $SHELL $testdir/
3222         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3223                 ( cd $testdir && md5sum -c)
3224 }
3225 run_test 43c "md5sum of copy into lustre========================"
3226
3227 test_44() {
3228         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3229         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3230         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3231 }
3232 run_test 44 "zero length read from a sparse stripe ============="
3233
3234 test_44a() {
3235     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3236                          awk '{print $2}'`
3237     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3238     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3239     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3240                       awk '{print $2}'`
3241     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3242         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3243     fi
3244
3245     OFFSETS="0 $((stride/2)) $((stride-1))"
3246     for offset in $OFFSETS ; do
3247       for i in `seq 0 $((nstripe-1))`; do
3248         local GLOBALOFFSETS=""
3249         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3250         local myfn=$DIR/d44a-$size
3251         echo "--------writing $myfn at $size"
3252         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3253         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3254         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3255                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3256
3257         for j in `seq 0 $((nstripe-1))`; do
3258             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3259             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3260             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3261         done
3262         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3263                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3264         rm -f $myfn
3265       done
3266     done
3267 }
3268 run_test 44a "test sparse pwrite ==============================="
3269
3270 dirty_osc_total() {
3271         tot=0
3272         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3273                 tot=$(($tot + $d))
3274         done
3275         echo $tot
3276 }
3277 do_dirty_record() {
3278         before=`dirty_osc_total`
3279         echo executing "\"$*\""
3280         eval $*
3281         after=`dirty_osc_total`
3282         echo before $before, after $after
3283 }
3284 test_45() {
3285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3286         f="$DIR/f45"
3287         # Obtain grants from OST if it supports it
3288         echo blah > ${f}_grant
3289         stop_writeback
3290         sync
3291         do_dirty_record "echo blah > $f"
3292         [ $before -eq $after ] && error "write wasn't cached"
3293         do_dirty_record "> $f"
3294         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3295         do_dirty_record "echo blah > $f"
3296         [ $before -eq $after ] && error "write wasn't cached"
3297         do_dirty_record "sync"
3298         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3299         do_dirty_record "echo blah > $f"
3300         [ $before -eq $after ] && error "write wasn't cached"
3301         do_dirty_record "cancel_lru_locks osc"
3302         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3303         start_writeback
3304 }
3305 run_test 45 "osc io page accounting ============================"
3306
3307 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3308 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3309 # objects offset and an assert hit when an rpc was built with 1023's mapped
3310 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3311 test_46() {
3312         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3313         f="$DIR/f46"
3314         stop_writeback
3315         sync
3316         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3317         sync
3318         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3319         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3320         sync
3321         start_writeback
3322 }
3323 run_test 46 "dirtying a previously written page ================"
3324
3325 # test_47 is removed "Device nodes check" is moved to test_28
3326
3327 test_48a() { # bug 2399
3328         check_kernel_version 34 || return 0
3329         test_mkdir -p $DIR/$tdir
3330         cd $DIR/$tdir
3331         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3332         test_mkdir $DIR/$tdir || error "recreate directory failed"
3333         touch foo || error "'touch foo' failed after recreating cwd"
3334         test_mkdir $DIR/$tdir/bar ||
3335                      error "'mkdir foo' failed after recreating cwd"
3336         if check_kernel_version 44; then
3337                 touch .foo || error "'touch .foo' failed after recreating cwd"
3338                 test_mkdir $DIR/$tdir/.bar ||
3339                               error "'mkdir .foo' failed after recreating cwd"
3340         fi
3341         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3342         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3343         cd . || error "'cd .' failed after recreating cwd"
3344         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3345         rmdir . && error "'rmdir .' worked after recreating cwd"
3346         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3347         cd .. || error "'cd ..' failed after recreating cwd"
3348 }
3349 run_test 48a "Access renamed working dir (should return errors)="
3350
3351 test_48b() { # bug 2399
3352         check_kernel_version 34 || return 0
3353         rm -rf $DIR/$tdir
3354         test_mkdir -p $DIR/$tdir
3355         cd $DIR/$tdir
3356         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3357         touch foo && error "'touch foo' worked after removing cwd"
3358         test_mkdir $DIR/$tdir/foo &&
3359                      error "'mkdir foo' worked after removing cwd"
3360         if check_kernel_version 44; then
3361                 touch .foo && error "'touch .foo' worked after removing cwd"
3362                 test_mkdir $DIR/$tdir/.foo &&
3363                               error "'mkdir .foo' worked after removing cwd"
3364         fi
3365         ls . > /dev/null && error "'ls .' worked after removing cwd"
3366         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3367         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3368         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3369         rmdir . && error "'rmdir .' worked after removing cwd"
3370         ln -s . foo && error "'ln -s .' worked after removing cwd"
3371         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3372 }
3373 run_test 48b "Access removed working dir (should return errors)="
3374
3375 test_48c() { # bug 2350
3376         check_kernel_version 36 || return 0
3377         #lctl set_param debug=-1
3378         #set -vx
3379         rm -rf $DIR/$tdir
3380         test_mkdir -p $DIR/$tdir/dir
3381         cd $DIR/$tdir/dir
3382         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3383         $TRACE touch foo && error "touch foo worked after removing cwd"
3384         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3385         if check_kernel_version 44; then
3386                 touch .foo && error "touch .foo worked after removing cwd"
3387                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3388         fi
3389         $TRACE ls . && error "'ls .' worked after removing cwd"
3390         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3391         is_patchless || ( $TRACE cd . &&
3392                         error "'cd .' worked after removing cwd" )
3393         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3394         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3395         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3396         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3397 }
3398 run_test 48c "Access removed working subdir (should return errors)"
3399
3400 test_48d() { # bug 2350
3401         check_kernel_version 36 || return 0
3402         #lctl set_param debug=-1
3403         #set -vx
3404         rm -rf $DIR/$tdir
3405         test_mkdir -p $DIR/$tdir/dir
3406         cd $DIR/$tdir/dir
3407         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3408         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3409         $TRACE touch foo && error "'touch foo' worked after removing parent"
3410         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3411         if check_kernel_version 44; then
3412                 touch .foo && error "'touch .foo' worked after removing parent"
3413                 test_mkdir .foo &&
3414                               error "mkdir .foo worked after removing parent"
3415         fi
3416         $TRACE ls . && error "'ls .' worked after removing parent"
3417         $TRACE ls .. && error "'ls ..' worked after removing parent"
3418         is_patchless || ( $TRACE cd . &&
3419                         error "'cd .' worked after recreate parent" )
3420         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3421         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3422         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3423         is_patchless || ( $TRACE cd .. &&
3424                         error "'cd ..' worked after removing parent" || true )
3425 }
3426 run_test 48d "Access removed parent subdir (should return errors)"
3427
3428 test_48e() { # bug 4134
3429         check_kernel_version 41 || return 0
3430         #lctl set_param debug=-1
3431         #set -vx
3432         rm -rf $DIR/$tdir
3433         test_mkdir -p $DIR/$tdir/dir
3434         cd $DIR/$tdir/dir
3435         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3436         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3437         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3438         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3439         # On a buggy kernel addition of "touch foo" after cd .. will
3440         # produce kernel oops in lookup_hash_it
3441         touch ../foo && error "'cd ..' worked after recreate parent"
3442         cd $DIR
3443         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3444 }
3445 run_test 48e "Access to recreated parent subdir (should return errors)"
3446
3447 test_49() { # LU-1030
3448         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3449         # get ost1 size - lustre-OST0000
3450         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3451         # write 800M at maximum
3452         [ $ost1_size -gt 819200 ] && ost1_size=819200
3453
3454         lfs setstripe -c 1 -i 0 $DIR/$tfile
3455         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3456         local dd_pid=$!
3457
3458         # change max_pages_per_rpc while writing the file
3459         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3460         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3461         # loop until dd process exits
3462         while ps ax -opid | grep -wq $dd_pid; do
3463                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3464                 sleep $((RANDOM % 5 + 1))
3465         done
3466         # restore original max_pages_per_rpc
3467         $LCTL set_param $osc1_mppc=$orig_mppc
3468         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3469 }
3470 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3471
3472 test_50() {
3473         # bug 1485
3474         test_mkdir $DIR/$tdir
3475         cd $DIR/$tdir
3476         ls /proc/$$/cwd || error
3477 }
3478 run_test 50 "special situations: /proc symlinks  ==============="
3479
3480 test_51a() {    # was test_51
3481         # bug 1516 - create an empty entry right after ".." then split dir
3482         test_mkdir -p $DIR/$tdir
3483         touch $DIR/$tdir/foo
3484         $MCREATE $DIR/$tdir/bar
3485         rm $DIR/$tdir/foo
3486         createmany -m $DIR/$tdir/longfile 201
3487         FNUM=202
3488         while [ `ls -sd $DIR/$tdir | awk '{ print $1 }'` -eq 4 ]; do
3489                 $MCREATE $DIR/$tdir/longfile$FNUM
3490                 FNUM=$(($FNUM + 1))
3491                 echo -n "+"
3492         done
3493         echo
3494         ls -l $DIR/$tdir > /dev/null || error
3495 }
3496 run_test 51a "special situations: split htree with empty entry =="
3497
3498 export NUMTEST=70000
3499 test_51b() {
3500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3501         local BASE=$DIR/$tdir
3502
3503         # cleanup the directory
3504         rm -fr $BASE
3505
3506         test_mkdir -p $BASE
3507
3508         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3509         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3510         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3511                 return
3512
3513         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3514                 echo "reduced count to $NUMTEST due to inodes"
3515
3516         # need to check free space for the directories as well
3517         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3518         numfree=$((blkfree / 4))
3519         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3520                 echo "reduced count to $NUMTEST due to blocks"
3521
3522         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3523                 echo "failed" > $BASE/fnum
3524 }
3525 run_test 51b "exceed 64k subdirectory nlink limit"
3526
3527 test_51ba() { # LU-993
3528         local BASE=$DIR/$tdir
3529         # unlink all but 100 subdirectories, then check it still works
3530         local LEFT=100
3531         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3532
3533         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3534         local DELETE=$((NUMTEST - LEFT))
3535
3536         # continue on to run this test even if 51b didn't finish,
3537         # just to delete the many subdirectories created.
3538         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3539
3540         # for ldiskfs the nlink count should be 1, but this is OSD specific
3541         # and so this is listed for informational purposes only
3542         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3543         unlinkmany -d $BASE/d $DELETE
3544         RC=$?
3545
3546         if [ $RC -ne 0 ]; then
3547                 if [ "$NUMPREV" == "failed" ]; then
3548                         skip "previous setup failed"
3549                         return 0
3550                 else
3551                         error "unlink of first $DELETE subdirs failed"
3552                         return $RC
3553                 fi
3554         fi
3555
3556         echo "nlink between: $(stat -c %h $BASE)"
3557         # trim the first line of ls output
3558         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3559         [ $FOUND -ne $LEFT ] &&
3560                 error "can't find subdirs: found only $FOUND/$LEFT"
3561
3562         unlinkmany -d $BASE/d $DELETE $LEFT ||
3563                 error "unlink of second $LEFT subdirs failed"
3564         # regardless of whether the backing filesystem tracks nlink accurately
3565         # or not, the nlink count shouldn't be more than "." and ".." here
3566         local AFTER=$(stat -c %h $BASE)
3567         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3568                 echo "nlink after: $AFTER"
3569 }
3570 run_test 51ba "verify nlink for many subdirectory cleanup"
3571
3572 test_51d() {
3573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3574         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3575         test_mkdir -p $DIR/$tdir
3576         createmany -o $DIR/$tdir/t- 1000
3577         $GETSTRIPE $DIR/$tdir > $TMP/files
3578         for N in `seq 0 $((OSTCOUNT - 1))`; do
3579             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3580             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3581             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3582         done
3583         unlinkmany $DIR/$tdir/t- 1000
3584
3585         NLAST=0
3586         for N in `seq 1 $((OSTCOUNT - 1))`; do
3587             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3588                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3589             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3590                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3591
3592             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3593                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3594             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3595                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3596             NLAST=$N
3597         done
3598 }
3599 run_test 51d "check object distribution ===================="
3600
3601 test_52a() {
3602         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3603         test_mkdir -p $DIR/$tdir
3604         touch $DIR/$tdir/foo
3605         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3606         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3607         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3608         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3609         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3610                                         error "link worked"
3611         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3612         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3613         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3614                                                      error "lsattr"
3615         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3616         cp -r $DIR/$tdir /tmp/
3617         rm -fr $DIR/$tdir || error "cleanup rm failed"
3618 }
3619 run_test 52a "append-only flag test (should return errors) ====="
3620
3621 test_52b() {
3622         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3623         test_mkdir -p $DIR/$tdir
3624         touch $DIR/$tdir/foo
3625         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3626         cat test > $DIR/$tdir/foo && error "cat test worked"
3627         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3628         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3629         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3630                                         error "link worked"
3631         echo foo >> $DIR/$tdir/foo && error "echo worked"
3632         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3633         [ -f $DIR/$tdir/foo ] || error
3634         [ -f $DIR/$tdir/foo_ren ] && error
3635         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3636                                                         error "lsattr"
3637         chattr -i $DIR/$tdir/foo || error "chattr failed"
3638
3639         rm -fr $DIR/$tdir || error
3640 }
3641 run_test 52b "immutable flag test (should return errors) ======="
3642
3643 test_53() {
3644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3645         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3646         remote_ost_nodsh && skip "remote OST with nodsh" && return
3647
3648         local param
3649         local ostname
3650         local mds_last
3651         local ost_last
3652         local ostnum
3653         local node
3654
3655         # only test MDT0000
3656         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3657         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3658                 param=`echo ${value[0]} | cut -d "=" -f1`
3659                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3660                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3661                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
3662                 node=$(facet_active_host ost$((ostnum+1)))
3663                 param="obdfilter.$ostname.last_id"
3664                 ost_last=$(do_node $node lctl get_param -n $param | head -n 1)
3665                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3666                 if [ $ost_last != $mds_last ]; then
3667                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3668                 fi
3669         done
3670 }
3671 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3672
3673 test_54a() {
3674         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3675         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3676         $SOCKETSERVER $DIR/socket
3677         $SOCKETCLIENT $DIR/socket || error
3678         $MUNLINK $DIR/socket
3679 }
3680 run_test 54a "unix domain socket test =========================="
3681
3682 test_54b() {
3683         f="$DIR/f54b"
3684         mknod $f c 1 3
3685         chmod 0666 $f
3686         dd if=/dev/zero of=$f bs=`page_size` count=1
3687 }
3688 run_test 54b "char device works in lustre ======================"
3689
3690 find_loop_dev() {
3691         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3692         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3693         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3694
3695         for i in `seq 3 7`; do
3696                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3697                 LOOPDEV=$LOOPBASE$i
3698                 LOOPNUM=$i
3699                 break
3700         done
3701 }
3702
3703 test_54c() {
3704         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3705         tfile="$DIR/f54c"
3706         tdir="$DIR/d54c"
3707         loopdev="$DIR/loop54c"
3708
3709         find_loop_dev
3710         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3711         mknod $loopdev b 7 $LOOPNUM
3712         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3713         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3714         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3715         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3716         test_mkdir -p $tdir
3717         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3718         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3719         df $tdir
3720         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3721         $UMOUNT $tdir
3722         losetup -d $loopdev
3723         rm $loopdev
3724 }
3725 run_test 54c "block device works in lustre ====================="
3726
3727 test_54d() {
3728         f="$DIR/f54d"
3729         string="aaaaaa"
3730         mknod $f p
3731         [ "$string" = `echo $string > $f | cat $f` ] || error
3732 }
3733 run_test 54d "fifo device works in lustre ======================"
3734
3735 test_54e() {
3736         check_kernel_version 46 || return 0
3737         f="$DIR/f54e"
3738         string="aaaaaa"
3739         cp -aL /dev/console $f
3740         echo $string > $f || error
3741 }
3742 run_test 54e "console/tty device works in lustre ======================"
3743
3744 #The test_55 used to be iopen test and it was removed by bz#24037.
3745 #run_test 55 "check iopen_connect_dentry() ======================"
3746
3747 test_56a() {    # was test_56
3748         rm -rf $DIR/$tdir
3749         $SETSTRIPE -d $DIR
3750         test_mkdir $DIR/$tdir
3751         test_mkdir $DIR/$tdir/dir
3752         NUMFILES=3
3753         NUMFILESx2=$(($NUMFILES * 2))
3754         for i in `seq 1 $NUMFILES` ; do
3755                 touch $DIR/$tdir/file$i
3756                 touch $DIR/$tdir/dir/file$i
3757         done
3758
3759         # test lfs getstripe with --recursive
3760         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
3761         [ $FILENUM -eq $NUMFILESx2 ] ||
3762                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3763         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
3764         [ $FILENUM -eq $NUMFILES ] ||
3765                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
3766         echo "$GETSTRIPE --recursive passed."
3767
3768         # test lfs getstripe with file instead of dir
3769         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
3770         [ $FILENUM  -eq 1 ] || error \
3771                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
3772         echo "$GETSTRIPE file1 passed."
3773
3774         #test lfs getstripe with --verbose
3775         [ `$GETSTRIPE --verbose $DIR/$tdir |
3776                         grep -c lmm_magic` -eq $NUMFILES ] ||
3777                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
3778         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
3779             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
3780         echo "$GETSTRIPE --verbose passed."
3781
3782         #test lfs getstripe with --obd
3783         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
3784                                         grep -q "unknown obduuid" ||
3785                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3786
3787         [  "$OSTCOUNT" -lt 2 ] &&
3788                 skip_env "skipping other $GETSTRIPE --obd test" && return
3789
3790         OSTIDX=1
3791         OBDUUID=$(ostuuid_from_index $OSTIDX)
3792         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
3793         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
3794         [ $FOUND -eq $FILENUM ] ||
3795                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3796         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
3797                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3798                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3799                 error "$GETSTRIPE --obd: should not show file on other obd"
3800         echo "$GETSTRIPE --obd passed"
3801 }
3802 run_test 56a "check $GETSTRIPE"
3803
3804 NUMFILES=3
3805 NUMDIRS=3
3806 setup_56() {
3807         local LOCAL_NUMFILES="$1"
3808         local LOCAL_NUMDIRS="$2"
3809         local MKDIR_PARAMS="$3"
3810
3811         if [ ! -d "$TDIR" ] ; then
3812                 test_mkdir -p $TDIR
3813                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3814                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3815                         touch $TDIR/file$i
3816                 done
3817                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3818                         test_mkdir $TDIR/dir$i
3819                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3820                                 touch $TDIR/dir$i/file$j
3821                         done
3822                 done
3823         fi
3824 }
3825
3826 setup_56_special() {
3827         LOCAL_NUMFILES=$1
3828         LOCAL_NUMDIRS=$2
3829         setup_56 $1 $2
3830         if [ ! -e "$TDIR/loop1b" ] ; then
3831                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3832                         mknod $TDIR/loop${i}b b 7 $i
3833                         mknod $TDIR/null${i}c c 1 3
3834                         ln -s $TDIR/file1 $TDIR/link${i}l
3835                 done
3836                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3837                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3838                         mknod $TDIR/dir$i/null${i}c c 1 3
3839                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3840                 done
3841         fi
3842 }
3843
3844 test_56g() {
3845         $SETSTRIPE -d $DIR
3846
3847         TDIR=$DIR/${tdir}g
3848         setup_56 $NUMFILES $NUMDIRS
3849
3850         EXPECTED=$(($NUMDIRS + 2))
3851         # test lfs find with -name
3852         for i in $(seq 1 $NUMFILES) ; do
3853                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3854                 [ $NUMS -eq $EXPECTED ] ||
3855                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3856                               "found $NUMS, expected $EXPECTED"
3857         done
3858 }
3859 run_test 56g "check lfs find -name ============================="
3860
3861 test_56h() {
3862         $SETSTRIPE -d $DIR
3863
3864         TDIR=$DIR/${tdir}g
3865         setup_56 $NUMFILES $NUMDIRS
3866
3867         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3868         # test lfs find with ! -name
3869         for i in $(seq 1 $NUMFILES) ; do
3870                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3871                 [ $NUMS -eq $EXPECTED ] ||
3872                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3873                               "found $NUMS, expected $EXPECTED"
3874         done
3875 }
3876 run_test 56h "check lfs find ! -name ============================="
3877
3878 test_56i() {
3879        tdir=${tdir}i
3880        test_mkdir -p $DIR/$tdir
3881        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3882        CMD="$LFIND -ost $UUID $DIR/$tdir"
3883        OUT=$($CMD)
3884        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3885 }
3886 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3887
3888 test_56j() {
3889         TDIR=$DIR/${tdir}g
3890         setup_56_special $NUMFILES $NUMDIRS
3891
3892         EXPECTED=$((NUMDIRS + 1))
3893         CMD="$LFIND -type d $TDIR"
3894         NUMS=$($CMD | wc -l)
3895         [ $NUMS -eq $EXPECTED ] ||
3896                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3897 }
3898 run_test 56j "check lfs find -type d ============================="
3899
3900 test_56k() {
3901         TDIR=$DIR/${tdir}g
3902         setup_56_special $NUMFILES $NUMDIRS
3903
3904         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3905         CMD="$LFIND -type f $TDIR"
3906         NUMS=$($CMD | wc -l)
3907         [ $NUMS -eq $EXPECTED ] ||
3908                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3909 }
3910 run_test 56k "check lfs find -type f ============================="
3911
3912 test_56l() {
3913         TDIR=$DIR/${tdir}g
3914         setup_56_special $NUMFILES $NUMDIRS
3915
3916         EXPECTED=$((NUMDIRS + NUMFILES))
3917         CMD="$LFIND -type b $TDIR"
3918         NUMS=$($CMD | wc -l)
3919         [ $NUMS -eq $EXPECTED ] ||
3920                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3921 }
3922 run_test 56l "check lfs find -type b ============================="
3923
3924 test_56m() {
3925         TDIR=$DIR/${tdir}g
3926         setup_56_special $NUMFILES $NUMDIRS
3927
3928         EXPECTED=$((NUMDIRS + NUMFILES))
3929         CMD="$LFIND -type c $TDIR"
3930         NUMS=$($CMD | wc -l)
3931         [ $NUMS -eq $EXPECTED ] ||
3932                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3933 }
3934 run_test 56m "check lfs find -type c ============================="
3935
3936 test_56n() {
3937         TDIR=$DIR/${tdir}g
3938         setup_56_special $NUMFILES $NUMDIRS
3939
3940         EXPECTED=$((NUMDIRS + NUMFILES))
3941         CMD="$LFIND -type l $TDIR"
3942         NUMS=$($CMD | wc -l)
3943         [ $NUMS -eq $EXPECTED ] ||
3944                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3945 }
3946 run_test 56n "check lfs find -type l ============================="
3947
3948 test_56o() {
3949         TDIR=$DIR/${tdir}o
3950         setup_56 $NUMFILES $NUMDIRS
3951
3952         utime $TDIR/file1 > /dev/null || error "utime (1)"
3953         utime $TDIR/file2 > /dev/null || error "utime (2)"
3954         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3955         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3956         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3957         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3958
3959         EXPECTED=4
3960         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3961         [ $NUMS -eq $EXPECTED ] || \
3962                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3963
3964         EXPECTED=12
3965         CMD="$LFIND -mtime 0 $TDIR"
3966         NUMS=$($CMD | wc -l)
3967         [ $NUMS -eq $EXPECTED ] ||
3968                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3969 }
3970 run_test 56o "check lfs find -mtime for old files =========================="
3971
3972 test_56p() {
3973         [ $RUNAS_ID -eq $UID ] &&
3974                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3975
3976         TDIR=$DIR/${tdir}p
3977         setup_56 $NUMFILES $NUMDIRS
3978
3979         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3980         EXPECTED=$NUMFILES
3981         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3982         NUMS=$($CMD | wc -l)
3983         [ $NUMS -eq $EXPECTED ] || \
3984                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3985
3986         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3987         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3988         NUMS=$($CMD | wc -l)
3989         [ $NUMS -eq $EXPECTED ] || \
3990                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3991 }
3992 run_test 56p "check lfs find -uid and ! -uid ==============================="
3993
3994 test_56q() {
3995         [ $RUNAS_ID -eq $UID ] &&
3996                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3997
3998         TDIR=$DIR/${tdir}q
3999         setup_56 $NUMFILES $NUMDIRS
4000
4001         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4002
4003         EXPECTED=$NUMFILES
4004         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4005         NUMS=$($CMD | wc -l)
4006         [ $NUMS -eq $EXPECTED ] ||
4007                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4008
4009         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4010         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4011         NUMS=$($CMD | wc -l)
4012         [ $NUMS -eq $EXPECTED ] ||
4013                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4014 }
4015 run_test 56q "check lfs find -gid and ! -gid ==============================="
4016
4017 test_56r() {
4018         TDIR=$DIR/${tdir}r
4019         setup_56 $NUMFILES $NUMDIRS
4020
4021         EXPECTED=12
4022         CMD="$LFIND -size 0 -type f $TDIR"
4023         NUMS=$($CMD | wc -l)
4024         [ $NUMS -eq $EXPECTED ] ||
4025                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4026         EXPECTED=0
4027         CMD="$LFIND ! -size 0 -type f $TDIR"
4028         NUMS=$($CMD | wc -l)
4029         [ $NUMS -eq $EXPECTED ] ||
4030                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4031         echo "test" > $TDIR/$tfile
4032         echo "test2" > $TDIR/$tfile.2 && sync
4033         EXPECTED=1
4034         CMD="$LFIND -size 5 -type f $TDIR"
4035         NUMS=$($CMD | wc -l)
4036         [ $NUMS -eq $EXPECTED ] ||
4037                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4038         EXPECTED=1
4039         CMD="$LFIND -size +5 -type f $TDIR"
4040         NUMS=$($CMD | wc -l)
4041         [ $NUMS -eq $EXPECTED ] ||
4042                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4043         EXPECTED=2
4044         CMD="$LFIND -size +0 -type f $TDIR"
4045         NUMS=$($CMD | wc -l)
4046         [ $NUMS -eq $EXPECTED ] ||
4047                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4048         EXPECTED=2
4049         CMD="$LFIND ! -size -5 -type f $TDIR"
4050         NUMS=$($CMD | wc -l)
4051         [ $NUMS -eq $EXPECTED ] ||
4052                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4053         EXPECTED=12
4054         CMD="$LFIND -size -5 -type f $TDIR"
4055         NUMS=$($CMD | wc -l)
4056         [ $NUMS -eq $EXPECTED ] ||
4057                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4058 }
4059 run_test 56r "check lfs find -size works =========================="
4060
4061 test_56s() { # LU-611
4062         TDIR=$DIR/${tdir}s
4063         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4064
4065         if [ $OSTCOUNT -gt 1 ]; then
4066                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4067                 ONESTRIPE=4
4068                 EXTRA=4
4069         else
4070                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4071                 EXTRA=0
4072         fi
4073
4074         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4075         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4076         NUMS=$($CMD | wc -l)
4077         [ $NUMS -eq $EXPECTED ] ||
4078                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4079
4080         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4081         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4082         NUMS=$($CMD | wc -l)
4083         [ $NUMS -eq $EXPECTED ] ||
4084                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4085
4086         EXPECTED=$ONESTRIPE
4087         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4088         NUMS=$($CMD | wc -l)
4089         [ $NUMS -eq $EXPECTED ] ||
4090                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4091
4092         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4093         NUMS=$($CMD | wc -l)
4094         [ $NUMS -eq $EXPECTED ] ||
4095                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4096
4097         EXPECTED=0
4098         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4099         NUMS=$($CMD | wc -l)
4100         [ $NUMS -eq $EXPECTED ] ||
4101                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4102 }
4103 run_test 56s "check lfs find -stripe-count works"
4104
4105 test_56t() { # LU-611
4106         TDIR=$DIR/${tdir}t
4107         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4108
4109         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4110
4111         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4112         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4113         NUMS=$($CMD | wc -l)
4114         [ $NUMS -eq $EXPECTED ] ||
4115                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4116
4117         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4118         NUMS=$($CMD | wc -l)
4119         [ $NUMS -eq $EXPECTED ] ||
4120                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4121
4122         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4123         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4124         NUMS=$($CMD | wc -l)
4125         [ $NUMS -eq $EXPECTED ] ||
4126                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4127
4128         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4129         NUMS=$($CMD | wc -l)
4130         [ $NUMS -eq $EXPECTED ] ||
4131                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4132
4133         EXPECTED=4
4134         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4135         NUMS=$($CMD | wc -l)
4136         [ $NUMS -eq $EXPECTED ] ||
4137                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4138
4139         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4140         NUMS=$($CMD | wc -l)
4141         [ $NUMS -eq $EXPECTED ] ||
4142                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4143
4144         EXPECTED=0
4145         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4146         NUMS=$($CMD | wc -l)
4147         [ $NUMS -eq $EXPECTED ] ||
4148                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4149 }
4150 run_test 56t "check lfs find -stripe-size works"
4151
4152 test_56u() { # LU-611
4153         TDIR=$DIR/${tdir}u
4154         setup_56 $NUMFILES $NUMDIRS "-i 0"
4155
4156         if [ $OSTCOUNT -gt 1 ]; then
4157                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4158                 ONESTRIPE=4
4159         else
4160                 ONESTRIPE=0
4161         fi
4162
4163         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4164         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4165         NUMS=$($CMD | wc -l)
4166         [ $NUMS -eq $EXPECTED ] ||
4167                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4168
4169         EXPECTED=$ONESTRIPE
4170         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4171         NUMS=$($CMD | wc -l)
4172         [ $NUMS -eq $EXPECTED ] ||
4173                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4174
4175         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4176         NUMS=$($CMD | wc -l)
4177         [ $NUMS -eq $EXPECTED ] ||
4178                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4179
4180         EXPECTED=0
4181         # This should produce an error and not return any files
4182         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4183         NUMS=$($CMD 2>/dev/null | wc -l)
4184         [ $NUMS -eq $EXPECTED ] ||
4185                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4186
4187         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4188         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4189         NUMS=$($CMD | wc -l)
4190         [ $NUMS -eq $EXPECTED ] ||
4191                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4192 }
4193 run_test 56u "check lfs find -stripe-index works"
4194
4195 test_56v() {
4196     local MDT_IDX=0
4197
4198     TDIR=$DIR/${tdir}v
4199     rm -rf $TDIR
4200     setup_56 $NUMFILES $NUMDIRS
4201
4202     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4203     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4204
4205     for file in $($LFIND -mdt $UUID $TDIR); do
4206         file_mdt_idx=$($GETSTRIPE -M $file)
4207         [ $file_mdt_idx -eq $MDT_IDX ] ||
4208             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4209     done
4210 }
4211 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4212
4213 # Get and check the actual stripe count of one file.
4214 # Usage: check_stripe_count <file> <expected_stripe_count>
4215 check_stripe_count() {
4216     local file=$1
4217     local expected=$2
4218     local actual
4219
4220     [[ -z "$file" || -z "$expected" ]] &&
4221         error "check_stripe_count: invalid argument!"
4222
4223     local cmd="$GETSTRIPE -c $file"
4224     actual=$($cmd) || error "$cmd failed"
4225     actual=${actual%% *}
4226
4227     if [[ $actual -ne $expected ]]; then
4228         [[ $expected -eq -1 ]] ||
4229             error "$cmd wrong: found $actual, expected $expected"
4230         [[ $actual -eq $OSTCOUNT ]] ||
4231             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4232     fi
4233 }
4234
4235 test_56w() {
4236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4237         TDIR=$DIR/${tdir}w
4238
4239     rm -rf $TDIR || error "remove $TDIR failed"
4240     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4241
4242     local stripe_size
4243     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4244         error "$GETSTRIPE -S -d $TDIR failed"
4245     stripe_size=${stripe_size%% *}
4246
4247     local file_size=$((stripe_size * OSTCOUNT))
4248     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4249     local required_space=$((file_num * file_size))
4250     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4251     [[ $free_space -le $((required_space / 1024)) ]] &&
4252         skip_env "need at least $required_space bytes free space," \
4253                  "have $free_space kbytes" && return
4254
4255     local dd_bs=65536
4256     local dd_count=$((file_size / dd_bs))
4257
4258     # write data into the files
4259     local i
4260     local j
4261     local file
4262     for i in $(seq 1 $NUMFILES); do
4263         file=$TDIR/file$i
4264         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4265             error "write data into $file failed"
4266     done
4267     for i in $(seq 1 $NUMDIRS); do
4268         for j in $(seq 1 $NUMFILES); do
4269             file=$TDIR/dir$i/file$j
4270             yes | dd bs=$dd_bs count=$dd_count of=$file \
4271                 >/dev/null 2>&1 ||
4272                 error "write data into $file failed"
4273         done
4274     done
4275
4276     local expected=-1
4277     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4278
4279     # lfs_migrate file
4280     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4281     echo "$cmd"
4282     eval $cmd || error "$cmd failed"
4283
4284     check_stripe_count $TDIR/file1 $expected
4285
4286     # lfs_migrate dir
4287     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4288     echo "$cmd"
4289     eval $cmd || error "$cmd failed"
4290
4291     for j in $(seq 1 $NUMFILES); do
4292         check_stripe_count $TDIR/dir1/file$j $expected
4293     done
4294
4295     # lfs_migrate works with lfs find
4296     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4297          $LFS_MIGRATE -y -c $expected"
4298     echo "$cmd"
4299     eval $cmd || error "$cmd failed"
4300
4301     for i in $(seq 2 $NUMFILES); do
4302         check_stripe_count $TDIR/file$i $expected
4303     done
4304     for i in $(seq 2 $NUMDIRS); do
4305         for j in $(seq 1 $NUMFILES); do
4306             check_stripe_count $TDIR/dir$i/file$j $expected
4307         done
4308     done
4309 }
4310 run_test 56w "check lfs_migrate -c stripe_count works"
4311
4312 test_57a() {
4313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4314         # note test will not do anything if MDS is not local
4315         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4316                 skip "Only applicable to ldiskfs-based MDTs"
4317                 return
4318         fi
4319
4320         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4321         local MNTDEV="osd*.*MDT*.mntdev"
4322         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4323         [ -z "$DEV" ] && error "can't access $MNTDEV"
4324         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4325                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4326                         error "can't access $DEV"
4327                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4328                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4329                 rm $TMP/t57a.dump
4330         done
4331 }
4332 run_test 57a "verify MDS filesystem created with large inodes =="
4333
4334 test_57b() {
4335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4336         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4337                 skip "Only applicable to ldiskfs-based MDTs"
4338                 return
4339         fi
4340
4341         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4342         local dir=$DIR/d57b
4343
4344         local FILECOUNT=100
4345         local FILE1=$dir/f1
4346         local FILEN=$dir/f$FILECOUNT
4347
4348         rm -rf $dir || error "removing $dir"
4349         test_mkdir -p $dir || error "creating $dir"
4350         local num=$(get_mds_dir $dir)
4351         local mymds=mds$num
4352
4353         echo "mcreating $FILECOUNT files"
4354         createmany -m $dir/f 1 $FILECOUNT || \
4355                 error "creating files in $dir"
4356
4357         # verify that files do not have EAs yet
4358         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4359         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4360
4361         sync
4362         sleep 1
4363         df $dir  #make sure we get new statfs data
4364         local MDSFREE=$(do_facet $mymds \
4365                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4366         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4367         echo "opening files to create objects/EAs"
4368         local FILE
4369         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4370                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4371         done
4372
4373         # verify that files have EAs now
4374         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4375         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4376
4377         sleep 1  #make sure we get new statfs data
4378         df $dir
4379         local MDSFREE2=$(do_facet $mymds \
4380                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4381         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4382         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4383                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4384                         error "MDC before $MDCFREE != after $MDCFREE2"
4385                 else
4386                         echo "MDC before $MDCFREE != after $MDCFREE2"
4387                         echo "unable to confirm if MDS has large inodes"
4388                 fi
4389         fi
4390         rm -rf $dir
4391 }
4392 run_test 57b "default LOV EAs are stored inside large inodes ==="
4393
4394 test_58() {
4395         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4396         [ -z "$(which wiretest 2>/dev/null)" ] &&
4397                         skip_env "could not find wiretest" && return
4398         wiretest
4399 }
4400 run_test 58 "verify cross-platform wire constants =============="
4401
4402 test_59() {
4403         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4404         echo "touch 130 files"
4405         createmany -o $DIR/f59- 130
4406         echo "rm 130 files"
4407         unlinkmany $DIR/f59- 130
4408         sync
4409         # wait for commitment of removal
4410         wait_delete_completed
4411 }
4412 run_test 59 "verify cancellation of llog records async ========="
4413
4414 TEST60_HEAD="test_60 run $RANDOM"
4415 test_60a() {
4416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4417         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4418         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4419         log "$TEST60_HEAD - from kernel mode"
4420         do_facet mgs sh run-llog.sh
4421 }
4422 run_test 60a "llog sanity tests run from kernel module =========="
4423
4424 test_60b() { # bug 6411
4425         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4426         dmesg > $DIR/$tfile
4427         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4428                                  /llog.test/ {
4429                                          if (marker)
4430                                                  from_marker++
4431                                          from_begin++
4432                                  }
4433                                  END {
4434                                          if (marker)
4435                                                  print from_marker
4436                                          else
4437                                                  print from_begin
4438                                  }"`
4439         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4440 }
4441 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4442
4443 test_60c() {
4444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4445         echo "create 5000 files"
4446         createmany -o $DIR/f60c- 5000
4447 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4448         lctl set_param fail_loc=0x80000137
4449         unlinkmany $DIR/f60c- 5000
4450         lctl set_param fail_loc=0
4451 }
4452 run_test 60c "unlink file when mds full"
4453
4454 test_60d() {
4455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4456         SAVEPRINTK=$(lctl get_param -n printk)
4457
4458         # verify "lctl mark" is even working"
4459         MESSAGE="test message ID $RANDOM $$"
4460         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4461         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4462
4463         lctl set_param printk=0 || error "set lnet.printk failed"
4464         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4465         MESSAGE="new test message ID $RANDOM $$"
4466         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4467         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4468         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4469
4470         lctl set_param -n printk="$SAVEPRINTK"
4471 }
4472 run_test 60d "test printk console message masking"
4473
4474 test_61() {
4475         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4476         f="$DIR/f61"
4477         dd if=/dev/zero of=$f bs=`page_size` count=1
4478         cancel_lru_locks osc
4479         $MULTIOP $f OSMWUc || error
4480         sync
4481 }
4482 run_test 61 "mmap() writes don't make sync hang ================"
4483
4484 # bug 2330 - insufficient obd_match error checking causes LBUG
4485 test_62() {
4486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4487         f="$DIR/f62"
4488         echo foo > $f
4489         cancel_lru_locks osc
4490         lctl set_param fail_loc=0x405
4491         cat $f && error "cat succeeded, expect -EIO"
4492         lctl set_param fail_loc=0
4493 }
4494 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4495 # match every page all of the time.
4496 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4497
4498 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4499 test_63a() {    # was test_63
4500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4501         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4502         lctl set_param -n osc.*.max_dirty_mb 0
4503         for i in `seq 10` ; do
4504                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4505                 sleep 5
4506                 kill $!
4507                 sleep 1
4508         done
4509
4510         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4511         rm -f $DIR/f63 || true
4512 }
4513 run_test 63a "Verify oig_wait interruption does not crash ======="
4514
4515 # bug 2248 - async write errors didn't return to application on sync
4516 # bug 3677 - async write errors left page locked
4517 test_63b() {
4518         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4519         debugsave
4520         lctl set_param debug=-1
4521
4522         # ensure we have a grant to do async writes
4523         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4524         rm $DIR/$tfile
4525
4526         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4527         lctl set_param fail_loc=0x80000406
4528         $MULTIOP $DIR/$tfile Owy && \
4529                 error "sync didn't return ENOMEM"
4530         sync; sleep 2; sync     # do a real sync this time to flush page
4531         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4532                 error "locked page left in cache after async error" || true
4533         debugrestore
4534 }
4535 run_test 63b "async write errors should be returned to fsync ==="
4536
4537 test_64a () {
4538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4539         df $DIR
4540         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4541 }
4542 run_test 64a "verify filter grant calculations (in kernel) ====="
4543
4544 test_64b () {
4545         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4546         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4547         sh oos.sh $MOUNT
4548 }
4549 run_test 64b "check out-of-space detection on client ==========="
4550
4551 test_64c() {
4552         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4553 }
4554 run_test 64c "verify grant shrink ========================------"
4555
4556 # bug 1414 - set/get directories' stripe info
4557 test_65a() {
4558         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4559         test_mkdir -p $DIR/$tdir
4560         touch $DIR/$tdir/f1
4561         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4562 }
4563 run_test 65a "directory with no stripe info ===================="
4564
4565 test_65b() {
4566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4567         test_mkdir -p $DIR/$tdir
4568         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4569                                                 error "setstripe"
4570         touch $DIR/$tdir/f2
4571         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4572 }
4573 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4574
4575 test_65c() {
4576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4577         if [ $OSTCOUNT -gt 1 ]; then
4578                 test_mkdir -p $DIR/$tdir
4579                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4580                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4581                 touch $DIR/$tdir/f3
4582                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4583         fi
4584 }
4585 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4586
4587 test_65d() {
4588         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4589         test_mkdir -p $DIR/$tdir
4590         if [ $STRIPECOUNT -le 0 ]; then
4591                 sc=1
4592         elif [ $STRIPECOUNT -gt 2000 ]; then
4593 #LOV_MAX_STRIPE_COUNT is 2000
4594                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4595         else
4596                 sc=$(($STRIPECOUNT - 1))
4597         fi
4598         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4599         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4600         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4601                                                 error "lverify failed"
4602 }
4603 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4604
4605 test_65e() {
4606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4607         test_mkdir -p $DIR/$tdir
4608
4609         $SETSTRIPE $DIR/$tdir || error "setstripe"
4610         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4611                                         error "no stripe info failed"
4612         touch $DIR/$tdir/f6
4613         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4614 }
4615 run_test 65e "directory setstripe defaults ======================="
4616
4617 test_65f() {
4618         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4619         test_mkdir -p $DIR/${tdir}f
4620         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4621 }
4622 run_test 65f "dir setstripe permission (should return error) ==="
4623
4624 test_65g() {
4625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4626         test_mkdir -p $DIR/$tdir
4627         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4628                                                         error "setstripe"
4629         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
4630         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4631                 error "delete default stripe failed"
4632 }
4633 run_test 65g "directory setstripe -d ==========================="
4634
4635 test_65h() {
4636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4637         test_mkdir -p $DIR/$tdir
4638         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4639                                                         error "setstripe"
4640         test_mkdir -p $DIR/$tdir/dd1
4641         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
4642                 error "stripe info inherit failed"
4643 }
4644 run_test 65h "directory stripe info inherit ===================="
4645
4646 test_65i() { # bug6367
4647         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4648         $SETSTRIPE -S 65536 -c -1 $MOUNT
4649 }
4650 run_test 65i "set non-default striping on root directory (bug 6367)="
4651
4652 test_65ia() { # bug12836
4653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4654         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4655 }
4656 run_test 65ia "getstripe on -1 default directory striping"
4657
4658 test_65ib() { # bug12836
4659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4660         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4661 }
4662 run_test 65ib "getstripe -v on -1 default directory striping"
4663
4664 test_65ic() { # bug12836
4665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4666         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4667 }
4668 run_test 65ic "new find on -1 default directory striping"
4669
4670 test_65j() { # bug6367
4671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4672         sync; sleep 1
4673         # if we aren't already remounting for each test, do so for this test
4674         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4675                 cleanup || error "failed to unmount"
4676                 setup
4677         fi
4678         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4679 }
4680 run_test 65j "set default striping on root directory (bug 6367)="
4681
4682 test_65k() { # bug11679
4683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4684         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4685         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4686
4687     echo "Check OST status: "
4688     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4689               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4690
4691     for OSC in $MDS_OSCS; do
4692         echo $OSC "is activate"
4693         do_facet $SINGLEMDS lctl --device %$OSC activate
4694     done
4695
4696     mkdir -p $DIR/$tdir
4697     for INACTIVE_OSC in $MDS_OSCS; do
4698         echo "Deactivate: " $INACTIVE_OSC
4699         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4700         for STRIPE_OSC in $MDS_OSCS; do
4701             OST=`osc_to_ost $STRIPE_OSC`
4702             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4703                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4704
4705             [ -f $DIR/$tdir/$IDX ] && continue
4706             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4707             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4708             RC=$?
4709             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4710         done
4711         rm -f $DIR/$tdir/*
4712         echo $INACTIVE_OSC "is Activate."
4713         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4714     done
4715 }
4716 run_test 65k "validate manual striping works properly with deactivated OSCs"
4717
4718 test_65l() { # bug 12836
4719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4720         test_mkdir -p $DIR/$tdir/test_dir
4721         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4722         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4723 }
4724 run_test 65l "lfs find on -1 stripe dir ========================"
4725
4726 # bug 2543 - update blocks count on client
4727 test_66() {
4728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4729         COUNT=${COUNT:-8}
4730         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4731         sync; sync_all_data; sync; sync_all_data
4732         cancel_lru_locks osc
4733         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4734         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4735 }
4736 run_test 66 "update inode blocks count on client ==============="
4737
4738 LLOOP=
4739 LLITELOOPLOAD=
4740 cleanup_68() {
4741         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4742         trap 0
4743         if [ ! -z "$LLOOP" ]; then
4744                 if swapon -s | grep -q $LLOOP; then
4745                         swapoff $LLOOP || error "swapoff failed"
4746                 fi
4747
4748                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4749                 rm -f $LLOOP
4750                 unset LLOOP
4751         fi
4752         if [ ! -z "$LLITELOOPLOAD" ]; then
4753                 rmmod llite_lloop
4754                 unset LLITELOOPLOAD
4755         fi
4756         rm -f $DIR/f68*
4757 }
4758
4759 meminfo() {
4760         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4761 }
4762
4763 swap_used() {
4764         swapon -s | awk '($1 == "'$1'") { print $4 }'
4765 }
4766
4767 # test case for lloop driver, basic function
4768 test_68a() {
4769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4770         [ "$UID" != 0 ] && skip_env "must run as root" && return
4771         llite_lloop_enabled || \
4772                 { skip_env "llite_lloop module disabled" && return; }
4773
4774         trap cleanup_68 EXIT
4775
4776         if ! module_loaded llite_lloop; then
4777                 if load_module llite/llite_lloop; then
4778                         LLITELOOPLOAD=yes
4779                 else
4780                         skip_env "can't find module llite_lloop"
4781                         return
4782                 fi
4783         fi
4784
4785         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4786         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4787         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4788
4789         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4790         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4791
4792         cleanup_68
4793 }
4794 run_test 68a "lloop driver - basic test ========================"
4795
4796 # excercise swapping to lustre by adding a high priority swapfile entry
4797 # and then consuming memory until it is used.
4798 test_68b() {  # was test_68
4799         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4800         [ "$UID" != 0 ] && skip_env "must run as root" && return
4801         lctl get_param -n devices | grep -q obdfilter && \
4802                 skip "local OST" && return
4803
4804         grep -q llite_lloop /proc/modules
4805         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4806
4807         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4808                 skip "can't reliably test swap with TCP" && return
4809
4810         MEMTOTAL=`meminfo MemTotal`
4811         NR_BLOCKS=$((MEMTOTAL>>8))
4812         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4813
4814         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4815         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4816         mkswap $DIR/f68b
4817
4818         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4819
4820         trap cleanup_68 EXIT
4821
4822         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4823
4824         echo "before: `swapon -s | grep $LLOOP`"
4825         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4826         echo "after: `swapon -s | grep $LLOOP`"
4827         SWAPUSED=`swap_used $LLOOP`
4828
4829         cleanup_68
4830
4831         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4832 }
4833 run_test 68b "support swapping to Lustre ========================"
4834
4835 # bug5265, obdfilter oa2dentry return -ENOENT
4836 # #define OBD_FAIL_OST_ENOENT 0x217
4837 test_69() {
4838         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4839         remote_ost_nodsh && skip "remote OST with nodsh" && return
4840
4841         f="$DIR/$tfile"
4842         $SETSTRIPE -c 1 -i 0 $f
4843
4844         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4845
4846         do_facet ost1 lctl set_param fail_loc=0x217
4847         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4848         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4849
4850         do_facet ost1 lctl set_param fail_loc=0
4851         $DIRECTIO write $f 0 2 || error "write error"
4852
4853         cancel_lru_locks osc
4854         $DIRECTIO read $f 0 1 || error "read error"
4855
4856         do_facet ost1 lctl set_param fail_loc=0x217
4857         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4858
4859         do_facet ost1 lctl set_param fail_loc=0
4860         rm -f $f
4861 }
4862 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4863
4864 test_71() {
4865     test_mkdir -p $DIR/$tdir
4866     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4867 }
4868 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4869
4870 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4871         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4872         check_kernel_version 43 || return 0
4873         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4874
4875         # Check that testing environment is properly set up. Skip if not
4876         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4877                 skip_env "User $RUNAS_ID does not exist - skipping"
4878                 return 0
4879         }
4880         # We had better clear the $DIR to get enough space for dd
4881         rm -rf $DIR/*
4882         touch $DIR/f72
4883         chmod 777 $DIR/f72
4884         chmod ug+s $DIR/f72
4885         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4886         # See if we are still setuid/sgid
4887         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4888         # Now test that MDS is updated too
4889         cancel_lru_locks mdc
4890         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4891         rm -f $DIR/f72
4892 }
4893 run_test 72a "Test that remove suid works properly (bug5695) ===="
4894
4895 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4896         local perm
4897
4898         [ "$RUNAS_ID" = "$UID" ] && \
4899                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4900         [ "$RUNAS_ID" -eq 0 ] && \
4901                 skip_env "RUNAS_ID = 0 -- skipping" && return
4902
4903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4904         # Check that testing environment is properly set up. Skip if not
4905         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4906                 skip_env "User $RUNAS_ID does not exist - skipping"
4907                 return 0
4908         }
4909         touch $DIR/${tfile}-f{g,u}
4910         test_mkdir $DIR/${tfile}-dg
4911         test_mkdir $DIR/${tfile}-du
4912         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4913         chmod g+s $DIR/${tfile}-{f,d}g
4914         chmod u+s $DIR/${tfile}-{f,d}u
4915         for perm in 777 2777 4777; do
4916                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4917                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4918                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4919                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4920         done
4921         true
4922 }
4923 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4924
4925 # bug 3462 - multiple simultaneous MDC requests
4926 test_73() {
4927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4928         test_mkdir $DIR/d73-1
4929         test_mkdir $DIR/d73-2
4930         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4931         pid1=$!
4932
4933         lctl set_param fail_loc=0x80000129
4934         $MULTIOP $DIR/d73-1/f73-2 Oc &
4935         sleep 1
4936         lctl set_param fail_loc=0
4937
4938         $MULTIOP $DIR/d73-2/f73-3 Oc &
4939         pid3=$!
4940
4941         kill -USR1 $pid1
4942         wait $pid1 || return 1
4943
4944         sleep 25
4945
4946         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4947         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4948         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4949
4950         rm -rf $DIR/d73-*
4951 }
4952 run_test 73 "multiple MDC requests (should not deadlock)"
4953
4954 test_74a() { # bug 6149, 6184
4955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4956         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4957         #
4958         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4959         # will spin in a tight reconnection loop
4960         touch $DIR/f74a
4961         lctl set_param fail_loc=0x8000030e
4962         # get any lock that won't be difficult - lookup works.
4963         ls $DIR/f74a
4964         lctl set_param fail_loc=0
4965         true
4966         rm -f $DIR/f74a
4967 }
4968 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4969
4970 test_74b() { # bug 13310
4971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4972         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4973         #
4974         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4975         # will spin in a tight reconnection loop
4976         lctl set_param fail_loc=0x8000030e
4977         # get a "difficult" lock
4978         touch $DIR/f74b
4979         lctl set_param fail_loc=0
4980         true
4981         rm -f $DIR/f74b
4982 }
4983 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4984
4985 test_74c() {
4986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4987 #define OBD_FAIL_LDLM_NEW_LOCK
4988         lctl set_param fail_loc=0x80000319
4989         touch $DIR/$tfile && error "Touch successful"
4990         true
4991 }
4992 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4993
4994 num_inodes() {
4995         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4996 }
4997
4998 get_inode_slab_tunables() {
4999         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5000 }
5001
5002 set_inode_slab_tunables() {
5003         echo "lustre_inode_cache $1" > /proc/slabinfo
5004 }
5005
5006 test_76() { # Now for bug 20433, added originally in bug 1443
5007         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5008         local SLAB_SETTINGS=`get_inode_slab_tunables`
5009         local CPUS=`getconf _NPROCESSORS_ONLN`
5010         # we cannot set limit below 1 which means 1 inode in each
5011         # per-cpu cache is still allowed
5012         set_inode_slab_tunables "1 1 0"
5013         cancel_lru_locks osc
5014         BEFORE_INODES=`num_inodes`
5015         echo "before inodes: $BEFORE_INODES"
5016         local COUNT=1000
5017         [ "$SLOW" = "no" ] && COUNT=100
5018         for i in `seq $COUNT`; do
5019                 touch $DIR/$tfile
5020                 rm -f $DIR/$tfile
5021         done
5022         cancel_lru_locks osc
5023         AFTER_INODES=`num_inodes`
5024         echo "after inodes: $AFTER_INODES"
5025         local wait=0
5026         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
5027                 sleep 2
5028                 AFTER_INODES=`num_inodes`
5029                 wait=$((wait+2))
5030                 echo "wait $wait seconds inodes: $AFTER_INODES"
5031                 if [ $wait -gt 30 ]; then
5032                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5033                 fi
5034         done
5035         set_inode_slab_tunables "$SLAB_SETTINGS"
5036 }
5037 run_test 76 "confirm clients recycle inodes properly ===="
5038
5039
5040 export ORIG_CSUM=""
5041 set_checksums()
5042 {
5043         # Note: in sptlrpc modes which enable its own bulk checksum, the
5044         # original crc32_le bulk checksum will be automatically disabled,
5045         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5046         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5047         # In this case set_checksums() will not be no-op, because sptlrpc
5048         # bulk checksum will be enabled all through the test.
5049
5050         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5051         lctl set_param -n osc.*.checksums $1
5052         return 0
5053 }
5054
5055 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5056                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5057 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5058 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5059 set_checksum_type()
5060 {
5061         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5062         log "set checksum type to $1"
5063         return 0
5064 }
5065 F77_TMP=$TMP/f77-temp
5066 F77SZ=8
5067 setup_f77() {
5068         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5069                 error "error writing to $F77_TMP"
5070 }
5071
5072 test_77a() { # bug 10889
5073         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5074         $GSS && skip "could not run with gss" && return
5075         [ ! -f $F77_TMP ] && setup_f77
5076         set_checksums 1
5077         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5078         set_checksums 0
5079         rm -f $DIR/$tfile
5080 }
5081 run_test 77a "normal checksum read/write operation ============="
5082
5083 test_77b() { # bug 10889
5084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5085         $GSS && skip "could not run with gss" && return
5086         [ ! -f $F77_TMP ] && setup_f77
5087         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5088         lctl set_param fail_loc=0x80000409
5089         set_checksums 1
5090         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
5091                 error "dd error: $?"
5092         lctl set_param fail_loc=0
5093         set_checksums 0
5094 }
5095 run_test 77b "checksum error on client write ===================="
5096
5097 test_77c() { # bug 10889
5098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5099         $GSS && skip "could not run with gss" && return
5100         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
5101         set_checksums 1
5102         for algo in $CKSUM_TYPES; do
5103                 cancel_lru_locks osc
5104                 set_checksum_type $algo
5105                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5106                 lctl set_param fail_loc=0x80000408
5107                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
5108                 lctl set_param fail_loc=0
5109         done
5110         set_checksums 0
5111         set_checksum_type $ORIG_CSUM_TYPE
5112         rm -f $DIR/f77b
5113 }
5114 run_test 77c "checksum error on client read ==================="
5115
5116 test_77d() { # bug 10889
5117         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5118         $GSS && skip "could not run with gss" && return
5119         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5120         lctl set_param fail_loc=0x80000409
5121         set_checksums 1
5122         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5123                 error "direct write: rc=$?"
5124         lctl set_param fail_loc=0
5125         set_checksums 0
5126 }
5127 run_test 77d "checksum error on OST direct write ==============="
5128
5129 test_77e() { # bug 10889
5130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5131         $GSS && skip "could not run with gss" && return
5132         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
5133         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5134         lctl set_param fail_loc=0x80000408
5135         set_checksums 1
5136         cancel_lru_locks osc
5137         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5138                 error "direct read: rc=$?"
5139         lctl set_param fail_loc=0
5140         set_checksums 0
5141 }
5142 run_test 77e "checksum error on OST direct read ================"
5143
5144 test_77f() { # bug 10889
5145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5146         $GSS && skip "could not run with gss" && return
5147         set_checksums 1
5148         for algo in $CKSUM_TYPES; do
5149                 cancel_lru_locks osc
5150                 set_checksum_type $algo
5151                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5152                 lctl set_param fail_loc=0x409
5153                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
5154                         error "direct write succeeded"
5155                 lctl set_param fail_loc=0
5156         done
5157         set_checksum_type $ORIG_CSUM_TYPE
5158         set_checksums 0
5159 }
5160 run_test 77f "repeat checksum error on write (expect error) ===="
5161
5162 test_77g() { # bug 10889
5163         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5164         $GSS && skip "could not run with gss" && return
5165         remote_ost_nodsh && skip "remote OST with nodsh" && return
5166
5167         [ ! -f $F77_TMP ] && setup_f77
5168
5169         $SETSTRIPE -c 1 -i 0 $DIR/f77g
5170         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5171         do_facet ost1 lctl set_param fail_loc=0x8000021a
5172         set_checksums 1
5173         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
5174                 error "write error: rc=$?"
5175         do_facet ost1 lctl set_param fail_loc=0
5176         set_checksums 0
5177 }
5178 run_test 77g "checksum error on OST write ======================"
5179
5180 test_77h() { # bug 10889
5181         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5182         $GSS && skip "could not run with gss" && return
5183         remote_ost_nodsh && skip "remote OST with nodsh" && return
5184
5185         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
5186         cancel_lru_locks osc
5187         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5188         do_facet ost1 lctl set_param fail_loc=0x8000021b
5189         set_checksums 1
5190         cmp $F77_TMP $DIR/f77g || error "file compare failed"
5191         do_facet ost1 lctl set_param fail_loc=0
5192         set_checksums 0
5193 }
5194 run_test 77h "checksum error on OST read ======================="
5195
5196 test_77i() { # bug 13805
5197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5198         $GSS && skip "could not run with gss" && return
5199         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5200         lctl set_param fail_loc=0x40b
5201         remount_client $MOUNT
5202         lctl set_param fail_loc=0
5203         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5204                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5205                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5206                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5207         done
5208         remount_client $MOUNT
5209 }
5210 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
5211
5212 test_77j() { # bug 13805
5213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5214         $GSS && skip "could not run with gss" && return
5215         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5216         lctl set_param fail_loc=0x40c
5217         remount_client $MOUNT
5218         lctl set_param fail_loc=0
5219         sleep 2 # wait async osc connect to finish
5220         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5221                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5222                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5223                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5224         done
5225         remount_client $MOUNT
5226 }
5227 run_test 77j "client only supporting ADLER32 ===================="
5228
5229 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5230 rm -f $F77_TMP
5231 unset F77_TMP
5232
5233 test_78() { # bug 10901
5234         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5235         remote_ost || { skip_env "local OST" && return; }
5236
5237         NSEQ=5
5238         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5239         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5240         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5241         echo "MemTotal: $MEMTOTAL"
5242 # reserve 256MB of memory for the kernel and other running processes,
5243 # and then take 1/2 of the remaining memory for the read/write buffers.
5244     if [ $MEMTOTAL -gt 512 ] ;then
5245         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5246     else
5247         # for those poor memory-starved high-end clusters...
5248         MEMTOTAL=$((MEMTOTAL / 2))
5249     fi
5250         echo "Mem to use for directio: $MEMTOTAL"
5251         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5252         [ $F78SIZE -gt 512 ] && F78SIZE=512
5253         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5254         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5255         echo "Smallest OST: $SMALLESTOST"
5256         [ $SMALLESTOST -lt 10240 ] && \
5257                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5258
5259         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5260                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5261
5262         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5263         echo "File size: $F78SIZE"
5264         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5265         for i in `seq 1 $NSEQ`
5266         do
5267                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5268                 echo directIO rdwr round $i of $NSEQ
5269                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5270         done
5271
5272         rm -f $DIR/$tfile
5273 }
5274 run_test 78 "handle large O_DIRECT writes correctly ============"
5275
5276 test_79() { # bug 12743
5277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5278         wait_delete_completed
5279
5280         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5281         BKFREE=$(calc_osc_kbytes kbytesfree)
5282         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5283
5284         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5285         DFTOTAL=`echo $STRING | cut -d, -f1`
5286         DFUSED=`echo $STRING  | cut -d, -f2`
5287         DFAVAIL=`echo $STRING | cut -d, -f3`
5288         DFFREE=$(($DFTOTAL - $DFUSED))
5289
5290         ALLOWANCE=$((64 * $OSTCOUNT))
5291
5292         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5293            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5294                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5295         fi
5296         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5297            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5298                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5299         fi
5300         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5301            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5302                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5303         fi
5304 }
5305 run_test 79 "df report consistency check ======================="
5306
5307 test_80() { # bug 10718
5308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5309         # relax strong synchronous semantics for slow backends like ZFS
5310         local soc="obdfilter.*.sync_on_lock_cancel"
5311         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5312         local hosts=
5313         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5314                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5315                           facet_active_host $host; done | sort -u)
5316                 do_nodes $hosts lctl set_param $soc=never
5317         fi
5318
5319         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5320         sync; sleep 1; sync
5321         local BEFORE=`date +%s`
5322         cancel_lru_locks osc
5323         local AFTER=`date +%s`
5324         local DIFF=$((AFTER-BEFORE))
5325         if [ $DIFF -gt 1 ] ; then
5326                 error "elapsed for 1M@1T = $DIFF"
5327         fi
5328
5329         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5330
5331         rm -f $DIR/$tfile
5332 }
5333 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5334
5335 test_81a() { # LU-456
5336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5337         remote_ost_nodsh && skip "remote OST with nodsh" && return
5338         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5339         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5340         do_facet ost1 lctl set_param fail_loc=0x80000228
5341
5342         # write should trigger a retry and success
5343         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5344         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5345         RC=$?
5346         if [ $RC -ne 0 ] ; then
5347                 error "write should success, but failed for $RC"
5348         fi
5349 }
5350 run_test 81a "OST should retry write when get -ENOSPC ==============="
5351
5352 test_81b() { # LU-456
5353         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5354         remote_ost_nodsh && skip "remote OST with nodsh" && return
5355         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5356         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5357         do_facet ost1 lctl set_param fail_loc=0x228
5358
5359         # write should retry several times and return -ENOSPC finally
5360         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5361         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5362         RC=$?
5363         ENOSPC=28
5364         if [ $RC -ne $ENOSPC ] ; then
5365                 error "dd should fail for -ENOSPC, but succeed."
5366         fi
5367 }
5368 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5369
5370 test_82() { # LU-1031
5371         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5372         local gid1=14091995
5373         local gid2=16022000
5374
5375         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5376         local MULTIPID1=$!
5377         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5378         local MULTIPID2=$!
5379         kill -USR1 $MULTIPID2
5380         sleep 2
5381         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5382                 error "First grouplock does not block second one"
5383         else
5384                 echo "Second grouplock blocks first one"
5385         fi
5386         kill -USR1 $MULTIPID1
5387         wait $MULTIPID1
5388         wait $MULTIPID2
5389 }
5390 run_test 82 "Basic grouplock test ==============================="
5391
5392 test_99a() {
5393         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5394             return
5395         test_mkdir -p $DIR/d99cvsroot
5396         chown $RUNAS_ID $DIR/d99cvsroot
5397         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5398         cd $TMP
5399
5400         $RUNAS cvs -d $DIR/d99cvsroot init || error
5401         cd $oldPWD
5402 }
5403 run_test 99a "cvs init ========================================="
5404
5405 test_99b() {
5406         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5407         [ ! -d $DIR/d99cvsroot ] && test_99a
5408         cd /etc/init.d
5409         # some versions of cvs import exit(1) when asked to import links or
5410         # files they can't read.  ignore those files.
5411         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5412                         ! -perm +4 -printf '-I %f\n')
5413         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5414                 d99reposname vtag rtag
5415 }
5416 run_test 99b "cvs import ======================================="
5417
5418 test_99c() {
5419         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5420         [ ! -d $DIR/d99cvsroot ] && test_99b
5421         cd $DIR
5422         test_mkdir -p $DIR/d99reposname
5423         chown $RUNAS_ID $DIR/d99reposname
5424         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5425 }
5426 run_test 99c "cvs checkout ====================================="
5427
5428 test_99d() {
5429         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5430         [ ! -d $DIR/d99cvsroot ] && test_99c
5431         cd $DIR/d99reposname
5432         $RUNAS touch foo99
5433         $RUNAS cvs add -m 'addmsg' foo99
5434 }
5435 run_test 99d "cvs add =========================================="
5436
5437 test_99e() {
5438         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5439         [ ! -d $DIR/d99cvsroot ] && test_99c
5440         cd $DIR/d99reposname
5441         $RUNAS cvs update
5442 }
5443 run_test 99e "cvs update ======================================="
5444
5445 test_99f() {
5446         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5447         [ ! -d $DIR/d99cvsroot ] && test_99d
5448         cd $DIR/d99reposname
5449         $RUNAS cvs commit -m 'nomsg' foo99
5450     rm -fr $DIR/d99cvsroot
5451 }
5452 run_test 99f "cvs commit ======================================="
5453
5454 test_100() {
5455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5456         [ "$NETTYPE" = tcp ] || \
5457                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5458                         return ; }
5459
5460         remote_ost_nodsh && skip "remote OST with nodsh" && return
5461         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5462         remote_servers || \
5463                 { skip "useless for local single node setup" && return; }
5464
5465         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5466                 [ "$PROT" != "tcp" ] && continue
5467                 RPORT=$(echo $REMOTE | cut -d: -f2)
5468                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5469
5470                 rc=0
5471                 LPORT=`echo $LOCAL | cut -d: -f2`
5472                 if [ $LPORT -ge 1024 ]; then
5473                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5474                         netstat -tna
5475                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5476                 fi
5477         done
5478         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5479 }
5480 run_test 100 "check local port using privileged port ==========="
5481
5482 function get_named_value()
5483 {
5484     local tag
5485
5486     tag=$1
5487     while read ;do
5488         line=$REPLY
5489         case $line in
5490         $tag*)
5491             echo $line | sed "s/^$tag[ ]*//"
5492             break
5493             ;;
5494         esac
5495     done
5496 }
5497
5498 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5499                    awk '/^max_cached_mb/ { print $2 }')
5500
5501 cleanup_101a() {
5502         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5503         trap 0
5504 }
5505
5506 test_101a() {
5507         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5508         local s
5509         local discard
5510         local nreads=10000
5511         local cache_limit=32
5512
5513         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5514         trap cleanup_101a EXIT
5515         $LCTL set_param -n llite.*.read_ahead_stats 0
5516         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5517
5518         #
5519         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5520         #
5521         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5522         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5523
5524         discard=0
5525         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5526                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5527                         discard=$(($discard + $s))
5528         done
5529         cleanup_101a
5530
5531         if [ $(($discard * 10)) -gt $nreads ] ;then
5532                 $LCTL get_param osc.*-osc*.rpc_stats
5533                 $LCTL get_param llite.*.read_ahead_stats
5534                 error "too many ($discard) discarded pages"
5535         fi
5536         rm -f $DIR/$tfile || true
5537 }
5538 run_test 101a "check read-ahead for random reads ================"
5539
5540 setup_test101bc() {
5541         test_mkdir -p $DIR/$tdir
5542         STRIPE_SIZE=1048576
5543         STRIPE_COUNT=$OSTCOUNT
5544         STRIPE_OFFSET=0
5545
5546         local list=$(comma_list $(osts_nodes))
5547         set_osd_param $list '' read_cache_enable 0
5548         set_osd_param $list '' writethrough_cache_enable 0
5549
5550         trap cleanup_test101bc EXIT
5551         # prepare the read-ahead file
5552         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5553
5554         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5555 }
5556
5557 cleanup_test101bc() {
5558         trap 0
5559         rm -rf $DIR/$tdir
5560         rm -f $DIR/$tfile
5561
5562         local list=$(comma_list $(osts_nodes))
5563         set_osd_param $list '' read_cache_enable 1
5564         set_osd_param $list '' writethrough_cache_enable 1
5565 }
5566
5567 calc_total() {
5568         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5569 }
5570
5571 ra_check_101() {
5572         local READ_SIZE=$1
5573         local STRIPE_SIZE=1048576
5574         local RA_INC=1048576
5575         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5576         local FILE_LENGTH=$((64*100))
5577         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5578                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5579         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5580                         get_named_value 'read but discarded' | \
5581                         cut -d" " -f1 | calc_total`
5582         if [ $DISCARD -gt $discard_limit ]; then
5583                 $LCTL get_param llite.*.read_ahead_stats
5584                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5585         else
5586                 echo "Read-ahead success for size ${READ_SIZE}"
5587         fi
5588 }
5589
5590 test_101b() {
5591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5592         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5593         local STRIPE_SIZE=1048576
5594         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5595         local FILE_LENGTH=$((STRIPE_SIZE*100))
5596         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5597         # prepare the read-ahead file
5598         setup_test101bc
5599         cancel_lru_locks osc
5600         for BIDX in 2 4 8 16 32 64 128 256
5601         do
5602                 local BSIZE=$((BIDX*4096))
5603                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5604                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5605                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5606                 $LCTL set_param -n llite.*.read_ahead_stats 0
5607                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5608                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5609                 cancel_lru_locks osc
5610                 ra_check_101 $BSIZE
5611         done
5612         cleanup_test101bc
5613         true
5614 }
5615 run_test 101b "check stride-io mode read-ahead ================="
5616
5617 test_101c() {
5618         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5619         local STRIPE_SIZE=1048576
5620         local FILE_LENGTH=$((STRIPE_SIZE*100))
5621         local nreads=10000
5622         local osc
5623
5624     setup_test101bc
5625
5626     cancel_lru_locks osc
5627     $LCTL set_param osc.*.rpc_stats 0
5628     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5629     for osc in $($LCTL get_param -N osc.*); do
5630         if [ "$osc" == "osc.num_refs" ]; then
5631             continue
5632         fi
5633
5634         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5635         if [ $lines -le 20 ]; then
5636             continue
5637         fi
5638
5639         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5640                                      awk '$1 == "1:" { print $2; exit; }')
5641         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5642                                      awk '$1 == "2:" { print $2; exit; }')
5643         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5644                                      awk '$1 == "4:" { print $2; exit; }')
5645         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5646                                      awk '$1 == "8:" { print $2; exit; }')
5647
5648         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5649         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5650         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5651         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5652         echo "${osc} rpc check passed!"
5653     done
5654     cleanup_test101bc
5655     true
5656 }
5657 run_test 101c "check stripe_size aligned read-ahead ================="
5658
5659 set_read_ahead() {
5660    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5661    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5662 }
5663
5664 test_101d() {
5665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5666         local file=$DIR/$tfile
5667         local size=${FILESIZE_101c:-500}
5668         local ra_MB=${READAHEAD_MB:-40}
5669
5670         local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5671         [ $space -gt $((size * 1024)) ] ||
5672                 { skip "Need free space ${size}M, have $space" && return; }
5673
5674     echo Creating ${size}M test file $file
5675     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5676     echo Cancel LRU locks on lustre client to flush the client cache
5677     cancel_lru_locks osc
5678
5679     echo Disable read-ahead
5680     local old_READAHEAD=$(set_read_ahead 0)
5681
5682     echo Reading the test file $file with read-ahead disabled
5683     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5684
5685     echo Cancel LRU locks on lustre client to flush the client cache
5686     cancel_lru_locks osc
5687     echo Enable read-ahead with ${ra_MB}MB
5688     set_read_ahead $ra_MB
5689
5690     echo Reading the test file $file with read-ahead enabled
5691     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5692
5693     echo read-ahead disabled time read $time_ra_OFF
5694     echo read-ahead enabled  time read $time_ra_ON
5695
5696         set_read_ahead $old_READAHEAD
5697         rm -f $file
5698         wait_delete_completed
5699
5700     [ $time_ra_ON -lt $time_ra_OFF ] ||
5701         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5702                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5703 }
5704 run_test 101d "file read with and without read-ahead enabled  ================="
5705
5706 test_101e() {
5707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5708     local file=$DIR/$tfile
5709     local size=500  #KB
5710     local count=100
5711     local blksize=1024
5712
5713     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5714     local need_space=$((count * size))
5715     [ $space -gt $need_space ] ||
5716         { skip_env "Need free space $need_space, have $space" && return; }
5717
5718     echo Creating $count ${size}K test files
5719     for ((i = 0; i < $count; i++)); do
5720         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5721     done
5722
5723     echo Cancel LRU locks on lustre client to flush the client cache
5724     cancel_lru_locks osc
5725
5726     echo Reset readahead stats
5727     $LCTL set_param -n llite.*.read_ahead_stats 0
5728
5729     for ((i = 0; i < $count; i++)); do
5730         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5731     done
5732
5733     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5734           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5735
5736     for ((i = 0; i < $count; i++)); do
5737         rm -rf ${file}_${i} 2>/dev/null
5738     done
5739
5740     #10000 means 20% reads are missing in readahead
5741     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5742 }
5743 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5744
5745 cleanup_test101f() {
5746     trap 0
5747     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5748     rm -rf $DIR/$tfile 2>/dev/null
5749 }
5750
5751 test_101f() {
5752         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5753     local file=$DIR/$tfile
5754     local nreads=1000
5755
5756     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5757     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5758     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5759     trap cleanup_test101f EXIT
5760
5761     echo Cancel LRU locks on lustre client to flush the client cache
5762     cancel_lru_locks osc
5763
5764     echo Reset readahead stats
5765     $LCTL set_param -n llite.*.read_ahead_stats 0
5766     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5767     # readahead should read in 2M file on second read, so only miss
5768     # 2 pages.
5769     echo Random 4K reads on 2M file for 1000 times
5770     $READS -f $file -s 2097152 -b 4096 -n $nreads
5771
5772     echo checking missing pages
5773     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5774           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5775
5776     [ $miss -lt 3 ] || error "misses too much pages!"
5777     cleanup_test101f
5778 }
5779 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5780
5781 setup_test102() {
5782         test_mkdir -p $DIR/$tdir
5783         chown $RUNAS_ID $DIR/$tdir
5784         STRIPE_SIZE=65536
5785         STRIPE_OFFSET=1
5786         STRIPE_COUNT=$OSTCOUNT
5787         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5788
5789         trap cleanup_test102 EXIT
5790         cd $DIR
5791         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5792         cd $DIR/$tdir
5793         for num in 1 2 3 4; do
5794                 for count in $(seq 1 $STRIPE_COUNT); do
5795                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5796                                 local size=`expr $STRIPE_SIZE \* $num`
5797                                 local file=file"$num-$idx-$count"
5798                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5799                         done
5800                 done
5801         done
5802
5803         cd $DIR
5804         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5805 }
5806
5807 cleanup_test102() {
5808         trap 0
5809         rm -f $TMP/f102.tar
5810         rm -rf $DIR/d0.sanity/d102
5811 }
5812
5813 test_102a() {
5814         local testfile=$DIR/xattr_testfile
5815
5816         touch $testfile
5817
5818         [ "$UID" != 0 ] && skip_env "must run as root" && return
5819         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5820                 skip_env "must have user_xattr" && return
5821
5822         [ -z "$(which setfattr 2>/dev/null)" ] &&
5823                 skip_env "could not find setfattr" && return
5824
5825         echo "set/get xattr..."
5826         setfattr -n trusted.name1 -v value1 $testfile || error
5827         getfattr -n trusted.name1 $testfile 2> /dev/null |
5828           grep "trusted.name1=.value1" ||
5829                 error "$testfile missing trusted.name1=value1"
5830
5831         setfattr -n user.author1 -v author1 $testfile || error
5832         getfattr -n user.author1 $testfile 2> /dev/null |
5833           grep "user.author1=.author1" ||
5834                 error "$testfile missing trusted.author1=author1"
5835
5836         echo "listxattr..."
5837         setfattr -n trusted.name2 -v value2 $testfile ||
5838                 error "$testfile unable to set trusted.name2"
5839         setfattr -n trusted.name3 -v value3 $testfile ||
5840                 error "$testfile unable to set trusted.name3"
5841         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5842             grep "trusted.name" | wc -l) -eq 3 ] ||
5843                 error "$testfile missing 3 trusted.name xattrs"
5844
5845         setfattr -n user.author2 -v author2 $testfile ||
5846                 error "$testfile unable to set user.author2"
5847         setfattr -n user.author3 -v author3 $testfile ||
5848                 error "$testfile unable to set user.author3"
5849         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5850             grep "user.author" | wc -l) -eq 3 ] ||
5851                 error "$testfile missing 3 user.author xattrs"
5852
5853         echo "remove xattr..."
5854         setfattr -x trusted.name1 $testfile ||
5855                 error "$testfile error deleting trusted.name1"
5856         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5857                 error "$testfile did not delete trusted.name1 xattr"
5858
5859         setfattr -x user.author1 $testfile ||
5860                 error "$testfile error deleting user.author1"
5861         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5862                 error "$testfile did not delete trusted.name1 xattr"
5863
5864         # b10667: setting lustre special xattr be silently discarded
5865         echo "set lustre special xattr ..."
5866         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5867                 error "$testfile allowed setting trusted.lov"
5868 }
5869 run_test 102a "user xattr test =================================="
5870
5871 test_102b() {
5872         # b10930: get/set/list trusted.lov xattr
5873         echo "get/set/list trusted.lov xattr ..."
5874         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5875         local testfile=$DIR/$tfile
5876         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5877                 error "setstripe failed"
5878         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5879                 error "getstripe failed"
5880         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5881         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5882
5883         local testfile2=${testfile}2
5884         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5885                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5886
5887         $MCREATE $testfile2
5888         setfattr -n trusted.lov -v $value $testfile2
5889         local stripe_size=$($GETSTRIPE -S $testfile2)
5890         local stripe_count=$($GETSTRIPE -c $testfile2)
5891         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5892         [ $stripe_count -eq $STRIPECOUNT ] ||
5893                 error "stripe count $stripe_count != $STRIPECOUNT"
5894         rm -f $DIR/$tfile
5895 }
5896 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5897
5898 test_102c() {
5899         # b10930: get/set/list lustre.lov xattr
5900         echo "get/set/list lustre.lov xattr ..."
5901         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5902         test_mkdir -p $DIR/$tdir
5903         chown $RUNAS_ID $DIR/$tdir
5904         local testfile=$DIR/$tdir/$tfile
5905         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5906                 error "setstripe failed"
5907         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5908                 error "getstripe failed"
5909         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5910         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5911
5912         local testfile2=${testfile}2
5913         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5914                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5915
5916         $RUNAS $MCREATE $testfile2
5917         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5918         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5919         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5920         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5921         [ $stripe_count -eq $STRIPECOUNT ] ||
5922                 error "stripe count $stripe_count != $STRIPECOUNT"
5923 }
5924 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5925
5926 compare_stripe_info1() {
5927         local stripe_index_all_zero=true
5928
5929         for num in 1 2 3 4; do
5930                 for count in $(seq 1 $STRIPE_COUNT); do
5931                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5932                                 local size=$((STRIPE_SIZE * num))
5933                                 local file=file"$num-$offset-$count"
5934                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5935                                 [ $stripe_size -ne $size ] &&
5936                                     error "$file: size $stripe_size != $size"
5937                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5938                                 # allow fewer stripes to be created, ORI-601
5939                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5940                                     error "$file: count $stripe_count != $count"
5941                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5942                                 [ $stripe_index -ne 0 ] &&
5943                                         stripe_index_all_zero=false
5944                         done
5945                 done
5946         done
5947         $stripe_index_all_zero &&
5948                 error "all files are being extracted starting from OST index 0"
5949         return 0
5950 }
5951
5952 find_lustre_tar() {
5953         [ -n "$(which tar 2>/dev/null)" ] &&
5954                 strings $(which tar) | grep -q "lustre" && echo tar
5955 }
5956
5957 test_102d() {
5958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5959         # b10930: tar test for trusted.lov xattr
5960         TAR=$(find_lustre_tar)
5961         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5962         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5963         setup_test102
5964         test_mkdir -p $DIR/d102d
5965         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5966         cd $DIR/d102d/$tdir
5967         compare_stripe_info1
5968 }
5969 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5970
5971 test_102f() {
5972         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5973         # b10930: tar test for trusted.lov xattr
5974         TAR=$(find_lustre_tar)
5975         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5976         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5977         setup_test102
5978         test_mkdir -p $DIR/d102f
5979         cd $DIR
5980         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5981         cd $DIR/d102f/$tdir
5982         compare_stripe_info1
5983 }
5984 run_test 102f "tar copy files, not keep osts ==========="
5985
5986 grow_xattr() {
5987         local xsize=${1:-1024}  # in bytes
5988         local file=$DIR/$tfile
5989
5990         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5991                 skip "must have user_xattr" && return 0
5992         [ -z "$(which setfattr 2>/dev/null)" ] &&
5993                 skip_env "could not find setfattr" && return 0
5994         [ -z "$(which getfattr 2>/dev/null)" ] &&
5995                 skip_env "could not find getfattr" && return 0
5996
5997         touch $file
5998
5999         local value="$(generate_string $xsize)"
6000
6001         local xbig=trusted.big
6002         log "save $xbig on $file"
6003         setfattr -n $xbig -v $value $file ||
6004                 error "saving $xbig on $file failed"
6005
6006         local orig=$(get_xattr_value $xbig $file)
6007         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6008
6009         local xsml=trusted.sml
6010         log "save $xsml on $file"
6011         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6012
6013         local new=$(get_xattr_value $xbig $file)
6014         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6015
6016         log "grow $xsml on $file"
6017         setfattr -n $xsml -v "$value" $file ||
6018                 error "growing $xsml on $file failed"
6019
6020         new=$(get_xattr_value $xbig $file)
6021         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6022         log "$xbig still valid after growing $xsml"
6023
6024         rm -f $file
6025 }
6026
6027 test_102h() { # bug 15777
6028         grow_xattr 1024
6029 }
6030 run_test 102h "grow xattr from inside inode to external block"
6031
6032 test_102ha() {
6033         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6034         grow_xattr $(max_xattr_size)
6035 }
6036 run_test 102ha "grow xattr from inside inode to external inode"
6037
6038 test_102i() { # bug 17038
6039         touch $DIR/$tfile
6040         ln -s $DIR/$tfile $DIR/${tfile}link
6041         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
6042         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 | grep -i "no such attr" || error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6043         rm -f $DIR/$tfile $DIR/${tfile}link
6044 }
6045 run_test 102i "lgetxattr test on symbolic link ============"
6046
6047 test_102j() {
6048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6049         TAR=$(find_lustre_tar)
6050         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6051         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
6052         setup_test102 "$RUNAS"
6053         test_mkdir -p $DIR/d102j
6054         chown $RUNAS_ID $DIR/d102j
6055         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6056         cd $DIR/d102j/$tdir
6057         compare_stripe_info1 "$RUNAS"
6058 }
6059 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6060
6061 test_102k() {
6062         touch $DIR/$tfile
6063         # b22187 just check that does not crash for regular file.
6064         setfattr -n trusted.lov $DIR/$tfile
6065         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6066         local test_kdir=$DIR/d102k
6067         test_mkdir $test_kdir
6068         local default_size=`$GETSTRIPE -S $test_kdir`
6069         local default_count=`$GETSTRIPE -c $test_kdir`
6070         local default_offset=`$GETSTRIPE -i $test_kdir`
6071         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
6072                 error 'dir setstripe failed'
6073         setfattr -n trusted.lov $test_kdir
6074         local stripe_size=`$GETSTRIPE -S $test_kdir`
6075         local stripe_count=`$GETSTRIPE -c $test_kdir`
6076         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6077         [ $stripe_size -eq $default_size ] ||
6078                 error "stripe size $stripe_size != $default_size"
6079         [ $stripe_count -eq $default_count ] ||
6080                 error "stripe count $stripe_count != $default_count"
6081         [ $stripe_offset -eq $default_offset ] ||
6082                 error "stripe offset $stripe_offset != $default_offset"
6083         rm -rf $DIR/$tfile $test_kdir
6084 }
6085 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6086
6087 test_102l() {
6088         # LU-532 trusted. xattr is invisible to non-root
6089         local testfile=$DIR/$tfile
6090
6091         touch $testfile
6092
6093         echo "listxattr as user..."
6094         chown $RUNAS_ID $testfile
6095         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6096             grep -q "trusted" &&
6097                 error "$testfile trusted xattrs are user visible"
6098
6099         return 0;
6100 }
6101 run_test 102l "listxattr filter test =================================="
6102
6103 cleanup_test102
6104
6105 run_acl_subtest()
6106 {
6107     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6108     return $?
6109 }
6110
6111 test_103 () {
6112     [ "$UID" != 0 ] && skip_env "must run as root" && return
6113     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6114     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
6115     $GSS && skip "could not run under gss" && return
6116
6117     declare -a identity_old
6118
6119         for num in $(seq $MDSCOUNT); do
6120                 switch_identity $num true || identity_old[$num]=$?
6121         done
6122
6123     SAVE_UMASK=`umask`
6124     umask 0022
6125     cd $DIR
6126
6127     echo "performing cp ..."
6128     run_acl_subtest cp || error
6129     echo "performing getfacl-noacl..."
6130     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6131     echo "performing misc..."
6132     run_acl_subtest misc || error  "misc test failed"
6133     echo "performing permissions..."
6134     run_acl_subtest permissions || error "permissions failed"
6135     echo "performing setfacl..."
6136     run_acl_subtest setfacl || error  "setfacl test failed"
6137
6138     # inheritance test got from HP
6139     echo "performing inheritance..."
6140     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6141     chmod +x make-tree || error "chmod +x failed"
6142     run_acl_subtest inheritance || error "inheritance test failed"
6143     rm -f make-tree
6144
6145     echo "LU-974 ignore umask when acl is enabled..."
6146     run_acl_subtest 974 || error "LU-974 test failed"
6147
6148     echo "LU-2561 newly created file is same size as directory..."
6149     run_acl_subtest 2561 || error "LU-2561 test failed"
6150
6151     cd $SAVE_PWD
6152     umask $SAVE_UMASK
6153
6154         for num in $(seq $MDSCOUNT); do
6155                 if [ "${identity_old[$num]}" = 1 ]; then
6156                         switch_identity $num false || identity_old[$num]=$?
6157                 fi
6158         done
6159 }
6160 run_test 103 "acl test ========================================="
6161
6162 test_104a() {
6163         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6164         touch $DIR/$tfile
6165         lfs df || error "lfs df failed"
6166         lfs df -ih || error "lfs df -ih failed"
6167         lfs df -h $DIR || error "lfs df -h $DIR failed"
6168         lfs df -i $DIR || error "lfs df -i $DIR failed"
6169         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6170         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6171
6172         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
6173         lctl --device %$OSC deactivate
6174         lfs df || error "lfs df with deactivated OSC failed"
6175         lctl --device %$OSC activate
6176         # wait the osc back to normal
6177         wait_osc_import_state client ost FULL
6178
6179         lfs df || error "lfs df with reactivated OSC failed"
6180         rm -f $DIR/$tfile
6181 }
6182 run_test 104a "lfs df [-ih] [path] test ========================="
6183
6184 test_104b() {
6185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6186         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6187         chmod 666 /dev/obd
6188         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
6189         if [ $denied_cnt -ne 0 ];
6190         then
6191                     error "lfs check servers test failed"
6192         fi
6193 }
6194 run_test 104b "$RUNAS lfs check servers test ===================="
6195
6196 test_105a() {
6197         # doesn't work on 2.4 kernels
6198         touch $DIR/$tfile
6199         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6200                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6201         else
6202                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6203         fi
6204         rm -f $DIR/$tfile
6205 }
6206 run_test 105a "flock when mounted without -o flock test ========"
6207
6208 test_105b() {
6209         touch $DIR/$tfile
6210         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6211                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6212         else
6213                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6214         fi
6215         rm -f $DIR/$tfile
6216 }
6217 run_test 105b "fcntl when mounted without -o flock test ========"
6218
6219 test_105c() {
6220         touch $DIR/$tfile
6221         if [ -n "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ]; then
6222                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6223         else
6224                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6225         fi
6226         rm -f $DIR/$tfile
6227 }
6228 run_test 105c "lockf when mounted without -o flock test ========"
6229
6230 test_105d() { # bug 15924
6231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6232         test_mkdir -p $DIR/$tdir
6233         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6234                 skip "mount w/o flock enabled" && return
6235         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6236         $LCTL set_param fail_loc=0x80000315
6237         flocks_test 2 $DIR/$tdir
6238 }
6239 run_test 105d "flock race (should not freeze) ========"
6240
6241 test_105e() { # bug 22660 && 22040
6242         [ -z "`mount | grep \"$MOUNT.*flock\" | grep -v noflock`" ] && \
6243                 skip "mount w/o flock enabled" && return
6244         touch $DIR/$tfile
6245         flocks_test 3 $DIR/$tfile
6246 }
6247 run_test 105e "Two conflicting flocks from same process ======="
6248
6249 test_106() { #bug 10921
6250         test_mkdir -p $DIR/$tdir
6251         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6252         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6253 }
6254 run_test 106 "attempt exec of dir followed by chown of that dir"
6255
6256 test_107() {
6257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6258         CDIR=`pwd`
6259         cd $DIR
6260
6261         local file=core
6262         rm -f $file
6263
6264         local save_pattern=$(sysctl -n kernel.core_pattern)
6265         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6266         sysctl -w kernel.core_pattern=$file
6267         sysctl -w kernel.core_uses_pid=0
6268
6269         ulimit -c unlimited
6270         sleep 60 &
6271         SLEEPPID=$!
6272
6273         sleep 1
6274
6275         kill -s 11 $SLEEPPID
6276         wait $SLEEPPID
6277         if [ -e $file ]; then
6278                 size=`stat -c%s $file`
6279                 [ $size -eq 0 ] && error "Fail to create core file $file"
6280         else
6281                 error "Fail to create core file $file"
6282         fi
6283         rm -f $file
6284         sysctl -w kernel.core_pattern=$save_pattern
6285         sysctl -w kernel.core_uses_pid=$save_uses_pid
6286         cd $CDIR
6287 }
6288 run_test 107 "Coredump on SIG"
6289
6290 test_110() {
6291         test_mkdir -p $DIR/$tdir
6292         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6293                 error "mkdir with 255 char failed"
6294         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6295                 error "mkdir with 256 char should fail, but did not"
6296         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6297                 error "create with 255 char failed"
6298         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6299                 error "create with 256 char should fail, but did not"
6300
6301         ls -l $DIR/$tdir
6302         rm -rf $DIR/$tdir
6303 }
6304 run_test 110 "filename length checking"
6305
6306 test_115() {
6307         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6308         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6309             cut -c11-20)
6310         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6311             return
6312         echo "Starting with $OSTIO_pre threads"
6313
6314         NUMTEST=20000
6315         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6316         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6317         echo "$NUMTEST creates/unlinks"
6318         test_mkdir -p $DIR/$tdir
6319         createmany -o $DIR/$tdir/$tfile $NUMTEST
6320         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6321
6322         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6323             cut -c11-20)
6324
6325         # don't return an error
6326         [ $OSTIO_post == $OSTIO_pre ] && echo \
6327             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6328             echo "This may be fine, depending on what ran before this test" &&
6329             echo "and how fast this system is." && return
6330
6331         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6332 }
6333 run_test 115 "verify dynamic thread creation===================="
6334
6335 free_min_max () {
6336         wait_delete_completed
6337         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6338         echo OST kbytes available: ${AVAIL[@]}
6339         MAXI=0; MAXV=${AVAIL[0]}
6340         MINI=0; MINV=${AVAIL[0]}
6341         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6342             #echo OST $i: ${AVAIL[i]}kb
6343             if [ ${AVAIL[i]} -gt $MAXV ]; then
6344                 MAXV=${AVAIL[i]}; MAXI=$i
6345             fi
6346             if [ ${AVAIL[i]} -lt $MINV ]; then
6347                 MINV=${AVAIL[i]}; MINI=$i
6348             fi
6349         done
6350         echo Min free space: OST $MINI: $MINV
6351         echo Max free space: OST $MAXI: $MAXV
6352 }
6353
6354 test_116a() { # was previously test_116()
6355         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6356         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6357
6358         echo -n "Free space priority "
6359         lctl get_param -n lov.*-clilov-*.qos_prio_free
6360         declare -a AVAIL
6361         free_min_max
6362         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6363                 return
6364
6365         # generate uneven OSTs
6366         test_mkdir -p $DIR/$tdir/OST${MINI}
6367         declare -i FILL
6368         FILL=$(($MINV / 4))
6369         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6370         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6371         i=0
6372         while [ $FILL -gt 0 ]; do
6373             i=$(($i + 1))
6374             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6375             FILL=$(($FILL - 2048))
6376             echo -n .
6377         done
6378         FILL=$(($MINV / 4))
6379         sync
6380         sleep_maxage
6381
6382         free_min_max
6383         DIFF=$(($MAXV - $MINV))
6384         DIFF2=$(($DIFF * 100 / $MINV))
6385         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6386         if [ $DIFF2 -gt 20 ]; then
6387             echo "ok"
6388         else
6389             echo "failed - QOS mode won't be used"
6390             error_ignore "QOS imbalance criteria not met"
6391             return
6392         fi
6393
6394         MINI1=$MINI; MINV1=$MINV
6395         MAXI1=$MAXI; MAXV1=$MAXV
6396
6397         # now fill using QOS
6398         echo writing a bunch of files to QOS-assigned OSTs
6399         $SETSTRIPE -c 1 $DIR/$tdir
6400         i=0
6401         while [ $FILL -gt 0 ]; do
6402             i=$(($i + 1))
6403             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6404             FILL=$(($FILL - 200))
6405             echo -n .
6406         done
6407         echo "wrote $i 200k files"
6408         sync
6409         sleep_maxage
6410
6411         echo "Note: free space may not be updated, so measurements might be off"
6412         free_min_max
6413         DIFF2=$(($MAXV - $MINV))
6414         echo "free space delta: orig $DIFF final $DIFF2"
6415         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6416         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6417         echo "Wrote $DIFF to smaller OST $MINI1"
6418         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6419         echo "Wrote $DIFF2 to larger OST $MAXI1"
6420         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6421
6422         # Figure out which files were written where
6423         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6424                awk '/'$MINI1': / {print $2; exit}')
6425         echo $UUID
6426         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6427         echo "$MINC files created on smaller OST $MINI1"
6428         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6429                awk '/'$MAXI1': / {print $2; exit}')
6430         echo $UUID
6431         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6432         echo "$MAXC files created on larger OST $MAXI1"
6433         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6434         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6435
6436         rm -rf $DIR/$tdir
6437 }
6438 run_test 116a "stripe QOS: free space balance ==================="
6439
6440 test_116b() { # LU-2093
6441         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6442 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6443         local old_rr
6444         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6445         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6446         mkdir -p $DIR/$tdir
6447         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6448         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6449         do_facet $SINGLEMDS lctl set_param fail_loc=0
6450         rm -rf $DIR/$tdir
6451         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6452 }
6453 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6454
6455 test_117() # bug 10891
6456 {
6457         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6458         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6459         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6460         lctl set_param fail_loc=0x21e
6461         > $DIR/$tfile || error "truncate failed"
6462         lctl set_param fail_loc=0
6463         echo "Truncate succeeded."
6464         rm -f $DIR/$tfile
6465 }
6466 run_test 117 "verify fsfilt_extend =========="
6467
6468 NO_SLOW_RESENDCOUNT=4
6469 export OLD_RESENDCOUNT=""
6470 set_resend_count () {
6471         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6472         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6473         lctl set_param -n $PROC_RESENDCOUNT $1
6474         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6475 }
6476
6477 # for reduce test_118* time (b=14842)
6478 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6479
6480 # Reset async IO behavior after error case
6481 reset_async() {
6482         FILE=$DIR/reset_async
6483
6484         # Ensure all OSCs are cleared
6485         $SETSTRIPE -c -1 $FILE
6486         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6487         sync
6488         rm $FILE
6489 }
6490
6491 test_118a() #bug 11710
6492 {
6493         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6494         reset_async
6495
6496         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6497         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6498         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6499
6500         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6501                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6502                 return 1;
6503         fi
6504         rm -f $DIR/$tfile
6505 }
6506 run_test 118a "verify O_SYNC works =========="
6507
6508 test_118b()
6509 {
6510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6511         remote_ost_nodsh && skip "remote OST with nodsh" && return
6512
6513         reset_async
6514
6515         #define OBD_FAIL_OST_ENOENT 0x217
6516         set_nodes_failloc "$(osts_nodes)" 0x217
6517         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6518         RC=$?
6519         set_nodes_failloc "$(osts_nodes)" 0
6520         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6521         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6522                     grep -c writeback)
6523
6524         if [[ $RC -eq 0 ]]; then
6525                 error "Must return error due to dropped pages, rc=$RC"
6526                 return 1;
6527         fi
6528
6529         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6530                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6531                 return 1;
6532         fi
6533
6534         echo "Dirty pages not leaked on ENOENT"
6535
6536         # Due to the above error the OSC will issue all RPCs syncronously
6537         # until a subsequent RPC completes successfully without error.
6538         $MULTIOP $DIR/$tfile Ow4096yc
6539         rm -f $DIR/$tfile
6540
6541         return 0
6542 }
6543 run_test 118b "Reclaim dirty pages on fatal error =========="
6544
6545 test_118c()
6546 {
6547         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6548
6549         # for 118c, restore the original resend count, LU-1940
6550         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
6551                                 set_resend_count $OLD_RESENDCOUNT
6552         remote_ost_nodsh && skip "remote OST with nodsh" && return
6553
6554         reset_async
6555
6556         #define OBD_FAIL_OST_EROFS               0x216
6557         set_nodes_failloc "$(osts_nodes)" 0x216
6558
6559         # multiop should block due to fsync until pages are written
6560         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6561         MULTIPID=$!
6562         sleep 1
6563
6564         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6565                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6566         fi
6567
6568         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6569                     grep -c writeback)
6570         if [[ $WRITEBACK -eq 0 ]]; then
6571                 error "No page in writeback, writeback=$WRITEBACK"
6572         fi
6573
6574         set_nodes_failloc "$(osts_nodes)" 0
6575         wait $MULTIPID
6576         RC=$?
6577         if [[ $RC -ne 0 ]]; then
6578                 error "Multiop fsync failed, rc=$RC"
6579         fi
6580
6581         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6582         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6583                     grep -c writeback)
6584         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6585                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6586         fi
6587
6588         rm -f $DIR/$tfile
6589         echo "Dirty pages flushed via fsync on EROFS"
6590         return 0
6591 }
6592 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6593
6594 # continue to use small resend count to reduce test_118* time (b=14842)
6595 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
6596
6597 test_118d()
6598 {
6599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6600         remote_ost_nodsh && skip "remote OST with nodsh" && return
6601
6602         reset_async
6603
6604         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6605         set_nodes_failloc "$(osts_nodes)" 0x214
6606         # multiop should block due to fsync until pages are written
6607         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6608         MULTIPID=$!
6609         sleep 1
6610
6611         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6612                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6613         fi
6614
6615         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6616                     grep -c writeback)
6617         if [[ $WRITEBACK -eq 0 ]]; then
6618                 error "No page in writeback, writeback=$WRITEBACK"
6619         fi
6620
6621         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6622         set_nodes_failloc "$(osts_nodes)" 0
6623
6624         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6625         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6626                     grep -c writeback)
6627         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6628                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6629         fi
6630
6631         rm -f $DIR/$tfile
6632         echo "Dirty pages gaurenteed flushed via fsync"
6633         return 0
6634 }
6635 run_test 118d "Fsync validation inject a delay of the bulk =========="
6636
6637 test_118f() {
6638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6639         reset_async
6640
6641         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6642         lctl set_param fail_loc=0x8000040a
6643
6644         # Should simulate EINVAL error which is fatal
6645         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6646         RC=$?
6647         if [[ $RC -eq 0 ]]; then
6648                 error "Must return error due to dropped pages, rc=$RC"
6649         fi
6650
6651         lctl set_param fail_loc=0x0
6652
6653         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6654         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6655         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6656                     grep -c writeback)
6657         if [[ $LOCKED -ne 0 ]]; then
6658                 error "Locked pages remain in cache, locked=$LOCKED"
6659         fi
6660
6661         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6662                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6663         fi
6664
6665         rm -f $DIR/$tfile
6666         echo "No pages locked after fsync"
6667
6668         reset_async
6669         return 0
6670 }
6671 run_test 118f "Simulate unrecoverable OSC side error =========="
6672
6673 test_118g() {
6674         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6675         reset_async
6676
6677         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6678         lctl set_param fail_loc=0x406
6679
6680         # simulate local -ENOMEM
6681         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6682         RC=$?
6683
6684         lctl set_param fail_loc=0
6685         if [[ $RC -eq 0 ]]; then
6686                 error "Must return error due to dropped pages, rc=$RC"
6687         fi
6688
6689         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6690         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6691         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6692                         grep -c writeback)
6693         if [[ $LOCKED -ne 0 ]]; then
6694                 error "Locked pages remain in cache, locked=$LOCKED"
6695         fi
6696
6697         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6698                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6699         fi
6700
6701         rm -f $DIR/$tfile
6702         echo "No pages locked after fsync"
6703
6704         reset_async
6705         return 0
6706 }
6707 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6708
6709 test_118h() {
6710         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6711         remote_ost_nodsh && skip "remote OST with nodsh" && return
6712
6713         reset_async
6714
6715         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6716         set_nodes_failloc "$(osts_nodes)" 0x20e
6717         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6718         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6719         RC=$?
6720
6721         set_nodes_failloc "$(osts_nodes)" 0
6722         if [[ $RC -eq 0 ]]; then
6723                 error "Must return error due to dropped pages, rc=$RC"
6724         fi
6725
6726         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6727         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6728         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6729                     grep -c writeback)
6730         if [[ $LOCKED -ne 0 ]]; then
6731                 error "Locked pages remain in cache, locked=$LOCKED"
6732         fi
6733
6734         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6735                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6736         fi
6737
6738         rm -f $DIR/$tfile
6739         echo "No pages locked after fsync"
6740
6741         return 0
6742 }
6743 run_test 118h "Verify timeout in handling recoverables errors  =========="
6744
6745 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6746
6747 test_118i() {
6748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6749         remote_ost_nodsh && skip "remote OST with nodsh" && return
6750
6751         reset_async
6752
6753         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6754         set_nodes_failloc "$(osts_nodes)" 0x20e
6755
6756         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6757         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6758         PID=$!
6759         sleep 5
6760         set_nodes_failloc "$(osts_nodes)" 0
6761
6762         wait $PID
6763         RC=$?
6764         if [[ $RC -ne 0 ]]; then
6765                 error "got error, but should be not, rc=$RC"
6766         fi
6767
6768         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6769         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6770         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6771         if [[ $LOCKED -ne 0 ]]; then
6772                 error "Locked pages remain in cache, locked=$LOCKED"
6773         fi
6774
6775         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6776                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6777         fi
6778
6779         rm -f $DIR/$tfile
6780         echo "No pages locked after fsync"
6781
6782         return 0
6783 }
6784 run_test 118i "Fix error before timeout in recoverable error  =========="
6785
6786 [ "$SLOW" = "no" ] && set_resend_count 4
6787
6788 test_118j() {
6789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6790         remote_ost_nodsh && skip "remote OST with nodsh" && return
6791
6792         reset_async
6793
6794         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6795         set_nodes_failloc "$(osts_nodes)" 0x220
6796
6797         # return -EIO from OST
6798         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6799         RC=$?
6800         set_nodes_failloc "$(osts_nodes)" 0x0
6801         if [[ $RC -eq 0 ]]; then
6802                 error "Must return error due to dropped pages, rc=$RC"
6803         fi
6804
6805         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6806         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6807         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6808         if [[ $LOCKED -ne 0 ]]; then
6809                 error "Locked pages remain in cache, locked=$LOCKED"
6810         fi
6811
6812         # in recoverable error on OST we want resend and stay until it finished
6813         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6814                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6815         fi
6816
6817         rm -f $DIR/$tfile
6818         echo "No pages locked after fsync"
6819
6820         return 0
6821 }
6822 run_test 118j "Simulate unrecoverable OST side error =========="
6823
6824 test_118k()
6825 {
6826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6827         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6828
6829         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6830         set_nodes_failloc "$(osts_nodes)" 0x20e
6831         test_mkdir -p $DIR/$tdir
6832
6833         for ((i=0;i<10;i++)); do
6834                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6835                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6836                 SLEEPPID=$!
6837                 sleep 0.500s
6838                 kill $SLEEPPID
6839                 wait $SLEEPPID
6840         done
6841
6842         set_nodes_failloc "$(osts_nodes)" 0
6843         rm -rf $DIR/$tdir
6844 }
6845 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6846
6847 test_118l()
6848 {
6849         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6850         # LU-646
6851         test_mkdir -p $DIR/$tdir
6852         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6853         rm -rf $DIR/$tdir
6854 }
6855 run_test 118l "fsync dir ========="
6856
6857 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6858
6859 test_119a() # bug 11737
6860 {
6861         BSIZE=$((512 * 1024))
6862         directio write $DIR/$tfile 0 1 $BSIZE
6863         # We ask to read two blocks, which is more than a file size.
6864         # directio will indicate an error when requested and actual
6865         # sizes aren't equeal (a normal situation in this case) and
6866         # print actual read amount.
6867         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6868         if [ "$NOB" != "$BSIZE" ]; then
6869                 error "read $NOB bytes instead of $BSIZE"
6870         fi
6871         rm -f $DIR/$tfile
6872 }
6873 run_test 119a "Short directIO read must return actual read amount"
6874
6875 test_119b() # bug 11737
6876 {
6877         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6878
6879         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6880         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6881         sync
6882         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6883                 error "direct read failed"
6884         rm -f $DIR/$tfile
6885 }
6886 run_test 119b "Sparse directIO read must return actual read amount"
6887
6888 test_119c() # bug 13099
6889 {
6890         BSIZE=1048576
6891         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6892         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6893         rm -f $DIR/$tfile
6894 }
6895 run_test 119c "Testing for direct read hitting hole"
6896
6897 test_119d() # bug 15950
6898 {
6899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6900         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6901         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6902         BSIZE=1048576
6903         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6904         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6905         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6906         lctl set_param fail_loc=0x40d
6907         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6908         pid_dio=$!
6909         sleep 1
6910         cat $DIR/$tfile > /dev/null &
6911         lctl set_param fail_loc=0
6912         pid_reads=$!
6913         wait $pid_dio
6914         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6915         sleep 2
6916         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6917         error "the read rpcs have not completed in 2s"
6918         rm -f $DIR/$tfile
6919         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6920 }
6921 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6922
6923 test_120a() {
6924         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6925         test_mkdir -p $DIR/$tdir
6926         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6927                skip "no early lock cancel on server" && return 0
6928         lru_resize_disable mdc
6929         lru_resize_disable osc
6930         cancel_lru_locks mdc
6931         stat $DIR/$tdir > /dev/null
6932         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6933         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6934         test_mkdir $DIR/$tdir/d1
6935         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6936         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6937         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6938         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6939         lru_resize_enable mdc
6940         lru_resize_enable osc
6941 }
6942 run_test 120a "Early Lock Cancel: mkdir test"
6943
6944 test_120b() {
6945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6946         test_mkdir -p $DIR/$tdir
6947         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6948                skip "no early lock cancel on server" && return 0
6949         lru_resize_disable mdc
6950         lru_resize_disable osc
6951         cancel_lru_locks mdc
6952         stat $DIR/$tdir > /dev/null
6953         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6954         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6955         touch $DIR/$tdir/f1
6956         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6957         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6958         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6959         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6960         lru_resize_enable mdc
6961         lru_resize_enable osc
6962 }
6963 run_test 120b "Early Lock Cancel: create test"
6964
6965 test_120c() {
6966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6967         test_mkdir -p $DIR/$tdir
6968         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6969                skip "no early lock cancel on server" && return 0
6970         lru_resize_disable mdc
6971         lru_resize_disable osc
6972         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6973         touch $DIR/$tdir/d1/f1
6974         cancel_lru_locks mdc
6975         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6976         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6977         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6978         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6979         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6980         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6981         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6982         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6983         lru_resize_enable mdc
6984         lru_resize_enable osc
6985 }
6986 run_test 120c "Early Lock Cancel: link test"
6987
6988 test_120d() {
6989         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6990         test_mkdir -p $DIR/$tdir
6991         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6992                skip "no early lock cancel on server" && return 0
6993         lru_resize_disable mdc
6994         lru_resize_disable osc
6995         touch $DIR/$tdir
6996         cancel_lru_locks mdc
6997         stat $DIR/$tdir > /dev/null
6998         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6999         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7000         chmod a+x $DIR/$tdir
7001         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
7002         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
7003         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7004         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7005         lru_resize_enable mdc
7006         lru_resize_enable osc
7007 }
7008 run_test 120d "Early Lock Cancel: setattr test"
7009
7010 test_120e() {
7011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7012         test_mkdir -p $DIR/$tdir
7013         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7014                skip "no early lock cancel on server" && return 0
7015         lru_resize_disable mdc
7016         lru_resize_disable osc
7017         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7018         cancel_lru_locks mdc
7019         cancel_lru_locks osc
7020         dd if=$DIR/$tdir/f1 of=/dev/null
7021         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7022         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7023               awk '/ldlm_cancel/ {print $2}'`
7024         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7025               awk '/ldlm_bl_callback/ {print $2}'`
7026         unlink $DIR/$tdir/f1
7027         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7028               awk '/ldlm_cancel/ {print $2}'`
7029         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7030               awk '/ldlm_bl_callback/ {print $2}'`
7031         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7032         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7033         lru_resize_enable mdc
7034         lru_resize_enable osc
7035 }
7036 run_test 120e "Early Lock Cancel: unlink test"
7037
7038 test_120f() {
7039         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7040         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7041                skip "no early lock cancel on server" && return 0
7042         test_mkdir -p $DIR/$tdir
7043         lru_resize_disable mdc
7044         lru_resize_disable osc
7045         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
7046         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7047         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7048         cancel_lru_locks mdc
7049         cancel_lru_locks osc
7050         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7051         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7052         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7053         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7054               awk '/ldlm_cancel/ {print $2}'`
7055         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7056               awk '/ldlm_bl_callback/ {print $2}'`
7057         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7058         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7059               awk '/ldlm_cancel/ {print $2}'`
7060         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7061               awk '/ldlm_bl_callback/ {print $2}'`
7062         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7063         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7064         lru_resize_enable mdc
7065         lru_resize_enable osc
7066 }
7067 run_test 120f "Early Lock Cancel: rename test"
7068
7069 test_120g() {
7070         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7071         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7072                skip "no early lock cancel on server" && return 0
7073         lru_resize_disable mdc
7074         lru_resize_disable osc
7075         count=10000
7076         echo create $count files
7077         test_mkdir -p $DIR/$tdir
7078         cancel_lru_locks mdc
7079         cancel_lru_locks osc
7080         t0=`date +%s`
7081
7082         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7083               awk '/ldlm_cancel/ {print $2}'`
7084         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7085               awk '/ldlm_bl_callback/ {print $2}'`
7086         createmany -o $DIR/$tdir/f $count
7087         sync
7088         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7089               awk '/ldlm_cancel/ {print $2}'`
7090         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7091               awk '/ldlm_bl_callback/ {print $2}'`
7092         t1=`date +%s`
7093         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7094         echo rm $count files
7095         rm -r $DIR/$tdir
7096         sync
7097         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
7098               awk '/ldlm_cancel/ {print $2}'`
7099         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
7100               awk '/ldlm_bl_callback/ {print $2}'`
7101         t2=`date +%s`
7102         echo total: $count removes in $((t2-t1))
7103         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7104         sleep 2
7105         # wait for commitment of removal
7106         lru_resize_enable mdc
7107         lru_resize_enable osc
7108 }
7109 run_test 120g "Early Lock Cancel: performance test"
7110
7111 test_121() { #bug #10589
7112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7113         rm -rf $DIR/$tfile
7114         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7115 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7116         lctl set_param fail_loc=0x310
7117         cancel_lru_locks osc > /dev/null
7118         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7119         lctl set_param fail_loc=0
7120         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
7121 }
7122 run_test 121 "read cancel race ========="
7123
7124 test_123a() { # was test 123, statahead(bug 11401)
7125         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7126         SLOWOK=0
7127         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7128             log "testing on UP system. Performance may be not as good as expected."
7129                         SLOWOK=1
7130         fi
7131
7132         rm -rf $DIR/$tdir
7133         test_mkdir -p $DIR/$tdir
7134         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
7135         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7136         MULT=10
7137         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7138                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7139
7140                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7141                 lctl set_param -n llite.*.statahead_max 0
7142                 lctl get_param llite.*.statahead_max
7143                 cancel_lru_locks mdc
7144                 cancel_lru_locks osc
7145                 stime=`date +%s`
7146                 time ls -l $DIR/$tdir | wc -l
7147                 etime=`date +%s`
7148                 delta=$((etime - stime))
7149                 log "ls $i files without statahead: $delta sec"
7150                 lctl set_param llite.*.statahead_max=$max
7151
7152                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7153                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7154                 cancel_lru_locks mdc
7155                 cancel_lru_locks osc
7156                 stime=`date +%s`
7157                 time ls -l $DIR/$tdir | wc -l
7158                 etime=`date +%s`
7159                 delta_sa=$((etime - stime))
7160                 log "ls $i files with statahead: $delta_sa sec"
7161                 lctl get_param -n llite.*.statahead_stats
7162                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7163
7164                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
7165                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
7166
7167                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7168                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7169                     lctl set_param -n llite.*.statahead_max 0
7170                     lctl get_param llite.*.statahead_max
7171                     cancel_lru_locks mdc
7172                     cancel_lru_locks osc
7173                     stime=`date +%s`
7174                     time ls -l $DIR/$tdir | wc -l
7175                     etime=`date +%s`
7176                     delta=$((etime - stime))
7177                     log "ls $i files again without statahead: $delta sec"
7178                     lctl set_param llite.*.statahead_max=$max
7179                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7180                         if [  $SLOWOK -eq 0 ]; then
7181                                 error "ls $i files is slower with statahead!"
7182                         else
7183                                 log "ls $i files is slower with statahead!"
7184                         fi
7185                         break
7186                     fi
7187                 fi
7188
7189                 [ $delta -gt 20 ] && break
7190                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7191                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7192         done
7193         log "ls done"
7194
7195         stime=`date +%s`
7196         rm -r $DIR/$tdir
7197         sync
7198         etime=`date +%s`
7199         delta=$((etime - stime))
7200         log "rm -r $DIR/$tdir/: $delta seconds"
7201         log "rm done"
7202         lctl get_param -n llite.*.statahead_stats
7203 }
7204 run_test 123a "verify statahead work"
7205
7206 test_123b () { # statahead(bug 15027)
7207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7208         test_mkdir -p $DIR/$tdir
7209         createmany -o $DIR/$tdir/$tfile-%d 1000
7210
7211         cancel_lru_locks mdc
7212         cancel_lru_locks osc
7213
7214 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7215         lctl set_param fail_loc=0x80000803
7216         ls -lR $DIR/$tdir > /dev/null
7217         log "ls done"
7218         lctl set_param fail_loc=0x0
7219         lctl get_param -n llite.*.statahead_stats
7220         rm -r $DIR/$tdir
7221         sync
7222
7223 }
7224 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7225
7226 test_124a() {
7227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7228         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7229                skip "no lru resize on server" && return 0
7230         local NR=2000
7231         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7232
7233         log "create $NR files at $DIR/$tdir"
7234         createmany -o $DIR/$tdir/f $NR ||
7235                 error "failed to create $NR files in $DIR/$tdir"
7236
7237         cancel_lru_locks mdc
7238         ls -l $DIR/$tdir > /dev/null
7239
7240         local NSDIR=""
7241         local LRU_SIZE=0
7242         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7243                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7244                 LRU_SIZE=$(lctl get_param -n $PARAM)
7245                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7246                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7247                                                 log "NSDIR=$NSDIR"
7248                         log "NS=$(basename $NSDIR)"
7249                         break
7250                 fi
7251         done
7252
7253         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7254                 skip "Not enough cached locks created!"
7255                 return 0
7256         fi
7257         log "LRU=$LRU_SIZE"
7258
7259         local SLEEP=30
7260
7261         # We know that lru resize allows one client to hold $LIMIT locks
7262         # for 10h. After that locks begin to be killed by client.
7263         local MAX_HRS=10
7264         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7265                 log "LIMIT=$LIMIT"
7266
7267         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7268         # killing locks. Some time was spent for creating locks. This means
7269         # that up to the moment of sleep finish we must have killed some of
7270         # them (10-100 locks). This depends on how fast ther were created.
7271         # Many of them were touched in almost the same moment and thus will
7272         # be killed in groups.
7273         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7274
7275         # Use $LRU_SIZE_B here to take into account real number of locks
7276         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7277         local LRU_SIZE_B=$LRU_SIZE
7278         log "LVF=$LVF"
7279         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7280                 log "OLD_LVF=$OLD_LVF"
7281         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7282
7283         # Let's make sure that we really have some margin. Client checks
7284         # cached locks every 10 sec.
7285         SLEEP=$((SLEEP+20))
7286         log "Sleep ${SLEEP} sec"
7287         local SEC=0
7288         while ((SEC<$SLEEP)); do
7289                 echo -n "..."
7290                 sleep 5
7291                 SEC=$((SEC+5))
7292                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7293                 echo -n "$LRU_SIZE"
7294         done
7295         echo ""
7296         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7297         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7298
7299         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7300                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7301                 unlinkmany $DIR/$tdir/f $NR
7302                 return
7303         }
7304
7305         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7306         log "unlink $NR files at $DIR/$tdir"
7307         unlinkmany $DIR/$tdir/f $NR
7308 }
7309 run_test 124a "lru resize ======================================="
7310
7311 get_max_pool_limit()
7312 {
7313         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7314         local max=0
7315         for l in $limit; do
7316                 if test $l -gt $max; then
7317                         max=$l
7318                 fi
7319         done
7320         echo $max
7321 }
7322
7323 test_124b() {
7324         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7325         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7326                skip "no lru resize on server" && return 0
7327
7328         LIMIT=`get_max_pool_limit`
7329
7330         NR=$(($(default_lru_size)*20))
7331         if [ $NR -gt $LIMIT ]; then
7332                 log "Limit lock number by $LIMIT locks"
7333                 NR=$LIMIT
7334         fi
7335         lru_resize_disable mdc
7336         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7337                 error "failed to create $DIR/$tdir/disable_lru_resize"
7338
7339         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7340         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7341         cancel_lru_locks mdc
7342         stime=`date +%s`
7343         PID=""
7344         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7345         PID="$PID $!"
7346         sleep 2
7347         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7348         PID="$PID $!"
7349         sleep 2
7350         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7351         PID="$PID $!"
7352         wait $PID
7353         etime=`date +%s`
7354         nolruresize_delta=$((etime-stime))
7355         log "ls -la time: $nolruresize_delta seconds"
7356         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7357         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7358
7359         lru_resize_enable mdc
7360         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7361                 error "failed to create $DIR/$tdir/enable_lru_resize"
7362
7363         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7364         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7365         cancel_lru_locks mdc
7366         stime=`date +%s`
7367         PID=""
7368         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7369         PID="$PID $!"
7370         sleep 2
7371         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7372         PID="$PID $!"
7373         sleep 2
7374         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7375         PID="$PID $!"
7376         wait $PID
7377         etime=`date +%s`
7378         lruresize_delta=$((etime-stime))
7379         log "ls -la time: $lruresize_delta seconds"
7380         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7381
7382         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7383                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7384         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7385                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7386         else
7387                 log "lru resize performs the same with no lru resize"
7388         fi
7389         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7390 }
7391 run_test 124b "lru resize (performance test) ======================="
7392
7393 test_125() { # 13358
7394         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7395         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7396         test_mkdir -p $DIR/d125 || error "mkdir failed"
7397         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7398         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7399         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7400 }
7401 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7402
7403 test_126() { # bug 12829/13455
7404         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7405         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7406         $GSS && skip "must run as gss disabled" && return
7407
7408         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7409         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7410         rm -f $DIR/$tfile
7411         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7412 }
7413 run_test 126 "check that the fsgid provided by the client is taken into account"
7414
7415 test_127a() { # bug 15521
7416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7417         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7418         $LCTL set_param osc.*.stats=0
7419         FSIZE=$((2048 * 1024))
7420         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7421         cancel_lru_locks osc
7422         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7423
7424         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7425         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7426                 echo "got $COUNT $NAME"
7427                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7428                 eval $NAME=$COUNT || error "Wrong proc format"
7429
7430                 case $NAME in
7431                         read_bytes|write_bytes)
7432                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7433                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7434                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7435                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7436                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7437                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7438                                 error "sumsquare is too small: $SUMSQ"
7439                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7440                                 error "sumsquare is too big: $SUMSQ"
7441                         ;;
7442                         *) ;;
7443                 esac
7444         done < $DIR/${tfile}.tmp
7445
7446         #check that we actually got some stats
7447         [ "$read_bytes" ] || error "Missing read_bytes stats"
7448         [ "$write_bytes" ] || error "Missing write_bytes stats"
7449         [ "$read_bytes" != 0 ] || error "no read done"
7450         [ "$write_bytes" != 0 ] || error "no write done"
7451 }
7452 run_test 127a "verify the client stats are sane"
7453
7454 test_127b() { # bug LU-333
7455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7456         $LCTL set_param llite.*.stats=0
7457         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7458         # perform 2 reads and writes so MAX is different from SUM.
7459         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7460         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7461         cancel_lru_locks osc
7462         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7463         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7464
7465         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7466         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7467                 echo "got $COUNT $NAME"
7468                 eval $NAME=$COUNT || error "Wrong proc format"
7469
7470         case $NAME in
7471                 read_bytes)
7472                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7473                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7474                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7475                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7476                         ;;
7477                 write_bytes)
7478                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7479                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7480                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7481                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7482                         ;;
7483                         *) ;;
7484                 esac
7485         done < $TMP/${tfile}.tmp
7486
7487         #check that we actually got some stats
7488         [ "$read_bytes" ] || error "Missing read_bytes stats"
7489         [ "$write_bytes" ] || error "Missing write_bytes stats"
7490         [ "$read_bytes" != 0 ] || error "no read done"
7491         [ "$write_bytes" != 0 ] || error "no write done"
7492 }
7493 run_test 127b "verify the llite client stats are sane"
7494
7495 test_128() { # bug 15212
7496         touch $DIR/$tfile
7497         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7498                 find $DIR/$tfile
7499                 find $DIR/$tfile
7500         EOF
7501
7502         result=$(grep error $TMP/$tfile.log)
7503         rm -f $DIR/$tfile
7504         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7505 }
7506 run_test 128 "interactive lfs for 2 consecutive find's"
7507
7508 set_dir_limits () {
7509         local mntdev
7510         local canondev
7511         local node
7512
7513         local LDPROC=/proc/fs/ldiskfs
7514         local facets=$(get_facets MDS)
7515
7516         for facet in ${facets//,/ }; do
7517                 canondev=$(ldiskfs_canon \
7518                            *.$(convert_facet2label $facet).mntdev $facet)
7519                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7520                                                 LDPROC=/sys/fs/ldiskfs
7521                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7522         done
7523 }
7524
7525 test_129() {
7526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7527         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7528                 skip "Only applicable to ldiskfs-based MDTs"
7529                 return
7530         fi
7531         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7532
7533         EFBIG=27
7534         MAX=16384
7535
7536         set_dir_limits $MAX
7537         test_mkdir -p $DIR/$tdir
7538
7539         local I=0
7540         local J=0
7541         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7542                 $MULTIOP $DIR/$tdir/$J Oc
7543                 rc=$?
7544                 if [ $rc -eq $EFBIG ]; then
7545                         set_dir_limits 0
7546                         echo "return code $rc received as expected"
7547                         return 0
7548                 elif [ $rc -ne 0 ]; then
7549                         set_dir_limits 0
7550                         error_exit "return code $rc received instead of expected $EFBIG"
7551                 fi
7552                 J=$((J+1))
7553                 I=$(stat -c%s "$DIR/$tdir")
7554         done
7555
7556         set_dir_limits 0
7557         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7558 }
7559 run_test 129 "test directory size limit ========================"
7560
7561 OLDIFS="$IFS"
7562 cleanup_130() {
7563         trap 0
7564         IFS="$OLDIFS"
7565 }
7566
7567 test_130a() {
7568         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7569         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7570                 return
7571
7572         trap cleanup_130 EXIT RETURN
7573
7574         local fm_file=$DIR/$tfile
7575         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7576         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7577                 error "dd failed for $fm_file"
7578
7579         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7580         filefrag -ves $fm_file
7581         RC=$?
7582         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7583                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7584         [ $RC != 0 ] && error "filefrag $fm_file failed"
7585
7586         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7587                       grep -v "ext:" | grep -v "found")
7588         lun=$($GETSTRIPE -i $fm_file)
7589
7590         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7591         IFS=$'\n'
7592         tot_len=0
7593         for line in $filefrag_op
7594         do
7595                 frag_lun=`echo $line | cut -d: -f5`
7596                 ext_len=`echo $line | cut -d: -f4`
7597                 if (( $frag_lun != $lun )); then
7598                         cleanup_130
7599                         error "FIEMAP on 1-stripe file($fm_file) failed"
7600                         return
7601                 fi
7602                 (( tot_len += ext_len ))
7603         done
7604
7605         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7606                 cleanup_130
7607                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7608                 return
7609         fi
7610
7611         cleanup_130
7612
7613         echo "FIEMAP on single striped file succeeded"
7614 }
7615 run_test 130a "FIEMAP (1-stripe file)"
7616
7617 test_130b() {
7618         [ "$OSTCOUNT" -lt "2" ] &&
7619                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7620
7621         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7622         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7623                 return
7624
7625         trap cleanup_130 EXIT RETURN
7626
7627         local fm_file=$DIR/$tfile
7628         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7629         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7630                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7631
7632         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7633                 error "dd failed on $fm_file"
7634
7635         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7636         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7637                       grep -v "ext:" | grep -v "found")
7638
7639         last_lun=$(echo $filefrag_op | cut -d: -f5)
7640
7641         IFS=$'\n'
7642         tot_len=0
7643         num_luns=1
7644         for line in $filefrag_op
7645         do
7646                 frag_lun=`echo $line | cut -d: -f5`
7647                 ext_len=`echo $line | cut -d: -f4`
7648                 if (( $frag_lun != $last_lun )); then
7649                         if (( tot_len != 1024 )); then
7650                                 cleanup_130
7651                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7652                                 return
7653                         else
7654                                 (( num_luns += 1 ))
7655                                 tot_len=0
7656                         fi
7657                 fi
7658                 (( tot_len += ext_len ))
7659                 last_lun=$frag_lun
7660         done
7661         if (( num_luns != 2 || tot_len != 1024 )); then
7662                 cleanup_130
7663                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7664                 return
7665         fi
7666
7667         cleanup_130
7668
7669         echo "FIEMAP on 2-stripe file succeeded"
7670 }
7671 run_test 130b "FIEMAP (2-stripe file)"
7672
7673 test_130c() {
7674         [ "$OSTCOUNT" -lt "2" ] &&
7675                 skip_env "skipping FIEMAP on 2-stripe file" && return
7676
7677         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7678         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7679                 return
7680
7681         trap cleanup_130 EXIT RETURN
7682
7683         local fm_file=$DIR/$tfile
7684         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7685         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7686                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7687
7688         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7689
7690         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7691         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7692
7693         last_lun=`echo $filefrag_op | cut -d: -f5`
7694
7695         IFS=$'\n'
7696         tot_len=0
7697         num_luns=1
7698         for line in $filefrag_op
7699         do
7700                 frag_lun=`echo $line | cut -d: -f5`
7701                 ext_len=`echo $line | cut -d: -f4`
7702                 if (( $frag_lun != $last_lun )); then
7703                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7704                         if (( logical != 512 )); then
7705                                 cleanup_130
7706                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7707                                 return
7708                         fi
7709                         if (( tot_len != 512 )); then
7710                                 cleanup_130
7711                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7712                                 return
7713                         else
7714                                 (( num_luns += 1 ))
7715                                 tot_len=0
7716                         fi
7717                 fi
7718                 (( tot_len += ext_len ))
7719                 last_lun=$frag_lun
7720         done
7721         if (( num_luns != 2 || tot_len != 512 )); then
7722                 cleanup_130
7723                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7724                 return
7725         fi
7726
7727         cleanup_130
7728
7729         echo "FIEMAP on 2-stripe file with hole succeeded"
7730 }
7731 run_test 130c "FIEMAP (2-stripe file with hole)"
7732
7733 test_130d() {
7734         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7735
7736         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7737         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7738
7739         trap cleanup_130 EXIT RETURN
7740
7741         local fm_file=$DIR/$tfile
7742         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7743         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7744                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7745         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7746
7747         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7748         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7749
7750         last_lun=`echo $filefrag_op | cut -d: -f5`
7751
7752         IFS=$'\n'
7753         tot_len=0
7754         num_luns=1
7755         for line in $filefrag_op
7756         do
7757                 frag_lun=`echo $line | cut -d: -f5`
7758                 ext_len=`echo $line | cut -d: -f4`
7759                 if (( $frag_lun != $last_lun )); then
7760                         if (( tot_len != 1024 )); then
7761                                 cleanup_130
7762                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7763                                 return
7764                         else
7765                                 (( num_luns += 1 ))
7766                                 tot_len=0
7767                         fi
7768                 fi
7769                 (( tot_len += ext_len ))
7770                 last_lun=$frag_lun
7771         done
7772         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7773                 cleanup_130
7774                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7775                 return
7776         fi
7777
7778         cleanup_130
7779
7780         echo "FIEMAP on N-stripe file succeeded"
7781 }
7782 run_test 130d "FIEMAP (N-stripe file)"
7783
7784 test_130e() {
7785         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7786
7787         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7788         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7789
7790         trap cleanup_130 EXIT RETURN
7791
7792         local fm_file=$DIR/$tfile
7793         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7794         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7795                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7796
7797         NUM_BLKS=512
7798         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7799         for ((i = 0; i < $NUM_BLKS; i++))
7800         do
7801                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7802         done
7803
7804         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7805         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7806
7807         last_lun=`echo $filefrag_op | cut -d: -f5`
7808
7809         IFS=$'\n'
7810         tot_len=0
7811         num_luns=1
7812         for line in $filefrag_op
7813         do
7814                 frag_lun=`echo $line | cut -d: -f5`
7815                 ext_len=`echo $line | cut -d: -f4`
7816                 if (( $frag_lun != $last_lun )); then
7817                         if (( tot_len != $EXPECTED_LEN )); then
7818                                 cleanup_130
7819                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7820                                 return
7821                         else
7822                                 (( num_luns += 1 ))
7823                                 tot_len=0
7824                         fi
7825                 fi
7826                 (( tot_len += ext_len ))
7827                 last_lun=$frag_lun
7828         done
7829         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7830                 cleanup_130
7831                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7832                 return
7833         fi
7834
7835         cleanup_130
7836
7837         echo "FIEMAP with continuation calls succeeded"
7838 }
7839 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7840
7841 # Test for writev/readv
7842 test_131a() {
7843         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7844         error "writev test failed"
7845         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7846         error "readv failed"
7847         rm -f $DIR/$tfile
7848 }
7849 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7850
7851 test_131b() {
7852         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7853         error "append writev test failed"
7854         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7855         error "append writev test failed"
7856         rm -f $DIR/$tfile
7857 }
7858 run_test 131b "test append writev"
7859
7860 test_131c() {
7861         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7862         error "NOT PASS"
7863 }
7864 run_test 131c "test read/write on file w/o objects"
7865
7866 test_131d() {
7867         rwv -f $DIR/$tfile -w -n 1 1572864
7868         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7869         if [ "$NOB" != 1572864 ]; then
7870                 error "Short read filed: read $NOB bytes instead of 1572864"
7871         fi
7872         rm -f $DIR/$tfile
7873 }
7874 run_test 131d "test short read"
7875
7876 test_131e() {
7877         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7878         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7879         error "read hitting hole failed"
7880         rm -f $DIR/$tfile
7881 }
7882 run_test 131e "test read hitting hole"
7883
7884 get_ost_param() {
7885         local token=$1
7886         local gl_sum=0
7887         for node in $(osts_nodes); do
7888                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7889                 [ x$gl = x"" ] && gl=0
7890                 gl_sum=$((gl_sum + gl))
7891         done
7892         echo $gl
7893 }
7894
7895 som_mode_switch() {
7896         local som=$1
7897         local gl1=$2
7898         local gl2=$3
7899
7900         if [ x$som = x"enabled" ]; then
7901                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7902                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7903                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7904         else
7905                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7906                 MOUNTOPT="$MOUNTOPT,som_preview"
7907                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7908         fi
7909
7910         # do remount to make new mount-conf parameters actual
7911         echo remounting...
7912         sync
7913         stopall
7914         setupall
7915 }
7916
7917 test_132() { #1028, SOM
7918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7919         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7920         local num=$(get_mds_dir $DIR)
7921         local mymds=mds${num}
7922         local MOUNTOPT_SAVE=$MOUNTOPT
7923
7924         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7925         cancel_lru_locks osc
7926
7927         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7928
7929         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7930         stat $DIR/$tfile >/dev/null
7931         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7932         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7933         rm $DIR/$tfile
7934         som_mode_switch $som1 $gl1 $gl2
7935
7936         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7937         cancel_lru_locks osc
7938
7939         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7940         if [ $som1 == $som2 ]; then
7941             error "som is still "$som2
7942             if [ x$som2 = x"enabled" ]; then
7943                 som2="disabled"
7944             else
7945                 som2="enabled"
7946             fi
7947         fi
7948
7949         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7950         stat $DIR/$tfile >/dev/null
7951         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7952         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7953         som_mode_switch $som2 $gl1 $gl2
7954         MOUNTOPT=$MOUNTOPT_SAVE
7955 }
7956 run_test 132 "som avoids glimpse rpc"
7957
7958 check_stats() {
7959         local res
7960         local count
7961         case $1 in
7962         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7963                  ;;
7964         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7965                  ;;
7966         *) error "Wrong argument $1" ;;
7967         esac
7968         echo $res
7969         count=`echo $res | awk '{print $2}'`
7970         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7971         # if the argument $3 is zero, it means any stat increment is ok.
7972         if [ $3 -gt 0 ] ; then
7973                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7974         fi
7975 }
7976
7977 test_133a() {
7978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7979         remote_ost_nodsh && skip "remote OST with nodsh" && return
7980         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7981
7982         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7983                 { skip "MDS doesn't support rename stats"; return; }
7984         local testdir=$DIR/${tdir}/stats_testdir
7985         mkdir -p $DIR/${tdir}
7986
7987         # clear stats.
7988         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7989         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7990
7991         # verify mdt stats first.
7992         mkdir ${testdir} || error "mkdir failed"
7993         check_stats $SINGLEMDS "mkdir" 1
7994         touch ${testdir}/${tfile} || "touch failed"
7995         check_stats $SINGLEMDS "open" 1
7996         check_stats $SINGLEMDS "close" 1
7997         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7998         check_stats $SINGLEMDS "mknod" 1
7999         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8000         check_stats $SINGLEMDS "unlink" 1
8001         rm -f ${testdir}/${tfile} || error "file remove failed"
8002         check_stats $SINGLEMDS "unlink" 2
8003
8004         # remove working dir and check mdt stats again.
8005         rmdir ${testdir} || error "rmdir failed"
8006         check_stats $SINGLEMDS "rmdir" 1
8007
8008         local testdir1=$DIR/${tdir}/stats_testdir1
8009         mkdir -p ${testdir}
8010         mkdir -p ${testdir1}
8011         touch ${testdir1}/test1
8012         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8013         check_stats $SINGLEMDS "crossdir_rename" 1
8014
8015         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8016         check_stats $SINGLEMDS "samedir_rename" 1
8017
8018         rm -rf $DIR/${tdir}
8019 }
8020 run_test 133a "Verifying MDT stats ========================================"
8021
8022 test_133b() {
8023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8024         remote_ost_nodsh && skip "remote OST with nodsh" && return
8025         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8026         local testdir=$DIR/${tdir}/stats_testdir
8027         mkdir -p ${testdir} || error "mkdir failed"
8028         touch ${testdir}/${tfile} || "touch failed"
8029         cancel_lru_locks mdc
8030
8031         # clear stats.
8032         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8033         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8034
8035         # extra mdt stats verification.
8036         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8037         check_stats $SINGLEMDS "setattr" 1
8038         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8039         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8040         then            # LU-1740
8041                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8042                 check_stats $SINGLEMDS "getattr" 1
8043         fi
8044         $LFS df || error "lfs failed"
8045         check_stats $SINGLEMDS "statfs" 1
8046
8047         rm -rf $DIR/${tdir}
8048 }
8049 run_test 133b "Verifying extra MDT stats =================================="
8050
8051 test_133c() {
8052         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8053         remote_ost_nodsh && skip "remote OST with nodsh" && return
8054         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8055         local testdir=$DIR/${tdir}/stats_testdir
8056         test_mkdir -p ${testdir} || error "mkdir failed"
8057
8058         # verify obdfilter stats.
8059         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8060         sync
8061         cancel_lru_locks osc
8062         wait_delete_completed
8063
8064         # clear stats.
8065         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8066         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8067
8068         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8069         sync
8070         cancel_lru_locks osc
8071         check_stats ost "write" 1
8072
8073         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8074         check_stats ost "read" 1
8075
8076         > ${testdir}/${tfile} || error "truncate failed"
8077         check_stats ost "punch" 1
8078
8079         rm -f ${testdir}/${tfile} || error "file remove failed"
8080         wait_delete_completed
8081         check_stats ost "destroy" 1
8082
8083         rm -rf $DIR/${tdir}
8084 }
8085 run_test 133c "Verifying OST stats ========================================"
8086
8087 order_2() {
8088     local value=$1
8089     local orig=$value
8090     local order=1
8091
8092     while [ $value -ge 2 ]; do
8093         order=$((order*2))
8094         value=$((value/2))
8095     done
8096
8097     if [ $orig -gt $order ]; then
8098         order=$((order*2))
8099     fi
8100     echo $order
8101 }
8102
8103 size_in_KMGT() {
8104     local value=$1
8105     local size=('K' 'M' 'G' 'T');
8106     local i=0
8107     local size_string=$value
8108
8109     while [ $value -ge 1024 ]; do
8110         if [ $i -gt 3 ]; then
8111             #T is the biggest unit we get here, if that is bigger,
8112             #just return XXXT
8113             size_string=${value}T
8114             break
8115         fi
8116         value=$((value >> 10))
8117         if [ $value -lt 1024 ]; then
8118             size_string=${value}${size[$i]}
8119             break
8120         fi
8121         i=$((i + 1))
8122     done
8123
8124     echo $size_string
8125 }
8126
8127 get_rename_size() {
8128     local size=$1
8129     local context=${2:-.}
8130     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8131                 grep -A1 $context |
8132                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8133     echo $sample
8134 }
8135
8136 test_133d() {
8137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8138     remote_ost_nodsh && skip "remote OST with nodsh" && return
8139     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8140     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8141         { skip "MDS doesn't support rename stats"; return; }
8142
8143     local testdir1=$DIR/${tdir}/stats_testdir1
8144     local testdir2=$DIR/${tdir}/stats_testdir2
8145
8146     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8147
8148     test_mkdir -p ${testdir1} || error "mkdir failed"
8149     test_mkdir -p ${testdir2} || error "mkdir failed"
8150
8151     createmany -o $testdir1/test 512 || error "createmany failed"
8152
8153         # check samedir rename size
8154         mv ${testdir1}/test0 ${testdir1}/test_0
8155
8156         local testdir1_size=$(ls -l $DIR/${tdir} |
8157                 awk '/stats_testdir1/ {print $5}')
8158         local testdir2_size=$(ls -l $DIR/${tdir} |
8159                 awk '/stats_testdir2/ {print $5}')
8160
8161         testdir1_size=$(order_2 $testdir1_size)
8162         testdir2_size=$(order_2 $testdir2_size)
8163
8164         testdir1_size=$(size_in_KMGT $testdir1_size)
8165         testdir2_size=$(size_in_KMGT $testdir2_size)
8166
8167         echo "source rename dir size: ${testdir1_size}"
8168         echo "target rename dir size: ${testdir2_size}"
8169
8170     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8171     eval $cmd || error "$cmd failed"
8172     local samedir=$($cmd | grep 'same_dir')
8173     local same_sample=$(get_rename_size $testdir1_size)
8174     [ -z "$samedir" ] && error "samedir_rename_size count error"
8175     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
8176     echo "Check same dir rename stats success"
8177
8178     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8179
8180     # check crossdir rename size
8181     mv ${testdir1}/test_0 ${testdir2}/test_0
8182
8183         testdir1_size=$(ls -l $DIR/${tdir} |
8184                 awk '/stats_testdir1/ {print $5}')
8185         testdir2_size=$(ls -l $DIR/${tdir} |
8186                 awk '/stats_testdir2/ {print $5}')
8187
8188         testdir1_size=$(order_2 $testdir1_size)
8189         testdir2_size=$(order_2 $testdir2_size)
8190
8191         testdir1_size=$(size_in_KMGT $testdir1_size)
8192         testdir2_size=$(size_in_KMGT $testdir2_size)
8193
8194         echo "source rename dir size: ${testdir1_size}"
8195         echo "target rename dir size: ${testdir2_size}"
8196
8197     eval $cmd || error "$cmd failed"
8198     local crossdir=$($cmd | grep 'crossdir')
8199     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8200     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8201     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8202     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8203     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8204     echo "Check cross dir rename stats success"
8205     rm -rf $DIR/${tdir}
8206 }
8207 run_test 133d "Verifying rename_stats ========================================"
8208
8209 test_133e() {
8210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8211         local testdir=$DIR/${tdir}/stats_testdir
8212         local ctr f0 f1 bs=32768 count=42 sum
8213
8214         remote_ost_nodsh && skip "remote OST with nodsh" && return
8215         mkdir -p ${testdir} || error "mkdir failed"
8216
8217         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8218
8219         for ctr in {write,read}_bytes; do
8220                 sync
8221                 cancel_lru_locks osc
8222
8223                 do_facet ost1 $LCTL set_param -n \
8224                         "obdfilter.*.exports.clear=clear"
8225
8226                 if [ $ctr = write_bytes ]; then
8227                         f0=/dev/zero
8228                         f1=${testdir}/${tfile}
8229                 else
8230                         f0=${testdir}/${tfile}
8231                         f1=/dev/null
8232                 fi
8233
8234                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8235                         error "dd failed"
8236                 sync
8237                 cancel_lru_locks osc
8238
8239                 sum=$(do_facet ost1 $LCTL get_param \
8240                                 "obdfilter.*.exports.*.stats" | \
8241                           awk -v ctr=$ctr '\
8242                                 BEGIN { sum = 0 }
8243                                 $1 == ctr { sum += $7 }
8244                                 END { print sum }')
8245
8246                 if ((sum != bs * count)); then
8247                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8248                 fi
8249         done
8250
8251         rm -rf $DIR/${tdir}
8252 }
8253 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8254
8255 test_140() { #bug-17379
8256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8257         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8258         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8259         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8260
8261         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8262         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8263         local i=0
8264         while i=`expr $i + 1`; do
8265                 test_mkdir -p $i || error "Creating dir $i"
8266                 cd $i || error "Changing to $i"
8267                 ln -s ../stat stat || error "Creating stat symlink"
8268                 # Read the symlink until ELOOP present,
8269                 # not LBUGing the system is considered success,
8270                 # we didn't overrun the stack.
8271                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8272                 [ $ret -ne 0 ] && {
8273                         if [ $ret -eq 40 ]; then
8274                                 break  # -ELOOP
8275                         else
8276                                 error "Open stat symlink"
8277                                 return
8278                         fi
8279                 }
8280         done
8281         i=`expr $i - 1`
8282         echo "The symlink depth = $i"
8283         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8284                                         error "Invalid symlink depth"
8285
8286         # Test recursive symlink
8287         ln -s symlink_self symlink_self
8288         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8289         echo "open symlink_self returns $ret"
8290         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8291 }
8292 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8293
8294 test_150() {
8295         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8296         local TF="$TMP/$tfile"
8297
8298         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8299         cp $TF $DIR/$tfile
8300         cancel_lru_locks osc
8301         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8302         remount_client $MOUNT
8303         df -P $MOUNT
8304         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8305
8306         $TRUNCATE $TF 6000
8307         $TRUNCATE $DIR/$tfile 6000
8308         cancel_lru_locks osc
8309         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8310
8311         echo "12345" >>$TF
8312         echo "12345" >>$DIR/$tfile
8313         cancel_lru_locks osc
8314         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8315
8316         echo "12345" >>$TF
8317         echo "12345" >>$DIR/$tfile
8318         cancel_lru_locks osc
8319         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8320
8321         rm -f $TF
8322         true
8323 }
8324 run_test 150 "truncate/append tests"
8325
8326 function roc_hit() {
8327         local list=$(comma_list $(osts_nodes))
8328
8329         echo $(get_osd_param $list '' stats |
8330                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
8331 }
8332
8333 function set_cache() {
8334         local on=1
8335
8336         if [ "$2" == "off" ]; then
8337                 on=0;
8338         fi
8339         local list=$(comma_list $(osts_nodes))
8340         set_osd_param $list '' $1_cache_enable $on
8341
8342         cancel_lru_locks osc
8343 }
8344
8345 test_151() {
8346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8347         remote_ost_nodsh && skip "remote OST with nodsh" && return
8348
8349         local CPAGES=3
8350         local list=$(comma_list $(osts_nodes))
8351
8352         # check whether obdfilter is cache capable at all
8353         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
8354                 echo "not cache-capable obdfilter"
8355                 return 0
8356         fi
8357
8358         # check cache is enabled on all obdfilters
8359         if get_osd_param $list '' read_cache_enable | grep 0; then
8360                 echo "oss cache is disabled"
8361                 return 0
8362         fi
8363
8364         set_osd_param $list '' writethrough_cache_enable 1
8365
8366         # pages should be in the case right after write
8367         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
8368         local BEFORE=`roc_hit`
8369         cancel_lru_locks osc
8370         cat $DIR/$tfile >/dev/null
8371         local AFTER=`roc_hit`
8372         if ! let "AFTER - BEFORE == CPAGES"; then
8373                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8374         fi
8375
8376         # the following read invalidates the cache
8377         cancel_lru_locks osc
8378         set_osd_param $list '' read_cache_enable 0
8379         cat $DIR/$tfile >/dev/null
8380
8381         # now data shouldn't be found in the cache
8382         BEFORE=`roc_hit`
8383         cancel_lru_locks osc
8384         cat $DIR/$tfile >/dev/null
8385         AFTER=`roc_hit`
8386         if let "AFTER - BEFORE != 0"; then
8387                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8388         fi
8389
8390         set_osd_param $list '' read_cache_enable 1
8391         rm -f $DIR/$tfile
8392 }
8393 run_test 151 "test cache on oss and controls ==============================="
8394
8395 test_152() {
8396         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8397         local TF="$TMP/$tfile"
8398
8399         # simulate ENOMEM during write
8400 #define OBD_FAIL_OST_NOMEM      0x226
8401         lctl set_param fail_loc=0x80000226
8402         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8403         cp $TF $DIR/$tfile
8404         sync || error "sync failed"
8405         lctl set_param fail_loc=0
8406
8407         # discard client's cache
8408         cancel_lru_locks osc
8409
8410         # simulate ENOMEM during read
8411         lctl set_param fail_loc=0x80000226
8412         cmp $TF $DIR/$tfile || error "cmp failed"
8413         lctl set_param fail_loc=0
8414
8415         rm -f $TF
8416 }
8417 run_test 152 "test read/write with enomem ============================"
8418
8419 test_153() {
8420         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
8421 }
8422 run_test 153 "test if fdatasync does not crash ======================="
8423
8424 test_154() {
8425         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8426         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8427                 { skip "Need MDS version at least 2.2.51"; return 0; }
8428
8429         cp /etc/hosts $DIR/$tfile
8430
8431         fid=$($LFS path2fid $DIR/$tfile)
8432         rc=$?
8433         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8434
8435         ffid=$DIR/.lustre/fid/$fid
8436
8437         echo "stat fid $fid"
8438         stat $ffid > /dev/null || error "stat $ffid failed."
8439         echo "touch fid $fid"
8440         touch $ffid || error "touch $ffid failed."
8441         echo "write to fid $fid"
8442         cat /etc/hosts > $ffid || error "write $ffid failed."
8443         echo "read fid $fid"
8444         diff /etc/hosts $ffid || error "read $ffid failed."
8445         echo "append write to fid $fid"
8446         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8447         echo "rename fid $fid"
8448         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
8449         touch $DIR/$tfile.1
8450         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
8451         rm -f $DIR/$tfile.1
8452         echo "truncate fid $fid"
8453         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8454         echo "link fid $fid"
8455         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
8456         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8457                 echo "setfacl fid $fid"
8458                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8459                 echo "getfacl fid $fid"
8460                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8461         fi
8462         echo "unlink fid $fid"
8463         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
8464         echo "mknod fid $fid"
8465         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8466
8467         fid=[0xf00000400:0x1:0x0]
8468         ffid=$DIR/.lustre/fid/$fid
8469
8470         echo "stat non-exist fid $fid"
8471         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8472         echo "write to non-exist fid $fid"
8473         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8474         echo "link new fid $fid"
8475         ln $DIR/$tfile $ffid && error "link $ffid should fail."
8476
8477         test_mkdir -p $DIR/$tdir
8478         touch $DIR/$tdir/$tfile
8479         fid=$($LFS path2fid $DIR/$tdir)
8480         rc=$?
8481         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8482
8483         ffid=$DIR/.lustre/fid/$fid
8484
8485         echo "ls $fid"
8486         ls $ffid > /dev/null || error "ls $ffid failed."
8487         echo "touch $fid/$tfile.1"
8488         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8489
8490         echo "touch $DIR/.lustre/fid/$tfile"
8491         touch $DIR/.lustre/fid/$tfile && \
8492                 error "touch $DIR/.lustre/fid/$tfile should fail."
8493
8494         echo "setxattr to $DIR/.lustre/fid"
8495         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
8496                 error "setxattr should fail."
8497
8498         echo "listxattr for $DIR/.lustre/fid"
8499         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
8500                 error "listxattr should fail."
8501
8502         echo "delxattr from $DIR/.lustre/fid"
8503         setfattr -x trusted.name1 $DIR/.lustre/fid &&
8504                 error "delxattr should fail."
8505
8506         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
8507         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
8508                 error "touch invalid fid should fail."
8509
8510         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
8511         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
8512                 error "touch non-normal fid should fail."
8513
8514         echo "rename $tdir to $DIR/.lustre/fid"
8515         mrename $DIR/$tdir $DIR/.lustre/fid &&
8516                 error "rename to $DIR/.lustre/fid should fail."
8517
8518         echo "rename .lustre to itself"
8519         fid=$($LFS path2fid $DIR)
8520         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
8521                 error "rename .lustre to itself should fail."
8522
8523         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
8524         fid=$($LFS path2fid $DIR/$tfile-2)
8525         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
8526         cp /etc/passwd $DIR/.lustre/fid/$fid &&
8527                 error "create lov data thru .lustre should fail."
8528         echo "cp /etc/passwd $DIR/$tfile-2"
8529         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
8530         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
8531         diff /etc/passwd $DIR/.lustre/fid/$fid ||
8532                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
8533
8534         echo "Open-by-FID succeeded"
8535 }
8536 run_test 154 "Open-by-FID"
8537
8538 test_155_small_load() {
8539     local temp=$TMP/$tfile
8540     local file=$DIR/$tfile
8541
8542     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8543         error "dd of=$temp bs=6096 count=1 failed"
8544     cp $temp $file
8545     cancel_lru_locks osc
8546     cmp $temp $file || error "$temp $file differ"
8547
8548     $TRUNCATE $temp 6000
8549     $TRUNCATE $file 6000
8550     cmp $temp $file || error "$temp $file differ (truncate1)"
8551
8552     echo "12345" >>$temp
8553     echo "12345" >>$file
8554     cmp $temp $file || error "$temp $file differ (append1)"
8555
8556     echo "12345" >>$temp
8557     echo "12345" >>$file
8558     cmp $temp $file || error "$temp $file differ (append2)"
8559
8560     rm -f $temp $file
8561     true
8562 }
8563
8564 test_155_big_load() {
8565     remote_ost_nodsh && skip "remote OST with nodsh" && return
8566     local temp=$TMP/$tfile
8567     local file=$DIR/$tfile
8568
8569     free_min_max
8570     local cache_size=$(do_facet ost$((MAXI+1)) \
8571         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8572     local large_file_size=$((cache_size * 2))
8573
8574     echo "OSS cache size: $cache_size KB"
8575     echo "Large file size: $large_file_size KB"
8576
8577     [ $MAXV -le $large_file_size ] && \
8578         skip_env "max available OST size needs > $large_file_size KB" && \
8579         return 0
8580
8581     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8582
8583     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8584         error "dd of=$temp bs=$large_file_size count=1k failed"
8585     cp $temp $file
8586     ls -lh $temp $file
8587     cancel_lru_locks osc
8588     cmp $temp $file || error "$temp $file differ"
8589
8590     rm -f $temp $file
8591     true
8592 }
8593
8594 test_155a() {
8595         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8596         set_cache read on
8597         set_cache writethrough on
8598         test_155_small_load
8599 }
8600 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8601
8602 test_155b() {
8603         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8604         set_cache read on
8605         set_cache writethrough off
8606         test_155_small_load
8607 }
8608 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8609
8610 test_155c() {
8611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8612         set_cache read off
8613         set_cache writethrough on
8614         test_155_small_load
8615 }
8616 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8617
8618 test_155d() {
8619         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8620         set_cache read off
8621         set_cache writethrough off
8622         test_155_small_load
8623 }
8624 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8625
8626 test_155e() {
8627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8628         set_cache read on
8629         set_cache writethrough on
8630         test_155_big_load
8631 }
8632 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8633
8634 test_155f() {
8635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8636         set_cache read on
8637         set_cache writethrough off
8638         test_155_big_load
8639 }
8640 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8641
8642 test_155g() {
8643         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8644         set_cache read off
8645         set_cache writethrough on
8646         test_155_big_load
8647 }
8648 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8649
8650 test_155h() {
8651         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8652         set_cache read off
8653         set_cache writethrough off
8654         test_155_big_load
8655 }
8656 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8657
8658 test_156() {
8659         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8660         local CPAGES=3
8661         local BEFORE
8662         local AFTER
8663         local file="$DIR/$tfile"
8664
8665         [ "$(facet_fstype ost1)" = "zfs" ] &&
8666                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8667                 return
8668
8669     log "Turn on read and write cache"
8670     set_cache read on
8671     set_cache writethrough on
8672
8673     log "Write data and read it back."
8674     log "Read should be satisfied from the cache."
8675     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8676     BEFORE=`roc_hit`
8677     cancel_lru_locks osc
8678     cat $file >/dev/null
8679     AFTER=`roc_hit`
8680     if ! let "AFTER - BEFORE == CPAGES"; then
8681         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8682     else
8683         log "cache hits:: before: $BEFORE, after: $AFTER"
8684     fi
8685
8686     log "Read again; it should be satisfied from the cache."
8687     BEFORE=$AFTER
8688     cancel_lru_locks osc
8689     cat $file >/dev/null
8690     AFTER=`roc_hit`
8691     if ! let "AFTER - BEFORE == CPAGES"; then
8692         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8693     else
8694         log "cache hits:: before: $BEFORE, after: $AFTER"
8695     fi
8696
8697
8698     log "Turn off the read cache and turn on the write cache"
8699     set_cache read off
8700     set_cache writethrough on
8701
8702     log "Read again; it should be satisfied from the cache."
8703     BEFORE=`roc_hit`
8704     cancel_lru_locks osc
8705     cat $file >/dev/null
8706     AFTER=`roc_hit`
8707     if ! let "AFTER - BEFORE == CPAGES"; then
8708         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8709     else
8710         log "cache hits:: before: $BEFORE, after: $AFTER"
8711     fi
8712
8713     log "Read again; it should not be satisfied from the cache."
8714     BEFORE=$AFTER
8715     cancel_lru_locks osc
8716     cat $file >/dev/null
8717     AFTER=`roc_hit`
8718     if ! let "AFTER - BEFORE == 0"; then
8719         error "IN CACHE: before: $BEFORE, after: $AFTER"
8720     else
8721         log "cache hits:: before: $BEFORE, after: $AFTER"
8722     fi
8723
8724     log "Write data and read it back."
8725     log "Read should be satisfied from the cache."
8726     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8727     BEFORE=`roc_hit`
8728     cancel_lru_locks osc
8729     cat $file >/dev/null
8730     AFTER=`roc_hit`
8731     if ! let "AFTER - BEFORE == CPAGES"; then
8732         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8733     else
8734         log "cache hits:: before: $BEFORE, after: $AFTER"
8735     fi
8736
8737     log "Read again; it should not be satisfied from the cache."
8738     BEFORE=$AFTER
8739     cancel_lru_locks osc
8740     cat $file >/dev/null
8741     AFTER=`roc_hit`
8742     if ! let "AFTER - BEFORE == 0"; then
8743         error "IN CACHE: before: $BEFORE, after: $AFTER"
8744     else
8745         log "cache hits:: before: $BEFORE, after: $AFTER"
8746     fi
8747
8748
8749     log "Turn off read and write cache"
8750     set_cache read off
8751     set_cache writethrough off
8752
8753     log "Write data and read it back"
8754     log "It should not be satisfied from the cache."
8755     rm -f $file
8756     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8757     cancel_lru_locks osc
8758     BEFORE=`roc_hit`
8759     cat $file >/dev/null
8760     AFTER=`roc_hit`
8761     if ! let "AFTER - BEFORE == 0"; then
8762         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8763     else
8764         log "cache hits:: before: $BEFORE, after: $AFTER"
8765     fi
8766
8767
8768     log "Turn on the read cache and turn off the write cache"
8769     set_cache read on
8770     set_cache writethrough off
8771
8772     log "Write data and read it back"
8773     log "It should not be satisfied from the cache."
8774     rm -f $file
8775     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8776     BEFORE=`roc_hit`
8777     cancel_lru_locks osc
8778     cat $file >/dev/null
8779     AFTER=`roc_hit`
8780     if ! let "AFTER - BEFORE == 0"; then
8781         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8782     else
8783         log "cache hits:: before: $BEFORE, after: $AFTER"
8784     fi
8785
8786     log "Read again; it should be satisfied from the cache."
8787     BEFORE=`roc_hit`
8788     cancel_lru_locks osc
8789     cat $file >/dev/null
8790     AFTER=`roc_hit`
8791     if ! let "AFTER - BEFORE == CPAGES"; then
8792         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8793     else
8794         log "cache hits:: before: $BEFORE, after: $AFTER"
8795     fi
8796
8797     rm -f $file
8798 }
8799 run_test 156 "Verification of tunables ============================"
8800
8801 #Changelogs
8802 err17935 () {
8803     if [ $MDSCOUNT -gt 1 ]; then
8804         error_ignore 17935 $*
8805     else
8806         error $*
8807     fi
8808 }
8809
8810 changelog_chmask()
8811 {
8812     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8813            grep -c $1)
8814
8815     if [ $MASK -eq 1 ]; then
8816         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8817     else
8818         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8819     fi
8820 }
8821
8822 test_160() {
8823         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8824     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8825     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8826         { skip "Need MDS version at least 2.2.0"; return; }
8827     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8828     echo "Registered as changelog user $USER"
8829     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8830         grep -q $USER || error "User $USER not found in changelog_users"
8831
8832     # change something
8833     test_mkdir -p $DIR/$tdir/pics/2008/zachy
8834     touch $DIR/$tdir/pics/2008/zachy/timestamp
8835     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8836     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8837     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8838     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8839     rm $DIR/$tdir/pics/desktop.jpg
8840
8841     $LFS changelog $MDT0 | tail -5
8842
8843     echo "verifying changelog mask"
8844     changelog_chmask "MKDIR"
8845     changelog_chmask "CLOSE"
8846
8847     test_mkdir -p $DIR/$tdir/pics/zach/sofia
8848     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8849
8850     changelog_chmask "MKDIR"
8851     changelog_chmask "CLOSE"
8852
8853     test_mkdir -p $DIR/$tdir/pics/2008/sofia
8854     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8855
8856     $LFS changelog $MDT0
8857     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8858     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8859     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8860     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8861
8862     # verify contents
8863     echo "verifying target fid"
8864     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8865         tail -1 | awk '{print $6}')
8866     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8867     [ "$fidc" == "t=$fidf" ] || \
8868         err17935 "fid in changelog $fidc != file fid $fidf"
8869     echo "verifying parent fid"
8870     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8871         tail -1 | awk '{print $7}')
8872     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8873     [ "$fidc" == "p=$fidf" ] || \
8874         err17935 "pfid in changelog $fidc != dir fid $fidf"
8875
8876     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8877         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8878     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8879     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8880         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8881     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8882     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8883         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8884
8885     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8886         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8887     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8888     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8889     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8890         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8891
8892     echo "verifying user deregister"
8893     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8894     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8895         grep -q $USER && error "User $USER still found in changelog_users"
8896
8897     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8898         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8899     if [ $USERS -eq 0 ]; then
8900         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8901             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8902         touch $DIR/$tdir/chloe
8903         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8904             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8905         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8906         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8907     else
8908         echo "$USERS other changelog users; can't verify off"
8909     fi
8910 }
8911 run_test 160 "changelog sanity"
8912
8913 test_161() {
8914         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8915     test_mkdir -p $DIR/$tdir
8916     cp /etc/hosts $DIR/$tdir/$tfile
8917     test_mkdir $DIR/$tdir/foo1
8918     test_mkdir $DIR/$tdir/foo2
8919     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8920     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8921     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8922     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8923     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8924     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8925         $LFS fid2path $DIR $FID
8926         err17935 "bad link ea"
8927     fi
8928     # middle
8929     rm $DIR/$tdir/foo2/zachary
8930     # last
8931     rm $DIR/$tdir/foo2/thor
8932     # first
8933     rm $DIR/$tdir/$tfile
8934     # rename
8935     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8936     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8937         then
8938         $LFS fid2path $DIR $FID
8939         err17935 "bad link rename"
8940     fi
8941     rm $DIR/$tdir/foo2/maggie
8942
8943     # overflow the EA
8944     local longname=filename_avg_len_is_thirty_two_
8945     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8946         error "failed to hardlink many files"
8947     links=$($LFS fid2path $DIR $FID | wc -l)
8948     echo -n "${links}/1000 links in link EA"
8949     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8950     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8951         error "failed to unlink many hardlinks"
8952 }
8953 run_test 161 "link ea sanity"
8954
8955 check_path() {
8956     local expected=$1
8957     shift
8958     local fid=$2
8959
8960     local path=$(${LFS} fid2path $*)
8961     RC=$?
8962
8963     if [ $RC -ne 0 ]; then
8964         err17935 "path looked up of $expected failed. Error $RC"
8965         return $RC
8966     elif [ "${path}" != "${expected}" ]; then
8967         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8968         return 2
8969     fi
8970     echo "fid $fid resolves to path $path (expected $expected)"
8971 }
8972
8973 test_162() {
8974         # Make changes to filesystem
8975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8976         test_mkdir -p $DIR/$tdir/d2
8977         touch $DIR/$tdir/d2/$tfile
8978         touch $DIR/$tdir/d2/x1
8979         touch $DIR/$tdir/d2/x2
8980         test_mkdir -p $DIR/$tdir/d2/a/b/c
8981         test_mkdir -p $DIR/$tdir/d2/p/q/r
8982         # regular file
8983         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8984         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8985
8986         # softlink
8987         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8988         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8989         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8990
8991         # softlink to wrong file
8992         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8993         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8994         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8995
8996         # hardlink
8997         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8998         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8999         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
9000         # fid2path dir/fsname should both work
9001         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
9002         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
9003
9004         # hardlink count: check that there are 2 links
9005         # Doesnt work with CMD yet: 17935
9006         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
9007                 err17935 "expected 2 links"
9008
9009         # hardlink indexing: remove the first link
9010         rm $DIR/$tdir/d2/p/q/r/hlink
9011         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
9012
9013         return 0
9014 }
9015 run_test 162 "path lookup sanity"
9016
9017 test_163() {
9018         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9019         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9020         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
9021         copytool $FSNAME &
9022         sleep 1
9023         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
9024         # this proc file is temporary and linux-only
9025         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
9026          error "kernel->userspace send failed"
9027         kill -INT $!
9028 }
9029 run_test 163 "kernel <-> userspace comms"
9030
9031 test_169() {
9032         # do directio so as not to populate the page cache
9033         log "creating a 10 Mb file"
9034         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9035         log "starting reads"
9036         dd if=$DIR/$tfile of=/dev/null bs=4096 &
9037         log "truncating the file"
9038         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9039         log "killing dd"
9040         kill %+ || true # reads might have finished
9041         echo "wait until dd is finished"
9042         wait
9043         log "removing the temporary file"
9044         rm -rf $DIR/$tfile || error "tmp file removal failed"
9045 }
9046 run_test 169 "parallel read and truncate should not deadlock"
9047
9048 test_170() {
9049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9050         $LCTL clear     # bug 18514
9051         $LCTL debug_daemon start $TMP/${tfile}_log_good
9052         touch $DIR/$tfile
9053         $LCTL debug_daemon stop
9054         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
9055                error "sed failed to read log_good"
9056
9057         $LCTL debug_daemon start $TMP/${tfile}_log_good
9058         rm -rf $DIR/$tfile
9059         $LCTL debug_daemon stop
9060
9061         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
9062                error "lctl df log_bad failed"
9063
9064         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9065         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9066
9067         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
9068         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
9069
9070         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
9071                 error "bad_line good_line1 good_line2 are empty"
9072
9073         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9074         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
9075         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
9076
9077         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
9078         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
9079         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
9080
9081         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
9082                 error "bad_line_new good_line_new are empty"
9083
9084         local expected_good=$((good_line1 + good_line2*2))
9085
9086         rm -f $TMP/${tfile}*
9087         # LU-231, short malformed line may not be counted into bad lines
9088         if [ $bad_line -ne $bad_line_new ] &&
9089                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
9090                 error "expected $bad_line bad lines, but got $bad_line_new"
9091                 return 1
9092         fi
9093
9094         if [ $expected_good -ne $good_line_new ]; then
9095                 error "expected $expected_good good lines, but got $good_line_new"
9096                 return 2
9097         fi
9098         true
9099 }
9100 run_test 170 "test lctl df to handle corrupted log ====================="
9101
9102 test_171() { # bug20592
9103         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9104 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
9105         $LCTL set_param fail_loc=0x50e
9106         $LCTL set_param fail_val=3000
9107         multiop_bg_pause $DIR/$tfile O_s || true
9108         local MULTIPID=$!
9109         kill -USR1 $MULTIPID
9110         # cause log dump
9111         sleep 3
9112         wait $MULTIPID
9113         if dmesg | grep "recursive fault"; then
9114                 error "caught a recursive fault"
9115         fi
9116         $LCTL set_param fail_loc=0
9117         true
9118 }
9119 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
9120
9121 # it would be good to share it with obdfilter-survey/libecho code
9122 setup_obdecho_osc () {
9123         local rc=0
9124         local ost_nid=$1
9125         local obdfilter_name=$2
9126         echo "Creating new osc for $obdfilter_name on $ost_nid"
9127         # make sure we can find loopback nid
9128         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
9129
9130         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
9131                            ${obdfilter_name}_osc_UUID || rc=2; }
9132         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
9133                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
9134         return $rc
9135 }
9136
9137 cleanup_obdecho_osc () {
9138         local obdfilter_name=$1
9139         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
9140         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
9141         return 0
9142 }
9143
9144 obdecho_create_test() {
9145         local OBD=$1
9146         local node=$2
9147         local rc=0
9148         local id
9149         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
9150         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
9151                            rc=2; }
9152         if [ $rc -eq 0 ]; then
9153             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
9154             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
9155         fi
9156         echo "New object id is $id"
9157         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
9158                            rc=4; }
9159         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
9160                                         "cleanup" || rc=5; }
9161         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
9162                                         "detach" || rc=6; }
9163         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
9164         return $rc
9165 }
9166
9167 test_180a() {
9168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9169         remote_ost_nodsh && skip "remote OST with nodsh" && return
9170         local rc=0
9171         local rmmod_local=0
9172
9173         if ! module_loaded obdecho; then
9174             load_module obdecho/obdecho
9175             rmmod_local=1
9176         fi
9177
9178         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
9179         local host=$(lctl get_param -n osc.$osc.import |
9180                              awk '/current_connection:/ {print $2}' )
9181         local target=$(lctl get_param -n osc.$osc.import |
9182                              awk '/target:/ {print $2}' )
9183         target=${target%_UUID}
9184
9185         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
9186         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
9187         [[ -n $target ]] && cleanup_obdecho_osc $target
9188         [ $rmmod_local -eq 1 ] && rmmod obdecho
9189         return $rc
9190 }
9191 run_test 180a "test obdecho on osc"
9192
9193 test_180b() {
9194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9195         remote_ost_nodsh && skip "remote OST with nodsh" && return
9196         local rc=0
9197         local rmmod_remote=0
9198
9199         do_facet ost1 "lsmod | grep -q obdecho || "                      \
9200                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
9201                       "modprobe obdecho; }" && rmmod_remote=1
9202         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
9203         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
9204         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
9205         return $rc
9206 }
9207 run_test 180b "test obdecho directly on obdfilter"
9208
9209 test_181() { # bug 22177
9210         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9211         # create enough files to index the directory
9212         createmany -o $DIR/$tdir/foobar 4000
9213         # print attributes for debug purpose
9214         lsattr -d .
9215         # open dir
9216         multiop_bg_pause $DIR/$tdir D_Sc || return 1
9217         MULTIPID=$!
9218         # remove the files & current working dir
9219         unlinkmany $DIR/$tdir/foobar 4000
9220         rmdir $DIR/$tdir
9221         kill -USR1 $MULTIPID
9222         wait $MULTIPID
9223         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
9224         return 0
9225 }
9226 run_test 181 "Test open-unlinked dir ========================"
9227
9228 test_182() {
9229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9230         # disable MDC RPC lock wouldn't crash client
9231         local fcount=1000
9232         local tcount=4
9233
9234         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9235 #define OBD_FAIL_MDC_RPCS_SEM           0x804
9236         $LCTL set_param fail_loc=0x804
9237
9238         for (( i=0; i < $tcount; i++ )) ; do
9239                 mkdir $DIR/$tdir/$i
9240                 createmany -o $DIR/$tdir/$i/f- $fcount &
9241         done
9242         wait
9243
9244         for (( i=0; i < $tcount; i++ )) ; do
9245                 unlinkmany $DIR/$tdir/$i/f- $fcount &
9246         done
9247         wait
9248
9249         rm -rf $DIR/$tdir
9250
9251         $LCTL set_param fail_loc=0
9252 }
9253 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
9254
9255 test_183() { # LU-2275
9256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9257         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9258         echo aaa > $DIR/$tdir/$tfile
9259
9260 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
9261         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
9262
9263         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
9264         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
9265
9266         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
9267
9268         # Flush negative dentry cache
9269         touch $DIR/$tdir/$tfile
9270
9271         # We are not checking for any leaked references here, they'll
9272         # become evident next time we do cleanup with module unload.
9273         rm -rf $DIR/$tdir
9274 }
9275 run_test 183 "No crash or request leak in case of strange dispositions ========"
9276
9277 test_185() { # LU-2441
9278         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9279         touch $DIR/$tdir/spoo
9280         local mtime1=$(stat -c "%Y" $DIR/$tdir)
9281         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
9282                 error "cannot create/write a volatile file"
9283         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
9284                 error "FID is still valid after close"
9285
9286         multiop_bg_pause $DIR/$tdir vVw4096_c
9287         local multi_pid=$!
9288
9289         local OLD_IFS=$IFS
9290         IFS=":"
9291         local fidv=($fid)
9292         IFS=$OLD_IFS
9293         # assume that the next FID for this client is sequential, since stdout
9294         # is unfortunately eaten by multiop_bg_pause
9295         local n=$((${fidv[1]} + 1))
9296         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
9297         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
9298                 error "FID is missing before close"
9299         kill -USR1 $multi_pid
9300         # 1 second delay, so if mtime change we will see it
9301         sleep 1
9302         local mtime2=$(stat -c "%Y" $DIR/$tdir)
9303         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
9304 }
9305 run_test 185 "Volatile file support"
9306
9307 check_swap_layouts_support()
9308 {
9309         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
9310                 { skip "Does not support layout lock."; return 0; }
9311         return 1
9312 }
9313
9314 # test suite 184 is for LU-2016, LU-2017
9315 test_184a() {
9316         check_swap_layouts_support && return 0
9317
9318         dir0=$DIR/$tdir/$testnum
9319         test_mkdir -p $dir0 || error "creating dir $dir0"
9320         ref1=/etc/passwd
9321         ref2=/etc/group
9322         file1=$dir0/f1
9323         file2=$dir0/f2
9324         $SETSTRIPE -c1 $file1
9325         cp $ref1 $file1
9326         $SETSTRIPE -c2 $file2
9327         cp $ref2 $file2
9328         gen1=$($GETSTRIPE -g $file1)
9329         gen2=$($GETSTRIPE -g $file2)
9330
9331         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
9332         gen=$($GETSTRIPE -g $file1)
9333         [[ $gen1 != $gen ]] ||
9334                 "Layout generation on $file1 does not change"
9335         gen=$($GETSTRIPE -g $file2)
9336         [[ $gen2 != $gen ]] ||
9337                 "Layout generation on $file2 does not change"
9338
9339         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
9340         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
9341 }
9342 run_test 184a "Basic layout swap"
9343
9344 test_184b() {
9345         check_swap_layouts_support && return 0
9346
9347         dir0=$DIR/$tdir/$testnum
9348         mkdir -p $dir0 || error "creating dir $dir0"
9349         file1=$dir0/f1
9350         file2=$dir0/f2
9351         file3=$dir0/f3
9352         dir1=$dir0/d1
9353         dir2=$dir0/d2
9354         mkdir $dir1 $dir2
9355         $SETSTRIPE -c1 $file1
9356         $SETSTRIPE -c2 $file2
9357         $SETSTRIPE -c1 $file3
9358         chown $RUNAS_ID $file3
9359         gen1=$($GETSTRIPE -g $file1)
9360         gen2=$($GETSTRIPE -g $file2)
9361
9362         $LFS swap_layouts $dir1 $dir2 &&
9363                 error "swap of directories layouts should fail"
9364         $LFS swap_layouts $dir1 $file1 &&
9365                 error "swap of directory and file layouts should fail"
9366         $RUNAS $LFS swap_layouts $file1 $file2 &&
9367                 error "swap of file we cannot write should fail"
9368         $LFS swap_layouts $file1 $file3 &&
9369                 error "swap of file with different owner should fail"
9370         /bin/true # to clear error code
9371 }
9372 run_test 184b "Forbidden layout swap (will generate errors)"
9373
9374 test_184c() {
9375         check_swap_layouts_support && return 0
9376
9377         dir0=$DIR/$tdir/$testnum
9378         mkdir -p $dir0 || error "creating dir $dir0"
9379         ref1=$dir0/ref1
9380         ref2=$dir0/ref2
9381         file1=$dir0/file1
9382         file2=$dir0/file2
9383         # create a file large enough for the concurent test
9384         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
9385         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
9386         echo "ref file size: ref1(`stat -c %s $ref1`), ref2(`stat -c %s $ref2`)"
9387
9388         cp $ref2 $file2
9389         dd if=$ref1 of=$file1 bs=64k &
9390         sleep 0.$((RANDOM % 5 + 1))
9391
9392         $LFS swap_layouts $file1 $file2
9393         rc=$?
9394         wait $DD_PID
9395         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
9396         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
9397
9398         # how many bytes copied before swapping layout
9399         local copied=`stat -c %s $file2`
9400         local remaining=`stat -c %s $ref1`
9401         remaining=$((remaining - copied))
9402         echo "Copied $copied bytes before swapping layout..."
9403
9404         cmp -n $copied $file1 $ref2 ||
9405                 error "Content mismatch [0, $copied) of ref2 and file1"
9406         cmp -n $copied $file2 $ref1 ||
9407                 error "Content mismatch [0, $copied) of ref1 and file2"
9408         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
9409                 error "Content mismatch [$copied, EOF) of ref1 and file1"
9410
9411         # clean up
9412         rm -f $ref1 $ref2 $file1 $file2
9413 }
9414 run_test 184c "Concurrent write and layout swap"
9415
9416 # OST pools tests
9417 check_file_in_pool()
9418 {
9419         local file=$1
9420         local pool=$2
9421         local tlist="$3"
9422         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
9423         for i in $res
9424         do
9425                 for t in $tlist ; do
9426                         [ "$i" -eq "$t" ] && continue 2
9427                 done
9428
9429                 echo "pool list: $tlist"
9430                 echo "striping: $res"
9431                 error_noexit "$file not allocated in $pool"
9432                 return 1
9433         done
9434         return 0
9435 }
9436
9437 pool_add() {
9438         echo "Creating new pool"
9439         local pool=$1
9440
9441         create_pool $FSNAME.$pool ||
9442                 { error_noexit "No pool created, result code $?"; return 1; }
9443         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
9444                 { error_noexit "$pool not in lfs pool_list"; return 2; }
9445 }
9446
9447 pool_add_targets() {
9448         echo "Adding targets to pool"
9449         local pool=$1
9450         local first=$2
9451         local last=$3
9452         local step=${4:-1}
9453
9454         local list=$(seq $first $step $last)
9455
9456         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
9457         do_facet mgs $LCTL pool_add \
9458                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
9459         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
9460                         | sort -u | tr '\n' ' ' " "$t" || { 
9461                 error_noexit "Add to pool failed"
9462                 return 1
9463         }
9464         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
9465         local addcount=$(((last - first) / step + 1))
9466         [ $lfscount -eq $addcount ] || {
9467                 error_noexit "lfs pool_list bad ost count" \
9468                                                 "$lfscount != $addcount"
9469                 return 2
9470         }
9471 }
9472
9473 pool_set_dir() {
9474         local pool=$1
9475         local tdir=$2
9476         echo "Setting pool on directory $tdir"
9477
9478         $SETSTRIPE -c 2 -p $pool $tdir && return 0
9479
9480         error_noexit "Cannot set pool $pool to $tdir"
9481         return 1
9482 }
9483
9484 pool_check_dir() {
9485         local pool=$1
9486         local tdir=$2
9487         echo "Checking pool on directory $tdir"
9488
9489         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
9490         [ "$res" = "$pool" ] && return 0
9491
9492         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
9493         return 1
9494 }
9495
9496 pool_dir_rel_path() {
9497         echo "Testing relative path works well"
9498         local pool=$1
9499         local tdir=$2
9500         local root=$3
9501
9502         mkdir -p $root/$tdir/$tdir
9503         cd $root/$tdir
9504         pool_set_dir $pool $tdir          || return 1
9505         pool_set_dir $pool ./$tdir        || return 2
9506         pool_set_dir $pool ../$tdir       || return 3
9507         pool_set_dir $pool ../$tdir/$tdir || return 4
9508         rm -rf $tdir; cd - > /dev/null
9509 }
9510
9511 pool_alloc_files() {
9512         echo "Checking files allocation from directory pool"
9513         local pool=$1
9514         local tdir=$2
9515         local count=$3
9516         local tlist="$4"
9517
9518         local failed=0
9519         for i in $(seq -w 1 $count)
9520         do
9521                 local file=$tdir/file-$i
9522                 touch $file
9523                 check_file_in_pool $file $pool "$tlist" || \
9524                         failed=$((failed + 1))
9525         done
9526         [ "$failed" = 0 ] && return 0
9527
9528         error_noexit "$failed files not allocated in $pool"
9529         return 1
9530 }
9531
9532 pool_create_files() {
9533         echo "Creating files in pool"
9534         local pool=$1
9535         local tdir=$2
9536         local count=$3
9537         local tlist="$4"
9538
9539         mkdir -p $tdir
9540         local failed=0
9541         for i in $(seq -w 1 $count)
9542         do
9543                 local file=$tdir/spoo-$i
9544                 $SETSTRIPE -p $pool $file
9545                 check_file_in_pool $file $pool "$tlist" || \
9546                         failed=$((failed + 1))
9547         done
9548         [ "$failed" = 0 ] && return 0
9549
9550         error_noexit "$failed files not allocated in $pool"
9551         return 1
9552 }
9553
9554 pool_lfs_df() {
9555         echo "Checking 'lfs df' output"
9556         local pool=$1
9557
9558         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
9559                         tr '\n' ' ')
9560         local res=$($LFS df --pool $FSNAME.$pool |
9561                         awk '{print $1}' |
9562                         grep "$FSNAME-OST" |
9563                         tr '\n' ' ')
9564         [ "$res" = "$t" ] && return 0
9565
9566         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
9567         return 1
9568 }
9569
9570 pool_file_rel_path() {
9571         echo "Creating files in a pool with relative pathname"
9572         local pool=$1
9573         local tdir=$2
9574
9575         mkdir -p $tdir ||
9576                 { error_noexit "unable to create $tdir"; return 1 ; }
9577         local file="/..$tdir/$tfile-1"
9578         $SETSTRIPE -p $pool $file ||
9579                 { error_noexit "unable to create $file" ; return 2 ; }
9580
9581         cd $tdir
9582         $SETSTRIPE -p $pool $tfile-2 || {
9583                 error_noexit "unable to create $tfile-2 in $tdir"
9584                 return 3
9585         }
9586 }
9587
9588 pool_remove_first_target() {
9589         echo "Removing first target from a pool"
9590         local pool=$1
9591
9592         local pname="lov.$FSNAME-*.pools.$pool"
9593         local t=$($LCTL get_param -n $pname | head -1)
9594         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9595         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9596                 error_noexit "$t not removed from $FSNAME.$pool"
9597                 return 1
9598         }
9599 }
9600
9601 pool_remove_all_targets() {
9602         echo "Removing all targets from pool"
9603         local pool=$1
9604         local file=$2
9605         local pname="lov.$FSNAME-*.pools.$pool"
9606         for t in $($LCTL get_param -n $pname | sort -u)
9607         do
9608                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9609         done
9610         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9611                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9612                 return 1
9613         }
9614         # striping on an empty/nonexistant pool should fall back 
9615         # to "pool of everything"
9616         touch $file || {
9617                 error_noexit "failed to use fallback striping for empty pool"
9618                 return 2
9619         }
9620         # setstripe on an empty pool should fail
9621         $SETSTRIPE -p $pool $file 2>/dev/null && {
9622                 error_noexit "expected failure when creating file" \
9623                                                         "with empty pool"
9624                 return 3
9625         }
9626         return 0
9627 }
9628
9629 pool_remove() {
9630         echo "Destroying pool"
9631         local pool=$1
9632         local file=$2
9633
9634         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9635
9636         sleep 2
9637         # striping on an empty/nonexistant pool should fall back 
9638         # to "pool of everything"
9639         touch $file || {
9640                 error_noexit "failed to use fallback striping for missing pool"
9641                 return 1
9642         }
9643         # setstripe on an empty pool should fail
9644         $SETSTRIPE -p $pool $file 2>/dev/null && {
9645                 error_noexit "expected failure when creating file" \
9646                                                         "with missing pool"
9647                 return 2
9648         }
9649
9650         # get param should return err once pool is gone
9651         if wait_update $HOSTNAME "lctl get_param -n \
9652                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9653         then
9654                 remove_pool_from_list $FSNAME.$pool
9655                 return 0
9656         fi
9657         error_noexit "Pool $FSNAME.$pool is not destroyed"
9658         return 3
9659 }
9660
9661 test_200() {
9662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9663         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9664
9665         local POOL=${POOL:-cea1}
9666         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9667         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9668         # Pool OST targets
9669         local first_ost=0
9670         local last_ost=$(($OSTCOUNT - 1))
9671         local ost_step=2
9672         local ost_list=$(seq $first_ost $ost_step $last_ost)
9673         local ost_range="$first_ost $last_ost $ost_step"
9674         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9675         local file_dir=$POOL_ROOT/file_tst
9676
9677         local rc=0
9678         while : ; do
9679                 # former test_200a test_200b
9680                 pool_add $POOL                          || { rc=$? ; break; }
9681                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9682                 # former test_200c test_200d
9683                 mkdir -p $test_path
9684                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9685                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9686                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9687                                                         || { rc=$? ; break; }
9688                 # former test_200e test_200f
9689                 local files=$((OSTCOUNT*3))
9690                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9691                                                         || { rc=$? ; break; }
9692                 pool_create_files $POOL $file_dir $files "$ost_list" \
9693                                                         || { rc=$? ; break; }
9694                 # former test_200g test_200h
9695                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9696                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9697
9698                 # former test_201a test_201b test_201c
9699                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9700
9701                 local f=$test_path/$tfile
9702                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9703                 pool_remove $POOL $f                    || { rc=$? ; break; }
9704                 break
9705         done
9706
9707         cleanup_pools
9708         return $rc
9709 }
9710 run_test 200 "OST pools"
9711
9712 # usage: default_attr <count | size | offset>
9713 default_attr() {
9714         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9715 }
9716
9717 # usage: check_default_stripe_attr
9718 check_default_stripe_attr() {
9719         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9720         case $1 in
9721         --stripe-count|--count)
9722                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9723         --stripe-size|--size)
9724                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9725         --stripe-index|--index)
9726                 EXPECTED=-1;;
9727         *)
9728                 error "unknown getstripe attr '$1'"
9729         esac
9730
9731         [ $ACTUAL != $EXPECTED ] &&
9732                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9733 }
9734
9735 test_204a() {
9736         test_mkdir -p $DIR/$tdir
9737         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9738
9739         check_default_stripe_attr --stripe-count
9740         check_default_stripe_attr --stripe-size
9741         check_default_stripe_attr --stripe-index
9742
9743         return 0
9744 }
9745 run_test 204a "Print default stripe attributes ================="
9746
9747 test_204b() {
9748         test_mkdir -p $DIR/$tdir
9749         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9750
9751         check_default_stripe_attr --stripe-size
9752         check_default_stripe_attr --stripe-index
9753
9754         return 0
9755 }
9756 run_test 204b "Print default stripe size and offset  ==========="
9757
9758 test_204c() {
9759         test_mkdir -p $DIR/$tdir
9760         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9761
9762         check_default_stripe_attr --stripe-count
9763         check_default_stripe_attr --stripe-index
9764
9765         return 0
9766 }
9767 run_test 204c "Print default stripe count and offset ==========="
9768
9769 test_204d() {
9770         test_mkdir -p $DIR/$tdir
9771         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9772
9773         check_default_stripe_attr --stripe-count
9774         check_default_stripe_attr --stripe-size
9775
9776         return 0
9777 }
9778 run_test 204d "Print default stripe count and size ============="
9779
9780 test_204e() {
9781         test_mkdir -p $DIR/$tdir
9782         $SETSTRIPE -d $DIR/$tdir
9783
9784         check_default_stripe_attr --stripe-count --raw
9785         check_default_stripe_attr --stripe-size --raw
9786         check_default_stripe_attr --stripe-index --raw
9787
9788         return 0
9789 }
9790 run_test 204e "Print raw stripe attributes ================="
9791
9792 test_204f() {
9793         test_mkdir -p $DIR/$tdir
9794         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9795
9796         check_default_stripe_attr --stripe-size --raw
9797         check_default_stripe_attr --stripe-index --raw
9798
9799         return 0
9800 }
9801 run_test 204f "Print raw stripe size and offset  ==========="
9802
9803 test_204g() {
9804         test_mkdir -p $DIR/$tdir
9805         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9806
9807         check_default_stripe_attr --stripe-count --raw
9808         check_default_stripe_attr --stripe-index --raw
9809
9810         return 0
9811 }
9812 run_test 204g "Print raw stripe count and offset ==========="
9813
9814 test_204h() {
9815         test_mkdir -p $DIR/$tdir
9816         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9817
9818         check_default_stripe_attr --stripe-count --raw
9819         check_default_stripe_attr --stripe-size --raw
9820
9821         return 0
9822 }
9823 run_test 204h "Print raw stripe count and size ============="
9824
9825 # Figure out which job scheduler is being used, if any,
9826 # or use a fake one
9827 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9828         JOBENV=SLURM_JOB_ID
9829 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9830         JOBENV=LSB_JOBID
9831 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9832         JOBENV=PBS_JOBID
9833 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9834         JOBENV=LOADL_STEP_ID
9835 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9836         JOBENV=JOB_ID
9837 else
9838         JOBENV=FAKE_JOBID
9839 fi
9840
9841 verify_jobstats() {
9842         local cmd=$1
9843         local target=$2
9844
9845         # clear old jobstats
9846         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9847         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9848
9849         # use a new JobID for this test, or we might see an old one
9850         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9851
9852         JOBVAL=${!JOBENV}
9853         log "Test: $cmd"
9854         log "Using JobID environment variable $JOBENV=$JOBVAL"
9855
9856         if [ $JOBENV = "FAKE_JOBID" ]; then
9857                 FAKE_JOBID=$JOBVAL $cmd
9858         else
9859                 $cmd
9860         fi
9861
9862         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9863                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9864                 do_facet $FACET lctl get_param mdt.*.job_stats |
9865                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9866         fi
9867         if [ "$target" = "ost" -o "$target" = "both" ]; then
9868                 FACET=ost1
9869                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9870                         grep $JOBVAL || error "No job stats found on OST $FACET"
9871         fi
9872 }
9873
9874 test_205() { # Job stats
9875         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9876         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9877                 skip "Server doesn't support jobstats" && return 0
9878
9879         local cmd
9880         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9881         if [ $OLD_JOBENV != $JOBENV ]; then
9882                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9883                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9884                         $JOBENV || return 1
9885         fi
9886
9887         # mkdir
9888         cmd="mkdir $DIR/$tfile"
9889         verify_jobstats "$cmd" "mdt"
9890         # rmdir
9891         cmd="rm -fr $DIR/$tfile"
9892         verify_jobstats "$cmd" "mdt"
9893         # mknod
9894         cmd="mknod $DIR/$tfile c 1 3"
9895         verify_jobstats "$cmd" "mdt"
9896         # unlink
9897         cmd="rm -f $DIR/$tfile"
9898         verify_jobstats "$cmd" "mdt"
9899         # open & close
9900         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9901         verify_jobstats "$cmd" "mdt"
9902         # setattr
9903         cmd="touch $DIR/$tfile"
9904         verify_jobstats "$cmd" "both"
9905         # write
9906         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9907         verify_jobstats "$cmd" "ost"
9908         # read
9909         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9910         verify_jobstats "$cmd" "ost"
9911         # truncate
9912         cmd="$TRUNCATE $DIR/$tfile 0"
9913         verify_jobstats "$cmd" "both"
9914         # rename
9915         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9916         verify_jobstats "$cmd" "mdt"
9917
9918         # cleanup
9919         rm -f $DIR/jobstats_test_rename
9920
9921         if [ $OLD_JOBENV != $JOBENV ]; then
9922                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9923                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9924                         $OLD_JOBENV || return 1
9925         fi
9926 }
9927 run_test 205 "Verify job stats"
9928
9929 # LU-1480, LU-1773 and LU-1657
9930 test_206() {
9931         mkdir -p $DIR/$tdir
9932         lfs setstripe -c -1 $DIR/$tdir
9933 #define OBD_FAIL_LOV_INIT 0x1403
9934         $LCTL set_param fail_loc=0xa0001403
9935         $LCTL set_param fail_val=1
9936         touch $DIR/$tdir/$tfile || true
9937 }
9938 run_test 206 "fail lov_init_raid0() doesn't lbug"
9939
9940 test_207a() {
9941         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
9942         local fsz=`stat -c %s $DIR/$tfile`
9943         cancel_lru_locks mdc
9944
9945         # do not return layout in getattr intent
9946 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
9947         $LCTL set_param fail_loc=0x170
9948         local sz=`stat -c %s $DIR/$tfile`
9949
9950         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
9951
9952         rm -rf $DIR/$tfile
9953 }
9954 run_test 207a "can refresh layout at glimpse"
9955
9956 test_207b() {
9957         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
9958         local cksum=`md5sum $DIR/$tfile`
9959         local fsz=`stat -c %s $DIR/$tfile`
9960         cancel_lru_locks mdc
9961         cancel_lru_locks osc
9962
9963         # do not return layout in getattr intent
9964 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
9965         $LCTL set_param fail_loc=0x171
9966
9967         # it will refresh layout after the file is opened but before read issues
9968         echo checksum is "$cksum"
9969         echo "$cksum" |md5sum -c --quiet || error "file differs"
9970
9971         rm -rf $DIR/$tfile
9972 }
9973 run_test 207b "can refresh layout at open"
9974
9975 test_212() {
9976         size=`date +%s`
9977         size=$((size % 8192 + 1))
9978         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9979         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9980         rm -f $DIR/f212 $DIR/f212.xyz
9981 }
9982 run_test 212 "Sendfile test ============================================"
9983
9984 test_213() {
9985         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9986         cancel_lru_locks osc
9987         lctl set_param fail_loc=0x8000040f
9988         # generate a read lock
9989         cat $DIR/$tfile > /dev/null
9990         # write to the file, it will try to cancel the above read lock.
9991         cat /etc/hosts >> $DIR/$tfile
9992 }
9993 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9994
9995 test_214() { # for bug 20133
9996         test_mkdir -p $DIR/d214p/d214c
9997         for (( i=0; i < 340; i++ )) ; do
9998                 touch $DIR/d214p/d214c/a$i
9999         done
10000
10001         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
10002         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
10003         ls $DIR/d214c || error "ls $DIR/d214c failed"
10004         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
10005 }
10006 run_test 214 "hash-indexed directory test - bug 20133"
10007
10008 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
10009 create_lnet_proc_files() {
10010         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
10011         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
10012
10013         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
10014         rm -f "$TMP/lnet_$1.sys_tmp"
10015 }
10016
10017 # counterpart of create_lnet_proc_files
10018 remove_lnet_proc_files() {
10019         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
10020 }
10021
10022 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10023 # 3rd arg as regexp for body
10024 check_lnet_proc_stats() {
10025         local l=$(cat "$TMP/lnet_$1" |wc -l)
10026         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
10027
10028         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
10029 }
10030
10031 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
10032 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
10033 # optional and can be regexp for 2nd line (lnet.routes case)
10034 check_lnet_proc_entry() {
10035         local blp=2            # blp stands for 'position of 1st line of body'
10036         [ "$5" = "" ] || blp=3 # lnet.routes case
10037
10038         local l=$(cat "$TMP/lnet_$1" |wc -l)
10039         # subtracting one from $blp because the body can be empty
10040         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
10041
10042         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
10043                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
10044
10045         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
10046                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
10047
10048         # bail out if any unexpected line happened
10049         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
10050         [ "$?" != 0 ] || error "$2 misformatted"
10051 }
10052
10053 test_215() { # for bugs 18102, 21079, 21517
10054         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10055         local N='(0|[1-9][0-9]*)'       # non-negative numeric
10056         local P='[1-9][0-9]*'           # positive numeric
10057         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
10058         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
10059         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
10060         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
10061
10062         local L1 # regexp for 1st line
10063         local L2 # regexp for 2nd line (optional)
10064         local BR # regexp for the rest (body)
10065
10066         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
10067         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
10068         create_lnet_proc_files "stats"
10069         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
10070         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
10071         remove_lnet_proc_files "stats"
10072
10073         # /proc/sys/lnet/routes should look like this:
10074         # Routing disabled/enabled
10075         # net hops state router
10076         # where net is a string like tcp0, hops >= 0, state is up/down,
10077         # router is a string like 192.168.1.1@tcp2
10078         L1="^Routing (disabled|enabled)$"
10079         L2="^net +hops +state +router$"
10080         BR="^$NET +$N +(up|down) +$NID$"
10081         create_lnet_proc_files "routes"
10082         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
10083         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
10084         remove_lnet_proc_files "routes"
10085
10086         # /proc/sys/lnet/routers should look like this:
10087         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
10088         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
10089         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
10090         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
10091         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
10092         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
10093         create_lnet_proc_files "routers"
10094         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
10095         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
10096         remove_lnet_proc_files "routers"
10097
10098         # /proc/sys/lnet/peers should look like this:
10099         # nid refs state last max rtr min tx min queue
10100         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
10101         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
10102         # numeric (0 or >0 or <0), queue >= 0.
10103         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
10104         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
10105         create_lnet_proc_files "peers"
10106         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
10107         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
10108         remove_lnet_proc_files "peers"
10109
10110         # /proc/sys/lnet/buffers  should look like this:
10111         # pages count credits min
10112         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
10113         L1="^pages +count +credits +min$"
10114         BR="^ +$N +$N +$I +$I$"
10115         create_lnet_proc_files "buffers"
10116         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
10117         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
10118         remove_lnet_proc_files "buffers"
10119
10120         # /proc/sys/lnet/nis should look like this:
10121         # nid status alive refs peer rtr max tx min
10122         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
10123         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
10124         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
10125         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
10126         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
10127         create_lnet_proc_files "nis"
10128         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
10129         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
10130         remove_lnet_proc_files "nis"
10131
10132         # can we successfully write to /proc/sys/lnet/stats?
10133         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
10134         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
10135 }
10136 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
10137
10138 test_216() { # bug 20317
10139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10140         remote_ost_nodsh && skip "remote OST with nodsh" && return
10141         local node
10142         local p="$TMP/sanityN-$TESTNAME.parameters"
10143         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
10144         for node in $(osts_nodes); do
10145                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
10146                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
10147                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
10148         done
10149         clear_osc_stats
10150
10151         # agressive lockless i/o settings
10152         for node in $(osts_nodes); do
10153                 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'
10154         done
10155         lctl set_param -n osc.*.contention_seconds 60
10156
10157         $DIRECTIO write $DIR/$tfile 0 10 4096
10158         $CHECKSTAT -s 40960 $DIR/$tfile
10159
10160         # disable lockless i/o
10161         for node in $(osts_nodes); do
10162                 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'
10163         done
10164         lctl set_param -n osc.*.contention_seconds 0
10165         clear_osc_stats
10166
10167         dd if=/dev/zero of=$DIR/$tfile count=0
10168         $CHECKSTAT -s 0 $DIR/$tfile
10169
10170         restore_lustre_params <$p
10171         rm -f $p
10172         rm $DIR/$tfile
10173 }
10174 run_test 216 "check lockless direct write works and updates file size and kms correctly"
10175
10176 test_217() { # bug 22430
10177         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10178         local node
10179         local nid
10180
10181         for node in $(nodes_list); do
10182                 nid=$(host_nids_address $node $NETTYPE)
10183                 if [[ $nid = *-* ]] ; then
10184                         echo "lctl ping $nid@$NETTYPE"
10185                         lctl ping $nid@$NETTYPE
10186                 else
10187                         echo "skipping $node (no hyphen detected)"
10188                 fi
10189         done
10190 }
10191 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
10192
10193 test_218() {
10194        # do directio so as not to populate the page cache
10195        log "creating a 10 Mb file"
10196        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10197        log "starting reads"
10198        dd if=$DIR/$tfile of=/dev/null bs=4096 &
10199        log "truncating the file"
10200        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10201        log "killing dd"
10202        kill %+ || true # reads might have finished
10203        echo "wait until dd is finished"
10204        wait
10205        log "removing the temporary file"
10206        rm -rf $DIR/$tfile || error "tmp file removal failed"
10207 }
10208 run_test 218 "parallel read and truncate should not deadlock ======================="
10209
10210 test_219() {
10211         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10212         # write one partial page
10213         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
10214         # set no grant so vvp_io_commit_write will do sync write
10215         $LCTL set_param fail_loc=0x411
10216         # write a full page at the end of file
10217         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
10218
10219         $LCTL set_param fail_loc=0
10220         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
10221         $LCTL set_param fail_loc=0x411
10222         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
10223 }
10224 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
10225
10226 test_220() { #LU-325
10227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10228         remote_ost_nodsh && skip "remote OST with nodsh" && return
10229         local OSTIDX=0
10230
10231         test_mkdir -p $DIR/$tdir
10232         local OST=$(lfs osts | grep ${OSTIDX}": " | \
10233                 awk '{print $2}' | sed -e 's/_UUID$//')
10234
10235         # on the mdt's osc
10236         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
10237         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
10238                         osc.$mdtosc_proc1.prealloc_last_id)
10239         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
10240                         osc.$mdtosc_proc1.prealloc_next_id)
10241
10242         $LFS df -i
10243
10244         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
10245         #define OBD_FAIL_OST_ENOINO              0x229
10246         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
10247         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
10248         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
10249
10250         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
10251
10252         MDSOBJS=$((last_id - next_id))
10253         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
10254
10255         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
10256         echo "OST still has $count kbytes free"
10257
10258         echo "create $MDSOBJS files @next_id..."
10259         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
10260
10261         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10262                         osc.$mdtosc_proc1.prealloc_last_id)
10263         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
10264                         osc.$mdtosc_proc1.prealloc_next_id)
10265
10266         echo "after creation, last_id=$last_id2, next_id=$next_id2"
10267         $LFS df -i
10268
10269         echo "cleanup..."
10270
10271         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
10272         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
10273
10274         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
10275         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
10276         echo "unlink $MDSOBJS files @$next_id..."
10277         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
10278 }
10279 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
10280
10281 test_221() {
10282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10283         dd if=`which date` of=$MOUNT/date oflag=sync
10284         chmod +x $MOUNT/date
10285
10286         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
10287         $LCTL set_param fail_loc=0x80001401
10288
10289         $MOUNT/date > /dev/null
10290         rm -f $MOUNT/date
10291 }
10292 run_test 221 "make sure fault and truncate race to not cause OOM"
10293
10294 test_222a () {
10295         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10296        rm -rf $DIR/$tdir
10297        test_mkdir -p $DIR/$tdir
10298        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10299        createmany -o $DIR/$tdir/$tfile 10
10300        cancel_lru_locks mdc
10301        cancel_lru_locks osc
10302        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10303        $LCTL set_param fail_loc=0x31a
10304        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
10305        $LCTL set_param fail_loc=0
10306        rm -r $DIR/$tdir
10307 }
10308 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
10309
10310 test_222b () {
10311         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10312        rm -rf $DIR/$tdir
10313        test_mkdir -p $DIR/$tdir
10314        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10315        createmany -o $DIR/$tdir/$tfile 10
10316        cancel_lru_locks mdc
10317        cancel_lru_locks osc
10318        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
10319        $LCTL set_param fail_loc=0x31a
10320        rm -r $DIR/$tdir || "AGL for rmdir failed"
10321        $LCTL set_param fail_loc=0
10322 }
10323 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
10324
10325 test_223 () {
10326         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10327        rm -rf $DIR/$tdir
10328        test_mkdir -p $DIR/$tdir
10329        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
10330        createmany -o $DIR/$tdir/$tfile 10
10331        cancel_lru_locks mdc
10332        cancel_lru_locks osc
10333        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
10334        $LCTL set_param fail_loc=0x31b
10335        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
10336        $LCTL set_param fail_loc=0
10337        rm -r $DIR/$tdir
10338 }
10339 run_test 223 "osc reenqueue if without AGL lock granted ======================="
10340
10341 test_224a() { # LU-1039, MRP-303
10342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10343         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
10344         $LCTL set_param fail_loc=0x508
10345         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
10346         $LCTL set_param fail_loc=0
10347         df $DIR
10348 }
10349 run_test 224a "Don't panic on bulk IO failure"
10350
10351 test_224b() { # LU-1039, MRP-303
10352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10353         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
10354         cancel_lru_locks osc
10355         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
10356         $LCTL set_param fail_loc=0x515
10357         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
10358         $LCTL set_param fail_loc=0
10359         df $DIR
10360 }
10361 run_test 224b "Don't panic on bulk IO failure"
10362
10363 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
10364 test_225a () {
10365         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10366        if [ -z ${MDSSURVEY} ]; then
10367               skip_env "mds-survey not found" && return
10368        fi
10369        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10370             { skip "Need MDS version at least 2.2.51"; return; }
10371
10372        local mds=$(facet_host $SINGLEMDS)
10373        local target=$(do_nodes $mds 'lctl dl' | \
10374                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10375
10376        local cmd1="file_count=1000 thrhi=4"
10377        local cmd2="dir_count=2 layer=mdd stripe_count=0"
10378        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10379        local cmd="$cmd1 $cmd2 $cmd3"
10380
10381        rm -f ${TMP}/mds_survey*
10382        echo + $cmd
10383        eval $cmd || error "mds-survey with zero-stripe failed"
10384        cat ${TMP}/mds_survey*
10385        rm -f ${TMP}/mds_survey*
10386 }
10387 run_test 225a "Metadata survey sanity with zero-stripe"
10388
10389 test_225b () {
10390         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10391        if [ -z ${MDSSURVEY} ]; then
10392               skip_env "mds-survey not found" && return
10393        fi
10394        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
10395             { skip "Need MDS version at least 2.2.51"; return; }
10396
10397        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
10398               skip_env "Need to mount OST to test" && return
10399        fi
10400
10401        local mds=$(facet_host $SINGLEMDS)
10402        local target=$(do_nodes $mds 'lctl dl' | \
10403                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
10404
10405        local cmd1="file_count=1000 thrhi=4"
10406        local cmd2="dir_count=2 layer=mdd stripe_count=1"
10407        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
10408        local cmd="$cmd1 $cmd2 $cmd3"
10409
10410        rm -f ${TMP}/mds_survey*
10411        echo + $cmd
10412        eval $cmd || error "mds-survey with stripe_count failed"
10413        cat ${TMP}/mds_survey*
10414        rm -f ${TMP}/mds_survey*
10415 }
10416 run_test 225b "Metadata survey sanity with stripe_count = 1"
10417
10418 mcreate_path2fid () {
10419         local mode=$1
10420         local major=$2
10421         local minor=$3
10422         local name=$4
10423         local desc=$5
10424         local path=$DIR/$tdir/$name
10425         local fid
10426         local rc
10427         local fid_path
10428
10429         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
10430                 error "cannot create $desc"
10431
10432         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
10433         rc=$?
10434         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
10435
10436         fid_path=$($LFS fid2path $MOUNT $fid)
10437         rc=$?
10438         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
10439
10440         [ "$path" == "$fid_path" ] ||
10441                 error "fid2path returned $fid_path, expected $path"
10442
10443         echo "pass with $path and $fid"
10444 }
10445
10446 test_226 () {
10447         rm -rf $DIR/$tdir
10448         mkdir -p $DIR/$tdir
10449
10450         mcreate_path2fid 0010666 0 0 fifo "FIFO"
10451         mcreate_path2fid 0020666 1 3 null "character special file (null)"
10452         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
10453         mcreate_path2fid 0040666 0 0 dir "directory"
10454         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
10455         mcreate_path2fid 0100666 0 0 file "regular file"
10456         mcreate_path2fid 0120666 0 0 link "symbolic link"
10457         mcreate_path2fid 0140666 0 0 sock "socket"
10458 }
10459 run_test 226 "call path2fid and fid2path on files of all type"
10460
10461 # LU-1299 Executing or running ldd on a truncated executable does not
10462 # cause an out-of-memory condition.
10463 test_227() {
10464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10465         dd if=`which date` of=$MOUNT/date bs=1k count=1
10466         chmod +x $MOUNT/date
10467
10468         $MOUNT/date > /dev/null
10469         ldd $MOUNT/date > /dev/null
10470         rm -f $MOUNT/date
10471 }
10472 run_test 227 "running truncated executable does not cause OOM"
10473
10474 # LU-1512 try to reuse idle OI blocks
10475 test_228a() {
10476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10477         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10478                 skip "non-ldiskfs backend" && return
10479
10480         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10481         local myDIR=$DIR/$tdir
10482
10483         mkdir -p $myDIR
10484         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10485         $LCTL set_param fail_loc=0x80001002
10486         createmany -o $myDIR/t- 10000
10487         $LCTL set_param fail_loc=0
10488         # The guard is current the largest FID holder
10489         touch $myDIR/guard
10490         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10491                     tr -d '[')
10492         local IDX=$(($SEQ % 64))
10493
10494         do_facet $SINGLEMDS sync
10495         # Make sure journal flushed.
10496         sleep 6
10497         local blk1=$(do_facet $SINGLEMDS \
10498                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10499                      grep Blockcount | awk '{print $4}')
10500
10501         # Remove old files, some OI blocks will become idle.
10502         unlinkmany $myDIR/t- 10000
10503         # Create new files, idle OI blocks should be reused.
10504         createmany -o $myDIR/t- 2000
10505         do_facet $SINGLEMDS sync
10506         # Make sure journal flushed.
10507         sleep 6
10508         local blk2=$(do_facet $SINGLEMDS \
10509                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10510                      grep Blockcount | awk '{print $4}')
10511
10512         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10513 }
10514 run_test 228a "try to reuse idle OI blocks"
10515
10516 test_228b() {
10517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10518         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10519                 skip "non-ldiskfs backend" && return
10520
10521         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10522         local myDIR=$DIR/$tdir
10523
10524         mkdir -p $myDIR
10525         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10526         $LCTL set_param fail_loc=0x80001002
10527         createmany -o $myDIR/t- 10000
10528         $LCTL set_param fail_loc=0
10529         # The guard is current the largest FID holder
10530         touch $myDIR/guard
10531         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10532                     tr -d '[')
10533         local IDX=$(($SEQ % 64))
10534
10535         do_facet $SINGLEMDS sync
10536         # Make sure journal flushed.
10537         sleep 6
10538         local blk1=$(do_facet $SINGLEMDS \
10539                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10540                      grep Blockcount | awk '{print $4}')
10541
10542         # Remove old files, some OI blocks will become idle.
10543         unlinkmany $myDIR/t- 10000
10544
10545         # stop the MDT
10546         stop $SINGLEMDS || error "Fail to stop MDT."
10547         # remount the MDT
10548         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10549
10550         df $MOUNT || error "Fail to df."
10551         # Create new files, idle OI blocks should be reused.
10552         createmany -o $myDIR/t- 2000
10553         do_facet $SINGLEMDS sync
10554         # Make sure journal flushed.
10555         sleep 6
10556         local blk2=$(do_facet $SINGLEMDS \
10557                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10558                      grep Blockcount | awk '{print $4}')
10559
10560         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10561 }
10562 run_test 228b "idle OI blocks can be reused after MDT restart"
10563
10564 #LU-1881
10565 test_228c() {
10566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10567         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10568                 skip "non-ldiskfs backend" && return
10569
10570         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10571         local myDIR=$DIR/$tdir
10572
10573         mkdir -p $myDIR
10574         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10575         $LCTL set_param fail_loc=0x80001002
10576         # 20000 files can guarantee there are index nodes in the OI file
10577         createmany -o $myDIR/t- 20000
10578         $LCTL set_param fail_loc=0
10579         # The guard is current the largest FID holder
10580         touch $myDIR/guard
10581         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10582                     tr -d '[')
10583         local IDX=$(($SEQ % 64))
10584
10585         do_facet $SINGLEMDS sync
10586         # Make sure journal flushed.
10587         sleep 6
10588         local blk1=$(do_facet $SINGLEMDS \
10589                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10590                      grep Blockcount | awk '{print $4}')
10591
10592         # Remove old files, some OI blocks will become idle.
10593         unlinkmany $myDIR/t- 20000
10594         rm -f $myDIR/guard
10595         # The OI file should become empty now
10596
10597         # Create new files, idle OI blocks should be reused.
10598         createmany -o $myDIR/t- 2000
10599         do_facet $SINGLEMDS sync
10600         # Make sure journal flushed.
10601         sleep 6
10602         local blk2=$(do_facet $SINGLEMDS \
10603                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10604                      grep Blockcount | awk '{print $4}')
10605
10606         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10607 }
10608 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
10609
10610 test_230a() {
10611         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10612         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10613         local MDTIDX=1
10614
10615         mkdir -p $DIR/$tdir/test_230_local
10616         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
10617         [ $mdt_idx -ne 0 ] &&
10618                 error "create local directory on wrong MDT $mdt_idx"
10619
10620         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
10621                         error "create remote directory failed"
10622         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
10623         [ $mdt_idx -ne $MDTIDX ] &&
10624                 error "create remote directory on wrong MDT $mdt_idx"
10625
10626         createmany -o $DIR/$tdir/test_230/t- 10 ||
10627                 error "create files on remote directory failed"
10628         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
10629         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
10630         rm -r $DIR/$tdir || error "unlink remote directory failed"
10631 }
10632 run_test 230a "Create remote directory and files under the remote directory"
10633
10634 test_230b() {
10635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10636         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10637         local MDTIDX=1
10638         local remote_dir=$DIR/$tdir/remote_dir
10639         local rc=0
10640
10641         mkdir -p $DIR/$tdir
10642         $LFS mkdir -i $MDTIDX $remote_dir ||
10643                 error "create remote directory failed"
10644
10645         $LFS mkdir -i 0 $remote_dir/new_dir &&
10646                 error "nested remote directory create succeed!"
10647
10648         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=1
10649         $LFS mkdir -i 0 $remote_dir/new_dir || rc=$?
10650         do_facet mds$((MDTIDX + 1)) lctl set_param mdt.*.enable_remote_dir=0
10651
10652         [ $rc -ne 0 ] &&
10653            error "create remote directory failed after set enable_remote_dir"
10654
10655         rm -r $DIR/$tdir || error "unlink remote directory failed"
10656 }
10657 run_test 230b "nested remote directory should be failed"
10658
10659 test_231a()
10660 {
10661         # For simplicity this test assumes that max_pages_per_rpc
10662         # is the same across all OSCs
10663         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -1)
10664         local bulk_size=$((max_pages * 4096))
10665
10666         mkdir -p $DIR/$tdir
10667
10668         # clear the OSC stats
10669         $LCTL set_param osc.*.stats=0 &>/dev/null
10670
10671         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
10672         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
10673                 oflag=direct &>/dev/null || error "dd failed"
10674
10675         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
10676         if [ x$nrpcs != "x1" ]; then
10677                 error "found $nrpc ost_write RPCs, not 1 as expected"
10678         fi
10679
10680         # Drop the OSC cache, otherwise we will read from it
10681         cancel_lru_locks osc
10682
10683         # clear the OSC stats
10684         $LCTL set_param osc.*.stats=0 &>/dev/null
10685
10686         # Client reads $bulk_size.
10687         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
10688                 iflag=direct &>/dev/null || error "dd failed"
10689
10690         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
10691         if [ x$nrpcs != "x1" ]; then
10692                 error "found $nrpc ost_read RPCs, not 1 as expected"
10693         fi
10694 }
10695 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
10696
10697 test_231b() {
10698         mkdir -p $DIR/$tdir
10699         local i
10700         for i in {0..1023}; do
10701                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
10702                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
10703                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
10704         done
10705         sync
10706 }
10707 run_test 231b "must not assert on fully utilized OST request buffer"
10708
10709 #
10710 # tests that do cleanup/setup should be run at the end
10711 #
10712
10713 test_900() {
10714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10715         local ls
10716         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
10717         $LCTL set_param fail_loc=0x903
10718         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
10719         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
10720                 echo "clear" > $ls
10721         done
10722         FAIL_ON_ERROR=true cleanup
10723         FAIL_ON_ERROR=true setup
10724 }
10725 run_test 900 "umount should not race with any mgc requeue thread"
10726
10727 complete $SECONDS
10728 check_and_cleanup_lustre
10729 if [ "$I_MOUNTED" != "yes" ]; then
10730         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
10731 fi
10732 exit_status