Whamcloud - gitweb
LU-2395 ofd: Maintain per-export {read,write}_bytes counters
[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 # Tests that fail on uml
21 CPU=`awk '/model/ {print $4}' /proc/cpuinfo`
22 #                                    buffer i/o errs             sock spc runas
23 [ "$CPU" = "UML" ] && EXCEPT="$EXCEPT 27m 27n 27o 27p 27q 27r 31d 54a  64b 99a 99b 99c 99d 99e 99f 101a"
24
25 SRCDIR=$(cd $(dirname $0); echo $PWD)
26 export PATH=$PATH:/sbin
27
28 TMP=${TMP:-/tmp}
29
30 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
31 CREATETEST=${CREATETEST:-createtest}
32 LFS=${LFS:-lfs}
33 LFIND=${LFIND:-"$LFS find"}
34 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
35 LCTL=${LCTL:-lctl}
36 MCREATE=${MCREATE:-mcreate}
37 OPENFILE=${OPENFILE:-openfile}
38 OPENUNLINK=${OPENUNLINK:-openunlink}
39 export MULTIOP=${MULTIOP:-multiop}
40 READS=${READS:-"reads"}
41 MUNLINK=${MUNLINK:-munlink}
42 SOCKETSERVER=${SOCKETSERVER:-socketserver}
43 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
44 MEMHOG=${MEMHOG:-memhog}
45 DIRECTIO=${DIRECTIO:-directio}
46 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
47 UMOUNT=${UMOUNT:-"umount -d"}
48 STRIPES_PER_OBJ=-1
49 CHECK_GRANT=${CHECK_GRANT:-"yes"}
50 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
51
52 export NAME=${NAME:-local}
53
54 SAVE_PWD=$PWD
55
56 CLEANUP=${CLEANUP:-:}
57 SETUP=${SETUP:-:}
58 TRACE=${TRACE:-""}
59 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
60 . $LUSTRE/tests/test-framework.sh
61 init_test_env $@
62 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
63 init_logging
64
65 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 60c 63 64b 68 71 73 77f 78 101a 103 115 120g 124b"
66
67 FAIL_ON_ERROR=false
68
69 cleanup() {
70         echo -n "cln.."
71         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
72         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
73 }
74 setup() {
75         echo -n "mnt.."
76         load_modules
77         setupall || exit 10
78         echo "done"
79 }
80
81 check_kernel_version() {
82         WANT_VER=$1
83         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
84         case $GOT_VER in
85         patchless|patchless_client) return 0;;
86         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
87         esac
88         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
89         return 1
90 }
91
92 if [ "$ONLY" == "cleanup" ]; then
93        sh llmountcleanup.sh
94        exit 0
95 fi
96
97 check_and_setup_lustre
98
99 DIR=${DIR:-$MOUNT}
100 assert_DIR
101
102 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
103     awk '{gsub(/_UUID/,""); print $1}' | head -1)
104 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
105 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
106 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
107 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
108 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
109 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
110
111 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
112 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
113 rm -rf $DIR/[Rdfs][0-9]*
114
115 # $RUNAS_ID may get set incorrectly somewhere else
116 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
117
118 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
119
120 build_test_filter
121
122 if [ "${ONLY}" = "MOUNT" ] ; then
123         echo "Lustre is up, please go on"
124         exit
125 fi
126
127 echo "preparing for tests involving mounts"
128 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
129 touch $EXT2_DEV
130 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
131 echo # add a newline after mke2fs.
132
133 umask 077
134
135 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
136 lctl set_param debug=-1 2> /dev/null || true
137 test_0() {
138         touch $DIR/$tfile
139         $CHECKSTAT -t file $DIR/$tfile || error
140         rm $DIR/$tfile
141         $CHECKSTAT -a $DIR/$tfile || error
142 }
143 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
144
145 test_0b() {
146         chmod 0755 $DIR || error
147         $CHECKSTAT -p 0755 $DIR || error
148 }
149 run_test 0b "chmod 0755 $DIR ============================="
150
151 test_0c() {
152     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
153     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
154 }
155 run_test 0c "check import proc ============================="
156
157 test_1a() {
158         test_mkdir -p $DIR/$tdir
159         test_mkdir -p $DIR/$tdir/d2
160         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
161         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
162 }
163 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
164
165 test_1b() {
166         rmdir $DIR/$tdir/d2
167         rmdir $DIR/$tdir
168         $CHECKSTAT -a $DIR/$tdir || error
169 }
170 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
171
172 test_2a() {
173         test_mkdir $DIR/$tdir
174         touch $DIR/$tdir/$tfile
175         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
176 }
177 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
178
179 test_2b() {
180         rm -r $DIR/$tdir
181         $CHECKSTAT -a $DIR/$tdir || error
182 }
183 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
184
185 test_3a() {
186         test_mkdir -p $DIR/$tdir
187         $CHECKSTAT -t dir $DIR/$tdir || error
188 }
189 run_test 3a "mkdir .../d3 ======================================"
190
191 test_3b() {
192         if [ ! -d $DIR/$tdir ]; then
193                 mkdir $DIR/$tdir
194         fi
195         touch $DIR/$tdir/$tfile
196         $CHECKSTAT -t file $DIR/$tdir/$tfile || error
197 }
198 run_test 3b "touch .../d3/f ===================================="
199
200 test_3c() {
201         rm -r $DIR/$tdir
202         $CHECKSTAT -a $DIR/$tdir || error
203 }
204 run_test 3c "rm -r .../d3 ======================================"
205
206 test_4a() {
207         test_mkdir -p $DIR/$tdir
208         $CHECKSTAT -t dir $DIR/$tdir || error
209 }
210 run_test 4a "mkdir .../d4 ======================================"
211
212 test_4b() {
213         if [ ! -d $DIR/$tdir ]; then
214                 test_mkdir $DIR/$tdir
215         fi
216         test_mkdir $DIR/$tdir/d2
217         mkdir $DIR/$tdir/d2
218         $CHECKSTAT -t dir $DIR/$tdir/d2 || error
219 }
220 run_test 4b "mkdir .../d4/d2 ==================================="
221
222 test_5() {
223         test_mkdir $DIR/$tdir
224         test_mkdir $DIR/$tdir/d2
225         chmod 0707 $DIR/$tdir/d2
226         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error
227 }
228 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
229
230 test_6a() {
231         touch $DIR/$tfile
232         chmod 0666 $DIR/$tfile || error
233         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
234 }
235 run_test 6a "touch .../f6a; chmod .../f6a ======================"
236
237 test_6b() {
238         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
239         if [ ! -f $DIR/$tfile ]; then
240                 touch $DIR/$tfile
241                 chmod 0666 $DIR/$tfile
242         fi
243         $RUNAS chmod 0444 $DIR/$tfile && error
244         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile || error
245 }
246 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
247
248 test_6c() {
249         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
250         touch $DIR/$tfile
251         chown $RUNAS_ID $DIR/$tfile || error
252         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
253 }
254 run_test 6c "touch .../f6c; chown .../f6c ======================"
255
256 test_6d() {
257         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
258         if [ ! -f $DIR/$tfile ]; then
259                 touch $DIR/$tfile
260                 chown $RUNAS_ID $DIR/$tfile
261         fi
262         $RUNAS chown $UID $DIR/$tfile && error
263         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile || error
264 }
265 run_test 6d "$RUNAS chown .../f6c (should return error) =="
266
267 test_6e() {
268         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
269         touch $DIR/$tfile
270         chgrp $RUNAS_ID $DIR/$tfile || error
271         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
272 }
273 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
274
275 test_6f() {
276         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
277         if [ ! -f $DIR/$tfile ]; then
278                 touch $DIR/$tfile
279                 chgrp $RUNAS_ID $DIR/$tfile
280         fi
281         $RUNAS chgrp $UID $DIR/$tfile && error
282         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile || error
283 }
284 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
285
286 test_6g() {
287         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
288         test_mkdir $DIR/$tdir || error
289         chmod 777 $DIR/$tdir || error
290         $RUNAS mkdir $DIR/$tdir/d || error
291         chmod g+s $DIR/$tdir/d || error
292         test_mkdir $DIR/$tdir/d/subdir
293         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir || error
294 }
295 run_test 6g "Is new dir in sgid dir inheriting group?"
296
297 test_6h() { # bug 7331
298         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
299         touch $DIR/$tfile || error "touch failed"
300         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
301         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
302                 error "chown worked"
303         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile || error
304 }
305 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
306
307 test_7a() {
308         test_mkdir $DIR/$tdir
309         $MCREATE $DIR/$tdir/$tfile
310         chmod 0666 $DIR/$tdir/$tfile
311         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
312 }
313 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
314
315 test_7b() {
316         if [ ! -d $DIR/$tdir ]; then
317                 mkdir $DIR/$tdir
318         fi
319         $MCREATE $DIR/$tdir/$tfile
320         echo -n foo > $DIR/$tdir/$tfile
321         [ "`cat $DIR/$tdir/$tfile`" = "foo" ] || error
322         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error
323 }
324 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
325
326 test_8() {
327         test_mkdir $DIR/$tdir
328         touch $DIR/$tdir/$tfile
329         chmod 0666 $DIR/$tdir/$tfile
330         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile || error
331 }
332 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
333
334 test_9() {
335         test_mkdir $DIR/$tdir
336         test_mkdir $DIR/$tdir/d2
337         test_mkdir $DIR/$tdir/d2/d3
338         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error
339 }
340 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
341
342 test_10() {
343         test_mkdir $DIR/$tdir
344         test_mkdir $DIR/$tdir/d2
345         touch $DIR/$tdir/d2/$tfile
346         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile || error
347 }
348 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
349
350 test_11() {
351         test_mkdir $DIR/$tdir
352         test_mkdir $DIR/$tdir/d2
353         chmod 0666 $DIR/$tdir/d2
354         chmod 0705 $DIR/$tdir/d2
355         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 || error
356 }
357 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
358
359 test_12() {
360         test_mkdir $DIR/$tdir
361         touch $DIR/$tdir/$tfile
362         chmod 0666 $DIR/$tdir/$tfile
363         chmod 0654 $DIR/$tdir/$tfile
364         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile || error
365 }
366 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
367
368 test_13() {
369         test_mkdir $DIR/$tdir
370         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
371         >  $DIR/$tdir/$tfile
372         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile || error
373 }
374 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
375
376 test_14() {
377         test_mkdir $DIR/$tdir
378         touch $DIR/$tdir/$tfile
379         rm $DIR/$tdir/$tfile
380         $CHECKSTAT -a $DIR/$tdir/$tfile || error
381 }
382 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
383
384 test_15() {
385         test_mkdir $DIR/$tdir
386         touch $DIR/$tdir/$tfile
387         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
388         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 || error
389 }
390 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
391
392 test_16() {
393         test_mkdir $DIR/$tdir
394         touch $DIR/$tdir/$tfile
395         rm -rf $DIR/$tdir/$tfile
396         $CHECKSTAT -a $DIR/$tdir/$tfile || error
397 }
398 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
399
400 test_17a() {
401         test_mkdir -p $DIR/$tdir
402         touch $DIR/$tdir/$tfile
403         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
404         ls -l $DIR/$tdir
405         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist || error
406         $CHECKSTAT -f -t f $DIR/$tdir/l-exist || error
407         rm -f $DIR/$tdir/l-exist
408         $CHECKSTAT -a $DIR/$tdir/l-exist || error
409 }
410 run_test 17a "symlinks: create, remove (real) =================="
411
412 test_17b() {
413         test_mkdir -p $DIR/$tdir
414         ln -s no-such-file $DIR/$tdir/l-dangle
415         ls -l $DIR/$tdir
416         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle || error
417         $CHECKSTAT -fa $DIR/$tdir/l-dangle || error
418         rm -f $DIR/$tdir/l-dangle
419         $CHECKSTAT -a $DIR/$tdir/l-dangle || error
420 }
421 run_test 17b "symlinks: create, remove (dangling) =============="
422
423 test_17c() { # bug 3440 - don't save failed open RPC for replay
424         test_mkdir -p $DIR/$tdir
425         ln -s foo $DIR/$tdir/$tfile
426         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
427 }
428 run_test 17c "symlinks: open dangling (should return error) ===="
429
430 test_17d() {
431         test_mkdir -p $DIR/$tdir
432         ln -s foo $DIR/$tdir/$tfile
433         touch $DIR/$tdir/$tfile || error "creating to new symlink"
434 }
435 run_test 17d "symlinks: create dangling ========================"
436
437 test_17e() {
438         test_mkdir -p $DIR/$tdir
439         local foo=$DIR/$tdir/$tfile
440         ln -s $foo $foo || error "create symlink failed"
441         ls -l $foo || error "ls -l failed"
442         ls $foo && error "ls not failed" || true
443 }
444 run_test 17e "symlinks: create recursive symlink (should return error) ===="
445
446 test_17f() {
447         test_mkdir -p $DIR/d17f
448         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
449         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
453         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
454         ls -l  $DIR/d17f
455 }
456 run_test 17f "symlinks: long and very long symlink name ========================"
457
458 # str_repeat(S, N) generate a string that is string S repeated N times
459 str_repeat() {
460         local s=$1
461         local n=$2
462         local ret=''
463         while [ $((n -= 1)) -ge 0 ]; do
464                 ret=$ret$s
465         done
466         echo $ret
467 }
468
469 # Long symlinks and LU-2241
470 test_17g() {
471         test_mkdir -p $DIR/$tdir
472         local TESTS="59 60 61 4094 4095"
473
474         for i in $TESTS; do
475                 local SYMNAME=$(str_repeat 'x' $i)
476                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
477                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
478         done
479 }
480 run_test 17g "symlinks: really long symlink name and inode boundaries"
481
482 test_17h() { #bug 17378
483         remote_mds_nodsh && skip "remote MDS with nodsh" && return
484         local mdt_idx
485         test_mkdir -p $DIR/$tdir
486         if [ $MDSCOUNT -gt 1 ]; then
487                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
488         else
489                 mdt_idx=0
490         fi
491         $SETSTRIPE -c -1 $DIR/$tdir
492 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
493         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
494         touch $DIR/$tdir/$tfile || true
495 }
496 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
497
498 test_17i() { #bug 20018
499         remote_mds_nodsh && skip "remote MDS with nodsh" && return
500         test_mkdir -p $DIR/$tdir
501         local foo=$DIR/$tdir/$tfile
502         local mdt_idx
503         if [ $MDSCOUNT -gt 1 ]; then
504                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
505         else
506                 mdt_idx=0
507         fi
508         ln -s $foo $foo || error "create symlink failed"
509 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
510         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
511         ls -l $foo && error "error not detected"
512         return 0
513 }
514 run_test 17i "don't panic on short symlink"
515
516 test_17k() { #bug 22301
517         rsync --help | grep -q xattr ||
518                 skip_env "$(rsync --version| head -1) does not support xattrs"
519         test_mkdir -p $DIR/$tdir
520         test_mkdir -p $DIR/$tdir.new
521         touch $DIR/$tdir/$tfile
522         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
523         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
524                 error "rsync failed with xattrs enabled"
525 }
526 run_test 17k "symlinks: rsync with xattrs enabled ========================="
527
528 test_17l() { # LU-279
529         mkdir -p $DIR/$tdir
530         touch $DIR/$tdir/$tfile
531         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
532         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
533                 # -h to not follow symlinks. -m '' to list all the xattrs.
534                 # grep to remove first line: '# file: $path'.
535                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
536                 do
537                         lgetxattr_size_check $path $xattr ||
538                                 error "lgetxattr_size_check $path $xattr failed"
539                 done
540         done
541 }
542 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
543
544 # LU-1540
545 test_17m() {
546         local short_sym="0123456789"
547         local WDIR=$DIR/${tdir}m
548         local mds_index
549         local devname
550         local cmd
551         local i
552         local rc=0
553
554         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
555         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
556                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
557
558         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
559                 skip "only for ldiskfs MDT" && return 0
560
561         mkdir -p $WDIR
562         long_sym=$short_sym
563         # create a long symlink file
564         for ((i = 0; i < 4; ++i)); do
565                 long_sym=${long_sym}${long_sym}
566         done
567
568         echo "create 512 short and long symlink files under $WDIR"
569         for ((i = 0; i < 256; ++i)); do
570                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
571                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
572         done
573
574         echo "erase them"
575         rm -f $WDIR/*
576         sync
577         wait_delete_completed
578
579         echo "recreate the 512 symlink files with a shorter string"
580         for ((i = 0; i < 512; ++i)); do
581                 # rewrite the symlink file with a shorter string
582                 ln -sf ${long_sym} $WDIR/long-$i
583                 ln -sf ${short_sym} $WDIR/short-$i
584         done
585
586         mds_index=$($LFS getstripe -M $WDIR)
587         mds_index=$((mds_index+1))
588         devname=$(mdsdevname $mds_index)
589         cmd="$E2FSCK -fnvd $devname"
590
591         echo "stop and checking mds${mds_index}: $cmd"
592         # e2fsck should not return error
593         stop mds${mds_index} -f
594         do_facet mds${mds_index} $cmd || rc=$?
595
596         start mds${mds_index} $devname $MDS_MOUNT_OPTS
597         df $MOUNT > /dev/null 2>&1
598         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
599                 "short/long symlink MDT: rc=$rc"
600         return $rc
601 }
602 run_test 17m "run e2fsck against MDT which contains short/long symlink"
603
604 check_fs_consistency_17n() {
605         local mdt_index
606         local devname
607         local cmd
608         local rc=0
609
610         for mdt_index in $(seq 1 $MDSCOUNT); do
611                 devname=$(mdsdevname $mdt_index)
612                 cmd="$E2FSCK -fnvd $devname"
613
614                 echo "stop and checking mds${mdt_index}: $cmd"
615                 # e2fsck should not return error
616                 stop mds${mdt_index}
617                 do_facet mds${mdt_index} $cmd || rc=$?
618
619                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
620                 df $MOUNT > /dev/null 2>&1
621                 [ $rc -ne 0 ] && break
622         done
623         return $rc
624 }
625
626 test_17n() {
627         local i
628
629         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
630         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
631                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
632
633         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
634                 skip "only for ldiskfs MDT" && return 0
635
636         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
637
638         mkdir -p $DIR/$tdir
639         for ((i=0; i<10; i++)); do
640                 $LFS mkdir -i 1 $DIR/$tdir/remote_dir_${i} ||
641                         error "create remote dir error $i"
642                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
643                         error "create files under remote dir failed $i"
644         done
645
646         check_fs_consistency_17n || error "e2fsck report error"
647
648         for ((i=0;i<10;i++)); do
649                 rm -rf $DIR/$tdir/remote_dir_${i} ||
650                         error "destroy remote dir error $i"
651         done
652
653         check_fs_consistency_17n || error "e2fsck report error"
654 }
655 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
656
657 test_18() {
658         touch $DIR/f
659         ls $DIR || error
660 }
661 run_test 18 "touch .../f ; ls ... =============================="
662
663 test_19a() {
664         touch $DIR/f19
665         ls -l $DIR
666         rm $DIR/f19
667         $CHECKSTAT -a $DIR/f19 || error
668 }
669 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
670
671 test_19b() {
672         ls -l $DIR/f19 && error || true
673 }
674 run_test 19b "ls -l .../f19 (should return error) =============="
675
676 test_19c() {
677         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
678         $RUNAS touch $DIR/f19 && error || true
679 }
680 run_test 19c "$RUNAS touch .../f19 (should return error) =="
681
682 test_19d() {
683         cat $DIR/f19 && error || true
684 }
685 run_test 19d "cat .../f19 (should return error) =============="
686
687 test_20() {
688         touch $DIR/f
689         rm $DIR/f
690         log "1 done"
691         touch $DIR/f
692         rm $DIR/f
693         log "2 done"
694         touch $DIR/f
695         rm $DIR/f
696         log "3 done"
697         $CHECKSTAT -a $DIR/f || error
698 }
699 run_test 20 "touch .../f ; ls -l ... ==========================="
700
701 test_21() {
702         test_mkdir $DIR/d21
703         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
704         ln -s dangle $DIR/d21/link
705         echo foo >> $DIR/d21/link
706         cat $DIR/d21/dangle
707         $CHECKSTAT -t link $DIR/d21/link || error
708         $CHECKSTAT -f -t file $DIR/d21/link || error
709 }
710 run_test 21 "write to dangling link ============================"
711
712 test_22() {
713         WDIR=$DIR/$tdir
714         test_mkdir -p $DIR/$tdir
715         chown $RUNAS_ID:$RUNAS_GID $WDIR
716         (cd $WDIR || error "cd $WDIR failed";
717         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
718         $RUNAS tar xf -)
719         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
720         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
721         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
722 }
723 run_test 22 "unpack tar archive as non-root user ==============="
724
725 # was test_23
726 test_23a() {
727         test_mkdir -p $DIR/$tdir
728         local file=$DIR/$tdir/$tfile
729
730         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
731         openfile -f O_CREAT:O_EXCL $file &&
732                 error "$file recreate succeeded" || true
733 }
734 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
735
736 test_23b() { # bug 18988
737         test_mkdir -p $DIR/$tdir
738         local file=$DIR/$tdir/$tfile
739
740         rm -f $file
741         echo foo > $file || error "write filed"
742         echo bar >> $file || error "append filed"
743         $CHECKSTAT -s 8 $file || error "wrong size"
744         rm $file
745 }
746 run_test 23b "O_APPEND check =========================="
747
748 test_24a() {
749         echo '== rename sanity =============================================='
750         echo '-- same directory rename'
751         test_mkdir $DIR/R1
752         touch $DIR/R1/f
753         mv $DIR/R1/f $DIR/R1/g
754         $CHECKSTAT -t file $DIR/R1/g || error
755 }
756 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
757
758 test_24b() {
759         test_mkdir $DIR/R2
760         touch $DIR/R2/{f,g}
761         mv $DIR/R2/f $DIR/R2/g
762         $CHECKSTAT -a $DIR/R2/f || error
763         $CHECKSTAT -t file $DIR/R2/g || error
764 }
765 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
766
767 test_24c() {
768         test_mkdir $DIR/R3
769         test_mkdir $DIR/R3/f
770         mv $DIR/R3/f $DIR/R3/g
771         $CHECKSTAT -a $DIR/R3/f || error
772         $CHECKSTAT -t dir $DIR/R3/g || error
773 }
774 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
775
776 test_24d() {
777         test_mkdir $DIR/R4
778         test_mkdir $DIR/R4/f
779         test_mkdir $DIR/R4/g
780         mrename $DIR/R4/f $DIR/R4/g
781         $CHECKSTAT -a $DIR/R4/f || error
782         $CHECKSTAT -t dir $DIR/R4/g || error
783 }
784 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
785
786 test_24e() {
787         echo '-- cross directory renames --'
788         test_mkdir $DIR/R5a
789         test_mkdir $DIR/R5b
790         touch $DIR/R5a/f
791         mv $DIR/R5a/f $DIR/R5b/g
792         $CHECKSTAT -a $DIR/R5a/f || error
793         $CHECKSTAT -t file $DIR/R5b/g || error
794 }
795 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
796
797 test_24f() {
798         test_mkdir $DIR/R6a
799         test_mkdir $DIR/R6b
800         touch $DIR/R6a/f $DIR/R6b/g
801         mv $DIR/R6a/f $DIR/R6b/g
802         $CHECKSTAT -a $DIR/R6a/f || error
803         $CHECKSTAT -t file $DIR/R6b/g || error
804 }
805 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
806
807 test_24g() {
808         test_mkdir $DIR/R7a
809         test_mkdir $DIR/R7b
810         test_mkdir $DIR/R7a/d
811         mv $DIR/R7a/d $DIR/R7b/e
812         $CHECKSTAT -a $DIR/R7a/d || error
813         $CHECKSTAT -t dir $DIR/R7b/e || error
814 }
815 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
816
817 test_24h() {
818         test_mkdir $DIR/R8a
819         test_mkdir $DIR/R8b
820         test_mkdir $DIR/R8a/d
821         test_mkdir $DIR/R8b/e
822         mrename $DIR/R8a/d $DIR/R8b/e
823         $CHECKSTAT -a $DIR/R8a/d || error
824         $CHECKSTAT -t dir $DIR/R8b/e || error
825 }
826 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
827
828 test_24i() {
829         echo "-- rename error cases"
830         test_mkdir $DIR/R9
831         test_mkdir $DIR/R9/a
832         touch $DIR/R9/f
833         mrename $DIR/R9/f $DIR/R9/a
834         $CHECKSTAT -t file $DIR/R9/f || error
835         $CHECKSTAT -t dir  $DIR/R9/a || error
836         $CHECKSTAT -a $DIR/R9/a/f || error
837 }
838 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
839
840 test_24j() {
841         test_mkdir $DIR/R10
842         mrename $DIR/R10/f $DIR/R10/g
843         $CHECKSTAT -t dir $DIR/R10 || error
844         $CHECKSTAT -a $DIR/R10/f || error
845         $CHECKSTAT -a $DIR/R10/g || error
846 }
847 run_test 24j "source does not exist ============================"
848
849 test_24k() {
850         test_mkdir $DIR/R11a
851         test_mkdir $DIR/R11a/d
852         touch $DIR/R11a/f
853         mv $DIR/R11a/f $DIR/R11a/d
854         $CHECKSTAT -a $DIR/R11a/f || error
855         $CHECKSTAT -t file $DIR/R11a/d/f || error
856 }
857 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
858
859 # bug 2429 - rename foo foo foo creates invalid file
860 test_24l() {
861         f="$DIR/f24l"
862         $MULTIOP $f OcNs || error
863 }
864 run_test 24l "Renaming a file to itself ========================"
865
866 test_24m() {
867         f="$DIR/f24m"
868         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
869         # on ext3 this does not remove either the source or target files
870         # though the "expected" operation would be to remove the source
871         $CHECKSTAT -t file ${f} || error "${f} missing"
872         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
873 }
874 run_test 24m "Renaming a file to a hard link to itself ========="
875
876 test_24n() {
877     f="$DIR/f24n"
878     # this stats the old file after it was renamed, so it should fail
879     touch ${f}
880     $CHECKSTAT ${f}
881     mv ${f} ${f}.rename
882     $CHECKSTAT ${f}.rename
883     $CHECKSTAT -a ${f}
884 }
885 run_test 24n "Statting the old file after renaming (Posix rename 2)"
886
887 test_24o() {
888         check_kernel_version 37 || return 0
889         test_mkdir -p $DIR/d24o
890         rename_many -s random -v -n 10 $DIR/d24o
891 }
892 run_test 24o "rename of files during htree split ==============="
893
894 test_24p() {
895         test_mkdir $DIR/R12a
896         test_mkdir $DIR/R12b
897         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
898         mrename $DIR/R12a $DIR/R12b
899         $CHECKSTAT -a $DIR/R12a || error
900         $CHECKSTAT -t dir $DIR/R12b || error
901         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
902         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
903 }
904 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
905
906 test_24q() {
907         test_mkdir $DIR/R13a
908         test_mkdir $DIR/R13b
909         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
910         multiop_bg_pause $DIR/R13b D_c || return 1
911         MULTIPID=$!
912
913         mrename $DIR/R13a $DIR/R13b
914         $CHECKSTAT -a $DIR/R13a || error
915         $CHECKSTAT -t dir $DIR/R13b || error
916         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
917         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
918         kill -USR1 $MULTIPID
919         wait $MULTIPID || error "multiop close failed"
920 }
921 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
922
923 test_24r() { #bug 3789
924         test_mkdir $DIR/R14a
925         test_mkdir $DIR/R14a/b
926         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
927         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
928         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
929 }
930 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
931
932 test_24s() {
933         test_mkdir $DIR/R15a
934         test_mkdir $DIR/R15a/b
935         test_mkdir $DIR/R15a/b/c
936         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
937         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
938         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
939 }
940 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
941 test_24t() {
942         test_mkdir $DIR/R16a
943         test_mkdir $DIR/R16a/b
944         test_mkdir $DIR/R16a/b/c
945         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
946         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
947         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
948 }
949 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
950
951 test_24u() { # bug12192
952         rm -rf $DIR/$tfile
953         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
954         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
955 }
956 run_test 24u "create stripe file"
957
958 page_size() {
959         getconf PAGE_SIZE
960 }
961
962 simple_cleanup_common() {
963         trap 0
964         rm -rf $DIR/$tdir
965         wait_delete_completed
966 }
967
968 max_pages_per_rpc() {
969         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -1
970 }
971
972 test_24v() {
973         local NRFILES=100000
974         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
975         [ $FREE_INODES -lt $NRFILES ] && \
976                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
977                 return
978
979         trap simple_cleanup_common EXIT
980
981         mkdir -p $DIR/$tdir
982         createmany -m $DIR/$tdir/$tfile $NRFILES
983
984         cancel_lru_locks mdc
985         lctl set_param mdc.*.stats clear
986
987         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
988
989         # LU-5 large readdir
990         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
991         #               8 bytes for name(filename is mostly 5 in this test) +
992         #               8 bytes for luda_type
993         # take into account of overhead in lu_dirpage header and end mark in
994         # each page, plus one in RPC_NUM calculation.
995         DIRENT_SIZE=48
996         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
997         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
998         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats | \
999                                 awk '/^mds_readpage/ {print $2}')
1000         [ $mds_readpage -gt $RPC_NUM ] && \
1001                 error "large readdir doesn't take effect"
1002
1003         simple_cleanup_common
1004 }
1005 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1006
1007 test_24w() { # bug21506
1008         SZ1=234852
1009         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1010         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1011         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1012         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1013         [ "$SZ1" = "$SZ2" ] || \
1014                 error "Error reading at the end of the file $tfile"
1015 }
1016 run_test 24w "Reading a file larger than 4Gb"
1017
1018 test_24x() {
1019         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1020         local MDTIDX=1
1021         local remote_dir=$DIR/$tdir/remote_dir
1022
1023         mkdir -p $DIR/$tdir
1024         $LFS mkdir -i $MDTIDX $remote_dir ||
1025                 error "create remote directory failed"
1026
1027         mkdir -p $DIR/$tdir/src_dir
1028         touch $DIR/$tdir/src_file
1029         mkdir -p $remote_dir/tgt_dir
1030         touch $remote_dir/tgt_file
1031
1032         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1033                 error "rename dir cross MDT works!"
1034
1035         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1036                 error "rename file cross MDT works!"
1037
1038         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1039                 error "ln file cross MDT should not work!"
1040
1041         rm -rf $DIR/$tdir || error "Can not delete directories"
1042 }
1043 run_test 24x "cross rename/link should be failed"
1044
1045 test_24y() {
1046         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1047         local MDTIDX=1
1048         local remote_dir=$DIR/$tdir/remote_dir
1049
1050         mkdir -p $DIR/$tdir
1051         $LFS mkdir -i $MDTIDX $remote_dir ||
1052                    error "create remote directory failed"
1053
1054         mkdir -p $remote_dir/src_dir
1055         touch $remote_dir/src_file
1056         mkdir -p $remote_dir/tgt_dir
1057         touch $remote_dir/tgt_file
1058
1059         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1060                 error "rename subdir in the same remote dir failed!"
1061
1062         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1063                 error "rename files in the same remote dir failed!"
1064
1065         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1066                 error "link files in the same remote dir failed!"
1067
1068         rm -rf $DIR/$tdir || error "Can not delete directories"
1069 }
1070 run_test 24y "rename/link on the same dir should succeed"
1071
1072 test_24z() {
1073         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1074         local MDTIDX=1
1075         local remote_src=$DIR/$tdir/remote_dir
1076         local remote_tgt=$DIR/$tdir/remote_tgt
1077
1078         mkdir -p $DIR/$tdir
1079         $LFS mkdir -i $MDTIDX $remote_src ||
1080                    error "create remote directory failed"
1081
1082         $LFS mkdir -i $MDTIDX $remote_tgt ||
1083                    error "create remote directory failed"
1084
1085         mrename $remote_src $remote_tgt &&
1086                 error "rename remote dirs should not work!"
1087
1088         rm -rf $DIR/$tdir || error "Can not delete directories"
1089 }
1090 run_test 24z "rename one remote dir to another remote dir should fail"
1091
1092 test_25a() {
1093         echo '== symlink sanity ============================================='
1094
1095         test_mkdir $DIR/d25
1096         ln -s d25 $DIR/s25
1097         touch $DIR/s25/foo || error
1098 }
1099 run_test 25a "create file in symlinked directory ==============="
1100
1101 test_25b() {
1102         [ ! -d $DIR/d25 ] && test_25a
1103         $CHECKSTAT -t file $DIR/s25/foo || error
1104 }
1105 run_test 25b "lookup file in symlinked directory ==============="
1106
1107 test_26a() {
1108         test_mkdir $DIR/d26
1109         test_mkdir $DIR/d26/d26-2
1110         ln -s d26/d26-2 $DIR/s26
1111         touch $DIR/s26/foo || error
1112 }
1113 run_test 26a "multiple component symlink ======================="
1114
1115 test_26b() {
1116         test_mkdir -p $DIR/d26b/d26-2
1117         ln -s d26b/d26-2/foo $DIR/s26-2
1118         touch $DIR/s26-2 || error
1119 }
1120 run_test 26b "multiple component symlink at end of lookup ======"
1121
1122 test_26c() {
1123         test_mkdir $DIR/d26.2
1124         touch $DIR/d26.2/foo
1125         ln -s d26.2 $DIR/s26.2-1
1126         ln -s s26.2-1 $DIR/s26.2-2
1127         ln -s s26.2-2 $DIR/s26.2-3
1128         chmod 0666 $DIR/s26.2-3/foo
1129 }
1130 run_test 26c "chain of symlinks ================================"
1131
1132 # recursive symlinks (bug 439)
1133 test_26d() {
1134         ln -s d26-3/foo $DIR/d26-3
1135 }
1136 run_test 26d "create multiple component recursive symlink ======"
1137
1138 test_26e() {
1139         [ ! -h $DIR/d26-3 ] && test_26d
1140         rm $DIR/d26-3
1141 }
1142 run_test 26e "unlink multiple component recursive symlink ======"
1143
1144 # recursive symlinks (bug 7022)
1145 test_26f() {
1146         test_mkdir -p $DIR/$tdir
1147         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1148         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1149         test_mkdir -p lndir bar1      || error "mkdir lndir/bar1 failed"
1150         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1151         cd $tfile                || error "cd $tfile failed"
1152         ln -s .. dotdot          || error "ln dotdot failed"
1153         ln -s dotdot/lndir lndir || error "ln lndir failed"
1154         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1155         output=`ls $tfile/$tfile/lndir/bar1`
1156         [ "$output" = bar1 ] && error "unexpected output"
1157         rm -r $tfile             || error "rm $tfile failed"
1158         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1159 }
1160 run_test 26f "rm -r of a directory which has recursive symlink ="
1161
1162 test_27a() {
1163         echo '== stripe sanity =============================================='
1164         test_mkdir -p $DIR/d27 || error "mkdir failed"
1165         $GETSTRIPE $DIR/d27
1166         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1167         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1168         pass
1169         log "== test_27a: write to one stripe file ========================="
1170         cp /etc/hosts $DIR/d27/f0 || error
1171 }
1172 run_test 27a "one stripe file =================================="
1173
1174 test_27b() {
1175         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1176         test_mkdir -p $DIR/d27
1177         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1178         $GETSTRIPE -c $DIR/d27/f01
1179         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1180                 error "two-stripe file doesn't have two stripes"
1181 }
1182 run_test 27b "create two stripe file"
1183
1184 test_27c() {
1185         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1186
1187         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1188 }
1189 run_test 27c "write to two stripe file"
1190
1191 test_27d() {
1192         test_mkdir -p $DIR/d27
1193         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1194         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1195         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1196 }
1197 run_test 27d "create file with default settings ================"
1198
1199 test_27e() {
1200         test_mkdir -p $DIR/d27
1201         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1202         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1203         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1204 }
1205 run_test 27e "setstripe existing file (should return error) ======"
1206
1207 test_27f() {
1208         test_mkdir -p $DIR/d27
1209         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1210         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1211         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1212 }
1213 run_test 27f "setstripe with bad stripe size (should return error)"
1214
1215 test_27g() {
1216         test_mkdir -p $DIR/d27
1217         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1218         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1219                 error "$DIR/d27/fnone has object"
1220 }
1221 run_test 27g "$GETSTRIPE with no objects"
1222
1223 test_27i() {
1224         touch $DIR/d27/fsome || error "touch failed"
1225         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1226 }
1227 run_test 27i "$GETSTRIPE with some objects"
1228
1229 test_27j() {
1230         test_mkdir -p $DIR/d27
1231         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1232 }
1233 run_test 27j "setstripe with bad stripe offset (should return error)"
1234
1235 test_27k() { # bug 2844
1236         test_mkdir -p $DIR/d27
1237         FILE=$DIR/d27/f27k
1238         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1239         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1240         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1241         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1242         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1243         dd if=/dev/zero of=$FILE bs=4k count=1
1244         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1245         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1246 }
1247 run_test 27k "limit i_blksize for broken user apps ============="
1248
1249 test_27l() {
1250         test_mkdir -p $DIR/d27
1251         mcreate $DIR/f27l || error "creating file"
1252         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1253                 error "setstripe should have failed" || true
1254 }
1255 run_test 27l "check setstripe permissions (should return error)"
1256
1257 test_27m() {
1258         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1259                 return
1260         if [ $ORIGFREE -gt $MAXFREE ]; then
1261                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1262                 return
1263         fi
1264         trap simple_cleanup_common EXIT
1265         test_mkdir -p $DIR/$tdir
1266         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1267         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1268                 error "dd should fill OST0"
1269         i=2
1270         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1271                 i=`expr $i + 1`
1272                 [ $i -gt 256 ] && break
1273         done
1274         i=`expr $i + 1`
1275         touch $DIR/$tdir/f27m_$i
1276         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1277                 error "OST0 was full but new created file still use it"
1278         i=`expr $i + 1`
1279         touch $DIR/$tdir/f27m_$i
1280         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1281                 error "OST0 was full but new created file still use it"
1282         simple_cleanup_common
1283 }
1284 run_test 27m "create file while OST0 was full =================="
1285
1286 sleep_maxage() {
1287         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1288         sleep $DELAY
1289 }
1290
1291 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1292 # if the OST isn't full anymore.
1293 reset_enospc() {
1294         local OSTIDX=${1:-""}
1295
1296         local list=$(comma_list $(osts_nodes))
1297         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1298
1299         do_nodes $list lctl set_param fail_loc=0
1300         sync    # initiate all OST_DESTROYs from MDS to OST
1301         sleep_maxage
1302 }
1303
1304 exhaust_precreations() {
1305         local OSTIDX=$1
1306         local FAILLOC=$2
1307         local FAILIDX=${3:-$OSTIDX}
1308
1309         test_mkdir -p $DIR/$tdir
1310         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1311         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1312
1313         local OST=$(ostname_from_index $OSTIDX)
1314         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1315                           sed -e 's/_UUID$//;s/^.*-//')
1316
1317         # on the mdt's osc
1318         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1319         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1320         osc.$mdtosc_proc1.prealloc_last_id)
1321         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1322         osc.$mdtosc_proc1.prealloc_next_id)
1323
1324         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1325         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1326
1327         test_mkdir -p $DIR/$tdir/${OST}
1328         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1329 #define OBD_FAIL_OST_ENOSPC              0x215
1330         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1331         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1332         echo "Creating to objid $last_id on ost $OST..."
1333         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1334         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1335         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1336         sleep_maxage
1337 }
1338
1339 exhaust_all_precreations() {
1340         local i
1341         for (( i=0; i < OSTCOUNT; i++ )) ; do
1342                 exhaust_precreations $i $1 -1
1343         done
1344 }
1345
1346 test_27n() {
1347         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1348         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1349         remote_ost_nodsh && skip "remote OST with nodsh" && return
1350
1351         reset_enospc
1352         rm -f $DIR/$tdir/$tfile
1353         exhaust_precreations 0 0x80000215
1354         $SETSTRIPE -c -1 $DIR/$tdir
1355         touch $DIR/$tdir/$tfile || error
1356         $GETSTRIPE $DIR/$tdir/$tfile
1357         reset_enospc
1358 }
1359 run_test 27n "create file with some full OSTs =================="
1360
1361 test_27o() {
1362         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && 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_all_precreations 0x215
1369
1370         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1371
1372         reset_enospc
1373         rm -rf $DIR/$tdir/*
1374 }
1375 run_test 27o "create file with all full OSTs (should error) ===="
1376
1377 test_27p() {
1378         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && 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         test_mkdir -p $DIR/$tdir
1385
1386         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1387         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1388         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1389
1390         exhaust_precreations 0 0x80000215
1391         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1392         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1393         $GETSTRIPE $DIR/$tdir/$tfile
1394
1395         reset_enospc
1396 }
1397 run_test 27p "append to a truncated file with some full OSTs ==="
1398
1399 test_27q() {
1400         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1401         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1402         remote_ost_nodsh && skip "remote OST with nodsh" && return
1403
1404         reset_enospc
1405         rm -f $DIR/$tdir/$tfile
1406
1407         test_mkdir -p $DIR/$tdir
1408         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1409         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1410         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1411
1412         exhaust_all_precreations 0x215
1413
1414         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1415         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1416
1417         reset_enospc
1418 }
1419 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1420
1421 test_27r() {
1422         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1423         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1424         remote_ost_nodsh && skip "remote OST with nodsh" && return
1425
1426         reset_enospc
1427         rm -f $DIR/$tdir/$tfile
1428         exhaust_precreations 0 0x80000215
1429
1430         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1431
1432         reset_enospc
1433 }
1434 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1435
1436 test_27s() { # bug 10725
1437         test_mkdir -p $DIR/$tdir
1438         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1439         local stripe_count=0
1440         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1441         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1442                 error "stripe width >= 2^32 succeeded" || true
1443
1444 }
1445 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1446
1447 test_27t() { # bug 10864
1448         WDIR=`pwd`
1449         WLFS=`which lfs`
1450         cd $DIR
1451         touch $tfile
1452         $WLFS getstripe $tfile
1453         cd $WDIR
1454 }
1455 run_test 27t "check that utils parse path correctly"
1456
1457 test_27u() { # bug 4900
1458         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1459         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1460
1461 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1462         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1463         test_mkdir -p $DIR/$tdir
1464         createmany -o $DIR/$tdir/t- 1000
1465         do_facet $SINGLEMDS lctl set_param fail_loc=0
1466
1467         TLOG=$DIR/$tfile.getstripe
1468         $GETSTRIPE $DIR/$tdir > $TLOG
1469         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1470         unlinkmany $DIR/$tdir/t- 1000
1471         [ $OBJS -gt 0 ] && \
1472                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1473 }
1474 run_test 27u "skip object creation on OSC w/o objects =========="
1475
1476 test_27v() { # bug 4900
1477         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1478         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1479         remote_ost_nodsh && skip "remote OST with nodsh" && return
1480
1481         exhaust_all_precreations 0x215
1482         reset_enospc
1483
1484         test_mkdir -p $DIR/$tdir
1485         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1486
1487         touch $DIR/$tdir/$tfile
1488         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1489         # all except ost1
1490         for (( i=1; i < OSTCOUNT; i++ )); do
1491                 do_facet ost$i lctl set_param fail_loc=0x705
1492         done
1493         local START=`date +%s`
1494         createmany -o $DIR/$tdir/$tfile 32
1495
1496         local FINISH=`date +%s`
1497         local TIMEOUT=`lctl get_param -n timeout`
1498         local PROCESS=$((FINISH - START))
1499         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1500                error "$FINISH - $START >= $TIMEOUT / 2"
1501         sleep $((TIMEOUT / 2 - PROCESS))
1502         reset_enospc
1503 }
1504 run_test 27v "skip object creation on slow OST ================="
1505
1506 test_27w() { # bug 10997
1507         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1508         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1509         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1510                 error "stripe size $size != 65536" || true
1511         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1512                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1513 }
1514 run_test 27w "check $SETSTRIPE -S option"
1515
1516 test_27wa() {
1517         [ "$OSTCOUNT" -lt "2" ] &&
1518                 skip_env "skipping multiple stripe count/offset test" && return
1519
1520         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1521         for i in $(seq 1 $OSTCOUNT); do
1522                 offset=$((i - 1))
1523                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1524                         error "setstripe -c $i -i $offset failed"
1525                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1526                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1527                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1528                 [ $index -ne $offset ] &&
1529                         error "stripe offset $index != $offset" || true
1530         done
1531 }
1532 run_test 27wa "check $SETSTRIPE -c -i options"
1533
1534 test_27x() {
1535         remote_ost_nodsh && skip "remote OST with nodsh" && return
1536         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1537         OFFSET=$(($OSTCOUNT - 1))
1538         OSTIDX=0
1539         local OST=$(ostname_from_index $OSTIDX)
1540
1541         test_mkdir -p $DIR/$tdir
1542         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1543         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1544         sleep_maxage
1545         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1546         for i in `seq 0 $OFFSET`; do
1547                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1548                 error "OST0 was degraded but new created file still use it"
1549         done
1550         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1551 }
1552 run_test 27x "create files while OST0 is degraded"
1553
1554 test_27y() {
1555         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1556         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1557         remote_ost_nodsh && skip "remote OST with nodsh" && return
1558
1559         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1560         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1561             osc.$mdtosc.prealloc_last_id)
1562         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1563             osc.$mdtosc.prealloc_next_id)
1564         local fcount=$((last_id - next_id))
1565         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1566         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1567
1568         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1569                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1570         local OST_DEACTIVE_IDX=-1
1571         local OSC
1572         local OSTIDX
1573         local OST
1574
1575         for OSC in $MDS_OSCS; do
1576                 OST=$(osc_to_ost $OSC)
1577                 OSTIDX=$(index_from_ostuuid $OST)
1578                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1579                         OST_DEACTIVE_IDX=$OSTIDX
1580                 fi
1581                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1582                         echo $OSC "is Deactivated:"
1583                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1584                 fi
1585         done
1586
1587         OSTIDX=$(index_from_ostuuid $OST)
1588         mkdir -p $DIR/$tdir
1589         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1590
1591         for OSC in $MDS_OSCS; do
1592                 OST=$(osc_to_ost $OSC)
1593                 OSTIDX=$(index_from_ostuuid $OST)
1594                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1595                         echo $OST "is degraded:"
1596                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1597                                                 obdfilter.$OST.degraded=1
1598                 fi
1599         done
1600
1601         sleep_maxage
1602         createmany -o $DIR/$tdir/$tfile $fcount
1603
1604         for OSC in $MDS_OSCS; do
1605                 OST=$(osc_to_ost $OSC)
1606                 OSTIDX=$(index_from_ostuuid $OST)
1607                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1608                         echo $OST "is recovered from degraded:"
1609                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1610                                                 obdfilter.$OST.degraded=0
1611                 else
1612                         do_facet $SINGLEMDS lctl --device %$OSC activate
1613                 fi
1614         done
1615 }
1616 run_test 27y "create files while OST0 is degraded and the rest inactive"
1617
1618 check_seq_oid()
1619 {
1620         log "check file $1"
1621
1622         lmm_count=$($GETSTRIPE -c $1)
1623         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1624         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1625
1626         local old_ifs="$IFS"
1627         IFS=$'[:]'
1628         fid=($($LFS path2fid $1))
1629         IFS="$old_ifs"
1630
1631         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1632         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1633
1634         # compare lmm_seq and lu_fid->f_seq
1635         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1636         # compare lmm_object_id and lu_fid->oid
1637         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1638
1639         # check the trusted.fid attribute of the OST objects of the file
1640         local have_obdidx=false
1641         local stripe_nr=0
1642         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1643                 # skip lines up to and including "obdidx"
1644                 [ -z "$obdidx" ] && break
1645                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1646                 $have_obdidx || continue
1647
1648                 local ost=$((obdidx + 1))
1649                 local dev=$(ostdevname $ost)
1650
1651                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1652                         echo "Currently only works with ldiskfs-based OSTs"
1653                         continue
1654                 fi
1655
1656                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1657
1658                 #don't unmount/remount the OSTs if we don't need to do that
1659                 #local dir=$(facet_mntpt ost$ost)
1660                 #stop ost$dev
1661                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1662                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1663                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1664                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1665
1666                 local obj_file="O/$seq/d$((oid %32))/$oid"
1667                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1668                            $dev 2>/dev/null" | grep "parent=")
1669
1670                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1671
1672                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1673
1674                 #do_facet ost$ost umount -d $dir
1675                 #start ost$ost $dev $OST_MOUNT_OPTS
1676
1677                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1678                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1679                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1680                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1681                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1682                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1683
1684                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1685                 [ $ff_pseq = $lmm_seq ] ||
1686                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1687                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1688                 [ $ff_poid = $lmm_oid ] ||
1689                         error "FF parent OID $ff_poid != $lmm_oid"
1690                 [ $ff_pstripe = $stripe_nr ] ||
1691                         error "FF stripe $ff_pstripe != $stripe_nr"
1692
1693                 stripe_nr=$((stripe_nr + 1))
1694         done
1695 }
1696
1697 test_27z() {
1698         remote_ost_nodsh && skip "remote OST with nodsh" && return
1699         test_mkdir -p $DIR/$tdir
1700
1701         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1702                 { error "setstripe -c -1 failed"; return 1; }
1703         # We need to send a write to every object to get parent FID info set.
1704         # This _should_ also work for setattr, but does not currently.
1705         # touch $DIR/$tdir/$tfile-1 ||
1706         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1707                 { error "dd $tfile-1 failed"; return 2; }
1708         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1709                 { error "setstripe -c -1 failed"; return 3; }
1710         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1711                 { error "dd $tfile-2 failed"; return 4; }
1712
1713         # make sure write RPCs have been sent to OSTs
1714         sync; sleep 5; sync
1715
1716         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1717         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1718 }
1719 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1720
1721 test_27A() { # b=19102
1722         local restore_size=$($GETSTRIPE -S $MOUNT)
1723         local restore_count=$($GETSTRIPE -c $MOUNT)
1724         local restore_offset=$($GETSTRIPE -i $MOUNT)
1725         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1726         local default_size=$($GETSTRIPE -S $MOUNT)
1727         local default_count=$($GETSTRIPE -c $MOUNT)
1728         local default_offset=$($GETSTRIPE -i $MOUNT)
1729         local dsize=$((1024 * 1024))
1730         [ $default_size -eq $dsize ] ||
1731                 error "stripe size $default_size != $dsize"
1732         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1733         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1734         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1735 }
1736 run_test 27A "check filesystem-wide default LOV EA values"
1737
1738 # createtest also checks that device nodes are created and
1739 # then visible correctly (#2091)
1740 test_28() { # bug 2091
1741         test_mkdir $DIR/d28
1742         $CREATETEST $DIR/d28/ct || error
1743 }
1744 run_test 28 "create/mknod/mkdir with bad file types ============"
1745
1746 test_29() {
1747         cancel_lru_locks mdc
1748         test_mkdir $DIR/d29
1749         touch $DIR/d29/foo
1750         log 'first d29'
1751         ls -l $DIR/d29
1752
1753         declare -i LOCKCOUNTORIG=0
1754         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1755                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1756         done
1757         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1758
1759         declare -i LOCKUNUSEDCOUNTORIG=0
1760         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1761                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1762         done
1763
1764         log 'second d29'
1765         ls -l $DIR/d29
1766         log 'done'
1767
1768         declare -i LOCKCOUNTCURRENT=0
1769         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1770                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1771         done
1772
1773         declare -i LOCKUNUSEDCOUNTCURRENT=0
1774         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1775                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1776         done
1777
1778         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1779                 lctl set_param -n ldlm.dump_namespaces ""
1780                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1781                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1782                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1783                 return 2
1784         fi
1785         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1786                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1787                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1788                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1789                 return 3
1790         fi
1791 }
1792 run_test 29 "IT_GETATTR regression  ============================"
1793
1794 test_30a() { # was test_30
1795         cp `which ls` $DIR || cp /bin/ls $DIR
1796         $DIR/ls / || error
1797         rm $DIR/ls
1798 }
1799 run_test 30a "execute binary from Lustre (execve) =============="
1800
1801 test_30b() {
1802         cp `which ls` $DIR || cp /bin/ls $DIR
1803         chmod go+rx $DIR/ls
1804         $RUNAS $DIR/ls / || error
1805         rm $DIR/ls
1806 }
1807 run_test 30b "execute binary from Lustre as non-root ==========="
1808
1809 test_30c() { # b=22376
1810         cp `which ls` $DIR || cp /bin/ls $DIR
1811         chmod a-rw $DIR/ls
1812         cancel_lru_locks mdc
1813         cancel_lru_locks osc
1814         $RUNAS $DIR/ls / || error
1815         rm -f $DIR/ls
1816 }
1817 run_test 30c "execute binary from Lustre without read perms ===="
1818
1819 test_31a() {
1820         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1821         $CHECKSTAT -a $DIR/f31 || error
1822 }
1823 run_test 31a "open-unlink file =================================="
1824
1825 test_31b() {
1826         touch $DIR/f31 || error
1827         ln $DIR/f31 $DIR/f31b || error
1828         $MULTIOP $DIR/f31b Ouc || error
1829         $CHECKSTAT -t file $DIR/f31 || error
1830 }
1831 run_test 31b "unlink file with multiple links while open ======="
1832
1833 test_31c() {
1834         touch $DIR/f31 || error
1835         ln $DIR/f31 $DIR/f31c || error
1836         multiop_bg_pause $DIR/f31 O_uc || return 1
1837         MULTIPID=$!
1838         $MULTIOP $DIR/f31c Ouc
1839         kill -USR1 $MULTIPID
1840         wait $MULTIPID
1841 }
1842 run_test 31c "open-unlink file with multiple links ============="
1843
1844 test_31d() {
1845         opendirunlink $DIR/d31d $DIR/d31d || error
1846         $CHECKSTAT -a $DIR/d31d || error
1847 }
1848 run_test 31d "remove of open directory ========================="
1849
1850 test_31e() { # bug 2904
1851         check_kernel_version 34 || return 0
1852         openfilleddirunlink $DIR/d31e || error
1853 }
1854 run_test 31e "remove of open non-empty directory ==============="
1855
1856 test_31f() { # bug 4554
1857         set -vx
1858         test_mkdir $DIR/d31f
1859         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1860         cp /etc/hosts $DIR/d31f
1861         ls -l $DIR/d31f
1862         $GETSTRIPE $DIR/d31f/hosts
1863         multiop_bg_pause $DIR/d31f D_c || return 1
1864         MULTIPID=$!
1865
1866         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1867         test_mkdir $DIR/d31f
1868         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1869         cp /etc/hosts $DIR/d31f
1870         ls -l $DIR/d31f
1871         $GETSTRIPE $DIR/d31f/hosts
1872         multiop_bg_pause $DIR/d31f D_c || return 1
1873         MULTIPID2=$!
1874
1875         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1876         wait $MULTIPID || error "first opendir $MULTIPID failed"
1877
1878         sleep 6
1879
1880         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1881         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1882         set +vx
1883 }
1884 run_test 31f "remove of open directory with open-unlink file ==="
1885
1886 test_31g() {
1887         echo "-- cross directory link --"
1888         test_mkdir $DIR/d31ga
1889         test_mkdir $DIR/d31gb
1890         touch $DIR/d31ga/f
1891         ln $DIR/d31ga/f $DIR/d31gb/g
1892         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1893         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1894         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1895         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1896 }
1897 run_test 31g "cross directory link==============="
1898
1899 test_31h() {
1900         echo "-- cross directory link --"
1901         test_mkdir $DIR/d31h
1902         test_mkdir $DIR/d31h/dir
1903         touch $DIR/d31h/f
1904         ln $DIR/d31h/f $DIR/d31h/dir/g
1905         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1906         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1907         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1908         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1909 }
1910 run_test 31h "cross directory link under child==============="
1911
1912 test_31i() {
1913         echo "-- cross directory link --"
1914         test_mkdir $DIR/d31i
1915         test_mkdir $DIR/d31i/dir
1916         touch $DIR/d31i/dir/f
1917         ln $DIR/d31i/dir/f $DIR/d31i/g
1918         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1919         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1920         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1921         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1922 }
1923 run_test 31i "cross directory link under parent==============="
1924
1925
1926 test_31j() {
1927         test_mkdir $DIR/d31j
1928         test_mkdir $DIR/d31j/dir1
1929         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1930         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1931         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1932         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1933         return 0
1934 }
1935 run_test 31j "link for directory==============="
1936
1937
1938 test_31k() {
1939         test_mkdir $DIR/d31k
1940         touch $DIR/d31k/s
1941         touch $DIR/d31k/exist
1942         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1943         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1944         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1945         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1946         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1947         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1948         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1949         return 0
1950 }
1951 run_test 31k "link to file: the same, non-existing, dir==============="
1952
1953 test_31m() {
1954         test_mkdir $DIR/d31m
1955         touch $DIR/d31m/s
1956         test_mkdir $DIR/d31m2
1957         touch $DIR/d31m2/exist
1958         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1959         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1960         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1961         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1962         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1963         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1964         return 0
1965 }
1966 run_test 31m "link to file: the same, non-existing, dir==============="
1967
1968 cleanup_test32_mount() {
1969         trap 0
1970         $UMOUNT $DIR/$tdir/ext2-mountpoint
1971 }
1972
1973 test_32a() {
1974         echo "== more mountpoints and symlinks ================="
1975         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1976         trap cleanup_test32_mount EXIT
1977         test_mkdir -p $DIR/$tdir/ext2-mountpoint
1978         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1979         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
1980         cleanup_test32_mount
1981 }
1982 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
1983
1984 test_32b() {
1985         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1986         trap cleanup_test32_mount EXIT
1987         test_mkdir -p $DIR/$tdir/ext2-mountpoint
1988         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1989         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
1990         cleanup_test32_mount
1991 }
1992 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
1993
1994 test_32c() {
1995         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1996         trap cleanup_test32_mount EXIT
1997         test_mkdir -p $DIR/$tdir/ext2-mountpoint
1998         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1999         test_mkdir -p $DIR/$tdir/d2/test_dir
2000         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2001         cleanup_test32_mount
2002 }
2003 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2004
2005 test_32d() {
2006         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2007         trap cleanup_test32_mount EXIT
2008         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2009         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2010         test_mkdir -p $DIR/$tdir/d2/test_dir
2011         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2012         cleanup_test32_mount
2013 }
2014 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2015
2016 test_32e() {
2017         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2018         test_mkdir -p $DIR/d32e/tmp
2019         TMP_DIR=$DIR/d32e/tmp
2020         ln -s $DIR/d32e $TMP_DIR/symlink11
2021         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2022         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2023         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2024 }
2025 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2026
2027 test_32f() {
2028         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2029         test_mkdir -p $DIR/d32f/tmp
2030         TMP_DIR=$DIR/d32f/tmp
2031         ln -s $DIR/d32f $TMP_DIR/symlink11
2032         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2033         ls $DIR/d32f/tmp/symlink11  || error
2034         ls $DIR/d32f/symlink01 || error
2035 }
2036 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2037
2038 test_32g() {
2039         TMP_DIR=$DIR/$tdir/tmp
2040         test_mkdir -p $DIR/$tdir/tmp
2041         test_mkdir $DIR/${tdir}2
2042         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2043         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2044         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2045         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2046         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2047         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2048 }
2049 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2050
2051 test_32h() {
2052         rm -fr $DIR/$tdir $DIR/${tdir}2
2053         TMP_DIR=$DIR/$tdir/tmp
2054         test_mkdir -p $DIR/$tdir/tmp
2055         test_mkdir $DIR/${tdir}2
2056         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2057         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2058         ls $TMP_DIR/symlink12 || error
2059         ls $DIR/$tdir/symlink02  || error
2060 }
2061 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2062
2063 test_32i() {
2064         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2065         trap cleanup_test32_mount EXIT
2066         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2067         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2068         touch $DIR/$tdir/test_file
2069         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2070         cleanup_test32_mount
2071 }
2072 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2073
2074 test_32j() {
2075         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2076         trap cleanup_test32_mount EXIT
2077         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2078         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2079         touch $DIR/$tdir/test_file
2080         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2081         cleanup_test32_mount
2082 }
2083 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2084
2085 test_32k() {
2086         rm -fr $DIR/$tdir
2087         trap cleanup_test32_mount EXIT
2088         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2089         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2090         test_mkdir -p $DIR/$tdir/d2
2091         touch $DIR/$tdir/d2/test_file || error
2092         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2093         cleanup_test32_mount
2094 }
2095 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2096
2097 test_32l() {
2098         rm -fr $DIR/$tdir
2099         trap cleanup_test32_mount EXIT
2100         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2101         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2102         test_mkdir -p $DIR/$tdir/d2
2103         touch $DIR/$tdir/d2/test_file
2104         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2105         cleanup_test32_mount
2106 }
2107 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2108
2109 test_32m() {
2110         rm -fr $DIR/d32m
2111         test_mkdir -p $DIR/d32m/tmp
2112         TMP_DIR=$DIR/d32m/tmp
2113         ln -s $DIR $TMP_DIR/symlink11
2114         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2115         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2116         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2117 }
2118 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2119
2120 test_32n() {
2121         rm -fr $DIR/d32n
2122         test_mkdir -p $DIR/d32n/tmp
2123         TMP_DIR=$DIR/d32n/tmp
2124         ln -s $DIR $TMP_DIR/symlink11
2125         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2126         ls -l $DIR/d32n/tmp/symlink11  || error
2127         ls -l $DIR/d32n/symlink01 || error
2128 }
2129 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2130
2131 test_32o() {
2132         rm -fr $DIR/d32o $DIR/$tfile
2133         touch $DIR/$tfile
2134         test_mkdir -p $DIR/d32o/tmp
2135         TMP_DIR=$DIR/d32o/tmp
2136         ln -s $DIR/$tfile $TMP_DIR/symlink12
2137         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2138         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2139         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2140         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2141         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2142 }
2143 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2144
2145 test_32p() {
2146     log 32p_1
2147         rm -fr $DIR/d32p
2148     log 32p_2
2149         rm -f $DIR/$tfile
2150     log 32p_3
2151         touch $DIR/$tfile
2152     log 32p_4
2153         test_mkdir -p $DIR/d32p/tmp
2154     log 32p_5
2155         TMP_DIR=$DIR/d32p/tmp
2156     log 32p_6
2157         ln -s $DIR/$tfile $TMP_DIR/symlink12
2158     log 32p_7
2159         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2160     log 32p_8
2161         cat $DIR/d32p/tmp/symlink12 || error
2162     log 32p_9
2163         cat $DIR/d32p/symlink02 || error
2164     log 32p_10
2165 }
2166 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2167
2168 cleanup_testdir_mount() {
2169         trap 0
2170         $UMOUNT $DIR/$tdir
2171 }
2172
2173 test_32q() {
2174         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2175         trap cleanup_testdir_mount EXIT
2176         test_mkdir -p $DIR/$tdir
2177         touch $DIR/$tdir/under_the_mount
2178         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2179         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2180         cleanup_testdir_mount
2181 }
2182 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2183
2184 test_32r() {
2185         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2186         trap cleanup_testdir_mount EXIT
2187         test_mkdir -p $DIR/$tdir
2188         touch $DIR/$tdir/under_the_mount
2189         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2190         ls $DIR/$tdir | grep -q under_the_mount && error || true
2191         cleanup_testdir_mount
2192 }
2193 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2194
2195 test_33aa() {
2196         rm -f $DIR/$tfile
2197         touch $DIR/$tfile
2198         chmod 444 $DIR/$tfile
2199         chown $RUNAS_ID $DIR/$tfile
2200         log 33_1
2201         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2202         log 33_2
2203 }
2204 run_test 33aa "write file with mode 444 (should return error) ===="
2205
2206 test_33a() {
2207         rm -fr $DIR/d33
2208         test_mkdir -p $DIR/d33
2209         chown $RUNAS_ID $DIR/d33
2210         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2211         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2212                 error "open RDWR" || true
2213 }
2214 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2215
2216 test_33b() {
2217         rm -fr $DIR/d33
2218         test_mkdir -p $DIR/d33
2219         chown $RUNAS_ID $DIR/d33
2220         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2221 }
2222 run_test 33b "test open file with malformed flags (No panic and return error)"
2223
2224 test_33c() {
2225         local ostnum
2226         local ostname
2227         local write_bytes
2228         local all_zeros
2229
2230         remote_ost_nodsh && skip "remote OST with nodsh" && return
2231         all_zeros=:
2232         rm -fr $DIR/d33
2233         test_mkdir -p $DIR/d33
2234         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2235
2236         sync
2237         for ostnum in $(seq $OSTCOUNT); do
2238                 # test-framework's OST numbering is one-based, while Lustre's
2239                 # is zero-based
2240                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2241                 # Parsing llobdstat's output sucks; we could grep the /proc
2242                 # path, but that's likely to not be as portable as using the
2243                 # llobdstat utility.  So we parse lctl output instead.
2244                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2245                         obdfilter/$ostname/stats |
2246                         awk '/^write_bytes/ {print $7}' )
2247                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2248                 if (( ${write_bytes:-0} > 0 ))
2249                 then
2250                         all_zeros=false
2251                         break;
2252                 fi
2253         done
2254
2255         $all_zeros || return 0
2256
2257         # Write four bytes
2258         echo foo > $DIR/d33/bar
2259         # Really write them
2260         sync
2261
2262         # Total up write_bytes after writing.  We'd better find non-zeros.
2263         for ostnum in $(seq $OSTCOUNT); do
2264                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2265                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2266                         obdfilter/$ostname/stats |
2267                         awk '/^write_bytes/ {print $7}' )
2268                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2269                 if (( ${write_bytes:-0} > 0 ))
2270                 then
2271                         all_zeros=false
2272                         break;
2273                 fi
2274         done
2275
2276         if $all_zeros
2277         then
2278                 for ostnum in $(seq $OSTCOUNT); do
2279                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2280                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2281                         do_facet ost$ostnum lctl get_param -n \
2282                                 obdfilter/$ostname/stats
2283                 done
2284                 error "OST not keeping write_bytes stats (b22312)"
2285         fi
2286 }
2287 run_test 33c "test llobdstat and write_bytes"
2288
2289 test_33d() {
2290         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2291         local MDTIDX=1
2292         local remote_dir=$DIR/$tdir/remote_dir
2293
2294         mkdir -p $DIR/$tdir
2295         $LFS mkdir -i $MDTIDX $remote_dir ||
2296                 error "create remote directory failed"
2297
2298         touch $remote_dir/$tfile
2299         chmod 444 $remote_dir/$tfile
2300         chown $RUNAS_ID $remote_dir/$tfile
2301
2302         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2303
2304         chown $RUNAS_ID $remote_dir
2305         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2306                                         error "create" || true
2307         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2308                                     error "open RDWR" || true
2309         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2310                                     error "create" || true
2311 }
2312 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2313
2314 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2315 test_34a() {
2316         rm -f $DIR/f34
2317         $MCREATE $DIR/f34 || error
2318         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2319         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2320         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2321         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2322 }
2323 run_test 34a "truncate file that has not been opened ==========="
2324
2325 test_34b() {
2326         [ ! -f $DIR/f34 ] && test_34a
2327         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2328         $OPENFILE -f O_RDONLY $DIR/f34
2329         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2330         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2331 }
2332 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2333
2334 test_34c() {
2335         [ ! -f $DIR/f34 ] && test_34a
2336         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2337         $OPENFILE -f O_RDWR $DIR/f34
2338         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2339         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2340 }
2341 run_test 34c "O_RDWR opening file-with-size works =============="
2342
2343 test_34d() {
2344         [ ! -f $DIR/f34 ] && test_34a
2345         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2346         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2347         rm $DIR/f34
2348 }
2349 run_test 34d "write to sparse file ============================="
2350
2351 test_34e() {
2352         rm -f $DIR/f34e
2353         $MCREATE $DIR/f34e || error
2354         $TRUNCATE $DIR/f34e 1000 || error
2355         $CHECKSTAT -s 1000 $DIR/f34e || error
2356         $OPENFILE -f O_RDWR $DIR/f34e
2357         $CHECKSTAT -s 1000 $DIR/f34e || error
2358 }
2359 run_test 34e "create objects, some with size and some without =="
2360
2361 test_34f() { # bug 6242, 6243
2362         SIZE34F=48000
2363         rm -f $DIR/f34f
2364         $MCREATE $DIR/f34f || error
2365         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2366         dd if=$DIR/f34f of=$TMP/f34f
2367         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2368         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2369         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2370         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2371         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2372 }
2373 run_test 34f "read from a file with no objects until EOF ======="
2374
2375 test_34g() {
2376         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2377         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2378         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2379         cancel_lru_locks osc
2380         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2381                 error "wrong size after lock cancel"
2382
2383         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2384         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2385                 error "expanding truncate failed"
2386         cancel_lru_locks osc
2387         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2388                 error "wrong expanded size after lock cancel"
2389 }
2390 run_test 34g "truncate long file ==============================="
2391
2392 test_34h() {
2393         local gid=10
2394         local sz=1000
2395
2396         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2397         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2398         MULTIPID=$!
2399         sleep 2
2400
2401         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2402                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2403                 kill -9 $MULTIPID
2404         fi
2405         wait $MULTIPID
2406         local nsz=`stat -c %s $DIR/$tfile`
2407         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2408 }
2409 run_test 34h "ftruncate file under grouplock should not block"
2410
2411 test_35a() {
2412         cp /bin/sh $DIR/f35a
2413         chmod 444 $DIR/f35a
2414         chown $RUNAS_ID $DIR/f35a
2415         $RUNAS $DIR/f35a && error || true
2416         rm $DIR/f35a
2417 }
2418 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2419
2420 test_36a() {
2421         rm -f $DIR/f36
2422         utime $DIR/f36 || error
2423 }
2424 run_test 36a "MDS utime check (mknod, utime) ==================="
2425
2426 test_36b() {
2427         echo "" > $DIR/f36
2428         utime $DIR/f36 || error
2429 }
2430 run_test 36b "OST utime check (open, utime) ===================="
2431
2432 test_36c() {
2433         rm -f $DIR/d36/f36
2434         test_mkdir $DIR/d36
2435         chown $RUNAS_ID $DIR/d36
2436         $RUNAS utime $DIR/d36/f36 || error
2437 }
2438 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2439
2440 test_36d() {
2441         [ ! -d $DIR/d36 ] && test_36c
2442         echo "" > $DIR/d36/f36
2443         $RUNAS utime $DIR/d36/f36 || error
2444 }
2445 run_test 36d "non-root OST utime check (open, utime) ==========="
2446
2447 test_36e() {
2448         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2449         test_mkdir -p $DIR/$tdir
2450         touch $DIR/$tdir/$tfile
2451         $RUNAS utime $DIR/$tdir/$tfile && \
2452                 error "utime worked, expected failure" || true
2453 }
2454 run_test 36e "utime on non-owned file (should return error) ===="
2455
2456 subr_36fh() {
2457         local fl="$1"
2458         local LANG_SAVE=$LANG
2459         local LC_LANG_SAVE=$LC_LANG
2460         export LANG=C LC_LANG=C # for date language
2461
2462         DATESTR="Dec 20  2000"
2463         test_mkdir -p $DIR/$tdir
2464         lctl set_param fail_loc=$fl
2465         date; date +%s
2466         cp /etc/hosts $DIR/$tdir/$tfile
2467         sync & # write RPC generated with "current" inode timestamp, but delayed
2468         sleep 1
2469         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2470         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2471         cancel_lru_locks osc
2472         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2473         date; date +%s
2474         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2475                 echo "BEFORE: $LS_BEFORE" && \
2476                 echo "AFTER : $LS_AFTER" && \
2477                 echo "WANT  : $DATESTR" && \
2478                 error "$DIR/$tdir/$tfile timestamps changed" || true
2479
2480         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2481 }
2482
2483 test_36f() {
2484         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2485         subr_36fh "0x80000214"
2486 }
2487 run_test 36f "utime on file racing with OST BRW write =========="
2488
2489 test_36g() {
2490         remote_ost_nodsh && skip "remote OST with nodsh" && return
2491         local fmd_max_age
2492         local fmd_before
2493         local fmd_after
2494
2495         test_mkdir -p $DIR/$tdir
2496         fmd_max_age=$(do_facet ost1 \
2497                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2498                 head -n 1")
2499
2500         fmd_before=$(do_facet ost1 \
2501                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2502         touch $DIR/$tdir/$tfile
2503         sleep $((fmd_max_age + 12))
2504         fmd_after=$(do_facet ost1 \
2505                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2506
2507         echo "fmd_before: $fmd_before"
2508         echo "fmd_after: $fmd_after"
2509         [ "$fmd_after" -gt "$fmd_before" ] && \
2510                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2511                 error "fmd didn't expire after ping" || true
2512 }
2513 run_test 36g "filter mod data cache expiry ====================="
2514
2515 test_36h() {
2516         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2517         subr_36fh "0x80000227"
2518 }
2519 run_test 36h "utime on file racing with OST BRW write =========="
2520
2521 # test_37 - duplicate with tests 32q 32r
2522
2523 test_38() {
2524         local file=$DIR/$tfile
2525         touch $file
2526         openfile -f O_DIRECTORY $file
2527         local RC=$?
2528         local ENOTDIR=20
2529         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2530         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2531 }
2532 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2533
2534 test_39() {
2535         touch $DIR/$tfile
2536         touch $DIR/${tfile}2
2537 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2538 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2539 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2540         sleep 2
2541         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2542         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2543                 echo "mtime"
2544                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2545                 echo "atime"
2546                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2547                 echo "ctime"
2548                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2549                 error "O_TRUNC didn't change timestamps"
2550         fi
2551 }
2552 run_test 39 "mtime changed on create ==========================="
2553
2554 test_39b() {
2555         test_mkdir -p $DIR/$tdir
2556         cp -p /etc/passwd $DIR/$tdir/fopen
2557         cp -p /etc/passwd $DIR/$tdir/flink
2558         cp -p /etc/passwd $DIR/$tdir/funlink
2559         cp -p /etc/passwd $DIR/$tdir/frename
2560         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2561
2562         sleep 1
2563         echo "aaaaaa" >> $DIR/$tdir/fopen
2564         echo "aaaaaa" >> $DIR/$tdir/flink
2565         echo "aaaaaa" >> $DIR/$tdir/funlink
2566         echo "aaaaaa" >> $DIR/$tdir/frename
2567
2568         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2569         local link_new=`stat -c %Y $DIR/$tdir/flink`
2570         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2571         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2572
2573         cat $DIR/$tdir/fopen > /dev/null
2574         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2575         rm -f $DIR/$tdir/funlink2
2576         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2577
2578         for (( i=0; i < 2; i++ )) ; do
2579                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2580                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2581                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2582                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2583
2584                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2585                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2586                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2587                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2588
2589                 cancel_lru_locks osc
2590                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2591         done
2592 }
2593 run_test 39b "mtime change on open, link, unlink, rename  ======"
2594
2595 # this should be set to past
2596 TEST_39_MTIME=`date -d "1 year ago" +%s`
2597
2598 # bug 11063
2599 test_39c() {
2600         touch $DIR1/$tfile
2601         sleep 2
2602         local mtime0=`stat -c %Y $DIR1/$tfile`
2603
2604         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2605         local mtime1=`stat -c %Y $DIR1/$tfile`
2606         [ "$mtime1" = $TEST_39_MTIME ] || \
2607                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2608
2609         local d1=`date +%s`
2610         echo hello >> $DIR1/$tfile
2611         local d2=`date +%s`
2612         local mtime2=`stat -c %Y $DIR1/$tfile`
2613         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2614                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2615
2616         mv $DIR1/$tfile $DIR1/$tfile-1
2617
2618         for (( i=0; i < 2; i++ )) ; do
2619                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2620                 [ "$mtime2" = "$mtime3" ] || \
2621                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2622
2623                 cancel_lru_locks osc
2624                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2625         done
2626 }
2627 run_test 39c "mtime change on rename ==========================="
2628
2629 # bug 21114
2630 test_39d() {
2631         touch $DIR1/$tfile
2632
2633         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2634
2635         for (( i=0; i < 2; i++ )) ; do
2636                 local mtime=`stat -c %Y $DIR1/$tfile`
2637                 [ $mtime = $TEST_39_MTIME ] || \
2638                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2639
2640                 cancel_lru_locks osc
2641                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2642         done
2643 }
2644 run_test 39d "create, utime, stat =============================="
2645
2646 # bug 21114
2647 test_39e() {
2648         touch $DIR1/$tfile
2649         local mtime1=`stat -c %Y $DIR1/$tfile`
2650
2651         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2652
2653         for (( i=0; i < 2; i++ )) ; do
2654                 local mtime2=`stat -c %Y $DIR1/$tfile`
2655                 [ $mtime2 = $TEST_39_MTIME ] || \
2656                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2657
2658                 cancel_lru_locks osc
2659                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2660         done
2661 }
2662 run_test 39e "create, stat, utime, stat ========================"
2663
2664 # bug 21114
2665 test_39f() {
2666         touch $DIR1/$tfile
2667         mtime1=`stat -c %Y $DIR1/$tfile`
2668
2669         sleep 2
2670         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2671
2672         for (( i=0; i < 2; i++ )) ; do
2673                 local mtime2=`stat -c %Y $DIR1/$tfile`
2674                 [ $mtime2 = $TEST_39_MTIME ] || \
2675                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2676
2677                 cancel_lru_locks osc
2678                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2679         done
2680 }
2681 run_test 39f "create, stat, sleep, utime, stat ================="
2682
2683 # bug 11063
2684 test_39g() {
2685         echo hello >> $DIR1/$tfile
2686         local mtime1=`stat -c %Y $DIR1/$tfile`
2687
2688         sleep 2
2689         chmod o+r $DIR1/$tfile
2690
2691         for (( i=0; i < 2; i++ )) ; do
2692                 local mtime2=`stat -c %Y $DIR1/$tfile`
2693                 [ "$mtime1" = "$mtime2" ] || \
2694                         error "lost mtime: $mtime2, should be $mtime1"
2695
2696                 cancel_lru_locks osc
2697                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2698         done
2699 }
2700 run_test 39g "write, chmod, stat ==============================="
2701
2702 # bug 11063
2703 test_39h() {
2704         touch $DIR1/$tfile
2705         sleep 1
2706
2707         local d1=`date`
2708         echo hello >> $DIR1/$tfile
2709         local mtime1=`stat -c %Y $DIR1/$tfile`
2710
2711         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2712         local d2=`date`
2713         if [ "$d1" != "$d2" ]; then
2714                 echo "write and touch not within one second"
2715         else
2716                 for (( i=0; i < 2; i++ )) ; do
2717                         local mtime2=`stat -c %Y $DIR1/$tfile`
2718                         [ "$mtime2" = $TEST_39_MTIME ] || \
2719                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2720
2721                         cancel_lru_locks osc
2722                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2723                 done
2724         fi
2725 }
2726 run_test 39h "write, utime within one second, stat ============="
2727
2728 test_39i() {
2729         touch $DIR1/$tfile
2730         sleep 1
2731
2732         echo hello >> $DIR1/$tfile
2733         local mtime1=`stat -c %Y $DIR1/$tfile`
2734
2735         mv $DIR1/$tfile $DIR1/$tfile-1
2736
2737         for (( i=0; i < 2; i++ )) ; do
2738                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2739
2740                 [ "$mtime1" = "$mtime2" ] || \
2741                         error "lost mtime: $mtime2, should be $mtime1"
2742
2743                 cancel_lru_locks osc
2744                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2745         done
2746 }
2747 run_test 39i "write, rename, stat =============================="
2748
2749 test_39j() {
2750         start_full_debug_logging
2751         touch $DIR1/$tfile
2752         sleep 1
2753
2754         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2755         lctl set_param fail_loc=0x80000412
2756         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2757                 error "multiop failed"
2758         local multipid=$!
2759         local mtime1=`stat -c %Y $DIR1/$tfile`
2760
2761         mv $DIR1/$tfile $DIR1/$tfile-1
2762
2763         kill -USR1 $multipid
2764         wait $multipid || error "multiop close failed"
2765
2766         for (( i=0; i < 2; i++ )) ; do
2767                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2768                 [ "$mtime1" = "$mtime2" ] ||
2769                         error "mtime is lost on close: $mtime2, " \
2770                               "should be $mtime1"
2771
2772                 cancel_lru_locks osc
2773                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2774         done
2775         lctl set_param fail_loc=0
2776         stop_full_debug_logging
2777 }
2778 run_test 39j "write, rename, close, stat ======================="
2779
2780 test_39k() {
2781         touch $DIR1/$tfile
2782         sleep 1
2783
2784         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2785         local multipid=$!
2786         local mtime1=`stat -c %Y $DIR1/$tfile`
2787
2788         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2789
2790         kill -USR1 $multipid
2791         wait $multipid || error "multiop close failed"
2792
2793         for (( i=0; i < 2; i++ )) ; do
2794                 local mtime2=`stat -c %Y $DIR1/$tfile`
2795
2796                 [ "$mtime2" = $TEST_39_MTIME ] || \
2797                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2798
2799                 cancel_lru_locks osc
2800                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2801         done
2802 }
2803 run_test 39k "write, utime, close, stat ========================"
2804
2805 # this should be set to future
2806 TEST_39_ATIME=`date -d "1 year" +%s`
2807
2808 test_39l() {
2809         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2810         local atime_diff=$(do_facet $SINGLEMDS \
2811                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
2812         rm -rf $DIR/$tdir
2813         mkdir -p $DIR/$tdir
2814
2815         # test setting directory atime to future
2816         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2817         local atime=$(stat -c %X $DIR/$tdir)
2818         [ "$atime" = $TEST_39_ATIME ] || \
2819                 error "atime is not set to future: $atime, $TEST_39_ATIME"
2820
2821         # test setting directory atime from future to now
2822         local d1=$(date +%s)
2823         ls $DIR/$tdir
2824         local d2=$(date +%s)
2825
2826         cancel_lru_locks mdc
2827         atime=$(stat -c %X $DIR/$tdir)
2828         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2829                 error "atime is not updated from future: $atime, $d1<atime<$d2"
2830
2831         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
2832         sleep 3
2833
2834         # test setting directory atime when now > dir atime + atime_diff
2835         d1=$(date +%s)
2836         ls $DIR/$tdir
2837         d2=$(date +%s)
2838         cancel_lru_locks mdc
2839         atime=$(stat -c %X $DIR/$tdir)
2840         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2841                 error "atime is not updated  : $atime, should be $d2"
2842
2843         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
2844         sleep 3
2845
2846         # test not setting directory atime when now < dir atime + atime_diff
2847         ls $DIR/$tdir
2848         cancel_lru_locks mdc
2849         atime=$(stat -c %X $DIR/$tdir)
2850         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2851                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2852
2853         do_facet $SINGLEMDS \
2854                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
2855 }
2856 run_test 39l "directory atime update ==========================="
2857
2858 test_39m() {
2859         touch $DIR1/$tfile
2860         sleep 2
2861         local far_past_mtime=$(date -d "May 29 1953" +%s)
2862         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2863
2864         touch -m -d @$far_past_mtime $DIR1/$tfile
2865         touch -a -d @$far_past_atime $DIR1/$tfile
2866
2867         for (( i=0; i < 2; i++ )) ; do
2868                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2869                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2870                         error "atime or mtime set incorrectly"
2871
2872                 cancel_lru_locks osc
2873                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2874         done
2875 }
2876 run_test 39m "test atime and mtime before 1970"
2877
2878 test_40() {
2879         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2880         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2881         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2882 }
2883 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2884
2885 test_41() {
2886         # bug 1553
2887         small_write $DIR/f41 18
2888 }
2889 run_test 41 "test small file write + fstat ====================="
2890
2891 count_ost_writes() {
2892         lctl get_param -n osc.*.stats |
2893             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2894 }
2895
2896 # decent default
2897 WRITEBACK_SAVE=500
2898 DIRTY_RATIO_SAVE=40
2899 MAX_DIRTY_RATIO=50
2900 BG_DIRTY_RATIO_SAVE=10
2901 MAX_BG_DIRTY_RATIO=25
2902
2903 start_writeback() {
2904         trap 0
2905         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2906         # dirty_ratio, dirty_background_ratio
2907         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2908                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2909                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2910                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2911         else
2912                 # if file not here, we are a 2.4 kernel
2913                 kill -CONT `pidof kupdated`
2914         fi
2915 }
2916
2917 stop_writeback() {
2918         # setup the trap first, so someone cannot exit the test at the
2919         # exact wrong time and mess up a machine
2920         trap start_writeback EXIT
2921         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2922         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2923                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2924                 sysctl -w vm.dirty_writeback_centisecs=0
2925                 sysctl -w vm.dirty_writeback_centisecs=0
2926                 # save and increase /proc/sys/vm/dirty_ratio
2927                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2928                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2929                 # save and increase /proc/sys/vm/dirty_background_ratio
2930                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2931                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2932         else
2933                 # if file not here, we are a 2.4 kernel
2934                 kill -STOP `pidof kupdated`
2935         fi
2936 }
2937
2938 # ensure that all stripes have some grant before we test client-side cache
2939 setup_test42() {
2940         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2941                 dd if=/dev/zero of=$i bs=4k count=1
2942                 rm $i
2943         done
2944 }
2945
2946 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
2947 # file truncation, and file removal.
2948 test_42a() {
2949         setup_test42
2950         cancel_lru_locks osc
2951         stop_writeback
2952         sync; sleep 1; sync # just to be safe
2953         BEFOREWRITES=`count_ost_writes`
2954         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
2955         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
2956         AFTERWRITES=`count_ost_writes`
2957         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2958                 error "$BEFOREWRITES < $AFTERWRITES"
2959         start_writeback
2960 }
2961 run_test 42a "ensure that we don't flush on close =============="
2962
2963 test_42b() {
2964         setup_test42
2965         cancel_lru_locks osc
2966         stop_writeback
2967         sync
2968         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
2969         BEFOREWRITES=`count_ost_writes`
2970         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
2971         AFTERWRITES=`count_ost_writes`
2972         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2973                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
2974         fi
2975         BEFOREWRITES=`count_ost_writes`
2976         sync || error "sync: $?"
2977         AFTERWRITES=`count_ost_writes`
2978         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2979                 error "$BEFOREWRITES < $AFTERWRITES on sync"
2980         fi
2981         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
2982         start_writeback
2983         return 0
2984 }
2985 run_test 42b "test destroy of file with cached dirty data ======"
2986
2987 # if these tests just want to test the effect of truncation,
2988 # they have to be very careful.  consider:
2989 # - the first open gets a {0,EOF}PR lock
2990 # - the first write conflicts and gets a {0, count-1}PW
2991 # - the rest of the writes are under {count,EOF}PW
2992 # - the open for truncate tries to match a {0,EOF}PR
2993 #   for the filesize and cancels the PWs.
2994 # any number of fixes (don't get {0,EOF} on open, match
2995 # composite locks, do smarter file size management) fix
2996 # this, but for now we want these tests to verify that
2997 # the cancellation with truncate intent works, so we
2998 # start the file with a full-file pw lock to match against
2999 # until the truncate.
3000 trunc_test() {
3001         test=$1
3002         file=$DIR/$test
3003         offset=$2
3004         cancel_lru_locks osc
3005         stop_writeback
3006         # prime the file with 0,EOF PW to match
3007         touch $file
3008         $TRUNCATE $file 0
3009         sync; sync
3010         # now the real test..
3011         dd if=/dev/zero of=$file bs=1024 count=100
3012         BEFOREWRITES=`count_ost_writes`
3013         $TRUNCATE $file $offset
3014         cancel_lru_locks osc
3015         AFTERWRITES=`count_ost_writes`
3016         start_writeback
3017 }
3018
3019 test_42c() {
3020         trunc_test 42c 1024
3021         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3022             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3023         rm $file
3024 }
3025 run_test 42c "test partial truncate of file with cached dirty data"
3026
3027 test_42d() {
3028         trunc_test 42d 0
3029         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3030             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3031         rm $file
3032 }
3033 run_test 42d "test complete truncate of file with cached dirty data"
3034
3035 test_42e() { # bug22074
3036         local TDIR=$DIR/${tdir}e
3037         local pagesz=$(page_size)
3038         local pages=16 # hardcoded 16 pages, don't change it.
3039         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3040         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3041         local max_dirty_mb
3042         local warmup_files
3043
3044         test_mkdir -p $DIR/${tdir}e
3045         $SETSTRIPE -c 1 $TDIR
3046         createmany -o $TDIR/f $files
3047
3048         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3049
3050         # we assume that with $OSTCOUNT files, at least one of them will
3051         # be allocated on OST0.
3052         warmup_files=$((OSTCOUNT * max_dirty_mb))
3053         createmany -o $TDIR/w $warmup_files
3054
3055         # write a large amount of data into one file and sync, to get good
3056         # avail_grant number from OST.
3057         for ((i=0; i<$warmup_files; i++)); do
3058                 idx=$($GETSTRIPE -i $TDIR/w$i)
3059                 [ $idx -ne 0 ] && continue
3060                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3061                 break
3062         done
3063         [ $i -gt $warmup_files ] && error "OST0 is still cold"
3064         sync
3065         $LCTL get_param $proc_osc0/cur_dirty_bytes
3066         $LCTL get_param $proc_osc0/cur_grant_bytes
3067
3068         # create as much dirty pages as we can while not to trigger the actual
3069         # RPCs directly. but depends on the env, VFS may trigger flush during this
3070         # period, hopefully we are good.
3071         for ((i=0; i<$warmup_files; i++)); do
3072                 idx=$($GETSTRIPE -i $TDIR/w$i)
3073                 [ $idx -ne 0 ] && continue
3074                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3075         done
3076         $LCTL get_param $proc_osc0/cur_dirty_bytes
3077         $LCTL get_param $proc_osc0/cur_grant_bytes
3078
3079         # perform the real test
3080         $LCTL set_param $proc_osc0/rpc_stats 0
3081         for ((;i<$files; i++)); do
3082                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3083                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3084         done
3085         sync
3086         $LCTL get_param $proc_osc0/rpc_stats
3087
3088         local percent=0
3089         local have_ppr=false
3090         $LCTL get_param $proc_osc0/rpc_stats |
3091                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3092                         # skip lines until we are at the RPC histogram data
3093                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3094                         $have_ppr || continue
3095
3096                         # we only want the percent stat for < 16 pages
3097                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
3098
3099                         percent=$((percent + WPCT))
3100                         if [ $percent -gt 15 ]; then
3101                                 error "less than 16-pages write RPCs" \
3102                                       "$percent% > 15%"
3103                                 break
3104                         fi
3105                 done
3106         rm -rf $TDIR
3107 }
3108 run_test 42e "verify sub-RPC writes are not done synchronously"
3109
3110 test_43() {
3111         test_mkdir -p $DIR/$tdir
3112         cp -p /bin/ls $DIR/$tdir/$tfile
3113         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3114         pid=$!
3115         # give multiop a chance to open
3116         sleep 1
3117
3118         $DIR/$tdir/$tfile && error || true
3119         kill -USR1 $pid
3120 }
3121 run_test 43 "execution of file opened for write should return -ETXTBSY"
3122
3123 test_43a() {
3124         test_mkdir -p $DIR/$tdir
3125         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3126                         cp -p multiop $DIR/$tdir/multiop
3127         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3128                         return 1
3129         MULTIOP_PID=$!
3130         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3131         kill -USR1 $MULTIOP_PID || return 2
3132         wait $MULTIOP_PID || return 3
3133         rm $TMP/test43.junk
3134 }
3135 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3136
3137 test_43b() {
3138         test_mkdir -p $DIR/$tdir
3139         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3140                         cp -p multiop $DIR/$tdir/multiop
3141         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/test43.junk O_c ||
3142                         return 1
3143         MULTIOP_PID=$!
3144         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3145         kill -USR1 $MULTIOP_PID || return 2
3146         wait $MULTIOP_PID || return 3
3147         rm $TMP/test43.junk
3148 }
3149 run_test 43b "truncate of file being executed should return -ETXTBSY"
3150
3151 test_43c() {
3152         local testdir="$DIR/$tdir"
3153         test_mkdir -p $DIR/$tdir
3154         cp $SHELL $testdir/
3155         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3156                 ( cd $testdir && md5sum -c)
3157 }
3158 run_test 43c "md5sum of copy into lustre========================"
3159
3160 test_44() {
3161         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
3162         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3163         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3164 }
3165 run_test 44 "zero length read from a sparse stripe ============="
3166
3167 test_44a() {
3168     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
3169                          awk '{print $2}'`
3170     [ -z "$nstripe" ] && skip "can't get stripe info" && return
3171     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
3172     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
3173                       awk '{print $2}'`
3174     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
3175         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
3176     fi
3177
3178     OFFSETS="0 $((stride/2)) $((stride-1))"
3179     for offset in $OFFSETS ; do
3180       for i in `seq 0 $((nstripe-1))`; do
3181         local GLOBALOFFSETS=""
3182         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
3183         local myfn=$DIR/d44a-$size
3184         echo "--------writing $myfn at $size"
3185         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
3186         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3187         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3188                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3189
3190         for j in `seq 0 $((nstripe-1))`; do
3191             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
3192             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
3193             GLOBALOFFSETS="$GLOBALOFFSETS $size"
3194         done
3195         ll_sparseness_verify $myfn $GLOBALOFFSETS \
3196                             || error "ll_sparseness_verify $GLOBALOFFSETS"
3197         rm -f $myfn
3198       done
3199     done
3200 }
3201 run_test 44a "test sparse pwrite ==============================="
3202
3203 dirty_osc_total() {
3204         tot=0
3205         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3206                 tot=$(($tot + $d))
3207         done
3208         echo $tot
3209 }
3210 do_dirty_record() {
3211         before=`dirty_osc_total`
3212         echo executing "\"$*\""
3213         eval $*
3214         after=`dirty_osc_total`
3215         echo before $before, after $after
3216 }
3217 test_45() {
3218         f="$DIR/f45"
3219         # Obtain grants from OST if it supports it
3220         echo blah > ${f}_grant
3221         stop_writeback
3222         sync
3223         do_dirty_record "echo blah > $f"
3224         [ $before -eq $after ] && error "write wasn't cached"
3225         do_dirty_record "> $f"
3226         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3227         do_dirty_record "echo blah > $f"
3228         [ $before -eq $after ] && error "write wasn't cached"
3229         do_dirty_record "sync"
3230         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3231         do_dirty_record "echo blah > $f"
3232         [ $before -eq $after ] && error "write wasn't cached"
3233         do_dirty_record "cancel_lru_locks osc"
3234         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3235         start_writeback
3236 }
3237 run_test 45 "osc io page accounting ============================"
3238
3239 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3240 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3241 # objects offset and an assert hit when an rpc was built with 1023's mapped
3242 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3243 test_46() {
3244         f="$DIR/f46"
3245         stop_writeback
3246         sync
3247         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3248         sync
3249         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3250         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3251         sync
3252         start_writeback
3253 }
3254 run_test 46 "dirtying a previously written page ================"
3255
3256 # test_47 is removed "Device nodes check" is moved to test_28
3257
3258 test_48a() { # bug 2399
3259         check_kernel_version 34 || return 0
3260         test_mkdir -p $DIR/$tdir
3261         cd $DIR/$tdir
3262         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3263         test_mkdir $DIR/$tdir || error "recreate directory failed"
3264         touch foo || error "'touch foo' failed after recreating cwd"
3265         test_mkdir $DIR/$tdir/bar ||
3266                      error "'mkdir foo' failed after recreating cwd"
3267         if check_kernel_version 44; then
3268                 touch .foo || error "'touch .foo' failed after recreating cwd"
3269                 test_mkdir $DIR/$tdir/.bar ||
3270                               error "'mkdir .foo' failed after recreating cwd"
3271         fi
3272         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3273         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3274         cd . || error "'cd .' failed after recreating cwd"
3275         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3276         rmdir . && error "'rmdir .' worked after recreating cwd"
3277         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3278         cd .. || error "'cd ..' failed after recreating cwd"
3279 }
3280 run_test 48a "Access renamed working dir (should return errors)="
3281
3282 test_48b() { # bug 2399
3283         check_kernel_version 34 || return 0
3284         rm -rf $DIR/$tdir
3285         test_mkdir -p $DIR/$tdir
3286         cd $DIR/$tdir
3287         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3288         touch foo && error "'touch foo' worked after removing cwd"
3289         test_mkdir $DIR/$tdir/foo &&
3290                      error "'mkdir foo' worked after removing cwd"
3291         if check_kernel_version 44; then
3292                 touch .foo && error "'touch .foo' worked after removing cwd"
3293                 test_mkdir $DIR/$tdir/.foo &&
3294                               error "'mkdir .foo' worked after removing cwd"
3295         fi
3296         ls . > /dev/null && error "'ls .' worked after removing cwd"
3297         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3298         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3299         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3300         rmdir . && error "'rmdir .' worked after removing cwd"
3301         ln -s . foo && error "'ln -s .' worked after removing cwd"
3302         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3303 }
3304 run_test 48b "Access removed working dir (should return errors)="
3305
3306 test_48c() { # bug 2350
3307         check_kernel_version 36 || return 0
3308         #lctl set_param debug=-1
3309         #set -vx
3310         rm -rf $DIR/$tdir
3311         test_mkdir -p $DIR/$tdir/dir
3312         cd $DIR/$tdir/dir
3313         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3314         $TRACE touch foo && error "touch foo worked after removing cwd"
3315         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3316         if check_kernel_version 44; then
3317                 touch .foo && error "touch .foo worked after removing cwd"
3318                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3319         fi
3320         $TRACE ls . && error "'ls .' worked after removing cwd"
3321         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3322         is_patchless || ( $TRACE cd . &&
3323                         error "'cd .' worked after removing cwd" )
3324         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3325         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3326         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3327         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3328 }
3329 run_test 48c "Access removed working subdir (should return errors)"
3330
3331 test_48d() { # bug 2350
3332         check_kernel_version 36 || return 0
3333         #lctl set_param debug=-1
3334         #set -vx
3335         rm -rf $DIR/$tdir
3336         test_mkdir -p $DIR/$tdir/dir
3337         cd $DIR/$tdir/dir
3338         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3339         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3340         $TRACE touch foo && error "'touch foo' worked after removing parent"
3341         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3342         if check_kernel_version 44; then
3343                 touch .foo && error "'touch .foo' worked after removing parent"
3344                 test_mkdir .foo &&
3345                               error "mkdir .foo worked after removing parent"
3346         fi
3347         $TRACE ls . && error "'ls .' worked after removing parent"
3348         $TRACE ls .. && error "'ls ..' worked after removing parent"
3349         is_patchless || ( $TRACE cd . &&
3350                         error "'cd .' worked after recreate parent" )
3351         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3352         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3353         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3354         is_patchless || ( $TRACE cd .. &&
3355                         error "'cd ..' worked after removing parent" || true )
3356 }
3357 run_test 48d "Access removed parent subdir (should return errors)"
3358
3359 test_48e() { # bug 4134
3360         check_kernel_version 41 || return 0
3361         #lctl set_param debug=-1
3362         #set -vx
3363         rm -rf $DIR/$tdir
3364         test_mkdir -p $DIR/$tdir/dir
3365         cd $DIR/$tdir/dir
3366         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3367         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3368         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3369         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3370         # On a buggy kernel addition of "touch foo" after cd .. will
3371         # produce kernel oops in lookup_hash_it
3372         touch ../foo && error "'cd ..' worked after recreate parent"
3373         cd $DIR
3374         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3375 }
3376 run_test 48e "Access to recreated parent subdir (should return errors)"
3377
3378 test_49() { # LU-1030
3379         # get ost1 size - lustre-OST0000
3380         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3381         # write 800M at maximum
3382         [ $ost1_size -gt 819200 ] && ost1_size=819200
3383
3384         lfs setstripe -c 1 -i 0 $DIR/$tfile
3385         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3386         local dd_pid=$!
3387
3388         # change max_pages_per_rpc while writing the file
3389         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3390         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3391         # loop until dd process exits
3392         while ps ax -opid | grep -wq $dd_pid; do
3393                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3394                 sleep $((RANDOM % 5 + 1))
3395         done
3396         # restore original max_pages_per_rpc
3397         $LCTL set_param $osc1_mppc=$orig_mppc
3398         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3399 }
3400 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3401
3402 test_50() {
3403         # bug 1485
3404         test_mkdir $DIR/$tdir
3405         cd $DIR/$tdir
3406         ls /proc/$$/cwd || error
3407 }
3408 run_test 50 "special situations: /proc symlinks  ==============="
3409
3410 test_51a() {    # was test_51
3411         # bug 1516 - create an empty entry right after ".." then split dir
3412         test_mkdir $DIR/$tdir
3413         touch $DIR/$tdir/foo
3414         $MCREATE $DIR/$tdir/bar
3415         rm $DIR/$tdir/foo
3416         createmany -m $DIR/$tdir/longfile 201
3417         FNUM=202
3418         while [ `ls -sd $DIR/$tdir | awk '{ print $1 }'` -eq 4 ]; do
3419                 $MCREATE $DIR/$tdir/longfile$FNUM
3420                 FNUM=$(($FNUM + 1))
3421                 echo -n "+"
3422         done
3423         echo
3424         ls -l $DIR/$tdir > /dev/null || error
3425 }
3426 run_test 51a "special situations: split htree with empty entry =="
3427
3428 export NUMTEST=70000
3429 test_51b() {
3430         local BASE=$DIR/$tdir
3431
3432         test_mkdir -p $BASE
3433
3434         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3435         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3436         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3437                 return
3438
3439         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3440                 echo "reduced count to $NUMTEST due to inodes"
3441
3442         # need to check free space for the directories as well
3443         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3444         numfree=$((blkfree / 4))
3445         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3446                 echo "reduced count to $NUMTEST due to blocks"
3447
3448         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3449                 echo "failed" > $BASE/fnum
3450 }
3451 run_test 51b "exceed 64k subdirectory nlink limit"
3452
3453 test_51ba() { # LU-993
3454         local BASE=$DIR/$tdir
3455         # unlink all but 100 subdirectories, then check it still works
3456         local LEFT=100
3457         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3458
3459         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3460         local DELETE=$((NUMTEST - LEFT))
3461
3462         # continue on to run this test even if 51b didn't finish,
3463         # just to delete the many subdirectories created.
3464         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3465
3466         # for ldiskfs the nlink count should be 1, but this is OSD specific
3467         # and so this is listed for informational purposes only
3468         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3469         unlinkmany -d $BASE/d $DELETE
3470         RC=$?
3471
3472         if [ $RC -ne 0 ]; then
3473                 if [ "$NUMPREV" == "failed" ]; then
3474                         skip "previous setup failed"
3475                         return 0
3476                 else
3477                         error "unlink of first $DELETE subdirs failed"
3478                         return $RC
3479                 fi
3480         fi
3481
3482         echo "nlink between: $(stat -c %h $BASE)"
3483         # trim the first line of ls output
3484         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3485         [ $FOUND -ne $LEFT ] &&
3486                 error "can't find subdirs: found only $FOUND/$LEFT"
3487
3488         unlinkmany -d $BASE/d $DELETE $LEFT ||
3489                 error "unlink of second $LEFT subdirs failed"
3490         # regardless of whether the backing filesystem tracks nlink accurately
3491         # or not, the nlink count shouldn't be more than "." and ".." here
3492         local AFTER=$(stat -c %h $BASE)
3493         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3494                 echo "nlink after: $AFTER"
3495 }
3496 run_test 51ba "verify nlink for many subdirectory cleanup"
3497
3498 test_51d() {
3499         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3500         test_mkdir -p $DIR/$tdir
3501         createmany -o $DIR/$tdir/t- 1000
3502         $GETSTRIPE $DIR/$tdir > $TMP/files
3503         for N in `seq 0 $((OSTCOUNT - 1))`; do
3504             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3505             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3506             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3507         done
3508         unlinkmany $DIR/$tdir/t- 1000
3509
3510         NLAST=0
3511         for N in `seq 1 $((OSTCOUNT - 1))`; do
3512             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3513                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3514             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3515                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3516
3517             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3518                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3519             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3520                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3521             NLAST=$N
3522         done
3523 }
3524 run_test 51d "check object distribution ===================="
3525
3526 test_52a() {
3527         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3528         test_mkdir -p $DIR/$tdir
3529         touch $DIR/$tdir/foo
3530         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3531         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3532         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3533         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3534         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3535                                         error "link worked"
3536         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3537         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3538         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3539                                                      error "lsattr"
3540         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3541         cp -r $DIR/$tdir /tmp/
3542         rm -fr $DIR/$tdir || error "cleanup rm failed"
3543 }
3544 run_test 52a "append-only flag test (should return errors) ====="
3545
3546 test_52b() {
3547         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3548         test_mkdir -p $DIR/$tdir
3549         touch $DIR/$tdir/foo
3550         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3551         cat test > $DIR/$tdir/foo && error "cat test worked"
3552         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3553         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3554         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3555                                         error "link worked"
3556         echo foo >> $DIR/$tdir/foo && error "echo worked"
3557         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3558         [ -f $DIR/$tdir/foo ] || error
3559         [ -f $DIR/$tdir/foo_ren ] && error
3560         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
3561                                                         error "lsattr"
3562         chattr -i $DIR/$tdir/foo || error "chattr failed"
3563
3564         rm -fr $DIR/$tdir || error
3565 }
3566 run_test 52b "immutable flag test (should return errors) ======="
3567
3568 test_53() {
3569         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3570         remote_ost_nodsh && skip "remote OST with nodsh" && return
3571
3572         local param
3573         local ostname
3574         local mds_last
3575         local ost_last
3576         local ostnum
3577
3578         # only test MDT0000
3579         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3580         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3581                 param=`echo ${value[0]} | cut -d "=" -f1`
3582                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3583                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3584                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
3585                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id | head -n 1)
3586                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3587                 if [ $ost_last != $mds_last ]; then
3588                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3589                 fi
3590         done
3591 }
3592 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3593
3594 test_54a() {
3595         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3596         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3597         $SOCKETSERVER $DIR/socket
3598         $SOCKETCLIENT $DIR/socket || error
3599         $MUNLINK $DIR/socket
3600 }
3601 run_test 54a "unix domain socket test =========================="
3602
3603 test_54b() {
3604         f="$DIR/f54b"
3605         mknod $f c 1 3
3606         chmod 0666 $f
3607         dd if=/dev/zero of=$f bs=`page_size` count=1
3608 }
3609 run_test 54b "char device works in lustre ======================"
3610
3611 find_loop_dev() {
3612         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3613         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3614         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3615
3616         for i in `seq 3 7`; do
3617                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3618                 LOOPDEV=$LOOPBASE$i
3619                 LOOPNUM=$i
3620                 break
3621         done
3622 }
3623
3624 test_54c() {
3625         tfile="$DIR/f54c"
3626         tdir="$DIR/d54c"
3627         loopdev="$DIR/loop54c"
3628
3629         find_loop_dev
3630         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3631         mknod $loopdev b 7 $LOOPNUM
3632         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3633         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3634         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3635         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3636         test_mkdir -p $tdir
3637         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3638         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3639         df $tdir
3640         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3641         $UMOUNT $tdir
3642         losetup -d $loopdev
3643         rm $loopdev
3644 }
3645 run_test 54c "block device works in lustre ====================="
3646
3647 test_54d() {
3648         f="$DIR/f54d"
3649         string="aaaaaa"
3650         mknod $f p
3651         [ "$string" = `echo $string > $f | cat $f` ] || error
3652 }
3653 run_test 54d "fifo device works in lustre ======================"
3654
3655 test_54e() {
3656         check_kernel_version 46 || return 0
3657         f="$DIR/f54e"
3658         string="aaaaaa"
3659         cp -aL /dev/console $f
3660         echo $string > $f || error
3661 }
3662 run_test 54e "console/tty device works in lustre ======================"
3663
3664 #The test_55 used to be iopen test and it was removed by bz#24037.
3665 #run_test 55 "check iopen_connect_dentry() ======================"
3666
3667 test_56a() {    # was test_56
3668         rm -rf $DIR/$tdir
3669         $SETSTRIPE -d $DIR
3670         test_mkdir $DIR/$tdir
3671         test_mkdir $DIR/$tdir/dir
3672         NUMFILES=3
3673         NUMFILESx2=$(($NUMFILES * 2))
3674         for i in `seq 1 $NUMFILES` ; do
3675                 touch $DIR/$tdir/file$i
3676                 touch $DIR/$tdir/dir/file$i
3677         done
3678
3679         # test lfs getstripe with --recursive
3680         FILENUM=`$GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx`
3681         [ $FILENUM -eq $NUMFILESx2 ] ||
3682                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3683         FILENUM=`$GETSTRIPE $DIR/$tdir | grep -c obdidx`
3684         [ $FILENUM -eq $NUMFILES ] ||
3685                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
3686         echo "$GETSTRIPE --recursive passed."
3687
3688         # test lfs getstripe with file instead of dir
3689         FILENUM=`$GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx`
3690         [ $FILENUM  -eq 1 ] || error \
3691                  "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
3692         echo "$GETSTRIPE file1 passed."
3693
3694         #test lfs getstripe with --verbose
3695         [ `$GETSTRIPE --verbose $DIR/$tdir |
3696                         grep -c lmm_magic` -eq $NUMFILES ] ||
3697                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
3698         [ `$GETSTRIPE $DIR/$tdir | grep -c lmm_magic` -eq 0 ] ||
3699             error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
3700         echo "$GETSTRIPE --verbose passed."
3701
3702         #test lfs getstripe with --obd
3703         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
3704                                         grep -q "unknown obduuid" ||
3705                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3706
3707         [  "$OSTCOUNT" -lt 2 ] &&
3708                 skip_env "skipping other $GETSTRIPE --obd test" && return
3709
3710         OSTIDX=1
3711         OBDUUID=$(ostuuid_from_index $OSTIDX)
3712         FILENUM=`$GETSTRIPE -ir $DIR/$tdir | grep -x $OSTIDX | wc -l`
3713         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l`
3714         [ $FOUND -eq $FILENUM ] ||
3715                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3716         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
3717                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3718                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3719                 error "$GETSTRIPE --obd: should not show file on other obd"
3720         echo "$GETSTRIPE --obd passed"
3721 }
3722 run_test 56a "check $GETSTRIPE"
3723
3724 NUMFILES=3
3725 NUMDIRS=3
3726 setup_56() {
3727         local LOCAL_NUMFILES="$1"
3728         local LOCAL_NUMDIRS="$2"
3729         local MKDIR_PARAMS="$3"
3730
3731         if [ ! -d "$TDIR" ] ; then
3732                 test_mkdir -p $TDIR
3733                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3734                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3735                         touch $TDIR/file$i
3736                 done
3737                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3738                         test_mkdir $TDIR/dir$i
3739                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3740                                 touch $TDIR/dir$i/file$j
3741                         done
3742                 done
3743         fi
3744 }
3745
3746 setup_56_special() {
3747         LOCAL_NUMFILES=$1
3748         LOCAL_NUMDIRS=$2
3749         setup_56 $1 $2
3750         if [ ! -e "$TDIR/loop1b" ] ; then
3751                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3752                         mknod $TDIR/loop${i}b b 7 $i
3753                         mknod $TDIR/null${i}c c 1 3
3754                         ln -s $TDIR/file1 $TDIR/link${i}l
3755                 done
3756                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3757                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3758                         mknod $TDIR/dir$i/null${i}c c 1 3
3759                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3760                 done
3761         fi
3762 }
3763
3764 test_56g() {
3765         $SETSTRIPE -d $DIR
3766
3767         TDIR=$DIR/${tdir}g
3768         setup_56 $NUMFILES $NUMDIRS
3769
3770         EXPECTED=$(($NUMDIRS + 2))
3771         # test lfs find with -name
3772         for i in $(seq 1 $NUMFILES) ; do
3773                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3774                 [ $NUMS -eq $EXPECTED ] ||
3775                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3776                               "found $NUMS, expected $EXPECTED"
3777         done
3778 }
3779 run_test 56g "check lfs find -name ============================="
3780
3781 test_56h() {
3782         $SETSTRIPE -d $DIR
3783
3784         TDIR=$DIR/${tdir}g
3785         setup_56 $NUMFILES $NUMDIRS
3786
3787         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3788         # test lfs find with ! -name
3789         for i in $(seq 1 $NUMFILES) ; do
3790                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3791                 [ $NUMS -eq $EXPECTED ] ||
3792                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3793                               "found $NUMS, expected $EXPECTED"
3794         done
3795 }
3796 run_test 56h "check lfs find ! -name ============================="
3797
3798 test_56i() {
3799        tdir=${tdir}i
3800        test_mkdir -p $DIR/$tdir
3801        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3802        CMD="$LFIND -ost $UUID $DIR/$tdir"
3803        OUT=$($CMD)
3804        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3805 }
3806 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3807
3808 test_56j() {
3809         TDIR=$DIR/${tdir}g
3810         setup_56_special $NUMFILES $NUMDIRS
3811
3812         EXPECTED=$((NUMDIRS + 1))
3813         CMD="$LFIND -type d $TDIR"
3814         NUMS=$($CMD | wc -l)
3815         [ $NUMS -eq $EXPECTED ] ||
3816                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3817 }
3818 run_test 56j "check lfs find -type d ============================="
3819
3820 test_56k() {
3821         TDIR=$DIR/${tdir}g
3822         setup_56_special $NUMFILES $NUMDIRS
3823
3824         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3825         CMD="$LFIND -type f $TDIR"
3826         NUMS=$($CMD | wc -l)
3827         [ $NUMS -eq $EXPECTED ] ||
3828                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3829 }
3830 run_test 56k "check lfs find -type f ============================="
3831
3832 test_56l() {
3833         TDIR=$DIR/${tdir}g
3834         setup_56_special $NUMFILES $NUMDIRS
3835
3836         EXPECTED=$((NUMDIRS + NUMFILES))
3837         CMD="$LFIND -type b $TDIR"
3838         NUMS=$($CMD | wc -l)
3839         [ $NUMS -eq $EXPECTED ] ||
3840                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3841 }
3842 run_test 56l "check lfs find -type b ============================="
3843
3844 test_56m() {
3845         TDIR=$DIR/${tdir}g
3846         setup_56_special $NUMFILES $NUMDIRS
3847
3848         EXPECTED=$((NUMDIRS + NUMFILES))
3849         CMD="$LFIND -type c $TDIR"
3850         NUMS=$($CMD | wc -l)
3851         [ $NUMS -eq $EXPECTED ] ||
3852                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3853 }
3854 run_test 56m "check lfs find -type c ============================="
3855
3856 test_56n() {
3857         TDIR=$DIR/${tdir}g
3858         setup_56_special $NUMFILES $NUMDIRS
3859
3860         EXPECTED=$((NUMDIRS + NUMFILES))
3861         CMD="$LFIND -type l $TDIR"
3862         NUMS=$($CMD | wc -l)
3863         [ $NUMS -eq $EXPECTED ] ||
3864                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3865 }
3866 run_test 56n "check lfs find -type l ============================="
3867
3868 test_56o() {
3869         TDIR=$DIR/${tdir}o
3870         setup_56 $NUMFILES $NUMDIRS
3871
3872         utime $TDIR/file1 > /dev/null || error "utime (1)"
3873         utime $TDIR/file2 > /dev/null || error "utime (2)"
3874         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3875         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3876         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3877         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3878
3879         EXPECTED=4
3880         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3881         [ $NUMS -eq $EXPECTED ] || \
3882                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3883
3884         EXPECTED=12
3885         CMD="$LFIND -mtime 0 $TDIR"
3886         NUMS=$($CMD | wc -l)
3887         [ $NUMS -eq $EXPECTED ] ||
3888                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3889 }
3890 run_test 56o "check lfs find -mtime for old files =========================="
3891
3892 test_56p() {
3893         [ $RUNAS_ID -eq $UID ] &&
3894                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3895
3896         TDIR=$DIR/${tdir}p
3897         setup_56 $NUMFILES $NUMDIRS
3898
3899         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3900         EXPECTED=$NUMFILES
3901         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3902         NUMS=$($CMD | wc -l)
3903         [ $NUMS -eq $EXPECTED ] || \
3904                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3905
3906         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3907         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3908         NUMS=$($CMD | wc -l)
3909         [ $NUMS -eq $EXPECTED ] || \
3910                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3911 }
3912 run_test 56p "check lfs find -uid and ! -uid ==============================="
3913
3914 test_56q() {
3915         [ $RUNAS_ID -eq $UID ] &&
3916                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3917
3918         TDIR=$DIR/${tdir}q
3919         setup_56 $NUMFILES $NUMDIRS
3920
3921         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3922
3923         EXPECTED=$NUMFILES
3924         CMD="$LFIND -gid $RUNAS_GID $TDIR"
3925         NUMS=$($CMD | wc -l)
3926         [ $NUMS -eq $EXPECTED ] ||
3927                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3928
3929         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
3930         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
3931         NUMS=$($CMD | wc -l)
3932         [ $NUMS -eq $EXPECTED ] ||
3933                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3934 }
3935 run_test 56q "check lfs find -gid and ! -gid ==============================="
3936
3937 test_56r() {
3938         TDIR=$DIR/${tdir}r
3939         setup_56 $NUMFILES $NUMDIRS
3940
3941         EXPECTED=12
3942         CMD="$LFIND -size 0 -type f $TDIR"
3943         NUMS=$($CMD | wc -l)
3944         [ $NUMS -eq $EXPECTED ] ||
3945                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3946         EXPECTED=0
3947         CMD="$LFIND ! -size 0 -type f $TDIR"
3948         NUMS=$($CMD | wc -l)
3949         [ $NUMS -eq $EXPECTED ] ||
3950                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3951         echo "test" > $TDIR/$tfile
3952         echo "test2" > $TDIR/$tfile.2 && sync
3953         EXPECTED=1
3954         CMD="$LFIND -size 5 -type f $TDIR"
3955         NUMS=$($CMD | wc -l)
3956         [ $NUMS -eq $EXPECTED ] ||
3957                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3958         EXPECTED=1
3959         CMD="$LFIND -size +5 -type f $TDIR"
3960         NUMS=$($CMD | wc -l)
3961         [ $NUMS -eq $EXPECTED ] ||
3962                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3963         EXPECTED=2
3964         CMD="$LFIND -size +0 -type f $TDIR"
3965         NUMS=$($CMD | wc -l)
3966         [ $NUMS -eq $EXPECTED ] ||
3967                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3968         EXPECTED=2
3969         CMD="$LFIND ! -size -5 -type f $TDIR"
3970         NUMS=$($CMD | wc -l)
3971         [ $NUMS -eq $EXPECTED ] ||
3972                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3973         EXPECTED=12
3974         CMD="$LFIND -size -5 -type f $TDIR"
3975         NUMS=$($CMD | wc -l)
3976         [ $NUMS -eq $EXPECTED ] ||
3977                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3978 }
3979 run_test 56r "check lfs find -size works =========================="
3980
3981 test_56s() { # LU-611
3982         TDIR=$DIR/${tdir}s
3983         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3984
3985         if [ $OSTCOUNT -gt 1 ]; then
3986                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
3987                 ONESTRIPE=4
3988                 EXTRA=4
3989         else
3990                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
3991                 EXTRA=0
3992         fi
3993
3994         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3995         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
3996         NUMS=$($CMD | wc -l)
3997         [ $NUMS -eq $EXPECTED ] ||
3998                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3999
4000         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4001         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4002         NUMS=$($CMD | wc -l)
4003         [ $NUMS -eq $EXPECTED ] ||
4004                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4005
4006         EXPECTED=$ONESTRIPE
4007         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4008         NUMS=$($CMD | wc -l)
4009         [ $NUMS -eq $EXPECTED ] ||
4010                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4011
4012         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4013         NUMS=$($CMD | wc -l)
4014         [ $NUMS -eq $EXPECTED ] ||
4015                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4016
4017         EXPECTED=0
4018         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4019         NUMS=$($CMD | wc -l)
4020         [ $NUMS -eq $EXPECTED ] ||
4021                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4022 }
4023 run_test 56s "check lfs find -stripe-count works"
4024
4025 test_56t() { # LU-611
4026         TDIR=$DIR/${tdir}t
4027         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4028
4029         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4030
4031         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4032         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4033         NUMS=$($CMD | wc -l)
4034         [ $NUMS -eq $EXPECTED ] ||
4035                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4036
4037         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4038         NUMS=$($CMD | wc -l)
4039         [ $NUMS -eq $EXPECTED ] ||
4040                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4041
4042         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4043         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4044         NUMS=$($CMD | wc -l)
4045         [ $NUMS -eq $EXPECTED ] ||
4046                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4047
4048         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4049         NUMS=$($CMD | wc -l)
4050         [ $NUMS -eq $EXPECTED ] ||
4051                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4052
4053         EXPECTED=4
4054         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4055         NUMS=$($CMD | wc -l)
4056         [ $NUMS -eq $EXPECTED ] ||
4057                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4058
4059         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4060         NUMS=$($CMD | wc -l)
4061         [ $NUMS -eq $EXPECTED ] ||
4062                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4063
4064         EXPECTED=0
4065         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4066         NUMS=$($CMD | wc -l)
4067         [ $NUMS -eq $EXPECTED ] ||
4068                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4069 }
4070 run_test 56t "check lfs find -stripe-size works"
4071
4072 test_56u() { # LU-611
4073         TDIR=$DIR/${tdir}u
4074         setup_56 $NUMFILES $NUMDIRS "-i 0"
4075
4076         if [ $OSTCOUNT -gt 1 ]; then
4077                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4078                 ONESTRIPE=4
4079         else
4080                 ONESTRIPE=0
4081         fi
4082
4083         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4084         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4085         NUMS=$($CMD | wc -l)
4086         [ $NUMS -eq $EXPECTED ] ||
4087                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4088
4089         EXPECTED=$ONESTRIPE
4090         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4091         NUMS=$($CMD | wc -l)
4092         [ $NUMS -eq $EXPECTED ] ||
4093                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4094
4095         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4096         NUMS=$($CMD | wc -l)
4097         [ $NUMS -eq $EXPECTED ] ||
4098                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4099
4100         EXPECTED=0
4101         # This should produce an error and not return any files
4102         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4103         NUMS=$($CMD 2>/dev/null | wc -l)
4104         [ $NUMS -eq $EXPECTED ] ||
4105                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4106
4107         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4108         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4109         NUMS=$($CMD | wc -l)
4110         [ $NUMS -eq $EXPECTED ] ||
4111                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4112 }
4113 run_test 56u "check lfs find -stripe-index works"
4114
4115 test_56v() {
4116     local MDT_IDX=0
4117
4118     TDIR=$DIR/${tdir}v
4119     rm -rf $TDIR
4120     setup_56 $NUMFILES $NUMDIRS
4121
4122     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4123     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4124
4125     for file in $($LFIND -mdt $UUID $TDIR); do
4126         file_mdt_idx=$($GETSTRIPE -M $file)
4127         [ $file_mdt_idx -eq $MDT_IDX ] ||
4128             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4129     done
4130 }
4131 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4132
4133 # Get and check the actual stripe count of one file.
4134 # Usage: check_stripe_count <file> <expected_stripe_count>
4135 check_stripe_count() {
4136     local file=$1
4137     local expected=$2
4138     local actual
4139
4140     [[ -z "$file" || -z "$expected" ]] &&
4141         error "check_stripe_count: invalid argument!"
4142
4143     local cmd="$GETSTRIPE -c $file"
4144     actual=$($cmd) || error "$cmd failed"
4145     actual=${actual%% *}
4146
4147     if [[ $actual -ne $expected ]]; then
4148         [[ $expected -eq -1 ]] ||
4149             error "$cmd wrong: found $actual, expected $expected"
4150         [[ $actual -eq $OSTCOUNT ]] ||
4151             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4152     fi
4153 }
4154
4155 test_56w() {
4156     TDIR=$DIR/${tdir}w
4157
4158     rm -rf $TDIR || error "remove $TDIR failed"
4159     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4160
4161     local stripe_size
4162     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4163         error "$GETSTRIPE -S -d $TDIR failed"
4164     stripe_size=${stripe_size%% *}
4165
4166     local file_size=$((stripe_size * OSTCOUNT))
4167     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4168     local required_space=$((file_num * file_size))
4169     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4170     [[ $free_space -le $((required_space / 1024)) ]] &&
4171         skip_env "need at least $required_space bytes free space," \
4172                  "have $free_space kbytes" && return
4173
4174     local dd_bs=65536
4175     local dd_count=$((file_size / dd_bs))
4176
4177     # write data into the files
4178     local i
4179     local j
4180     local file
4181     for i in $(seq 1 $NUMFILES); do
4182         file=$TDIR/file$i
4183         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4184             error "write data into $file failed"
4185     done
4186     for i in $(seq 1 $NUMDIRS); do
4187         for j in $(seq 1 $NUMFILES); do
4188             file=$TDIR/dir$i/file$j
4189             yes | dd bs=$dd_bs count=$dd_count of=$file \
4190                 >/dev/null 2>&1 ||
4191                 error "write data into $file failed"
4192         done
4193     done
4194
4195     local expected=-1
4196     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4197
4198     # lfs_migrate file
4199     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4200     echo "$cmd"
4201     eval $cmd || error "$cmd failed"
4202
4203     check_stripe_count $TDIR/file1 $expected
4204
4205     # lfs_migrate dir
4206     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4207     echo "$cmd"
4208     eval $cmd || error "$cmd failed"
4209
4210     for j in $(seq 1 $NUMFILES); do
4211         check_stripe_count $TDIR/dir1/file$j $expected
4212     done
4213
4214     # lfs_migrate works with lfs find
4215     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4216          $LFS_MIGRATE -y -c $expected"
4217     echo "$cmd"
4218     eval $cmd || error "$cmd failed"
4219
4220     for i in $(seq 2 $NUMFILES); do
4221         check_stripe_count $TDIR/file$i $expected
4222     done
4223     for i in $(seq 2 $NUMDIRS); do
4224         for j in $(seq 1 $NUMFILES); do
4225             check_stripe_count $TDIR/dir$i/file$j $expected
4226         done
4227     done
4228 }
4229 run_test 56w "check lfs_migrate -c stripe_count works"
4230
4231 test_57a() {
4232         # note test will not do anything if MDS is not local
4233         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4234                 skip "Only applicable to ldiskfs-based MDTs"
4235                 return
4236         fi
4237
4238         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4239         local MNTDEV="osd*.*MDT*.mntdev"
4240         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4241         [ -z "$DEV" ] && error "can't access $MNTDEV"
4242         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4243                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4244                         error "can't access $DEV"
4245                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4246                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4247                 rm $TMP/t57a.dump
4248         done
4249 }
4250 run_test 57a "verify MDS filesystem created with large inodes =="
4251
4252 test_57b() {
4253         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4254                 skip "Only applicable to ldiskfs-based MDTs"
4255                 return
4256         fi
4257
4258         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4259         local dir=$DIR/d57b
4260
4261         local FILECOUNT=100
4262         local FILE1=$dir/f1
4263         local FILEN=$dir/f$FILECOUNT
4264
4265         rm -rf $dir || error "removing $dir"
4266         test_mkdir -p $dir || error "creating $dir"
4267         local num=$(get_mds_dir $dir)
4268         local mymds=mds$num
4269
4270         echo "mcreating $FILECOUNT files"
4271         createmany -m $dir/f 1 $FILECOUNT || \
4272                 error "creating files in $dir"
4273
4274         # verify that files do not have EAs yet
4275         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4276         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4277
4278         sync
4279         sleep 1
4280         df $dir  #make sure we get new statfs data
4281         local MDSFREE=$(do_facet $mymds \
4282                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4283         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4284         echo "opening files to create objects/EAs"
4285         local FILE
4286         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4287                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4288         done
4289
4290         # verify that files have EAs now
4291         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4292         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4293
4294         sleep 1  #make sure we get new statfs data
4295         df $dir
4296         local MDSFREE2=$(do_facet $mymds \
4297                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4298         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4299         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4300                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4301                         error "MDC before $MDCFREE != after $MDCFREE2"
4302                 else
4303                         echo "MDC before $MDCFREE != after $MDCFREE2"
4304                         echo "unable to confirm if MDS has large inodes"
4305                 fi
4306         fi
4307         rm -rf $dir
4308 }
4309 run_test 57b "default LOV EAs are stored inside large inodes ==="
4310
4311 test_58() {
4312     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
4313     wiretest
4314 }
4315 run_test 58 "verify cross-platform wire constants =============="
4316
4317 test_59() {
4318         echo "touch 130 files"
4319         createmany -o $DIR/f59- 130
4320         echo "rm 130 files"
4321         unlinkmany $DIR/f59- 130
4322         sync
4323         # wait for commitment of removal
4324         wait_delete_completed
4325 }
4326 run_test 59 "verify cancellation of llog records async ========="
4327
4328 TEST60_HEAD="test_60 run $RANDOM"
4329 test_60a() {
4330         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4331         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4332         log "$TEST60_HEAD - from kernel mode"
4333         do_facet mgs sh run-llog.sh
4334 }
4335 run_test 60a "llog sanity tests run from kernel module =========="
4336
4337 test_60b() { # bug 6411
4338         dmesg > $DIR/$tfile
4339         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4340                                  /llog.test/ {
4341                                          if (marker)
4342                                                  from_marker++
4343                                          from_begin++
4344                                  }
4345                                  END {
4346                                          if (marker)
4347                                                  print from_marker
4348                                          else
4349                                                  print from_begin
4350                                  }"`
4351         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4352 }
4353 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4354
4355 test_60c() {
4356         echo "create 5000 files"
4357         createmany -o $DIR/f60c- 5000
4358 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4359         lctl set_param fail_loc=0x80000137
4360         unlinkmany $DIR/f60c- 5000
4361         lctl set_param fail_loc=0
4362 }
4363 run_test 60c "unlink file when mds full"
4364
4365 test_60d() {
4366         SAVEPRINTK=$(lctl get_param -n printk)
4367
4368         # verify "lctl mark" is even working"
4369         MESSAGE="test message ID $RANDOM $$"
4370         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4371         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4372
4373         lctl set_param printk=0 || error "set lnet.printk failed"
4374         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4375         MESSAGE="new test message ID $RANDOM $$"
4376         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4377         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4378         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4379
4380         lctl set_param -n printk="$SAVEPRINTK"
4381 }
4382 run_test 60d "test printk console message masking"
4383
4384 test_61() {
4385         f="$DIR/f61"
4386         dd if=/dev/zero of=$f bs=`page_size` count=1
4387         cancel_lru_locks osc
4388         $MULTIOP $f OSMWUc || error
4389         sync
4390 }
4391 run_test 61 "mmap() writes don't make sync hang ================"
4392
4393 # bug 2330 - insufficient obd_match error checking causes LBUG
4394 test_62() {
4395         f="$DIR/f62"
4396         echo foo > $f
4397         cancel_lru_locks osc
4398         lctl set_param fail_loc=0x405
4399         cat $f && error "cat succeeded, expect -EIO"
4400         lctl set_param fail_loc=0
4401 }
4402 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4403 # match every page all of the time.
4404 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4405
4406 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4407 test_63a() {    # was test_63
4408         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4409         lctl set_param -n osc.*.max_dirty_mb 0
4410         for i in `seq 10` ; do
4411                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4412                 sleep 5
4413                 kill $!
4414                 sleep 1
4415         done
4416
4417         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4418         rm -f $DIR/f63 || true
4419 }
4420 run_test 63a "Verify oig_wait interruption does not crash ======="
4421
4422 # bug 2248 - async write errors didn't return to application on sync
4423 # bug 3677 - async write errors left page locked
4424 test_63b() {
4425         debugsave
4426         lctl set_param debug=-1
4427
4428         # ensure we have a grant to do async writes
4429         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4430         rm $DIR/$tfile
4431
4432         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4433         lctl set_param fail_loc=0x80000406
4434         $MULTIOP $DIR/$tfile Owy && \
4435                 error "sync didn't return ENOMEM"
4436         sync; sleep 2; sync     # do a real sync this time to flush page
4437         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4438                 error "locked page left in cache after async error" || true
4439         debugrestore
4440 }
4441 run_test 63b "async write errors should be returned to fsync ==="
4442
4443 test_64a () {
4444         df $DIR
4445         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4446 }
4447 run_test 64a "verify filter grant calculations (in kernel) ====="
4448
4449 test_64b () {
4450         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4451         sh oos.sh $MOUNT
4452 }
4453 run_test 64b "check out-of-space detection on client ==========="
4454
4455 test_64c() {
4456         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4457 }
4458 run_test 64c "verify grant shrink ========================------"
4459
4460 # bug 1414 - set/get directories' stripe info
4461 test_65a() {
4462         test_mkdir -p $DIR/$tdir
4463         touch $DIR/$tdir/f1
4464         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
4465 }
4466 run_test 65a "directory with no stripe info ===================="
4467
4468 test_65b() {
4469         test_mkdir -p $DIR/$tdir
4470         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4471                                                 error "setstripe"
4472         touch $DIR/$tdir/f2
4473         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
4474 }
4475 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4476
4477 test_65c() {
4478         if [ $OSTCOUNT -gt 1 ]; then
4479                 test_mkdir -p $DIR/$tdir
4480                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4481                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
4482                 touch $DIR/$tdir/f3
4483                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
4484         fi
4485 }
4486 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4487
4488 test_65d() {
4489         test_mkdir -p $DIR/$tdir
4490         if [ $STRIPECOUNT -le 0 ]; then
4491                 sc=1
4492         elif [ $STRIPECOUNT -gt 2000 ]; then
4493 #LOV_MAX_STRIPE_COUNT is 2000
4494                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4495         else
4496                 sc=$(($STRIPECOUNT - 1))
4497         fi
4498         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
4499         touch $DIR/$tdir/f4 $DIR/$tdir/f5
4500         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
4501                                                 error "lverify failed"
4502 }
4503 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4504
4505 test_65e() {
4506         test_mkdir -p $DIR/$tdir
4507
4508         $SETSTRIPE $DIR/$tdir || error "setstripe"
4509         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4510                                         error "no stripe info failed"
4511         touch $DIR/$tdir/f6
4512         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
4513 }
4514 run_test 65e "directory setstripe defaults ======================="
4515
4516 test_65f() {
4517         test_mkdir -p $DIR/${tdir}f
4518         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
4519 }
4520 run_test 65f "dir setstripe permission (should return error) ==="
4521
4522 test_65g() {
4523         test_mkdir -p $DIR/$tdir
4524         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4525                                                         error "setstripe"
4526         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
4527         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
4528                 error "delete default stripe failed"
4529 }
4530 run_test 65g "directory setstripe -d ==========================="
4531
4532 test_65h() {
4533         test_mkdir -p $DIR/$tdir
4534         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
4535                                                         error "setstripe"
4536         test_mkdir -p $DIR/$tdir/dd1
4537         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
4538                 error "stripe info inherit failed"
4539 }
4540 run_test 65h "directory stripe info inherit ===================="
4541
4542 test_65i() { # bug6367
4543         $SETSTRIPE -S 65536 -c -1 $MOUNT
4544 }
4545 run_test 65i "set non-default striping on root directory (bug 6367)="
4546
4547 test_65ia() { # bug12836
4548         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4549 }
4550 run_test 65ia "getstripe on -1 default directory striping"
4551
4552 test_65ib() { # bug12836
4553         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4554 }
4555 run_test 65ib "getstripe -v on -1 default directory striping"
4556
4557 test_65ic() { # bug12836
4558         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4559 }
4560 run_test 65ic "new find on -1 default directory striping"
4561
4562 test_65j() { # bug6367
4563         sync; sleep 1
4564         # if we aren't already remounting for each test, do so for this test
4565         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4566                 cleanup || error "failed to unmount"
4567                 setup
4568         fi
4569         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4570 }
4571 run_test 65j "set default striping on root directory (bug 6367)="
4572
4573 test_65k() { # bug11679
4574     [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4575     remote_mds_nodsh && skip "remote MDS with nodsh" && return
4576
4577     echo "Check OST status: "
4578     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4579               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4580
4581     for OSC in $MDS_OSCS; do
4582         echo $OSC "is activate"
4583         do_facet $SINGLEMDS lctl --device %$OSC activate
4584     done
4585
4586     mkdir -p $DIR/$tdir
4587     for INACTIVE_OSC in $MDS_OSCS; do
4588         echo "Deactivate: " $INACTIVE_OSC
4589         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4590         for STRIPE_OSC in $MDS_OSCS; do
4591             OST=`osc_to_ost $STRIPE_OSC`
4592             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4593                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4594
4595             [ -f $DIR/$tdir/$IDX ] && continue
4596             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4597             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4598             RC=$?
4599             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4600         done
4601         rm -f $DIR/$tdir/*
4602         echo $INACTIVE_OSC "is Activate."
4603         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4604     done
4605 }
4606 run_test 65k "validate manual striping works properly with deactivated OSCs"
4607
4608 test_65l() { # bug 12836
4609         test_mkdir -p $DIR/$tdir/test_dir
4610         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4611         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4612 }
4613 run_test 65l "lfs find on -1 stripe dir ========================"
4614
4615 # bug 2543 - update blocks count on client
4616 test_66() {
4617         COUNT=${COUNT:-8}
4618         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4619         sync; sync_all_data; sync; sync_all_data
4620         cancel_lru_locks osc
4621         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4622         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4623 }
4624 run_test 66 "update inode blocks count on client ==============="
4625
4626 LLOOP=
4627 LLITELOOPLOAD=
4628 cleanup_68() {
4629         trap 0
4630         if [ ! -z "$LLOOP" ]; then
4631                 if swapon -s | grep -q $LLOOP; then
4632                         swapoff $LLOOP || error "swapoff failed"
4633                 fi
4634
4635                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4636                 rm -f $LLOOP
4637                 unset LLOOP
4638         fi
4639         if [ ! -z "$LLITELOOPLOAD" ]; then
4640                 rmmod llite_lloop
4641                 unset LLITELOOPLOAD
4642         fi
4643         rm -f $DIR/f68*
4644 }
4645
4646 meminfo() {
4647         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4648 }
4649
4650 swap_used() {
4651         swapon -s | awk '($1 == "'$1'") { print $4 }'
4652 }
4653
4654 # test case for lloop driver, basic function
4655 test_68a() {
4656         [ "$UID" != 0 ] && skip_env "must run as root" && return
4657         llite_lloop_enabled || \
4658                 { skip_env "llite_lloop module disabled" && return; }
4659
4660         trap cleanup_68 EXIT
4661
4662         if ! module_loaded llite_lloop; then
4663                 if load_module llite/llite_lloop; then
4664                         LLITELOOPLOAD=yes
4665                 else
4666                         skip_env "can't find module llite_lloop"
4667                         return
4668                 fi
4669         fi
4670
4671         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4672         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4673         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4674
4675         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4676         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4677
4678         cleanup_68
4679 }
4680 run_test 68a "lloop driver - basic test ========================"
4681
4682 # excercise swapping to lustre by adding a high priority swapfile entry
4683 # and then consuming memory until it is used.
4684 test_68b() {  # was test_68
4685         [ "$UID" != 0 ] && skip_env "must run as root" && return
4686         lctl get_param -n devices | grep -q obdfilter && \
4687                 skip "local OST" && return
4688
4689         grep -q llite_lloop /proc/modules
4690         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4691
4692         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4693                 skip "can't reliably test swap with TCP" && return
4694
4695         MEMTOTAL=`meminfo MemTotal`
4696         NR_BLOCKS=$((MEMTOTAL>>8))
4697         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4698
4699         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4700         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4701         mkswap $DIR/f68b
4702
4703         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4704
4705         trap cleanup_68 EXIT
4706
4707         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4708
4709         echo "before: `swapon -s | grep $LLOOP`"
4710         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4711         echo "after: `swapon -s | grep $LLOOP`"
4712         SWAPUSED=`swap_used $LLOOP`
4713
4714         cleanup_68
4715
4716         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4717 }
4718 run_test 68b "support swapping to Lustre ========================"
4719
4720 # bug5265, obdfilter oa2dentry return -ENOENT
4721 # #define OBD_FAIL_OST_ENOENT 0x217
4722 test_69() {
4723         remote_ost_nodsh && skip "remote OST with nodsh" && return
4724
4725         f="$DIR/$tfile"
4726         $SETSTRIPE -c 1 -i 0 $f
4727
4728         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4729
4730         do_facet ost1 lctl set_param fail_loc=0x217
4731         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4732         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4733
4734         do_facet ost1 lctl set_param fail_loc=0
4735         $DIRECTIO write $f 0 2 || error "write error"
4736
4737         cancel_lru_locks osc
4738         $DIRECTIO read $f 0 1 || error "read error"
4739
4740         do_facet ost1 lctl set_param fail_loc=0x217
4741         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4742
4743         do_facet ost1 lctl set_param fail_loc=0
4744         rm -f $f
4745 }
4746 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4747
4748 test_71() {
4749     test_mkdir -p $DIR/$tdir
4750     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4751 }
4752 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4753
4754 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4755         check_kernel_version 43 || return 0
4756         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4757
4758         # Check that testing environment is properly set up. Skip if not
4759         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4760                 skip_env "User $RUNAS_ID does not exist - skipping"
4761                 return 0
4762         }
4763         # We had better clear the $DIR to get enough space for dd
4764         rm -rf $DIR/*
4765         touch $DIR/f72
4766         chmod 777 $DIR/f72
4767         chmod ug+s $DIR/f72
4768         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4769         # See if we are still setuid/sgid
4770         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4771         # Now test that MDS is updated too
4772         cancel_lru_locks mdc
4773         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4774         rm -f $DIR/f72
4775 }
4776 run_test 72a "Test that remove suid works properly (bug5695) ===="
4777
4778 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4779         local perm
4780
4781         [ "$RUNAS_ID" = "$UID" ] && \
4782                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4783         [ "$RUNAS_ID" -eq 0 ] && \
4784                 skip_env "RUNAS_ID = 0 -- skipping" && return
4785
4786         # Check that testing environment is properly set up. Skip if not
4787         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4788                 skip_env "User $RUNAS_ID does not exist - skipping"
4789                 return 0
4790         }
4791         touch $DIR/${tfile}-f{g,u}
4792         test_mkdir $DIR/${tfile}-dg
4793         test_mkdir $DIR/${tfile}-du
4794         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4795         chmod g+s $DIR/${tfile}-{f,d}g
4796         chmod u+s $DIR/${tfile}-{f,d}u
4797         for perm in 777 2777 4777; do
4798                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4799                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4800                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4801                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4802         done
4803         true
4804 }
4805 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4806
4807 # bug 3462 - multiple simultaneous MDC requests
4808 test_73() {
4809         test_mkdir $DIR/d73-1
4810         test_mkdir $DIR/d73-2
4811         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4812         pid1=$!
4813
4814         lctl set_param fail_loc=0x80000129
4815         $MULTIOP $DIR/d73-1/f73-2 Oc &
4816         sleep 1
4817         lctl set_param fail_loc=0
4818
4819         $MULTIOP $DIR/d73-2/f73-3 Oc &
4820         pid3=$!
4821
4822         kill -USR1 $pid1
4823         wait $pid1 || return 1
4824
4825         sleep 25
4826
4827         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4828         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4829         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4830
4831         rm -rf $DIR/d73-*
4832 }
4833 run_test 73 "multiple MDC requests (should not deadlock)"
4834
4835 test_74a() { # bug 6149, 6184
4836         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4837         #
4838         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4839         # will spin in a tight reconnection loop
4840         touch $DIR/f74a
4841         lctl set_param fail_loc=0x8000030e
4842         # get any lock that won't be difficult - lookup works.
4843         ls $DIR/f74a
4844         lctl set_param fail_loc=0
4845         true
4846         rm -f $DIR/f74a
4847 }
4848 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4849
4850 test_74b() { # bug 13310
4851         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4852         #
4853         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4854         # will spin in a tight reconnection loop
4855         lctl set_param fail_loc=0x8000030e
4856         # get a "difficult" lock
4857         touch $DIR/f74b
4858         lctl set_param fail_loc=0
4859         true
4860         rm -f $DIR/f74b
4861 }
4862 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4863
4864 test_74c() {
4865 #define OBD_FAIL_LDLM_NEW_LOCK
4866         lctl set_param fail_loc=0x80000319
4867         touch $DIR/$tfile && error "Touch successful"
4868         true
4869 }
4870 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4871
4872 num_inodes() {
4873         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4874 }
4875
4876 get_inode_slab_tunables() {
4877         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4878 }
4879
4880 set_inode_slab_tunables() {
4881         echo "lustre_inode_cache $1" > /proc/slabinfo
4882 }
4883
4884 test_76() { # Now for bug 20433, added originally in bug 1443
4885         local SLAB_SETTINGS=`get_inode_slab_tunables`
4886         local CPUS=`getconf _NPROCESSORS_ONLN`
4887         # we cannot set limit below 1 which means 1 inode in each
4888         # per-cpu cache is still allowed
4889         set_inode_slab_tunables "1 1 0"
4890         cancel_lru_locks osc
4891         BEFORE_INODES=`num_inodes`
4892         echo "before inodes: $BEFORE_INODES"
4893         local COUNT=1000
4894         [ "$SLOW" = "no" ] && COUNT=100
4895         for i in `seq $COUNT`; do
4896                 touch $DIR/$tfile
4897                 rm -f $DIR/$tfile
4898         done
4899         cancel_lru_locks osc
4900         AFTER_INODES=`num_inodes`
4901         echo "after inodes: $AFTER_INODES"
4902         local wait=0
4903         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
4904                 sleep 2
4905                 AFTER_INODES=`num_inodes`
4906                 wait=$((wait+2))
4907                 echo "wait $wait seconds inodes: $AFTER_INODES"
4908                 if [ $wait -gt 30 ]; then
4909                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
4910                 fi
4911         done
4912         set_inode_slab_tunables "$SLAB_SETTINGS"
4913 }
4914 run_test 76 "confirm clients recycle inodes properly ===="
4915
4916
4917 export ORIG_CSUM=""
4918 set_checksums()
4919 {
4920         # Note: in sptlrpc modes which enable its own bulk checksum, the
4921         # original crc32_le bulk checksum will be automatically disabled,
4922         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
4923         # will be checked by sptlrpc code against sptlrpc bulk checksum.
4924         # In this case set_checksums() will not be no-op, because sptlrpc
4925         # bulk checksum will be enabled all through the test.
4926
4927         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
4928         lctl set_param -n osc.*.checksums $1
4929         return 0
4930 }
4931
4932 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
4933                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
4934 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
4935 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
4936 set_checksum_type()
4937 {
4938         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
4939         log "set checksum type to $1"
4940         return 0
4941 }
4942 F77_TMP=$TMP/f77-temp
4943 F77SZ=8
4944 setup_f77() {
4945         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
4946                 error "error writing to $F77_TMP"
4947 }
4948
4949 test_77a() { # bug 10889
4950         $GSS && skip "could not run with gss" && return
4951         [ ! -f $F77_TMP ] && setup_f77
4952         set_checksums 1
4953         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
4954         set_checksums 0
4955         rm -f $DIR/$tfile
4956 }
4957 run_test 77a "normal checksum read/write operation ============="
4958
4959 test_77b() { # bug 10889
4960         $GSS && skip "could not run with gss" && return
4961         [ ! -f $F77_TMP ] && setup_f77
4962         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4963         lctl set_param fail_loc=0x80000409
4964         set_checksums 1
4965         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
4966                 error "dd error: $?"
4967         lctl set_param fail_loc=0
4968         set_checksums 0
4969 }
4970 run_test 77b "checksum error on client write ===================="
4971
4972 test_77c() { # bug 10889
4973         $GSS && skip "could not run with gss" && return
4974         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
4975         set_checksums 1
4976         for algo in $CKSUM_TYPES; do
4977                 cancel_lru_locks osc
4978                 set_checksum_type $algo
4979                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4980                 lctl set_param fail_loc=0x80000408
4981                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
4982                 lctl set_param fail_loc=0
4983         done
4984         set_checksums 0
4985         set_checksum_type $ORIG_CSUM_TYPE
4986         rm -f $DIR/f77b
4987 }
4988 run_test 77c "checksum error on client read ==================="
4989
4990 test_77d() { # bug 10889
4991         $GSS && skip "could not run with gss" && return
4992         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4993         lctl set_param fail_loc=0x80000409
4994         set_checksums 1
4995         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4996                 error "direct write: rc=$?"
4997         lctl set_param fail_loc=0
4998         set_checksums 0
4999 }
5000 run_test 77d "checksum error on OST direct write ==============="
5001
5002 test_77e() { # bug 10889
5003         $GSS && skip "could not run with gss" && return
5004         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
5005         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5006         lctl set_param fail_loc=0x80000408
5007         set_checksums 1
5008         cancel_lru_locks osc
5009         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
5010                 error "direct read: rc=$?"
5011         lctl set_param fail_loc=0
5012         set_checksums 0
5013 }
5014 run_test 77e "checksum error on OST direct read ================"
5015
5016 test_77f() { # bug 10889
5017         $GSS && skip "could not run with gss" && return
5018         set_checksums 1
5019         for algo in $CKSUM_TYPES; do
5020                 cancel_lru_locks osc
5021                 set_checksum_type $algo
5022                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5023                 lctl set_param fail_loc=0x409
5024                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
5025                         error "direct write succeeded"
5026                 lctl set_param fail_loc=0
5027         done
5028         set_checksum_type $ORIG_CSUM_TYPE
5029         set_checksums 0
5030 }
5031 run_test 77f "repeat checksum error on write (expect error) ===="
5032
5033 test_77g() { # bug 10889
5034         $GSS && skip "could not run with gss" && return
5035         remote_ost_nodsh && skip "remote OST with nodsh" && return
5036
5037         [ ! -f $F77_TMP ] && setup_f77
5038
5039         $SETSTRIPE -c 1 -i 0 $DIR/f77g
5040         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5041         do_facet ost1 lctl set_param fail_loc=0x8000021a
5042         set_checksums 1
5043         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
5044                 error "write error: rc=$?"
5045         do_facet ost1 lctl set_param fail_loc=0
5046         set_checksums 0
5047 }
5048 run_test 77g "checksum error on OST write ======================"
5049
5050 test_77h() { # bug 10889
5051         $GSS && skip "could not run with gss" && return
5052         remote_ost_nodsh && skip "remote OST with nodsh" && return
5053
5054         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
5055         cancel_lru_locks osc
5056         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5057         do_facet ost1 lctl set_param fail_loc=0x8000021b
5058         set_checksums 1
5059         cmp $F77_TMP $DIR/f77g || error "file compare failed"
5060         do_facet ost1 lctl set_param fail_loc=0
5061         set_checksums 0
5062 }
5063 run_test 77h "checksum error on OST read ======================="
5064
5065 test_77i() { # bug 13805
5066         $GSS && skip "could not run with gss" && return
5067         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5068         lctl set_param fail_loc=0x40b
5069         remount_client $MOUNT
5070         lctl set_param fail_loc=0
5071         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5072                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5073                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5074                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5075         done
5076         remount_client $MOUNT
5077 }
5078 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
5079
5080 test_77j() { # bug 13805
5081         $GSS && skip "could not run with gss" && return
5082         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5083         lctl set_param fail_loc=0x40c
5084         remount_client $MOUNT
5085         lctl set_param fail_loc=0
5086         sleep 2 # wait async osc connect to finish
5087         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5088                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5089                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5090                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5091         done
5092         remount_client $MOUNT
5093 }
5094 run_test 77j "client only supporting ADLER32 ===================="
5095
5096 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5097 rm -f $F77_TMP
5098 unset F77_TMP
5099
5100 test_78() { # bug 10901
5101         remote_ost || { skip_env "local OST" && return; }
5102
5103         NSEQ=5
5104         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5105         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5106         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5107         echo "MemTotal: $MEMTOTAL"
5108 # reserve 256MB of memory for the kernel and other running processes,
5109 # and then take 1/2 of the remaining memory for the read/write buffers.
5110     if [ $MEMTOTAL -gt 512 ] ;then
5111         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5112     else
5113         # for those poor memory-starved high-end clusters...
5114         MEMTOTAL=$((MEMTOTAL / 2))
5115     fi
5116         echo "Mem to use for directio: $MEMTOTAL"
5117         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
5118         [ $F78SIZE -gt 512 ] && F78SIZE=512
5119         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
5120         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
5121         echo "Smallest OST: $SMALLESTOST"
5122         [ $SMALLESTOST -lt 10240 ] && \
5123                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5124
5125         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
5126                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5127
5128         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5129         echo "File size: $F78SIZE"
5130         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5131         for i in `seq 1 $NSEQ`
5132         do
5133                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5134                 echo directIO rdwr round $i of $NSEQ
5135                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5136         done
5137
5138         rm -f $DIR/$tfile
5139 }
5140 run_test 78 "handle large O_DIRECT writes correctly ============"
5141
5142 test_79() { # bug 12743
5143         wait_delete_completed
5144
5145         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5146         BKFREE=$(calc_osc_kbytes kbytesfree)
5147         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5148
5149         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5150         DFTOTAL=`echo $STRING | cut -d, -f1`
5151         DFUSED=`echo $STRING  | cut -d, -f2`
5152         DFAVAIL=`echo $STRING | cut -d, -f3`
5153         DFFREE=$(($DFTOTAL - $DFUSED))
5154
5155         ALLOWANCE=$((64 * $OSTCOUNT))
5156
5157         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5158            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5159                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5160         fi
5161         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5162            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5163                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5164         fi
5165         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5166            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5167                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5168         fi
5169 }
5170 run_test 79 "df report consistency check ======================="
5171
5172 test_80() { # bug 10718
5173         # relax strong synchronous semantics for slow backends like ZFS
5174         local soc="obdfilter.*.sync_on_lock_cancel"
5175         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5176         local hosts=
5177         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5178                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5179                           facet_active_host $host; done | sort -u)
5180                 do_nodes $hosts lctl set_param $soc=never
5181         fi
5182
5183         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5184         sync; sleep 1; sync
5185         local BEFORE=`date +%s`
5186         cancel_lru_locks osc
5187         local AFTER=`date +%s`
5188         local DIFF=$((AFTER-BEFORE))
5189         if [ $DIFF -gt 1 ] ; then
5190                 error "elapsed for 1M@1T = $DIFF"
5191         fi
5192
5193         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5194
5195         rm -f $DIR/$tfile
5196 }
5197 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5198
5199 test_81a() { # LU-456
5200         remote_ost_nodsh && skip "remote OST with nodsh" && return
5201         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5202         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5203         do_facet ost1 lctl set_param fail_loc=0x80000228
5204
5205         # write should trigger a retry and success
5206         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5207         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5208         RC=$?
5209         if [ $RC -ne 0 ] ; then
5210                 error "write should success, but failed for $RC"
5211         fi
5212 }
5213 run_test 81a "OST should retry write when get -ENOSPC ==============="
5214
5215 test_81b() { # LU-456
5216         remote_ost_nodsh && skip "remote OST with nodsh" && return
5217         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5218         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5219         do_facet ost1 lctl set_param fail_loc=0x228
5220
5221         # write should retry several times and return -ENOSPC finally
5222         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5223         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5224         RC=$?
5225         ENOSPC=28
5226         if [ $RC -ne $ENOSPC ] ; then
5227                 error "dd should fail for -ENOSPC, but succeed."
5228         fi
5229 }
5230 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5231
5232 test_82() { # LU-1031
5233         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5234         local gid1=14091995
5235         local gid2=16022000
5236
5237         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5238         local MULTIPID1=$!
5239         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5240         local MULTIPID2=$!
5241         kill -USR1 $MULTIPID2
5242         sleep 2
5243         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5244                 error "First grouplock does not block second one"
5245         else
5246                 echo "Second grouplock blocks first one"
5247         fi
5248         kill -USR1 $MULTIPID1
5249         wait $MULTIPID1
5250         wait $MULTIPID2
5251 }
5252 run_test 82 "Basic grouplock test ==============================="
5253
5254 test_99a() {
5255         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5256             return
5257         test_mkdir -p $DIR/d99cvsroot
5258         chown $RUNAS_ID $DIR/d99cvsroot
5259         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5260         cd $TMP
5261
5262         $RUNAS cvs -d $DIR/d99cvsroot init || error
5263         cd $oldPWD
5264 }
5265 run_test 99a "cvs init ========================================="
5266
5267 test_99b() {
5268         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5269         [ ! -d $DIR/d99cvsroot ] && test_99a
5270         cd /etc/init.d
5271         # some versions of cvs import exit(1) when asked to import links or
5272         # files they can't read.  ignore those files.
5273         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5274                         ! -perm +4 -printf '-I %f\n')
5275         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5276                 d99reposname vtag rtag
5277 }
5278 run_test 99b "cvs import ======================================="
5279
5280 test_99c() {
5281         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5282         [ ! -d $DIR/d99cvsroot ] && test_99b
5283         cd $DIR
5284         test_mkdir -p $DIR/d99reposname
5285         chown $RUNAS_ID $DIR/d99reposname
5286         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5287 }
5288 run_test 99c "cvs checkout ====================================="
5289
5290 test_99d() {
5291         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5292         [ ! -d $DIR/d99cvsroot ] && test_99c
5293         cd $DIR/d99reposname
5294         $RUNAS touch foo99
5295         $RUNAS cvs add -m 'addmsg' foo99
5296 }
5297 run_test 99d "cvs add =========================================="
5298
5299 test_99e() {
5300         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5301         [ ! -d $DIR/d99cvsroot ] && test_99c
5302         cd $DIR/d99reposname
5303         $RUNAS cvs update
5304 }
5305 run_test 99e "cvs update ======================================="
5306
5307 test_99f() {
5308         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5309         [ ! -d $DIR/d99cvsroot ] && test_99d
5310         cd $DIR/d99reposname
5311         $RUNAS cvs commit -m 'nomsg' foo99
5312     rm -fr $DIR/d99cvsroot
5313 }
5314 run_test 99f "cvs commit ======================================="
5315
5316 test_100() {
5317         [ "$NETTYPE" = tcp ] || \
5318                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5319                         return ; }
5320
5321         remote_ost_nodsh && skip "remote OST with nodsh" && return
5322         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5323         remote_servers || \
5324                 { skip "useless for local single node setup" && return; }
5325
5326         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5327                 [ "$PROT" != "tcp" ] && continue
5328                 RPORT=$(echo $REMOTE | cut -d: -f2)
5329                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5330
5331                 rc=0
5332                 LPORT=`echo $LOCAL | cut -d: -f2`
5333                 if [ $LPORT -ge 1024 ]; then
5334                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5335                         netstat -tna
5336                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5337                 fi
5338         done
5339         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5340 }
5341 run_test 100 "check local port using privileged port ==========="
5342
5343 function get_named_value()
5344 {
5345     local tag
5346
5347     tag=$1
5348     while read ;do
5349         line=$REPLY
5350         case $line in
5351         $tag*)
5352             echo $line | sed "s/^$tag[ ]*//"
5353             break
5354             ;;
5355         esac
5356     done
5357 }
5358
5359 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
5360 cleanup_101a() {
5361         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5362         trap 0
5363 }
5364
5365 test_101a() {
5366         local s
5367         local discard
5368         local nreads=10000
5369         [ "$CPU" = "UML" ] && nreads=1000
5370         local cache_limit=32
5371
5372         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5373         trap cleanup_101a EXIT
5374         $LCTL set_param -n llite.*.read_ahead_stats 0
5375         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5376
5377         #
5378         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5379         #
5380         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5381         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5382
5383         discard=0
5384         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5385                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5386                         discard=$(($discard + $s))
5387         done
5388         cleanup_101a
5389
5390         if [ $(($discard * 10)) -gt $nreads ] ;then
5391                 $LCTL get_param osc.*-osc*.rpc_stats
5392                 $LCTL get_param llite.*.read_ahead_stats
5393                 error "too many ($discard) discarded pages"
5394         fi
5395         rm -f $DIR/$tfile || true
5396 }
5397 run_test 101a "check read-ahead for random reads ================"
5398
5399 setup_test101bc() {
5400         test_mkdir -p $DIR/$tdir
5401         STRIPE_SIZE=1048576
5402         STRIPE_COUNT=$OSTCOUNT
5403         STRIPE_OFFSET=0
5404
5405         local list=$(comma_list $(osts_nodes))
5406         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=0
5407         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=0
5408
5409         trap cleanup_test101bc EXIT
5410         # prepare the read-ahead file
5411         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5412
5413         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5414 }
5415
5416 cleanup_test101bc() {
5417         trap 0
5418         rm -rf $DIR/$tdir
5419         rm -f $DIR/$tfile
5420
5421         local list=$(comma_list $(osts_nodes))
5422         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
5423         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=1
5424 }
5425
5426 calc_total() {
5427         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5428 }
5429
5430 ra_check_101() {
5431         local READ_SIZE=$1
5432         local STRIPE_SIZE=1048576
5433         local RA_INC=1048576
5434         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5435         local FILE_LENGTH=$((64*100))
5436         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5437                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5438         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5439                         get_named_value 'read but discarded' | \
5440                         cut -d" " -f1 | calc_total`
5441         if [ $DISCARD -gt $discard_limit ]; then
5442                 $LCTL get_param llite.*.read_ahead_stats
5443                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5444         else
5445                 echo "Read-ahead success for size ${READ_SIZE}"
5446         fi
5447 }
5448
5449 test_101b() {
5450         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5451         local STRIPE_SIZE=1048576
5452         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5453         local FILE_LENGTH=$((STRIPE_SIZE*100))
5454         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5455         # prepare the read-ahead file
5456         setup_test101bc
5457         cancel_lru_locks osc
5458         for BIDX in 2 4 8 16 32 64 128 256
5459         do
5460                 local BSIZE=$((BIDX*4096))
5461                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5462                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5463                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5464                 $LCTL set_param -n llite.*.read_ahead_stats 0
5465                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5466                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5467                 cancel_lru_locks osc
5468                 ra_check_101 $BSIZE
5469         done
5470         cleanup_test101bc
5471         true
5472 }
5473 run_test 101b "check stride-io mode read-ahead ================="
5474
5475 test_101c() {
5476     local STRIPE_SIZE=1048576
5477     local FILE_LENGTH=$((STRIPE_SIZE*100))
5478     local nreads=10000
5479     local osc
5480
5481     setup_test101bc
5482
5483     cancel_lru_locks osc
5484     $LCTL set_param osc.*.rpc_stats 0
5485     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5486     for osc in $($LCTL get_param -N osc.*); do
5487         if [ "$osc" == "osc.num_refs" ]; then
5488             continue
5489         fi
5490
5491         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5492         if [ $lines -le 20 ]; then
5493             continue
5494         fi
5495
5496         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5497                                      awk '$1 == "1:" { print $2; exit; }')
5498         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5499                                      awk '$1 == "2:" { print $2; exit; }')
5500         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5501                                      awk '$1 == "4:" { print $2; exit; }')
5502         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5503                                      awk '$1 == "8:" { print $2; exit; }')
5504
5505         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5506         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5507         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5508         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5509         echo "${osc} rpc check passed!"
5510     done
5511     cleanup_test101bc
5512     true
5513 }
5514 run_test 101c "check stripe_size aligned read-ahead ================="
5515
5516 set_read_ahead() {
5517    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5518    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5519 }
5520
5521 test_101d() {
5522     local file=$DIR/$tfile
5523     local size=${FILESIZE_101c:-500}
5524     local ra_MB=${READAHEAD_MB:-40}
5525
5526         local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5527         [ $space -gt $((size * 1024)) ] ||
5528                 { skip "Need free space ${size}M, have $space" && return; }
5529
5530     echo Creating ${size}M test file $file
5531     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5532     echo Cancel LRU locks on lustre client to flush the client cache
5533     cancel_lru_locks osc
5534
5535     echo Disable read-ahead
5536     local old_READAHEAD=$(set_read_ahead 0)
5537
5538     echo Reading the test file $file with read-ahead disabled
5539     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5540
5541     echo Cancel LRU locks on lustre client to flush the client cache
5542     cancel_lru_locks osc
5543     echo Enable read-ahead with ${ra_MB}MB
5544     set_read_ahead $ra_MB
5545
5546     echo Reading the test file $file with read-ahead enabled
5547     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5548
5549     echo read-ahead disabled time read $time_ra_OFF
5550     echo read-ahead enabled  time read $time_ra_ON
5551
5552         set_read_ahead $old_READAHEAD
5553         rm -f $file
5554         wait_delete_completed
5555
5556     [ $time_ra_ON -lt $time_ra_OFF ] ||
5557         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5558                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5559 }
5560 run_test 101d "file read with and without read-ahead enabled  ================="
5561
5562 test_101e() {
5563     local file=$DIR/$tfile
5564     local size=500  #KB
5565     local count=100
5566     local blksize=1024
5567
5568     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5569     local need_space=$((count * size))
5570     [ $space -gt $need_space ] ||
5571         { skip_env "Need free space $need_space, have $space" && return; }
5572
5573     echo Creating $count ${size}K test files
5574     for ((i = 0; i < $count; i++)); do
5575         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5576     done
5577
5578     echo Cancel LRU locks on lustre client to flush the client cache
5579     cancel_lru_locks osc
5580
5581     echo Reset readahead stats
5582     $LCTL set_param -n llite.*.read_ahead_stats 0
5583
5584     for ((i = 0; i < $count; i++)); do
5585         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5586     done
5587
5588     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5589           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5590
5591     for ((i = 0; i < $count; i++)); do
5592         rm -rf ${file}_${i} 2>/dev/null
5593     done
5594
5595     #10000 means 20% reads are missing in readahead
5596     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5597 }
5598 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5599
5600 cleanup_test101f() {
5601     trap 0
5602     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5603     rm -rf $DIR/$tfile 2>/dev/null
5604 }
5605
5606 test_101f() {
5607     local file=$DIR/$tfile
5608     local nreads=1000
5609
5610     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5611     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5612     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5613     trap cleanup_test101f EXIT
5614
5615     echo Cancel LRU locks on lustre client to flush the client cache
5616     cancel_lru_locks osc
5617
5618     echo Reset readahead stats
5619     $LCTL set_param -n llite.*.read_ahead_stats 0
5620     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5621     # readahead should read in 2M file on second read, so only miss
5622     # 2 pages.
5623     echo Random 4K reads on 2M file for 1000 times
5624     $READS -f $file -s 2097152 -b 4096 -n $nreads
5625
5626     echo checking missing pages
5627     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5628           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5629
5630     [ $miss -lt 3 ] || error "misses too much pages!"
5631     cleanup_test101f
5632 }
5633 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5634
5635 setup_test102() {
5636         test_mkdir -p $DIR/$tdir
5637         chown $RUNAS_ID $DIR/$tdir
5638         STRIPE_SIZE=65536
5639         STRIPE_OFFSET=1
5640         STRIPE_COUNT=$OSTCOUNT
5641         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5642
5643         trap cleanup_test102 EXIT
5644         cd $DIR
5645         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5646         cd $DIR/$tdir
5647         for num in 1 2 3 4; do
5648                 for count in $(seq 1 $STRIPE_COUNT); do
5649                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5650                                 local size=`expr $STRIPE_SIZE \* $num`
5651                                 local file=file"$num-$idx-$count"
5652                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5653                         done
5654                 done
5655         done
5656
5657         cd $DIR
5658         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5659 }
5660
5661 cleanup_test102() {
5662         trap 0
5663         rm -f $TMP/f102.tar
5664         rm -rf $DIR/d0.sanity/d102
5665 }
5666
5667 test_102a() {
5668         local testfile=$DIR/xattr_testfile
5669
5670         touch $testfile
5671
5672         [ "$UID" != 0 ] && skip_env "must run as root" && return
5673         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5674                 skip_env "must have user_xattr" && return
5675
5676         [ -z "$(which setfattr 2>/dev/null)" ] &&
5677                 skip_env "could not find setfattr" && return
5678
5679         echo "set/get xattr..."
5680         setfattr -n trusted.name1 -v value1 $testfile || error
5681         getfattr -n trusted.name1 $testfile 2> /dev/null |
5682           grep "trusted.name1=.value1" ||
5683                 error "$testfile missing trusted.name1=value1"
5684
5685         setfattr -n user.author1 -v author1 $testfile || error
5686         getfattr -n user.author1 $testfile 2> /dev/null |
5687           grep "user.author1=.author1" ||
5688                 error "$testfile missing trusted.author1=author1"
5689
5690         echo "listxattr..."
5691         setfattr -n trusted.name2 -v value2 $testfile ||
5692                 error "$testfile unable to set trusted.name2"
5693         setfattr -n trusted.name3 -v value3 $testfile ||
5694                 error "$testfile unable to set trusted.name3"
5695         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5696             grep "trusted.name" | wc -l) -eq 3 ] ||
5697                 error "$testfile missing 3 trusted.name xattrs"
5698
5699         setfattr -n user.author2 -v author2 $testfile ||
5700                 error "$testfile unable to set user.author2"
5701         setfattr -n user.author3 -v author3 $testfile ||
5702                 error "$testfile unable to set user.author3"
5703         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5704             grep "user.author" | wc -l) -eq 3 ] ||
5705                 error "$testfile missing 3 user.author xattrs"
5706
5707         echo "remove xattr..."
5708         setfattr -x trusted.name1 $testfile ||
5709                 error "$testfile error deleting trusted.name1"
5710         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5711                 error "$testfile did not delete trusted.name1 xattr"
5712
5713         setfattr -x user.author1 $testfile ||
5714                 error "$testfile error deleting user.author1"
5715         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5716                 error "$testfile did not delete trusted.name1 xattr"
5717
5718         # b10667: setting lustre special xattr be silently discarded
5719         echo "set lustre special xattr ..."
5720         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5721                 error "$testfile allowed setting trusted.lov"
5722 }
5723 run_test 102a "user xattr test =================================="
5724
5725 test_102b() {
5726         # b10930: get/set/list trusted.lov xattr
5727         echo "get/set/list trusted.lov xattr ..."
5728         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5729         local testfile=$DIR/$tfile
5730         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5731                 error "setstripe failed"
5732         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5733                 error "getstripe failed"
5734         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5735         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5736
5737         local testfile2=${testfile}2
5738         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5739                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5740
5741         $MCREATE $testfile2
5742         setfattr -n trusted.lov -v $value $testfile2
5743         local stripe_size=$($GETSTRIPE -S $testfile2)
5744         local stripe_count=$($GETSTRIPE -c $testfile2)
5745         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5746         [ $stripe_count -eq $STRIPECOUNT ] ||
5747                 error "stripe count $stripe_count != $STRIPECOUNT"
5748         rm -f $DIR/$tfile
5749 }
5750 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5751
5752 test_102c() {
5753         # b10930: get/set/list lustre.lov xattr
5754         echo "get/set/list lustre.lov xattr ..."
5755         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5756         test_mkdir -p $DIR/$tdir
5757         chown $RUNAS_ID $DIR/$tdir
5758         local testfile=$DIR/$tdir/$tfile
5759         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5760                 error "setstripe failed"
5761         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5762                 error "getstripe failed"
5763         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5764         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5765
5766         local testfile2=${testfile}2
5767         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5768                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5769
5770         $RUNAS $MCREATE $testfile2
5771         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5772         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5773         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5774         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5775         [ $stripe_count -eq $STRIPECOUNT ] ||
5776                 error "stripe count $stripe_count != $STRIPECOUNT"
5777 }
5778 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5779
5780 compare_stripe_info1() {
5781         local stripe_index_all_zero=true
5782
5783         for num in 1 2 3 4; do
5784                 for count in $(seq 1 $STRIPE_COUNT); do
5785                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5786                                 local size=$((STRIPE_SIZE * num))
5787                                 local file=file"$num-$offset-$count"
5788                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5789                                 [ $stripe_size -ne $size ] &&
5790                                     error "$file: size $stripe_size != $size"
5791                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5792                                 # allow fewer stripes to be created, ORI-601
5793                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5794                                     error "$file: count $stripe_count != $count"
5795                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5796                                 [ $stripe_index -ne 0 ] &&
5797                                         stripe_index_all_zero=false
5798                         done
5799                 done
5800         done
5801         $stripe_index_all_zero &&
5802                 error "all files are being extracted starting from OST index 0"
5803         return 0
5804 }
5805
5806 find_lustre_tar() {
5807         [ -n "$(which tar 2>/dev/null)" ] &&
5808                 strings $(which tar) | grep -q "lustre" && echo tar
5809 }
5810
5811 test_102d() {
5812         # b10930: tar test for trusted.lov xattr
5813         TAR=$(find_lustre_tar)
5814         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5815         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5816         setup_test102
5817         test_mkdir -p $DIR/d102d
5818         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5819         cd $DIR/d102d/$tdir
5820         compare_stripe_info1
5821 }
5822 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5823
5824 test_102f() {
5825         # b10930: tar test for trusted.lov xattr
5826         TAR=$(find_lustre_tar)
5827         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5828         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5829         setup_test102
5830         test_mkdir -p $DIR/d102f
5831         cd $DIR
5832         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5833         cd $DIR/d102f/$tdir
5834         compare_stripe_info1
5835 }
5836 run_test 102f "tar copy files, not keep osts ==========="
5837
5838 grow_xattr() {
5839         local xsize=${1:-1024}  # in bytes
5840         local file=$DIR/$tfile
5841
5842         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5843                 skip "must have user_xattr" && return 0
5844         [ -z "$(which setfattr 2>/dev/null)" ] &&
5845                 skip_env "could not find setfattr" && return 0
5846         [ -z "$(which getfattr 2>/dev/null)" ] &&
5847                 skip_env "could not find getfattr" && return 0
5848
5849         touch $file
5850
5851         local value="$(generate_string $xsize)"
5852
5853         local xbig=trusted.big
5854         log "save $xbig on $file"
5855         setfattr -n $xbig -v $value $file ||
5856                 error "saving $xbig on $file failed"
5857
5858         local orig=$(get_xattr_value $xbig $file)
5859         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
5860
5861         local xsml=trusted.sml
5862         log "save $xsml on $file"
5863         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
5864
5865         local new=$(get_xattr_value $xbig $file)
5866         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
5867
5868         log "grow $xsml on $file"
5869         setfattr -n $xsml -v "$value" $file ||
5870                 error "growing $xsml on $file failed"
5871
5872         new=$(get_xattr_value $xbig $file)
5873         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
5874         log "$xbig still valid after growing $xsml"
5875
5876         rm -f $file
5877 }
5878
5879 test_102h() { # bug 15777
5880         grow_xattr 1024
5881 }
5882 run_test 102h "grow xattr from inside inode to external block"
5883
5884 test_102ha() {
5885         large_xattr_enabled || { skip "large_xattr disabled" && return; }
5886         grow_xattr $(max_xattr_size)
5887 }
5888 run_test 102ha "grow xattr from inside inode to external inode"
5889
5890 test_102i() { # bug 17038
5891         touch $DIR/$tfile
5892         ln -s $DIR/$tfile $DIR/${tfile}link
5893         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
5894         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"
5895         rm -f $DIR/$tfile $DIR/${tfile}link
5896 }
5897 run_test 102i "lgetxattr test on symbolic link ============"
5898
5899 test_102j() {
5900         TAR=$(find_lustre_tar)
5901         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5902         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5903         setup_test102 "$RUNAS"
5904         test_mkdir -p $DIR/d102j
5905         chown $RUNAS_ID $DIR/d102j
5906         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
5907         cd $DIR/d102j/$tdir
5908         compare_stripe_info1 "$RUNAS"
5909 }
5910 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
5911
5912 test_102k() {
5913         touch $DIR/$tfile
5914         # b22187 just check that does not crash for regular file.
5915         setfattr -n trusted.lov $DIR/$tfile
5916         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
5917         local test_kdir=$DIR/d102k
5918         test_mkdir $test_kdir
5919         local default_size=`$GETSTRIPE -S $test_kdir`
5920         local default_count=`$GETSTRIPE -c $test_kdir`
5921         local default_offset=`$GETSTRIPE -i $test_kdir`
5922         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
5923                 error 'dir setstripe failed'
5924         setfattr -n trusted.lov $test_kdir
5925         local stripe_size=`$GETSTRIPE -S $test_kdir`
5926         local stripe_count=`$GETSTRIPE -c $test_kdir`
5927         local stripe_offset=`$GETSTRIPE -i $test_kdir`
5928         [ $stripe_size -eq $default_size ] ||
5929                 error "stripe size $stripe_size != $default_size"
5930         [ $stripe_count -eq $default_count ] ||
5931                 error "stripe count $stripe_count != $default_count"
5932         [ $stripe_offset -eq $default_offset ] ||
5933                 error "stripe offset $stripe_offset != $default_offset"
5934         rm -rf $DIR/$tfile $test_kdir
5935 }
5936 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
5937
5938 test_102l() {
5939         # LU-532 trusted. xattr is invisible to non-root
5940         local testfile=$DIR/$tfile
5941
5942         touch $testfile
5943
5944         echo "listxattr as user..."
5945         chown $RUNAS_ID $testfile
5946         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
5947             grep -q "trusted" &&
5948                 error "$testfile trusted xattrs are user visible"
5949
5950         return 0;
5951 }
5952 run_test 102l "listxattr filter test =================================="
5953
5954 cleanup_test102
5955
5956 run_acl_subtest()
5957 {
5958     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
5959     return $?
5960 }
5961
5962 test_103 () {
5963     [ "$UID" != 0 ] && skip_env "must run as root" && return
5964     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5965     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
5966     $GSS && skip "could not run under gss" && return
5967
5968     declare -a identity_old
5969
5970         for num in $(seq $MDSCOUNT); do
5971                 switch_identity $num true || identity_old[$num]=$?
5972         done
5973
5974     SAVE_UMASK=`umask`
5975     umask 0022
5976     cd $DIR
5977
5978     echo "performing cp ..."
5979     run_acl_subtest cp || error
5980     echo "performing getfacl-noacl..."
5981     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
5982     echo "performing misc..."
5983     run_acl_subtest misc || error  "misc test failed"
5984     echo "performing permissions..."
5985     run_acl_subtest permissions || error "permissions failed"
5986     echo "performing setfacl..."
5987     run_acl_subtest setfacl || error  "setfacl test failed"
5988
5989     # inheritance test got from HP
5990     echo "performing inheritance..."
5991     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
5992     chmod +x make-tree || error "chmod +x failed"
5993     run_acl_subtest inheritance || error "inheritance test failed"
5994     rm -f make-tree
5995
5996     echo "LU-974 ignore umask when acl is enabled..."
5997     mkdir $DIR/974
5998     cd $DIR/974
5999     run_acl_subtest 974 || error "LU-974 test failed"
6000     rm -rf $DIR/974
6001
6002     cd $SAVE_PWD
6003     umask $SAVE_UMASK
6004
6005         for num in $(seq $MDSCOUNT); do
6006                 if [ "${identity_old[$num]}" = 1 ]; then
6007                         switch_identity $num false || identity_old[$num]=$?
6008                 fi
6009         done
6010 }
6011 run_test 103 "acl test ========================================="
6012
6013 test_104a() {
6014         touch $DIR/$tfile
6015         lfs df || error "lfs df failed"
6016         lfs df -ih || error "lfs df -ih failed"
6017         lfs df -h $DIR || error "lfs df -h $DIR failed"
6018         lfs df -i $DIR || error "lfs df -i $DIR failed"
6019         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6020         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6021
6022         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
6023         lctl --device %$OSC deactivate
6024         lfs df || error "lfs df with deactivated OSC failed"
6025         lctl --device %$OSC activate
6026         # wait the osc back to normal
6027         wait_osc_import_state client ost FULL
6028
6029         lfs df || error "lfs df with reactivated OSC failed"
6030         rm -f $DIR/$tfile
6031 }
6032 run_test 104a "lfs df [-ih] [path] test ========================="
6033
6034 test_104b() {
6035         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6036         chmod 666 /dev/obd
6037         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
6038         if [ $denied_cnt -ne 0 ];
6039         then
6040                     error "lfs check servers test failed"
6041         fi
6042 }
6043 run_test 104b "$RUNAS lfs check servers test ===================="
6044
6045 test_105a() {
6046         # doesn't work on 2.4 kernels
6047         touch $DIR/$tfile
6048         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
6049         then
6050                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6051         else
6052                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6053         fi
6054         rm -f $DIR/$tfile
6055 }
6056 run_test 105a "flock when mounted without -o flock test ========"
6057
6058 test_105b() {
6059         touch $DIR/$tfile
6060         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
6061         then
6062                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6063         else
6064                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6065         fi
6066         rm -f $DIR/$tfile
6067 }
6068 run_test 105b "fcntl when mounted without -o flock test ========"
6069
6070 test_105c() {
6071         touch $DIR/$tfile
6072         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
6073         then
6074                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6075         else
6076                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6077         fi
6078         rm -f $DIR/$tfile
6079 }
6080 run_test 105c "lockf when mounted without -o flock test ========"
6081
6082 test_105d() { # bug 15924
6083         test_mkdir -p $DIR/$tdir
6084         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
6085                 skip "mount w/o flock enabled" && return
6086         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6087         $LCTL set_param fail_loc=0x80000315
6088         flocks_test 2 $DIR/$tdir
6089 }
6090 run_test 105d "flock race (should not freeze) ========"
6091
6092 test_105e() { # bug 22660 && 22040
6093         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
6094                 skip "mount w/o flock enabled" && return
6095         touch $DIR/$tfile
6096         flocks_test 3 $DIR/$tfile
6097 }
6098 run_test 105e "Two conflicting flocks from same process ======="
6099
6100 test_106() { #bug 10921
6101         test_mkdir -p $DIR/$tdir
6102         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6103         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6104 }
6105 run_test 106 "attempt exec of dir followed by chown of that dir"
6106
6107 test_107() {
6108         CDIR=`pwd`
6109         cd $DIR
6110
6111         local file=core
6112         rm -f $file
6113
6114         local save_pattern=$(sysctl -n kernel.core_pattern)
6115         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6116         sysctl -w kernel.core_pattern=$file
6117         sysctl -w kernel.core_uses_pid=0
6118
6119         ulimit -c unlimited
6120         sleep 60 &
6121         SLEEPPID=$!
6122
6123         sleep 1
6124
6125         kill -s 11 $SLEEPPID
6126         wait $SLEEPPID
6127         if [ -e $file ]; then
6128                 size=`stat -c%s $file`
6129                 [ $size -eq 0 ] && error "Fail to create core file $file"
6130         else
6131                 error "Fail to create core file $file"
6132         fi
6133         rm -f $file
6134         sysctl -w kernel.core_pattern=$save_pattern
6135         sysctl -w kernel.core_uses_pid=$save_uses_pid
6136         cd $CDIR
6137 }
6138 run_test 107 "Coredump on SIG"
6139
6140 test_110() {
6141         test_mkdir -p $DIR/$tdir
6142         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6143                 error "mkdir with 255 char failed"
6144         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6145                 error "mkdir with 256 char should fail, but did not"
6146         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6147                 error "create with 255 char failed"
6148         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6149                 error "create with 256 char should fail, but did not"
6150
6151         ls -l $DIR/$tdir
6152         rm -rf $DIR/$tdir
6153 }
6154 run_test 110 "filename length checking"
6155
6156 test_115() {
6157         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6158             cut -c11-20)
6159         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
6160             return
6161         echo "Starting with $OSTIO_pre threads"
6162
6163         NUMTEST=20000
6164         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6165         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
6166         echo "$NUMTEST creates/unlinks"
6167         test_mkdir -p $DIR/$tdir
6168         createmany -o $DIR/$tdir/$tfile $NUMTEST
6169         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6170
6171         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
6172             cut -c11-20)
6173
6174         # don't return an error
6175         [ $OSTIO_post == $OSTIO_pre ] && echo \
6176             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6177             echo "This may be fine, depending on what ran before this test" &&
6178             echo "and how fast this system is." && return
6179
6180         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6181 }
6182 run_test 115 "verify dynamic thread creation===================="
6183
6184 free_min_max () {
6185         wait_delete_completed
6186         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6187         echo OST kbytes available: ${AVAIL[@]}
6188         MAXI=0; MAXV=${AVAIL[0]}
6189         MINI=0; MINV=${AVAIL[0]}
6190         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6191             #echo OST $i: ${AVAIL[i]}kb
6192             if [ ${AVAIL[i]} -gt $MAXV ]; then
6193                 MAXV=${AVAIL[i]}; MAXI=$i
6194             fi
6195             if [ ${AVAIL[i]} -lt $MINV ]; then
6196                 MINV=${AVAIL[i]}; MINI=$i
6197             fi
6198         done
6199         echo Min free space: OST $MINI: $MINV
6200         echo Max free space: OST $MAXI: $MAXV
6201 }
6202
6203 test_116a() { # was previously test_116()
6204         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6205
6206         echo -n "Free space priority "
6207         lctl get_param -n lov.*-clilov-*.qos_prio_free
6208         declare -a AVAIL
6209         free_min_max
6210         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6211                 return
6212
6213         # generate uneven OSTs
6214         test_mkdir -p $DIR/$tdir/OST${MINI}
6215         declare -i FILL
6216         FILL=$(($MINV / 4))
6217         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6218         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6219         i=0
6220         while [ $FILL -gt 0 ]; do
6221             i=$(($i + 1))
6222             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6223             FILL=$(($FILL - 2048))
6224             echo -n .
6225         done
6226         FILL=$(($MINV / 4))
6227         sync
6228         sleep_maxage
6229
6230         free_min_max
6231         DIFF=$(($MAXV - $MINV))
6232         DIFF2=$(($DIFF * 100 / $MINV))
6233         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6234         if [ $DIFF2 -gt 20 ]; then
6235             echo "ok"
6236         else
6237             echo "failed - QOS mode won't be used"
6238             error_ignore "QOS imbalance criteria not met"
6239             return
6240         fi
6241
6242         MINI1=$MINI; MINV1=$MINV
6243         MAXI1=$MAXI; MAXV1=$MAXV
6244
6245         # now fill using QOS
6246         echo writing a bunch of files to QOS-assigned OSTs
6247         $SETSTRIPE -c 1 $DIR/$tdir
6248         i=0
6249         while [ $FILL -gt 0 ]; do
6250             i=$(($i + 1))
6251             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6252             FILL=$(($FILL - 200))
6253             echo -n .
6254         done
6255         echo "wrote $i 200k files"
6256         sync
6257         sleep_maxage
6258
6259         echo "Note: free space may not be updated, so measurements might be off"
6260         free_min_max
6261         DIFF2=$(($MAXV - $MINV))
6262         echo "free space delta: orig $DIFF final $DIFF2"
6263         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6264         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6265         echo "Wrote $DIFF to smaller OST $MINI1"
6266         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6267         echo "Wrote $DIFF2 to larger OST $MAXI1"
6268         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6269
6270         # Figure out which files were written where
6271         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6272                awk '/'$MINI1': / {print $2; exit}')
6273         echo $UUID
6274         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6275         echo "$MINC files created on smaller OST $MINI1"
6276         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6277                awk '/'$MAXI1': / {print $2; exit}')
6278         echo $UUID
6279         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6280         echo "$MAXC files created on larger OST $MAXI1"
6281         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6282         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6283
6284         rm -rf $DIR/$tdir
6285 }
6286 run_test 116a "stripe QOS: free space balance ==================="
6287
6288 test_116b() { # LU-2093
6289 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6290         local old_rr
6291         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6292         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6293         mkdir -p $DIR/$tdir
6294         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6295         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6296         do_facet $SINGLEMDS lctl set_param fail_loc=0
6297         rm -rf $DIR/$tdir
6298         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6299 }
6300 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6301
6302 test_117() # bug 10891
6303 {
6304         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6305         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6306         lctl set_param fail_loc=0x21e
6307         > $DIR/$tfile || error "truncate failed"
6308         lctl set_param fail_loc=0
6309         echo "Truncate succeeded."
6310         rm -f $DIR/$tfile
6311 }
6312 run_test 117 "verify fsfilt_extend =========="
6313
6314 export OLD_RESENDCOUNT=""
6315 set_resend_count () {
6316         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6317         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6318         lctl set_param -n $PROC_RESENDCOUNT $1
6319         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6320 }
6321
6322 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6323
6324 # Reset async IO behavior after error case
6325 reset_async() {
6326         FILE=$DIR/reset_async
6327
6328         # Ensure all OSCs are cleared
6329         $SETSTRIPE -c -1 $FILE
6330         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6331         sync
6332         rm $FILE
6333 }
6334
6335 test_118a() #bug 11710
6336 {
6337         reset_async
6338
6339         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6340         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6341         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6342
6343         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6344                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6345                 return 1;
6346         fi
6347         rm -f $DIR/$tfile
6348 }
6349 run_test 118a "verify O_SYNC works =========="
6350
6351 test_118b()
6352 {
6353         remote_ost_nodsh && skip "remote OST with nodsh" && return
6354
6355         reset_async
6356
6357         #define OBD_FAIL_OST_ENOENT 0x217
6358         set_nodes_failloc "$(osts_nodes)" 0x217
6359         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6360         RC=$?
6361         set_nodes_failloc "$(osts_nodes)" 0
6362         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6363         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6364                     grep -c writeback)
6365
6366         if [[ $RC -eq 0 ]]; then
6367                 error "Must return error due to dropped pages, rc=$RC"
6368                 return 1;
6369         fi
6370
6371         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6372                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6373                 return 1;
6374         fi
6375
6376         echo "Dirty pages not leaked on ENOENT"
6377
6378         # Due to the above error the OSC will issue all RPCs syncronously
6379         # until a subsequent RPC completes successfully without error.
6380         $MULTIOP $DIR/$tfile Ow4096yc
6381         rm -f $DIR/$tfile
6382
6383         return 0
6384 }
6385 run_test 118b "Reclaim dirty pages on fatal error =========="
6386
6387 test_118c()
6388 {
6389         remote_ost_nodsh && skip "remote OST with nodsh" && return
6390
6391         reset_async
6392
6393         #define OBD_FAIL_OST_EROFS               0x216
6394         set_nodes_failloc "$(osts_nodes)" 0x216
6395
6396         # multiop should block due to fsync until pages are written
6397         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6398         MULTIPID=$!
6399         sleep 1
6400
6401         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6402                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6403         fi
6404
6405         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6406                     grep -c writeback)
6407         if [[ $WRITEBACK -eq 0 ]]; then
6408                 error "No page in writeback, writeback=$WRITEBACK"
6409         fi
6410
6411         set_nodes_failloc "$(osts_nodes)" 0
6412         wait $MULTIPID
6413         RC=$?
6414         if [[ $RC -ne 0 ]]; then
6415                 error "Multiop fsync failed, rc=$RC"
6416         fi
6417
6418         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6419         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6420                     grep -c writeback)
6421         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6422                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6423         fi
6424
6425         rm -f $DIR/$tfile
6426         echo "Dirty pages flushed via fsync on EROFS"
6427         return 0
6428 }
6429 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6430
6431 test_118d()
6432 {
6433         remote_ost_nodsh && skip "remote OST with nodsh" && return
6434
6435         reset_async
6436
6437         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6438         set_nodes_failloc "$(osts_nodes)" 0x214
6439         # multiop should block due to fsync until pages are written
6440         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6441         MULTIPID=$!
6442         sleep 1
6443
6444         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6445                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6446         fi
6447
6448         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6449                     grep -c writeback)
6450         if [[ $WRITEBACK -eq 0 ]]; then
6451                 error "No page in writeback, writeback=$WRITEBACK"
6452         fi
6453
6454         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6455         set_nodes_failloc "$(osts_nodes)" 0
6456
6457         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6458         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6459                     grep -c writeback)
6460         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6461                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6462         fi
6463
6464         rm -f $DIR/$tfile
6465         echo "Dirty pages gaurenteed flushed via fsync"
6466         return 0
6467 }
6468 run_test 118d "Fsync validation inject a delay of the bulk =========="
6469
6470 test_118f() {
6471         reset_async
6472
6473         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6474         lctl set_param fail_loc=0x8000040a
6475
6476         # Should simulate EINVAL error which is fatal
6477         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6478         RC=$?
6479         if [[ $RC -eq 0 ]]; then
6480                 error "Must return error due to dropped pages, rc=$RC"
6481         fi
6482
6483         lctl set_param fail_loc=0x0
6484
6485         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6486         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6487         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6488                     grep -c writeback)
6489         if [[ $LOCKED -ne 0 ]]; then
6490                 error "Locked pages remain in cache, locked=$LOCKED"
6491         fi
6492
6493         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6494                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6495         fi
6496
6497         rm -f $DIR/$tfile
6498         echo "No pages locked after fsync"
6499
6500         reset_async
6501         return 0
6502 }
6503 run_test 118f "Simulate unrecoverable OSC side error =========="
6504
6505 test_118g() {
6506         reset_async
6507
6508         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6509         lctl set_param fail_loc=0x406
6510
6511         # simulate local -ENOMEM
6512         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6513         RC=$?
6514
6515         lctl set_param fail_loc=0
6516         if [[ $RC -eq 0 ]]; then
6517                 error "Must return error due to dropped pages, rc=$RC"
6518         fi
6519
6520         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6521         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6522         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6523                         grep -c writeback)
6524         if [[ $LOCKED -ne 0 ]]; then
6525                 error "Locked pages remain in cache, locked=$LOCKED"
6526         fi
6527
6528         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6529                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6530         fi
6531
6532         rm -f $DIR/$tfile
6533         echo "No pages locked after fsync"
6534
6535         reset_async
6536         return 0
6537 }
6538 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6539
6540 test_118h() {
6541         remote_ost_nodsh && skip "remote OST with nodsh" && return
6542
6543         reset_async
6544
6545         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6546         set_nodes_failloc "$(osts_nodes)" 0x20e
6547         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6548         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6549         RC=$?
6550
6551         set_nodes_failloc "$(osts_nodes)" 0
6552         if [[ $RC -eq 0 ]]; then
6553                 error "Must return error due to dropped pages, rc=$RC"
6554         fi
6555
6556         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6557         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6558         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6559                     grep -c writeback)
6560         if [[ $LOCKED -ne 0 ]]; then
6561                 error "Locked pages remain in cache, locked=$LOCKED"
6562         fi
6563
6564         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6565                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6566         fi
6567
6568         rm -f $DIR/$tfile
6569         echo "No pages locked after fsync"
6570
6571         return 0
6572 }
6573 run_test 118h "Verify timeout in handling recoverables errors  =========="
6574
6575 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6576
6577 test_118i() {
6578         remote_ost_nodsh && skip "remote OST with nodsh" && return
6579
6580         reset_async
6581
6582         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6583         set_nodes_failloc "$(osts_nodes)" 0x20e
6584
6585         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6586         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6587         PID=$!
6588         sleep 5
6589         set_nodes_failloc "$(osts_nodes)" 0
6590
6591         wait $PID
6592         RC=$?
6593         if [[ $RC -ne 0 ]]; then
6594                 error "got error, but should be not, rc=$RC"
6595         fi
6596
6597         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6598         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6599         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6600         if [[ $LOCKED -ne 0 ]]; then
6601                 error "Locked pages remain in cache, locked=$LOCKED"
6602         fi
6603
6604         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6605                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6606         fi
6607
6608         rm -f $DIR/$tfile
6609         echo "No pages locked after fsync"
6610
6611         return 0
6612 }
6613 run_test 118i "Fix error before timeout in recoverable error  =========="
6614
6615 [ "$SLOW" = "no" ] && set_resend_count 4
6616
6617 test_118j() {
6618         remote_ost_nodsh && skip "remote OST with nodsh" && return
6619
6620         reset_async
6621
6622         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6623         set_nodes_failloc "$(osts_nodes)" 0x220
6624
6625         # return -EIO from OST
6626         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6627         RC=$?
6628         set_nodes_failloc "$(osts_nodes)" 0x0
6629         if [[ $RC -eq 0 ]]; then
6630                 error "Must return error due to dropped pages, rc=$RC"
6631         fi
6632
6633         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6634         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6635         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6636         if [[ $LOCKED -ne 0 ]]; then
6637                 error "Locked pages remain in cache, locked=$LOCKED"
6638         fi
6639
6640         # in recoverable error on OST we want resend and stay until it finished
6641         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6642                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6643         fi
6644
6645         rm -f $DIR/$tfile
6646         echo "No pages locked after fsync"
6647
6648         return 0
6649 }
6650 run_test 118j "Simulate unrecoverable OST side error =========="
6651
6652 test_118k()
6653 {
6654         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6655
6656         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6657         set_nodes_failloc "$(osts_nodes)" 0x20e
6658         test_mkdir -p $DIR/$tdir
6659
6660         for ((i=0;i<10;i++)); do
6661                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6662                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6663                 SLEEPPID=$!
6664                 sleep 0.500s
6665                 kill $SLEEPPID
6666                 wait $SLEEPPID
6667         done
6668
6669         set_nodes_failloc "$(osts_nodes)" 0
6670         rm -rf $DIR/$tdir
6671 }
6672 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6673
6674 test_118l()
6675 {
6676         # LU-646
6677         test_mkdir -p $DIR/$tdir
6678         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6679         rm -rf $DIR/$tdir
6680 }
6681 run_test 118l "fsync dir ========="
6682
6683 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6684
6685 test_119a() # bug 11737
6686 {
6687         BSIZE=$((512 * 1024))
6688         directio write $DIR/$tfile 0 1 $BSIZE
6689         # We ask to read two blocks, which is more than a file size.
6690         # directio will indicate an error when requested and actual
6691         # sizes aren't equeal (a normal situation in this case) and
6692         # print actual read amount.
6693         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6694         if [ "$NOB" != "$BSIZE" ]; then
6695                 error "read $NOB bytes instead of $BSIZE"
6696         fi
6697         rm -f $DIR/$tfile
6698 }
6699 run_test 119a "Short directIO read must return actual read amount"
6700
6701 test_119b() # bug 11737
6702 {
6703         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6704
6705         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6706         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6707         sync
6708         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6709                 error "direct read failed"
6710         rm -f $DIR/$tfile
6711 }
6712 run_test 119b "Sparse directIO read must return actual read amount"
6713
6714 test_119c() # bug 13099
6715 {
6716         BSIZE=1048576
6717         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6718         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6719         rm -f $DIR/$tfile
6720 }
6721 run_test 119c "Testing for direct read hitting hole"
6722
6723 test_119d() # bug 15950
6724 {
6725         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6726         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6727         BSIZE=1048576
6728         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6729         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6730         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6731         lctl set_param fail_loc=0x40d
6732         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6733         pid_dio=$!
6734         sleep 1
6735         cat $DIR/$tfile > /dev/null &
6736         lctl set_param fail_loc=0
6737         pid_reads=$!
6738         wait $pid_dio
6739         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6740         sleep 2
6741         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6742         error "the read rpcs have not completed in 2s"
6743         rm -f $DIR/$tfile
6744         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6745 }
6746 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6747
6748 test_120a() {
6749         test_mkdir -p $DIR/$tdir
6750         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6751                skip "no early lock cancel on server" && return 0
6752         lru_resize_disable mdc
6753         lru_resize_disable osc
6754         cancel_lru_locks mdc
6755         stat $DIR/$tdir > /dev/null
6756         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6757         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6758         test_mkdir $DIR/$tdir/d1
6759         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6760         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6761         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6762         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6763         lru_resize_enable mdc
6764         lru_resize_enable osc
6765 }
6766 run_test 120a "Early Lock Cancel: mkdir test"
6767
6768 test_120b() {
6769         test_mkdir -p $DIR/$tdir
6770         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6771                skip "no early lock cancel on server" && return 0
6772         lru_resize_disable mdc
6773         lru_resize_disable osc
6774         cancel_lru_locks mdc
6775         stat $DIR/$tdir > /dev/null
6776         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6777         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6778         touch $DIR/$tdir/f1
6779         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6780         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6781         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6782         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6783         lru_resize_enable mdc
6784         lru_resize_enable osc
6785 }
6786 run_test 120b "Early Lock Cancel: create test"
6787
6788 test_120c() {
6789         test_mkdir -p $DIR/$tdir
6790         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6791                skip "no early lock cancel on server" && return 0
6792         lru_resize_disable mdc
6793         lru_resize_disable osc
6794         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6795         touch $DIR/$tdir/d1/f1
6796         cancel_lru_locks mdc
6797         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6798         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6799         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6800         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6801         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6802         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6803         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6804         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6805         lru_resize_enable mdc
6806         lru_resize_enable osc
6807 }
6808 run_test 120c "Early Lock Cancel: link test"
6809
6810 test_120d() {
6811         test_mkdir -p $DIR/$tdir
6812         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6813                skip "no early lock cancel on server" && return 0
6814         lru_resize_disable mdc
6815         lru_resize_disable osc
6816         touch $DIR/$tdir
6817         cancel_lru_locks mdc
6818         stat $DIR/$tdir > /dev/null
6819         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6820         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6821         chmod a+x $DIR/$tdir
6822         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6823         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6824         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6825         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6826         lru_resize_enable mdc
6827         lru_resize_enable osc
6828 }
6829 run_test 120d "Early Lock Cancel: setattr test"
6830
6831 test_120e() {
6832         test_mkdir -p $DIR/$tdir
6833         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6834                skip "no early lock cancel on server" && return 0
6835         lru_resize_disable mdc
6836         lru_resize_disable osc
6837         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6838         cancel_lru_locks mdc
6839         cancel_lru_locks osc
6840         dd if=$DIR/$tdir/f1 of=/dev/null
6841         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
6842         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6843               awk '/ldlm_cancel/ {print $2}'`
6844         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6845               awk '/ldlm_bl_callback/ {print $2}'`
6846         unlink $DIR/$tdir/f1
6847         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6848               awk '/ldlm_cancel/ {print $2}'`
6849         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6850               awk '/ldlm_bl_callback/ {print $2}'`
6851         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6852         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6853         lru_resize_enable mdc
6854         lru_resize_enable osc
6855 }
6856 run_test 120e "Early Lock Cancel: unlink test"
6857
6858 test_120f() {
6859         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6860                skip "no early lock cancel on server" && return 0
6861         test_mkdir -p $DIR/$tdir
6862         lru_resize_disable mdc
6863         lru_resize_disable osc
6864         test_mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6865         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
6866         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
6867         cancel_lru_locks mdc
6868         cancel_lru_locks osc
6869         dd if=$DIR/$tdir/d1/f1 of=/dev/null
6870         dd if=$DIR/$tdir/d2/f2 of=/dev/null
6871         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
6872         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6873               awk '/ldlm_cancel/ {print $2}'`
6874         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6875               awk '/ldlm_bl_callback/ {print $2}'`
6876         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6877         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6878               awk '/ldlm_cancel/ {print $2}'`
6879         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6880               awk '/ldlm_bl_callback/ {print $2}'`
6881         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6882         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6883         lru_resize_enable mdc
6884         lru_resize_enable osc
6885 }
6886 run_test 120f "Early Lock Cancel: rename test"
6887
6888 test_120g() {
6889         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6890                skip "no early lock cancel on server" && return 0
6891         lru_resize_disable mdc
6892         lru_resize_disable osc
6893         count=10000
6894         echo create $count files
6895         test_mkdir -p $DIR/$tdir
6896         cancel_lru_locks mdc
6897         cancel_lru_locks osc
6898         t0=`date +%s`
6899
6900         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6901               awk '/ldlm_cancel/ {print $2}'`
6902         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6903               awk '/ldlm_bl_callback/ {print $2}'`
6904         createmany -o $DIR/$tdir/f $count
6905         sync
6906         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6907               awk '/ldlm_cancel/ {print $2}'`
6908         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6909               awk '/ldlm_bl_callback/ {print $2}'`
6910         t1=`date +%s`
6911         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
6912         echo rm $count files
6913         rm -r $DIR/$tdir
6914         sync
6915         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6916               awk '/ldlm_cancel/ {print $2}'`
6917         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6918               awk '/ldlm_bl_callback/ {print $2}'`
6919         t2=`date +%s`
6920         echo total: $count removes in $((t2-t1))
6921         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
6922         sleep 2
6923         # wait for commitment of removal
6924         lru_resize_enable mdc
6925         lru_resize_enable osc
6926 }
6927 run_test 120g "Early Lock Cancel: performance test"
6928
6929 test_121() { #bug #10589
6930         rm -rf $DIR/$tfile
6931         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
6932 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
6933         lctl set_param fail_loc=0x310
6934         cancel_lru_locks osc > /dev/null
6935         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
6936         lctl set_param fail_loc=0
6937         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
6938 }
6939 run_test 121 "read cancel race ========="
6940
6941 test_123a() { # was test 123, statahead(bug 11401)
6942         SLOWOK=0
6943         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
6944             log "testing on UP system. Performance may be not as good as expected."
6945                         SLOWOK=1
6946         fi
6947
6948         rm -rf $DIR/$tdir
6949         test_mkdir -p $DIR/$tdir
6950         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6951         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
6952         MULT=10
6953         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
6954                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
6955
6956                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6957                 lctl set_param -n llite.*.statahead_max 0
6958                 lctl get_param llite.*.statahead_max
6959                 cancel_lru_locks mdc
6960                 cancel_lru_locks osc
6961                 stime=`date +%s`
6962                 time ls -l $DIR/$tdir | wc -l
6963                 etime=`date +%s`
6964                 delta=$((etime - stime))
6965                 log "ls $i files without statahead: $delta sec"
6966                 lctl set_param llite.*.statahead_max=$max
6967
6968                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6969                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
6970                 cancel_lru_locks mdc
6971                 cancel_lru_locks osc
6972                 stime=`date +%s`
6973                 time ls -l $DIR/$tdir | wc -l
6974                 etime=`date +%s`
6975                 delta_sa=$((etime - stime))
6976                 log "ls $i files with statahead: $delta_sa sec"
6977                 lctl get_param -n llite.*.statahead_stats
6978                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6979
6980                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
6981                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
6982
6983                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6984                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6985                     lctl set_param -n llite.*.statahead_max 0
6986                     lctl get_param llite.*.statahead_max
6987                     cancel_lru_locks mdc
6988                     cancel_lru_locks osc
6989                     stime=`date +%s`
6990                     time ls -l $DIR/$tdir | wc -l
6991                     etime=`date +%s`
6992                     delta=$((etime - stime))
6993                     log "ls $i files again without statahead: $delta sec"
6994                     lctl set_param llite.*.statahead_max=$max
6995                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6996                         if [  $SLOWOK -eq 0 ]; then
6997                                 error "ls $i files is slower with statahead!"
6998                         else
6999                                 log "ls $i files is slower with statahead!"
7000                         fi
7001                         break
7002                     fi
7003                 fi
7004
7005                 [ $delta -gt 20 ] && break
7006                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7007                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7008         done
7009         log "ls done"
7010
7011         stime=`date +%s`
7012         rm -r $DIR/$tdir
7013         sync
7014         etime=`date +%s`
7015         delta=$((etime - stime))
7016         log "rm -r $DIR/$tdir/: $delta seconds"
7017         log "rm done"
7018         lctl get_param -n llite.*.statahead_stats
7019 }
7020 run_test 123a "verify statahead work"
7021
7022 test_123b () { # statahead(bug 15027)
7023         test_mkdir -p $DIR/$tdir
7024         createmany -o $DIR/$tdir/$tfile-%d 1000
7025
7026         cancel_lru_locks mdc
7027         cancel_lru_locks osc
7028
7029 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7030         lctl set_param fail_loc=0x80000803
7031         ls -lR $DIR/$tdir > /dev/null
7032         log "ls done"
7033         lctl set_param fail_loc=0x0
7034         lctl get_param -n llite.*.statahead_stats
7035         rm -r $DIR/$tdir
7036         sync
7037
7038 }
7039 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7040
7041 test_124a() {
7042         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7043                skip "no lru resize on server" && return 0
7044         local NR=2000
7045         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7046
7047         log "create $NR files at $DIR/$tdir"
7048         createmany -o $DIR/$tdir/f $NR ||
7049                 error "failed to create $NR files in $DIR/$tdir"
7050
7051         cancel_lru_locks mdc
7052         ls -l $DIR/$tdir > /dev/null
7053
7054         local NSDIR=""
7055         local LRU_SIZE=0
7056         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
7057                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
7058                 LRU_SIZE=$(lctl get_param -n $PARAM)
7059                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
7060                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7061                                                 log "NSDIR=$NSDIR"
7062                         log "NS=$(basename $NSDIR)"
7063                         break
7064                 fi
7065         done
7066
7067         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
7068                 skip "Not enough cached locks created!"
7069                 return 0
7070         fi
7071         log "LRU=$LRU_SIZE"
7072
7073         local SLEEP=30
7074
7075         # We know that lru resize allows one client to hold $LIMIT locks
7076         # for 10h. After that locks begin to be killed by client.
7077         local MAX_HRS=10
7078         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
7079                 log "LIMIT=$LIMIT"
7080
7081         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7082         # killing locks. Some time was spent for creating locks. This means
7083         # that up to the moment of sleep finish we must have killed some of
7084         # them (10-100 locks). This depends on how fast ther were created.
7085         # Many of them were touched in almost the same moment and thus will
7086         # be killed in groups.
7087         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7088
7089         # Use $LRU_SIZE_B here to take into account real number of locks
7090         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7091         local LRU_SIZE_B=$LRU_SIZE
7092         log "LVF=$LVF"
7093         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
7094                 log "OLD_LVF=$OLD_LVF"
7095         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
7096
7097         # Let's make sure that we really have some margin. Client checks
7098         # cached locks every 10 sec.
7099         SLEEP=$((SLEEP+20))
7100         log "Sleep ${SLEEP} sec"
7101         local SEC=0
7102         while ((SEC<$SLEEP)); do
7103                 echo -n "..."
7104                 sleep 5
7105                 SEC=$((SEC+5))
7106                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
7107                 echo -n "$LRU_SIZE"
7108         done
7109         echo ""
7110         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7111         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
7112
7113         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
7114                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7115                 unlinkmany $DIR/$tdir/f $NR
7116                 return
7117         }
7118
7119         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7120         log "unlink $NR files at $DIR/$tdir"
7121         unlinkmany $DIR/$tdir/f $NR
7122 }
7123 run_test 124a "lru resize ======================================="
7124
7125 get_max_pool_limit()
7126 {
7127         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
7128         local max=0
7129         for l in $limit; do
7130                 if test $l -gt $max; then
7131                         max=$l
7132                 fi
7133         done
7134         echo $max
7135 }
7136
7137 test_124b() {
7138         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
7139                skip "no lru resize on server" && return 0
7140
7141         LIMIT=`get_max_pool_limit`
7142
7143         NR=$(($(default_lru_size)*20))
7144         if [ $NR -gt $LIMIT ]; then
7145                 log "Limit lock number by $LIMIT locks"
7146                 NR=$LIMIT
7147         fi
7148         lru_resize_disable mdc
7149         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
7150                 error "failed to create $DIR/$tdir/disable_lru_resize"
7151
7152         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
7153         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
7154         cancel_lru_locks mdc
7155         stime=`date +%s`
7156         PID=""
7157         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7158         PID="$PID $!"
7159         sleep 2
7160         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7161         PID="$PID $!"
7162         sleep 2
7163         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
7164         PID="$PID $!"
7165         wait $PID
7166         etime=`date +%s`
7167         nolruresize_delta=$((etime-stime))
7168         log "ls -la time: $nolruresize_delta seconds"
7169         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7170         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
7171
7172         lru_resize_enable mdc
7173         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
7174                 error "failed to create $DIR/$tdir/enable_lru_resize"
7175
7176         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
7177         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
7178         cancel_lru_locks mdc
7179         stime=`date +%s`
7180         PID=""
7181         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7182         PID="$PID $!"
7183         sleep 2
7184         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7185         PID="$PID $!"
7186         sleep 2
7187         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7188         PID="$PID $!"
7189         wait $PID
7190         etime=`date +%s`
7191         lruresize_delta=$((etime-stime))
7192         log "ls -la time: $lruresize_delta seconds"
7193         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7194
7195         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7196                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7197         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7198                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7199         else
7200                 log "lru resize performs the same with no lru resize"
7201         fi
7202         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7203 }
7204 run_test 124b "lru resize (performance test) ======================="
7205
7206 test_125() { # 13358
7207         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7208         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7209         test_mkdir -p $DIR/d125 || error "mkdir failed"
7210         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7211         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7212         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7213 }
7214 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7215
7216 test_126() { # bug 12829/13455
7217         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7218         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7219         $GSS && skip "must run as gss disabled" && return
7220
7221         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7222         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7223         rm -f $DIR/$tfile
7224         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7225 }
7226 run_test 126 "check that the fsgid provided by the client is taken into account"
7227
7228 test_127a() { # bug 15521
7229         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7230         $LCTL set_param osc.*.stats=0
7231         FSIZE=$((2048 * 1024))
7232         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7233         cancel_lru_locks osc
7234         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7235
7236         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7237         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7238                 echo "got $COUNT $NAME"
7239                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7240                 eval $NAME=$COUNT || error "Wrong proc format"
7241
7242                 case $NAME in
7243                         read_bytes|write_bytes)
7244                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7245                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7246                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7247                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7248                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7249                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7250                                 error "sumsquare is too small: $SUMSQ"
7251                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7252                                 error "sumsquare is too big: $SUMSQ"
7253                         ;;
7254                         *) ;;
7255                 esac
7256         done < $DIR/${tfile}.tmp
7257
7258         #check that we actually got some stats
7259         [ "$read_bytes" ] || error "Missing read_bytes stats"
7260         [ "$write_bytes" ] || error "Missing write_bytes stats"
7261         [ "$read_bytes" != 0 ] || error "no read done"
7262         [ "$write_bytes" != 0 ] || error "no write done"
7263 }
7264 run_test 127a "verify the client stats are sane"
7265
7266 test_127b() { # bug LU-333
7267         $LCTL set_param llite.*.stats=0
7268         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7269         # perform 2 reads and writes so MAX is different from SUM.
7270         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7271         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7272         cancel_lru_locks osc
7273         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7274         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7275
7276         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7277         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7278                 echo "got $COUNT $NAME"
7279                 eval $NAME=$COUNT || error "Wrong proc format"
7280
7281         case $NAME in
7282                 read_bytes)
7283                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7284                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7285                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7286                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7287                         ;;
7288                 write_bytes)
7289                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7290                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7291                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7292                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7293                         ;;
7294                         *) ;;
7295                 esac
7296         done < $TMP/${tfile}.tmp
7297
7298         #check that we actually got some stats
7299         [ "$read_bytes" ] || error "Missing read_bytes stats"
7300         [ "$write_bytes" ] || error "Missing write_bytes stats"
7301         [ "$read_bytes" != 0 ] || error "no read done"
7302         [ "$write_bytes" != 0 ] || error "no write done"
7303 }
7304 run_test 127b "verify the llite client stats are sane"
7305
7306 test_128() { # bug 15212
7307         touch $DIR/$tfile
7308         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7309                 find $DIR/$tfile
7310                 find $DIR/$tfile
7311         EOF
7312
7313         result=$(grep error $TMP/$tfile.log)
7314         rm -f $DIR/$tfile
7315         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7316 }
7317 run_test 128 "interactive lfs for 2 consecutive find's"
7318
7319 set_dir_limits () {
7320         local mntdev
7321         local canondev
7322         local node
7323
7324         local LDPROC=/proc/fs/ldiskfs
7325         local facets=$(get_facets MDS)
7326
7327         for facet in ${facets//,/ }; do
7328                 canondev=$(ldiskfs_canon \
7329                            *.$(convert_facet2label $facet).mntdev $facet)
7330                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
7331                                                 LDPROC=/sys/fs/ldiskfs
7332                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7333         done
7334 }
7335
7336 test_129() {
7337         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7338                 skip "Only applicable to ldiskfs-based MDTs"
7339                 return
7340         fi
7341         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7342
7343         EFBIG=27
7344         MAX=16384
7345
7346         set_dir_limits $MAX
7347         test_mkdir -p $DIR/$tdir
7348
7349         local I=0
7350         local J=0
7351         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7352                 $MULTIOP $DIR/$tdir/$J Oc
7353                 rc=$?
7354                 if [ $rc -eq $EFBIG ]; then
7355                         set_dir_limits 0
7356                         echo "return code $rc received as expected"
7357                         return 0
7358                 elif [ $rc -ne 0 ]; then
7359                         set_dir_limits 0
7360                         error_exit "return code $rc received instead of expected $EFBIG"
7361                 fi
7362                 J=$((J+1))
7363                 I=$(stat -c%s "$DIR/$tdir")
7364         done
7365
7366         set_dir_limits 0
7367         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7368 }
7369 run_test 129 "test directory size limit ========================"
7370
7371 OLDIFS="$IFS"
7372 cleanup_130() {
7373         trap 0
7374         IFS="$OLDIFS"
7375 }
7376
7377 test_130a() {
7378         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7379         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7380                 return
7381
7382         trap cleanup_130 EXIT RETURN
7383
7384         local fm_file=$DIR/$tfile
7385         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7386         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7387                 error "dd failed for $fm_file"
7388
7389         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7390         filefrag -ves $fm_file
7391         RC=$?
7392         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7393                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7394         [ $RC != 0 ] && error "filefrag $fm_file failed"
7395
7396         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7397                       grep -v "ext:" | grep -v "found")
7398         lun=$($GETSTRIPE -i $fm_file)
7399
7400         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7401         IFS=$'\n'
7402         tot_len=0
7403         for line in $filefrag_op
7404         do
7405                 frag_lun=`echo $line | cut -d: -f5`
7406                 ext_len=`echo $line | cut -d: -f4`
7407                 if (( $frag_lun != $lun )); then
7408                         cleanup_130
7409                         error "FIEMAP on 1-stripe file($fm_file) failed"
7410                         return
7411                 fi
7412                 (( tot_len += ext_len ))
7413         done
7414
7415         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7416                 cleanup_130
7417                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7418                 return
7419         fi
7420
7421         cleanup_130
7422
7423         echo "FIEMAP on single striped file succeeded"
7424 }
7425 run_test 130a "FIEMAP (1-stripe file)"
7426
7427 test_130b() {
7428         [ "$OSTCOUNT" -lt "2" ] &&
7429                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7430
7431         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7432         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7433                 return
7434
7435         trap cleanup_130 EXIT RETURN
7436
7437         local fm_file=$DIR/$tfile
7438         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7439         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7440                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7441
7442         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7443                 error "dd failed on $fm_file"
7444
7445         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7446         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7447                       grep -v "ext:" | grep -v "found")
7448
7449         last_lun=$(echo $filefrag_op | cut -d: -f5)
7450
7451         IFS=$'\n'
7452         tot_len=0
7453         num_luns=1
7454         for line in $filefrag_op
7455         do
7456                 frag_lun=`echo $line | cut -d: -f5`
7457                 ext_len=`echo $line | cut -d: -f4`
7458                 if (( $frag_lun != $last_lun )); then
7459                         if (( tot_len != 1024 )); then
7460                                 cleanup_130
7461                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7462                                 return
7463                         else
7464                                 (( num_luns += 1 ))
7465                                 tot_len=0
7466                         fi
7467                 fi
7468                 (( tot_len += ext_len ))
7469                 last_lun=$frag_lun
7470         done
7471         if (( num_luns != 2 || tot_len != 1024 )); then
7472                 cleanup_130
7473                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7474                 return
7475         fi
7476
7477         cleanup_130
7478
7479         echo "FIEMAP on 2-stripe file succeeded"
7480 }
7481 run_test 130b "FIEMAP (2-stripe file)"
7482
7483 test_130c() {
7484         [ "$OSTCOUNT" -lt "2" ] &&
7485                 skip_env "skipping FIEMAP on 2-stripe file" && return
7486
7487         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7488         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7489                 return
7490
7491         trap cleanup_130 EXIT RETURN
7492
7493         local fm_file=$DIR/$tfile
7494         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7495         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7496                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7497
7498         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7499
7500         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7501         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7502
7503         last_lun=`echo $filefrag_op | cut -d: -f5`
7504
7505         IFS=$'\n'
7506         tot_len=0
7507         num_luns=1
7508         for line in $filefrag_op
7509         do
7510                 frag_lun=`echo $line | cut -d: -f5`
7511                 ext_len=`echo $line | cut -d: -f4`
7512                 if (( $frag_lun != $last_lun )); then
7513                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7514                         if (( logical != 512 )); then
7515                                 cleanup_130
7516                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7517                                 return
7518                         fi
7519                         if (( tot_len != 512 )); then
7520                                 cleanup_130
7521                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7522                                 return
7523                         else
7524                                 (( num_luns += 1 ))
7525                                 tot_len=0
7526                         fi
7527                 fi
7528                 (( tot_len += ext_len ))
7529                 last_lun=$frag_lun
7530         done
7531         if (( num_luns != 2 || tot_len != 512 )); then
7532                 cleanup_130
7533                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7534                 return
7535         fi
7536
7537         cleanup_130
7538
7539         echo "FIEMAP on 2-stripe file with hole succeeded"
7540 }
7541 run_test 130c "FIEMAP (2-stripe file with hole)"
7542
7543 test_130d() {
7544         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7545
7546         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7547         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7548
7549         trap cleanup_130 EXIT RETURN
7550
7551         local fm_file=$DIR/$tfile
7552         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7553         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7554                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7555         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7556
7557         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7558         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7559
7560         last_lun=`echo $filefrag_op | cut -d: -f5`
7561
7562         IFS=$'\n'
7563         tot_len=0
7564         num_luns=1
7565         for line in $filefrag_op
7566         do
7567                 frag_lun=`echo $line | cut -d: -f5`
7568                 ext_len=`echo $line | cut -d: -f4`
7569                 if (( $frag_lun != $last_lun )); then
7570                         if (( tot_len != 1024 )); then
7571                                 cleanup_130
7572                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7573                                 return
7574                         else
7575                                 (( num_luns += 1 ))
7576                                 tot_len=0
7577                         fi
7578                 fi
7579                 (( tot_len += ext_len ))
7580                 last_lun=$frag_lun
7581         done
7582         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7583                 cleanup_130
7584                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7585                 return
7586         fi
7587
7588         cleanup_130
7589
7590         echo "FIEMAP on N-stripe file succeeded"
7591 }
7592 run_test 130d "FIEMAP (N-stripe file)"
7593
7594 test_130e() {
7595         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7596
7597         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7598         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7599
7600         trap cleanup_130 EXIT RETURN
7601
7602         local fm_file=$DIR/$tfile
7603         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7604         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7605                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7606
7607         NUM_BLKS=512
7608         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7609         for ((i = 0; i < $NUM_BLKS; i++))
7610         do
7611                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7612         done
7613
7614         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7615         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7616
7617         last_lun=`echo $filefrag_op | cut -d: -f5`
7618
7619         IFS=$'\n'
7620         tot_len=0
7621         num_luns=1
7622         for line in $filefrag_op
7623         do
7624                 frag_lun=`echo $line | cut -d: -f5`
7625                 ext_len=`echo $line | cut -d: -f4`
7626                 if (( $frag_lun != $last_lun )); then
7627                         if (( tot_len != $EXPECTED_LEN )); then
7628                                 cleanup_130
7629                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7630                                 return
7631                         else
7632                                 (( num_luns += 1 ))
7633                                 tot_len=0
7634                         fi
7635                 fi
7636                 (( tot_len += ext_len ))
7637                 last_lun=$frag_lun
7638         done
7639         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7640                 cleanup_130
7641                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7642                 return
7643         fi
7644
7645         cleanup_130
7646
7647         echo "FIEMAP with continuation calls succeeded"
7648 }
7649 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7650
7651 # Test for writev/readv
7652 test_131a() {
7653         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7654         error "writev test failed"
7655         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7656         error "readv failed"
7657         rm -f $DIR/$tfile
7658 }
7659 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7660
7661 test_131b() {
7662         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7663         error "append writev test failed"
7664         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7665         error "append writev test failed"
7666         rm -f $DIR/$tfile
7667 }
7668 run_test 131b "test append writev"
7669
7670 test_131c() {
7671         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7672         error "NOT PASS"
7673 }
7674 run_test 131c "test read/write on file w/o objects"
7675
7676 test_131d() {
7677         rwv -f $DIR/$tfile -w -n 1 1572864
7678         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7679         if [ "$NOB" != 1572864 ]; then
7680                 error "Short read filed: read $NOB bytes instead of 1572864"
7681         fi
7682         rm -f $DIR/$tfile
7683 }
7684 run_test 131d "test short read"
7685
7686 test_131e() {
7687         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7688         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7689         error "read hitting hole failed"
7690         rm -f $DIR/$tfile
7691 }
7692 run_test 131e "test read hitting hole"
7693
7694 get_ost_param() {
7695         local token=$1
7696         local gl_sum=0
7697         for node in $(osts_nodes); do
7698                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7699                 [ x$gl = x"" ] && gl=0
7700                 gl_sum=$((gl_sum + gl))
7701         done
7702         echo $gl
7703 }
7704
7705 som_mode_switch() {
7706         local som=$1
7707         local gl1=$2
7708         local gl2=$3
7709
7710         if [ x$som = x"enabled" ]; then
7711                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7712                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7713                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7714         else
7715                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7716                 MOUNTOPT="$MOUNTOPT,som_preview"
7717                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7718         fi
7719
7720         # do remount to make new mount-conf parameters actual
7721         echo remounting...
7722         sync
7723         stopall
7724         setupall
7725 }
7726
7727 test_132() { #1028, SOM
7728         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7729         local num=$(get_mds_dir $DIR)
7730         local mymds=mds${num}
7731         local MOUNTOPT_SAVE=$MOUNTOPT
7732
7733         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7734         cancel_lru_locks osc
7735
7736         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7737
7738         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7739         stat $DIR/$tfile >/dev/null
7740         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7741         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7742         rm $DIR/$tfile
7743         som_mode_switch $som1 $gl1 $gl2
7744
7745         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7746         cancel_lru_locks osc
7747
7748         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7749         if [ $som1 == $som2 ]; then
7750             error "som is still "$som2
7751             if [ x$som2 = x"enabled" ]; then
7752                 som2="disabled"
7753             else
7754                 som2="enabled"
7755             fi
7756         fi
7757
7758         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7759         stat $DIR/$tfile >/dev/null
7760         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7761         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7762         som_mode_switch $som2 $gl1 $gl2
7763         MOUNTOPT=$MOUNTOPT_SAVE
7764 }
7765 run_test 132 "som avoids glimpse rpc"
7766
7767 check_stats() {
7768         local res
7769         local count
7770         case $1 in
7771         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7772                  ;;
7773         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7774                  ;;
7775         *) error "Wrong argument $1" ;;
7776         esac
7777         echo $res
7778         count=`echo $res | awk '{print $2}'`
7779         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7780         # if the argument $3 is zero, it means any stat increment is ok.
7781         if [ $3 -gt 0 ] ; then
7782                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7783         fi
7784 }
7785
7786 test_133a() {
7787         remote_ost_nodsh && skip "remote OST with nodsh" && return
7788         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7789
7790         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7791                 { skip "MDS doesn't support rename stats"; return; }
7792         local testdir=$DIR/${tdir}/stats_testdir
7793         mkdir -p $DIR/${tdir}
7794
7795         # clear stats.
7796         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7797         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7798
7799         # verify mdt stats first.
7800         mkdir ${testdir} || error "mkdir failed"
7801         check_stats $SINGLEMDS "mkdir" 1
7802         touch ${testdir}/${tfile} || "touch failed"
7803         check_stats $SINGLEMDS "open" 1
7804         check_stats $SINGLEMDS "close" 1
7805         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7806         check_stats $SINGLEMDS "mknod" 1
7807         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7808         check_stats $SINGLEMDS "unlink" 1
7809         rm -f ${testdir}/${tfile} || error "file remove failed"
7810         check_stats $SINGLEMDS "unlink" 2
7811
7812         # remove working dir and check mdt stats again.
7813         rmdir ${testdir} || error "rmdir failed"
7814         check_stats $SINGLEMDS "rmdir" 1
7815
7816         local testdir1=$DIR/${tdir}/stats_testdir1
7817         mkdir -p ${testdir}
7818         mkdir -p ${testdir1}
7819         touch ${testdir1}/test1
7820         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7821         check_stats $SINGLEMDS "crossdir_rename" 1
7822
7823         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
7824         check_stats $SINGLEMDS "samedir_rename" 1
7825
7826         rm -rf $DIR/${tdir}
7827 }
7828 run_test 133a "Verifying MDT stats ========================================"
7829
7830 test_133b() {
7831         remote_ost_nodsh && skip "remote OST with nodsh" && return
7832         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7833         local testdir=$DIR/${tdir}/stats_testdir
7834         mkdir -p ${testdir} || error "mkdir failed"
7835         touch ${testdir}/${tfile} || "touch failed"
7836         cancel_lru_locks mdc
7837
7838         # clear stats.
7839         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7840         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7841
7842         # extra mdt stats verification.
7843         chmod 444 ${testdir}/${tfile} || error "chmod failed"
7844         check_stats $SINGLEMDS "setattr" 1
7845         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7846         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
7847         then            # LU-1740
7848                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
7849                 check_stats $SINGLEMDS "getattr" 1
7850         fi
7851         $LFS df || error "lfs failed"
7852         check_stats $SINGLEMDS "statfs" 1
7853
7854         rm -rf $DIR/${tdir}
7855 }
7856 run_test 133b "Verifying extra MDT stats =================================="
7857
7858 test_133c() {
7859         remote_ost_nodsh && skip "remote OST with nodsh" && return
7860         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7861         local testdir=$DIR/${tdir}/stats_testdir
7862         test_mkdir -p ${testdir} || error "mkdir failed"
7863
7864         # verify obdfilter stats.
7865         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
7866         sync
7867         cancel_lru_locks osc
7868         wait_delete_completed
7869
7870         # clear stats.
7871         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7872         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7873
7874         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
7875         sync
7876         cancel_lru_locks osc
7877         check_stats ost "write" 1
7878
7879         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
7880         check_stats ost "read" 1
7881
7882         > ${testdir}/${tfile} || error "truncate failed"
7883         check_stats ost "punch" 1
7884
7885         rm -f ${testdir}/${tfile} || error "file remove failed"
7886         wait_delete_completed
7887         check_stats ost "destroy" 1
7888
7889         rm -rf $DIR/${tdir}
7890 }
7891 run_test 133c "Verifying OST stats ========================================"
7892
7893 order_2() {
7894     local value=$1
7895     local orig=$value
7896     local order=1
7897
7898     while [ $value -ge 2 ]; do
7899         order=$((order*2))
7900         value=$((value/2))
7901     done
7902
7903     if [ $orig -gt $order ]; then
7904         order=$((order*2))
7905     fi
7906     echo $order
7907 }
7908
7909 size_in_KMGT() {
7910     local value=$1
7911     local size=('K' 'M' 'G' 'T');
7912     local i=0
7913     local size_string=$value
7914
7915     while [ $value -ge 1024 ]; do
7916         if [ $i -gt 3 ]; then
7917             #T is the biggest unit we get here, if that is bigger,
7918             #just return XXXT
7919             size_string=${value}T
7920             break
7921         fi
7922         value=$((value >> 10))
7923         if [ $value -lt 1024 ]; then
7924             size_string=${value}${size[$i]}
7925             break
7926         fi
7927         i=$((i + 1))
7928     done
7929
7930     echo $size_string
7931 }
7932
7933 get_rename_size() {
7934     local size=$1
7935     local context=${2:-.}
7936     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
7937                 grep -A1 $context |
7938                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
7939     echo $sample
7940 }
7941
7942 test_133d() {
7943     remote_ost_nodsh && skip "remote OST with nodsh" && return
7944     remote_mds_nodsh && skip "remote MDS with nodsh" && return
7945     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7946         { skip "MDS doesn't support rename stats"; return; }
7947
7948     local testdir1=$DIR/${tdir}/stats_testdir1
7949     local testdir2=$DIR/${tdir}/stats_testdir2
7950
7951     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7952
7953     test_mkdir -p ${testdir1} || error "mkdir failed"
7954     test_mkdir -p ${testdir2} || error "mkdir failed"
7955
7956     createmany -o $testdir1/test 512 || error "createmany failed"
7957
7958         # check samedir rename size
7959         mv ${testdir1}/test0 ${testdir1}/test_0
7960
7961         local testdir1_size=$(ls -l $DIR/${tdir} |
7962                 awk '/stats_testdir1/ {print $5}')
7963         local testdir2_size=$(ls -l $DIR/${tdir} |
7964                 awk '/stats_testdir2/ {print $5}')
7965
7966         testdir1_size=$(order_2 $testdir1_size)
7967         testdir2_size=$(order_2 $testdir2_size)
7968
7969         testdir1_size=$(size_in_KMGT $testdir1_size)
7970         testdir2_size=$(size_in_KMGT $testdir2_size)
7971
7972         echo "source rename dir size: ${testdir1_size}"
7973         echo "target rename dir size: ${testdir2_size}"
7974
7975     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
7976     eval $cmd || error "$cmd failed"
7977     local samedir=$($cmd | grep 'same_dir')
7978     local same_sample=$(get_rename_size $testdir1_size)
7979     [ -z "$samedir" ] && error "samedir_rename_size count error"
7980     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
7981     echo "Check same dir rename stats success"
7982
7983     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7984
7985     # check crossdir rename size
7986     mv ${testdir1}/test_0 ${testdir2}/test_0
7987
7988         testdir1_size=$(ls -l $DIR/${tdir} |
7989                 awk '/stats_testdir1/ {print $5}')
7990         testdir2_size=$(ls -l $DIR/${tdir} |
7991                 awk '/stats_testdir2/ {print $5}')
7992
7993         testdir1_size=$(order_2 $testdir1_size)
7994         testdir2_size=$(order_2 $testdir2_size)
7995
7996         testdir1_size=$(size_in_KMGT $testdir1_size)
7997         testdir2_size=$(size_in_KMGT $testdir2_size)
7998
7999         echo "source rename dir size: ${testdir1_size}"
8000         echo "target rename dir size: ${testdir2_size}"
8001
8002     eval $cmd || error "$cmd failed"
8003     local crossdir=$($cmd | grep 'crossdir')
8004     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8005     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8006     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8007     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
8008     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
8009     echo "Check cross dir rename stats success"
8010     rm -rf $DIR/${tdir}
8011 }
8012 run_test 133d "Verifying rename_stats ========================================"
8013
8014 test_133e() {
8015         local testdir=$DIR/${tdir}/stats_testdir
8016         local ctr f0 f1 bs=32768 count=42 sum
8017
8018         remote_ost_nodsh && skip "remote OST with nodsh" && return
8019         mkdir -p ${testdir} || error "mkdir failed"
8020
8021         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8022
8023         for ctr in {write,read}_bytes; do
8024                 sync
8025                 cancel_lru_locks osc
8026
8027                 do_facet ost1 $LCTL set_param -n \
8028                         "obdfilter.*.exports.clear=clear"
8029
8030                 if [ $ctr = write_bytes ]; then
8031                         f0=/dev/zero
8032                         f1=${testdir}/${tfile}
8033                 else
8034                         f0=${testdir}/${tfile}
8035                         f1=/dev/null
8036                 fi
8037
8038                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8039                         error "dd failed"
8040                 sync
8041                 cancel_lru_locks osc
8042
8043                 sum=$(do_facet ost1 $LCTL get_param \
8044                                 "obdfilter.*.exports.*.stats" | \
8045                           awk -v ctr=$ctr '\
8046                                 BEGIN { sum = 0 }
8047                                 $1 == ctr { sum += $7 }
8048                                 END { print sum }')
8049
8050                 if ((sum != bs * count)); then
8051                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8052                 fi
8053         done
8054
8055         rm -rf $DIR/${tdir}
8056 }
8057 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8058
8059 test_140() { #bug-17379
8060         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
8061         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
8062         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
8063
8064         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
8065         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
8066         local i=0
8067         while i=`expr $i + 1`; do
8068                 test_mkdir -p $i || error "Creating dir $i"
8069                 cd $i || error "Changing to $i"
8070                 ln -s ../stat stat || error "Creating stat symlink"
8071                 # Read the symlink until ELOOP present,
8072                 # not LBUGing the system is considered success,
8073                 # we didn't overrun the stack.
8074                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
8075                 [ $ret -ne 0 ] && {
8076                         if [ $ret -eq 40 ]; then
8077                                 break  # -ELOOP
8078                         else
8079                                 error "Open stat symlink"
8080                                 return
8081                         fi
8082                 }
8083         done
8084         i=`expr $i - 1`
8085         echo "The symlink depth = $i"
8086         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
8087                                         error "Invalid symlink depth"
8088
8089         # Test recursive symlink
8090         ln -s symlink_self symlink_self
8091         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
8092         echo "open symlink_self returns $ret"
8093         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
8094 }
8095 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
8096
8097 test_150() {
8098         local TF="$TMP/$tfile"
8099
8100         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8101         cp $TF $DIR/$tfile
8102         cancel_lru_locks osc
8103         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
8104         remount_client $MOUNT
8105         df -P $MOUNT
8106         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
8107
8108         $TRUNCATE $TF 6000
8109         $TRUNCATE $DIR/$tfile 6000
8110         cancel_lru_locks osc
8111         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
8112
8113         echo "12345" >>$TF
8114         echo "12345" >>$DIR/$tfile
8115         cancel_lru_locks osc
8116         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
8117
8118         echo "12345" >>$TF
8119         echo "12345" >>$DIR/$tfile
8120         cancel_lru_locks osc
8121         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
8122
8123         rm -f $TF
8124         true
8125 }
8126 run_test 150 "truncate/append tests"
8127
8128 function roc_hit() {
8129         local list=$(comma_list $(osts_nodes))
8130
8131         echo $(get_obdfilter_param $list '' stats |
8132                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
8133 }
8134
8135 function set_cache() {
8136         local on=1
8137
8138         if [ "$2" == "off" ]; then
8139                 on=0;
8140         fi
8141         local list=$(comma_list $(osts_nodes))
8142         set_obdfilter_param $list '' $1_cache_enable $on
8143
8144         cancel_lru_locks osc
8145 }
8146
8147 test_151() {
8148         remote_ost_nodsh && skip "remote OST with nodsh" && return
8149
8150         local CPAGES=3
8151         local list=$(comma_list $(osts_nodes))
8152
8153         # check whether obdfilter is cache capable at all
8154         if ! get_obdfilter_param $list '' read_cache_enable >/dev/null; then
8155                 echo "not cache-capable obdfilter"
8156                 return 0
8157         fi
8158
8159         # check cache is enabled on all obdfilters
8160         if get_obdfilter_param $list '' read_cache_enable | grep 0; then
8161                 echo "oss cache is disabled"
8162                 return 0
8163         fi
8164
8165         set_obdfilter_param $list '' writethrough_cache_enable 1
8166
8167         # pages should be in the case right after write
8168         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
8169         local BEFORE=`roc_hit`
8170         cancel_lru_locks osc
8171         cat $DIR/$tfile >/dev/null
8172         local AFTER=`roc_hit`
8173         if ! let "AFTER - BEFORE == CPAGES"; then
8174                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8175         fi
8176
8177         # the following read invalidates the cache
8178         cancel_lru_locks osc
8179         set_obdfilter_param $list '' read_cache_enable 0
8180         cat $DIR/$tfile >/dev/null
8181
8182         # now data shouldn't be found in the cache
8183         BEFORE=`roc_hit`
8184         cancel_lru_locks osc
8185         cat $DIR/$tfile >/dev/null
8186         AFTER=`roc_hit`
8187         if let "AFTER - BEFORE != 0"; then
8188                 error "IN CACHE: before: $BEFORE, after: $AFTER"
8189         fi
8190
8191         set_obdfilter_param $list '' read_cache_enable 1
8192         rm -f $DIR/$tfile
8193 }
8194 run_test 151 "test cache on oss and controls ==============================="
8195
8196 test_152() {
8197         local TF="$TMP/$tfile"
8198
8199         # simulate ENOMEM during write
8200 #define OBD_FAIL_OST_NOMEM      0x226
8201         lctl set_param fail_loc=0x80000226
8202         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
8203         cp $TF $DIR/$tfile
8204         sync || error "sync failed"
8205         lctl set_param fail_loc=0
8206
8207         # discard client's cache
8208         cancel_lru_locks osc
8209
8210         # simulate ENOMEM during read
8211         lctl set_param fail_loc=0x80000226
8212         cmp $TF $DIR/$tfile || error "cmp failed"
8213         lctl set_param fail_loc=0
8214
8215         rm -f $TF
8216 }
8217 run_test 152 "test read/write with enomem ============================"
8218
8219 test_153() {
8220         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
8221 }
8222 run_test 153 "test if fdatasync does not crash ======================="
8223
8224 test_154() {
8225         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
8226                 { skip "Need MDS version at least 2.2.51"; return 0; }
8227
8228         cp /etc/hosts $DIR/$tfile
8229
8230         fid=$($LFS path2fid $DIR/$tfile)
8231         rc=$?
8232         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8233
8234         ffid=$DIR/.lustre/fid/$fid
8235
8236         echo "stat fid $fid"
8237         stat $ffid > /dev/null || error "stat $ffid failed."
8238         echo "touch fid $fid"
8239         touch $ffid || error "touch $ffid failed."
8240         echo "write to fid $fid"
8241         cat /etc/hosts > $ffid || error "write $ffid failed."
8242         echo "read fid $fid"
8243         diff /etc/hosts $ffid || error "read $ffid failed."
8244         echo "append write to fid $fid"
8245         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8246         echo "rename fid $fid"
8247         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
8248         touch $DIR/$tfile.1
8249         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
8250         rm -f $DIR/$tfile.1
8251         echo "truncate fid $fid"
8252         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8253         echo "link fid $fid"
8254         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
8255         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8256                 echo "setfacl fid $fid"
8257                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8258                 echo "getfacl fid $fid"
8259                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8260         fi
8261         echo "unlink fid $fid"
8262         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
8263         echo "mknod fid $fid"
8264         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8265
8266         fid=[0xf00000400:0x1:0x0]
8267         ffid=$DIR/.lustre/fid/$fid
8268
8269         echo "stat non-exist fid $fid"
8270         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8271         echo "write to non-exist fid $fid"
8272         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8273         echo "link new fid $fid"
8274         ln $DIR/$tfile $ffid && error "link $ffid should fail."
8275
8276         test_mkdir -p $DIR/$tdir
8277         touch $DIR/$tdir/$tfile
8278         fid=$($LFS path2fid $DIR/$tdir)
8279         rc=$?
8280         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8281
8282         ffid=$DIR/.lustre/fid/$fid
8283
8284         echo "ls $fid"
8285         ls $ffid > /dev/null || error "ls $ffid failed."
8286         echo "touch $fid/$tfile.1"
8287         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8288
8289         echo "touch $DIR/.lustre/fid/$tfile"
8290         touch $DIR/.lustre/fid/$tfile && \
8291                 error "touch $DIR/.lustre/fid/$tfile should fail."
8292
8293         echo "setxattr to $DIR/.lustre/fid"
8294         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
8295                 error "setxattr should fail."
8296
8297         echo "listxattr for $DIR/.lustre/fid"
8298         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
8299                 error "listxattr should fail."
8300
8301         echo "delxattr from $DIR/.lustre/fid"
8302         setfattr -x trusted.name1 $DIR/.lustre/fid &&
8303                 error "delxattr should fail."
8304
8305         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
8306         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
8307                 error "touch invalid fid should fail."
8308
8309         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
8310         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
8311                 error "touch non-normal fid should fail."
8312
8313         echo "rename $tdir to $DIR/.lustre/fid"
8314         mrename $DIR/$tdir $DIR/.lustre/fid &&
8315                 error "rename to $DIR/.lustre/fid should fail."
8316
8317         echo "rename .lustre to itself"
8318         fid=$($LFS path2fid $DIR)
8319         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
8320                 error "rename .lustre to itself should fail."
8321
8322         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
8323         fid=$($LFS path2fid $DIR/$tfile-2)
8324         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
8325         cp /etc/passwd $DIR/.lustre/fid/$fid &&
8326                 error "create lov data thru .lustre should fail."
8327         echo "cp /etc/passwd $DIR/$tfile-2"
8328         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
8329         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
8330         diff /etc/passwd $DIR/.lustre/fid/$fid ||
8331                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
8332
8333         echo "Open-by-FID succeeded"
8334 }
8335 run_test 154 "Open-by-FID"
8336
8337 test_155_small_load() {
8338     local temp=$TMP/$tfile
8339     local file=$DIR/$tfile
8340
8341     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8342         error "dd of=$temp bs=6096 count=1 failed"
8343     cp $temp $file
8344     cancel_lru_locks osc
8345     cmp $temp $file || error "$temp $file differ"
8346
8347     $TRUNCATE $temp 6000
8348     $TRUNCATE $file 6000
8349     cmp $temp $file || error "$temp $file differ (truncate1)"
8350
8351     echo "12345" >>$temp
8352     echo "12345" >>$file
8353     cmp $temp $file || error "$temp $file differ (append1)"
8354
8355     echo "12345" >>$temp
8356     echo "12345" >>$file
8357     cmp $temp $file || error "$temp $file differ (append2)"
8358
8359     rm -f $temp $file
8360     true
8361 }
8362
8363 test_155_big_load() {
8364     remote_ost_nodsh && skip "remote OST with nodsh" && return
8365     local temp=$TMP/$tfile
8366     local file=$DIR/$tfile
8367
8368     free_min_max
8369     local cache_size=$(do_facet ost$((MAXI+1)) \
8370         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8371     local large_file_size=$((cache_size * 2))
8372
8373     echo "OSS cache size: $cache_size KB"
8374     echo "Large file size: $large_file_size KB"
8375
8376     [ $MAXV -le $large_file_size ] && \
8377         skip_env "max available OST size needs > $large_file_size KB" && \
8378         return 0
8379
8380     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8381
8382     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8383         error "dd of=$temp bs=$large_file_size count=1k failed"
8384     cp $temp $file
8385     ls -lh $temp $file
8386     cancel_lru_locks osc
8387     cmp $temp $file || error "$temp $file differ"
8388
8389     rm -f $temp $file
8390     true
8391 }
8392
8393 test_155a() {
8394     set_cache read on
8395     set_cache writethrough on
8396     test_155_small_load
8397 }
8398 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8399
8400 test_155b() {
8401     set_cache read on
8402     set_cache writethrough off
8403     test_155_small_load
8404 }
8405 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8406
8407 test_155c() {
8408     set_cache read off
8409     set_cache writethrough on
8410     test_155_small_load
8411 }
8412 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8413
8414 test_155d() {
8415     set_cache read off
8416     set_cache writethrough off
8417     test_155_small_load
8418 }
8419 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8420
8421 test_155e() {
8422     set_cache read on
8423     set_cache writethrough on
8424     test_155_big_load
8425 }
8426 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8427
8428 test_155f() {
8429     set_cache read on
8430     set_cache writethrough off
8431     test_155_big_load
8432 }
8433 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8434
8435 test_155g() {
8436     set_cache read off
8437     set_cache writethrough on
8438     test_155_big_load
8439 }
8440 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8441
8442 test_155h() {
8443     set_cache read off
8444     set_cache writethrough off
8445     test_155_big_load
8446 }
8447 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8448
8449 test_156() {
8450     local CPAGES=3
8451     local BEFORE
8452     local AFTER
8453     local file="$DIR/$tfile"
8454
8455         [ "$(facet_fstype ost1)" = "zfs" ] &&
8456                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8457                 return
8458
8459     log "Turn on read and write cache"
8460     set_cache read on
8461     set_cache writethrough on
8462
8463     log "Write data and read it back."
8464     log "Read should be satisfied from the cache."
8465     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8466     BEFORE=`roc_hit`
8467     cancel_lru_locks osc
8468     cat $file >/dev/null
8469     AFTER=`roc_hit`
8470     if ! let "AFTER - BEFORE == CPAGES"; then
8471         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8472     else
8473         log "cache hits:: before: $BEFORE, after: $AFTER"
8474     fi
8475
8476     log "Read again; it should be satisfied from the cache."
8477     BEFORE=$AFTER
8478     cancel_lru_locks osc
8479     cat $file >/dev/null
8480     AFTER=`roc_hit`
8481     if ! let "AFTER - BEFORE == CPAGES"; then
8482         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8483     else
8484         log "cache hits:: before: $BEFORE, after: $AFTER"
8485     fi
8486
8487
8488     log "Turn off the read cache and turn on the write cache"
8489     set_cache read off
8490     set_cache writethrough on
8491
8492     log "Read again; it should be satisfied from the cache."
8493     BEFORE=`roc_hit`
8494     cancel_lru_locks osc
8495     cat $file >/dev/null
8496     AFTER=`roc_hit`
8497     if ! let "AFTER - BEFORE == CPAGES"; then
8498         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8499     else
8500         log "cache hits:: before: $BEFORE, after: $AFTER"
8501     fi
8502
8503     log "Read again; it should not be satisfied from the cache."
8504     BEFORE=$AFTER
8505     cancel_lru_locks osc
8506     cat $file >/dev/null
8507     AFTER=`roc_hit`
8508     if ! let "AFTER - BEFORE == 0"; then
8509         error "IN CACHE: before: $BEFORE, after: $AFTER"
8510     else
8511         log "cache hits:: before: $BEFORE, after: $AFTER"
8512     fi
8513
8514     log "Write data and read it back."
8515     log "Read should be satisfied from the cache."
8516     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8517     BEFORE=`roc_hit`
8518     cancel_lru_locks osc
8519     cat $file >/dev/null
8520     AFTER=`roc_hit`
8521     if ! let "AFTER - BEFORE == CPAGES"; then
8522         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8523     else
8524         log "cache hits:: before: $BEFORE, after: $AFTER"
8525     fi
8526
8527     log "Read again; it should not be satisfied from the cache."
8528     BEFORE=$AFTER
8529     cancel_lru_locks osc
8530     cat $file >/dev/null
8531     AFTER=`roc_hit`
8532     if ! let "AFTER - BEFORE == 0"; then
8533         error "IN CACHE: before: $BEFORE, after: $AFTER"
8534     else
8535         log "cache hits:: before: $BEFORE, after: $AFTER"
8536     fi
8537
8538
8539     log "Turn off read and write cache"
8540     set_cache read off
8541     set_cache writethrough off
8542
8543     log "Write data and read it back"
8544     log "It should not be satisfied from the cache."
8545     rm -f $file
8546     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8547     cancel_lru_locks osc
8548     BEFORE=`roc_hit`
8549     cat $file >/dev/null
8550     AFTER=`roc_hit`
8551     if ! let "AFTER - BEFORE == 0"; then
8552         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8553     else
8554         log "cache hits:: before: $BEFORE, after: $AFTER"
8555     fi
8556
8557
8558     log "Turn on the read cache and turn off the write cache"
8559     set_cache read on
8560     set_cache writethrough off
8561
8562     log "Write data and read it back"
8563     log "It should not be satisfied from the cache."
8564     rm -f $file
8565     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8566     BEFORE=`roc_hit`
8567     cancel_lru_locks osc
8568     cat $file >/dev/null
8569     AFTER=`roc_hit`
8570     if ! let "AFTER - BEFORE == 0"; then
8571         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8572     else
8573         log "cache hits:: before: $BEFORE, after: $AFTER"
8574     fi
8575
8576     log "Read again; it should be satisfied from the cache."
8577     BEFORE=`roc_hit`
8578     cancel_lru_locks osc
8579     cat $file >/dev/null
8580     AFTER=`roc_hit`
8581     if ! let "AFTER - BEFORE == CPAGES"; then
8582         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8583     else
8584         log "cache hits:: before: $BEFORE, after: $AFTER"
8585     fi
8586
8587     rm -f $file
8588 }
8589 run_test 156 "Verification of tunables ============================"
8590
8591 #Changelogs
8592 err17935 () {
8593     if [ $MDSCOUNT -gt 1 ]; then
8594         error_ignore 17935 $*
8595     else
8596         error $*
8597     fi
8598 }
8599
8600 changelog_chmask()
8601 {
8602     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8603            grep -c $1)
8604
8605     if [ $MASK -eq 1 ]; then
8606         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8607     else
8608         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8609     fi
8610 }
8611
8612 test_160() {
8613     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8614     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8615         { skip "Need MDS version at least 2.2.0"; return; }
8616     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8617     echo "Registered as changelog user $USER"
8618     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8619         grep -q $USER || error "User $USER not found in changelog_users"
8620
8621     # change something
8622     test_mkdir -p $DIR/$tdir/pics/2008/zachy
8623     touch $DIR/$tdir/pics/2008/zachy/timestamp
8624     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8625     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8626     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8627     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8628     rm $DIR/$tdir/pics/desktop.jpg
8629
8630     $LFS changelog $MDT0 | tail -5
8631
8632     echo "verifying changelog mask"
8633     changelog_chmask "MKDIR"
8634     changelog_chmask "CLOSE"
8635
8636     test_mkdir -p $DIR/$tdir/pics/zach/sofia
8637     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8638
8639     changelog_chmask "MKDIR"
8640     changelog_chmask "CLOSE"
8641
8642     test_mkdir -p $DIR/$tdir/pics/2008/sofia
8643     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8644
8645     $LFS changelog $MDT0
8646     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8647     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8648     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8649     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8650
8651     # verify contents
8652     echo "verifying target fid"
8653     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8654         tail -1 | awk '{print $6}')
8655     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8656     [ "$fidc" == "t=$fidf" ] || \
8657         err17935 "fid in changelog $fidc != file fid $fidf"
8658     echo "verifying parent fid"
8659     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8660         tail -1 | awk '{print $7}')
8661     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8662     [ "$fidc" == "p=$fidf" ] || \
8663         err17935 "pfid in changelog $fidc != dir fid $fidf"
8664
8665     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8666         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8667     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8668     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8669         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8670     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8671     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8672         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8673
8674     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8675         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8676     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8677     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8678     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8679         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8680
8681     echo "verifying user deregister"
8682     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8683     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8684         grep -q $USER && error "User $USER still found in changelog_users"
8685
8686     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8687         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8688     if [ $USERS -eq 0 ]; then
8689         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8690             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8691         touch $DIR/$tdir/chloe
8692         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8693             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8694         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8695         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8696     else
8697         echo "$USERS other changelog users; can't verify off"
8698     fi
8699 }
8700 run_test 160 "changelog sanity"
8701
8702 test_161() {
8703     test_mkdir -p $DIR/$tdir
8704     cp /etc/hosts $DIR/$tdir/$tfile
8705     test_mkdir $DIR/$tdir/foo1
8706     test_mkdir $DIR/$tdir/foo2
8707     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8708     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8709     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8710     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8711     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8712     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8713         $LFS fid2path $DIR $FID
8714         err17935 "bad link ea"
8715     fi
8716     # middle
8717     rm $DIR/$tdir/foo2/zachary
8718     # last
8719     rm $DIR/$tdir/foo2/thor
8720     # first
8721     rm $DIR/$tdir/$tfile
8722     # rename
8723     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8724     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8725         then
8726         $LFS fid2path $DIR $FID
8727         err17935 "bad link rename"
8728     fi
8729     rm $DIR/$tdir/foo2/maggie
8730
8731     # overflow the EA
8732     local longname=filename_avg_len_is_thirty_two_
8733     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8734         error "failed to hardlink many files"
8735     links=$($LFS fid2path $DIR $FID | wc -l)
8736     echo -n "${links}/1000 links in link EA"
8737     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8738     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8739         error "failed to unlink many hardlinks"
8740 }
8741 run_test 161 "link ea sanity"
8742
8743 check_path() {
8744     local expected=$1
8745     shift
8746     local fid=$2
8747
8748     local path=$(${LFS} fid2path $*)
8749     RC=$?
8750
8751     if [ $RC -ne 0 ]; then
8752         err17935 "path looked up of $expected failed. Error $RC"
8753         return $RC
8754     elif [ "${path}" != "${expected}" ]; then
8755         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8756         return 2
8757     fi
8758     echo "fid $fid resolves to path $path (expected $expected)"
8759 }
8760
8761 test_162() {
8762         # Make changes to filesystem
8763         test_mkdir -p $DIR/$tdir/d2
8764         touch $DIR/$tdir/d2/$tfile
8765         touch $DIR/$tdir/d2/x1
8766         touch $DIR/$tdir/d2/x2
8767         test_mkdir -p $DIR/$tdir/d2/a/b/c
8768         test_mkdir -p $DIR/$tdir/d2/p/q/r
8769         # regular file
8770         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8771         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8772
8773         # softlink
8774         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8775         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8776         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8777
8778         # softlink to wrong file
8779         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8780         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8781         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8782
8783         # hardlink
8784         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8785         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8786         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8787         # fid2path dir/fsname should both work
8788         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8789         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8790
8791         # hardlink count: check that there are 2 links
8792         # Doesnt work with CMD yet: 17935
8793         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8794                 err17935 "expected 2 links"
8795
8796         # hardlink indexing: remove the first link
8797         rm $DIR/$tdir/d2/p/q/r/hlink
8798         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8799
8800         return 0
8801 }
8802 run_test 162 "path lookup sanity"
8803
8804 test_163() {
8805         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8806         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
8807         copytool $FSNAME &
8808         sleep 1
8809         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
8810         # this proc file is temporary and linux-only
8811         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
8812          error "kernel->userspace send failed"
8813         kill -INT $!
8814 }
8815 run_test 163 "kernel <-> userspace comms"
8816
8817 test_169() {
8818         # do directio so as not to populate the page cache
8819         log "creating a 10 Mb file"
8820         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8821         log "starting reads"
8822         dd if=$DIR/$tfile of=/dev/null bs=4096 &
8823         log "truncating the file"
8824         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8825         log "killing dd"
8826         kill %+ || true # reads might have finished
8827         echo "wait until dd is finished"
8828         wait
8829         log "removing the temporary file"
8830         rm -rf $DIR/$tfile || error "tmp file removal failed"
8831 }
8832 run_test 169 "parallel read and truncate should not deadlock"
8833
8834 test_170() {
8835         $LCTL clear     # bug 18514
8836         $LCTL debug_daemon start $TMP/${tfile}_log_good
8837         touch $DIR/$tfile
8838         $LCTL debug_daemon stop
8839         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
8840                error "sed failed to read log_good"
8841
8842         $LCTL debug_daemon start $TMP/${tfile}_log_good
8843         rm -rf $DIR/$tfile
8844         $LCTL debug_daemon stop
8845
8846         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
8847                error "lctl df log_bad failed"
8848
8849         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8850         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8851
8852         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
8853         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
8854
8855         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
8856                 error "bad_line good_line1 good_line2 are empty"
8857
8858         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8859         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
8860         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8861
8862         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
8863         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8864         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8865
8866         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
8867                 error "bad_line_new good_line_new are empty"
8868
8869         local expected_good=$((good_line1 + good_line2*2))
8870
8871         rm -f $TMP/${tfile}*
8872         # LU-231, short malformed line may not be counted into bad lines
8873         if [ $bad_line -ne $bad_line_new ] &&
8874                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
8875                 error "expected $bad_line bad lines, but got $bad_line_new"
8876                 return 1
8877         fi
8878
8879         if [ $expected_good -ne $good_line_new ]; then
8880                 error "expected $expected_good good lines, but got $good_line_new"
8881                 return 2
8882         fi
8883         true
8884 }
8885 run_test 170 "test lctl df to handle corrupted log ====================="
8886
8887 test_171() { # bug20592
8888 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8889         $LCTL set_param fail_loc=0x50e
8890         $LCTL set_param fail_val=3000
8891         multiop_bg_pause $DIR/$tfile O_s || true
8892         local MULTIPID=$!
8893         kill -USR1 $MULTIPID
8894         # cause log dump
8895         sleep 3
8896         wait $MULTIPID
8897         if dmesg | grep "recursive fault"; then
8898                 error "caught a recursive fault"
8899         fi
8900         $LCTL set_param fail_loc=0
8901         true
8902 }
8903 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
8904
8905 # it would be good to share it with obdfilter-survey/libecho code
8906 setup_obdecho_osc () {
8907         local rc=0
8908         local ost_nid=$1
8909         local obdfilter_name=$2
8910         echo "Creating new osc for $obdfilter_name on $ost_nid"
8911         # make sure we can find loopback nid
8912         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
8913
8914         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
8915                            ${obdfilter_name}_osc_UUID || rc=2; }
8916         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
8917                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
8918         return $rc
8919 }
8920
8921 cleanup_obdecho_osc () {
8922         local obdfilter_name=$1
8923         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
8924         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
8925         return 0
8926 }
8927
8928 obdecho_create_test() {
8929         local OBD=$1
8930         local node=$2
8931         local rc=0
8932         local id
8933         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
8934         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
8935                            rc=2; }
8936         if [ $rc -eq 0 ]; then
8937             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
8938             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
8939         fi
8940         echo "New object id is $id"
8941         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
8942                            rc=4; }
8943         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
8944                                         "cleanup" || rc=5; }
8945         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
8946                                         "detach" || rc=6; }
8947         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
8948         return $rc
8949 }
8950
8951 test_180a() {
8952         remote_ost_nodsh && skip "remote OST with nodsh" && return
8953         local rc=0
8954         local rmmod_local=0
8955
8956         if ! module_loaded obdecho; then
8957             load_module obdecho/obdecho
8958             rmmod_local=1
8959         fi
8960
8961         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
8962         local host=$(lctl get_param -n osc.$osc.import |
8963                              awk '/current_connection:/ {print $2}' )
8964         local target=$(lctl get_param -n osc.$osc.import |
8965                              awk '/target:/ {print $2}' )
8966         target=${target%_UUID}
8967
8968         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
8969         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
8970         [[ -n $target ]] && cleanup_obdecho_osc $target
8971         [ $rmmod_local -eq 1 ] && rmmod obdecho
8972         return $rc
8973 }
8974 run_test 180a "test obdecho on osc"
8975
8976 test_180b() {
8977         remote_ost_nodsh && skip "remote OST with nodsh" && return
8978         local rc=0
8979         local rmmod_remote=0
8980
8981         do_facet ost1 "lsmod | grep -q obdecho || "                      \
8982                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
8983                       "modprobe obdecho; }" && rmmod_remote=1
8984         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
8985         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
8986         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
8987         return $rc
8988 }
8989 run_test 180b "test obdecho directly on obdfilter"
8990
8991 test_181() { # bug 22177
8992         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8993         # create enough files to index the directory
8994         createmany -o $DIR/$tdir/foobar 4000
8995         # print attributes for debug purpose
8996         lsattr -d .
8997         # open dir
8998         multiop_bg_pause $DIR/$tdir D_Sc || return 1
8999         MULTIPID=$!
9000         # remove the files & current working dir
9001         unlinkmany $DIR/$tdir/foobar 4000
9002         rmdir $DIR/$tdir
9003         kill -USR1 $MULTIPID
9004         wait $MULTIPID
9005         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
9006         return 0
9007 }
9008 run_test 181 "Test open-unlinked dir ========================"
9009
9010 test_182() {
9011         # disable MDC RPC lock wouldn't crash client
9012         local fcount=1000
9013         local tcount=4
9014
9015         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9016 #define OBD_FAIL_MDC_RPCS_SEM           0x804
9017         $LCTL set_param fail_loc=0x804
9018
9019         for (( i=0; i < $tcount; i++ )) ; do
9020                 mkdir $DIR/$tdir/$i
9021                 createmany -o $DIR/$tdir/$i/f- $fcount &
9022         done
9023         wait
9024
9025         for (( i=0; i < $tcount; i++ )) ; do
9026                 unlinkmany $DIR/$tdir/$i/f- $fcount &
9027         done
9028         wait
9029
9030         rm -rf $DIR/$tdir
9031
9032         $LCTL set_param fail_loc=0
9033 }
9034 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
9035
9036 test_183() { # LU-2275
9037         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
9038         echo aaa > $DIR/$tdir/$tfile
9039
9040 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
9041         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
9042
9043         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
9044         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
9045
9046         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
9047
9048         # Flush negative dentry cache
9049         touch $DIR/$tdir/$tfile
9050
9051         # We are not checking for any leaked references here, they'll
9052         # become evident next time we do cleanup with module unload.
9053         rm -rf $DIR/$tdir
9054 }
9055 run_test 183 "No crash or request leak in case of strange dispositions ========"
9056
9057 # OST pools tests
9058 check_file_in_pool()
9059 {
9060         local file=$1
9061         local pool=$2
9062         local tlist="$3"
9063         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
9064         for i in $res
9065         do
9066                 for t in $tlist ; do
9067                         [ "$i" -eq "$t" ] && continue 2
9068                 done
9069
9070                 echo "pool list: $tlist"
9071                 echo "striping: $res"
9072                 error_noexit "$file not allocated in $pool"
9073                 return 1
9074         done
9075         return 0
9076 }
9077
9078 pool_add() {
9079         echo "Creating new pool"
9080         local pool=$1
9081
9082         create_pool $FSNAME.$pool ||
9083                 { error_noexit "No pool created, result code $?"; return 1; }
9084         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
9085                 { error_noexit "$pool not in lfs pool_list"; return 2; }
9086 }
9087
9088 pool_add_targets() {
9089         echo "Adding targets to pool"
9090         local pool=$1
9091         local first=$2
9092         local last=$3
9093         local step=${4:-1}
9094
9095         local list=$(seq $first $step $last)
9096
9097         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
9098         do_facet mgs $LCTL pool_add \
9099                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
9100         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
9101                         | sort -u | tr '\n' ' ' " "$t" || { 
9102                 error_noexit "Add to pool failed"
9103                 return 1
9104         }
9105         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
9106         local addcount=$(((last - first) / step + 1))
9107         [ $lfscount -eq $addcount ] || {
9108                 error_noexit "lfs pool_list bad ost count" \
9109                                                 "$lfscount != $addcount"
9110                 return 2
9111         }
9112 }
9113
9114 pool_set_dir() {
9115         local pool=$1
9116         local tdir=$2
9117         echo "Setting pool on directory $tdir"
9118
9119         $SETSTRIPE -c 2 -p $pool $tdir && return 0
9120
9121         error_noexit "Cannot set pool $pool to $tdir"
9122         return 1
9123 }
9124
9125 pool_check_dir() {
9126         local pool=$1
9127         local tdir=$2
9128         echo "Checking pool on directory $tdir"
9129
9130         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
9131         [ "$res" = "$pool" ] && return 0
9132
9133         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
9134         return 1
9135 }
9136
9137 pool_dir_rel_path() {
9138         echo "Testing relative path works well"
9139         local pool=$1
9140         local tdir=$2
9141         local root=$3
9142
9143         mkdir -p $root/$tdir/$tdir
9144         cd $root/$tdir
9145         pool_set_dir $pool $tdir          || return 1
9146         pool_set_dir $pool ./$tdir        || return 2
9147         pool_set_dir $pool ../$tdir       || return 3
9148         pool_set_dir $pool ../$tdir/$tdir || return 4
9149         rm -rf $tdir; cd - > /dev/null
9150 }
9151
9152 pool_alloc_files() {
9153         echo "Checking files allocation from directory pool"
9154         local pool=$1
9155         local tdir=$2
9156         local count=$3
9157         local tlist="$4"
9158
9159         local failed=0
9160         for i in $(seq -w 1 $count)
9161         do
9162                 local file=$tdir/file-$i
9163                 touch $file
9164                 check_file_in_pool $file $pool "$tlist" || \
9165                         failed=$((failed + 1))
9166         done
9167         [ "$failed" = 0 ] && return 0
9168
9169         error_noexit "$failed files not allocated in $pool"
9170         return 1
9171 }
9172
9173 pool_create_files() {
9174         echo "Creating files in pool"
9175         local pool=$1
9176         local tdir=$2
9177         local count=$3
9178         local tlist="$4"
9179
9180         mkdir -p $tdir
9181         local failed=0
9182         for i in $(seq -w 1 $count)
9183         do
9184                 local file=$tdir/spoo-$i
9185                 $SETSTRIPE -p $pool $file
9186                 check_file_in_pool $file $pool "$tlist" || \
9187                         failed=$((failed + 1))
9188         done
9189         [ "$failed" = 0 ] && return 0
9190
9191         error_noexit "$failed files not allocated in $pool"
9192         return 1
9193 }
9194
9195 pool_lfs_df() {
9196         echo "Checking 'lfs df' output"
9197         local pool=$1
9198
9199         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
9200                         tr '\n' ' ')
9201         local res=$($LFS df --pool $FSNAME.$pool |
9202                         awk '{print $1}' |
9203                         grep "$FSNAME-OST" |
9204                         tr '\n' ' ')
9205         [ "$res" = "$t" ] && return 0
9206
9207         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
9208         return 1
9209 }
9210
9211 pool_file_rel_path() {
9212         echo "Creating files in a pool with relative pathname"
9213         local pool=$1
9214         local tdir=$2
9215
9216         mkdir -p $tdir ||
9217                 { error_noexit "unable to create $tdir"; return 1 ; }
9218         local file="/..$tdir/$tfile-1"
9219         $SETSTRIPE -p $pool $file ||
9220                 { error_noexit "unable to create $file" ; return 2 ; }
9221
9222         cd $tdir
9223         $SETSTRIPE -p $pool $tfile-2 || {
9224                 error_noexit "unable to create $tfile-2 in $tdir"
9225                 return 3
9226         }
9227 }
9228
9229 pool_remove_first_target() {
9230         echo "Removing first target from a pool"
9231         local pool=$1
9232
9233         local pname="lov.$FSNAME-*.pools.$pool"
9234         local t=$($LCTL get_param -n $pname | head -1)
9235         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9236         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9237                 error_noexit "$t not removed from $FSNAME.$pool"
9238                 return 1
9239         }
9240 }
9241
9242 pool_remove_all_targets() {
9243         echo "Removing all targets from pool"
9244         local pool=$1
9245         local file=$2
9246         local pname="lov.$FSNAME-*.pools.$pool"
9247         for t in $($LCTL get_param -n $pname | sort -u)
9248         do
9249                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9250         done
9251         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9252                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9253                 return 1
9254         }
9255         # striping on an empty/nonexistant pool should fall back 
9256         # to "pool of everything"
9257         touch $file || {
9258                 error_noexit "failed to use fallback striping for empty pool"
9259                 return 2
9260         }
9261         # setstripe on an empty pool should fail
9262         $SETSTRIPE -p $pool $file 2>/dev/null && {
9263                 error_noexit "expected failure when creating file" \
9264                                                         "with empty pool"
9265                 return 3
9266         }
9267         return 0
9268 }
9269
9270 pool_remove() {
9271         echo "Destroying pool"
9272         local pool=$1
9273         local file=$2
9274
9275         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9276
9277         sleep 2
9278         # striping on an empty/nonexistant pool should fall back 
9279         # to "pool of everything"
9280         touch $file || {
9281                 error_noexit "failed to use fallback striping for missing pool"
9282                 return 1
9283         }
9284         # setstripe on an empty pool should fail
9285         $SETSTRIPE -p $pool $file 2>/dev/null && {
9286                 error_noexit "expected failure when creating file" \
9287                                                         "with missing pool"
9288                 return 2
9289         }
9290
9291         # get param should return err once pool is gone
9292         if wait_update $HOSTNAME "lctl get_param -n \
9293                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9294         then
9295                 remove_pool_from_list $FSNAME.$pool
9296                 return 0
9297         fi
9298         error_noexit "Pool $FSNAME.$pool is not destroyed"
9299         return 3
9300 }
9301
9302 test_200() {
9303         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9304
9305         local POOL=${POOL:-cea1}
9306         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9307         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9308         # Pool OST targets
9309         local first_ost=0
9310         local last_ost=$(($OSTCOUNT - 1))
9311         local ost_step=2
9312         local ost_list=$(seq $first_ost $ost_step $last_ost)
9313         local ost_range="$first_ost $last_ost $ost_step"
9314         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9315         local file_dir=$POOL_ROOT/file_tst
9316
9317         local rc=0
9318         while : ; do
9319                 # former test_200a test_200b
9320                 pool_add $POOL                          || { rc=$? ; break; }
9321                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9322                 # former test_200c test_200d
9323                 mkdir -p $test_path
9324                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9325                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9326                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9327                                                         || { rc=$? ; break; }
9328                 # former test_200e test_200f
9329                 local files=$((OSTCOUNT*3))
9330                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9331                                                         || { rc=$? ; break; }
9332                 pool_create_files $POOL $file_dir $files "$ost_list" \
9333                                                         || { rc=$? ; break; }
9334                 # former test_200g test_200h
9335                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9336                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9337
9338                 # former test_201a test_201b test_201c
9339                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9340
9341                 local f=$test_path/$tfile
9342                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9343                 pool_remove $POOL $f                    || { rc=$? ; break; }
9344                 break
9345         done
9346
9347         cleanup_pools
9348         return $rc
9349 }
9350 run_test 200 "OST pools"
9351
9352 # usage: default_attr <count | size | offset>
9353 default_attr() {
9354         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9355 }
9356
9357 # usage: check_default_stripe_attr
9358 check_default_stripe_attr() {
9359         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9360         case $1 in
9361         --stripe-count|--count)
9362                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9363         --stripe-size|--size)
9364                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9365         --stripe-index|--index)
9366                 EXPECTED=-1;;
9367         *)
9368                 error "unknown getstripe attr '$1'"
9369         esac
9370
9371         [ $ACTUAL != $EXPECTED ] &&
9372                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9373 }
9374
9375 test_204a() {
9376         test_mkdir -p $DIR/$tdir
9377         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9378
9379         check_default_stripe_attr --stripe-count
9380         check_default_stripe_attr --stripe-size
9381         check_default_stripe_attr --stripe-index
9382
9383         return 0
9384 }
9385 run_test 204a "Print default stripe attributes ================="
9386
9387 test_204b() {
9388         test_mkdir -p $DIR/$tdir
9389         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9390
9391         check_default_stripe_attr --stripe-size
9392         check_default_stripe_attr --stripe-index
9393
9394         return 0
9395 }
9396 run_test 204b "Print default stripe size and offset  ==========="
9397
9398 test_204c() {
9399         test_mkdir -p $DIR/$tdir
9400         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9401
9402         check_default_stripe_attr --stripe-count
9403         check_default_stripe_attr --stripe-index
9404
9405         return 0
9406 }
9407 run_test 204c "Print default stripe count and offset ==========="
9408
9409 test_204d() {
9410         test_mkdir -p $DIR/$tdir
9411         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9412
9413         check_default_stripe_attr --stripe-count
9414         check_default_stripe_attr --stripe-size
9415
9416         return 0
9417 }
9418 run_test 204d "Print default stripe count and size ============="
9419
9420 test_204e() {
9421         test_mkdir -p $DIR/$tdir
9422         $SETSTRIPE -d $DIR/$tdir
9423
9424         check_default_stripe_attr --stripe-count --raw
9425         check_default_stripe_attr --stripe-size --raw
9426         check_default_stripe_attr --stripe-index --raw
9427
9428         return 0
9429 }
9430 run_test 204e "Print raw stripe attributes ================="
9431
9432 test_204f() {
9433         test_mkdir -p $DIR/$tdir
9434         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9435
9436         check_default_stripe_attr --stripe-size --raw
9437         check_default_stripe_attr --stripe-index --raw
9438
9439         return 0
9440 }
9441 run_test 204f "Print raw stripe size and offset  ==========="
9442
9443 test_204g() {
9444         test_mkdir -p $DIR/$tdir
9445         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9446
9447         check_default_stripe_attr --stripe-count --raw
9448         check_default_stripe_attr --stripe-index --raw
9449
9450         return 0
9451 }
9452 run_test 204g "Print raw stripe count and offset ==========="
9453
9454 test_204h() {
9455         test_mkdir -p $DIR/$tdir
9456         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9457
9458         check_default_stripe_attr --stripe-count --raw
9459         check_default_stripe_attr --stripe-size --raw
9460
9461         return 0
9462 }
9463 run_test 204h "Print raw stripe count and size ============="
9464
9465 # Figure out which job scheduler is being used, if any,
9466 # or use a fake one
9467 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9468         JOBENV=SLURM_JOB_ID
9469 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9470         JOBENV=LSB_JOBID
9471 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9472         JOBENV=PBS_JOBID
9473 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9474         JOBENV=LOADL_STEP_ID
9475 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9476         JOBENV=JOB_ID
9477 else
9478         JOBENV=FAKE_JOBID
9479 fi
9480
9481 verify_jobstats() {
9482         local cmd=$1
9483         local target=$2
9484
9485         # clear old jobstats
9486         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9487         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9488
9489         # use a new JobID for this test, or we might see an old one
9490         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9491
9492         JOBVAL=${!JOBENV}
9493         log "Test: $cmd"
9494         log "Using JobID environment variable $JOBENV=$JOBVAL"
9495
9496         if [ $JOBENV = "FAKE_JOBID" ]; then
9497                 FAKE_JOBID=$JOBVAL $cmd
9498         else
9499                 $cmd
9500         fi
9501
9502         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9503                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9504                 do_facet $FACET lctl get_param mdt.*.job_stats |
9505                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9506         fi
9507         if [ "$target" = "ost" -o "$target" = "both" ]; then
9508                 FACET=ost1
9509                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9510                         grep $JOBVAL || error "No job stats found on OST $FACET"
9511         fi
9512 }
9513
9514 test_205() { # Job stats
9515         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9516                 skip "Server doesn't support jobstats" && return 0
9517
9518         local cmd
9519         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9520         if [ $OLD_JOBENV != $JOBENV ]; then
9521                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9522                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9523                         $JOBENV || return 1
9524         fi
9525
9526         # mkdir
9527         cmd="mkdir $DIR/$tfile"
9528         verify_jobstats "$cmd" "mdt"
9529         # rmdir
9530         cmd="rm -fr $DIR/$tfile"
9531         verify_jobstats "$cmd" "mdt"
9532         # mknod
9533         cmd="mknod $DIR/$tfile c 1 3"
9534         verify_jobstats "$cmd" "mdt"
9535         # unlink
9536         cmd="rm -f $DIR/$tfile"
9537         verify_jobstats "$cmd" "mdt"
9538         # open & close
9539         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9540         verify_jobstats "$cmd" "mdt"
9541         # setattr
9542         cmd="touch $DIR/$tfile"
9543         verify_jobstats "$cmd" "both"
9544         # write
9545         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9546         verify_jobstats "$cmd" "ost"
9547         # read
9548         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9549         verify_jobstats "$cmd" "ost"
9550         # truncate
9551         cmd="$TRUNCATE $DIR/$tfile 0"
9552         verify_jobstats "$cmd" "both"
9553         # rename
9554         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9555         verify_jobstats "$cmd" "mdt"
9556
9557         # cleanup
9558         rm -f $DIR/jobstats_test_rename
9559
9560         if [ $OLD_JOBENV != $JOBENV ]; then
9561                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9562                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9563                         $OLD_JOBENV || return 1
9564         fi
9565 }
9566 run_test 205 "Verify job stats"
9567
9568 # LU-1480, LU-1773 and LU-1657
9569 test_206() {
9570         mkdir -p $DIR/$tdir
9571         lfs setstripe -c -1 $DIR/$tdir
9572 #define OBD_FAIL_LOV_INIT 0x1403
9573         $LCTL set_param fail_loc=0xa0001403
9574         $LCTL set_param fail_val=1
9575         touch $DIR/$tdir/$tfile || true
9576 }
9577 run_test 206 "fail lov_init_raid0() doesn't lbug"
9578
9579 test_212() {
9580         size=`date +%s`
9581         size=$((size % 8192 + 1))
9582         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9583         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9584         rm -f $DIR/f212 $DIR/f212.xyz
9585 }
9586 run_test 212 "Sendfile test ============================================"
9587
9588 test_213() {
9589         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9590         cancel_lru_locks osc
9591         lctl set_param fail_loc=0x8000040f
9592         # generate a read lock
9593         cat $DIR/$tfile > /dev/null
9594         # write to the file, it will try to cancel the above read lock.
9595         cat /etc/hosts >> $DIR/$tfile
9596 }
9597 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9598
9599 test_214() { # for bug 20133
9600         test_mkdir -p $DIR/d214p/d214c
9601         for (( i=0; i < 340; i++ )) ; do
9602                 touch $DIR/d214p/d214c/a$i
9603         done
9604
9605         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9606         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
9607         ls $DIR/d214c || error "ls $DIR/d214c failed"
9608         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
9609 }
9610 run_test 214 "hash-indexed directory test - bug 20133"
9611
9612 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
9613 create_lnet_proc_files() {
9614         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
9615         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
9616
9617         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
9618         rm -f "$TMP/lnet_$1.sys_tmp"
9619 }
9620
9621 # counterpart of create_lnet_proc_files
9622 remove_lnet_proc_files() {
9623         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
9624 }
9625
9626 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9627 # 3rd arg as regexp for body
9628 check_lnet_proc_stats() {
9629         local l=$(cat "$TMP/lnet_$1" |wc -l)
9630         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
9631
9632         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
9633 }
9634
9635 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9636 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
9637 # optional and can be regexp for 2nd line (lnet.routes case)
9638 check_lnet_proc_entry() {
9639         local blp=2            # blp stands for 'position of 1st line of body'
9640         [ "$5" = "" ] || blp=3 # lnet.routes case
9641
9642         local l=$(cat "$TMP/lnet_$1" |wc -l)
9643         # subtracting one from $blp because the body can be empty
9644         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
9645
9646         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
9647                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
9648
9649         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
9650                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
9651
9652         # bail out if any unexpected line happened
9653         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
9654         [ "$?" != 0 ] || error "$2 misformatted"
9655 }
9656
9657 test_215() { # for bugs 18102, 21079, 21517
9658         local N='(0|[1-9][0-9]*)'       # non-negative numeric
9659         local P='[1-9][0-9]*'           # positive numeric
9660         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
9661         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
9662         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
9663         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
9664
9665         local L1 # regexp for 1st line
9666         local L2 # regexp for 2nd line (optional)
9667         local BR # regexp for the rest (body)
9668
9669         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
9670         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
9671         create_lnet_proc_files "stats"
9672         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
9673         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
9674         remove_lnet_proc_files "stats"
9675
9676         # /proc/sys/lnet/routes should look like this:
9677         # Routing disabled/enabled
9678         # net hops state router
9679         # where net is a string like tcp0, hops >= 0, state is up/down,
9680         # router is a string like 192.168.1.1@tcp2
9681         L1="^Routing (disabled|enabled)$"
9682         L2="^net +hops +state +router$"
9683         BR="^$NET +$N +(up|down) +$NID$"
9684         create_lnet_proc_files "routes"
9685         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
9686         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
9687         remove_lnet_proc_files "routes"
9688
9689         # /proc/sys/lnet/routers should look like this:
9690         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
9691         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
9692         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
9693         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
9694         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
9695         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
9696         create_lnet_proc_files "routers"
9697         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
9698         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
9699         remove_lnet_proc_files "routers"
9700
9701         # /proc/sys/lnet/peers should look like this:
9702         # nid refs state last max rtr min tx min queue
9703         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
9704         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
9705         # numeric (0 or >0 or <0), queue >= 0.
9706         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
9707         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
9708         create_lnet_proc_files "peers"
9709         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
9710         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
9711         remove_lnet_proc_files "peers"
9712
9713         # /proc/sys/lnet/buffers  should look like this:
9714         # pages count credits min
9715         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
9716         L1="^pages +count +credits +min$"
9717         BR="^ +$N +$N +$I +$I$"
9718         create_lnet_proc_files "buffers"
9719         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
9720         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
9721         remove_lnet_proc_files "buffers"
9722
9723         # /proc/sys/lnet/nis should look like this:
9724         # nid status alive refs peer rtr max tx min
9725         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
9726         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
9727         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
9728         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
9729         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
9730         create_lnet_proc_files "nis"
9731         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
9732         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
9733         remove_lnet_proc_files "nis"
9734
9735         # can we successfully write to /proc/sys/lnet/stats?
9736         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
9737         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
9738 }
9739 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
9740
9741 test_216() { # bug 20317
9742         remote_ost_nodsh && skip "remote OST with nodsh" && return
9743         local node
9744         local p="$TMP/sanityN-$TESTNAME.parameters"
9745         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
9746         for node in $(osts_nodes); do
9747                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
9748                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
9749                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
9750         done
9751         clear_osc_stats
9752
9753         # agressive lockless i/o settings
9754         for node in $(osts_nodes); do
9755                 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'
9756         done
9757         lctl set_param -n osc.*.contention_seconds 60
9758
9759         $DIRECTIO write $DIR/$tfile 0 10 4096
9760         $CHECKSTAT -s 40960 $DIR/$tfile
9761
9762         # disable lockless i/o
9763         for node in $(osts_nodes); do
9764                 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'
9765         done
9766         lctl set_param -n osc.*.contention_seconds 0
9767         clear_osc_stats
9768
9769         dd if=/dev/zero of=$DIR/$tfile count=0
9770         $CHECKSTAT -s 0 $DIR/$tfile
9771
9772         restore_lustre_params <$p
9773         rm -f $p
9774         rm $DIR/$tfile
9775 }
9776 run_test 216 "check lockless direct write works and updates file size and kms correctly"
9777
9778 test_217() { # bug 22430
9779         local node
9780         local nid
9781
9782         for node in $(nodes_list); do
9783                 nid=$(host_nids_address $node $NETTYPE)
9784                 if [[ $nid = *-* ]] ; then
9785                         echo "lctl ping $nid@$NETTYPE"
9786                         lctl ping $nid@$NETTYPE
9787                 else
9788                         echo "skipping $node (no hyphen detected)"
9789                 fi
9790         done
9791 }
9792 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
9793
9794 test_218() {
9795        # do directio so as not to populate the page cache
9796        log "creating a 10 Mb file"
9797        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9798        log "starting reads"
9799        dd if=$DIR/$tfile of=/dev/null bs=4096 &
9800        log "truncating the file"
9801        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9802        log "killing dd"
9803        kill %+ || true # reads might have finished
9804        echo "wait until dd is finished"
9805        wait
9806        log "removing the temporary file"
9807        rm -rf $DIR/$tfile || error "tmp file removal failed"
9808 }
9809 run_test 218 "parallel read and truncate should not deadlock ======================="
9810
9811 test_219() {
9812         # write one partial page
9813         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
9814         # set no grant so vvp_io_commit_write will do sync write
9815         $LCTL set_param fail_loc=0x411
9816         # write a full page at the end of file
9817         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
9818
9819         $LCTL set_param fail_loc=0
9820         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
9821         $LCTL set_param fail_loc=0x411
9822         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
9823 }
9824 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
9825
9826 test_220() { #LU-325
9827         remote_ost_nodsh && skip "remote OST with nodsh" && return
9828         local OSTIDX=0
9829
9830         test_mkdir -p $DIR/$tdir
9831         local OST=$(lfs osts | grep ${OSTIDX}": " | \
9832                 awk '{print $2}' | sed -e 's/_UUID$//')
9833
9834         # on the mdt's osc
9835         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
9836         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
9837                         osc.$mdtosc_proc1.prealloc_last_id)
9838         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
9839                         osc.$mdtosc_proc1.prealloc_next_id)
9840
9841         $LFS df -i
9842
9843         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
9844         #define OBD_FAIL_OST_ENOINO              0x229
9845         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
9846         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
9847         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
9848
9849         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
9850
9851         MDSOBJS=$((last_id - next_id))
9852         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
9853
9854         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
9855         echo "OST still has $count kbytes free"
9856
9857         echo "create $MDSOBJS files @next_id..."
9858         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
9859
9860         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9861                         osc.$mdtosc_proc1.prealloc_last_id)
9862         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9863                         osc.$mdtosc_proc1.prealloc_next_id)
9864
9865         echo "after creation, last_id=$last_id2, next_id=$next_id2"
9866         $LFS df -i
9867
9868         echo "cleanup..."
9869
9870         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
9871         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
9872
9873         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
9874         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
9875         echo "unlink $MDSOBJS files @$next_id..."
9876         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
9877 }
9878 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
9879
9880 test_221() {
9881         dd if=`which date` of=$MOUNT/date oflag=sync
9882         chmod +x $MOUNT/date
9883
9884         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
9885         $LCTL set_param fail_loc=0x80001401
9886
9887         $MOUNT/date > /dev/null
9888         rm -f $MOUNT/date
9889 }
9890 run_test 221 "make sure fault and truncate race to not cause OOM"
9891
9892 test_222a () {
9893        rm -rf $DIR/$tdir
9894        test_mkdir -p $DIR/$tdir
9895        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9896        createmany -o $DIR/$tdir/$tfile 10
9897        cancel_lru_locks mdc
9898        cancel_lru_locks osc
9899        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9900        $LCTL set_param fail_loc=0x31a
9901        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
9902        $LCTL set_param fail_loc=0
9903        rm -r $DIR/$tdir
9904 }
9905 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
9906
9907 test_222b () {
9908        rm -rf $DIR/$tdir
9909        test_mkdir -p $DIR/$tdir
9910        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9911        createmany -o $DIR/$tdir/$tfile 10
9912        cancel_lru_locks mdc
9913        cancel_lru_locks osc
9914        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9915        $LCTL set_param fail_loc=0x31a
9916        rm -r $DIR/$tdir || "AGL for rmdir failed"
9917        $LCTL set_param fail_loc=0
9918 }
9919 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
9920
9921 test_223 () {
9922        rm -rf $DIR/$tdir
9923        test_mkdir -p $DIR/$tdir
9924        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9925        createmany -o $DIR/$tdir/$tfile 10
9926        cancel_lru_locks mdc
9927        cancel_lru_locks osc
9928        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
9929        $LCTL set_param fail_loc=0x31b
9930        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
9931        $LCTL set_param fail_loc=0
9932        rm -r $DIR/$tdir
9933 }
9934 run_test 223 "osc reenqueue if without AGL lock granted ======================="
9935
9936 test_224a() { # LU-1039, MRP-303
9937         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
9938         $LCTL set_param fail_loc=0x508
9939         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
9940         $LCTL set_param fail_loc=0
9941         df $DIR
9942 }
9943 run_test 224a "Don't panic on bulk IO failure"
9944
9945 test_224b() { # LU-1039, MRP-303
9946         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
9947         cancel_lru_locks osc
9948         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
9949         $LCTL set_param fail_loc=0x515
9950         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
9951         $LCTL set_param fail_loc=0
9952         df $DIR
9953 }
9954 run_test 224b "Don't panic on bulk IO failure"
9955
9956 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
9957 test_225a () {
9958        if [ -z ${MDSSURVEY} ]; then
9959               skip_env "mds-survey not found" && return
9960        fi
9961        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9962             { skip "Need MDS version at least 2.2.51"; return; }
9963
9964        local mds=$(facet_host $SINGLEMDS)
9965        local target=$(do_nodes $mds 'lctl dl' | \
9966                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9967
9968        local cmd1="file_count=1000 thrhi=4"
9969        local cmd2="dir_count=2 layer=mdd stripe_count=0"
9970        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9971        local cmd="$cmd1 $cmd2 $cmd3"
9972
9973        rm -f ${TMP}/mds_survey*
9974        echo + $cmd
9975        eval $cmd || error "mds-survey with zero-stripe failed"
9976        cat ${TMP}/mds_survey*
9977        rm -f ${TMP}/mds_survey*
9978 }
9979 run_test 225a "Metadata survey sanity with zero-stripe"
9980
9981 test_225b () {
9982        if [ -z ${MDSSURVEY} ]; then
9983               skip_env "mds-survey not found" && return
9984        fi
9985        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9986             { skip "Need MDS version at least 2.2.51"; return; }
9987
9988        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
9989               skip_env "Need to mount OST to test" && return
9990        fi
9991
9992        local mds=$(facet_host $SINGLEMDS)
9993        local target=$(do_nodes $mds 'lctl dl' | \
9994                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9995
9996        local cmd1="file_count=1000 thrhi=4"
9997        local cmd2="dir_count=2 layer=mdd stripe_count=1"
9998        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9999        local cmd="$cmd1 $cmd2 $cmd3"
10000
10001        rm -f ${TMP}/mds_survey*
10002        echo + $cmd
10003        eval $cmd || error "mds-survey with stripe_count failed"
10004        cat ${TMP}/mds_survey*
10005        rm -f ${TMP}/mds_survey*
10006 }
10007 run_test 225b "Metadata survey sanity with stripe_count = 1"
10008
10009 mcreate_path2fid () {
10010         local mode=$1
10011         local major=$2
10012         local minor=$3
10013         local name=$4
10014         local desc=$5
10015         local path=$DIR/$tdir/$name
10016         local fid
10017         local rc
10018         local fid_path
10019
10020         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
10021                 error "cannot create $desc"
10022
10023         fid=$($LFS path2fid $path)
10024         rc=$?
10025         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
10026
10027         fid_path=$($LFS fid2path $DIR "$fid")
10028         rc=$?
10029         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
10030
10031         [ "$path" == "$fid_path" ] ||
10032                 error "fid2path returned $fid_path, expected $path"
10033
10034         echo "pass with $path and $fid"
10035 }
10036
10037 test_226 () {
10038         rm -rf $DIR/$tdir
10039         mkdir -p $DIR/$tdir
10040
10041         mcreate_path2fid 0010666 0 0 fifo "FIFO"
10042         mcreate_path2fid 0020666 1 3 null "character special file (null)"
10043         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
10044         mcreate_path2fid 0040666 0 0 dir "directory"
10045         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
10046         mcreate_path2fid 0100666 0 0 file "regular file"
10047         mcreate_path2fid 0120666 0 0 link "symbolic link"
10048         mcreate_path2fid 0140666 0 0 sock "socket"
10049 }
10050 run_test 226 "call path2fid and fid2path on files of all type"
10051
10052 # LU-1299 Executing or running ldd on a truncated executable does not
10053 # cause an out-of-memory condition.
10054 test_227() {
10055         dd if=`which date` of=$MOUNT/date bs=1k count=1
10056         chmod +x $MOUNT/date
10057
10058         $MOUNT/date > /dev/null
10059         ldd $MOUNT/date > /dev/null
10060         rm -f $MOUNT/date
10061 }
10062 run_test 227 "running truncated executable does not cause OOM"
10063
10064 # LU-1512 try to reuse idle OI blocks
10065 test_228a() {
10066         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10067                 skip "non-ldiskfs backend" && return
10068
10069         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10070         local myDIR=$DIR/$tdir
10071
10072         mkdir -p $myDIR
10073         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10074         $LCTL set_param fail_loc=0x80001002
10075         createmany -o $myDIR/t- 10000
10076         $LCTL set_param fail_loc=0
10077         # The guard is current the largest FID holder
10078         touch $myDIR/guard
10079         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10080                     tr -d '[')
10081         local IDX=$(($SEQ % 64))
10082
10083         do_facet $SINGLEMDS sync
10084         # Make sure journal flushed.
10085         sleep 6
10086         local blk1=$(do_facet $SINGLEMDS \
10087                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10088                      grep Blockcount | awk '{print $4}')
10089
10090         # Remove old files, some OI blocks will become idle.
10091         unlinkmany $myDIR/t- 10000
10092         # Create new files, idle OI blocks should be reused.
10093         createmany -o $myDIR/t- 2000
10094         do_facet $SINGLEMDS sync
10095         # Make sure journal flushed.
10096         sleep 6
10097         local blk2=$(do_facet $SINGLEMDS \
10098                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10099                      grep Blockcount | awk '{print $4}')
10100
10101         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10102 }
10103 run_test 228a "try to reuse idle OI blocks"
10104
10105 test_228b() {
10106         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10107                 skip "non-ldiskfs backend" && return
10108
10109         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10110         local myDIR=$DIR/$tdir
10111
10112         mkdir -p $myDIR
10113         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10114         $LCTL set_param fail_loc=0x80001002
10115         createmany -o $myDIR/t- 10000
10116         $LCTL set_param fail_loc=0
10117         # The guard is current the largest FID holder
10118         touch $myDIR/guard
10119         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10120                     tr -d '[')
10121         local IDX=$(($SEQ % 64))
10122
10123         do_facet $SINGLEMDS sync
10124         # Make sure journal flushed.
10125         sleep 6
10126         local blk1=$(do_facet $SINGLEMDS \
10127                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10128                      grep Blockcount | awk '{print $4}')
10129
10130         # Remove old files, some OI blocks will become idle.
10131         unlinkmany $myDIR/t- 10000
10132
10133         # stop the MDT
10134         stop $SINGLEMDS || error "Fail to stop MDT."
10135         # remount the MDT
10136         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10137
10138         df $MOUNT || error "Fail to df."
10139         # Create new files, idle OI blocks should be reused.
10140         createmany -o $myDIR/t- 2000
10141         do_facet $SINGLEMDS sync
10142         # Make sure journal flushed.
10143         sleep 6
10144         local blk2=$(do_facet $SINGLEMDS \
10145                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10146                      grep Blockcount | awk '{print $4}')
10147
10148         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10149 }
10150 run_test 228b "idle OI blocks can be reused after MDT restart"
10151
10152 #LU-1881
10153 test_228c() {
10154         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
10155                 skip "non-ldiskfs backend" && return
10156
10157         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10158         local myDIR=$DIR/$tdir
10159
10160         mkdir -p $myDIR
10161         #define OBD_FAIL_SEQ_EXHAUST             0x1002
10162         $LCTL set_param fail_loc=0x80001002
10163         # 20000 files can guarantee there are index nodes in the OI file
10164         createmany -o $myDIR/t- 20000
10165         $LCTL set_param fail_loc=0
10166         # The guard is current the largest FID holder
10167         touch $myDIR/guard
10168         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
10169                     tr -d '[')
10170         local IDX=$(($SEQ % 64))
10171
10172         do_facet $SINGLEMDS sync
10173         # Make sure journal flushed.
10174         sleep 6
10175         local blk1=$(do_facet $SINGLEMDS \
10176                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10177                      grep Blockcount | awk '{print $4}')
10178
10179         # Remove old files, some OI blocks will become idle.
10180         unlinkmany $myDIR/t- 20000
10181         rm -f $myDIR/guard
10182         # The OI file should become empty now
10183
10184         # Create new files, idle OI blocks should be reused.
10185         createmany -o $myDIR/t- 2000
10186         do_facet $SINGLEMDS sync
10187         # Make sure journal flushed.
10188         sleep 6
10189         local blk2=$(do_facet $SINGLEMDS \
10190                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
10191                      grep Blockcount | awk '{print $4}')
10192
10193         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
10194 }
10195 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
10196
10197 test_230a() {
10198         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10199         local MDTIDX=1
10200
10201         mkdir -p $DIR/$tdir/test_230_local
10202         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
10203         [ $mdt_idx -ne 0 ] &&
10204                 error "create local directory on wrong MDT $mdt_idx"
10205
10206         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
10207                         error "create remote directory failed"
10208         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
10209         [ $mdt_idx -ne $MDTIDX ] &&
10210                 error "create remote directory on wrong MDT $mdt_idx"
10211
10212         createmany -o $DIR/$tdir/test_230/t- 10 ||
10213                 error "create files on remote directory failed"
10214         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
10215         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
10216         rm -r $DIR/$tdir || error "unlink remote directory failed"
10217 }
10218 run_test 230a "Create remote directory and files under the remote directory"
10219
10220 test_230b() {
10221         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10222         local MDTIDX=1
10223         local remote_dir=$DIR/$tdir/remote_dir
10224
10225         mkdir -p $DIR/$tdir
10226         $LFS mkdir -i $MDTIDX $remote_dir ||
10227                 error "create remote directory failed"
10228
10229         $LFS mkdir -i 0 $remote_dir/new_dir &&
10230                 error "nested remote directory create succeed!"
10231
10232         rm -r $DIR/$tdir || error "unlink remote directory failed"
10233 }
10234 run_test 230b "nested remote directory should be failed"
10235
10236 #
10237 # tests that do cleanup/setup should be run at the end
10238 #
10239
10240 test_900() {
10241         local ls
10242         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
10243         $LCTL set_param fail_loc=0x903
10244         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
10245         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
10246                 echo "clear" > $ls
10247         done
10248         FAIL_ON_ERROR=true cleanup
10249         FAIL_ON_ERROR=true setup
10250 }
10251 run_test 900 "umount should not race with any mgc requeue thread"
10252
10253 complete $SECONDS
10254 check_and_cleanup_lustre
10255 if [ "$I_MOUNTED" != "yes" ]; then
10256         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
10257 fi
10258 exit_status