Whamcloud - gitweb
6ff54ce10d8f5656eb35506897f5790de9329bcb
[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         mkdir $DIR/d1
159         mkdir $DIR/d1/d2
160         mkdir $DIR/d1/d2 && error "we expect EEXIST, but not returned"
161         $CHECKSTAT -t dir $DIR/d1/d2 || error
162 }
163 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
164
165 test_1b() {
166         rmdir $DIR/d1/d2
167         rmdir $DIR/d1
168         $CHECKSTAT -a $DIR/d1 || error
169 }
170 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
171
172 test_2a() {
173         mkdir $DIR/d2
174         touch $DIR/d2/f
175         $CHECKSTAT -t file $DIR/d2/f || error
176 }
177 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
178
179 test_2b() {
180         rm -r $DIR/d2
181         $CHECKSTAT -a $DIR/d2 || error
182 }
183 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
184
185 test_3a() {
186         mkdir $DIR/d3
187         $CHECKSTAT -t dir $DIR/d3 || error
188 }
189 run_test 3a "mkdir .../d3 ======================================"
190
191 test_3b() {
192         if [ ! -d $DIR/d3 ]; then
193                 mkdir $DIR/d3
194         fi
195         touch $DIR/d3/f
196         $CHECKSTAT -t file $DIR/d3/f || error
197 }
198 run_test 3b "touch .../d3/f ===================================="
199
200 test_3c() {
201         rm -r $DIR/d3
202         $CHECKSTAT -a $DIR/d3 || error
203 }
204 run_test 3c "rm -r .../d3 ======================================"
205
206 test_4a() {
207         mkdir $DIR/d4
208         $CHECKSTAT -t dir $DIR/d4 || error
209 }
210 run_test 4a "mkdir .../d4 ======================================"
211
212 test_4b() {
213         if [ ! -d $DIR/d4 ]; then
214                 mkdir $DIR/d4
215         fi
216         mkdir $DIR/d4/d2
217         $CHECKSTAT -t dir $DIR/d4/d2 || error
218 }
219 run_test 4b "mkdir .../d4/d2 ==================================="
220
221 test_5() {
222         mkdir $DIR/d5
223         mkdir $DIR/d5/d2
224         chmod 0707 $DIR/d5/d2
225         $CHECKSTAT -t dir -p 0707 $DIR/d5/d2 || error
226 }
227 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
228
229 test_6a() {
230         touch $DIR/f6a
231         chmod 0666 $DIR/f6a || error
232         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
233 }
234 run_test 6a "touch .../f6a; chmod .../f6a ======================"
235
236 test_6b() {
237         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
238         if [ ! -f $DIR/f6a ]; then
239                 touch $DIR/f6a
240                 chmod 0666 $DIR/f6a
241         fi
242         $RUNAS chmod 0444 $DIR/f6a && error
243         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
244 }
245 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
246
247 test_6c() {
248         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
249         touch $DIR/f6c
250         chown $RUNAS_ID $DIR/f6c || error
251         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
252 }
253 run_test 6c "touch .../f6c; chown .../f6c ======================"
254
255 test_6d() {
256         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
257         if [ ! -f $DIR/f6c ]; then
258                 touch $DIR/f6c
259                 chown $RUNAS_ID $DIR/f6c
260         fi
261         $RUNAS chown $UID $DIR/f6c && error
262         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
263 }
264 run_test 6d "$RUNAS chown .../f6c (should return error) =="
265
266 test_6e() {
267         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
268         touch $DIR/f6e
269         chgrp $RUNAS_ID $DIR/f6e || error
270         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
271 }
272 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
273
274 test_6f() {
275         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
276         if [ ! -f $DIR/f6e ]; then
277                 touch $DIR/f6e
278                 chgrp $RUNAS_ID $DIR/f6e
279         fi
280         $RUNAS chgrp $UID $DIR/f6e && error
281         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
282 }
283 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
284
285 test_6g() {
286         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
287         mkdir $DIR/d6g || error
288         chmod 777 $DIR/d6g || error
289         $RUNAS mkdir $DIR/d6g/d || error
290         chmod g+s $DIR/d6g/d || error
291         mkdir $DIR/d6g/d/subdir
292         $CHECKSTAT -g \#$RUNAS_GID $DIR/d6g/d/subdir || error
293 }
294 run_test 6g "Is new dir in sgid dir inheriting group?"
295
296 test_6h() { # bug 7331
297         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
298         touch $DIR/f6h || error "touch failed"
299         chown $RUNAS_ID:$RUNAS_GID $DIR/f6h || error "initial chown failed"
300         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/f6h && error "chown worked"
301         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/f6h || error
302 }
303 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
304
305 test_7a() {
306         mkdir $DIR/d7
307         $MCREATE $DIR/d7/f
308         chmod 0666 $DIR/d7/f
309         $CHECKSTAT -t file -p 0666 $DIR/d7/f || error
310 }
311 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
312
313 test_7b() {
314         if [ ! -d $DIR/d7 ]; then
315                 mkdir $DIR/d7
316         fi
317         $MCREATE $DIR/d7/f2
318         echo -n foo > $DIR/d7/f2
319         [ "`cat $DIR/d7/f2`" = "foo" ] || error
320         $CHECKSTAT -t file -s 3 $DIR/d7/f2 || error
321 }
322 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
323
324 test_8() {
325         mkdir $DIR/d8
326         touch $DIR/d8/f
327         chmod 0666 $DIR/d8/f
328         $CHECKSTAT -t file -p 0666 $DIR/d8/f || error
329 }
330 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
331
332 test_9() {
333         mkdir $DIR/d9
334         mkdir $DIR/d9/d2
335         mkdir $DIR/d9/d2/d3
336         $CHECKSTAT -t dir $DIR/d9/d2/d3 || error
337 }
338 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
339
340 test_10() {
341         mkdir $DIR/d10
342         mkdir $DIR/d10/d2
343         touch $DIR/d10/d2/f
344         $CHECKSTAT -t file $DIR/d10/d2/f || error
345 }
346 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
347
348 test_11() {
349         mkdir $DIR/d11
350         mkdir $DIR/d11/d2
351         chmod 0666 $DIR/d11/d2
352         chmod 0705 $DIR/d11/d2
353         $CHECKSTAT -t dir -p 0705 $DIR/d11/d2 || error
354 }
355 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
356
357 test_12() {
358         mkdir $DIR/d12
359         touch $DIR/d12/f
360         chmod 0666 $DIR/d12/f
361         chmod 0654 $DIR/d12/f
362         $CHECKSTAT -t file -p 0654 $DIR/d12/f || error
363 }
364 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
365
366 test_13() {
367         mkdir $DIR/d13
368         dd if=/dev/zero of=$DIR/d13/f count=10
369         >  $DIR/d13/f
370         $CHECKSTAT -t file -s 0 $DIR/d13/f || error
371 }
372 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
373
374 test_14() {
375         mkdir $DIR/d14
376         touch $DIR/d14/f
377         rm $DIR/d14/f
378         $CHECKSTAT -a $DIR/d14/f || error
379 }
380 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
381
382 test_15() {
383         mkdir $DIR/d15
384         touch $DIR/d15/f
385         mv $DIR/d15/f $DIR/d15/f2
386         $CHECKSTAT -t file $DIR/d15/f2 || error
387 }
388 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
389
390 test_16() {
391         mkdir $DIR/d16
392         touch $DIR/d16/f
393         rm -rf $DIR/d16/f
394         $CHECKSTAT -a $DIR/d16/f || error
395 }
396 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
397
398 test_17a() {
399         mkdir -p $DIR/d17
400         touch $DIR/d17/f
401         ln -s $DIR/d17/f $DIR/d17/l-exist
402         ls -l $DIR/d17
403         $CHECKSTAT -l $DIR/d17/f $DIR/d17/l-exist || error
404         $CHECKSTAT -f -t f $DIR/d17/l-exist || error
405         rm -f $DIR/d17/l-exist
406         $CHECKSTAT -a $DIR/d17/l-exist || error
407 }
408 run_test 17a "symlinks: create, remove (real) =================="
409
410 test_17b() {
411         mkdir -p $DIR/d17
412         ln -s no-such-file $DIR/d17/l-dangle
413         ls -l $DIR/d17
414         $CHECKSTAT -l no-such-file $DIR/d17/l-dangle || error
415         $CHECKSTAT -fa $DIR/d17/l-dangle || error
416         rm -f $DIR/d17/l-dangle
417         $CHECKSTAT -a $DIR/d17/l-dangle || error
418 }
419 run_test 17b "symlinks: create, remove (dangling) =============="
420
421 test_17c() { # bug 3440 - don't save failed open RPC for replay
422         mkdir -p $DIR/d17
423         ln -s foo $DIR/d17/f17c
424         cat $DIR/d17/f17c && error "opened non-existent symlink" || true
425 }
426 run_test 17c "symlinks: open dangling (should return error) ===="
427
428 test_17d() {
429         mkdir -p $DIR/d17
430         ln -s foo $DIR/d17/f17d
431         touch $DIR/d17/f17d || error "creating to new symlink"
432 }
433 run_test 17d "symlinks: create dangling ========================"
434
435 test_17e() {
436         mkdir -p $DIR/$tdir
437         local foo=$DIR/$tdir/$tfile
438         ln -s $foo $foo || error "create symlink failed"
439         ls -l $foo || error "ls -l failed"
440         ls $foo && error "ls not failed" || true
441 }
442 run_test 17e "symlinks: create recursive symlink (should return error) ===="
443
444 test_17f() {
445         mkdir -p $DIR/d17f
446         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
447         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
448         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
449         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
451         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
452         ls -l  $DIR/d17f
453 }
454 run_test 17f "symlinks: long and very long symlink name ========================"
455
456 # str_repeat(S, N) generate a string that is string S repeated N times
457 str_repeat() {
458         local s=$1
459         local n=$2
460         local ret=''
461         while [ $((n -= 1)) -ge 0 ]; do
462                 ret=$ret$s
463         done
464         echo $ret
465 }
466
467 # Long symlinks and LU-2241
468 test_17g() {
469         mkdir -p $DIR/$tdir
470         local TESTS="59 60 61 4094 4095"
471
472         for i in $TESTS; do
473                 local SYMNAME=$(str_repeat 'x' $i)
474                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
475                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
476         done
477 }
478 run_test 17g "symlinks: really long symlink name and inode boundaries"
479
480 test_17h() { #bug 17378
481         remote_mds_nodsh && skip "remote MDS with nodsh" && return
482         mkdir -p $DIR/$tdir
483         $SETSTRIPE -c -1 $DIR/$tdir
484 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
485         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000141
486         touch $DIR/$tdir/$tfile || true
487 }
488 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
489
490 test_17i() { #bug 20018
491         remote_mds_nodsh && skip "remote MDS with nodsh" && return
492         mkdir -p $DIR/$tdir
493         local foo=$DIR/$tdir/$tfile
494         ln -s $foo $foo || error "create symlink failed"
495 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
496         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000143
497         ls -l $foo && error "error not detected"
498         return 0
499 }
500 run_test 17i "don't panic on short symlink"
501
502 test_17k() { #bug 22301
503         rsync --help | grep -q xattr ||
504                 skip_env "$(rsync --version| head -1) does not support xattrs"
505         mkdir -p $DIR/{$tdir,$tdir.new}
506         touch $DIR/$tdir/$tfile
507         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
508         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
509                 error "rsync failed with xattrs enabled"
510 }
511 run_test 17k "symlinks: rsync with xattrs enabled ========================="
512
513 test_17l() { # LU-279
514         mkdir -p $DIR/$tdir
515         touch $DIR/$tdir/$tfile
516         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
517         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
518                 # -h to not follow symlinks. -m '' to list all the xattrs.
519                 # grep to remove first line: '# file: $path'.
520                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
521                 do
522                         lgetxattr_size_check $path $xattr ||
523                                 error "lgetxattr_size_check $path $xattr failed"
524                 done
525         done
526 }
527 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
528
529 # LU-1540
530 test_17m() {
531         local short_sym="0123456789"
532         local WDIR=$DIR/${tdir}m
533         local mds_index
534         local devname
535         local cmd
536         local i
537         local rc=0
538
539         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
540         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
541                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
542
543         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
544                 skip "only for ldiskfs MDT" && return 0
545
546         mkdir -p $WDIR
547         long_sym=$short_sym
548         # create a long symlink file
549         for ((i = 0; i < 4; ++i)); do
550                 long_sym=${long_sym}${long_sym}
551         done
552
553         echo "create 512 short and long symlink files under $WDIR"
554         for ((i = 0; i < 256; ++i)); do
555                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
556                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
557         done
558
559         echo "erase them"
560         rm -f $WDIR/*
561         sync
562         wait_delete_completed
563
564         echo "recreate the 512 symlink files with a shorter string"
565         for ((i = 0; i < 512; ++i)); do
566                 # rewrite the symlink file with a shorter string
567                 ln -sf ${long_sym} $WDIR/long-$i
568                 ln -sf ${short_sym} $WDIR/short-$i
569         done
570
571         mds_index=$($LFS getstripe -M $WDIR)
572         mds_index=$((mds_index+1))
573         devname=$(mdsdevname $mds_index)
574         cmd="$E2FSCK -fnvd $devname"
575
576         echo "stop and checking mds${mds_index}: $cmd"
577         # e2fsck should not return error
578         stop mds${mds_index} -f
579         do_facet mds${mds_index} $cmd || rc=$?
580
581         start mds${mds_index} $devname $MDS_MOUNT_OPTS
582         df $MOUNT > /dev/null 2>&1
583         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
584                 "short/long symlink MDT: rc=$rc"
585         return $rc
586 }
587 run_test 17m "run e2fsck against MDT which contains short/long symlink"
588
589 test_18() {
590         touch $DIR/f
591         ls $DIR || error
592 }
593 run_test 18 "touch .../f ; ls ... =============================="
594
595 test_19a() {
596         touch $DIR/f19
597         ls -l $DIR
598         rm $DIR/f19
599         $CHECKSTAT -a $DIR/f19 || error
600 }
601 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
602
603 test_19b() {
604         ls -l $DIR/f19 && error || true
605 }
606 run_test 19b "ls -l .../f19 (should return error) =============="
607
608 test_19c() {
609         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
610         $RUNAS touch $DIR/f19 && error || true
611 }
612 run_test 19c "$RUNAS touch .../f19 (should return error) =="
613
614 test_19d() {
615         cat $DIR/f19 && error || true
616 }
617 run_test 19d "cat .../f19 (should return error) =============="
618
619 test_20() {
620         touch $DIR/f
621         rm $DIR/f
622         log "1 done"
623         touch $DIR/f
624         rm $DIR/f
625         log "2 done"
626         touch $DIR/f
627         rm $DIR/f
628         log "3 done"
629         $CHECKSTAT -a $DIR/f || error
630 }
631 run_test 20 "touch .../f ; ls -l ... ==========================="
632
633 test_21() {
634         mkdir $DIR/d21
635         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
636         ln -s dangle $DIR/d21/link
637         echo foo >> $DIR/d21/link
638         cat $DIR/d21/dangle
639         $CHECKSTAT -t link $DIR/d21/link || error
640         $CHECKSTAT -f -t file $DIR/d21/link || error
641 }
642 run_test 21 "write to dangling link ============================"
643
644 test_22() {
645         WDIR=$DIR/$tdir
646         mkdir -p $WDIR
647         chown $RUNAS_ID:$RUNAS_GID $WDIR
648         (cd $WDIR || error "cd $WDIR failed";
649         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
650         $RUNAS tar xf -)
651         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
652         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
653         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
654 }
655 run_test 22 "unpack tar archive as non-root user ==============="
656
657 # was test_23
658 test_23a() {
659         mkdir -p $DIR/$tdir
660         local file=$DIR/$tdir/$tfile
661
662         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
663         openfile -f O_CREAT:O_EXCL $file &&
664                 error "$file recreate succeeded" || true
665 }
666 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
667
668 test_23b() { # bug 18988
669         mkdir -p $DIR/$tdir
670         local file=$DIR/$tdir/$tfile
671
672         rm -f $file
673         echo foo > $file || error "write filed"
674         echo bar >> $file || error "append filed"
675         $CHECKSTAT -s 8 $file || error "wrong size"
676         rm $file
677 }
678 run_test 23b "O_APPEND check =========================="
679
680 test_24a() {
681         echo '== rename sanity =============================================='
682         echo '-- same directory rename'
683         mkdir $DIR/R1
684         touch $DIR/R1/f
685         mv $DIR/R1/f $DIR/R1/g
686         $CHECKSTAT -t file $DIR/R1/g || error
687 }
688 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
689
690 test_24b() {
691         mkdir $DIR/R2
692         touch $DIR/R2/{f,g}
693         mv $DIR/R2/f $DIR/R2/g
694         $CHECKSTAT -a $DIR/R2/f || error
695         $CHECKSTAT -t file $DIR/R2/g || error
696 }
697 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
698
699 test_24c() {
700         mkdir $DIR/R3
701         mkdir $DIR/R3/f
702         mv $DIR/R3/f $DIR/R3/g
703         $CHECKSTAT -a $DIR/R3/f || error
704         $CHECKSTAT -t dir $DIR/R3/g || error
705 }
706 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
707
708 test_24d() {
709         mkdir $DIR/R4
710         mkdir $DIR/R4/{f,g}
711         mrename $DIR/R4/f $DIR/R4/g
712         $CHECKSTAT -a $DIR/R4/f || error
713         $CHECKSTAT -t dir $DIR/R4/g || error
714 }
715 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
716
717 test_24e() {
718         echo '-- cross directory renames --'
719         mkdir $DIR/R5{a,b}
720         touch $DIR/R5a/f
721         mv $DIR/R5a/f $DIR/R5b/g
722         $CHECKSTAT -a $DIR/R5a/f || error
723         $CHECKSTAT -t file $DIR/R5b/g || error
724 }
725 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
726
727 test_24f() {
728         mkdir $DIR/R6{a,b}
729         touch $DIR/R6a/f $DIR/R6b/g
730         mv $DIR/R6a/f $DIR/R6b/g
731         $CHECKSTAT -a $DIR/R6a/f || error
732         $CHECKSTAT -t file $DIR/R6b/g || error
733 }
734 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
735
736 test_24g() {
737         mkdir $DIR/R7{a,b}
738         mkdir $DIR/R7a/d
739         mv $DIR/R7a/d $DIR/R7b/e
740         $CHECKSTAT -a $DIR/R7a/d || error
741         $CHECKSTAT -t dir $DIR/R7b/e || error
742 }
743 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
744
745 test_24h() {
746         mkdir $DIR/R8{a,b}
747         mkdir $DIR/R8a/d $DIR/R8b/e
748         mrename $DIR/R8a/d $DIR/R8b/e
749         $CHECKSTAT -a $DIR/R8a/d || error
750         $CHECKSTAT -t dir $DIR/R8b/e || error
751 }
752 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
753
754 test_24i() {
755         echo "-- rename error cases"
756         mkdir $DIR/R9
757         mkdir $DIR/R9/a
758         touch $DIR/R9/f
759         mrename $DIR/R9/f $DIR/R9/a
760         $CHECKSTAT -t file $DIR/R9/f || error
761         $CHECKSTAT -t dir  $DIR/R9/a || error
762         $CHECKSTAT -a $DIR/R9/a/f || error
763 }
764 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
765
766 test_24j() {
767         mkdir $DIR/R10
768         mrename $DIR/R10/f $DIR/R10/g
769         $CHECKSTAT -t dir $DIR/R10 || error
770         $CHECKSTAT -a $DIR/R10/f || error
771         $CHECKSTAT -a $DIR/R10/g || error
772 }
773 run_test 24j "source does not exist ============================"
774
775 test_24k() {
776         mkdir $DIR/R11a $DIR/R11a/d
777         touch $DIR/R11a/f
778         mv $DIR/R11a/f $DIR/R11a/d
779         $CHECKSTAT -a $DIR/R11a/f || error
780         $CHECKSTAT -t file $DIR/R11a/d/f || error
781 }
782 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
783
784 # bug 2429 - rename foo foo foo creates invalid file
785 test_24l() {
786         f="$DIR/f24l"
787         $MULTIOP $f OcNs || error
788 }
789 run_test 24l "Renaming a file to itself ========================"
790
791 test_24m() {
792         f="$DIR/f24m"
793         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
794         # on ext3 this does not remove either the source or target files
795         # though the "expected" operation would be to remove the source
796         $CHECKSTAT -t file ${f} || error "${f} missing"
797         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
798 }
799 run_test 24m "Renaming a file to a hard link to itself ========="
800
801 test_24n() {
802     f="$DIR/f24n"
803     # this stats the old file after it was renamed, so it should fail
804     touch ${f}
805     $CHECKSTAT ${f}
806     mv ${f} ${f}.rename
807     $CHECKSTAT ${f}.rename
808     $CHECKSTAT -a ${f}
809 }
810 run_test 24n "Statting the old file after renaming (Posix rename 2)"
811
812 test_24o() {
813         check_kernel_version 37 || return 0
814         mkdir -p $DIR/d24o
815         rename_many -s random -v -n 10 $DIR/d24o
816 }
817 run_test 24o "rename of files during htree split ==============="
818
819 test_24p() {
820         mkdir $DIR/R12{a,b}
821         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
822         mrename $DIR/R12a $DIR/R12b
823         $CHECKSTAT -a $DIR/R12a || error
824         $CHECKSTAT -t dir $DIR/R12b || error
825         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
826         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
827 }
828 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
829
830 test_24q() {
831         mkdir $DIR/R13{a,b}
832         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
833         multiop_bg_pause $DIR/R13b D_c || return 1
834         MULTIPID=$!
835
836         mrename $DIR/R13a $DIR/R13b
837         $CHECKSTAT -a $DIR/R13a || error
838         $CHECKSTAT -t dir $DIR/R13b || error
839         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
840         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
841         kill -USR1 $MULTIPID
842         wait $MULTIPID || error "multiop close failed"
843 }
844 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
845
846 test_24r() { #bug 3789
847         mkdir $DIR/R14a $DIR/R14a/b
848         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
849         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
850         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
851 }
852 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
853
854 test_24s() {
855         mkdir $DIR/R15a $DIR/R15a/b $DIR/R15a/b/c
856         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
857         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
858         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
859 }
860 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
861 test_24t() {
862         mkdir $DIR/R16a $DIR/R16a/b $DIR/R16a/b/c
863         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
864         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
865         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
866 }
867 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
868
869 test_24u() { # bug12192
870         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
871         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
872 }
873 run_test 24u "create stripe file"
874
875 page_size() {
876         getconf PAGE_SIZE
877 }
878
879 simple_cleanup_common() {
880         trap 0
881         rm -rf $DIR/$tdir
882         wait_delete_completed
883 }
884
885 test_24v() {
886         local NRFILES=100000
887         local FREE_INODES=$(lfs_df -i | grep "summary" | awk '{print $4}')
888         [ $FREE_INODES -lt $NRFILES ] && \
889                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
890                 return
891
892         trap simple_cleanup_common EXIT
893
894         mkdir -p $DIR/$tdir
895         createmany -m $DIR/$tdir/$tfile $NRFILES
896
897         cancel_lru_locks mdc
898         lctl set_param mdc.*.stats clear
899
900         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
901
902         # LU-5 large readdir
903         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
904         #               8 bytes for name(filename is mostly 5 in this test) +
905         #               8 bytes for luda_type
906         # take into account of overhead in lu_dirpage header and end mark in
907         # each page, plus one in RPC_NUM calculation.
908         DIRENT_SIZE=48
909         RPC_SIZE=$(($(lctl get_param -n mdc.*.max_pages_per_rpc)*$(page_size)))
910         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
911         mds_readpage=`lctl get_param mdc.*.stats | \
912                                 awk '/^mds_readpage/ {print $2}'`
913         [ $mds_readpage -gt $RPC_NUM ] && \
914                 error "large readdir doesn't take effect"
915
916         simple_cleanup_common
917 }
918 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
919
920 test_24w() { # bug21506
921         SZ1=234852
922         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
923         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
924         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
925         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
926         [ "$SZ1" = "$SZ2" ] || \
927                 error "Error reading at the end of the file $tfile"
928 }
929 run_test 24w "Reading a file larger than 4Gb"
930
931 test_25a() {
932         echo '== symlink sanity ============================================='
933
934         mkdir $DIR/d25
935         ln -s d25 $DIR/s25
936         touch $DIR/s25/foo || error
937 }
938 run_test 25a "create file in symlinked directory ==============="
939
940 test_25b() {
941         [ ! -d $DIR/d25 ] && test_25a
942         $CHECKSTAT -t file $DIR/s25/foo || error
943 }
944 run_test 25b "lookup file in symlinked directory ==============="
945
946 test_26a() {
947         mkdir $DIR/d26
948         mkdir $DIR/d26/d26-2
949         ln -s d26/d26-2 $DIR/s26
950         touch $DIR/s26/foo || error
951 }
952 run_test 26a "multiple component symlink ======================="
953
954 test_26b() {
955         mkdir -p $DIR/d26b/d26-2
956         ln -s d26b/d26-2/foo $DIR/s26-2
957         touch $DIR/s26-2 || error
958 }
959 run_test 26b "multiple component symlink at end of lookup ======"
960
961 test_26c() {
962         mkdir $DIR/d26.2
963         touch $DIR/d26.2/foo
964         ln -s d26.2 $DIR/s26.2-1
965         ln -s s26.2-1 $DIR/s26.2-2
966         ln -s s26.2-2 $DIR/s26.2-3
967         chmod 0666 $DIR/s26.2-3/foo
968 }
969 run_test 26c "chain of symlinks ================================"
970
971 # recursive symlinks (bug 439)
972 test_26d() {
973         ln -s d26-3/foo $DIR/d26-3
974 }
975 run_test 26d "create multiple component recursive symlink ======"
976
977 test_26e() {
978         [ ! -h $DIR/d26-3 ] && test_26d
979         rm $DIR/d26-3
980 }
981 run_test 26e "unlink multiple component recursive symlink ======"
982
983 # recursive symlinks (bug 7022)
984 test_26f() {
985         mkdir -p $DIR/$tdir
986         mkdir $DIR/$tdir/$tfile        || error "mkdir $DIR/$tdir/$tfile failed"
987         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
988         mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
989         mkdir $tfile             || error "mkdir $tfile failed"
990         cd $tfile                || error "cd $tfile failed"
991         ln -s .. dotdot          || error "ln dotdot failed"
992         ln -s dotdot/lndir lndir || error "ln lndir failed"
993         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
994         output=`ls $tfile/$tfile/lndir/bar1`
995         [ "$output" = bar1 ] && error "unexpected output"
996         rm -r $tfile             || error "rm $tfile failed"
997         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
998 }
999 run_test 26f "rm -r of a directory which has recursive symlink ="
1000
1001 test_27a() {
1002         echo '== stripe sanity =============================================='
1003         mkdir -p $DIR/d27 || error "mkdir failed"
1004         $GETSTRIPE $DIR/d27
1005         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1006         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1007         pass
1008         log "== test_27a: write to one stripe file ========================="
1009         cp /etc/hosts $DIR/d27/f0 || error
1010 }
1011 run_test 27a "one stripe file =================================="
1012
1013 test_27b() {
1014         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
1015         mkdir -p $DIR/d27
1016         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1017         $GETSTRIPE -c $DIR/d27/f01
1018         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1019                 error "two-stripe file doesn't have two stripes"
1020 }
1021 run_test 27b "create two stripe file"
1022
1023 test_27c() {
1024         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
1025
1026         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1027 }
1028 run_test 27c "write to two stripe file"
1029
1030 test_27d() {
1031         mkdir -p $DIR/d27
1032         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1033         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1034         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1035 }
1036 run_test 27d "create file with default settings ================"
1037
1038 test_27e() {
1039         mkdir -p $DIR/d27
1040         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1041         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1042         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1043 }
1044 run_test 27e "setstripe existing file (should return error) ======"
1045
1046 test_27f() {
1047         mkdir -p $DIR/d27
1048         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1049         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1050         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1051 }
1052 run_test 27f "setstripe with bad stripe size (should return error)"
1053
1054 test_27g() {
1055         mkdir -p $DIR/d27
1056         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1057         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1058                 error "$DIR/d27/fnone has object"
1059 }
1060 run_test 27g "$GETSTRIPE with no objects"
1061
1062 test_27i() {
1063         touch $DIR/d27/fsome || error "touch failed"
1064         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1065 }
1066 run_test 27i "$GETSTRIPE with some objects"
1067
1068 test_27j() {
1069         mkdir -p $DIR/d27
1070         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1071 }
1072 run_test 27j "setstripe with bad stripe offset (should return error)"
1073
1074 test_27k() { # bug 2844
1075         mkdir -p $DIR/d27
1076         FILE=$DIR/d27/f27k
1077         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1078         [ ! -d $DIR/d27 ] && mkdir -p $DIR/d27
1079         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1080         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1081         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1082         dd if=/dev/zero of=$FILE bs=4k count=1
1083         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1084         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1085 }
1086 run_test 27k "limit i_blksize for broken user apps ============="
1087
1088 test_27l() {
1089         mkdir -p $DIR/d27
1090         mcreate $DIR/f27l || error "creating file"
1091         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1092                 error "setstripe should have failed" || true
1093 }
1094 run_test 27l "check setstripe permissions (should return error)"
1095
1096 test_27m() {
1097         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1098                 return
1099         if [ $ORIGFREE -gt $MAXFREE ]; then
1100                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1101                 return
1102         fi
1103         trap simple_cleanup_common EXIT
1104         mkdir -p $DIR/$tdir
1105         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1106         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1107                 error "dd should fill OST0"
1108         i=2
1109         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1110                 i=`expr $i + 1`
1111                 [ $i -gt 256 ] && break
1112         done
1113         i=`expr $i + 1`
1114         touch $DIR/$tdir/f27m_$i
1115         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1116                 error "OST0 was full but new created file still use it"
1117         i=`expr $i + 1`
1118         touch $DIR/$tdir/f27m_$i
1119         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1120                 error "OST0 was full but new created file still use it"
1121         simple_cleanup_common
1122 }
1123 run_test 27m "create file while OST0 was full =================="
1124
1125 sleep_maxage() {
1126         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1127         sleep $DELAY
1128 }
1129
1130 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1131 # if the OST isn't full anymore.
1132 reset_enospc() {
1133         local OSTIDX=${1:-""}
1134
1135         local list=$(comma_list $(osts_nodes))
1136         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1137
1138         do_nodes $list lctl set_param fail_loc=0
1139         sync    # initiate all OST_DESTROYs from MDS to OST
1140         sleep_maxage
1141 }
1142
1143 exhaust_precreations() {
1144         local OSTIDX=$1
1145         local FAILLOC=$2
1146         local FAILIDX=${3:-$OSTIDX}
1147
1148         mkdir -p $DIR/$tdir
1149         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1150         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1151
1152         local OST=$(ostname_from_index $OSTIDX)
1153         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1154                           sed -e 's/_UUID$//;s/^.*-//')
1155
1156         # on the mdt's osc
1157         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1158         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1159         osc.$mdtosc_proc1.prealloc_last_id)
1160         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1161         osc.$mdtosc_proc1.prealloc_next_id)
1162
1163         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1164         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1165
1166         mkdir -p $DIR/$tdir/${OST}
1167         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1168 #define OBD_FAIL_OST_ENOSPC              0x215
1169         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1170         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1171         echo "Creating to objid $last_id on ost $OST..."
1172         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1173         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1174         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1175         sleep_maxage
1176 }
1177
1178 exhaust_all_precreations() {
1179         local i
1180         for (( i=0; i < OSTCOUNT; i++ )) ; do
1181                 exhaust_precreations $i $1 -1
1182         done
1183 }
1184
1185 test_27n() {
1186         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1187         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1188         remote_ost_nodsh && skip "remote OST with nodsh" && return
1189
1190         reset_enospc
1191         rm -f $DIR/$tdir/$tfile
1192         exhaust_precreations 0 0x80000215
1193         $SETSTRIPE -c -1 $DIR/$tdir
1194         touch $DIR/$tdir/$tfile || error
1195         $GETSTRIPE $DIR/$tdir/$tfile
1196         reset_enospc
1197 }
1198 run_test 27n "create file with some full OSTs =================="
1199
1200 test_27o() {
1201         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1202         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1203         remote_ost_nodsh && skip "remote OST with nodsh" && return
1204
1205         reset_enospc
1206         rm -f $DIR/$tdir/$tfile
1207         exhaust_all_precreations 0x215
1208
1209         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1210
1211         reset_enospc
1212         rm -rf $DIR/$tdir/*
1213 }
1214 run_test 27o "create file with all full OSTs (should error) ===="
1215
1216 test_27p() {
1217         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1218         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1219         remote_ost_nodsh && skip "remote OST with nodsh" && return
1220
1221         reset_enospc
1222         rm -f $DIR/$tdir/$tfile
1223         mkdir -p $DIR/$tdir
1224
1225         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1226         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1227         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1228
1229         exhaust_precreations 0 0x80000215
1230         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1231         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1232         $GETSTRIPE $DIR/$tdir/$tfile
1233
1234         reset_enospc
1235 }
1236 run_test 27p "append to a truncated file with some full OSTs ==="
1237
1238 test_27q() {
1239         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1240         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1241         remote_ost_nodsh && skip "remote OST with nodsh" && return
1242
1243         reset_enospc
1244         rm -f $DIR/$tdir/$tfile
1245
1246         mkdir -p $DIR/$tdir
1247         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1248         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1249         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1250
1251         exhaust_all_precreations 0x215
1252
1253         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1254         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1255
1256         reset_enospc
1257 }
1258 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1259
1260 test_27r() {
1261         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1262         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1263         remote_ost_nodsh && skip "remote OST with nodsh" && return
1264
1265         reset_enospc
1266         rm -f $DIR/$tdir/$tfile
1267         exhaust_precreations 0 0x80000215
1268
1269         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1270
1271         reset_enospc
1272 }
1273 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1274
1275 test_27s() { # bug 10725
1276         mkdir -p $DIR/$tdir
1277         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1278         local stripe_count=0
1279         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1280         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1281                 error "stripe width >= 2^32 succeeded" || true
1282
1283 }
1284 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1285
1286 test_27t() { # bug 10864
1287         WDIR=`pwd`
1288         WLFS=`which lfs`
1289         cd $DIR
1290         touch $tfile
1291         $WLFS getstripe $tfile
1292         cd $WDIR
1293 }
1294 run_test 27t "check that utils parse path correctly"
1295
1296 test_27u() { # bug 4900
1297         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1298         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1299
1300 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1301         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1302         mkdir -p $DIR/$tdir
1303         createmany -o $DIR/$tdir/t- 1000
1304         do_facet $SINGLEMDS lctl set_param fail_loc=0
1305
1306         TLOG=$DIR/$tfile.getstripe
1307         $GETSTRIPE $DIR/$tdir > $TLOG
1308         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1309         unlinkmany $DIR/$tdir/t- 1000
1310         [ $OBJS -gt 0 ] && \
1311                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1312 }
1313 run_test 27u "skip object creation on OSC w/o objects =========="
1314
1315 test_27v() { # bug 4900
1316         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1317         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1318         remote_ost_nodsh && skip "remote OST with nodsh" && return
1319
1320         exhaust_all_precreations 0x215
1321         reset_enospc
1322
1323         mkdir -p $DIR/$tdir
1324         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1325
1326         touch $DIR/$tdir/$tfile
1327         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1328         # all except ost1
1329         for (( i=1; i < OSTCOUNT; i++ )); do
1330                 do_facet ost$i lctl set_param fail_loc=0x705
1331         done
1332         local START=`date +%s`
1333         createmany -o $DIR/$tdir/$tfile 32
1334
1335         local FINISH=`date +%s`
1336         local TIMEOUT=`lctl get_param -n timeout`
1337         local PROCESS=$((FINISH - START))
1338         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1339                error "$FINISH - $START >= $TIMEOUT / 2"
1340         sleep $((TIMEOUT / 2 - PROCESS))
1341         reset_enospc
1342 }
1343 run_test 27v "skip object creation on slow OST ================="
1344
1345 test_27w() { # bug 10997
1346         mkdir -p $DIR/$tdir || error "mkdir failed"
1347         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1348         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1349                 error "stripe size $size != 65536" || true
1350         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1351                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1352 }
1353 run_test 27w "check $SETSTRIPE -S option"
1354
1355 test_27wa() {
1356         [ "$OSTCOUNT" -lt "2" ] &&
1357                 skip_env "skipping multiple stripe count/offset test" && return
1358
1359         mkdir -p $DIR/$tdir || error "mkdir failed"
1360         for i in $(seq 1 $OSTCOUNT); do
1361                 offset=$((i - 1))
1362                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1363                         error "setstripe -c $i -i $offset failed"
1364                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1365                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1366                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1367                 [ $index -ne $offset ] &&
1368                         error "stripe offset $index != $offset" || true
1369         done
1370 }
1371 run_test 27wa "check $SETSTRIPE -c -i options"
1372
1373 test_27x() {
1374         remote_ost_nodsh && skip "remote OST with nodsh" && return
1375         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1376         OFFSET=$(($OSTCOUNT - 1))
1377         OSTIDX=0
1378         local OST=$(ostname_from_index $OSTIDX)
1379
1380         mkdir -p $DIR/$tdir
1381         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1382         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1383         sleep_maxage
1384         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1385         for i in `seq 0 $OFFSET`; do
1386                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1387                 error "OST0 was degraded but new created file still use it"
1388         done
1389         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1390 }
1391 run_test 27x "create files while OST0 is degraded"
1392
1393 test_27y() {
1394         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1395         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1396         remote_ost_nodsh && skip "remote OST with nodsh" && return
1397
1398         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1399         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1400             osc.$mdtosc.prealloc_last_id)
1401         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1402             osc.$mdtosc.prealloc_next_id)
1403         local fcount=$((last_id - next_id))
1404         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1405         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1406
1407         MDS_OSCS=`do_facet $SINGLEMDS lctl dl | awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
1408         OFFSET=$(($OSTCOUNT-1))
1409         OST=-1
1410         for OSC in $MDS_OSCS; do
1411                 if [ $OST == -1 ]; then {
1412                         OST=`osc_to_ost $OSC`
1413                 } else {
1414                         echo $OSC "is Deactivate:"
1415                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1416                 } fi
1417         done
1418
1419         OSTIDX=$(index_from_ostuuid $OST)
1420         mkdir -p $DIR/$tdir
1421         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1422
1423         do_facet ost$((OSTIDX+1)) lctl set_param -n obdfilter.$OST.degraded 1
1424         sleep_maxage
1425         createmany -o $DIR/$tdir/$tfile $fcount
1426         do_facet ost$((OSTIDX+1)) lctl set_param -n obdfilter.$OST.degraded 0
1427
1428         for i in `seq 0 $OFFSET`; do
1429                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "$OSTIDX"` ] || \
1430                       error "files created on deactivated OSTs instead of degraded OST"
1431         done
1432         for OSC in $MDS_OSCS; do
1433                 [ `osc_to_ost $OSC` != $OST  ] && {
1434                         echo $OSC "is activate"
1435                         do_facet $SINGLEMDS lctl --device %$OSC activate
1436                 }
1437         done
1438 }
1439 run_test 27y "create files while OST0 is degraded and the rest inactive"
1440
1441 check_seq_oid()
1442 {
1443         log "check file $1"
1444
1445         lmm_count=$($GETSTRIPE -c $1)
1446         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1447         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1448
1449         local old_ifs="$IFS"
1450         IFS=$'[:]'
1451         fid=($($LFS path2fid $1))
1452         IFS="$old_ifs"
1453
1454         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1455         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1456
1457         # compare lmm_seq and lu_fid->f_seq
1458         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1459         # compare lmm_object_id and lu_fid->oid
1460         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1461
1462         # check the trusted.fid attribute of the OST objects of the file
1463         local have_obdidx=false
1464         local stripe_nr=0
1465         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1466                 # skip lines up to and including "obdidx"
1467                 [ -z "$obdidx" ] && break
1468                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1469                 $have_obdidx || continue
1470
1471                 local ost=$((obdidx + 1))
1472                 local dev=$(ostdevname $ost)
1473
1474                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1475                         echo "Currently only works with ldiskfs-based OSTs"
1476                         continue
1477                 fi
1478
1479                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1480
1481                 #don't unmount/remount the OSTs if we don't need to do that
1482                 #local dir=$(facet_mntpt ost$ost)
1483                 #stop ost$dev
1484                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1485                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1486                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1487                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1488
1489                 local obj_file="O/$seq/d$((oid %32))/$oid"
1490                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1491                            $dev 2>/dev/null" | grep "parent=")
1492
1493                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1494
1495                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1496
1497                 #do_facet ost$ost umount -d $dir
1498                 #start ost$ost $dev $OST_MOUNT_OPTS
1499
1500                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1501                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1502                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1503                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1504                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1505                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1506
1507                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1508                 [ $ff_pseq = $lmm_seq ] ||
1509                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1510                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1511                 [ $ff_poid = $lmm_oid ] ||
1512                         error "FF parent OID $ff_poid != $lmm_oid"
1513                 [ $ff_pstripe = $stripe_nr ] ||
1514                         error "FF stripe $ff_pstripe != $stripe_nr"
1515
1516                 stripe_nr=$((stripe_nr + 1))
1517         done
1518 }
1519
1520 test_27z() {
1521         remote_ost_nodsh && skip "remote OST with nodsh" && return
1522         mkdir -p $DIR/$tdir
1523
1524         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1525                 { error "setstripe -c -1 failed"; return 1; }
1526         # We need to send a write to every object to get parent FID info set.
1527         # This _should_ also work for setattr, but does not currently.
1528         # touch $DIR/$tdir/$tfile-1 ||
1529         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1530                 { error "dd $tfile-1 failed"; return 2; }
1531         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1532                 { error "setstripe -c -1 failed"; return 3; }
1533         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1534                 { error "dd $tfile-2 failed"; return 4; }
1535
1536         # make sure write RPCs have been sent to OSTs
1537         sync; sleep 5; sync
1538
1539         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1540         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1541 }
1542 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1543
1544 test_27A() { # b=19102
1545         local restore_size=$($GETSTRIPE -S $MOUNT)
1546         local restore_count=$($GETSTRIPE -c $MOUNT)
1547         local restore_offset=$($GETSTRIPE -i $MOUNT)
1548         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1549         local default_size=$($GETSTRIPE -S $MOUNT)
1550         local default_count=$($GETSTRIPE -c $MOUNT)
1551         local default_offset=$($GETSTRIPE -i $MOUNT)
1552         local dsize=$((1024 * 1024))
1553         [ $default_size -eq $dsize ] ||
1554                 error "stripe size $default_size != $dsize"
1555         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1556         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1557         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1558 }
1559 run_test 27A "check filesystem-wide default LOV EA values"
1560
1561 # createtest also checks that device nodes are created and
1562 # then visible correctly (#2091)
1563 test_28() { # bug 2091
1564         mkdir $DIR/d28
1565         $CREATETEST $DIR/d28/ct || error
1566 }
1567 run_test 28 "create/mknod/mkdir with bad file types ============"
1568
1569 test_29() {
1570         cancel_lru_locks mdc
1571         mkdir $DIR/d29
1572         touch $DIR/d29/foo
1573         log 'first d29'
1574         ls -l $DIR/d29
1575
1576         declare -i LOCKCOUNTORIG=0
1577         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1578                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1579         done
1580         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1581
1582         declare -i LOCKUNUSEDCOUNTORIG=0
1583         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1584                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1585         done
1586
1587         log 'second d29'
1588         ls -l $DIR/d29
1589         log 'done'
1590
1591         declare -i LOCKCOUNTCURRENT=0
1592         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1593                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1594         done
1595
1596         declare -i LOCKUNUSEDCOUNTCURRENT=0
1597         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1598                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1599         done
1600
1601         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1602                 lctl set_param -n ldlm.dump_namespaces ""
1603                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1604                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1605                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1606                 return 2
1607         fi
1608         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1609                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1610                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1611                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1612                 return 3
1613         fi
1614 }
1615 run_test 29 "IT_GETATTR regression  ============================"
1616
1617 test_30a() { # was test_30
1618         cp `which ls` $DIR || cp /bin/ls $DIR
1619         $DIR/ls / || error
1620         rm $DIR/ls
1621 }
1622 run_test 30a "execute binary from Lustre (execve) =============="
1623
1624 test_30b() {
1625         cp `which ls` $DIR || cp /bin/ls $DIR
1626         chmod go+rx $DIR/ls
1627         $RUNAS $DIR/ls / || error
1628         rm $DIR/ls
1629 }
1630 run_test 30b "execute binary from Lustre as non-root ==========="
1631
1632 test_30c() { # b=22376
1633         cp `which ls` $DIR || cp /bin/ls $DIR
1634         chmod a-rw $DIR/ls
1635         cancel_lru_locks mdc
1636         cancel_lru_locks osc
1637         $RUNAS $DIR/ls / || error
1638         rm -f $DIR/ls
1639 }
1640 run_test 30c "execute binary from Lustre without read perms ===="
1641
1642 test_31a() {
1643         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1644         $CHECKSTAT -a $DIR/f31 || error
1645 }
1646 run_test 31a "open-unlink file =================================="
1647
1648 test_31b() {
1649         touch $DIR/f31 || error
1650         ln $DIR/f31 $DIR/f31b || error
1651         $MULTIOP $DIR/f31b Ouc || error
1652         $CHECKSTAT -t file $DIR/f31 || error
1653 }
1654 run_test 31b "unlink file with multiple links while open ======="
1655
1656 test_31c() {
1657         touch $DIR/f31 || error
1658         ln $DIR/f31 $DIR/f31c || error
1659         multiop_bg_pause $DIR/f31 O_uc || return 1
1660         MULTIPID=$!
1661         $MULTIOP $DIR/f31c Ouc
1662         kill -USR1 $MULTIPID
1663         wait $MULTIPID
1664 }
1665 run_test 31c "open-unlink file with multiple links ============="
1666
1667 test_31d() {
1668         opendirunlink $DIR/d31d $DIR/d31d || error
1669         $CHECKSTAT -a $DIR/d31d || error
1670 }
1671 run_test 31d "remove of open directory ========================="
1672
1673 test_31e() { # bug 2904
1674         check_kernel_version 34 || return 0
1675         openfilleddirunlink $DIR/d31e || error
1676 }
1677 run_test 31e "remove of open non-empty directory ==============="
1678
1679 test_31f() { # bug 4554
1680         set -vx
1681         mkdir $DIR/d31f
1682         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1683         cp /etc/hosts $DIR/d31f
1684         ls -l $DIR/d31f
1685         $GETSTRIPE $DIR/d31f/hosts
1686         multiop_bg_pause $DIR/d31f D_c || return 1
1687         MULTIPID=$!
1688
1689         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1690         mkdir $DIR/d31f
1691         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1692         cp /etc/hosts $DIR/d31f
1693         ls -l $DIR/d31f
1694         $GETSTRIPE $DIR/d31f/hosts
1695         multiop_bg_pause $DIR/d31f D_c || return 1
1696         MULTIPID2=$!
1697
1698         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1699         wait $MULTIPID || error "first opendir $MULTIPID failed"
1700
1701         sleep 6
1702
1703         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1704         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1705         set +vx
1706 }
1707 run_test 31f "remove of open directory with open-unlink file ==="
1708
1709 test_31g() {
1710         echo "-- cross directory link --"
1711         mkdir $DIR/d31g{a,b}
1712         touch $DIR/d31ga/f
1713         ln $DIR/d31ga/f $DIR/d31gb/g
1714         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1715         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1716         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1717         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1718 }
1719 run_test 31g "cross directory link==============="
1720
1721 test_31h() {
1722         echo "-- cross directory link --"
1723         mkdir $DIR/d31h
1724         mkdir $DIR/d31h/dir
1725         touch $DIR/d31h/f
1726         ln $DIR/d31h/f $DIR/d31h/dir/g
1727         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1728         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1729         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1730         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1731 }
1732 run_test 31h "cross directory link under child==============="
1733
1734 test_31i() {
1735         echo "-- cross directory link --"
1736         mkdir $DIR/d31i
1737         mkdir $DIR/d31i/dir
1738         touch $DIR/d31i/dir/f
1739         ln $DIR/d31i/dir/f $DIR/d31i/g
1740         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1741         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1742         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1743         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1744 }
1745 run_test 31i "cross directory link under parent==============="
1746
1747
1748 test_31j() {
1749         mkdir $DIR/d31j
1750         mkdir $DIR/d31j/dir1
1751         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1752         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1753         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1754         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1755         return 0
1756 }
1757 run_test 31j "link for directory==============="
1758
1759
1760 test_31k() {
1761         mkdir $DIR/d31k
1762         touch $DIR/d31k/s
1763         touch $DIR/d31k/exist
1764         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1765         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1766         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1767         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1768         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1769         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1770         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1771         return 0
1772 }
1773 run_test 31k "link to file: the same, non-existing, dir==============="
1774
1775 test_31m() {
1776         mkdir $DIR/d31m
1777         touch $DIR/d31m/s
1778         mkdir $DIR/d31m2
1779         touch $DIR/d31m2/exist
1780         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1781         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1782         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1783         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1784         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1785         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1786         return 0
1787 }
1788 run_test 31m "link to file: the same, non-existing, dir==============="
1789
1790 cleanup_test32_mount() {
1791         trap 0
1792         $UMOUNT $DIR/$tdir/ext2-mountpoint
1793 }
1794
1795 test_32a() {
1796         echo "== more mountpoints and symlinks ================="
1797         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1798         trap cleanup_test32_mount EXIT
1799         mkdir -p $DIR/$tdir/ext2-mountpoint
1800         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1801         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
1802         cleanup_test32_mount
1803 }
1804 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
1805
1806 test_32b() {
1807         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1808         trap cleanup_test32_mount EXIT
1809         mkdir -p $DIR/$tdir/ext2-mountpoint
1810         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1811         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
1812         cleanup_test32_mount
1813 }
1814 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
1815
1816 test_32c() {
1817         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1818         trap cleanup_test32_mount EXIT
1819         mkdir -p $DIR/$tdir/ext2-mountpoint
1820         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1821         mkdir -p $DIR/$tdir/d2/test_dir
1822         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
1823         cleanup_test32_mount
1824 }
1825 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
1826
1827 test_32d() {
1828         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1829         trap cleanup_test32_mount EXIT
1830         mkdir -p $DIR/$tdir/ext2-mountpoint
1831         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1832         mkdir -p $DIR/$tdir/d2/test_dir
1833         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
1834         cleanup_test32_mount
1835 }
1836 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
1837
1838 test_32e() {
1839         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
1840         mkdir -p $DIR/d32e/tmp
1841         TMP_DIR=$DIR/d32e/tmp
1842         ln -s $DIR/d32e $TMP_DIR/symlink11
1843         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1844         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
1845         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
1846 }
1847 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
1848
1849 test_32f() {
1850         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
1851         mkdir -p $DIR/d32f/tmp
1852         TMP_DIR=$DIR/d32f/tmp
1853         ln -s $DIR/d32f $TMP_DIR/symlink11
1854         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1855         ls $DIR/d32f/tmp/symlink11  || error
1856         ls $DIR/d32f/symlink01 || error
1857 }
1858 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
1859
1860 test_32g() {
1861         TMP_DIR=$DIR/$tdir/tmp
1862         mkdir -p $TMP_DIR $DIR/${tdir}2
1863         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1864         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1865         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
1866         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
1867         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
1868         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
1869 }
1870 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1871
1872 test_32h() {
1873         rm -fr $DIR/$tdir $DIR/${tdir}2
1874         TMP_DIR=$DIR/$tdir/tmp
1875         mkdir -p $TMP_DIR $DIR/${tdir}2
1876         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1877         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1878         ls $TMP_DIR/symlink12 || error
1879         ls $DIR/$tdir/symlink02  || error
1880 }
1881 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1882
1883 test_32i() {
1884         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1885         trap cleanup_test32_mount EXIT
1886         mkdir -p $DIR/$tdir/ext2-mountpoint
1887         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1888         touch $DIR/$tdir/test_file
1889         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
1890         cleanup_test32_mount
1891 }
1892 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
1893
1894 test_32j() {
1895         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1896         trap cleanup_test32_mount EXIT
1897         mkdir -p $DIR/$tdir/ext2-mountpoint
1898         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1899         touch $DIR/$tdir/test_file
1900         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
1901         cleanup_test32_mount
1902 }
1903 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
1904
1905 test_32k() {
1906         rm -fr $DIR/$tdir
1907         trap cleanup_test32_mount EXIT
1908         mkdir -p $DIR/$tdir/ext2-mountpoint
1909         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
1910         mkdir -p $DIR/$tdir/d2
1911         touch $DIR/$tdir/d2/test_file || error
1912         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
1913         cleanup_test32_mount
1914 }
1915 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
1916
1917 test_32l() {
1918         rm -fr $DIR/$tdir
1919         trap cleanup_test32_mount EXIT
1920         mkdir -p $DIR/$tdir/ext2-mountpoint
1921         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
1922         mkdir -p $DIR/$tdir/d2
1923         touch $DIR/$tdir/d2/test_file
1924         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
1925         cleanup_test32_mount
1926 }
1927 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
1928
1929 test_32m() {
1930         rm -fr $DIR/d32m
1931         mkdir -p $DIR/d32m/tmp
1932         TMP_DIR=$DIR/d32m/tmp
1933         ln -s $DIR $TMP_DIR/symlink11
1934         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1935         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
1936         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
1937 }
1938 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
1939
1940 test_32n() {
1941         rm -fr $DIR/d32n
1942         mkdir -p $DIR/d32n/tmp
1943         TMP_DIR=$DIR/d32n/tmp
1944         ln -s $DIR $TMP_DIR/symlink11
1945         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1946         ls -l $DIR/d32n/tmp/symlink11  || error
1947         ls -l $DIR/d32n/symlink01 || error
1948 }
1949 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
1950
1951 test_32o() {
1952         rm -fr $DIR/d32o $DIR/$tfile
1953         touch $DIR/$tfile
1954         mkdir -p $DIR/d32o/tmp
1955         TMP_DIR=$DIR/d32o/tmp
1956         ln -s $DIR/$tfile $TMP_DIR/symlink12
1957         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1958         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
1959         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
1960         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
1961         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
1962 }
1963 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
1964
1965 test_32p() {
1966     log 32p_1
1967         rm -fr $DIR/d32p
1968     log 32p_2
1969         rm -f $DIR/$tfile
1970     log 32p_3
1971         touch $DIR/$tfile
1972     log 32p_4
1973         mkdir -p $DIR/d32p/tmp
1974     log 32p_5
1975         TMP_DIR=$DIR/d32p/tmp
1976     log 32p_6
1977         ln -s $DIR/$tfile $TMP_DIR/symlink12
1978     log 32p_7
1979         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1980     log 32p_8
1981         cat $DIR/d32p/tmp/symlink12 || error
1982     log 32p_9
1983         cat $DIR/d32p/symlink02 || error
1984     log 32p_10
1985 }
1986 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
1987
1988 cleanup_testdir_mount() {
1989         trap 0
1990         $UMOUNT $DIR/$tdir
1991 }
1992
1993 test_32q() {
1994         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
1995         trap cleanup_testdir_mount EXIT
1996         mkdir -p $DIR/$tdir
1997         touch $DIR/$tdir/under_the_mount
1998         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
1999         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2000         cleanup_testdir_mount
2001 }
2002 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2003
2004 test_32r() {
2005         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2006         trap cleanup_testdir_mount EXIT
2007         mkdir -p $DIR/$tdir
2008         touch $DIR/$tdir/under_the_mount
2009         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2010         ls $DIR/$tdir | grep -q under_the_mount && error || true
2011         cleanup_testdir_mount
2012 }
2013 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2014
2015 test_33() {
2016         rm -f $DIR/$tfile
2017         touch $DIR/$tfile
2018         chmod 444 $DIR/$tfile
2019         chown $RUNAS_ID $DIR/$tfile
2020         log 33_1
2021         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2022         log 33_2
2023 }
2024 run_test 33 "write file with mode 444 (should return error) ===="
2025
2026 test_33a() {
2027         rm -fr $DIR/d33
2028         mkdir -p $DIR/d33
2029         chown $RUNAS_ID $DIR/d33
2030         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2031         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2032                 error "open RDWR" || true
2033 }
2034 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2035
2036 test_33b() {
2037         rm -fr $DIR/d33
2038         mkdir -p $DIR/d33
2039         chown $RUNAS_ID $DIR/d33
2040         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2041 }
2042 run_test 33b "test open file with malformed flags (No panic and return error)"
2043
2044 test_33c() {
2045         local ostnum
2046         local ostname
2047         local write_bytes
2048         local all_zeros
2049
2050         remote_ost_nodsh && skip "remote OST with nodsh" && return
2051         all_zeros=:
2052         rm -fr $DIR/d33
2053         mkdir -p $DIR/d33
2054         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2055
2056         sync
2057         for ostnum in $(seq $OSTCOUNT); do
2058                 # test-framework's OST numbering is one-based, while Lustre's
2059                 # is zero-based
2060                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2061                 # Parsing llobdstat's output sucks; we could grep the /proc
2062                 # path, but that's likely to not be as portable as using the
2063                 # llobdstat utility.  So we parse lctl output instead.
2064                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2065                         obdfilter/$ostname/stats |
2066                         awk '/^write_bytes/ {print $7}' )
2067                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2068                 if (( ${write_bytes:-0} > 0 ))
2069                 then
2070                         all_zeros=false
2071                         break;
2072                 fi
2073         done
2074
2075         $all_zeros || return 0
2076
2077         # Write four bytes
2078         echo foo > $DIR/d33/bar
2079         # Really write them
2080         sync
2081
2082         # Total up write_bytes after writing.  We'd better find non-zeros.
2083         for ostnum in $(seq $OSTCOUNT); do
2084                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2085                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2086                         obdfilter/$ostname/stats |
2087                         awk '/^write_bytes/ {print $7}' )
2088                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2089                 if (( ${write_bytes:-0} > 0 ))
2090                 then
2091                         all_zeros=false
2092                         break;
2093                 fi
2094         done
2095
2096         if $all_zeros
2097         then
2098                 for ostnum in $(seq $OSTCOUNT); do
2099                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2100                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2101                         do_facet ost$ostnum lctl get_param -n \
2102                                 obdfilter/$ostname/stats
2103                 done
2104                 error "OST not keeping write_bytes stats (b22312)"
2105         fi
2106 }
2107 run_test 33c "test llobdstat and write_bytes"
2108
2109 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2110 test_34a() {
2111         rm -f $DIR/f34
2112         $MCREATE $DIR/f34 || error
2113         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2114         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2115         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2116         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2117 }
2118 run_test 34a "truncate file that has not been opened ==========="
2119
2120 test_34b() {
2121         [ ! -f $DIR/f34 ] && test_34a
2122         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2123         $OPENFILE -f O_RDONLY $DIR/f34
2124         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2125         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2126 }
2127 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2128
2129 test_34c() {
2130         [ ! -f $DIR/f34 ] && test_34a
2131         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2132         $OPENFILE -f O_RDWR $DIR/f34
2133         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2134         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2135 }
2136 run_test 34c "O_RDWR opening file-with-size works =============="
2137
2138 test_34d() {
2139         [ ! -f $DIR/f34 ] && test_34a
2140         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2141         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2142         rm $DIR/f34
2143 }
2144 run_test 34d "write to sparse file ============================="
2145
2146 test_34e() {
2147         rm -f $DIR/f34e
2148         $MCREATE $DIR/f34e || error
2149         $TRUNCATE $DIR/f34e 1000 || error
2150         $CHECKSTAT -s 1000 $DIR/f34e || error
2151         $OPENFILE -f O_RDWR $DIR/f34e
2152         $CHECKSTAT -s 1000 $DIR/f34e || error
2153 }
2154 run_test 34e "create objects, some with size and some without =="
2155
2156 test_34f() { # bug 6242, 6243
2157         SIZE34F=48000
2158         rm -f $DIR/f34f
2159         $MCREATE $DIR/f34f || error
2160         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2161         dd if=$DIR/f34f of=$TMP/f34f
2162         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2163         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2164         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2165         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2166         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2167 }
2168 run_test 34f "read from a file with no objects until EOF ======="
2169
2170 test_34g() {
2171         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2172         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2173         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2174         cancel_lru_locks osc
2175         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2176                 error "wrong size after lock cancel"
2177
2178         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2179         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2180                 error "expanding truncate failed"
2181         cancel_lru_locks osc
2182         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2183                 error "wrong expanded size after lock cancel"
2184 }
2185 run_test 34g "truncate long file ==============================="
2186
2187 test_34h() {
2188         local gid=10
2189         local sz=1000
2190
2191         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2192         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2193         MULTIPID=$!
2194         sleep 2
2195
2196         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2197                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2198                 kill -9 $MULTIPID
2199         fi
2200         wait $MULTIPID
2201         local nsz=`stat -c %s $DIR/$tfile`
2202         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2203 }
2204 run_test 34h "ftruncate file under grouplock should not block"
2205
2206 test_35a() {
2207         cp /bin/sh $DIR/f35a
2208         chmod 444 $DIR/f35a
2209         chown $RUNAS_ID $DIR/f35a
2210         $RUNAS $DIR/f35a && error || true
2211         rm $DIR/f35a
2212 }
2213 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2214
2215 test_36a() {
2216         rm -f $DIR/f36
2217         utime $DIR/f36 || error
2218 }
2219 run_test 36a "MDS utime check (mknod, utime) ==================="
2220
2221 test_36b() {
2222         echo "" > $DIR/f36
2223         utime $DIR/f36 || error
2224 }
2225 run_test 36b "OST utime check (open, utime) ===================="
2226
2227 test_36c() {
2228         rm -f $DIR/d36/f36
2229         mkdir $DIR/d36
2230         chown $RUNAS_ID $DIR/d36
2231         $RUNAS utime $DIR/d36/f36 || error
2232 }
2233 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2234
2235 test_36d() {
2236         [ ! -d $DIR/d36 ] && test_36c
2237         echo "" > $DIR/d36/f36
2238         $RUNAS utime $DIR/d36/f36 || error
2239 }
2240 run_test 36d "non-root OST utime check (open, utime) ==========="
2241
2242 test_36e() {
2243         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2244         mkdir -p $DIR/$tdir
2245         touch $DIR/$tdir/$tfile
2246         $RUNAS utime $DIR/$tdir/$tfile && \
2247                 error "utime worked, expected failure" || true
2248 }
2249 run_test 36e "utime on non-owned file (should return error) ===="
2250
2251 subr_36fh() {
2252         local fl="$1"
2253         local LANG_SAVE=$LANG
2254         local LC_LANG_SAVE=$LC_LANG
2255         export LANG=C LC_LANG=C # for date language
2256
2257         DATESTR="Dec 20  2000"
2258         mkdir -p $DIR/$tdir
2259         lctl set_param fail_loc=$fl
2260         date; date +%s
2261         cp /etc/hosts $DIR/$tdir/$tfile
2262         sync & # write RPC generated with "current" inode timestamp, but delayed
2263         sleep 1
2264         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2265         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2266         cancel_lru_locks osc
2267         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2268         date; date +%s
2269         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2270                 echo "BEFORE: $LS_BEFORE" && \
2271                 echo "AFTER : $LS_AFTER" && \
2272                 echo "WANT  : $DATESTR" && \
2273                 error "$DIR/$tdir/$tfile timestamps changed" || true
2274
2275         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2276 }
2277
2278 test_36f() {
2279         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2280         subr_36fh "0x80000214"
2281 }
2282 run_test 36f "utime on file racing with OST BRW write =========="
2283
2284 test_36g() {
2285         remote_ost_nodsh && skip "remote OST with nodsh" && return
2286         local fmd_max_age
2287         local fmd_before
2288         local fmd_after
2289
2290         mkdir -p $DIR/$tdir
2291         fmd_max_age=$(do_facet ost1 \
2292                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2293                 head -n 1")
2294
2295         fmd_before=$(do_facet ost1 \
2296                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2297         touch $DIR/$tdir/$tfile
2298         sleep $((fmd_max_age + 12))
2299         fmd_after=$(do_facet ost1 \
2300                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2301
2302         echo "fmd_before: $fmd_before"
2303         echo "fmd_after: $fmd_after"
2304         [ "$fmd_after" -gt "$fmd_before" ] && \
2305                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2306                 error "fmd didn't expire after ping" || true
2307 }
2308 run_test 36g "filter mod data cache expiry ====================="
2309
2310 test_36h() {
2311         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2312         subr_36fh "0x80000227"
2313 }
2314 run_test 36h "utime on file racing with OST BRW write =========="
2315
2316 # test_37 - duplicate with tests 32q 32r
2317
2318 test_38() {
2319         local file=$DIR/$tfile
2320         touch $file
2321         openfile -f O_DIRECTORY $file
2322         local RC=$?
2323         local ENOTDIR=20
2324         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2325         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2326 }
2327 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2328
2329 test_39() {
2330         touch $DIR/$tfile
2331         touch $DIR/${tfile}2
2332 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2333 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2334 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2335         sleep 2
2336         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2337         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2338                 echo "mtime"
2339                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2340                 echo "atime"
2341                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2342                 echo "ctime"
2343                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2344                 error "O_TRUNC didn't change timestamps"
2345         fi
2346 }
2347 run_test 39 "mtime changed on create ==========================="
2348
2349 test_39b() {
2350         mkdir -p $DIR/$tdir
2351         cp -p /etc/passwd $DIR/$tdir/fopen
2352         cp -p /etc/passwd $DIR/$tdir/flink
2353         cp -p /etc/passwd $DIR/$tdir/funlink
2354         cp -p /etc/passwd $DIR/$tdir/frename
2355         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2356
2357         sleep 1
2358         echo "aaaaaa" >> $DIR/$tdir/fopen
2359         echo "aaaaaa" >> $DIR/$tdir/flink
2360         echo "aaaaaa" >> $DIR/$tdir/funlink
2361         echo "aaaaaa" >> $DIR/$tdir/frename
2362
2363         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2364         local link_new=`stat -c %Y $DIR/$tdir/flink`
2365         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2366         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2367
2368         cat $DIR/$tdir/fopen > /dev/null
2369         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2370         rm -f $DIR/$tdir/funlink2
2371         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2372
2373         for (( i=0; i < 2; i++ )) ; do
2374                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2375                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2376                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2377                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2378
2379                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2380                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2381                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2382                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2383
2384                 cancel_lru_locks osc
2385                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2386         done
2387 }
2388 run_test 39b "mtime change on open, link, unlink, rename  ======"
2389
2390 # this should be set to past
2391 TEST_39_MTIME=`date -d "1 year ago" +%s`
2392
2393 # bug 11063
2394 test_39c() {
2395         touch $DIR1/$tfile
2396         sleep 2
2397         local mtime0=`stat -c %Y $DIR1/$tfile`
2398
2399         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2400         local mtime1=`stat -c %Y $DIR1/$tfile`
2401         [ "$mtime1" = $TEST_39_MTIME ] || \
2402                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2403
2404         local d1=`date +%s`
2405         echo hello >> $DIR1/$tfile
2406         local d2=`date +%s`
2407         local mtime2=`stat -c %Y $DIR1/$tfile`
2408         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2409                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2410
2411         mv $DIR1/$tfile $DIR1/$tfile-1
2412
2413         for (( i=0; i < 2; i++ )) ; do
2414                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2415                 [ "$mtime2" = "$mtime3" ] || \
2416                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2417
2418                 cancel_lru_locks osc
2419                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2420         done
2421 }
2422 run_test 39c "mtime change on rename ==========================="
2423
2424 # bug 21114
2425 test_39d() {
2426         touch $DIR1/$tfile
2427
2428         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2429
2430         for (( i=0; i < 2; i++ )) ; do
2431                 local mtime=`stat -c %Y $DIR1/$tfile`
2432                 [ $mtime = $TEST_39_MTIME ] || \
2433                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2434
2435                 cancel_lru_locks osc
2436                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2437         done
2438 }
2439 run_test 39d "create, utime, stat =============================="
2440
2441 # bug 21114
2442 test_39e() {
2443         touch $DIR1/$tfile
2444         local mtime1=`stat -c %Y $DIR1/$tfile`
2445
2446         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2447
2448         for (( i=0; i < 2; i++ )) ; do
2449                 local mtime2=`stat -c %Y $DIR1/$tfile`
2450                 [ $mtime2 = $TEST_39_MTIME ] || \
2451                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2452
2453                 cancel_lru_locks osc
2454                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2455         done
2456 }
2457 run_test 39e "create, stat, utime, stat ========================"
2458
2459 # bug 21114
2460 test_39f() {
2461         touch $DIR1/$tfile
2462         mtime1=`stat -c %Y $DIR1/$tfile`
2463
2464         sleep 2
2465         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2466
2467         for (( i=0; i < 2; i++ )) ; do
2468                 local mtime2=`stat -c %Y $DIR1/$tfile`
2469                 [ $mtime2 = $TEST_39_MTIME ] || \
2470                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2471
2472                 cancel_lru_locks osc
2473                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2474         done
2475 }
2476 run_test 39f "create, stat, sleep, utime, stat ================="
2477
2478 # bug 11063
2479 test_39g() {
2480         echo hello >> $DIR1/$tfile
2481         local mtime1=`stat -c %Y $DIR1/$tfile`
2482
2483         sleep 2
2484         chmod o+r $DIR1/$tfile
2485
2486         for (( i=0; i < 2; i++ )) ; do
2487                 local mtime2=`stat -c %Y $DIR1/$tfile`
2488                 [ "$mtime1" = "$mtime2" ] || \
2489                         error "lost mtime: $mtime2, should be $mtime1"
2490
2491                 cancel_lru_locks osc
2492                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2493         done
2494 }
2495 run_test 39g "write, chmod, stat ==============================="
2496
2497 # bug 11063
2498 test_39h() {
2499         touch $DIR1/$tfile
2500         sleep 1
2501
2502         local d1=`date`
2503         echo hello >> $DIR1/$tfile
2504         local mtime1=`stat -c %Y $DIR1/$tfile`
2505
2506         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2507         local d2=`date`
2508         if [ "$d1" != "$d2" ]; then
2509                 echo "write and touch not within one second"
2510         else
2511                 for (( i=0; i < 2; i++ )) ; do
2512                         local mtime2=`stat -c %Y $DIR1/$tfile`
2513                         [ "$mtime2" = $TEST_39_MTIME ] || \
2514                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2515
2516                         cancel_lru_locks osc
2517                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2518                 done
2519         fi
2520 }
2521 run_test 39h "write, utime within one second, stat ============="
2522
2523 test_39i() {
2524         touch $DIR1/$tfile
2525         sleep 1
2526
2527         echo hello >> $DIR1/$tfile
2528         local mtime1=`stat -c %Y $DIR1/$tfile`
2529
2530         mv $DIR1/$tfile $DIR1/$tfile-1
2531
2532         for (( i=0; i < 2; i++ )) ; do
2533                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2534
2535                 [ "$mtime1" = "$mtime2" ] || \
2536                         error "lost mtime: $mtime2, should be $mtime1"
2537
2538                 cancel_lru_locks osc
2539                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2540         done
2541 }
2542 run_test 39i "write, rename, stat =============================="
2543
2544 test_39j() {
2545         start_full_debug_logging
2546         touch $DIR1/$tfile
2547         sleep 1
2548
2549         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2550         lctl set_param fail_loc=0x80000412
2551         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2552                 error "multiop failed"
2553         local multipid=$!
2554         local mtime1=`stat -c %Y $DIR1/$tfile`
2555
2556         mv $DIR1/$tfile $DIR1/$tfile-1
2557
2558         kill -USR1 $multipid
2559         wait $multipid || error "multiop close failed"
2560
2561         for (( i=0; i < 2; i++ )) ; do
2562                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2563                 [ "$mtime1" = "$mtime2" ] ||
2564                         error "mtime is lost on close: $mtime2, " \
2565                               "should be $mtime1"
2566
2567                 cancel_lru_locks osc
2568                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2569         done
2570         lctl set_param fail_loc=0
2571         stop_full_debug_logging
2572 }
2573 run_test 39j "write, rename, close, stat ======================="
2574
2575 test_39k() {
2576         touch $DIR1/$tfile
2577         sleep 1
2578
2579         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2580         local multipid=$!
2581         local mtime1=`stat -c %Y $DIR1/$tfile`
2582
2583         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2584
2585         kill -USR1 $multipid
2586         wait $multipid || error "multiop close failed"
2587
2588         for (( i=0; i < 2; i++ )) ; do
2589                 local mtime2=`stat -c %Y $DIR1/$tfile`
2590
2591                 [ "$mtime2" = $TEST_39_MTIME ] || \
2592                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2593
2594                 cancel_lru_locks osc
2595                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2596         done
2597 }
2598 run_test 39k "write, utime, close, stat ========================"
2599
2600 # this should be set to future
2601 TEST_39_ATIME=`date -d "1 year" +%s`
2602
2603 test_39l() {
2604         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2605         local atime_diff=$(do_facet $SINGLEMDS lctl get_param -n mdd.*.atime_diff)
2606
2607         mkdir -p $DIR/$tdir
2608
2609         # test setting directory atime to future
2610         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2611         local atime=$(stat -c %X $DIR/$tdir)
2612         [ "$atime" = $TEST_39_ATIME ] || \
2613                 error "atime is not set to future: $atime, should be $TEST_39_ATIME"
2614
2615         # test setting directory atime from future to now
2616         local d1=$(date +%s)
2617         ls $DIR/$tdir
2618         local d2=$(date +%s)
2619
2620         cancel_lru_locks mdc
2621         atime=$(stat -c %X $DIR/$tdir)
2622         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2623                 error "atime is not updated from future: $atime, should be $d1<atime<$d2"
2624
2625         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=2
2626         sleep 3
2627
2628         # test setting directory atime when now > dir atime + atime_diff
2629         d1=$(date +%s)
2630         ls $DIR/$tdir
2631         d2=$(date +%s)
2632         cancel_lru_locks mdc
2633         atime=$(stat -c %X $DIR/$tdir)
2634         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2635                 error "atime is not updated  : $atime, should be $d2"
2636
2637         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=60
2638         sleep 3
2639
2640         # test not setting directory atime when now < dir atime + atime_diff
2641         ls $DIR/$tdir
2642         cancel_lru_locks mdc
2643         atime=$(stat -c %X $DIR/$tdir)
2644         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2645                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2646
2647         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=$atime_diff
2648 }
2649 run_test 39l "directory atime update ==========================="
2650
2651 test_39m() {
2652         touch $DIR1/$tfile
2653         sleep 2
2654         local far_past_mtime=$(date -d "May 29 1953" +%s)
2655         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2656
2657         touch -m -d @$far_past_mtime $DIR1/$tfile
2658         touch -a -d @$far_past_atime $DIR1/$tfile
2659
2660         for (( i=0; i < 2; i++ )) ; do
2661                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2662                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2663                         error "atime or mtime set incorrectly"
2664
2665                 cancel_lru_locks osc
2666                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2667         done
2668 }
2669 run_test 39m "test atime and mtime before 1970"
2670
2671 test_40() {
2672         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2673         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2674         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2675 }
2676 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2677
2678 test_41() {
2679         # bug 1553
2680         small_write $DIR/f41 18
2681 }
2682 run_test 41 "test small file write + fstat ====================="
2683
2684 count_ost_writes() {
2685         lctl get_param -n osc.*.stats |
2686             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2687 }
2688
2689 # decent default
2690 WRITEBACK_SAVE=500
2691 DIRTY_RATIO_SAVE=40
2692 MAX_DIRTY_RATIO=50
2693 BG_DIRTY_RATIO_SAVE=10
2694 MAX_BG_DIRTY_RATIO=25
2695
2696 start_writeback() {
2697         trap 0
2698         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2699         # dirty_ratio, dirty_background_ratio
2700         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2701                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2702                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2703                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2704         else
2705                 # if file not here, we are a 2.4 kernel
2706                 kill -CONT `pidof kupdated`
2707         fi
2708 }
2709
2710 stop_writeback() {
2711         # setup the trap first, so someone cannot exit the test at the
2712         # exact wrong time and mess up a machine
2713         trap start_writeback EXIT
2714         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2715         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2716                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2717                 sysctl -w vm.dirty_writeback_centisecs=0
2718                 sysctl -w vm.dirty_writeback_centisecs=0
2719                 # save and increase /proc/sys/vm/dirty_ratio
2720                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2721                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2722                 # save and increase /proc/sys/vm/dirty_background_ratio
2723                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2724                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2725         else
2726                 # if file not here, we are a 2.4 kernel
2727                 kill -STOP `pidof kupdated`
2728         fi
2729 }
2730
2731 # ensure that all stripes have some grant before we test client-side cache
2732 setup_test42() {
2733         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2734                 dd if=/dev/zero of=$i bs=4k count=1
2735                 rm $i
2736         done
2737 }
2738
2739 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
2740 # file truncation, and file removal.
2741 test_42a() {
2742         setup_test42
2743         cancel_lru_locks osc
2744         stop_writeback
2745         sync; sleep 1; sync # just to be safe
2746         BEFOREWRITES=`count_ost_writes`
2747         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
2748         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
2749         AFTERWRITES=`count_ost_writes`
2750         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2751                 error "$BEFOREWRITES < $AFTERWRITES"
2752         start_writeback
2753 }
2754 run_test 42a "ensure that we don't flush on close =============="
2755
2756 test_42b() {
2757         setup_test42
2758         cancel_lru_locks osc
2759         stop_writeback
2760         sync
2761         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
2762         BEFOREWRITES=`count_ost_writes`
2763         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
2764         AFTERWRITES=`count_ost_writes`
2765         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2766                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
2767         fi
2768         BEFOREWRITES=`count_ost_writes`
2769         sync || error "sync: $?"
2770         AFTERWRITES=`count_ost_writes`
2771         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2772                 error "$BEFOREWRITES < $AFTERWRITES on sync"
2773         fi
2774         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
2775         start_writeback
2776         return 0
2777 }
2778 run_test 42b "test destroy of file with cached dirty data ======"
2779
2780 # if these tests just want to test the effect of truncation,
2781 # they have to be very careful.  consider:
2782 # - the first open gets a {0,EOF}PR lock
2783 # - the first write conflicts and gets a {0, count-1}PW
2784 # - the rest of the writes are under {count,EOF}PW
2785 # - the open for truncate tries to match a {0,EOF}PR
2786 #   for the filesize and cancels the PWs.
2787 # any number of fixes (don't get {0,EOF} on open, match
2788 # composite locks, do smarter file size management) fix
2789 # this, but for now we want these tests to verify that
2790 # the cancellation with truncate intent works, so we
2791 # start the file with a full-file pw lock to match against
2792 # until the truncate.
2793 trunc_test() {
2794         test=$1
2795         file=$DIR/$test
2796         offset=$2
2797         cancel_lru_locks osc
2798         stop_writeback
2799         # prime the file with 0,EOF PW to match
2800         touch $file
2801         $TRUNCATE $file 0
2802         sync; sync
2803         # now the real test..
2804         dd if=/dev/zero of=$file bs=1024 count=100
2805         BEFOREWRITES=`count_ost_writes`
2806         $TRUNCATE $file $offset
2807         cancel_lru_locks osc
2808         AFTERWRITES=`count_ost_writes`
2809         start_writeback
2810 }
2811
2812 test_42c() {
2813         trunc_test 42c 1024
2814         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
2815             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
2816         rm $file
2817 }
2818 run_test 42c "test partial truncate of file with cached dirty data"
2819
2820 test_42d() {
2821         trunc_test 42d 0
2822         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2823             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
2824         rm $file
2825 }
2826 run_test 42d "test complete truncate of file with cached dirty data"
2827
2828 test_42e() { # bug22074
2829         local TDIR=$DIR/${tdir}e
2830         local pagesz=$(page_size)
2831         local pages=16 # hardcoded 16 pages, don't change it.
2832         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
2833         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
2834         local max_dirty_mb
2835         local warmup_files
2836
2837         mkdir -p $TDIR
2838         $SETSTRIPE -c 1 $TDIR
2839         createmany -o $TDIR/f $files
2840
2841         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
2842
2843         # we assume that with $OSTCOUNT files, at least one of them will
2844         # be allocated on OST0.
2845         warmup_files=$((OSTCOUNT * max_dirty_mb))
2846         createmany -o $TDIR/w $warmup_files
2847
2848         # write a large amount of data into one file and sync, to get good
2849         # avail_grant number from OST.
2850         for ((i=0; i<$warmup_files; i++)); do
2851                 idx=$($GETSTRIPE -i $TDIR/w$i)
2852                 [ $idx -ne 0 ] && continue
2853                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
2854                 break
2855         done
2856         [ $i -gt $warmup_files ] && error "OST0 is still cold"
2857         sync
2858         $LCTL get_param $proc_osc0/cur_dirty_bytes
2859         $LCTL get_param $proc_osc0/cur_grant_bytes
2860
2861         # create as much dirty pages as we can while not to trigger the actual
2862         # RPCs directly. but depends on the env, VFS may trigger flush during this
2863         # period, hopefully we are good.
2864         for ((i=0; i<$warmup_files; i++)); do
2865                 idx=$($GETSTRIPE -i $TDIR/w$i)
2866                 [ $idx -ne 0 ] && continue
2867                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
2868         done
2869         $LCTL get_param $proc_osc0/cur_dirty_bytes
2870         $LCTL get_param $proc_osc0/cur_grant_bytes
2871
2872         # perform the real test
2873         $LCTL set_param $proc_osc0/rpc_stats 0
2874         for ((;i<$files; i++)); do
2875                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
2876                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
2877         done
2878         sync
2879         $LCTL get_param $proc_osc0/rpc_stats
2880
2881         local percent=0
2882         local have_ppr=false
2883         $LCTL get_param $proc_osc0/rpc_stats |
2884                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
2885                         # skip lines until we are at the RPC histogram data
2886                         [ "$PPR" == "pages" ] && have_ppr=true && continue
2887                         $have_ppr || continue
2888
2889                         # we only want the percent stat for < 16 pages
2890                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
2891
2892                         percent=$((percent + WPCT))
2893                         if [ $percent -gt 15 ]; then
2894                                 error "less than 16-pages write RPCs" \
2895                                       "$percent% > 15%"
2896                                 break
2897                         fi
2898                 done
2899         rm -rf $TDIR
2900 }
2901 run_test 42e "verify sub-RPC writes are not done synchronously"
2902
2903 test_43() {
2904         mkdir -p $DIR/$tdir
2905         cp -p /bin/ls $DIR/$tdir/$tfile
2906         $MULTIOP $DIR/$tdir/$tfile Ow_c &
2907         pid=$!
2908         # give multiop a chance to open
2909         sleep 1
2910
2911         $DIR/$tdir/$tfile && error || true
2912         kill -USR1 $pid
2913 }
2914 run_test 43 "execution of file opened for write should return -ETXTBSY"
2915
2916 test_43a() {
2917         mkdir -p $DIR/d43
2918         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2919         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2920         MULTIOP_PID=$!
2921         $MULTIOP $DIR/d43/multiop Oc && error "expected error, got success"
2922         kill -USR1 $MULTIOP_PID || return 2
2923         wait $MULTIOP_PID || return 3
2924         rm $TMP/test43.junk
2925 }
2926 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
2927
2928 test_43b() {
2929         mkdir -p $DIR/d43
2930         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2931         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2932         MULTIOP_PID=$!
2933         $TRUNCATE $DIR/d43/multiop 0 && error "expected error, got success"
2934         kill -USR1 $MULTIOP_PID || return 2
2935         wait $MULTIOP_PID || return 3
2936         rm $TMP/test43.junk
2937 }
2938 run_test 43b "truncate of file being executed should return -ETXTBSY"
2939
2940 test_43c() {
2941         local testdir="$DIR/d43c"
2942         mkdir -p $testdir
2943         cp $SHELL $testdir/
2944         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
2945                 ( cd $testdir && md5sum -c)
2946 }
2947 run_test 43c "md5sum of copy into lustre========================"
2948
2949 test_44() {
2950         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
2951         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
2952         dd if=$DIR/f1 bs=4k count=1 > /dev/null
2953 }
2954 run_test 44 "zero length read from a sparse stripe ============="
2955
2956 test_44a() {
2957     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
2958                          awk '{print $2}'`
2959     [ -z "$nstripe" ] && skip "can't get stripe info" && return
2960     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
2961     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
2962                       awk '{print $2}'`
2963     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
2964         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
2965     fi
2966
2967     OFFSETS="0 $((stride/2)) $((stride-1))"
2968     for offset in $OFFSETS ; do
2969       for i in `seq 0 $((nstripe-1))`; do
2970         local GLOBALOFFSETS=""
2971         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
2972         local myfn=$DIR/d44a-$size
2973         echo "--------writing $myfn at $size"
2974         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
2975         GLOBALOFFSETS="$GLOBALOFFSETS $size"
2976         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2977                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2978
2979         for j in `seq 0 $((nstripe-1))`; do
2980             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
2981             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
2982             GLOBALOFFSETS="$GLOBALOFFSETS $size"
2983         done
2984         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2985                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2986         rm -f $myfn
2987       done
2988     done
2989 }
2990 run_test 44a "test sparse pwrite ==============================="
2991
2992 dirty_osc_total() {
2993         tot=0
2994         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
2995                 tot=$(($tot + $d))
2996         done
2997         echo $tot
2998 }
2999 do_dirty_record() {
3000         before=`dirty_osc_total`
3001         echo executing "\"$*\""
3002         eval $*
3003         after=`dirty_osc_total`
3004         echo before $before, after $after
3005 }
3006 test_45() {
3007         f="$DIR/f45"
3008         # Obtain grants from OST if it supports it
3009         echo blah > ${f}_grant
3010         stop_writeback
3011         sync
3012         do_dirty_record "echo blah > $f"
3013         [ $before -eq $after ] && error "write wasn't cached"
3014         do_dirty_record "> $f"
3015         [ $before -gt $after ] || error "truncate didn't lower dirty count"
3016         do_dirty_record "echo blah > $f"
3017         [ $before -eq $after ] && error "write wasn't cached"
3018         do_dirty_record "sync"
3019         [ $before -gt $after ] || error "writeback didn't lower dirty count"
3020         do_dirty_record "echo blah > $f"
3021         [ $before -eq $after ] && error "write wasn't cached"
3022         do_dirty_record "cancel_lru_locks osc"
3023         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
3024         start_writeback
3025 }
3026 run_test 45 "osc io page accounting ============================"
3027
3028 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3029 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3030 # objects offset and an assert hit when an rpc was built with 1023's mapped
3031 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3032 test_46() {
3033         f="$DIR/f46"
3034         stop_writeback
3035         sync
3036         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3037         sync
3038         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3039         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3040         sync
3041         start_writeback
3042 }
3043 run_test 46 "dirtying a previously written page ================"
3044
3045 # test_47 is removed "Device nodes check" is moved to test_28
3046
3047 test_48a() { # bug 2399
3048         check_kernel_version 34 || return 0
3049         mkdir -p $DIR/d48a
3050         cd $DIR/d48a
3051         mv $DIR/d48a $DIR/d48.new || error "move directory failed"
3052         mkdir $DIR/d48a || error "recreate directory failed"
3053         touch foo || error "'touch foo' failed after recreating cwd"
3054         mkdir bar || error "'mkdir foo' failed after recreating cwd"
3055         if check_kernel_version 44; then
3056                 touch .foo || error "'touch .foo' failed after recreating cwd"
3057                 mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3058         fi
3059         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3060         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3061         cd . || error "'cd .' failed after recreating cwd"
3062         mkdir . && error "'mkdir .' worked after recreating cwd"
3063         rmdir . && error "'rmdir .' worked after recreating cwd"
3064         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3065         cd .. || error "'cd ..' failed after recreating cwd"
3066 }
3067 run_test 48a "Access renamed working dir (should return errors)="
3068
3069 test_48b() { # bug 2399
3070         check_kernel_version 34 || return 0
3071         mkdir -p $DIR/d48b
3072         cd $DIR/d48b
3073         rmdir $DIR/d48b || error "remove cwd $DIR/d48b failed"
3074         touch foo && error "'touch foo' worked after removing cwd"
3075         mkdir foo && error "'mkdir foo' worked after removing cwd"
3076         if check_kernel_version 44; then
3077                 touch .foo && error "'touch .foo' worked after removing cwd"
3078                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3079         fi
3080         ls . > /dev/null && error "'ls .' worked after removing cwd"
3081         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3082         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3083         mkdir . && error "'mkdir .' worked after removing cwd"
3084         rmdir . && error "'rmdir .' worked after removing cwd"
3085         ln -s . foo && error "'ln -s .' worked after removing cwd"
3086         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3087 }
3088 run_test 48b "Access removed working dir (should return errors)="
3089
3090 test_48c() { # bug 2350
3091         check_kernel_version 36 || return 0
3092         #lctl set_param debug=-1
3093         #set -vx
3094         mkdir -p $DIR/d48c/dir
3095         cd $DIR/d48c/dir
3096         $TRACE rmdir $DIR/d48c/dir || error "remove cwd $DIR/d48c/dir failed"
3097         $TRACE touch foo && error "'touch foo' worked after removing cwd"
3098         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
3099         if check_kernel_version 44; then
3100                 touch .foo && error "'touch .foo' worked after removing cwd"
3101                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3102         fi
3103         $TRACE ls . && error "'ls .' worked after removing cwd"
3104         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3105         is_patchless || ( $TRACE cd . && error "'cd .' worked after removing cwd" )
3106         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
3107         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3108         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3109         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3110 }
3111 run_test 48c "Access removed working subdir (should return errors)"
3112
3113 test_48d() { # bug 2350
3114         check_kernel_version 36 || return 0
3115         #lctl set_param debug=-1
3116         #set -vx
3117         mkdir -p $DIR/d48d/dir
3118         cd $DIR/d48d/dir
3119         $TRACE rmdir $DIR/d48d/dir || error "remove cwd $DIR/d48d/dir failed"
3120         $TRACE rmdir $DIR/d48d || error "remove parent $DIR/d48d failed"
3121         $TRACE touch foo && error "'touch foo' worked after removing parent"
3122         $TRACE mkdir foo && error "'mkdir foo' worked after removing parent"
3123         if check_kernel_version 44; then
3124                 touch .foo && error "'touch .foo' worked after removing parent"
3125                 mkdir .foo && error "'mkdir .foo' worked after removing parent"
3126         fi
3127         $TRACE ls . && error "'ls .' worked after removing parent"
3128         $TRACE ls .. && error "'ls ..' worked after removing parent"
3129         is_patchless || ( $TRACE cd . && error "'cd .' worked after recreate parent" )
3130         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
3131         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3132         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3133         is_patchless || ( $TRACE cd .. && error "'cd ..' worked after removing parent" || true )
3134 }
3135 run_test 48d "Access removed parent subdir (should return errors)"
3136
3137 test_48e() { # bug 4134
3138         check_kernel_version 41 || return 0
3139         #lctl set_param debug=-1
3140         #set -vx
3141         mkdir -p $DIR/d48e/dir
3142         cd $DIR/d48e/dir
3143         $TRACE rmdir $DIR/d48e/dir || error "remove cwd $DIR/d48e/dir failed"
3144         $TRACE rmdir $DIR/d48e || error "remove parent $DIR/d48e failed"
3145         $TRACE touch $DIR/d48e || error "'touch $DIR/d48e' failed"
3146         $TRACE chmod +x $DIR/d48e || error "'chmod +x $DIR/d48e' failed"
3147         # On a buggy kernel addition of "touch foo" after cd .. will
3148         # produce kernel oops in lookup_hash_it
3149         touch ../foo && error "'cd ..' worked after recreate parent"
3150         cd $DIR
3151         $TRACE rm $DIR/d48e || error "rm '$DIR/d48e' failed"
3152 }
3153 run_test 48e "Access to recreated parent subdir (should return errors)"
3154
3155 test_49() { # LU-1030
3156         # get ost1 size - lustre-OST0000
3157         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3158         # write 800M at maximum
3159         [ $ost1_size -gt 819200 ] && ost1_size=819200
3160
3161         lfs setstripe -c 1 -i 0 $DIR/$tfile
3162         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3163         local dd_pid=$!
3164
3165         # change max_pages_per_rpc while writing the file
3166         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3167         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3168         # loop until dd process exits
3169         while ps ax -opid | grep -wq $dd_pid; do
3170                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3171                 sleep $((RANDOM % 5 + 1))
3172         done
3173         # restore original max_pages_per_rpc
3174         $LCTL set_param $osc1_mppc=$orig_mppc
3175         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3176 }
3177 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3178
3179 test_50() {
3180         # bug 1485
3181         mkdir $DIR/d50
3182         cd $DIR/d50
3183         ls /proc/$$/cwd || error
3184 }
3185 run_test 50 "special situations: /proc symlinks  ==============="
3186
3187 test_51a() {    # was test_51
3188         # bug 1516 - create an empty entry right after ".." then split dir
3189         mkdir $DIR/d51
3190         touch $DIR/d51/foo
3191         $MCREATE $DIR/d51/bar
3192         rm $DIR/d51/foo
3193         createmany -m $DIR/d51/longfile 201
3194         FNUM=202
3195         while [ `ls -sd $DIR/d51 | awk '{ print $1 }'` -eq 4 ]; do
3196                 $MCREATE $DIR/d51/longfile$FNUM
3197                 FNUM=$(($FNUM + 1))
3198                 echo -n "+"
3199         done
3200         echo
3201         ls -l $DIR/d51 > /dev/null || error
3202 }
3203 run_test 51a "special situations: split htree with empty entry =="
3204
3205 export NUMTEST=70000
3206 test_51b() {
3207         local BASE=$DIR/$tdir
3208         mkdir -p $BASE
3209
3210         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3211         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3212         [ $numfree -lt 21000 ] && skip "not enough free inodes ($numfree)" &&
3213                 return
3214
3215         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3216                 echo "reduced count to $NUMTEST due to inodes"
3217
3218         # need to check free space for the directories as well
3219         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3220         numfree=$((blkfree / 4))
3221         [ $numfree -lt $NUMTEST ] && NUMTEST=$(($numfree - 50)) &&
3222                 echo "reduced count to $NUMTEST due to blocks"
3223
3224         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3225                 echo "failed" > $BASE/fnum
3226 }
3227 run_test 51b "exceed 64k subdirectory nlink limit"
3228
3229 test_51ba() { # LU-993
3230         local BASE=$DIR/$tdir
3231         # unlink all but 100 subdirectories, then check it still works
3232         local LEFT=100
3233         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3234
3235         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3236         local DELETE=$((NUMTEST - LEFT))
3237
3238         # continue on to run this test even if 51b didn't finish,
3239         # just to delete the many subdirectories created.
3240         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3241
3242         # for ldiskfs the nlink count should be 1, but this is OSD specific
3243         # and so this is listed for informational purposes only
3244         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3245         unlinkmany -d $BASE/d $DELETE
3246         RC=$?
3247
3248         if [ $RC -ne 0 ]; then
3249                 if [ "$NUMPREV" == "failed" ]; then
3250                         skip "previous setup failed"
3251                         return 0
3252                 else
3253                         error "unlink of first $DELETE subdirs failed"
3254                         return $RC
3255                 fi
3256         fi
3257
3258         echo "nlink between: $(stat -c %h $BASE)"
3259         # trim the first line of ls output
3260         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3261         [ $FOUND -ne $LEFT ] &&
3262                 error "can't find subdirs: found only $FOUND/$LEFT"
3263
3264         unlinkmany -d $BASE/d $DELETE $LEFT ||
3265                 error "unlink of second $LEFT subdirs failed"
3266         # regardless of whether the backing filesystem tracks nlink accurately
3267         # or not, the nlink count shouldn't be more than "." and ".." here
3268         local AFTER=$(stat -c %h $BASE)
3269         [ $AFTER -gt 2 ] && error "nlink after: $AFTER > 2" ||
3270                 echo "nlink after: $AFTER"
3271 }
3272 run_test 51ba "verify nlink for many subdirectory cleanup"
3273
3274 test_51bb() {
3275         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3276
3277         local ndirs=${TEST51BB_NDIRS:-10}
3278         local nfiles=${TEST51BB_NFILES:-100}
3279
3280         local numfree=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3281
3282         [ $numfree -lt $(( ndirs * nfiles)) ] && \
3283                 nfiles=$(( numfree / ndirs - 10 ))
3284
3285         local dir=$DIR/d51bb
3286         mkdir -p $dir
3287         local savePOLICY=$(lctl get_param -n lmv.*.placement)
3288         lctl set_param -n lmv.*.placement=CHAR
3289
3290         lfs df -i $dir
3291         local IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3292         OLDUSED=($IUSED)
3293
3294         declare -a dirs
3295         for ((i=0; i < $ndirs; i++)); do
3296                 dirs[i]=$dir/$RANDOM
3297                 echo Creating directory ${dirs[i]}
3298                 mkdir -p ${dirs[i]}
3299                 ls $dir
3300                 echo Creating $nfiles in dir ${dirs[i]} ...
3301                 echo "createmany -o ${dirs[i]}/$tfile- $nfiles"
3302                 createmany -o ${dirs[i]}/$tfile- $nfiles
3303         done
3304         ls $dir
3305
3306         sleep 1
3307
3308         IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3309         NEWUSED=($IUSED)
3310
3311         local rc=0
3312         for ((i=0; i<${#NEWUSED[@]}; i++)); do
3313                 echo "mds $i: inodes count OLD ${OLDUSED[$i]} NEW ${NEWUSED[$i]}"
3314                 [ ${OLDUSED[$i]} -lt ${NEWUSED[$i]} ] || rc=$((rc + 1))
3315         done
3316
3317         lctl set_param -n lmv.*.placement=$savePOLICY
3318
3319         [ $rc -ne $MDSCOUNT ] || \
3320                 error "Objects/inodes are not distributed over all mds servers"
3321 }
3322 run_test 51bb "mkdir createmany CMD $MDSCOUNT  ===================="
3323
3324 test_51d() {
3325         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3326         mkdir -p $DIR/d51d
3327         createmany -o $DIR/d51d/t- 1000
3328         $GETSTRIPE $DIR/d51d > $TMP/files
3329         for N in `seq 0 $((OSTCOUNT - 1))`; do
3330             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3331             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3332             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3333         done
3334         unlinkmany $DIR/d51d/t- 1000
3335
3336         NLAST=0
3337         for N in `seq 1 $((OSTCOUNT - 1))`; do
3338             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3339                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3340             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3341                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3342
3343             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3344                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3345             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3346                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3347             NLAST=$N
3348         done
3349 }
3350 run_test 51d "check object distribution ===================="
3351
3352 test_52a() {
3353         [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
3354         mkdir -p $DIR/d52a
3355         touch $DIR/d52a/foo
3356         chattr +a $DIR/d52a/foo || error "chattr +a failed"
3357         echo bar >> $DIR/d52a/foo || error "append bar failed"
3358         cp /etc/hosts $DIR/d52a/foo && error "cp worked"
3359         rm -f $DIR/d52a/foo 2>/dev/null && error "rm worked"
3360         link $DIR/d52a/foo $DIR/d52a/foo_link 2>/dev/null && error "link worked"
3361         echo foo >> $DIR/d52a/foo || error "append foo failed"
3362         mrename $DIR/d52a/foo $DIR/d52a/foo_ren && error "rename worked"
3363         lsattr $DIR/d52a/foo | egrep -q "^-+a[-e]+ $DIR/d52a/foo" || error "lsattr"
3364         chattr -a $DIR/d52a/foo || error "chattr -a failed"
3365         cp -r $DIR/d52a /tmp/
3366         rm -fr $DIR/d52a || error "cleanup rm failed"
3367 }
3368 run_test 52a "append-only flag test (should return errors) ====="
3369
3370 test_52b() {
3371         [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
3372         mkdir -p $DIR/d52b
3373         touch $DIR/d52b/foo
3374         chattr +i $DIR/d52b/foo || error "chattr +i failed"
3375         cat test > $DIR/d52b/foo && error "cat test worked"
3376         cp /etc/hosts $DIR/d52b/foo && error "cp worked"
3377         rm -f $DIR/d52b/foo 2>/dev/null && error "rm worked"
3378         link $DIR/d52b/foo $DIR/d52b/foo_link 2>/dev/null && error  "link worked"
3379         echo foo >> $DIR/d52b/foo && error "echo worked"
3380         mrename $DIR/d52b/foo $DIR/d52b/foo_ren && error "rename worked"
3381         [ -f $DIR/d52b/foo ] || error
3382         [ -f $DIR/d52b/foo_ren ] && error
3383         lsattr $DIR/d52b/foo | egrep -q "^-+i[-e]+ $DIR/d52b/foo" || error "lsattr"
3384         chattr -i $DIR/d52b/foo || error "chattr failed"
3385
3386         rm -fr $DIR/d52b || error
3387 }
3388 run_test 52b "immutable flag test (should return errors) ======="
3389
3390 test_53() {
3391         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3392         remote_ost_nodsh && skip "remote OST with nodsh" && return
3393
3394         local param
3395         local ostname
3396         local mds_last
3397         local ost_last
3398         local ostnum
3399
3400         # only test MDT0000
3401         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3402         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3403                 param=`echo ${value[0]} | cut -d "=" -f1`
3404                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3405                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3406                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
3407                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id | head -n 1)
3408                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3409                 if [ $ost_last != $mds_last ]; then
3410                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3411                 fi
3412         done
3413 }
3414 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3415
3416 test_54a() {
3417         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3418         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3419         $SOCKETSERVER $DIR/socket
3420         $SOCKETCLIENT $DIR/socket || error
3421         $MUNLINK $DIR/socket
3422 }
3423 run_test 54a "unix domain socket test =========================="
3424
3425 test_54b() {
3426         f="$DIR/f54b"
3427         mknod $f c 1 3
3428         chmod 0666 $f
3429         dd if=/dev/zero of=$f bs=`page_size` count=1
3430 }
3431 run_test 54b "char device works in lustre ======================"
3432
3433 find_loop_dev() {
3434         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3435         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3436         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3437
3438         for i in `seq 3 7`; do
3439                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3440                 LOOPDEV=$LOOPBASE$i
3441                 LOOPNUM=$i
3442                 break
3443         done
3444 }
3445
3446 test_54c() {
3447         tfile="$DIR/f54c"
3448         tdir="$DIR/d54c"
3449         loopdev="$DIR/loop54c"
3450
3451         find_loop_dev
3452         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3453         mknod $loopdev b 7 $LOOPNUM
3454         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3455         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3456         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3457         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3458         mkdir -p $tdir
3459         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3460         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3461         df $tdir
3462         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3463         $UMOUNT $tdir
3464         losetup -d $loopdev
3465         rm $loopdev
3466 }
3467 run_test 54c "block device works in lustre ====================="
3468
3469 test_54d() {
3470         f="$DIR/f54d"
3471         string="aaaaaa"
3472         mknod $f p
3473         [ "$string" = `echo $string > $f | cat $f` ] || error
3474 }
3475 run_test 54d "fifo device works in lustre ======================"
3476
3477 test_54e() {
3478         check_kernel_version 46 || return 0
3479         f="$DIR/f54e"
3480         string="aaaaaa"
3481         cp -aL /dev/console $f
3482         echo $string > $f || error
3483 }
3484 run_test 54e "console/tty device works in lustre ======================"
3485
3486 #The test_55 used to be iopen test and it was removed by bz#24037.
3487 #run_test 55 "check iopen_connect_dentry() ======================"
3488
3489 test_56a() {    # was test_56
3490         rm -rf $DIR/d56
3491         $SETSTRIPE -d $DIR
3492         mkdir $DIR/d56
3493         mkdir $DIR/d56/dir
3494         NUMFILES=3
3495         NUMFILESx2=$(($NUMFILES * 2))
3496         for i in `seq 1 $NUMFILES` ; do
3497                 touch $DIR/d56/file$i
3498                 touch $DIR/d56/dir/file$i
3499         done
3500
3501         # test lfs getstripe with --recursive
3502         FILENUM=`$GETSTRIPE --recursive $DIR/d56 | grep -c obdidx`
3503         [ $FILENUM -eq $NUMFILESx2 ] ||
3504                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3505         FILENUM=`$GETSTRIPE $DIR/d56 | grep -c obdidx`
3506         [ $FILENUM -eq $NUMFILES ] ||
3507                 error "$GETSTRIPE $DIR/d56: found $FILENUM, not $NUMFILES"
3508         echo "$GETSTRIPE --recursive passed."
3509
3510         # test lfs getstripe with file instead of dir
3511         FILENUM=`$GETSTRIPE $DIR/d56/file1 | grep -c obdidx`
3512         [ $FILENUM  -eq 1 ] || error \
3513                  "$GETSTRIPE $DIR/d56/file1: found $FILENUM, not 1"
3514         echo "$GETSTRIPE file1 passed."
3515
3516         #test lfs getstripe with --verbose
3517         [ `$GETSTRIPE --verbose $DIR/d56 | grep -c lmm_magic` -eq $NUMFILES ] ||
3518                 error "$GETSTRIPE --verbose $DIR/d56: want $NUMFILES lmm_magic"
3519         [ `$GETSTRIPE $DIR/d56 | grep -c lmm_magic` -eq 0 ] ||
3520             error "$GETSTRIPE $DIR/d56: showed lmm_magic"
3521         echo "$GETSTRIPE --verbose passed."
3522
3523         #test lfs getstripe with --obd
3524         $GETSTRIPE --obd wrong_uuid $DIR/d56 2>&1 | grep -q "unknown obduuid" ||
3525                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3526
3527         [  "$OSTCOUNT" -lt 2 ] &&
3528                 skip_env "skipping other $GETSTRIPE --obd test" && return
3529
3530         OSTIDX=1
3531         OBDUUID=$(ostuuid_from_index $OSTIDX)
3532         FILENUM=`$GETSTRIPE -ir $DIR/d56 | grep -x $OSTIDX | wc -l`
3533         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/d56 | grep obdidx | wc -l`
3534         [ $FOUND -eq $FILENUM ] ||
3535                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3536         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/d56 |
3537                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3538                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3539                 error "$GETSTRIPE --obd: should not show file on other obd"
3540         echo "$GETSTRIPE --obd passed"
3541 }
3542 run_test 56a "check $GETSTRIPE"
3543
3544 NUMFILES=3
3545 NUMDIRS=3
3546 setup_56() {
3547         local LOCAL_NUMFILES="$1"
3548         local LOCAL_NUMDIRS="$2"
3549         local MKDIR_PARAMS="$3"
3550
3551         if [ ! -d "$TDIR" ] ; then
3552                 mkdir -p $TDIR
3553                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3554                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3555                         touch $TDIR/file$i
3556                 done
3557                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3558                         mkdir $TDIR/dir$i
3559                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3560                                 touch $TDIR/dir$i/file$j
3561                         done
3562                 done
3563         fi
3564 }
3565
3566 setup_56_special() {
3567         LOCAL_NUMFILES=$1
3568         LOCAL_NUMDIRS=$2
3569         setup_56 $1 $2
3570         if [ ! -e "$TDIR/loop1b" ] ; then
3571                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3572                         mknod $TDIR/loop${i}b b 7 $i
3573                         mknod $TDIR/null${i}c c 1 3
3574                         ln -s $TDIR/file1 $TDIR/link${i}l
3575                 done
3576                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3577                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3578                         mknod $TDIR/dir$i/null${i}c c 1 3
3579                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3580                 done
3581         fi
3582 }
3583
3584 test_56g() {
3585         $SETSTRIPE -d $DIR
3586
3587         TDIR=$DIR/${tdir}g
3588         setup_56 $NUMFILES $NUMDIRS
3589
3590         EXPECTED=$(($NUMDIRS + 2))
3591         # test lfs find with -name
3592         for i in $(seq 1 $NUMFILES) ; do
3593                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3594                 [ $NUMS -eq $EXPECTED ] ||
3595                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3596                               "found $NUMS, expected $EXPECTED"
3597         done
3598 }
3599 run_test 56g "check lfs find -name ============================="
3600
3601 test_56h() {
3602         $SETSTRIPE -d $DIR
3603
3604         TDIR=$DIR/${tdir}g
3605         setup_56 $NUMFILES $NUMDIRS
3606
3607         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3608         # test lfs find with ! -name
3609         for i in $(seq 1 $NUMFILES) ; do
3610                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3611                 [ $NUMS -eq $EXPECTED ] ||
3612                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3613                               "found $NUMS, expected $EXPECTED"
3614         done
3615 }
3616 run_test 56h "check lfs find ! -name ============================="
3617
3618 test_56i() {
3619        tdir=${tdir}i
3620        mkdir -p $DIR/$tdir
3621        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3622        CMD="$LFIND -ost $UUID $DIR/$tdir"
3623        OUT=$($CMD)
3624        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3625 }
3626 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3627
3628 test_56j() {
3629         TDIR=$DIR/${tdir}g
3630         setup_56_special $NUMFILES $NUMDIRS
3631
3632         EXPECTED=$((NUMDIRS + 1))
3633         CMD="$LFIND -type d $TDIR"
3634         NUMS=$($CMD | wc -l)
3635         [ $NUMS -eq $EXPECTED ] ||
3636                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3637 }
3638 run_test 56j "check lfs find -type d ============================="
3639
3640 test_56k() {
3641         TDIR=$DIR/${tdir}g
3642         setup_56_special $NUMFILES $NUMDIRS
3643
3644         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3645         CMD="$LFIND -type f $TDIR"
3646         NUMS=$($CMD | wc -l)
3647         [ $NUMS -eq $EXPECTED ] ||
3648                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3649 }
3650 run_test 56k "check lfs find -type f ============================="
3651
3652 test_56l() {
3653         TDIR=$DIR/${tdir}g
3654         setup_56_special $NUMFILES $NUMDIRS
3655
3656         EXPECTED=$((NUMDIRS + NUMFILES))
3657         CMD="$LFIND -type b $TDIR"
3658         NUMS=$($CMD | wc -l)
3659         [ $NUMS -eq $EXPECTED ] ||
3660                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3661 }
3662 run_test 56l "check lfs find -type b ============================="
3663
3664 test_56m() {
3665         TDIR=$DIR/${tdir}g
3666         setup_56_special $NUMFILES $NUMDIRS
3667
3668         EXPECTED=$((NUMDIRS + NUMFILES))
3669         CMD="$LFIND -type c $TDIR"
3670         NUMS=$($CMD | wc -l)
3671         [ $NUMS -eq $EXPECTED ] ||
3672                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3673 }
3674 run_test 56m "check lfs find -type c ============================="
3675
3676 test_56n() {
3677         TDIR=$DIR/${tdir}g
3678         setup_56_special $NUMFILES $NUMDIRS
3679
3680         EXPECTED=$((NUMDIRS + NUMFILES))
3681         CMD="$LFIND -type l $TDIR"
3682         NUMS=$($CMD | wc -l)
3683         [ $NUMS -eq $EXPECTED ] ||
3684                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3685 }
3686 run_test 56n "check lfs find -type l ============================="
3687
3688 test_56o() {
3689         TDIR=$DIR/${tdir}o
3690         setup_56 $NUMFILES $NUMDIRS
3691
3692         utime $TDIR/file1 > /dev/null || error "utime (1)"
3693         utime $TDIR/file2 > /dev/null || error "utime (2)"
3694         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3695         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3696         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3697         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3698
3699         EXPECTED=4
3700         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3701         [ $NUMS -eq $EXPECTED ] || \
3702                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3703
3704         EXPECTED=12
3705         CMD="$LFIND -mtime 0 $TDIR"
3706         NUMS=$($CMD | wc -l)
3707         [ $NUMS -eq $EXPECTED ] ||
3708                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3709 }
3710 run_test 56o "check lfs find -mtime for old files =========================="
3711
3712 test_56p() {
3713         [ $RUNAS_ID -eq $UID ] &&
3714                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3715
3716         TDIR=$DIR/${tdir}p
3717         setup_56 $NUMFILES $NUMDIRS
3718
3719         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3720         EXPECTED=$NUMFILES
3721         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3722         NUMS=$($CMD | wc -l)
3723         [ $NUMS -eq $EXPECTED ] || \
3724                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3725
3726         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3727         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3728         NUMS=$($CMD | wc -l)
3729         [ $NUMS -eq $EXPECTED ] || \
3730                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3731 }
3732 run_test 56p "check lfs find -uid and ! -uid ==============================="
3733
3734 test_56q() {
3735         [ $RUNAS_ID -eq $UID ] &&
3736                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3737
3738         TDIR=$DIR/${tdir}q
3739         setup_56 $NUMFILES $NUMDIRS
3740
3741         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3742
3743         EXPECTED=$NUMFILES
3744         CMD="$LFIND -gid $RUNAS_GID $TDIR"
3745         NUMS=$($CMD | wc -l)
3746         [ $NUMS -eq $EXPECTED ] ||
3747                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3748
3749         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
3750         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
3751         NUMS=$($CMD | wc -l)
3752         [ $NUMS -eq $EXPECTED ] ||
3753                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3754 }
3755 run_test 56q "check lfs find -gid and ! -gid ==============================="
3756
3757 test_56r() {
3758         TDIR=$DIR/${tdir}r
3759         setup_56 $NUMFILES $NUMDIRS
3760
3761         EXPECTED=12
3762         CMD="$LFIND -size 0 -type f $TDIR"
3763         NUMS=$($CMD | wc -l)
3764         [ $NUMS -eq $EXPECTED ] ||
3765                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3766         EXPECTED=0
3767         CMD="$LFIND ! -size 0 -type f $TDIR"
3768         NUMS=$($CMD | wc -l)
3769         [ $NUMS -eq $EXPECTED ] ||
3770                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3771         echo "test" > $TDIR/$tfile
3772         echo "test2" > $TDIR/$tfile.2 && sync
3773         EXPECTED=1
3774         CMD="$LFIND -size 5 -type f $TDIR"
3775         NUMS=$($CMD | wc -l)
3776         [ $NUMS -eq $EXPECTED ] ||
3777                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3778         EXPECTED=1
3779         CMD="$LFIND -size +5 -type f $TDIR"
3780         NUMS=$($CMD | wc -l)
3781         [ $NUMS -eq $EXPECTED ] ||
3782                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3783         EXPECTED=2
3784         CMD="$LFIND -size +0 -type f $TDIR"
3785         NUMS=$($CMD | wc -l)
3786         [ $NUMS -eq $EXPECTED ] ||
3787                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3788         EXPECTED=2
3789         CMD="$LFIND ! -size -5 -type f $TDIR"
3790         NUMS=$($CMD | wc -l)
3791         [ $NUMS -eq $EXPECTED ] ||
3792                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3793         EXPECTED=12
3794         CMD="$LFIND -size -5 -type f $TDIR"
3795         NUMS=$($CMD | wc -l)
3796         [ $NUMS -eq $EXPECTED ] ||
3797                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3798 }
3799 run_test 56r "check lfs find -size works =========================="
3800
3801 test_56s() { # LU-611
3802         TDIR=$DIR/${tdir}s
3803         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3804
3805         if [ $OSTCOUNT -gt 1 ]; then
3806                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
3807                 ONESTRIPE=4
3808                 EXTRA=4
3809         else
3810                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
3811                 EXTRA=0
3812         fi
3813
3814         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3815         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
3816         NUMS=$($CMD | wc -l)
3817         [ $NUMS -eq $EXPECTED ] ||
3818                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3819
3820         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
3821         CMD="$LFIND -stripe-count +0 -type f $TDIR"
3822         NUMS=$($CMD | wc -l)
3823         [ $NUMS -eq $EXPECTED ] ||
3824                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3825
3826         EXPECTED=$ONESTRIPE
3827         CMD="$LFIND -stripe-count 1 -type f $TDIR"
3828         NUMS=$($CMD | wc -l)
3829         [ $NUMS -eq $EXPECTED ] ||
3830                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3831
3832         CMD="$LFIND -stripe-count -2 -type f $TDIR"
3833         NUMS=$($CMD | wc -l)
3834         [ $NUMS -eq $EXPECTED ] ||
3835                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3836
3837         EXPECTED=0
3838         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
3839         NUMS=$($CMD | wc -l)
3840         [ $NUMS -eq $EXPECTED ] ||
3841                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3842 }
3843 run_test 56s "check lfs find -stripe-count works"
3844
3845 test_56t() { # LU-611
3846         TDIR=$DIR/${tdir}t
3847         setup_56 $NUMFILES $NUMDIRS "-s 512k"
3848
3849         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
3850
3851         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3852         CMD="$LFIND -stripe-size 512k -type f $TDIR"
3853         NUMS=$($CMD | wc -l)
3854         [ $NUMS -eq $EXPECTED ] ||
3855                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3856
3857         CMD="$LFIND -stripe-size +320k -type f $TDIR"
3858         NUMS=$($CMD | wc -l)
3859         [ $NUMS -eq $EXPECTED ] ||
3860                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3861
3862         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
3863         CMD="$LFIND -stripe-size +200k -type f $TDIR"
3864         NUMS=$($CMD | wc -l)
3865         [ $NUMS -eq $EXPECTED ] ||
3866                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3867
3868         CMD="$LFIND -stripe-size -640k -type f $TDIR"
3869         NUMS=$($CMD | wc -l)
3870         [ $NUMS -eq $EXPECTED ] ||
3871                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3872
3873         EXPECTED=4
3874         CMD="$LFIND -stripe-size 256k -type f $TDIR"
3875         NUMS=$($CMD | wc -l)
3876         [ $NUMS -eq $EXPECTED ] ||
3877                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3878
3879         CMD="$LFIND -stripe-size -320k -type f $TDIR"
3880         NUMS=$($CMD | wc -l)
3881         [ $NUMS -eq $EXPECTED ] ||
3882                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3883
3884         EXPECTED=0
3885         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
3886         NUMS=$($CMD | wc -l)
3887         [ $NUMS -eq $EXPECTED ] ||
3888                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3889 }
3890 run_test 56t "check lfs find -stripe-size works"
3891
3892 test_56u() { # LU-611
3893         TDIR=$DIR/${tdir}u
3894         setup_56 $NUMFILES $NUMDIRS "-i 0"
3895
3896         if [ $OSTCOUNT -gt 1 ]; then
3897                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
3898                 ONESTRIPE=4
3899         else
3900                 ONESTRIPE=0
3901         fi
3902
3903         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3904         CMD="$LFIND -stripe-index 0 -type f $TDIR"
3905         NUMS=$($CMD | wc -l)
3906         [ $NUMS -eq $EXPECTED ] ||
3907                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3908
3909         EXPECTED=$ONESTRIPE
3910         CMD="$LFIND -stripe-index 1 -type f $TDIR"
3911         NUMS=$($CMD | wc -l)
3912         [ $NUMS -eq $EXPECTED ] ||
3913                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3914
3915         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
3916         NUMS=$($CMD | wc -l)
3917         [ $NUMS -eq $EXPECTED ] ||
3918                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3919
3920         EXPECTED=0
3921         # This should produce an error and not return any files
3922         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
3923         NUMS=$($CMD 2>/dev/null | wc -l)
3924         [ $NUMS -eq $EXPECTED ] ||
3925                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3926
3927         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
3928         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
3929         NUMS=$($CMD | wc -l)
3930         [ $NUMS -eq $EXPECTED ] ||
3931                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3932 }
3933 run_test 56u "check lfs find -stripe-index works"
3934
3935 test_56v() {
3936     local MDT_IDX=0
3937
3938     TDIR=$DIR/${tdir}v
3939     rm -rf $TDIR
3940     setup_56 $NUMFILES $NUMDIRS
3941
3942     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
3943     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
3944
3945     for file in $($LFIND -mdt $UUID $TDIR); do
3946         file_mdt_idx=$($GETSTRIPE -M $file)
3947         [ $file_mdt_idx -eq $MDT_IDX ] ||
3948             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
3949     done
3950 }
3951 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
3952
3953 # Get and check the actual stripe count of one file.
3954 # Usage: check_stripe_count <file> <expected_stripe_count>
3955 check_stripe_count() {
3956     local file=$1
3957     local expected=$2
3958     local actual
3959
3960     [[ -z "$file" || -z "$expected" ]] &&
3961         error "check_stripe_count: invalid argument!"
3962
3963     local cmd="$GETSTRIPE -c $file"
3964     actual=$($cmd) || error "$cmd failed"
3965     actual=${actual%% *}
3966
3967     if [[ $actual -ne $expected ]]; then
3968         [[ $expected -eq -1 ]] ||
3969             error "$cmd wrong: found $actual, expected $expected"
3970         [[ $actual -eq $OSTCOUNT ]] ||
3971             error "$cmd wrong: found $actual, expected $OSTCOUNT"
3972     fi
3973 }
3974
3975 test_56w() {
3976     TDIR=$DIR/${tdir}w
3977
3978     rm -rf $TDIR || error "remove $TDIR failed"
3979     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3980
3981     local stripe_size
3982     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
3983         error "$GETSTRIPE -S -d $TDIR failed"
3984     stripe_size=${stripe_size%% *}
3985
3986     local file_size=$((stripe_size * OSTCOUNT))
3987     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
3988     local required_space=$((file_num * file_size))
3989     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
3990     [[ $free_space -le $((required_space / 1024)) ]] &&
3991         skip_env "need at least $required_space bytes free space," \
3992                  "have $free_space kbytes" && return
3993
3994     local dd_bs=65536
3995     local dd_count=$((file_size / dd_bs))
3996
3997     # write data into the files
3998     local i
3999     local j
4000     local file
4001     for i in $(seq 1 $NUMFILES); do
4002         file=$TDIR/file$i
4003         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4004             error "write data into $file failed"
4005     done
4006     for i in $(seq 1 $NUMDIRS); do
4007         for j in $(seq 1 $NUMFILES); do
4008             file=$TDIR/dir$i/file$j
4009             yes | dd bs=$dd_bs count=$dd_count of=$file \
4010                 >/dev/null 2>&1 ||
4011                 error "write data into $file failed"
4012         done
4013     done
4014
4015     local expected=-1
4016     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4017
4018     # lfs_migrate file
4019     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4020     echo "$cmd"
4021     eval $cmd || error "$cmd failed"
4022
4023     check_stripe_count $TDIR/file1 $expected
4024
4025     # lfs_migrate dir
4026     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4027     echo "$cmd"
4028     eval $cmd || error "$cmd failed"
4029
4030     for j in $(seq 1 $NUMFILES); do
4031         check_stripe_count $TDIR/dir1/file$j $expected
4032     done
4033
4034     # lfs_migrate works with lfs find
4035     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4036          $LFS_MIGRATE -y -c $expected"
4037     echo "$cmd"
4038     eval $cmd || error "$cmd failed"
4039
4040     for i in $(seq 2 $NUMFILES); do
4041         check_stripe_count $TDIR/file$i $expected
4042     done
4043     for i in $(seq 2 $NUMDIRS); do
4044         for j in $(seq 1 $NUMFILES); do
4045             check_stripe_count $TDIR/dir$i/file$j $expected
4046         done
4047     done
4048 }
4049 run_test 56w "check lfs_migrate -c stripe_count works"
4050
4051 test_57a() {
4052         # note test will not do anything if MDS is not local
4053         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4054                 skip "Only applicable to ldiskfs-based MDTs"
4055                 return
4056         fi
4057
4058         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4059         local MNTDEV="osd*.*MDT*.mntdev"
4060         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4061         [ -z "$DEV" ] && error "can't access $MNTDEV"
4062         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4063                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4064                         error "can't access $DEV"
4065                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
4066                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
4067                 rm $TMP/t57a.dump
4068         done
4069 }
4070 run_test 57a "verify MDS filesystem created with large inodes =="
4071
4072 test_57b() {
4073         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4074                 skip "Only applicable to ldiskfs-based MDTs"
4075                 return
4076         fi
4077
4078         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4079         local dir=$DIR/d57b
4080
4081         local FILECOUNT=100
4082         local FILE1=$dir/f1
4083         local FILEN=$dir/f$FILECOUNT
4084
4085         rm -rf $dir || error "removing $dir"
4086         mkdir -p $dir || error "creating $dir"
4087         local num=$(get_mds_dir $dir)
4088         local mymds=mds$num
4089
4090         echo "mcreating $FILECOUNT files"
4091         createmany -m $dir/f 1 $FILECOUNT || \
4092                 error "creating files in $dir"
4093
4094         # verify that files do not have EAs yet
4095         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4096         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4097
4098         sync
4099         sleep 1
4100         df $dir  #make sure we get new statfs data
4101         local MDSFREE=$(do_facet $mymds \
4102                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4103         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4104         echo "opening files to create objects/EAs"
4105         local FILE
4106         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4107                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4108         done
4109
4110         # verify that files have EAs now
4111         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4112         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4113
4114         sleep 1  #make sure we get new statfs data
4115         df $dir
4116         local MDSFREE2=$(do_facet $mymds \
4117                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4118         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4119         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4120                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4121                         error "MDC before $MDCFREE != after $MDCFREE2"
4122                 else
4123                         echo "MDC before $MDCFREE != after $MDCFREE2"
4124                         echo "unable to confirm if MDS has large inodes"
4125                 fi
4126         fi
4127         rm -rf $dir
4128 }
4129 run_test 57b "default LOV EAs are stored inside large inodes ==="
4130
4131 test_58() {
4132     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
4133     wiretest
4134 }
4135 run_test 58 "verify cross-platform wire constants =============="
4136
4137 test_59() {
4138         echo "touch 130 files"
4139         createmany -o $DIR/f59- 130
4140         echo "rm 130 files"
4141         unlinkmany $DIR/f59- 130
4142         sync
4143         # wait for commitment of removal
4144         wait_delete_completed
4145 }
4146 run_test 59 "verify cancellation of llog records async ========="
4147
4148 TEST60_HEAD="test_60 run $RANDOM"
4149 test_60a() {
4150         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4151         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4152         log "$TEST60_HEAD - from kernel mode"
4153         do_facet mgs sh run-llog.sh
4154 }
4155 run_test 60a "llog sanity tests run from kernel module =========="
4156
4157 test_60b() { # bug 6411
4158         dmesg > $DIR/$tfile
4159         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4160                                  /llog.test/ {
4161                                          if (marker)
4162                                                  from_marker++
4163                                          from_begin++
4164                                  }
4165                                  END {
4166                                          if (marker)
4167                                                  print from_marker
4168                                          else
4169                                                  print from_begin
4170                                  }"`
4171         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4172 }
4173 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4174
4175 test_60c() {
4176         echo "create 5000 files"
4177         createmany -o $DIR/f60c- 5000
4178 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4179         lctl set_param fail_loc=0x80000137
4180         unlinkmany $DIR/f60c- 5000
4181         lctl set_param fail_loc=0
4182 }
4183 run_test 60c "unlink file when mds full"
4184
4185 test_60d() {
4186         SAVEPRINTK=$(lctl get_param -n printk)
4187
4188         # verify "lctl mark" is even working"
4189         MESSAGE="test message ID $RANDOM $$"
4190         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4191         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4192
4193         lctl set_param printk=0 || error "set lnet.printk failed"
4194         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4195         MESSAGE="new test message ID $RANDOM $$"
4196         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4197         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4198         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4199
4200         lctl set_param -n printk="$SAVEPRINTK"
4201 }
4202 run_test 60d "test printk console message masking"
4203
4204 test_61() {
4205         f="$DIR/f61"
4206         dd if=/dev/zero of=$f bs=`page_size` count=1
4207         cancel_lru_locks osc
4208         $MULTIOP $f OSMWUc || error
4209         sync
4210 }
4211 run_test 61 "mmap() writes don't make sync hang ================"
4212
4213 # bug 2330 - insufficient obd_match error checking causes LBUG
4214 test_62() {
4215         f="$DIR/f62"
4216         echo foo > $f
4217         cancel_lru_locks osc
4218         lctl set_param fail_loc=0x405
4219         cat $f && error "cat succeeded, expect -EIO"
4220         lctl set_param fail_loc=0
4221 }
4222 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4223 # match every page all of the time.
4224 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4225
4226 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4227 test_63a() {    # was test_63
4228         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4229         lctl set_param -n osc.*.max_dirty_mb 0
4230         for i in `seq 10` ; do
4231                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4232                 sleep 5
4233                 kill $!
4234                 sleep 1
4235         done
4236
4237         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4238         rm -f $DIR/f63 || true
4239 }
4240 run_test 63a "Verify oig_wait interruption does not crash ======="
4241
4242 # bug 2248 - async write errors didn't return to application on sync
4243 # bug 3677 - async write errors left page locked
4244 test_63b() {
4245         debugsave
4246         lctl set_param debug=-1
4247
4248         # ensure we have a grant to do async writes
4249         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4250         rm $DIR/$tfile
4251
4252         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4253         lctl set_param fail_loc=0x80000406
4254         $MULTIOP $DIR/$tfile Owy && \
4255                 error "sync didn't return ENOMEM"
4256         sync; sleep 2; sync     # do a real sync this time to flush page
4257         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4258                 error "locked page left in cache after async error" || true
4259         debugrestore
4260 }
4261 run_test 63b "async write errors should be returned to fsync ==="
4262
4263 test_64a () {
4264         df $DIR
4265         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4266 }
4267 run_test 64a "verify filter grant calculations (in kernel) ====="
4268
4269 test_64b () {
4270         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4271         sh oos.sh $MOUNT
4272 }
4273 run_test 64b "check out-of-space detection on client ==========="
4274
4275 test_64c() {
4276         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
4277 }
4278 run_test 64c "verify grant shrink ========================------"
4279
4280 # bug 1414 - set/get directories' stripe info
4281 test_65a() {
4282         mkdir -p $DIR/d65
4283         touch $DIR/d65/f1
4284         $LVERIFY $DIR/d65 $DIR/d65/f1 || error "lverify failed"
4285 }
4286 run_test 65a "directory with no stripe info ===================="
4287
4288 test_65b() {
4289         mkdir -p $DIR/d65
4290         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65||error "setstripe"
4291         touch $DIR/d65/f2
4292         $LVERIFY $DIR/d65 $DIR/d65/f2 || error "lverify failed"
4293 }
4294 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4295
4296 test_65c() {
4297         if [ $OSTCOUNT -gt 1 ]; then
4298                 mkdir -p $DIR/d65
4299                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4300                         -c $(($OSTCOUNT - 1)) $DIR/d65 || error "setstripe"
4301                 touch $DIR/d65/f3
4302                 $LVERIFY $DIR/d65 $DIR/d65/f3 || error "lverify failed"
4303         fi
4304 }
4305 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4306
4307 test_65d() {
4308         mkdir -p $DIR/d65
4309         if [ $STRIPECOUNT -le 0 ]; then
4310                 sc=1
4311         elif [ $STRIPECOUNT -gt 2000 ]; then
4312 #LOV_MAX_STRIPE_COUNT is 2000
4313                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4314         else
4315                 sc=$(($STRIPECOUNT - 1))
4316         fi
4317         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/d65 || error "setstripe"
4318         touch $DIR/d65/f4 $DIR/d65/f5
4319         $LVERIFY $DIR/d65 $DIR/d65/f4 $DIR/d65/f5 || error "lverify failed"
4320 }
4321 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4322
4323 test_65e() {
4324         mkdir -p $DIR/d65
4325
4326         $SETSTRIPE $DIR/d65 || error "setstripe"
4327         $GETSTRIPE -v $DIR/d65 | grep "Default" || error "no stripe info failed"
4328         touch $DIR/d65/f6
4329         $LVERIFY $DIR/d65 $DIR/d65/f6 || error "lverify failed"
4330 }
4331 run_test 65e "directory setstripe defaults ======================="
4332
4333 test_65f() {
4334         mkdir -p $DIR/d65f
4335         $RUNAS $SETSTRIPE $DIR/d65f && error "setstripe succeeded" || true
4336 }
4337 run_test 65f "dir setstripe permission (should return error) ==="
4338
4339 test_65g() {
4340         mkdir -p $DIR/d65
4341         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4342         $SETSTRIPE -d $DIR/d65 || error "setstripe"
4343         $GETSTRIPE -v $DIR/d65 | grep "Default" || \
4344                 error "delete default stripe failed"
4345 }
4346 run_test 65g "directory setstripe -d ==========================="
4347
4348 test_65h() {
4349         mkdir -p $DIR/d65
4350         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4351         mkdir -p $DIR/d65/dd1
4352         [ $($GETSTRIPE -c $DIR/d65) == $($GETSTRIPE -c $DIR/d65/dd1) ] ||
4353                 error "stripe info inherit failed"
4354 }
4355 run_test 65h "directory stripe info inherit ===================="
4356
4357 test_65i() { # bug6367
4358         $SETSTRIPE -S 65536 -c -1 $MOUNT
4359 }
4360 run_test 65i "set non-default striping on root directory (bug 6367)="
4361
4362 test_65ia() { # bug12836
4363         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4364 }
4365 run_test 65ia "getstripe on -1 default directory striping"
4366
4367 test_65ib() { # bug12836
4368         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4369 }
4370 run_test 65ib "getstripe -v on -1 default directory striping"
4371
4372 test_65ic() { # bug12836
4373         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4374 }
4375 run_test 65ic "new find on -1 default directory striping"
4376
4377 test_65j() { # bug6367
4378         sync; sleep 1
4379         # if we aren't already remounting for each test, do so for this test
4380         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4381                 cleanup || error "failed to unmount"
4382                 setup
4383         fi
4384         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4385 }
4386 run_test 65j "set default striping on root directory (bug 6367)="
4387
4388 test_65k() { # bug11679
4389     [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4390     remote_mds_nodsh && skip "remote MDS with nodsh" && return
4391
4392     echo "Check OST status: "
4393     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4394               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4395
4396     for OSC in $MDS_OSCS; do
4397         echo $OSC "is activate"
4398         do_facet $SINGLEMDS lctl --device %$OSC activate
4399     done
4400
4401     do_facet client mkdir -p $DIR/$tdir
4402     for INACTIVE_OSC in $MDS_OSCS; do
4403         echo "Deactivate: " $INACTIVE_OSC
4404         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4405         for STRIPE_OSC in $MDS_OSCS; do
4406             OST=`osc_to_ost $STRIPE_OSC`
4407             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4408                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4409
4410             [ -f $DIR/$tdir/$IDX ] && continue
4411             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4412             do_facet client $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4413             RC=$?
4414             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4415         done
4416         do_facet client rm -f $DIR/$tdir/*
4417         echo $INACTIVE_OSC "is Activate."
4418         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4419     done
4420 }
4421 run_test 65k "validate manual striping works properly with deactivated OSCs"
4422
4423 test_65l() { # bug 12836
4424         mkdir -p $DIR/$tdir/test_dir
4425         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4426         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4427 }
4428 run_test 65l "lfs find on -1 stripe dir ========================"
4429
4430 # bug 2543 - update blocks count on client
4431 test_66() {
4432         COUNT=${COUNT:-8}
4433         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4434         sync; sync_all_data; sync; sync_all_data
4435         cancel_lru_locks osc
4436         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4437         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4438 }
4439 run_test 66 "update inode blocks count on client ==============="
4440
4441 LLOOP=
4442 LLITELOOPLOAD=
4443 cleanup_68() {
4444         trap 0
4445         if [ ! -z "$LLOOP" ]; then
4446                 if swapon -s | grep -q $LLOOP; then
4447                         swapoff $LLOOP || error "swapoff failed"
4448                 fi
4449
4450                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4451                 rm -f $LLOOP
4452                 unset LLOOP
4453         fi
4454         if [ ! -z "$LLITELOOPLOAD" ]; then
4455                 rmmod llite_lloop
4456                 unset LLITELOOPLOAD
4457         fi
4458         rm -f $DIR/f68*
4459 }
4460
4461 meminfo() {
4462         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4463 }
4464
4465 swap_used() {
4466         swapon -s | awk '($1 == "'$1'") { print $4 }'
4467 }
4468
4469 # test case for lloop driver, basic function
4470 test_68a() {
4471         [ "$UID" != 0 ] && skip_env "must run as root" && return
4472         llite_lloop_enabled || \
4473                 { skip_env "llite_lloop module disabled" && return; }
4474
4475         trap cleanup_68 EXIT
4476
4477         if ! module_loaded llite_lloop; then
4478                 if load_module llite/llite_lloop; then
4479                         LLITELOOPLOAD=yes
4480                 else
4481                         skip_env "can't find module llite_lloop"
4482                         return
4483                 fi
4484         fi
4485
4486         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4487         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4488         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4489
4490         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4491         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4492
4493         cleanup_68
4494 }
4495 run_test 68a "lloop driver - basic test ========================"
4496
4497 # excercise swapping to lustre by adding a high priority swapfile entry
4498 # and then consuming memory until it is used.
4499 test_68b() {  # was test_68
4500         [ "$UID" != 0 ] && skip_env "must run as root" && return
4501         lctl get_param -n devices | grep -q obdfilter && \
4502                 skip "local OST" && return
4503
4504         grep -q llite_lloop /proc/modules
4505         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4506
4507         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4508                 skip "can't reliably test swap with TCP" && return
4509
4510         MEMTOTAL=`meminfo MemTotal`
4511         NR_BLOCKS=$((MEMTOTAL>>8))
4512         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4513
4514         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4515         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4516         mkswap $DIR/f68b
4517
4518         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4519
4520         trap cleanup_68 EXIT
4521
4522         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4523
4524         echo "before: `swapon -s | grep $LLOOP`"
4525         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4526         echo "after: `swapon -s | grep $LLOOP`"
4527         SWAPUSED=`swap_used $LLOOP`
4528
4529         cleanup_68
4530
4531         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4532 }
4533 run_test 68b "support swapping to Lustre ========================"
4534
4535 # bug5265, obdfilter oa2dentry return -ENOENT
4536 # #define OBD_FAIL_OST_ENOENT 0x217
4537 test_69() {
4538         remote_ost_nodsh && skip "remote OST with nodsh" && return
4539
4540         f="$DIR/$tfile"
4541         $SETSTRIPE -c 1 -i 0 $f
4542
4543         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4544
4545         do_facet ost1 lctl set_param fail_loc=0x217
4546         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4547         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4548
4549         do_facet ost1 lctl set_param fail_loc=0
4550         $DIRECTIO write $f 0 2 || error "write error"
4551
4552         cancel_lru_locks osc
4553         $DIRECTIO read $f 0 1 || error "read error"
4554
4555         do_facet ost1 lctl set_param fail_loc=0x217
4556         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4557
4558         do_facet ost1 lctl set_param fail_loc=0
4559         rm -f $f
4560 }
4561 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4562
4563 test_71() {
4564     mkdir -p $DIR/$tdir
4565     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4566 }
4567 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4568
4569 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4570         check_kernel_version 43 || return 0
4571         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4572
4573         # Check that testing environment is properly set up. Skip if not
4574         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4575                 skip_env "User $RUNAS_ID does not exist - skipping"
4576                 return 0
4577         }
4578         # We had better clear the $DIR to get enough space for dd
4579         rm -rf $DIR/*
4580         touch $DIR/f72
4581         chmod 777 $DIR/f72
4582         chmod ug+s $DIR/f72
4583         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4584         # See if we are still setuid/sgid
4585         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4586         # Now test that MDS is updated too
4587         cancel_lru_locks mdc
4588         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4589         rm -f $DIR/f72
4590 }
4591 run_test 72a "Test that remove suid works properly (bug5695) ===="
4592
4593 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4594         local perm
4595
4596         [ "$RUNAS_ID" = "$UID" ] && \
4597                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4598         [ "$RUNAS_ID" -eq 0 ] && \
4599                 skip_env "RUNAS_ID = 0 -- skipping" && return
4600
4601         # Check that testing environment is properly set up. Skip if not
4602         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4603                 skip_env "User $RUNAS_ID does not exist - skipping"
4604                 return 0
4605         }
4606         touch $DIR/${tfile}-f{g,u}
4607         mkdir $DIR/${tfile}-d{g,u}
4608         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4609         chmod g+s $DIR/${tfile}-{f,d}g
4610         chmod u+s $DIR/${tfile}-{f,d}u
4611         for perm in 777 2777 4777; do
4612                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4613                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4614                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4615                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4616         done
4617         true
4618 }
4619 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4620
4621 # bug 3462 - multiple simultaneous MDC requests
4622 test_73() {
4623         mkdir $DIR/d73-1
4624         mkdir $DIR/d73-2
4625         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4626         pid1=$!
4627
4628         lctl set_param fail_loc=0x80000129
4629         $MULTIOP $DIR/d73-1/f73-2 Oc &
4630         sleep 1
4631         lctl set_param fail_loc=0
4632
4633         $MULTIOP $DIR/d73-2/f73-3 Oc &
4634         pid3=$!
4635
4636         kill -USR1 $pid1
4637         wait $pid1 || return 1
4638
4639         sleep 25
4640
4641         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4642         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4643         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4644
4645         rm -rf $DIR/d73-*
4646 }
4647 run_test 73 "multiple MDC requests (should not deadlock)"
4648
4649 test_74a() { # bug 6149, 6184
4650         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4651         #
4652         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4653         # will spin in a tight reconnection loop
4654         touch $DIR/f74a
4655         lctl set_param fail_loc=0x8000030e
4656         # get any lock that won't be difficult - lookup works.
4657         ls $DIR/f74a
4658         lctl set_param fail_loc=0
4659         true
4660         rm -f $DIR/f74a
4661 }
4662 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4663
4664 test_74b() { # bug 13310
4665         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4666         #
4667         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4668         # will spin in a tight reconnection loop
4669         lctl set_param fail_loc=0x8000030e
4670         # get a "difficult" lock
4671         touch $DIR/f74b
4672         lctl set_param fail_loc=0
4673         true
4674         rm -f $DIR/f74b
4675 }
4676 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4677
4678 test_74c() {
4679 #define OBD_FAIL_LDLM_NEW_LOCK
4680         lctl set_param fail_loc=0x80000319
4681         touch $DIR/$tfile && error "Touch successful"
4682         true
4683 }
4684 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4685
4686 num_inodes() {
4687         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4688 }
4689
4690 get_inode_slab_tunables() {
4691         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4692 }
4693
4694 set_inode_slab_tunables() {
4695         echo "lustre_inode_cache $1" > /proc/slabinfo
4696 }
4697
4698 test_76() { # Now for bug 20433, added originally in bug 1443
4699         local SLAB_SETTINGS=`get_inode_slab_tunables`
4700         local CPUS=`getconf _NPROCESSORS_ONLN`
4701         # we cannot set limit below 1 which means 1 inode in each
4702         # per-cpu cache is still allowed
4703         set_inode_slab_tunables "1 1 0"
4704         cancel_lru_locks osc
4705         BEFORE_INODES=`num_inodes`
4706         echo "before inodes: $BEFORE_INODES"
4707         local COUNT=1000
4708         [ "$SLOW" = "no" ] && COUNT=100
4709         for i in `seq $COUNT`; do
4710                 touch $DIR/$tfile
4711                 rm -f $DIR/$tfile
4712         done
4713         cancel_lru_locks osc
4714         AFTER_INODES=`num_inodes`
4715         echo "after inodes: $AFTER_INODES"
4716         local wait=0
4717         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
4718                 sleep 2
4719                 AFTER_INODES=`num_inodes`
4720                 wait=$((wait+2))
4721                 echo "wait $wait seconds inodes: $AFTER_INODES"
4722                 if [ $wait -gt 30 ]; then
4723                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
4724                 fi
4725         done
4726         set_inode_slab_tunables "$SLAB_SETTINGS"
4727 }
4728 run_test 76 "confirm clients recycle inodes properly ===="
4729
4730
4731 export ORIG_CSUM=""
4732 set_checksums()
4733 {
4734         # Note: in sptlrpc modes which enable its own bulk checksum, the
4735         # original crc32_le bulk checksum will be automatically disabled,
4736         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
4737         # will be checked by sptlrpc code against sptlrpc bulk checksum.
4738         # In this case set_checksums() will not be no-op, because sptlrpc
4739         # bulk checksum will be enabled all through the test.
4740
4741         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
4742         lctl set_param -n osc.*.checksums $1
4743         return 0
4744 }
4745
4746 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
4747                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
4748 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
4749 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
4750 set_checksum_type()
4751 {
4752         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
4753         log "set checksum type to $1"
4754         return 0
4755 }
4756 F77_TMP=$TMP/f77-temp
4757 F77SZ=8
4758 setup_f77() {
4759         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
4760                 error "error writing to $F77_TMP"
4761 }
4762
4763 test_77a() { # bug 10889
4764         $GSS && skip "could not run with gss" && return
4765         [ ! -f $F77_TMP ] && setup_f77
4766         set_checksums 1
4767         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
4768         set_checksums 0
4769         rm -f $DIR/$tfile
4770 }
4771 run_test 77a "normal checksum read/write operation ============="
4772
4773 test_77b() { # bug 10889
4774         $GSS && skip "could not run with gss" && return
4775         [ ! -f $F77_TMP ] && setup_f77
4776         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4777         lctl set_param fail_loc=0x80000409
4778         set_checksums 1
4779         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
4780                 error "dd error: $?"
4781         lctl set_param fail_loc=0
4782         set_checksums 0
4783 }
4784 run_test 77b "checksum error on client write ===================="
4785
4786 test_77c() { # bug 10889
4787         $GSS && skip "could not run with gss" && return
4788         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
4789         set_checksums 1
4790         for algo in $CKSUM_TYPES; do
4791                 cancel_lru_locks osc
4792                 set_checksum_type $algo
4793                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4794                 lctl set_param fail_loc=0x80000408
4795                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
4796                 lctl set_param fail_loc=0
4797         done
4798         set_checksums 0
4799         set_checksum_type $ORIG_CSUM_TYPE
4800         rm -f $DIR/f77b
4801 }
4802 run_test 77c "checksum error on client read ==================="
4803
4804 test_77d() { # bug 10889
4805         $GSS && skip "could not run with gss" && return
4806         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4807         lctl set_param fail_loc=0x80000409
4808         set_checksums 1
4809         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4810                 error "direct write: rc=$?"
4811         lctl set_param fail_loc=0
4812         set_checksums 0
4813 }
4814 run_test 77d "checksum error on OST direct write ==============="
4815
4816 test_77e() { # bug 10889
4817         $GSS && skip "could not run with gss" && return
4818         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
4819         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4820         lctl set_param fail_loc=0x80000408
4821         set_checksums 1
4822         cancel_lru_locks osc
4823         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4824                 error "direct read: rc=$?"
4825         lctl set_param fail_loc=0
4826         set_checksums 0
4827 }
4828 run_test 77e "checksum error on OST direct read ================"
4829
4830 test_77f() { # bug 10889
4831         $GSS && skip "could not run with gss" && return
4832         set_checksums 1
4833         for algo in $CKSUM_TYPES; do
4834                 cancel_lru_locks osc
4835                 set_checksum_type $algo
4836                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4837                 lctl set_param fail_loc=0x409
4838                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
4839                         error "direct write succeeded"
4840                 lctl set_param fail_loc=0
4841         done
4842         set_checksum_type $ORIG_CSUM_TYPE
4843         set_checksums 0
4844 }
4845 run_test 77f "repeat checksum error on write (expect error) ===="
4846
4847 test_77g() { # bug 10889
4848         $GSS && skip "could not run with gss" && return
4849         remote_ost_nodsh && skip "remote OST with nodsh" && return
4850
4851         [ ! -f $F77_TMP ] && setup_f77
4852
4853         $SETSTRIPE -c 1 -i 0 $DIR/f77g
4854         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
4855         do_facet ost1 lctl set_param fail_loc=0x8000021a
4856         set_checksums 1
4857         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
4858                 error "write error: rc=$?"
4859         do_facet ost1 lctl set_param fail_loc=0
4860         set_checksums 0
4861 }
4862 run_test 77g "checksum error on OST write ======================"
4863
4864 test_77h() { # bug 10889
4865         $GSS && skip "could not run with gss" && return
4866         remote_ost_nodsh && skip "remote OST with nodsh" && return
4867
4868         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
4869         cancel_lru_locks osc
4870         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
4871         do_facet ost1 lctl set_param fail_loc=0x8000021b
4872         set_checksums 1
4873         cmp $F77_TMP $DIR/f77g || error "file compare failed"
4874         do_facet ost1 lctl set_param fail_loc=0
4875         set_checksums 0
4876 }
4877 run_test 77h "checksum error on OST read ======================="
4878
4879 test_77i() { # bug 13805
4880         $GSS && skip "could not run with gss" && return
4881         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
4882         lctl set_param fail_loc=0x40b
4883         remount_client $MOUNT
4884         lctl set_param fail_loc=0
4885         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4886                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4887                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4888                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4889         done
4890         remount_client $MOUNT
4891 }
4892 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
4893
4894 test_77j() { # bug 13805
4895         $GSS && skip "could not run with gss" && return
4896         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
4897         lctl set_param fail_loc=0x40c
4898         remount_client $MOUNT
4899         lctl set_param fail_loc=0
4900         sleep 2 # wait async osc connect to finish
4901         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4902                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4903                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4904                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4905         done
4906         remount_client $MOUNT
4907 }
4908 run_test 77j "client only supporting ADLER32 ===================="
4909
4910 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
4911 rm -f $F77_TMP
4912 unset F77_TMP
4913
4914 test_78() { # bug 10901
4915         remote_ost || { skip_env "local OST" && return; }
4916
4917         NSEQ=5
4918         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
4919         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
4920         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
4921         echo "MemTotal: $MEMTOTAL"
4922 # reserve 256MB of memory for the kernel and other running processes,
4923 # and then take 1/2 of the remaining memory for the read/write buffers.
4924     if [ $MEMTOTAL -gt 512 ] ;then
4925         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
4926     else
4927         # for those poor memory-starved high-end clusters...
4928         MEMTOTAL=$((MEMTOTAL / 2))
4929     fi
4930         echo "Mem to use for directio: $MEMTOTAL"
4931         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
4932         [ $F78SIZE -gt 512 ] && F78SIZE=512
4933         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
4934         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
4935         echo "Smallest OST: $SMALLESTOST"
4936         [ $SMALLESTOST -lt 10240 ] && \
4937                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
4938
4939         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
4940                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
4941
4942         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
4943         echo "File size: $F78SIZE"
4944         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
4945         for i in `seq 1 $NSEQ`
4946         do
4947                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
4948                 echo directIO rdwr round $i of $NSEQ
4949                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
4950         done
4951
4952         rm -f $DIR/$tfile
4953 }
4954 run_test 78 "handle large O_DIRECT writes correctly ============"
4955
4956 test_79() { # bug 12743
4957         wait_delete_completed
4958
4959         BKTOTAL=$(calc_osc_kbytes kbytestotal)
4960         BKFREE=$(calc_osc_kbytes kbytesfree)
4961         BKAVAIL=$(calc_osc_kbytes kbytesavail)
4962
4963         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
4964         DFTOTAL=`echo $STRING | cut -d, -f1`
4965         DFUSED=`echo $STRING  | cut -d, -f2`
4966         DFAVAIL=`echo $STRING | cut -d, -f3`
4967         DFFREE=$(($DFTOTAL - $DFUSED))
4968
4969         ALLOWANCE=$((64 * $OSTCOUNT))
4970
4971         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
4972            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
4973                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
4974         fi
4975         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
4976            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
4977                 error "df free($DFFREE) mismatch OST free($BKFREE)"
4978         fi
4979         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
4980            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
4981                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
4982         fi
4983 }
4984 run_test 79 "df report consistency check ======================="
4985
4986 test_80() { # bug 10718
4987         # relax strong synchronous semantics for slow backends like ZFS
4988         local soc="obdfilter.*.sync_on_lock_cancel"
4989         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
4990         local hosts=
4991         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
4992                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
4993                           facet_active_host $host; done | sort -u)
4994                 do_nodes $hosts lctl set_param $soc=never
4995         fi
4996
4997         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
4998         sync; sleep 1; sync
4999         local BEFORE=`date +%s`
5000         cancel_lru_locks osc
5001         local AFTER=`date +%s`
5002         local DIFF=$((AFTER-BEFORE))
5003         if [ $DIFF -gt 1 ] ; then
5004                 error "elapsed for 1M@1T = $DIFF"
5005         fi
5006
5007         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5008
5009         rm -f $DIR/$tfile
5010 }
5011 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5012
5013 test_81a() { # LU-456
5014         remote_ost_nodsh && skip "remote OST with nodsh" && return
5015         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5016         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5017         do_facet ost1 lctl set_param fail_loc=0x80000228
5018
5019         # write should trigger a retry and success
5020         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5021         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5022         RC=$?
5023         if [ $RC -ne 0 ] ; then
5024                 error "write should success, but failed for $RC"
5025         fi
5026 }
5027 run_test 81a "OST should retry write when get -ENOSPC ==============="
5028
5029 test_81b() { # LU-456
5030         remote_ost_nodsh && skip "remote OST with nodsh" && return
5031         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5032         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5033         do_facet ost1 lctl set_param fail_loc=0x228
5034
5035         # write should retry several times and return -ENOSPC finally
5036         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5037         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5038         RC=$?
5039         ENOSPC=28
5040         if [ $RC -ne $ENOSPC ] ; then
5041                 error "dd should fail for -ENOSPC, but succeed."
5042         fi
5043 }
5044 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5045
5046 test_82() { # LU-1031
5047         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5048         local gid1=14091995
5049         local gid2=16022000
5050
5051         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5052         local MULTIPID1=$!
5053         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5054         local MULTIPID2=$!
5055         kill -USR1 $MULTIPID2
5056         sleep 2
5057         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5058                 error "First grouplock does not block second one"
5059         else
5060                 echo "Second grouplock blocks first one"
5061         fi
5062         kill -USR1 $MULTIPID1
5063         wait $MULTIPID1
5064         wait $MULTIPID2
5065 }
5066 run_test 82 "Basic grouplock test ==============================="
5067
5068 test_99a() {
5069         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
5070             return
5071         mkdir -p $DIR/d99cvsroot
5072         chown $RUNAS_ID $DIR/d99cvsroot
5073         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5074         cd $TMP
5075
5076         $RUNAS cvs -d $DIR/d99cvsroot init || error
5077         cd $oldPWD
5078 }
5079 run_test 99a "cvs init ========================================="
5080
5081 test_99b() {
5082         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5083         [ ! -d $DIR/d99cvsroot ] && test_99a
5084         cd /etc/init.d
5085         # some versions of cvs import exit(1) when asked to import links or
5086         # files they can't read.  ignore those files.
5087         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5088                         ! -perm +4 -printf '-I %f\n')
5089         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5090                 d99reposname vtag rtag
5091 }
5092 run_test 99b "cvs import ======================================="
5093
5094 test_99c() {
5095         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5096         [ ! -d $DIR/d99cvsroot ] && test_99b
5097         cd $DIR
5098         mkdir -p $DIR/d99reposname
5099         chown $RUNAS_ID $DIR/d99reposname
5100         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5101 }
5102 run_test 99c "cvs checkout ====================================="
5103
5104 test_99d() {
5105         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5106         [ ! -d $DIR/d99cvsroot ] && test_99c
5107         cd $DIR/d99reposname
5108         $RUNAS touch foo99
5109         $RUNAS cvs add -m 'addmsg' foo99
5110 }
5111 run_test 99d "cvs add =========================================="
5112
5113 test_99e() {
5114         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5115         [ ! -d $DIR/d99cvsroot ] && test_99c
5116         cd $DIR/d99reposname
5117         $RUNAS cvs update
5118 }
5119 run_test 99e "cvs update ======================================="
5120
5121 test_99f() {
5122         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5123         [ ! -d $DIR/d99cvsroot ] && test_99d
5124         cd $DIR/d99reposname
5125         $RUNAS cvs commit -m 'nomsg' foo99
5126     rm -fr $DIR/d99cvsroot
5127 }
5128 run_test 99f "cvs commit ======================================="
5129
5130 test_100() {
5131         [ "$NETTYPE" = tcp ] || \
5132                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5133                         return ; }
5134
5135         remote_ost_nodsh && skip "remote OST with nodsh" && return
5136         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5137         remote_servers || \
5138                 { skip "useless for local single node setup" && return; }
5139
5140         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5141                 [ "$PROT" != "tcp" ] && continue
5142                 RPORT=$(echo $REMOTE | cut -d: -f2)
5143                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5144
5145                 rc=0
5146                 LPORT=`echo $LOCAL | cut -d: -f2`
5147                 if [ $LPORT -ge 1024 ]; then
5148                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5149                         netstat -tna
5150                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5151                 fi
5152         done
5153         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5154 }
5155 run_test 100 "check local port using privileged port ==========="
5156
5157 function get_named_value()
5158 {
5159     local tag
5160
5161     tag=$1
5162     while read ;do
5163         line=$REPLY
5164         case $line in
5165         $tag*)
5166             echo $line | sed "s/^$tag[ ]*//"
5167             break
5168             ;;
5169         esac
5170     done
5171 }
5172
5173 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
5174 cleanup_101a() {
5175         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5176         trap 0
5177 }
5178
5179 test_101a() {
5180         local s
5181         local discard
5182         local nreads=10000
5183         [ "$CPU" = "UML" ] && nreads=1000
5184         local cache_limit=32
5185
5186         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5187         trap cleanup_101a EXIT
5188         $LCTL set_param -n llite.*.read_ahead_stats 0
5189         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5190
5191         #
5192         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5193         #
5194         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5195         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5196
5197         discard=0
5198         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5199                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5200                         discard=$(($discard + $s))
5201         done
5202         cleanup_101a
5203
5204         if [ $(($discard * 10)) -gt $nreads ] ;then
5205                 $LCTL get_param osc.*-osc*.rpc_stats
5206                 $LCTL get_param llite.*.read_ahead_stats
5207                 error "too many ($discard) discarded pages"
5208         fi
5209         rm -f $DIR/$tfile || true
5210 }
5211 run_test 101a "check read-ahead for random reads ================"
5212
5213 setup_test101bc() {
5214         mkdir -p $DIR/$tdir
5215         STRIPE_SIZE=1048576
5216         STRIPE_COUNT=$OSTCOUNT
5217         STRIPE_OFFSET=0
5218
5219         local list=$(comma_list $(osts_nodes))
5220         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=0
5221         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=0
5222
5223         trap cleanup_test101bc EXIT
5224         # prepare the read-ahead file
5225         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5226
5227         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5228 }
5229
5230 cleanup_test101bc() {
5231         trap 0
5232         rm -rf $DIR/$tdir
5233         rm -f $DIR/$tfile
5234
5235         local list=$(comma_list $(osts_nodes))
5236         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
5237         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=1
5238 }
5239
5240 calc_total() {
5241         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5242 }
5243
5244 ra_check_101() {
5245         local READ_SIZE=$1
5246         local STRIPE_SIZE=1048576
5247         local RA_INC=1048576
5248         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5249         local FILE_LENGTH=$((64*100))
5250         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5251                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5252         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5253                         get_named_value 'read but discarded' | \
5254                         cut -d" " -f1 | calc_total`
5255         if [ $DISCARD -gt $discard_limit ]; then
5256                 $LCTL get_param llite.*.read_ahead_stats
5257                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5258         else
5259                 echo "Read-ahead success for size ${READ_SIZE}"
5260         fi
5261 }
5262
5263 test_101b() {
5264         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5265         local STRIPE_SIZE=1048576
5266         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5267         local FILE_LENGTH=$((STRIPE_SIZE*100))
5268         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5269         # prepare the read-ahead file
5270         setup_test101bc
5271         cancel_lru_locks osc
5272         for BIDX in 2 4 8 16 32 64 128 256
5273         do
5274                 local BSIZE=$((BIDX*4096))
5275                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5276                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5277                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5278                 $LCTL set_param -n llite.*.read_ahead_stats 0
5279                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5280                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5281                 cancel_lru_locks osc
5282                 ra_check_101 $BSIZE
5283         done
5284         cleanup_test101bc
5285         true
5286 }
5287 run_test 101b "check stride-io mode read-ahead ================="
5288
5289 test_101c() {
5290     local STRIPE_SIZE=1048576
5291     local FILE_LENGTH=$((STRIPE_SIZE*100))
5292     local nreads=10000
5293     local osc
5294
5295     setup_test101bc
5296
5297     cancel_lru_locks osc
5298     $LCTL set_param osc.*.rpc_stats 0
5299     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5300     for osc in $($LCTL get_param -N osc.*); do
5301         if [ "$osc" == "osc.num_refs" ]; then
5302             continue
5303         fi
5304
5305         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5306         if [ $lines -le 20 ]; then
5307             continue
5308         fi
5309
5310         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5311                                      awk '$1 == "1:" { print $2; exit; }')
5312         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5313                                      awk '$1 == "2:" { print $2; exit; }')
5314         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5315                                      awk '$1 == "4:" { print $2; exit; }')
5316         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5317                                      awk '$1 == "8:" { print $2; exit; }')
5318
5319         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5320         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5321         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5322         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5323         echo "${osc} rpc check passed!"
5324     done
5325     cleanup_test101bc
5326     true
5327 }
5328 run_test 101c "check stripe_size aligned read-ahead ================="
5329
5330 set_read_ahead() {
5331    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5332    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5333 }
5334
5335 test_101d() {
5336     local file=$DIR/$tfile
5337     local size=${FILESIZE_101c:-500}
5338     local ra_MB=${READAHEAD_MB:-40}
5339
5340     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5341     [ $space -gt $((size / 1024)) ] ||
5342         { skip "Need free space ${size}M, have $space" && return; }
5343
5344     echo Creating ${size}M test file $file
5345     dd if=/dev/zero of=$file bs=1M count=$size || error "dd failed"
5346     echo Cancel LRU locks on lustre client to flush the client cache
5347     cancel_lru_locks osc
5348
5349     echo Disable read-ahead
5350     local old_READAHEAD=$(set_read_ahead 0)
5351
5352     echo Reading the test file $file with read-ahead disabled
5353     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5354
5355     echo Cancel LRU locks on lustre client to flush the client cache
5356     cancel_lru_locks osc
5357     echo Enable read-ahead with ${ra_MB}MB
5358     set_read_ahead $ra_MB
5359
5360     echo Reading the test file $file with read-ahead enabled
5361     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5362
5363     echo read-ahead disabled time read $time_ra_OFF
5364     echo read-ahead enabled  time read $time_ra_ON
5365
5366         set_read_ahead $old_READAHEAD
5367         rm -f $file
5368         wait_delete_completed
5369
5370     [ $time_ra_ON -lt $time_ra_OFF ] ||
5371         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5372                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5373 }
5374 run_test 101d "file read with and without read-ahead enabled  ================="
5375
5376 test_101e() {
5377     local file=$DIR/$tfile
5378     local size=500  #KB
5379     local count=100
5380     local blksize=1024
5381
5382     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5383     local need_space=$((count * size))
5384     [ $space -gt $need_space ] ||
5385         { skip_env "Need free space $need_space, have $space" && return; }
5386
5387     echo Creating $count ${size}K test files
5388     for ((i = 0; i < $count; i++)); do
5389         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5390     done
5391
5392     echo Cancel LRU locks on lustre client to flush the client cache
5393     cancel_lru_locks osc
5394
5395     echo Reset readahead stats
5396     $LCTL set_param -n llite.*.read_ahead_stats 0
5397
5398     for ((i = 0; i < $count; i++)); do
5399         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5400     done
5401
5402     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5403           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5404
5405     for ((i = 0; i < $count; i++)); do
5406         rm -rf ${file}_${i} 2>/dev/null
5407     done
5408
5409     #10000 means 20% reads are missing in readahead
5410     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5411 }
5412 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5413
5414 cleanup_test101f() {
5415     trap 0
5416     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5417     rm -rf $DIR/$tfile 2>/dev/null
5418 }
5419
5420 test_101f() {
5421     local file=$DIR/$tfile
5422     local nreads=1000
5423
5424     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5425     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5426     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5427     trap cleanup_test101f EXIT
5428
5429     echo Cancel LRU locks on lustre client to flush the client cache
5430     cancel_lru_locks osc
5431
5432     echo Reset readahead stats
5433     $LCTL set_param -n llite.*.read_ahead_stats 0
5434     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5435     # readahead should read in 2M file on second read, so only miss
5436     # 2 pages.
5437     echo Random 4K reads on 2M file for 1000 times
5438     $READS -f $file -s 2097152 -b 4096 -n $nreads
5439
5440     echo checking missing pages
5441     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5442           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5443
5444     [ $miss -lt 3 ] || error "misses too much pages!"
5445     cleanup_test101f
5446 }
5447 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5448
5449 setup_test102() {
5450         mkdir -p $DIR/$tdir
5451         chown $RUNAS_ID $DIR/$tdir
5452         STRIPE_SIZE=65536
5453         STRIPE_OFFSET=1
5454         STRIPE_COUNT=$OSTCOUNT
5455         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5456
5457         trap cleanup_test102 EXIT
5458         cd $DIR
5459         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5460         cd $DIR/$tdir
5461         for num in 1 2 3 4; do
5462                 for count in $(seq 1 $STRIPE_COUNT); do
5463                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5464                                 local size=`expr $STRIPE_SIZE \* $num`
5465                                 local file=file"$num-$idx-$count"
5466                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5467                         done
5468                 done
5469         done
5470
5471         cd $DIR
5472         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5473 }
5474
5475 cleanup_test102() {
5476         trap 0
5477         rm -f $TMP/f102.tar
5478         rm -rf $DIR/d0.sanity/d102
5479 }
5480
5481 test_102a() {
5482         local testfile=$DIR/xattr_testfile
5483
5484         touch $testfile
5485
5486         [ "$UID" != 0 ] && skip_env "must run as root" && return
5487         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5488                 skip_env "must have user_xattr" && return
5489
5490         [ -z "$(which setfattr 2>/dev/null)" ] &&
5491                 skip_env "could not find setfattr" && return
5492
5493         echo "set/get xattr..."
5494         setfattr -n trusted.name1 -v value1 $testfile || error
5495         getfattr -n trusted.name1 $testfile 2> /dev/null |
5496           grep "trusted.name1=.value1" ||
5497                 error "$testfile missing trusted.name1=value1"
5498
5499         setfattr -n user.author1 -v author1 $testfile || error
5500         getfattr -n user.author1 $testfile 2> /dev/null |
5501           grep "user.author1=.author1" ||
5502                 error "$testfile missing trusted.author1=author1"
5503
5504         echo "listxattr..."
5505         setfattr -n trusted.name2 -v value2 $testfile ||
5506                 error "$testfile unable to set trusted.name2"
5507         setfattr -n trusted.name3 -v value3 $testfile ||
5508                 error "$testfile unable to set trusted.name3"
5509         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5510             grep "trusted.name" | wc -l) -eq 3 ] ||
5511                 error "$testfile missing 3 trusted.name xattrs"
5512
5513         setfattr -n user.author2 -v author2 $testfile ||
5514                 error "$testfile unable to set user.author2"
5515         setfattr -n user.author3 -v author3 $testfile ||
5516                 error "$testfile unable to set user.author3"
5517         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5518             grep "user.author" | wc -l) -eq 3 ] ||
5519                 error "$testfile missing 3 user.author xattrs"
5520
5521         echo "remove xattr..."
5522         setfattr -x trusted.name1 $testfile ||
5523                 error "$testfile error deleting trusted.name1"
5524         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5525                 error "$testfile did not delete trusted.name1 xattr"
5526
5527         setfattr -x user.author1 $testfile ||
5528                 error "$testfile error deleting user.author1"
5529         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5530                 error "$testfile did not delete trusted.name1 xattr"
5531
5532         # b10667: setting lustre special xattr be silently discarded
5533         echo "set lustre special xattr ..."
5534         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5535                 error "$testfile allowed setting trusted.lov"
5536 }
5537 run_test 102a "user xattr test =================================="
5538
5539 test_102b() {
5540         # b10930: get/set/list trusted.lov xattr
5541         echo "get/set/list trusted.lov xattr ..."
5542         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5543         local testfile=$DIR/$tfile
5544         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5545                 error "setstripe failed"
5546         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5547                 error "getstripe failed"
5548         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5549         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5550
5551         local testfile2=${testfile}2
5552         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5553                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5554
5555         $MCREATE $testfile2
5556         setfattr -n trusted.lov -v $value $testfile2
5557         local stripe_size=$($GETSTRIPE -S $testfile2)
5558         local stripe_count=$($GETSTRIPE -c $testfile2)
5559         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5560         [ $stripe_count -eq $STRIPECOUNT ] ||
5561                 error "stripe count $stripe_count != $STRIPECOUNT"
5562         rm -f $DIR/$tfile
5563 }
5564 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5565
5566 test_102c() {
5567         # b10930: get/set/list lustre.lov xattr
5568         echo "get/set/list lustre.lov xattr ..."
5569         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5570         mkdir -p $DIR/$tdir
5571         chown $RUNAS_ID $DIR/$tdir
5572         local testfile=$DIR/$tdir/$tfile
5573         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5574                 error "setstripe failed"
5575         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5576                 error "getstripe failed"
5577         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5578         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5579
5580         local testfile2=${testfile}2
5581         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5582                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5583
5584         $RUNAS $MCREATE $testfile2
5585         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5586         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5587         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5588         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5589         [ $stripe_count -eq $STRIPECOUNT ] ||
5590                 error "stripe count $stripe_count != $STRIPECOUNT"
5591 }
5592 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5593
5594 compare_stripe_info1() {
5595         local stripe_index_all_zero=true
5596
5597         for num in 1 2 3 4; do
5598                 for count in $(seq 1 $STRIPE_COUNT); do
5599                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5600                                 local size=$((STRIPE_SIZE * num))
5601                                 local file=file"$num-$offset-$count"
5602                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5603                                 [ $stripe_size -ne $size ] &&
5604                                     error "$file: size $stripe_size != $size"
5605                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5606                                 # allow fewer stripes to be created, ORI-601
5607                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5608                                     error "$file: count $stripe_count != $count"
5609                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5610                                 [ $stripe_index -ne 0 ] &&
5611                                         stripe_index_all_zero=false
5612                         done
5613                 done
5614         done
5615         $stripe_index_all_zero &&
5616                 error "all files are being extracted starting from OST index 0"
5617         return 0
5618 }
5619
5620 find_lustre_tar() {
5621         [ -n "$(which tar 2>/dev/null)" ] &&
5622                 strings $(which tar) | grep -q "lustre" && echo tar
5623 }
5624
5625 test_102d() {
5626         # b10930: tar test for trusted.lov xattr
5627         TAR=$(find_lustre_tar)
5628         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5629         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5630         setup_test102
5631         mkdir -p $DIR/d102d
5632         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5633         cd $DIR/d102d/$tdir
5634         compare_stripe_info1
5635 }
5636 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5637
5638 test_102f() {
5639         # b10930: tar test for trusted.lov xattr
5640         TAR=$(find_lustre_tar)
5641         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5642         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5643         setup_test102
5644         mkdir -p $DIR/d102f
5645         cd $DIR
5646         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5647         cd $DIR/d102f/$tdir
5648         compare_stripe_info1
5649 }
5650 run_test 102f "tar copy files, not keep osts ==========="
5651
5652 grow_xattr() {
5653         local xsize=${1:-1024}  # in bytes
5654         local file=$DIR/$tfile
5655
5656         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5657                 skip "must have user_xattr" && return 0
5658         [ -z "$(which setfattr 2>/dev/null)" ] &&
5659                 skip_env "could not find setfattr" && return 0
5660         [ -z "$(which getfattr 2>/dev/null)" ] &&
5661                 skip_env "could not find getfattr" && return 0
5662
5663         touch $file
5664
5665         local value="$(generate_string $xsize)"
5666
5667         local xbig=trusted.big
5668         log "save $xbig on $file"
5669         setfattr -n $xbig -v $value $file ||
5670                 error "saving $xbig on $file failed"
5671
5672         local orig=$(get_xattr_value $xbig $file)
5673         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
5674
5675         local xsml=trusted.sml
5676         log "save $xsml on $file"
5677         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
5678
5679         local new=$(get_xattr_value $xbig $file)
5680         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
5681
5682         log "grow $xsml on $file"
5683         setfattr -n $xsml -v "$value" $file ||
5684                 error "growing $xsml on $file failed"
5685
5686         new=$(get_xattr_value $xbig $file)
5687         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
5688         log "$xbig still valid after growing $xsml"
5689
5690         rm -f $file
5691 }
5692
5693 test_102h() { # bug 15777
5694         grow_xattr 1024
5695 }
5696 run_test 102h "grow xattr from inside inode to external block"
5697
5698 test_102ha() {
5699         large_xattr_enabled || { skip "large_xattr disabled" && return; }
5700         grow_xattr $(max_xattr_size)
5701 }
5702 run_test 102ha "grow xattr from inside inode to external inode"
5703
5704 test_102i() { # bug 17038
5705         touch $DIR/$tfile
5706         ln -s $DIR/$tfile $DIR/${tfile}link
5707         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
5708         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"
5709         rm -f $DIR/$tfile $DIR/${tfile}link
5710 }
5711 run_test 102i "lgetxattr test on symbolic link ============"
5712
5713 test_102j() {
5714         TAR=$(find_lustre_tar)
5715         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5716         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5717         setup_test102 "$RUNAS"
5718         mkdir -p $DIR/d102j
5719         chown $RUNAS_ID $DIR/d102j
5720         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
5721         cd $DIR/d102j/$tdir
5722         compare_stripe_info1 "$RUNAS"
5723 }
5724 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
5725
5726 test_102k() {
5727         touch $DIR/$tfile
5728         # b22187 just check that does not crash for regular file.
5729         setfattr -n trusted.lov $DIR/$tfile
5730         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
5731         local test_kdir=$DIR/d102k
5732         mkdir $test_kdir
5733         local default_size=`$GETSTRIPE -S $test_kdir`
5734         local default_count=`$GETSTRIPE -c $test_kdir`
5735         local default_offset=`$GETSTRIPE -i $test_kdir`
5736         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
5737                 error 'dir setstripe failed'
5738         setfattr -n trusted.lov $test_kdir
5739         local stripe_size=`$GETSTRIPE -S $test_kdir`
5740         local stripe_count=`$GETSTRIPE -c $test_kdir`
5741         local stripe_offset=`$GETSTRIPE -i $test_kdir`
5742         [ $stripe_size -eq $default_size ] ||
5743                 error "stripe size $stripe_size != $default_size"
5744         [ $stripe_count -eq $default_count ] ||
5745                 error "stripe count $stripe_count != $default_count"
5746         [ $stripe_offset -eq $default_offset ] ||
5747                 error "stripe offset $stripe_offset != $default_offset"
5748         rm -rf $DIR/$tfile $test_kdir
5749 }
5750 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
5751
5752 test_102l() {
5753         # LU-532 trusted. xattr is invisible to non-root
5754         local testfile=$DIR/$tfile
5755
5756         touch $testfile
5757
5758         echo "listxattr as user..."
5759         chown $RUNAS_ID $testfile
5760         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
5761             grep -q "trusted" &&
5762                 error "$testfile trusted xattrs are user visible"
5763
5764         return 0;
5765 }
5766 run_test 102l "listxattr filter test =================================="
5767
5768 cleanup_test102
5769
5770 run_acl_subtest()
5771 {
5772     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
5773     return $?
5774 }
5775
5776 test_103 () {
5777     [ "$UID" != 0 ] && skip_env "must run as root" && return
5778     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5779     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
5780     $GSS && skip "could not run under gss" && return
5781
5782     declare -a identity_old
5783
5784     for num in `seq $MDSCOUNT`; do
5785         switch_identity $num true || identity_old[$num]=$?
5786     done
5787
5788     SAVE_UMASK=`umask`
5789     umask 0022
5790     cd $DIR
5791
5792     echo "performing cp ..."
5793     run_acl_subtest cp || error
5794     echo "performing getfacl-noacl..."
5795     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
5796     echo "performing misc..."
5797     run_acl_subtest misc || error  "misc test failed"
5798     echo "performing permissions..."
5799     run_acl_subtest permissions || error "permissions failed"
5800     echo "performing setfacl..."
5801     run_acl_subtest setfacl || error  "setfacl test failed"
5802
5803     # inheritance test got from HP
5804     echo "performing inheritance..."
5805     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
5806     chmod +x make-tree || error "chmod +x failed"
5807     run_acl_subtest inheritance || error "inheritance test failed"
5808     rm -f make-tree
5809
5810     echo "LU-974 ignore umask when acl is enabled..."
5811     mkdir $DIR/974
5812     cd $DIR/974
5813     run_acl_subtest 974 || error "LU-974 test failed"
5814     rm -rf $DIR/974
5815
5816     cd $SAVE_PWD
5817     umask $SAVE_UMASK
5818
5819     for num in `seq $MDSCOUNT`; do
5820         if [ "${identity_old[$num]}" = 1 ]; then
5821             switch_identity $num false || identity_old[$num]=$?
5822         fi
5823     done
5824 }
5825 run_test 103 "acl test ========================================="
5826
5827 test_104a() {
5828         touch $DIR/$tfile
5829         lfs df || error "lfs df failed"
5830         lfs df -ih || error "lfs df -ih failed"
5831         lfs df -h $DIR || error "lfs df -h $DIR failed"
5832         lfs df -i $DIR || error "lfs df -i $DIR failed"
5833         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
5834         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
5835
5836         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
5837         lctl --device %$OSC deactivate
5838         lfs df || error "lfs df with deactivated OSC failed"
5839         lctl --device %$OSC activate
5840         # wait the osc back to normal
5841         wait_osc_import_state client ost FULL
5842
5843         lfs df || error "lfs df with reactivated OSC failed"
5844         rm -f $DIR/$tfile
5845 }
5846 run_test 104a "lfs df [-ih] [path] test ========================="
5847
5848 test_104b() {
5849         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5850         chmod 666 /dev/obd
5851         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
5852         if [ $denied_cnt -ne 0 ];
5853         then
5854                     error "lfs check servers test failed"
5855         fi
5856 }
5857 run_test 104b "$RUNAS lfs check servers test ===================="
5858
5859 test_105a() {
5860         # doesn't work on 2.4 kernels
5861         touch $DIR/$tfile
5862         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5863         then
5864                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
5865         else
5866                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
5867         fi
5868         rm -f $DIR/$tfile
5869 }
5870 run_test 105a "flock when mounted without -o flock test ========"
5871
5872 test_105b() {
5873         touch $DIR/$tfile
5874         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5875         then
5876                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
5877         else
5878                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
5879         fi
5880         rm -f $DIR/$tfile
5881 }
5882 run_test 105b "fcntl when mounted without -o flock test ========"
5883
5884 test_105c() {
5885         touch $DIR/$tfile
5886         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5887         then
5888                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
5889         else
5890                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
5891         fi
5892         rm -f $DIR/$tfile
5893 }
5894 run_test 105c "lockf when mounted without -o flock test ========"
5895
5896 test_105d() { # bug 15924
5897         mkdir -p $DIR/$tdir
5898         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5899                 skip "mount w/o flock enabled" && return
5900         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
5901         $LCTL set_param fail_loc=0x80000315
5902         flocks_test 2 $DIR/$tdir
5903 }
5904 run_test 105d "flock race (should not freeze) ========"
5905
5906 test_105e() { # bug 22660 && 22040
5907         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5908                 skip "mount w/o flock enabled" && return
5909         touch $DIR/$tfile
5910         flocks_test 3 $DIR/$tfile
5911 }
5912 run_test 105e "Two conflicting flocks from same process ======="
5913
5914 test_106() { #bug 10921
5915         mkdir -p $DIR/$tdir
5916         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
5917         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
5918 }
5919 run_test 106 "attempt exec of dir followed by chown of that dir"
5920
5921 test_107() {
5922         CDIR=`pwd`
5923         cd $DIR
5924
5925         local file=core
5926         rm -f $file
5927
5928         local save_pattern=$(sysctl -n kernel.core_pattern)
5929         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
5930         sysctl -w kernel.core_pattern=$file
5931         sysctl -w kernel.core_uses_pid=0
5932
5933         ulimit -c unlimited
5934         sleep 60 &
5935         SLEEPPID=$!
5936
5937         sleep 1
5938
5939         kill -s 11 $SLEEPPID
5940         wait $SLEEPPID
5941         if [ -e $file ]; then
5942                 size=`stat -c%s $file`
5943                 [ $size -eq 0 ] && error "Fail to create core file $file"
5944         else
5945                 error "Fail to create core file $file"
5946         fi
5947         rm -f $file
5948         sysctl -w kernel.core_pattern=$save_pattern
5949         sysctl -w kernel.core_uses_pid=$save_uses_pid
5950         cd $CDIR
5951 }
5952 run_test 107 "Coredump on SIG"
5953
5954 test_110() {
5955         mkdir -p $DIR/$tdir
5956         mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
5957                 error "mkdir with 255 char failed"
5958         mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
5959                 error "mkdir with 256 char should fail, but did not"
5960         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
5961                 error "create with 255 char failed"
5962         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
5963                 error "create with 256 char should fail, but did not"
5964
5965         ls -l $DIR/$tdir
5966         rm -rf $DIR/$tdir
5967 }
5968 run_test 110 "filename length checking"
5969
5970 test_115() {
5971         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5972             cut -c11-20)
5973         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
5974             return
5975         echo "Starting with $OSTIO_pre threads"
5976
5977         NUMTEST=20000
5978         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
5979         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
5980         echo "$NUMTEST creates/unlinks"
5981         mkdir -p $DIR/$tdir
5982         createmany -o $DIR/$tdir/$tfile $NUMTEST
5983         unlinkmany $DIR/$tdir/$tfile $NUMTEST
5984
5985         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5986             cut -c11-20)
5987
5988         # don't return an error
5989         [ $OSTIO_post == $OSTIO_pre ] && echo \
5990             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
5991             echo "This may be fine, depending on what ran before this test" &&
5992             echo "and how fast this system is." && return
5993
5994         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
5995 }
5996 run_test 115 "verify dynamic thread creation===================="
5997
5998 free_min_max () {
5999         wait_delete_completed
6000         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6001         echo OST kbytes available: ${AVAIL[@]}
6002         MAXI=0; MAXV=${AVAIL[0]}
6003         MINI=0; MINV=${AVAIL[0]}
6004         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6005             #echo OST $i: ${AVAIL[i]}kb
6006             if [ ${AVAIL[i]} -gt $MAXV ]; then
6007                 MAXV=${AVAIL[i]}; MAXI=$i
6008             fi
6009             if [ ${AVAIL[i]} -lt $MINV ]; then
6010                 MINV=${AVAIL[i]}; MINI=$i
6011             fi
6012         done
6013         echo Min free space: OST $MINI: $MINV
6014         echo Max free space: OST $MAXI: $MAXV
6015 }
6016
6017 test_116a() { # was previously test_116()
6018         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
6019
6020         echo -n "Free space priority "
6021         lctl get_param -n lov.*-clilov-*.qos_prio_free
6022         declare -a AVAIL
6023         free_min_max
6024         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
6025                 return
6026
6027         # generate uneven OSTs
6028         mkdir -p $DIR/$tdir/OST${MINI}
6029         declare -i FILL
6030         FILL=$(($MINV / 4))
6031         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
6032         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
6033         i=0
6034         while [ $FILL -gt 0 ]; do
6035             i=$(($i + 1))
6036             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
6037             FILL=$(($FILL - 2048))
6038             echo -n .
6039         done
6040         FILL=$(($MINV / 4))
6041         sync
6042         sleep_maxage
6043
6044         free_min_max
6045         DIFF=$(($MAXV - $MINV))
6046         DIFF2=$(($DIFF * 100 / $MINV))
6047         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
6048         if [ $DIFF2 -gt 20 ]; then
6049             echo "ok"
6050         else
6051             echo "failed - QOS mode won't be used"
6052             error_ignore "QOS imbalance criteria not met"
6053             return
6054         fi
6055
6056         MINI1=$MINI; MINV1=$MINV
6057         MAXI1=$MAXI; MAXV1=$MAXV
6058
6059         # now fill using QOS
6060         echo writing a bunch of files to QOS-assigned OSTs
6061         $SETSTRIPE -c 1 $DIR/$tdir
6062         i=0
6063         while [ $FILL -gt 0 ]; do
6064             i=$(($i + 1))
6065             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
6066             FILL=$(($FILL - 200))
6067             echo -n .
6068         done
6069         echo "wrote $i 200k files"
6070         sync
6071         sleep_maxage
6072
6073         echo "Note: free space may not be updated, so measurements might be off"
6074         free_min_max
6075         DIFF2=$(($MAXV - $MINV))
6076         echo "free space delta: orig $DIFF final $DIFF2"
6077         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
6078         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
6079         echo "Wrote $DIFF to smaller OST $MINI1"
6080         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
6081         echo "Wrote $DIFF2 to larger OST $MAXI1"
6082         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
6083
6084         # Figure out which files were written where
6085         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6086                awk '/'$MINI1': / {print $2; exit}')
6087         echo $UUID
6088         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6089         echo "$MINC files created on smaller OST $MINI1"
6090         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
6091                awk '/'$MAXI1': / {print $2; exit}')
6092         echo $UUID
6093         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
6094         echo "$MAXC files created on larger OST $MAXI1"
6095         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
6096         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
6097
6098         rm -rf $DIR/$tdir
6099 }
6100 run_test 116a "stripe QOS: free space balance ==================="
6101
6102 test_116b() { # LU-2093
6103 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
6104         local old_rr
6105         old_rr=$(do_facet $SINGLEMDS lctl get_param -n lov.*mdtlov*.qos_threshold_rr)
6106         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr 0
6107         mkdir -p $DIR/$tdir
6108         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
6109         createmany -o $DIR/$tdir/f- 20 || error "can't create"
6110         do_facet $SINGLEMDS lctl set_param fail_loc=0
6111         rm -rf $DIR/$tdir
6112         do_facet $SINGLEMDS lctl set_param lov.*mdtlov*.qos_threshold_rr $old_rr
6113 }
6114 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
6115
6116 test_117() # bug 10891
6117 {
6118         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6119         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6120         lctl set_param fail_loc=0x21e
6121         > $DIR/$tfile || error "truncate failed"
6122         lctl set_param fail_loc=0
6123         echo "Truncate succeeded."
6124         rm -f $DIR/$tfile
6125 }
6126 run_test 117 "verify fsfilt_extend =========="
6127
6128 export OLD_RESENDCOUNT=""
6129 set_resend_count () {
6130         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6131         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6132         lctl set_param -n $PROC_RESENDCOUNT $1
6133         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6134 }
6135
6136 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6137
6138 # Reset async IO behavior after error case
6139 reset_async() {
6140         FILE=$DIR/reset_async
6141
6142         # Ensure all OSCs are cleared
6143         $SETSTRIPE -c -1 $FILE
6144         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6145         sync
6146         rm $FILE
6147 }
6148
6149 test_118a() #bug 11710
6150 {
6151         reset_async
6152
6153         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6154         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6155         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6156
6157         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6158                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6159                 return 1;
6160         fi
6161         rm -f $DIR/$tfile
6162 }
6163 run_test 118a "verify O_SYNC works =========="
6164
6165 test_118b()
6166 {
6167         remote_ost_nodsh && skip "remote OST with nodsh" && return
6168
6169         reset_async
6170
6171         #define OBD_FAIL_OST_ENOENT 0x217
6172         set_nodes_failloc "$(osts_nodes)" 0x217
6173         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6174         RC=$?
6175         set_nodes_failloc "$(osts_nodes)" 0
6176         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6177         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6178                     grep -c writeback)
6179
6180         if [[ $RC -eq 0 ]]; then
6181                 error "Must return error due to dropped pages, rc=$RC"
6182                 return 1;
6183         fi
6184
6185         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6186                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6187                 return 1;
6188         fi
6189
6190         echo "Dirty pages not leaked on ENOENT"
6191
6192         # Due to the above error the OSC will issue all RPCs syncronously
6193         # until a subsequent RPC completes successfully without error.
6194         $MULTIOP $DIR/$tfile Ow4096yc
6195         rm -f $DIR/$tfile
6196
6197         return 0
6198 }
6199 run_test 118b "Reclaim dirty pages on fatal error =========="
6200
6201 test_118c()
6202 {
6203         remote_ost_nodsh && skip "remote OST with nodsh" && return
6204
6205         reset_async
6206
6207         #define OBD_FAIL_OST_EROFS               0x216
6208         set_nodes_failloc "$(osts_nodes)" 0x216
6209
6210         # multiop should block due to fsync until pages are written
6211         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6212         MULTIPID=$!
6213         sleep 1
6214
6215         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6216                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6217         fi
6218
6219         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6220                     grep -c writeback)
6221         if [[ $WRITEBACK -eq 0 ]]; then
6222                 error "No page in writeback, writeback=$WRITEBACK"
6223         fi
6224
6225         set_nodes_failloc "$(osts_nodes)" 0
6226         wait $MULTIPID
6227         RC=$?
6228         if [[ $RC -ne 0 ]]; then
6229                 error "Multiop fsync failed, rc=$RC"
6230         fi
6231
6232         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6233         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6234                     grep -c writeback)
6235         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6236                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6237         fi
6238
6239         rm -f $DIR/$tfile
6240         echo "Dirty pages flushed via fsync on EROFS"
6241         return 0
6242 }
6243 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6244
6245 test_118d()
6246 {
6247         remote_ost_nodsh && skip "remote OST with nodsh" && return
6248
6249         reset_async
6250
6251         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6252         set_nodes_failloc "$(osts_nodes)" 0x214
6253         # multiop should block due to fsync until pages are written
6254         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6255         MULTIPID=$!
6256         sleep 1
6257
6258         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6259                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6260         fi
6261
6262         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6263                     grep -c writeback)
6264         if [[ $WRITEBACK -eq 0 ]]; then
6265                 error "No page in writeback, writeback=$WRITEBACK"
6266         fi
6267
6268         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6269         set_nodes_failloc "$(osts_nodes)" 0
6270
6271         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6272         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6273                     grep -c writeback)
6274         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6275                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6276         fi
6277
6278         rm -f $DIR/$tfile
6279         echo "Dirty pages gaurenteed flushed via fsync"
6280         return 0
6281 }
6282 run_test 118d "Fsync validation inject a delay of the bulk =========="
6283
6284 test_118f() {
6285         reset_async
6286
6287         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6288         lctl set_param fail_loc=0x8000040a
6289
6290         # Should simulate EINVAL error which is fatal
6291         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6292         RC=$?
6293         if [[ $RC -eq 0 ]]; then
6294                 error "Must return error due to dropped pages, rc=$RC"
6295         fi
6296
6297         lctl set_param fail_loc=0x0
6298
6299         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6300         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6301         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6302                     grep -c writeback)
6303         if [[ $LOCKED -ne 0 ]]; then
6304                 error "Locked pages remain in cache, locked=$LOCKED"
6305         fi
6306
6307         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6308                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6309         fi
6310
6311         rm -f $DIR/$tfile
6312         echo "No pages locked after fsync"
6313
6314         reset_async
6315         return 0
6316 }
6317 run_test 118f "Simulate unrecoverable OSC side error =========="
6318
6319 test_118g() {
6320         reset_async
6321
6322         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6323         lctl set_param fail_loc=0x406
6324
6325         # simulate local -ENOMEM
6326         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6327         RC=$?
6328
6329         lctl set_param fail_loc=0
6330         if [[ $RC -eq 0 ]]; then
6331                 error "Must return error due to dropped pages, rc=$RC"
6332         fi
6333
6334         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6335         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6336         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6337                         grep -c writeback)
6338         if [[ $LOCKED -ne 0 ]]; then
6339                 error "Locked pages remain in cache, locked=$LOCKED"
6340         fi
6341
6342         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6343                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6344         fi
6345
6346         rm -f $DIR/$tfile
6347         echo "No pages locked after fsync"
6348
6349         reset_async
6350         return 0
6351 }
6352 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6353
6354 test_118h() {
6355         remote_ost_nodsh && skip "remote OST with nodsh" && return
6356
6357         reset_async
6358
6359         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6360         set_nodes_failloc "$(osts_nodes)" 0x20e
6361         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6362         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6363         RC=$?
6364
6365         set_nodes_failloc "$(osts_nodes)" 0
6366         if [[ $RC -eq 0 ]]; then
6367                 error "Must return error due to dropped pages, rc=$RC"
6368         fi
6369
6370         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6371         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6372         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6373                     grep -c writeback)
6374         if [[ $LOCKED -ne 0 ]]; then
6375                 error "Locked pages remain in cache, locked=$LOCKED"
6376         fi
6377
6378         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6379                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6380         fi
6381
6382         rm -f $DIR/$tfile
6383         echo "No pages locked after fsync"
6384
6385         return 0
6386 }
6387 run_test 118h "Verify timeout in handling recoverables errors  =========="
6388
6389 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6390
6391 test_118i() {
6392         remote_ost_nodsh && skip "remote OST with nodsh" && return
6393
6394         reset_async
6395
6396         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6397         set_nodes_failloc "$(osts_nodes)" 0x20e
6398
6399         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6400         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6401         PID=$!
6402         sleep 5
6403         set_nodes_failloc "$(osts_nodes)" 0
6404
6405         wait $PID
6406         RC=$?
6407         if [[ $RC -ne 0 ]]; then
6408                 error "got error, but should be not, rc=$RC"
6409         fi
6410
6411         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6412         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6413         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6414         if [[ $LOCKED -ne 0 ]]; then
6415                 error "Locked pages remain in cache, locked=$LOCKED"
6416         fi
6417
6418         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6419                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6420         fi
6421
6422         rm -f $DIR/$tfile
6423         echo "No pages locked after fsync"
6424
6425         return 0
6426 }
6427 run_test 118i "Fix error before timeout in recoverable error  =========="
6428
6429 [ "$SLOW" = "no" ] && set_resend_count 4
6430
6431 test_118j() {
6432         remote_ost_nodsh && skip "remote OST with nodsh" && return
6433
6434         reset_async
6435
6436         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6437         set_nodes_failloc "$(osts_nodes)" 0x220
6438
6439         # return -EIO from OST
6440         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6441         RC=$?
6442         set_nodes_failloc "$(osts_nodes)" 0x0
6443         if [[ $RC -eq 0 ]]; then
6444                 error "Must return error due to dropped pages, rc=$RC"
6445         fi
6446
6447         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6448         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6449         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6450         if [[ $LOCKED -ne 0 ]]; then
6451                 error "Locked pages remain in cache, locked=$LOCKED"
6452         fi
6453
6454         # in recoverable error on OST we want resend and stay until it finished
6455         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6456                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6457         fi
6458
6459         rm -f $DIR/$tfile
6460         echo "No pages locked after fsync"
6461
6462         return 0
6463 }
6464 run_test 118j "Simulate unrecoverable OST side error =========="
6465
6466 test_118k()
6467 {
6468         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6469
6470         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6471         set_nodes_failloc "$(osts_nodes)" 0x20e
6472         mkdir -p $DIR/$tdir
6473
6474         for ((i=0;i<10;i++)); do
6475                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6476                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6477                 SLEEPPID=$!
6478                 sleep 0.500s
6479                 kill $SLEEPPID
6480                 wait $SLEEPPID
6481         done
6482
6483         set_nodes_failloc "$(osts_nodes)" 0
6484         rm -rf $DIR/$tdir
6485 }
6486 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6487
6488 test_118l()
6489 {
6490         # LU-646
6491         mkdir -p $DIR/$tdir
6492         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6493         rm -rf $DIR/$tdir
6494 }
6495 run_test 118l "fsync dir ========="
6496
6497 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6498
6499 test_119a() # bug 11737
6500 {
6501         BSIZE=$((512 * 1024))
6502         directio write $DIR/$tfile 0 1 $BSIZE
6503         # We ask to read two blocks, which is more than a file size.
6504         # directio will indicate an error when requested and actual
6505         # sizes aren't equeal (a normal situation in this case) and
6506         # print actual read amount.
6507         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6508         if [ "$NOB" != "$BSIZE" ]; then
6509                 error "read $NOB bytes instead of $BSIZE"
6510         fi
6511         rm -f $DIR/$tfile
6512 }
6513 run_test 119a "Short directIO read must return actual read amount"
6514
6515 test_119b() # bug 11737
6516 {
6517         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6518
6519         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6520         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6521         sync
6522         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6523                 error "direct read failed"
6524         rm -f $DIR/$tfile
6525 }
6526 run_test 119b "Sparse directIO read must return actual read amount"
6527
6528 test_119c() # bug 13099
6529 {
6530         BSIZE=1048576
6531         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6532         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6533         rm -f $DIR/$tfile
6534 }
6535 run_test 119c "Testing for direct read hitting hole"
6536
6537 test_119d() # bug 15950
6538 {
6539         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6540         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6541         BSIZE=1048576
6542         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6543         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6544         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6545         lctl set_param fail_loc=0x40d
6546         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6547         pid_dio=$!
6548         sleep 1
6549         cat $DIR/$tfile > /dev/null &
6550         lctl set_param fail_loc=0
6551         pid_reads=$!
6552         wait $pid_dio
6553         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6554         sleep 2
6555         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6556         error "the read rpcs have not completed in 2s"
6557         rm -f $DIR/$tfile
6558         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6559 }
6560 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6561
6562 test_120a() {
6563         mkdir -p $DIR/$tdir
6564         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6565                skip "no early lock cancel on server" && return 0
6566         lru_resize_disable mdc
6567         lru_resize_disable osc
6568         cancel_lru_locks mdc
6569         stat $DIR/$tdir > /dev/null
6570         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6571         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6572         mkdir $DIR/$tdir/d1
6573         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6574         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6575         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6576         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6577         lru_resize_enable mdc
6578         lru_resize_enable osc
6579 }
6580 run_test 120a "Early Lock Cancel: mkdir test"
6581
6582 test_120b() {
6583         mkdir -p $DIR/$tdir
6584         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6585                skip "no early lock cancel on server" && return 0
6586         lru_resize_disable mdc
6587         lru_resize_disable osc
6588         cancel_lru_locks mdc
6589         stat $DIR/$tdir > /dev/null
6590         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6591         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6592         touch $DIR/$tdir/f1
6593         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6594         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6595         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6596         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6597         lru_resize_enable mdc
6598         lru_resize_enable osc
6599 }
6600 run_test 120b "Early Lock Cancel: create test"
6601
6602 test_120c() {
6603         mkdir -p $DIR/$tdir
6604         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6605                skip "no early lock cancel on server" && return 0
6606         lru_resize_disable mdc
6607         lru_resize_disable osc
6608         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6609         touch $DIR/$tdir/d1/f1
6610         cancel_lru_locks mdc
6611         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6612         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6613         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6614         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6615         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6616         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6617         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6618         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6619         lru_resize_enable mdc
6620         lru_resize_enable osc
6621 }
6622 run_test 120c "Early Lock Cancel: link test"
6623
6624 test_120d() {
6625         mkdir -p $DIR/$tdir
6626         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6627                skip "no early lock cancel on server" && return 0
6628         lru_resize_disable mdc
6629         lru_resize_disable osc
6630         touch $DIR/$tdir
6631         cancel_lru_locks mdc
6632         stat $DIR/$tdir > /dev/null
6633         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6634         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6635         chmod a+x $DIR/$tdir
6636         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6637         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6638         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6639         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6640         lru_resize_enable mdc
6641         lru_resize_enable osc
6642 }
6643 run_test 120d "Early Lock Cancel: setattr test"
6644
6645 test_120e() {
6646         mkdir -p $DIR/$tdir
6647         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6648                skip "no early lock cancel on server" && return 0
6649         lru_resize_disable mdc
6650         lru_resize_disable osc
6651         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6652         cancel_lru_locks mdc
6653         cancel_lru_locks osc
6654         dd if=$DIR/$tdir/f1 of=/dev/null
6655         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
6656         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6657               awk '/ldlm_cancel/ {print $2}'`
6658         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6659               awk '/ldlm_bl_callback/ {print $2}'`
6660         unlink $DIR/$tdir/f1
6661         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6662               awk '/ldlm_cancel/ {print $2}'`
6663         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6664               awk '/ldlm_bl_callback/ {print $2}'`
6665         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6666         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6667         lru_resize_enable mdc
6668         lru_resize_enable osc
6669 }
6670 run_test 120e "Early Lock Cancel: unlink test"
6671
6672 test_120f() {
6673         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6674                skip "no early lock cancel on server" && return 0
6675         mkdir -p $DIR/$tdir
6676         lru_resize_disable mdc
6677         lru_resize_disable osc
6678         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6679         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
6680         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
6681         cancel_lru_locks mdc
6682         cancel_lru_locks osc
6683         dd if=$DIR/$tdir/d1/f1 of=/dev/null
6684         dd if=$DIR/$tdir/d2/f2 of=/dev/null
6685         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
6686         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6687               awk '/ldlm_cancel/ {print $2}'`
6688         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6689               awk '/ldlm_bl_callback/ {print $2}'`
6690         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6691         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6692               awk '/ldlm_cancel/ {print $2}'`
6693         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6694               awk '/ldlm_bl_callback/ {print $2}'`
6695         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6696         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6697         lru_resize_enable mdc
6698         lru_resize_enable osc
6699 }
6700 run_test 120f "Early Lock Cancel: rename test"
6701
6702 test_120g() {
6703         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6704                skip "no early lock cancel on server" && return 0
6705         lru_resize_disable mdc
6706         lru_resize_disable osc
6707         count=10000
6708         echo create $count files
6709         mkdir -p $DIR/$tdir
6710         cancel_lru_locks mdc
6711         cancel_lru_locks osc
6712         t0=`date +%s`
6713
6714         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6715               awk '/ldlm_cancel/ {print $2}'`
6716         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6717               awk '/ldlm_bl_callback/ {print $2}'`
6718         createmany -o $DIR/$tdir/f $count
6719         sync
6720         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6721               awk '/ldlm_cancel/ {print $2}'`
6722         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6723               awk '/ldlm_bl_callback/ {print $2}'`
6724         t1=`date +%s`
6725         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
6726         echo rm $count files
6727         rm -r $DIR/$tdir
6728         sync
6729         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6730               awk '/ldlm_cancel/ {print $2}'`
6731         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6732               awk '/ldlm_bl_callback/ {print $2}'`
6733         t2=`date +%s`
6734         echo total: $count removes in $((t2-t1))
6735         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
6736         sleep 2
6737         # wait for commitment of removal
6738         lru_resize_enable mdc
6739         lru_resize_enable osc
6740 }
6741 run_test 120g "Early Lock Cancel: performance test"
6742
6743 test_121() { #bug #10589
6744         rm -rf $DIR/$tfile
6745         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
6746 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
6747         lctl set_param fail_loc=0x310
6748         cancel_lru_locks osc > /dev/null
6749         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
6750         lctl set_param fail_loc=0
6751         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
6752 }
6753 run_test 121 "read cancel race ========="
6754
6755 test_123a() { # was test 123, statahead(bug 11401)
6756         SLOWOK=0
6757         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
6758             log "testing on UP system. Performance may be not as good as expected."
6759                         SLOWOK=1
6760         fi
6761
6762         rm -rf $DIR/$tdir
6763         mkdir -p $DIR/$tdir
6764         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6765         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
6766         MULT=10
6767         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
6768                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
6769
6770                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6771                 lctl set_param -n llite.*.statahead_max 0
6772                 lctl get_param llite.*.statahead_max
6773                 cancel_lru_locks mdc
6774                 cancel_lru_locks osc
6775                 stime=`date +%s`
6776                 time ls -l $DIR/$tdir | wc -l
6777                 etime=`date +%s`
6778                 delta=$((etime - stime))
6779                 log "ls $i files without statahead: $delta sec"
6780                 lctl set_param llite.*.statahead_max=$max
6781
6782                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6783                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
6784                 cancel_lru_locks mdc
6785                 cancel_lru_locks osc
6786                 stime=`date +%s`
6787                 time ls -l $DIR/$tdir | wc -l
6788                 etime=`date +%s`
6789                 delta_sa=$((etime - stime))
6790                 log "ls $i files with statahead: $delta_sa sec"
6791                 lctl get_param -n llite.*.statahead_stats
6792                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6793
6794                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
6795                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
6796
6797                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6798                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6799                     lctl set_param -n llite.*.statahead_max 0
6800                     lctl get_param llite.*.statahead_max
6801                     cancel_lru_locks mdc
6802                     cancel_lru_locks osc
6803                     stime=`date +%s`
6804                     time ls -l $DIR/$tdir | wc -l
6805                     etime=`date +%s`
6806                     delta=$((etime - stime))
6807                     log "ls $i files again without statahead: $delta sec"
6808                     lctl set_param llite.*.statahead_max=$max
6809                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6810                         if [  $SLOWOK -eq 0 ]; then
6811                                 error "ls $i files is slower with statahead!"
6812                         else
6813                                 log "ls $i files is slower with statahead!"
6814                         fi
6815                         break
6816                     fi
6817                 fi
6818
6819                 [ $delta -gt 20 ] && break
6820                 [ $delta -gt 8 ] && MULT=$((50 / delta))
6821                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
6822         done
6823         log "ls done"
6824
6825         stime=`date +%s`
6826         rm -r $DIR/$tdir
6827         sync
6828         etime=`date +%s`
6829         delta=$((etime - stime))
6830         log "rm -r $DIR/$tdir/: $delta seconds"
6831         log "rm done"
6832         lctl get_param -n llite.*.statahead_stats
6833 }
6834 run_test 123a "verify statahead work"
6835
6836 test_123b () { # statahead(bug 15027)
6837         mkdir -p $DIR/$tdir
6838         createmany -o $DIR/$tdir/$tfile-%d 1000
6839
6840         cancel_lru_locks mdc
6841         cancel_lru_locks osc
6842
6843 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
6844         lctl set_param fail_loc=0x80000803
6845         ls -lR $DIR/$tdir > /dev/null
6846         log "ls done"
6847         lctl set_param fail_loc=0x0
6848         lctl get_param -n llite.*.statahead_stats
6849         rm -r $DIR/$tdir
6850         sync
6851
6852 }
6853 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
6854
6855 test_124a() {
6856         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6857                skip "no lru resize on server" && return 0
6858         local NR=2000
6859         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
6860
6861         log "create $NR files at $DIR/$tdir"
6862         createmany -o $DIR/$tdir/f $NR ||
6863                 error "failed to create $NR files in $DIR/$tdir"
6864
6865         cancel_lru_locks mdc
6866         ls -l $DIR/$tdir > /dev/null
6867
6868         local NSDIR=""
6869         local LRU_SIZE=0
6870         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
6871                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6872                 LRU_SIZE=$(lctl get_param -n $PARAM)
6873                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
6874                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
6875                                                 log "NSDIR=$NSDIR"
6876                         log "NS=$(basename $NSDIR)"
6877                         break
6878                 fi
6879         done
6880
6881         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
6882                 skip "Not enough cached locks created!"
6883                 return 0
6884         fi
6885         log "LRU=$LRU_SIZE"
6886
6887         local SLEEP=30
6888
6889         # We know that lru resize allows one client to hold $LIMIT locks
6890         # for 10h. After that locks begin to be killed by client.
6891         local MAX_HRS=10
6892         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
6893                 log "LIMIT=$LIMIT"
6894
6895         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
6896         # killing locks. Some time was spent for creating locks. This means
6897         # that up to the moment of sleep finish we must have killed some of
6898         # them (10-100 locks). This depends on how fast ther were created.
6899         # Many of them were touched in almost the same moment and thus will
6900         # be killed in groups.
6901         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
6902
6903         # Use $LRU_SIZE_B here to take into account real number of locks
6904         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
6905         local LRU_SIZE_B=$LRU_SIZE
6906         log "LVF=$LVF"
6907         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
6908                 log "OLD_LVF=$OLD_LVF"
6909         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
6910
6911         # Let's make sure that we really have some margin. Client checks
6912         # cached locks every 10 sec.
6913         SLEEP=$((SLEEP+20))
6914         log "Sleep ${SLEEP} sec"
6915         local SEC=0
6916         while ((SEC<$SLEEP)); do
6917                 echo -n "..."
6918                 sleep 5
6919                 SEC=$((SEC+5))
6920                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
6921                 echo -n "$LRU_SIZE"
6922         done
6923         echo ""
6924         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
6925         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
6926
6927         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
6928                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
6929                 unlinkmany $DIR/$tdir/f $NR
6930                 return
6931         }
6932
6933         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
6934         log "unlink $NR files at $DIR/$tdir"
6935         unlinkmany $DIR/$tdir/f $NR
6936 }
6937 run_test 124a "lru resize ======================================="
6938
6939 get_max_pool_limit()
6940 {
6941         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
6942         local max=0
6943         for l in $limit; do
6944                 if test $l -gt $max; then
6945                         max=$l
6946                 fi
6947         done
6948         echo $max
6949 }
6950
6951 test_124b() {
6952         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6953                skip "no lru resize on server" && return 0
6954
6955         LIMIT=`get_max_pool_limit`
6956
6957         NR=$(($(default_lru_size)*20))
6958         if [ $NR -gt $LIMIT ]; then
6959                 log "Limit lock number by $LIMIT locks"
6960                 NR=$LIMIT
6961         fi
6962         lru_resize_disable mdc
6963         mkdir -p $DIR/$tdir/disable_lru_resize ||
6964                 error "failed to create $DIR/$tdir/disable_lru_resize"
6965
6966         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
6967         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
6968         cancel_lru_locks mdc
6969         stime=`date +%s`
6970         PID=""
6971         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6972         PID="$PID $!"
6973         sleep 2
6974         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6975         PID="$PID $!"
6976         sleep 2
6977         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6978         PID="$PID $!"
6979         wait $PID
6980         etime=`date +%s`
6981         nolruresize_delta=$((etime-stime))
6982         log "ls -la time: $nolruresize_delta seconds"
6983         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6984         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
6985
6986         lru_resize_enable mdc
6987         mkdir -p $DIR/$tdir/enable_lru_resize ||
6988                 error "failed to create $DIR/$tdir/enable_lru_resize"
6989
6990         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
6991         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
6992         cancel_lru_locks mdc
6993         stime=`date +%s`
6994         PID=""
6995         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6996         PID="$PID $!"
6997         sleep 2
6998         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6999         PID="$PID $!"
7000         sleep 2
7001         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
7002         PID="$PID $!"
7003         wait $PID
7004         etime=`date +%s`
7005         lruresize_delta=$((etime-stime))
7006         log "ls -la time: $lruresize_delta seconds"
7007         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
7008
7009         if [ $lruresize_delta -gt $nolruresize_delta ]; then
7010                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
7011         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
7012                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
7013         else
7014                 log "lru resize performs the same with no lru resize"
7015         fi
7016         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
7017 }
7018 run_test 124b "lru resize (performance test) ======================="
7019
7020 test_125() { # 13358
7021         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7022         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
7023         mkdir -p $DIR/d125 || error "mkdir failed"
7024         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
7025         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
7026         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
7027 }
7028 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
7029
7030 test_126() { # bug 12829/13455
7031         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
7032         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
7033         $GSS && skip "must run as gss disabled" && return
7034
7035         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
7036         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
7037         rm -f $DIR/$tfile
7038         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
7039 }
7040 run_test 126 "check that the fsgid provided by the client is taken into account"
7041
7042 test_127a() { # bug 15521
7043         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
7044         $LCTL set_param osc.*.stats=0
7045         FSIZE=$((2048 * 1024))
7046         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7047         cancel_lru_locks osc
7048         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
7049
7050         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
7051         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7052                 echo "got $COUNT $NAME"
7053                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
7054                 eval $NAME=$COUNT || error "Wrong proc format"
7055
7056                 case $NAME in
7057                         read_bytes|write_bytes)
7058                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
7059                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
7060                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
7061                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
7062                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
7063                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
7064                                 error "sumsquare is too small: $SUMSQ"
7065                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
7066                                 error "sumsquare is too big: $SUMSQ"
7067                         ;;
7068                         *) ;;
7069                 esac
7070         done < $DIR/${tfile}.tmp
7071
7072         #check that we actually got some stats
7073         [ "$read_bytes" ] || error "Missing read_bytes stats"
7074         [ "$write_bytes" ] || error "Missing write_bytes stats"
7075         [ "$read_bytes" != 0 ] || error "no read done"
7076         [ "$write_bytes" != 0 ] || error "no write done"
7077 }
7078 run_test 127a "verify the client stats are sane"
7079
7080 test_127b() { # bug LU-333
7081         $LCTL set_param llite.*.stats=0
7082         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
7083         # perform 2 reads and writes so MAX is different from SUM.
7084         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7085         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
7086         cancel_lru_locks osc
7087         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7088         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
7089
7090         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
7091         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
7092                 echo "got $COUNT $NAME"
7093                 eval $NAME=$COUNT || error "Wrong proc format"
7094
7095         case $NAME in
7096                 read_bytes)
7097                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7098                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7099                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7100                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7101                         ;;
7102                 write_bytes)
7103                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
7104                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
7105                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
7106                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
7107                         ;;
7108                         *) ;;
7109                 esac
7110         done < $TMP/${tfile}.tmp
7111
7112         #check that we actually got some stats
7113         [ "$read_bytes" ] || error "Missing read_bytes stats"
7114         [ "$write_bytes" ] || error "Missing write_bytes stats"
7115         [ "$read_bytes" != 0 ] || error "no read done"
7116         [ "$write_bytes" != 0 ] || error "no write done"
7117 }
7118 run_test 127b "verify the llite client stats are sane"
7119
7120 test_128() { # bug 15212
7121         touch $DIR/$tfile
7122         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7123                 find $DIR/$tfile
7124                 find $DIR/$tfile
7125         EOF
7126
7127         result=$(grep error $TMP/$tfile.log)
7128         rm -f $DIR/$tfile
7129         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7130 }
7131 run_test 128 "interactive lfs for 2 consecutive find's"
7132
7133 set_dir_limits () {
7134         local mntdev
7135         local canondev
7136         local node
7137
7138         local LDPROC=/proc/fs/ldiskfs
7139
7140         for facet in $(get_facets MDS); do
7141                 canondev=$(ldiskfs_canon *.$(convert_facet2label $facet).mntdev $facet)
7142                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" || LDPROC=/sys/fs/ldiskfs
7143                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7144         done
7145 }
7146 test_129() {
7147         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
7148                 skip "Only applicable to ldiskfs-based MDTs"
7149                 return
7150         fi
7151         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7152
7153         EFBIG=27
7154         MAX=16384
7155
7156         set_dir_limits $MAX
7157
7158         mkdir -p $DIR/$tdir
7159
7160         I=0
7161         J=0
7162         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7163                 $MULTIOP $DIR/$tdir/$J Oc
7164                 rc=$?
7165                 if [ $rc -eq $EFBIG ]; then
7166                         set_dir_limits 0
7167                         echo "return code $rc received as expected"
7168                         return 0
7169                 elif [ $rc -ne 0 ]; then
7170                         set_dir_limits 0
7171                         error_exit "return code $rc received instead of expected $EFBIG"
7172                 fi
7173                 J=$((J+1))
7174                 I=$(stat -c%s "$DIR/$tdir")
7175         done
7176
7177         set_dir_limits 0
7178         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7179 }
7180 run_test 129 "test directory size limit ========================"
7181
7182 OLDIFS="$IFS"
7183 cleanup_130() {
7184         trap 0
7185         IFS="$OLDIFS"
7186 }
7187
7188 test_130a() {
7189         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7190         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7191                 return
7192
7193         trap cleanup_130 EXIT RETURN
7194
7195         local fm_file=$DIR/$tfile
7196         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7197         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7198                 error "dd failed for $fm_file"
7199
7200         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7201         filefrag -ves $fm_file
7202         RC=$?
7203         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7204                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7205         [ $RC != 0 ] && error "filefrag $fm_file failed"
7206
7207         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7208                       grep -v "ext:" | grep -v "found")
7209         lun=$($GETSTRIPE -i $fm_file)
7210
7211         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7212         IFS=$'\n'
7213         tot_len=0
7214         for line in $filefrag_op
7215         do
7216                 frag_lun=`echo $line | cut -d: -f5`
7217                 ext_len=`echo $line | cut -d: -f4`
7218                 if (( $frag_lun != $lun )); then
7219                         cleanup_130
7220                         error "FIEMAP on 1-stripe file($fm_file) failed"
7221                         return
7222                 fi
7223                 (( tot_len += ext_len ))
7224         done
7225
7226         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7227                 cleanup_130
7228                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7229                 return
7230         fi
7231
7232         cleanup_130
7233
7234         echo "FIEMAP on single striped file succeeded"
7235 }
7236 run_test 130a "FIEMAP (1-stripe file)"
7237
7238 test_130b() {
7239         [ "$OSTCOUNT" -lt "2" ] &&
7240                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7241
7242         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7243         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7244                 return
7245
7246         trap cleanup_130 EXIT RETURN
7247
7248         local fm_file=$DIR/$tfile
7249         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7250         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7251                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7252
7253         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7254                 error "dd failed on $fm_file"
7255
7256         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7257         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7258                       grep -v "ext:" | grep -v "found")
7259
7260         last_lun=$(echo $filefrag_op | cut -d: -f5)
7261
7262         IFS=$'\n'
7263         tot_len=0
7264         num_luns=1
7265         for line in $filefrag_op
7266         do
7267                 frag_lun=`echo $line | cut -d: -f5`
7268                 ext_len=`echo $line | cut -d: -f4`
7269                 if (( $frag_lun != $last_lun )); then
7270                         if (( tot_len != 1024 )); then
7271                                 cleanup_130
7272                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7273                                 return
7274                         else
7275                                 (( num_luns += 1 ))
7276                                 tot_len=0
7277                         fi
7278                 fi
7279                 (( tot_len += ext_len ))
7280                 last_lun=$frag_lun
7281         done
7282         if (( num_luns != 2 || tot_len != 1024 )); then
7283                 cleanup_130
7284                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7285                 return
7286         fi
7287
7288         cleanup_130
7289
7290         echo "FIEMAP on 2-stripe file succeeded"
7291 }
7292 run_test 130b "FIEMAP (2-stripe file)"
7293
7294 test_130c() {
7295         [ "$OSTCOUNT" -lt "2" ] &&
7296                 skip_env "skipping FIEMAP on 2-stripe file" && return
7297
7298         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7299         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7300                 return
7301
7302         trap cleanup_130 EXIT RETURN
7303
7304         local fm_file=$DIR/$tfile
7305         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7306         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7307                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7308
7309         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7310
7311         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7312         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7313
7314         last_lun=`echo $filefrag_op | cut -d: -f5`
7315
7316         IFS=$'\n'
7317         tot_len=0
7318         num_luns=1
7319         for line in $filefrag_op
7320         do
7321                 frag_lun=`echo $line | cut -d: -f5`
7322                 ext_len=`echo $line | cut -d: -f4`
7323                 if (( $frag_lun != $last_lun )); then
7324                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7325                         if (( logical != 512 )); then
7326                                 cleanup_130
7327                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7328                                 return
7329                         fi
7330                         if (( tot_len != 512 )); then
7331                                 cleanup_130
7332                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7333                                 return
7334                         else
7335                                 (( num_luns += 1 ))
7336                                 tot_len=0
7337                         fi
7338                 fi
7339                 (( tot_len += ext_len ))
7340                 last_lun=$frag_lun
7341         done
7342         if (( num_luns != 2 || tot_len != 512 )); then
7343                 cleanup_130
7344                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7345                 return
7346         fi
7347
7348         cleanup_130
7349
7350         echo "FIEMAP on 2-stripe file with hole succeeded"
7351 }
7352 run_test 130c "FIEMAP (2-stripe file with hole)"
7353
7354 test_130d() {
7355         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7356
7357         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7358         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7359
7360         trap cleanup_130 EXIT RETURN
7361
7362         local fm_file=$DIR/$tfile
7363         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7364         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7365                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7366         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7367
7368         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7369         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7370
7371         last_lun=`echo $filefrag_op | cut -d: -f5`
7372
7373         IFS=$'\n'
7374         tot_len=0
7375         num_luns=1
7376         for line in $filefrag_op
7377         do
7378                 frag_lun=`echo $line | cut -d: -f5`
7379                 ext_len=`echo $line | cut -d: -f4`
7380                 if (( $frag_lun != $last_lun )); then
7381                         if (( tot_len != 1024 )); then
7382                                 cleanup_130
7383                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7384                                 return
7385                         else
7386                                 (( num_luns += 1 ))
7387                                 tot_len=0
7388                         fi
7389                 fi
7390                 (( tot_len += ext_len ))
7391                 last_lun=$frag_lun
7392         done
7393         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7394                 cleanup_130
7395                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7396                 return
7397         fi
7398
7399         cleanup_130
7400
7401         echo "FIEMAP on N-stripe file succeeded"
7402 }
7403 run_test 130d "FIEMAP (N-stripe file)"
7404
7405 test_130e() {
7406         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7407
7408         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7409         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7410
7411         trap cleanup_130 EXIT RETURN
7412
7413         local fm_file=$DIR/$tfile
7414         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7415         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7416                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7417
7418         NUM_BLKS=512
7419         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7420         for ((i = 0; i < $NUM_BLKS; i++))
7421         do
7422                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7423         done
7424
7425         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7426         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7427
7428         last_lun=`echo $filefrag_op | cut -d: -f5`
7429
7430         IFS=$'\n'
7431         tot_len=0
7432         num_luns=1
7433         for line in $filefrag_op
7434         do
7435                 frag_lun=`echo $line | cut -d: -f5`
7436                 ext_len=`echo $line | cut -d: -f4`
7437                 if (( $frag_lun != $last_lun )); then
7438                         if (( tot_len != $EXPECTED_LEN )); then
7439                                 cleanup_130
7440                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7441                                 return
7442                         else
7443                                 (( num_luns += 1 ))
7444                                 tot_len=0
7445                         fi
7446                 fi
7447                 (( tot_len += ext_len ))
7448                 last_lun=$frag_lun
7449         done
7450         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7451                 cleanup_130
7452                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7453                 return
7454         fi
7455
7456         cleanup_130
7457
7458         echo "FIEMAP with continuation calls succeeded"
7459 }
7460 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7461
7462 # Test for writev/readv
7463 test_131a() {
7464         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7465         error "writev test failed"
7466         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7467         error "readv failed"
7468         rm -f $DIR/$tfile
7469 }
7470 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7471
7472 test_131b() {
7473         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7474         error "append writev test failed"
7475         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7476         error "append writev test failed"
7477         rm -f $DIR/$tfile
7478 }
7479 run_test 131b "test append writev"
7480
7481 test_131c() {
7482         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7483         error "NOT PASS"
7484 }
7485 run_test 131c "test read/write on file w/o objects"
7486
7487 test_131d() {
7488         rwv -f $DIR/$tfile -w -n 1 1572864
7489         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7490         if [ "$NOB" != 1572864 ]; then
7491                 error "Short read filed: read $NOB bytes instead of 1572864"
7492         fi
7493         rm -f $DIR/$tfile
7494 }
7495 run_test 131d "test short read"
7496
7497 test_131e() {
7498         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7499         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7500         error "read hitting hole failed"
7501         rm -f $DIR/$tfile
7502 }
7503 run_test 131e "test read hitting hole"
7504
7505 get_ost_param() {
7506         local token=$1
7507         local gl_sum=0
7508         for node in $(osts_nodes); do
7509                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7510                 [ x$gl = x"" ] && gl=0
7511                 gl_sum=$((gl_sum + gl))
7512         done
7513         echo $gl
7514 }
7515
7516 som_mode_switch() {
7517         local som=$1
7518         local gl1=$2
7519         local gl2=$3
7520
7521         if [ x$som = x"enabled" ]; then
7522                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7523                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7524                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7525         else
7526                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7527                 MOUNTOPT="$MOUNTOPT,som_preview"
7528                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7529         fi
7530
7531         # do remount to make new mount-conf parameters actual
7532         echo remounting...
7533         sync
7534         stopall
7535         setupall
7536 }
7537
7538 test_132() { #1028, SOM
7539         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7540         local num=$(get_mds_dir $DIR)
7541         local mymds=mds${num}
7542         local MOUNTOPT_SAVE=$MOUNTOPT
7543
7544         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7545         cancel_lru_locks osc
7546
7547         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7548
7549         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7550         stat $DIR/$tfile >/dev/null
7551         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7552         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7553         rm $DIR/$tfile
7554         som_mode_switch $som1 $gl1 $gl2
7555
7556         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7557         cancel_lru_locks osc
7558
7559         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7560         if [ $som1 == $som2 ]; then
7561             error "som is still "$som2
7562             if [ x$som2 = x"enabled" ]; then
7563                 som2="disabled"
7564             else
7565                 som2="enabled"
7566             fi
7567         fi
7568
7569         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7570         stat $DIR/$tfile >/dev/null
7571         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7572         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7573         som_mode_switch $som2 $gl1 $gl2
7574         MOUNTOPT=$MOUNTOPT_SAVE
7575 }
7576 run_test 132 "som avoids glimpse rpc"
7577
7578 check_stats() {
7579         local res
7580         local count
7581         case $1 in
7582         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7583                  ;;
7584         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7585                  ;;
7586         *) error "Wrong argument $1" ;;
7587         esac
7588         echo $res
7589         count=`echo $res | awk '{print $2}'`
7590         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7591         # if the argument $3 is zero, it means any stat increment is ok.
7592         if [ $3 -gt 0 ] ; then
7593                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7594         fi
7595 }
7596
7597 test_133a() {
7598         remote_ost_nodsh && skip "remote OST with nodsh" && return
7599         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7600
7601         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7602                 { skip "MDS doesn't support rename stats"; return; }
7603         local testdir=$DIR/${tdir}/stats_testdir
7604         mkdir -p $DIR/${tdir}
7605
7606         # clear stats.
7607         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7608         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7609
7610         # verify mdt stats first.
7611         mkdir ${testdir} || error "mkdir failed"
7612         check_stats $SINGLEMDS "mkdir" 1
7613         touch ${testdir}/${tfile} || "touch failed"
7614         check_stats $SINGLEMDS "open" 1
7615         check_stats $SINGLEMDS "close" 1
7616         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7617         check_stats $SINGLEMDS "mknod" 1
7618         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7619         check_stats $SINGLEMDS "unlink" 1
7620         rm -f ${testdir}/${tfile} || error "file remove failed"
7621         check_stats $SINGLEMDS "unlink" 2
7622
7623         # remove working dir and check mdt stats again.
7624         rmdir ${testdir} || error "rmdir failed"
7625         check_stats $SINGLEMDS "rmdir" 1
7626
7627         local testdir1=$DIR/${tdir}/stats_testdir1
7628         mkdir -p ${testdir}
7629         mkdir -p ${testdir1}
7630         touch ${testdir1}/test1
7631         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7632         check_stats $SINGLEMDS "crossdir_rename" 1
7633
7634         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
7635         check_stats $SINGLEMDS "samedir_rename" 1
7636
7637         rm -rf $DIR/${tdir}
7638 }
7639 run_test 133a "Verifying MDT stats ========================================"
7640
7641 test_133b() {
7642         remote_ost_nodsh && skip "remote OST with nodsh" && return
7643         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7644         local testdir=$DIR/${tdir}/stats_testdir
7645         mkdir -p ${testdir} || error "mkdir failed"
7646         touch ${testdir}/${tfile} || "touch failed"
7647         cancel_lru_locks mdc
7648
7649         # clear stats.
7650         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7651         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7652
7653         # extra mdt stats verification.
7654         chmod 444 ${testdir}/${tfile} || error "chmod failed"
7655         check_stats $SINGLEMDS "setattr" 1
7656         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7657         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
7658         then            # LU-1740
7659                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
7660                 check_stats $SINGLEMDS "getattr" 1
7661         fi
7662         $LFS df || error "lfs failed"
7663         check_stats $SINGLEMDS "statfs" 1
7664
7665         rm -rf $DIR/${tdir}
7666 }
7667 run_test 133b "Verifying extra MDT stats =================================="
7668
7669 test_133c() {
7670         remote_ost_nodsh && skip "remote OST with nodsh" && return
7671         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7672         local testdir=$DIR/${tdir}/stats_testdir
7673         mkdir -p ${testdir} || error "mkdir failed"
7674
7675         # verify obdfilter stats.
7676         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
7677         sync
7678         cancel_lru_locks osc
7679         wait_delete_completed
7680
7681         # clear stats.
7682         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7683         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7684
7685         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
7686         sync
7687         cancel_lru_locks osc
7688         check_stats ost "write" 1
7689
7690         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
7691         check_stats ost "read" 1
7692
7693         > ${testdir}/${tfile} || error "truncate failed"
7694         check_stats ost "punch" 1
7695
7696         rm -f ${testdir}/${tfile} || error "file remove failed"
7697         wait_delete_completed
7698         check_stats ost "destroy" 1
7699
7700         rm -rf $DIR/${tdir}
7701 }
7702 run_test 133c "Verifying OST stats ========================================"
7703
7704 order_2() {
7705     local value=$1
7706     local orig=$value
7707     local order=1
7708
7709     while [ $value -ge 2 ]; do
7710         order=$((order*2))
7711         value=$((value/2))
7712     done
7713
7714     if [ $orig -gt $order ]; then
7715         order=$((order*2))
7716     fi
7717     echo $order
7718 }
7719
7720 size_in_KMGT() {
7721     local value=$1
7722     local size=('K' 'M' 'G' 'T');
7723     local i=0
7724     local size_string=$value
7725
7726     while [ $value -ge 1024 ]; do
7727         if [ $i -gt 3 ]; then
7728             #T is the biggest unit we get here, if that is bigger,
7729             #just return XXXT
7730             size_string=${value}T
7731             break
7732         fi
7733         value=$((value >> 10))
7734         if [ $value -lt 1024 ]; then
7735             size_string=${value}${size[$i]}
7736             break
7737         fi
7738         i=$((i + 1))
7739     done
7740
7741     echo $size_string
7742 }
7743
7744 get_rename_size() {
7745     local size=$1
7746     local context=${2:-.}
7747     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
7748                 grep -A1 $context |
7749                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
7750     echo $sample
7751 }
7752
7753 test_133d() {
7754     remote_ost_nodsh && skip "remote OST with nodsh" && return
7755     remote_mds_nodsh && skip "remote MDS with nodsh" && return
7756     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7757         { skip "MDS doesn't support rename stats"; return; }
7758
7759     local testdir1=$DIR/${tdir}/stats_testdir1
7760     local testdir2=$DIR/${tdir}/stats_testdir2
7761
7762     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7763
7764     mkdir -p ${testdir1} || error "mkdir failed"
7765     mkdir -p ${testdir2} || error "mkdir failed"
7766
7767     createmany -o $testdir1/test 512 || error "createmany failed"
7768
7769         # check samedir rename size
7770         mv ${testdir1}/test0 ${testdir1}/test_0
7771
7772         local testdir1_size=$(ls -l $DIR/${tdir} |
7773                 awk '/stats_testdir1/ {print $5}')
7774         local testdir2_size=$(ls -l $DIR/${tdir} |
7775                 awk '/stats_testdir2/ {print $5}')
7776
7777         testdir1_size=$(order_2 $testdir1_size)
7778         testdir2_size=$(order_2 $testdir2_size)
7779
7780         testdir1_size=$(size_in_KMGT $testdir1_size)
7781         testdir2_size=$(size_in_KMGT $testdir2_size)
7782
7783         echo "source rename dir size: ${testdir1_size}"
7784         echo "target rename dir size: ${testdir2_size}"
7785
7786     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
7787     eval $cmd || error "$cmd failed"
7788     local samedir=$($cmd | grep 'same_dir')
7789     local same_sample=$(get_rename_size $testdir1_size)
7790     [ -z "$samedir" ] && error "samedir_rename_size count error"
7791     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
7792     echo "Check same dir rename stats success"
7793
7794     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7795
7796     # check crossdir rename size
7797     mv ${testdir1}/test_0 ${testdir2}/test_0
7798
7799         testdir1_size=$(ls -l $DIR/${tdir} |
7800                 awk '/stats_testdir1/ {print $5}')
7801         testdir2_size=$(ls -l $DIR/${tdir} |
7802                 awk '/stats_testdir2/ {print $5}')
7803
7804         testdir1_size=$(order_2 $testdir1_size)
7805         testdir2_size=$(order_2 $testdir2_size)
7806
7807         testdir1_size=$(size_in_KMGT $testdir1_size)
7808         testdir2_size=$(size_in_KMGT $testdir2_size)
7809
7810         echo "source rename dir size: ${testdir1_size}"
7811         echo "target rename dir size: ${testdir2_size}"
7812
7813     eval $cmd || error "$cmd failed"
7814     local crossdir=$($cmd | grep 'crossdir')
7815     local src_sample=$(get_rename_size $testdir1_size crossdir_src)
7816     local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
7817     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
7818     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
7819     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
7820     echo "Check cross dir rename stats success"
7821     rm -rf $DIR/${tdir}
7822 }
7823 run_test 133d "Verifying rename_stats ========================================"
7824
7825 test_140() { #bug-17379
7826         mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
7827         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
7828         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
7829
7830         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
7831         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
7832         local i=0
7833         while i=`expr $i + 1`; do
7834                 mkdir -p $i || error "Creating dir $i"
7835                 cd $i || error "Changing to $i"
7836                 ln -s ../stat stat || error "Creating stat symlink"
7837                 # Read the symlink until ELOOP present,
7838                 # not LBUGing the system is considered success,
7839                 # we didn't overrun the stack.
7840                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
7841                 [ $ret -ne 0 ] && {
7842                         if [ $ret -eq 40 ]; then
7843                                 break  # -ELOOP
7844                         else
7845                                 error "Open stat symlink"
7846                                 return
7847                         fi
7848                 }
7849         done
7850         i=`expr $i - 1`
7851         echo "The symlink depth = $i"
7852         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
7853                                         error "Invalid symlink depth"
7854
7855         # Test recursive symlink
7856         ln -s symlink_self symlink_self
7857         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
7858         echo "open symlink_self returns $ret"
7859         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
7860 }
7861 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
7862
7863 test_150() {
7864         local TF="$TMP/$tfile"
7865
7866         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7867         cp $TF $DIR/$tfile
7868         cancel_lru_locks osc
7869         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
7870         remount_client $MOUNT
7871         df -P $MOUNT
7872         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
7873
7874         $TRUNCATE $TF 6000
7875         $TRUNCATE $DIR/$tfile 6000
7876         cancel_lru_locks osc
7877         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
7878
7879         echo "12345" >>$TF
7880         echo "12345" >>$DIR/$tfile
7881         cancel_lru_locks osc
7882         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
7883
7884         echo "12345" >>$TF
7885         echo "12345" >>$DIR/$tfile
7886         cancel_lru_locks osc
7887         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
7888
7889         rm -f $TF
7890         true
7891 }
7892 run_test 150 "truncate/append tests"
7893
7894 function roc_hit() {
7895         local list=$(comma_list $(osts_nodes))
7896
7897         echo $(get_obdfilter_param $list '' stats |
7898                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
7899 }
7900
7901 function set_cache() {
7902         local on=1
7903
7904         if [ "$2" == "off" ]; then
7905                 on=0;
7906         fi
7907         local list=$(comma_list $(osts_nodes))
7908         set_obdfilter_param $list '' $1_cache_enable $on
7909
7910         cancel_lru_locks osc
7911 }
7912
7913 test_151() {
7914         remote_ost_nodsh && skip "remote OST with nodsh" && return
7915
7916         local CPAGES=3
7917         local list=$(comma_list $(osts_nodes))
7918
7919         # check whether obdfilter is cache capable at all
7920         if ! get_obdfilter_param $list '' read_cache_enable >/dev/null; then
7921                 echo "not cache-capable obdfilter"
7922                 return 0
7923         fi
7924
7925         # check cache is enabled on all obdfilters
7926         if get_obdfilter_param $list '' read_cache_enable | grep 0; then
7927                 echo "oss cache is disabled"
7928                 return 0
7929         fi
7930
7931         set_obdfilter_param $list '' writethrough_cache_enable 1
7932
7933         # pages should be in the case right after write
7934         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
7935         local BEFORE=`roc_hit`
7936         cancel_lru_locks osc
7937         cat $DIR/$tfile >/dev/null
7938         local AFTER=`roc_hit`
7939         if ! let "AFTER - BEFORE == CPAGES"; then
7940                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7941         fi
7942
7943         # the following read invalidates the cache
7944         cancel_lru_locks osc
7945         set_obdfilter_param $list '' read_cache_enable 0
7946         cat $DIR/$tfile >/dev/null
7947
7948         # now data shouldn't be found in the cache
7949         BEFORE=`roc_hit`
7950         cancel_lru_locks osc
7951         cat $DIR/$tfile >/dev/null
7952         AFTER=`roc_hit`
7953         if let "AFTER - BEFORE != 0"; then
7954                 error "IN CACHE: before: $BEFORE, after: $AFTER"
7955         fi
7956
7957         set_obdfilter_param $list '' read_cache_enable 1
7958         rm -f $DIR/$tfile
7959 }
7960 run_test 151 "test cache on oss and controls ==============================="
7961
7962 test_152() {
7963         local TF="$TMP/$tfile"
7964
7965         # simulate ENOMEM during write
7966 #define OBD_FAIL_OST_NOMEM      0x226
7967         lctl set_param fail_loc=0x80000226
7968         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7969         cp $TF $DIR/$tfile
7970         sync || error "sync failed"
7971         lctl set_param fail_loc=0
7972
7973         # discard client's cache
7974         cancel_lru_locks osc
7975
7976         # simulate ENOMEM during read
7977         lctl set_param fail_loc=0x80000226
7978         cmp $TF $DIR/$tfile || error "cmp failed"
7979         lctl set_param fail_loc=0
7980
7981         rm -f $TF
7982 }
7983 run_test 152 "test read/write with enomem ============================"
7984
7985 test_153() {
7986         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
7987 }
7988 run_test 153 "test if fdatasync does not crash ======================="
7989
7990 test_154() {
7991         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
7992                 { skip "Need MDS version at least 2.2.51"; return 0; }
7993
7994         cp /etc/hosts $DIR/$tfile
7995
7996         fid=$($LFS path2fid $DIR/$tfile)
7997         rc=$?
7998         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7999
8000         ffid=$DIR/.lustre/fid/$fid
8001
8002         echo "stat fid $fid"
8003         stat $ffid > /dev/null || error "stat $ffid failed."
8004         echo "touch fid $fid"
8005         touch $ffid || error "touch $ffid failed."
8006         echo "write to fid $fid"
8007         cat /etc/hosts > $ffid || error "write $ffid failed."
8008         echo "read fid $fid"
8009         diff /etc/hosts $ffid || error "read $ffid failed."
8010         echo "append write to fid $fid"
8011         cat /etc/hosts >> $ffid || error "append write $ffid failed."
8012         echo "rename fid $fid"
8013         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
8014         touch $DIR/$tfile.1
8015         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
8016         rm -f $DIR/$tfile.1
8017         echo "truncate fid $fid"
8018         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
8019         echo "link fid $fid"
8020         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
8021         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
8022                 echo "setfacl fid $fid"
8023                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
8024                 echo "getfacl fid $fid"
8025                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
8026         fi
8027         echo "unlink fid $fid"
8028         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
8029         echo "mknod fid $fid"
8030         mknod $ffid c 1 3 && error "mknod $ffid should fail."
8031
8032         fid=[0xf00000400:0x1:0x0]
8033         ffid=$DIR/.lustre/fid/$fid
8034
8035         echo "stat non-exist fid $fid"
8036         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
8037         echo "write to non-exist fid $fid"
8038         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
8039         echo "link new fid $fid"
8040         ln $DIR/$tfile $ffid && error "link $ffid should fail."
8041
8042         mkdir -p $DIR/$tdir
8043         touch $DIR/$tdir/$tfile
8044         fid=$($LFS path2fid $DIR/$tdir)
8045         rc=$?
8046         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
8047
8048         ffid=$DIR/.lustre/fid/$fid
8049
8050         echo "ls $fid"
8051         ls $ffid > /dev/null || error "ls $ffid failed."
8052         echo "touch $fid/$tfile.1"
8053         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
8054
8055         echo "touch $DIR/.lustre/fid/$tfile"
8056         touch $DIR/.lustre/fid/$tfile && \
8057                 error "touch $DIR/.lustre/fid/$tfile should fail."
8058
8059         echo "setxattr to $DIR/.lustre/fid"
8060         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
8061                 error "setxattr should fail."
8062
8063         echo "listxattr for $DIR/.lustre/fid"
8064         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
8065                 error "listxattr should fail."
8066
8067         echo "delxattr from $DIR/.lustre/fid"
8068         setfattr -x trusted.name1 $DIR/.lustre/fid &&
8069                 error "delxattr should fail."
8070
8071         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
8072         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
8073                 error "touch invalid fid should fail."
8074
8075         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
8076         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
8077                 error "touch non-normal fid should fail."
8078
8079         echo "rename $tdir to $DIR/.lustre/fid"
8080         mrename $DIR/$tdir $DIR/.lustre/fid &&
8081                 error "rename to $DIR/.lustre/fid should fail."
8082
8083         echo "rename .lustre to itself"
8084         fid=$($LFS path2fid $DIR)
8085         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
8086                 error "rename .lustre to itself should fail."
8087
8088         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
8089         fid=$($LFS path2fid $DIR/$tfile-2)
8090         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
8091         cp /etc/passwd $DIR/.lustre/fid/$fid &&
8092                 error "create lov data thru .lustre should fail."
8093         echo "cp /etc/passwd $DIR/$tfile-2"
8094         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
8095         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
8096         diff /etc/passwd $DIR/.lustre/fid/$fid ||
8097                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
8098
8099         echo "Open-by-FID succeeded"
8100 }
8101 run_test 154 "Open-by-FID"
8102
8103 test_155_small_load() {
8104     local temp=$TMP/$tfile
8105     local file=$DIR/$tfile
8106
8107     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
8108         error "dd of=$temp bs=6096 count=1 failed"
8109     cp $temp $file
8110     cancel_lru_locks osc
8111     cmp $temp $file || error "$temp $file differ"
8112
8113     $TRUNCATE $temp 6000
8114     $TRUNCATE $file 6000
8115     cmp $temp $file || error "$temp $file differ (truncate1)"
8116
8117     echo "12345" >>$temp
8118     echo "12345" >>$file
8119     cmp $temp $file || error "$temp $file differ (append1)"
8120
8121     echo "12345" >>$temp
8122     echo "12345" >>$file
8123     cmp $temp $file || error "$temp $file differ (append2)"
8124
8125     rm -f $temp $file
8126     true
8127 }
8128
8129 test_155_big_load() {
8130     remote_ost_nodsh && skip "remote OST with nodsh" && return
8131     local temp=$TMP/$tfile
8132     local file=$DIR/$tfile
8133
8134     free_min_max
8135     local cache_size=$(do_facet ost$((MAXI+1)) \
8136         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8137     local large_file_size=$((cache_size * 2))
8138
8139     echo "OSS cache size: $cache_size KB"
8140     echo "Large file size: $large_file_size KB"
8141
8142     [ $MAXV -le $large_file_size ] && \
8143         skip_env "max available OST size needs > $large_file_size KB" && \
8144         return 0
8145
8146     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8147
8148     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8149         error "dd of=$temp bs=$large_file_size count=1k failed"
8150     cp $temp $file
8151     ls -lh $temp $file
8152     cancel_lru_locks osc
8153     cmp $temp $file || error "$temp $file differ"
8154
8155     rm -f $temp $file
8156     true
8157 }
8158
8159 test_155a() {
8160     set_cache read on
8161     set_cache writethrough on
8162     test_155_small_load
8163 }
8164 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8165
8166 test_155b() {
8167     set_cache read on
8168     set_cache writethrough off
8169     test_155_small_load
8170 }
8171 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8172
8173 test_155c() {
8174     set_cache read off
8175     set_cache writethrough on
8176     test_155_small_load
8177 }
8178 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8179
8180 test_155d() {
8181     set_cache read off
8182     set_cache writethrough off
8183     test_155_small_load
8184 }
8185 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8186
8187 test_155e() {
8188     set_cache read on
8189     set_cache writethrough on
8190     test_155_big_load
8191 }
8192 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8193
8194 test_155f() {
8195     set_cache read on
8196     set_cache writethrough off
8197     test_155_big_load
8198 }
8199 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8200
8201 test_155g() {
8202     set_cache read off
8203     set_cache writethrough on
8204     test_155_big_load
8205 }
8206 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8207
8208 test_155h() {
8209     set_cache read off
8210     set_cache writethrough off
8211     test_155_big_load
8212 }
8213 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8214
8215 test_156() {
8216     local CPAGES=3
8217     local BEFORE
8218     local AFTER
8219     local file="$DIR/$tfile"
8220
8221         [ "$(facet_fstype ost1)" = "zfs" ] &&
8222                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
8223                 return
8224
8225     log "Turn on read and write cache"
8226     set_cache read on
8227     set_cache writethrough on
8228
8229     log "Write data and read it back."
8230     log "Read should be satisfied from the cache."
8231     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8232     BEFORE=`roc_hit`
8233     cancel_lru_locks osc
8234     cat $file >/dev/null
8235     AFTER=`roc_hit`
8236     if ! let "AFTER - BEFORE == CPAGES"; then
8237         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8238     else
8239         log "cache hits:: before: $BEFORE, after: $AFTER"
8240     fi
8241
8242     log "Read again; it should be satisfied from the cache."
8243     BEFORE=$AFTER
8244     cancel_lru_locks osc
8245     cat $file >/dev/null
8246     AFTER=`roc_hit`
8247     if ! let "AFTER - BEFORE == CPAGES"; then
8248         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8249     else
8250         log "cache hits:: before: $BEFORE, after: $AFTER"
8251     fi
8252
8253
8254     log "Turn off the read cache and turn on the write cache"
8255     set_cache read off
8256     set_cache writethrough on
8257
8258     log "Read again; it should be satisfied from the cache."
8259     BEFORE=`roc_hit`
8260     cancel_lru_locks osc
8261     cat $file >/dev/null
8262     AFTER=`roc_hit`
8263     if ! let "AFTER - BEFORE == CPAGES"; then
8264         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8265     else
8266         log "cache hits:: before: $BEFORE, after: $AFTER"
8267     fi
8268
8269     log "Read again; it should not be satisfied from the cache."
8270     BEFORE=$AFTER
8271     cancel_lru_locks osc
8272     cat $file >/dev/null
8273     AFTER=`roc_hit`
8274     if ! let "AFTER - BEFORE == 0"; then
8275         error "IN CACHE: before: $BEFORE, after: $AFTER"
8276     else
8277         log "cache hits:: before: $BEFORE, after: $AFTER"
8278     fi
8279
8280     log "Write data and read it back."
8281     log "Read should be satisfied from the cache."
8282     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8283     BEFORE=`roc_hit`
8284     cancel_lru_locks osc
8285     cat $file >/dev/null
8286     AFTER=`roc_hit`
8287     if ! let "AFTER - BEFORE == CPAGES"; then
8288         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8289     else
8290         log "cache hits:: before: $BEFORE, after: $AFTER"
8291     fi
8292
8293     log "Read again; it should not be satisfied from the cache."
8294     BEFORE=$AFTER
8295     cancel_lru_locks osc
8296     cat $file >/dev/null
8297     AFTER=`roc_hit`
8298     if ! let "AFTER - BEFORE == 0"; then
8299         error "IN CACHE: before: $BEFORE, after: $AFTER"
8300     else
8301         log "cache hits:: before: $BEFORE, after: $AFTER"
8302     fi
8303
8304
8305     log "Turn off read and write cache"
8306     set_cache read off
8307     set_cache writethrough off
8308
8309     log "Write data and read it back"
8310     log "It should not be satisfied from the cache."
8311     rm -f $file
8312     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8313     cancel_lru_locks osc
8314     BEFORE=`roc_hit`
8315     cat $file >/dev/null
8316     AFTER=`roc_hit`
8317     if ! let "AFTER - BEFORE == 0"; then
8318         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8319     else
8320         log "cache hits:: before: $BEFORE, after: $AFTER"
8321     fi
8322
8323
8324     log "Turn on the read cache and turn off the write cache"
8325     set_cache read on
8326     set_cache writethrough off
8327
8328     log "Write data and read it back"
8329     log "It should not be satisfied from the cache."
8330     rm -f $file
8331     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8332     BEFORE=`roc_hit`
8333     cancel_lru_locks osc
8334     cat $file >/dev/null
8335     AFTER=`roc_hit`
8336     if ! let "AFTER - BEFORE == 0"; then
8337         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8338     else
8339         log "cache hits:: before: $BEFORE, after: $AFTER"
8340     fi
8341
8342     log "Read again; it should be satisfied from the cache."
8343     BEFORE=`roc_hit`
8344     cancel_lru_locks osc
8345     cat $file >/dev/null
8346     AFTER=`roc_hit`
8347     if ! let "AFTER - BEFORE == CPAGES"; then
8348         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8349     else
8350         log "cache hits:: before: $BEFORE, after: $AFTER"
8351     fi
8352
8353     rm -f $file
8354 }
8355 run_test 156 "Verification of tunables ============================"
8356
8357 #Changelogs
8358 err17935 () {
8359     if [ $MDSCOUNT -gt 1 ]; then
8360         error_ignore 17935 $*
8361     else
8362         error $*
8363     fi
8364 }
8365
8366 changelog_chmask()
8367 {
8368     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8369            grep -c $1)
8370
8371     if [ $MASK -eq 1 ]; then
8372         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8373     else
8374         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8375     fi
8376 }
8377
8378 test_160() {
8379     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8380     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8381         { skip "Need MDS version at least 2.2.0"; return; }
8382     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8383     echo "Registered as changelog user $USER"
8384     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8385         grep -q $USER || error "User $USER not found in changelog_users"
8386
8387     # change something
8388     mkdir -p $DIR/$tdir/pics/2008/zachy
8389     touch $DIR/$tdir/pics/2008/zachy/timestamp
8390     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8391     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8392     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8393     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8394     rm $DIR/$tdir/pics/desktop.jpg
8395
8396     $LFS changelog $MDT0 | tail -5
8397
8398     echo "verifying changelog mask"
8399     changelog_chmask "MKDIR"
8400     changelog_chmask "CLOSE"
8401
8402     mkdir -p $DIR/$tdir/pics/zach/sofia
8403     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8404
8405     changelog_chmask "MKDIR"
8406     changelog_chmask "CLOSE"
8407
8408     mkdir -p $DIR/$tdir/pics/2008/sofia
8409     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8410
8411     $LFS changelog $MDT0
8412     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8413     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8414     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8415     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8416
8417     # verify contents
8418     echo "verifying target fid"
8419     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8420         tail -1 | awk '{print $6}')
8421     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8422     [ "$fidc" == "t=$fidf" ] || \
8423         err17935 "fid in changelog $fidc != file fid $fidf"
8424     echo "verifying parent fid"
8425     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8426         tail -1 | awk '{print $7}')
8427     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8428     [ "$fidc" == "p=$fidf" ] || \
8429         err17935 "pfid in changelog $fidc != dir fid $fidf"
8430
8431     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8432         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8433     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8434     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8435         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8436     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8437     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8438         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8439
8440     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8441         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8442     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8443     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8444     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8445         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8446
8447     echo "verifying user deregister"
8448     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8449     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8450         grep -q $USER && error "User $USER still found in changelog_users"
8451
8452     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8453         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8454     if [ $USERS -eq 0 ]; then
8455         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8456             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8457         touch $DIR/$tdir/chloe
8458         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8459             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8460         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8461         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8462     else
8463         echo "$USERS other changelog users; can't verify off"
8464     fi
8465 }
8466 run_test 160 "changelog sanity"
8467
8468 test_161() {
8469     mkdir -p $DIR/$tdir
8470     cp /etc/hosts $DIR/$tdir/$tfile
8471     mkdir $DIR/$tdir/foo1
8472     mkdir $DIR/$tdir/foo2
8473     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8474     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8475     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8476     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8477     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8478     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8479         $LFS fid2path $DIR $FID
8480         err17935 "bad link ea"
8481     fi
8482     # middle
8483     rm $DIR/$tdir/foo2/zachary
8484     # last
8485     rm $DIR/$tdir/foo2/thor
8486     # first
8487     rm $DIR/$tdir/$tfile
8488     # rename
8489     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8490     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8491         then
8492         $LFS fid2path $DIR $FID
8493         err17935 "bad link rename"
8494     fi
8495     rm $DIR/$tdir/foo2/maggie
8496
8497     # overflow the EA
8498     local longname=filename_avg_len_is_thirty_two_
8499     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8500         error "failed to hardlink many files"
8501     links=$($LFS fid2path $DIR $FID | wc -l)
8502     echo -n "${links}/1000 links in link EA"
8503     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8504     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8505         error "failed to unlink many hardlinks"
8506 }
8507 run_test 161 "link ea sanity"
8508
8509 check_path() {
8510     local expected=$1
8511     shift
8512     local fid=$2
8513
8514     local path=$(${LFS} fid2path $*)
8515     RC=$?
8516
8517     if [ $RC -ne 0 ]; then
8518         err17935 "path looked up of $expected failed. Error $RC"
8519         return $RC
8520     elif [ "${path}" != "${expected}" ]; then
8521         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8522         return 2
8523     fi
8524     echo "fid $fid resolves to path $path (expected $expected)"
8525 }
8526
8527 test_162() {
8528         # Make changes to filesystem
8529         mkdir -p $DIR/$tdir/d2
8530         touch $DIR/$tdir/d2/$tfile
8531         touch $DIR/$tdir/d2/x1
8532         touch $DIR/$tdir/d2/x2
8533         mkdir -p $DIR/$tdir/d2/a/b/c
8534         mkdir -p $DIR/$tdir/d2/p/q/r
8535         # regular file
8536         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8537         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8538
8539         # softlink
8540         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8541         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8542         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8543
8544         # softlink to wrong file
8545         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8546         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8547         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8548
8549         # hardlink
8550         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8551         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8552         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8553         # fid2path dir/fsname should both work
8554         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8555         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8556
8557         # hardlink count: check that there are 2 links
8558         # Doesnt work with CMD yet: 17935
8559         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8560                 err17935 "expected 2 links"
8561
8562         # hardlink indexing: remove the first link
8563         rm $DIR/$tdir/d2/p/q/r/hlink
8564         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8565
8566         return 0
8567 }
8568 run_test 162 "path lookup sanity"
8569
8570 test_163() {
8571         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8572         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
8573         copytool $FSNAME &
8574         sleep 1
8575         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
8576         # this proc file is temporary and linux-only
8577         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
8578          error "kernel->userspace send failed"
8579         kill -INT $!
8580 }
8581 run_test 163 "kernel <-> userspace comms"
8582
8583 test_169() {
8584         # do directio so as not to populate the page cache
8585         log "creating a 10 Mb file"
8586         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8587         log "starting reads"
8588         dd if=$DIR/$tfile of=/dev/null bs=4096 &
8589         log "truncating the file"
8590         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8591         log "killing dd"
8592         kill %+ || true # reads might have finished
8593         echo "wait until dd is finished"
8594         wait
8595         log "removing the temporary file"
8596         rm -rf $DIR/$tfile || error "tmp file removal failed"
8597 }
8598 run_test 169 "parallel read and truncate should not deadlock"
8599
8600 test_170() {
8601         $LCTL clear     # bug 18514
8602         $LCTL debug_daemon start $TMP/${tfile}_log_good
8603         touch $DIR/$tfile
8604         $LCTL debug_daemon stop
8605         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
8606                error "sed failed to read log_good"
8607
8608         $LCTL debug_daemon start $TMP/${tfile}_log_good
8609         rm -rf $DIR/$tfile
8610         $LCTL debug_daemon stop
8611
8612         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
8613                error "lctl df log_bad failed"
8614
8615         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8616         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8617
8618         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
8619         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
8620
8621         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
8622                 error "bad_line good_line1 good_line2 are empty"
8623
8624         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8625         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
8626         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8627
8628         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
8629         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8630         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8631
8632         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
8633                 error "bad_line_new good_line_new are empty"
8634
8635         local expected_good=$((good_line1 + good_line2*2))
8636
8637         rm -f $TMP/${tfile}*
8638         # LU-231, short malformed line may not be counted into bad lines
8639         if [ $bad_line -ne $bad_line_new ] &&
8640                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
8641                 error "expected $bad_line bad lines, but got $bad_line_new"
8642                 return 1
8643         fi
8644
8645         if [ $expected_good -ne $good_line_new ]; then
8646                 error "expected $expected_good good lines, but got $good_line_new"
8647                 return 2
8648         fi
8649         true
8650 }
8651 run_test 170 "test lctl df to handle corrupted log ====================="
8652
8653 test_171() { # bug20592
8654 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8655         $LCTL set_param fail_loc=0x50e
8656         $LCTL set_param fail_val=3000
8657         multiop_bg_pause $DIR/$tfile O_s || true
8658         local MULTIPID=$!
8659         kill -USR1 $MULTIPID
8660         # cause log dump
8661         sleep 3
8662         wait $MULTIPID
8663         if dmesg | grep "recursive fault"; then
8664                 error "caught a recursive fault"
8665         fi
8666         $LCTL set_param fail_loc=0
8667         true
8668 }
8669 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
8670
8671 # it would be good to share it with obdfilter-survey/libecho code
8672 setup_obdecho_osc () {
8673         local rc=0
8674         local ost_nid=$1
8675         local obdfilter_name=$2
8676         echo "Creating new osc for $obdfilter_name on $ost_nid"
8677         # make sure we can find loopback nid
8678         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
8679
8680         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
8681                            ${obdfilter_name}_osc_UUID || rc=2; }
8682         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
8683                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
8684         return $rc
8685 }
8686
8687 cleanup_obdecho_osc () {
8688         local obdfilter_name=$1
8689         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
8690         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
8691         return 0
8692 }
8693
8694 obdecho_create_test() {
8695         local OBD=$1
8696         local node=$2
8697         local rc=0
8698         local id
8699         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
8700         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
8701                            rc=2; }
8702         if [ $rc -eq 0 ]; then
8703             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
8704             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
8705         fi
8706         echo "New object id is $id"
8707         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
8708                            rc=4; }
8709         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
8710                                         "cleanup" || rc=5; }
8711         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
8712                                         "detach" || rc=6; }
8713         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
8714         return $rc
8715 }
8716
8717 test_180a() {
8718         remote_ost_nodsh && skip "remote OST with nodsh" && return
8719         local rc=0
8720         local rmmod_local=0
8721
8722         if ! module_loaded obdecho; then
8723             load_module obdecho/obdecho
8724             rmmod_local=1
8725         fi
8726
8727         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
8728         local host=$(lctl get_param -n osc.$osc.import |
8729                              awk '/current_connection:/ {print $2}' )
8730         local target=$(lctl get_param -n osc.$osc.import |
8731                              awk '/target:/ {print $2}' )
8732         target=${target%_UUID}
8733
8734         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
8735         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
8736         [[ -n $target ]] && cleanup_obdecho_osc $target
8737         [ $rmmod_local -eq 1 ] && rmmod obdecho
8738         return $rc
8739 }
8740 run_test 180a "test obdecho on osc"
8741
8742 test_180b() {
8743         remote_ost_nodsh && skip "remote OST with nodsh" && return
8744         local rc=0
8745         local rmmod_remote=0
8746
8747         do_facet ost1 "lsmod | grep -q obdecho || "                      \
8748                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
8749                       "modprobe obdecho; }" && rmmod_remote=1
8750         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
8751         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
8752         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
8753         return $rc
8754 }
8755 run_test 180b "test obdecho directly on obdfilter"
8756
8757 test_181() { # bug 22177
8758         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8759         # create enough files to index the directory
8760         createmany -o $DIR/$tdir/foobar 4000
8761         # print attributes for debug purpose
8762         lsattr -d .
8763         # open dir
8764         multiop_bg_pause $DIR/$tdir D_Sc || return 1
8765         MULTIPID=$!
8766         # remove the files & current working dir
8767         unlinkmany $DIR/$tdir/foobar 4000
8768         rmdir $DIR/$tdir
8769         kill -USR1 $MULTIPID
8770         wait $MULTIPID
8771         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
8772         return 0
8773 }
8774 run_test 181 "Test open-unlinked dir ========================"
8775
8776 test_182() {
8777         # disable MDC RPC lock wouldn't crash client
8778         local fcount=1000
8779         local tcount=4
8780
8781         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8782 #define OBD_FAIL_MDC_RPCS_SEM           0x804
8783         $LCTL set_param fail_loc=0x804
8784
8785         for (( i=0; i < $tcount; i++ )) ; do
8786                 mkdir $DIR/$tdir/$i
8787                 createmany -o $DIR/$tdir/$i/f- $fcount &
8788         done
8789         wait
8790
8791         for (( i=0; i < $tcount; i++ )) ; do
8792                 unlinkmany $DIR/$tdir/$i/f- $fcount &
8793         done
8794         wait
8795
8796         rm -rf $DIR/$tdir
8797
8798         $LCTL set_param fail_loc=0
8799 }
8800 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
8801
8802 test_183() { # LU-2275
8803         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8804         echo aaa > $DIR/$tdir/$tfile
8805
8806 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
8807         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
8808
8809         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
8810         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
8811
8812         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
8813
8814         # Flush negative dentry cache
8815         touch $DIR/$tdir/$tfile
8816
8817         # We are not checking for any leaked references here, they'll
8818         # become evident next time we do cleanup with module unload.
8819         rm -rf $DIR/$tdir
8820 }
8821 run_test 183 "No crash or request leak in case of strange dispositions ========"
8822
8823 # OST pools tests
8824 check_file_in_pool()
8825 {
8826         local file=$1
8827         local pool=$2
8828         local tlist="$3"
8829         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
8830         for i in $res
8831         do
8832                 for t in $tlist ; do
8833                         [ "$i" -eq "$t" ] && continue 2
8834                 done
8835
8836                 echo "pool list: $tlist"
8837                 echo "striping: $res"
8838                 error_noexit "$file not allocated in $pool"
8839                 return 1
8840         done
8841         return 0
8842 }
8843
8844 pool_add() {
8845         echo "Creating new pool"
8846         local pool=$1
8847
8848         create_pool $FSNAME.$pool ||
8849                 { error_noexit "No pool created, result code $?"; return 1; }
8850         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
8851                 { error_noexit "$pool not in lfs pool_list"; return 2; }
8852 }
8853
8854 pool_add_targets() {
8855         echo "Adding targets to pool"
8856         local pool=$1
8857         local first=$2
8858         local last=$3
8859         local step=${4:-1}
8860
8861         local list=$(seq $first $step $last)
8862
8863         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
8864         do_facet mgs $LCTL pool_add \
8865                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
8866         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
8867                         | sort -u | tr '\n' ' ' " "$t" || { 
8868                 error_noexit "Add to pool failed"
8869                 return 1
8870         }
8871         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
8872         local addcount=$(((last - first) / step + 1))
8873         [ $lfscount -eq $addcount ] || {
8874                 error_noexit "lfs pool_list bad ost count" \
8875                                                 "$lfscount != $addcount"
8876                 return 2
8877         }
8878 }
8879
8880 pool_set_dir() {
8881         local pool=$1
8882         local tdir=$2
8883         echo "Setting pool on directory $tdir"
8884
8885         $SETSTRIPE -c 2 -p $pool $tdir && return 0
8886
8887         error_noexit "Cannot set pool $pool to $tdir"
8888         return 1
8889 }
8890
8891 pool_check_dir() {
8892         local pool=$1
8893         local tdir=$2
8894         echo "Checking pool on directory $tdir"
8895
8896         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
8897         [ "$res" = "$pool" ] && return 0
8898
8899         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
8900         return 1
8901 }
8902
8903 pool_dir_rel_path() {
8904         echo "Testing relative path works well"
8905         local pool=$1
8906         local tdir=$2
8907         local root=$3
8908
8909         mkdir -p $root/$tdir/$tdir
8910         cd $root/$tdir
8911         pool_set_dir $pool $tdir          || return 1
8912         pool_set_dir $pool ./$tdir        || return 2
8913         pool_set_dir $pool ../$tdir       || return 3
8914         pool_set_dir $pool ../$tdir/$tdir || return 4
8915         rm -rf $tdir; cd - > /dev/null
8916 }
8917
8918 pool_alloc_files() {
8919         echo "Checking files allocation from directory pool"
8920         local pool=$1
8921         local tdir=$2
8922         local count=$3
8923         local tlist="$4"
8924
8925         local failed=0
8926         for i in $(seq -w 1 $count)
8927         do
8928                 local file=$tdir/file-$i
8929                 touch $file
8930                 check_file_in_pool $file $pool "$tlist" || \
8931                         failed=$((failed + 1))
8932         done
8933         [ "$failed" = 0 ] && return 0
8934
8935         error_noexit "$failed files not allocated in $pool"
8936         return 1
8937 }
8938
8939 pool_create_files() {
8940         echo "Creating files in pool"
8941         local pool=$1
8942         local tdir=$2
8943         local count=$3
8944         local tlist="$4"
8945
8946         mkdir -p $tdir
8947         local failed=0
8948         for i in $(seq -w 1 $count)
8949         do
8950                 local file=$tdir/spoo-$i
8951                 $SETSTRIPE -p $pool $file
8952                 check_file_in_pool $file $pool "$tlist" || \
8953                         failed=$((failed + 1))
8954         done
8955         [ "$failed" = 0 ] && return 0
8956
8957         error_noexit "$failed files not allocated in $pool"
8958         return 1
8959 }
8960
8961 pool_lfs_df() {
8962         echo "Checking 'lfs df' output"
8963         local pool=$1
8964
8965         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
8966                         tr '\n' ' ')
8967         local res=$($LFS df --pool $FSNAME.$pool |
8968                         awk '{print $1}' |
8969                         grep "$FSNAME-OST" |
8970                         tr '\n' ' ')
8971         [ "$res" = "$t" ] && return 0
8972
8973         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
8974         return 1
8975 }
8976
8977 pool_file_rel_path() {
8978         echo "Creating files in a pool with relative pathname"
8979         local pool=$1
8980         local tdir=$2
8981
8982         mkdir -p $tdir ||
8983                 { error_noexit "unable to create $tdir"; return 1 ; }
8984         local file="/..$tdir/$tfile-1"
8985         $SETSTRIPE -p $pool $file ||
8986                 { error_noexit "unable to create $file" ; return 2 ; }
8987
8988         cd $tdir
8989         $SETSTRIPE -p $pool $tfile-2 || {
8990                 error_noexit "unable to create $tfile-2 in $tdir"
8991                 return 3
8992         }
8993 }
8994
8995 pool_remove_first_target() {
8996         echo "Removing first target from a pool"
8997         local pool=$1
8998
8999         local pname="lov.$FSNAME-*.pools.$pool"
9000         local t=$($LCTL get_param -n $pname | head -1)
9001         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9002         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
9003                 error_noexit "$t not removed from $FSNAME.$pool"
9004                 return 1
9005         }
9006 }
9007
9008 pool_remove_all_targets() {
9009         echo "Removing all targets from pool"
9010         local pool=$1
9011         local file=$2
9012         local pname="lov.$FSNAME-*.pools.$pool"
9013         for t in $($LCTL get_param -n $pname | sort -u)
9014         do
9015                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
9016         done
9017         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
9018                 error_noexit "Pool $FSNAME.$pool cannot be drained"
9019                 return 1
9020         }
9021         # striping on an empty/nonexistant pool should fall back 
9022         # to "pool of everything"
9023         touch $file || {
9024                 error_noexit "failed to use fallback striping for empty pool"
9025                 return 2
9026         }
9027         # setstripe on an empty pool should fail
9028         $SETSTRIPE -p $pool $file 2>/dev/null && {
9029                 error_noexit "expected failure when creating file" \
9030                                                         "with empty pool"
9031                 return 3
9032         }
9033         return 0
9034 }
9035
9036 pool_remove() {
9037         echo "Destroying pool"
9038         local pool=$1
9039         local file=$2
9040
9041         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
9042
9043         sleep 2
9044         # striping on an empty/nonexistant pool should fall back 
9045         # to "pool of everything"
9046         touch $file || {
9047                 error_noexit "failed to use fallback striping for missing pool"
9048                 return 1
9049         }
9050         # setstripe on an empty pool should fail
9051         $SETSTRIPE -p $pool $file 2>/dev/null && {
9052                 error_noexit "expected failure when creating file" \
9053                                                         "with missing pool"
9054                 return 2
9055         }
9056
9057         # get param should return err once pool is gone
9058         if wait_update $HOSTNAME "lctl get_param -n \
9059                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
9060         then
9061                 remove_pool_from_list $FSNAME.$pool
9062                 return 0
9063         fi
9064         error_noexit "Pool $FSNAME.$pool is not destroyed"
9065         return 3
9066 }
9067
9068 test_200() {
9069         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
9070
9071         local POOL=${POOL:-cea1}
9072         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
9073         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
9074         # Pool OST targets
9075         local first_ost=0
9076         local last_ost=$(($OSTCOUNT - 1))
9077         local ost_step=2
9078         local ost_list=$(seq $first_ost $ost_step $last_ost)
9079         local ost_range="$first_ost $last_ost $ost_step"
9080         local test_path=$POOL_ROOT/$POOL_DIR_NAME
9081         local file_dir=$POOL_ROOT/file_tst
9082
9083         local rc=0
9084         while : ; do
9085                 # former test_200a test_200b
9086                 pool_add $POOL                          || { rc=$? ; break; }
9087                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
9088                 # former test_200c test_200d
9089                 mkdir -p $test_path
9090                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
9091                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
9092                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
9093                                                         || { rc=$? ; break; }
9094                 # former test_200e test_200f
9095                 local files=$((OSTCOUNT*3))
9096                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
9097                                                         || { rc=$? ; break; }
9098                 pool_create_files $POOL $file_dir $files "$ost_list" \
9099                                                         || { rc=$? ; break; }
9100                 # former test_200g test_200h
9101                 pool_lfs_df $POOL                       || { rc=$? ; break; }
9102                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
9103
9104                 # former test_201a test_201b test_201c
9105                 pool_remove_first_target $POOL          || { rc=$? ; break; }
9106
9107                 local f=$test_path/$tfile
9108                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
9109                 pool_remove $POOL $f                    || { rc=$? ; break; }
9110                 break
9111         done
9112
9113         cleanup_pools
9114         return $rc
9115 }
9116 run_test 200 "OST pools"
9117
9118 # usage: default_attr <count | size | offset>
9119 default_attr() {
9120         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
9121 }
9122
9123 # usage: check_default_stripe_attr
9124 check_default_stripe_attr() {
9125         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
9126         case $1 in
9127         --stripe-count|--count)
9128                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
9129         --stripe-size|--size)
9130                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
9131         --stripe-index|--index)
9132                 EXPECTED=-1;;
9133         *)
9134                 error "unknown getstripe attr '$1'"
9135         esac
9136
9137         [ $ACTUAL != $EXPECTED ] &&
9138                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
9139 }
9140
9141 test_204a() {
9142         mkdir -p $DIR/$tdir
9143         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
9144
9145         check_default_stripe_attr --stripe-count
9146         check_default_stripe_attr --stripe-size
9147         check_default_stripe_attr --stripe-index
9148
9149         return 0
9150 }
9151 run_test 204a "Print default stripe attributes ================="
9152
9153 test_204b() {
9154         mkdir -p $DIR/$tdir
9155         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9156
9157         check_default_stripe_attr --stripe-size
9158         check_default_stripe_attr --stripe-index
9159
9160         return 0
9161 }
9162 run_test 204b "Print default stripe size and offset  ==========="
9163
9164 test_204c() {
9165         mkdir -p $DIR/$tdir
9166         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9167
9168         check_default_stripe_attr --stripe-count
9169         check_default_stripe_attr --stripe-index
9170
9171         return 0
9172 }
9173 run_test 204c "Print default stripe count and offset ==========="
9174
9175 test_204d() {
9176         mkdir -p $DIR/$tdir
9177         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9178
9179         check_default_stripe_attr --stripe-count
9180         check_default_stripe_attr --stripe-size
9181
9182         return 0
9183 }
9184 run_test 204d "Print default stripe count and size ============="
9185
9186 test_204e() {
9187         mkdir -p $DIR/$tdir
9188         $SETSTRIPE -d $DIR/$tdir
9189
9190         check_default_stripe_attr --stripe-count --raw
9191         check_default_stripe_attr --stripe-size --raw
9192         check_default_stripe_attr --stripe-index --raw
9193
9194         return 0
9195 }
9196 run_test 204e "Print raw stripe attributes ================="
9197
9198 test_204f() {
9199         mkdir -p $DIR/$tdir
9200         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9201
9202         check_default_stripe_attr --stripe-size --raw
9203         check_default_stripe_attr --stripe-index --raw
9204
9205         return 0
9206 }
9207 run_test 204f "Print raw stripe size and offset  ==========="
9208
9209 test_204g() {
9210         mkdir -p $DIR/$tdir
9211         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9212
9213         check_default_stripe_attr --stripe-count --raw
9214         check_default_stripe_attr --stripe-index --raw
9215
9216         return 0
9217 }
9218 run_test 204g "Print raw stripe count and offset ==========="
9219
9220 test_204h() {
9221         mkdir -p $DIR/$tdir
9222         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9223
9224         check_default_stripe_attr --stripe-count --raw
9225         check_default_stripe_attr --stripe-size --raw
9226
9227         return 0
9228 }
9229 run_test 204h "Print raw stripe count and size ============="
9230
9231 # Figure out which job scheduler is being used, if any,
9232 # or use a fake one
9233 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9234         JOBENV=SLURM_JOB_ID
9235 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9236         JOBENV=LSB_JOBID
9237 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9238         JOBENV=PBS_JOBID
9239 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9240         JOBENV=LOADL_STEP_ID
9241 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9242         JOBENV=JOB_ID
9243 else
9244         JOBENV=FAKE_JOBID
9245 fi
9246
9247 verify_jobstats() {
9248         local cmd=$1
9249         local target=$2
9250
9251         # clear old jobstats
9252         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9253         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
9254
9255         # use a new JobID for this test, or we might see an old one
9256         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9257
9258         JOBVAL=${!JOBENV}
9259         log "Test: $cmd"
9260         log "Using JobID environment variable $JOBENV=$JOBVAL"
9261
9262         if [ $JOBENV = "FAKE_JOBID" ]; then
9263                 FAKE_JOBID=$JOBVAL $cmd
9264         else
9265                 $cmd
9266         fi
9267
9268         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9269                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9270                 do_facet $FACET lctl get_param mdt.*.job_stats |
9271                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9272         fi
9273         if [ "$target" = "ost" -o "$target" = "both" ]; then
9274                 FACET=ost1
9275                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9276                         grep $JOBVAL || error "No job stats found on OST $FACET"
9277         fi
9278 }
9279
9280 test_205() { # Job stats
9281         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9282                 skip "Server doesn't support jobstats" && return 0
9283
9284         local cmd
9285         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9286         if [ $OLD_JOBENV != $JOBENV ]; then
9287                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9288                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9289                         $JOBENV || return 1
9290         fi
9291
9292         # mkdir
9293         cmd="mkdir $DIR/$tfile"
9294         verify_jobstats "$cmd" "mdt"
9295         # rmdir
9296         cmd="rm -fr $DIR/$tfile"
9297         verify_jobstats "$cmd" "mdt"
9298         # mknod
9299         cmd="mknod $DIR/$tfile c 1 3"
9300         verify_jobstats "$cmd" "mdt"
9301         # unlink
9302         cmd="rm -f $DIR/$tfile"
9303         verify_jobstats "$cmd" "mdt"
9304         # open & close
9305         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9306         verify_jobstats "$cmd" "mdt"
9307         # setattr
9308         cmd="touch $DIR/$tfile"
9309         verify_jobstats "$cmd" "both"
9310         # write
9311         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9312         verify_jobstats "$cmd" "ost"
9313         # read
9314         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9315         verify_jobstats "$cmd" "ost"
9316         # truncate
9317         cmd="$TRUNCATE $DIR/$tfile 0"
9318         verify_jobstats "$cmd" "both"
9319         # rename
9320         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9321         verify_jobstats "$cmd" "mdt"
9322
9323         # cleanup
9324         rm -f $DIR/jobstats_test_rename
9325
9326         if [ $OLD_JOBENV != $JOBENV ]; then
9327                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9328                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9329                         $OLD_JOBENV || return 1
9330         fi
9331 }
9332 run_test 205 "Verify job stats"
9333
9334 # LU-1480, LU-1773 and LU-1657
9335 test_206() {
9336         mkdir -p $DIR/$tdir
9337         lfs setstripe -c -1 $DIR/$tdir
9338 #define OBD_FAIL_LOV_INIT 0x1403
9339         $LCTL set_param fail_loc=0xa0001403
9340         $LCTL set_param fail_val=1
9341         touch $DIR/$tdir/$tfile || true
9342 }
9343 run_test 206 "fail lov_init_raid0() doesn't lbug"
9344
9345 test_212() {
9346         size=`date +%s`
9347         size=$((size % 8192 + 1))
9348         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9349         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9350         rm -f $DIR/f212 $DIR/f212.xyz
9351 }
9352 run_test 212 "Sendfile test ============================================"
9353
9354 test_213() {
9355         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9356         cancel_lru_locks osc
9357         lctl set_param fail_loc=0x8000040f
9358         # generate a read lock
9359         cat $DIR/$tfile > /dev/null
9360         # write to the file, it will try to cancel the above read lock.
9361         cat /etc/hosts >> $DIR/$tfile
9362 }
9363 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9364
9365 test_214() { # for bug 20133
9366         mkdir -p $DIR/d214p/d214c
9367         for (( i=0; i < 340; i++ )) ; do
9368                 touch $DIR/d214p/d214c/a$i
9369         done
9370
9371         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9372         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
9373         ls $DIR/d214c || error "ls $DIR/d214c failed"
9374         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
9375 }
9376 run_test 214 "hash-indexed directory test - bug 20133"
9377
9378 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
9379 create_lnet_proc_files() {
9380         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
9381         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
9382
9383         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
9384         rm -f "$TMP/lnet_$1.sys_tmp"
9385 }
9386
9387 # counterpart of create_lnet_proc_files
9388 remove_lnet_proc_files() {
9389         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
9390 }
9391
9392 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9393 # 3rd arg as regexp for body
9394 check_lnet_proc_stats() {
9395         local l=$(cat "$TMP/lnet_$1" |wc -l)
9396         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
9397
9398         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
9399 }
9400
9401 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9402 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
9403 # optional and can be regexp for 2nd line (lnet.routes case)
9404 check_lnet_proc_entry() {
9405         local blp=2            # blp stands for 'position of 1st line of body'
9406         [ "$5" = "" ] || blp=3 # lnet.routes case
9407
9408         local l=$(cat "$TMP/lnet_$1" |wc -l)
9409         # subtracting one from $blp because the body can be empty
9410         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
9411
9412         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
9413                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
9414
9415         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
9416                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
9417
9418         # bail out if any unexpected line happened
9419         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
9420         [ "$?" != 0 ] || error "$2 misformatted"
9421 }
9422
9423 test_215() { # for bugs 18102, 21079, 21517
9424         local N='(0|[1-9][0-9]*)'       # non-negative numeric
9425         local P='[1-9][0-9]*'           # positive numeric
9426         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
9427         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
9428         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
9429         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
9430
9431         local L1 # regexp for 1st line
9432         local L2 # regexp for 2nd line (optional)
9433         local BR # regexp for the rest (body)
9434
9435         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
9436         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
9437         create_lnet_proc_files "stats"
9438         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
9439         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
9440         remove_lnet_proc_files "stats"
9441
9442         # /proc/sys/lnet/routes should look like this:
9443         # Routing disabled/enabled
9444         # net hops state router
9445         # where net is a string like tcp0, hops >= 0, state is up/down,
9446         # router is a string like 192.168.1.1@tcp2
9447         L1="^Routing (disabled|enabled)$"
9448         L2="^net +hops +state +router$"
9449         BR="^$NET +$N +(up|down) +$NID$"
9450         create_lnet_proc_files "routes"
9451         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
9452         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
9453         remove_lnet_proc_files "routes"
9454
9455         # /proc/sys/lnet/routers should look like this:
9456         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
9457         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
9458         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
9459         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
9460         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
9461         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
9462         create_lnet_proc_files "routers"
9463         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
9464         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
9465         remove_lnet_proc_files "routers"
9466
9467         # /proc/sys/lnet/peers should look like this:
9468         # nid refs state last max rtr min tx min queue
9469         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
9470         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
9471         # numeric (0 or >0 or <0), queue >= 0.
9472         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
9473         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
9474         create_lnet_proc_files "peers"
9475         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
9476         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
9477         remove_lnet_proc_files "peers"
9478
9479         # /proc/sys/lnet/buffers  should look like this:
9480         # pages count credits min
9481         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
9482         L1="^pages +count +credits +min$"
9483         BR="^ +$N +$N +$I +$I$"
9484         create_lnet_proc_files "buffers"
9485         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
9486         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
9487         remove_lnet_proc_files "buffers"
9488
9489         # /proc/sys/lnet/nis should look like this:
9490         # nid status alive refs peer rtr max tx min
9491         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
9492         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
9493         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
9494         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
9495         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
9496         create_lnet_proc_files "nis"
9497         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
9498         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
9499         remove_lnet_proc_files "nis"
9500
9501         # can we successfully write to /proc/sys/lnet/stats?
9502         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
9503         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
9504 }
9505 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
9506
9507 test_216() { # bug 20317
9508         remote_ost_nodsh && skip "remote OST with nodsh" && return
9509         local node
9510         local p="$TMP/sanityN-$TESTNAME.parameters"
9511         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
9512         for node in $(osts_nodes); do
9513                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
9514                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
9515                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
9516         done
9517         clear_osc_stats
9518
9519         # agressive lockless i/o settings
9520         for node in $(osts_nodes); do
9521                 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'
9522         done
9523         lctl set_param -n osc.*.contention_seconds 60
9524
9525         $DIRECTIO write $DIR/$tfile 0 10 4096
9526         $CHECKSTAT -s 40960 $DIR/$tfile
9527
9528         # disable lockless i/o
9529         for node in $(osts_nodes); do
9530                 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'
9531         done
9532         lctl set_param -n osc.*.contention_seconds 0
9533         clear_osc_stats
9534
9535         dd if=/dev/zero of=$DIR/$tfile count=0
9536         $CHECKSTAT -s 0 $DIR/$tfile
9537
9538         restore_lustre_params <$p
9539         rm -f $p
9540         rm $DIR/$tfile
9541 }
9542 run_test 216 "check lockless direct write works and updates file size and kms correctly"
9543
9544 test_217() { # bug 22430
9545         local node
9546         local nid
9547
9548         for node in $(nodes_list); do
9549                 nid=$(host_nids_address $node $NETTYPE)
9550                 if [[ $nid = *-* ]] ; then
9551                         echo "lctl ping $nid@$NETTYPE"
9552                         lctl ping $nid@$NETTYPE
9553                 else
9554                         echo "skipping $node (no hyphen detected)"
9555                 fi
9556         done
9557 }
9558 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
9559
9560 test_218() {
9561        # do directio so as not to populate the page cache
9562        log "creating a 10 Mb file"
9563        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9564        log "starting reads"
9565        dd if=$DIR/$tfile of=/dev/null bs=4096 &
9566        log "truncating the file"
9567        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9568        log "killing dd"
9569        kill %+ || true # reads might have finished
9570        echo "wait until dd is finished"
9571        wait
9572        log "removing the temporary file"
9573        rm -rf $DIR/$tfile || error "tmp file removal failed"
9574 }
9575 run_test 218 "parallel read and truncate should not deadlock ======================="
9576
9577 test_219() {
9578         # write one partial page
9579         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
9580         # set no grant so vvp_io_commit_write will do sync write
9581         $LCTL set_param fail_loc=0x411
9582         # write a full page at the end of file
9583         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
9584
9585         $LCTL set_param fail_loc=0
9586         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
9587         $LCTL set_param fail_loc=0x411
9588         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
9589 }
9590 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
9591
9592 test_220() { #LU-325
9593         remote_ost_nodsh && skip "remote OST with nodsh" && return
9594         local OSTIDX=0
9595
9596         mkdir -p $DIR/$tdir
9597         local OST=$(lfs osts | grep ${OSTIDX}": " | \
9598                 awk '{print $2}' | sed -e 's/_UUID$//')
9599
9600         # on the mdt's osc
9601         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
9602         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
9603                         osc.$mdtosc_proc1.prealloc_last_id)
9604         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
9605                         osc.$mdtosc_proc1.prealloc_next_id)
9606
9607         $LFS df -i
9608
9609         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
9610         #define OBD_FAIL_OST_ENOINO              0x229
9611         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
9612         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
9613         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
9614
9615         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
9616
9617         MDSOBJS=$((last_id - next_id))
9618         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
9619
9620         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
9621         echo "OST still has $count kbytes free"
9622
9623         echo "create $MDSOBJS files @next_id..."
9624         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
9625
9626         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9627                         osc.$mdtosc_proc1.prealloc_last_id)
9628         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9629                         osc.$mdtosc_proc1.prealloc_next_id)
9630
9631         echo "after creation, last_id=$last_id2, next_id=$next_id2"
9632         $LFS df -i
9633
9634         echo "cleanup..."
9635
9636         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
9637         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
9638
9639         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
9640         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
9641         echo "unlink $MDSOBJS files @$next_id..."
9642         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
9643 }
9644 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
9645
9646 test_221() {
9647         dd if=`which date` of=$MOUNT/date oflag=sync
9648         chmod +x $MOUNT/date
9649
9650         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
9651         $LCTL set_param fail_loc=0x80001401
9652
9653         $MOUNT/date > /dev/null
9654         rm -f $MOUNT/date
9655 }
9656 run_test 221 "make sure fault and truncate race to not cause OOM"
9657
9658 test_222a () {
9659        rm -rf $DIR/$tdir
9660        mkdir -p $DIR/$tdir
9661        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9662        createmany -o $DIR/$tdir/$tfile 10
9663        cancel_lru_locks mdc
9664        cancel_lru_locks osc
9665        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9666        $LCTL set_param fail_loc=0x31a
9667        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
9668        $LCTL set_param fail_loc=0
9669        rm -r $DIR/$tdir
9670 }
9671 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
9672
9673 test_222b () {
9674        rm -rf $DIR/$tdir
9675        mkdir -p $DIR/$tdir
9676        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9677        createmany -o $DIR/$tdir/$tfile 10
9678        cancel_lru_locks mdc
9679        cancel_lru_locks osc
9680        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9681        $LCTL set_param fail_loc=0x31a
9682        rm -r $DIR/$tdir || "AGL for rmdir failed"
9683        $LCTL set_param fail_loc=0
9684 }
9685 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
9686
9687 test_223 () {
9688        rm -rf $DIR/$tdir
9689        mkdir -p $DIR/$tdir
9690        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9691        createmany -o $DIR/$tdir/$tfile 10
9692        cancel_lru_locks mdc
9693        cancel_lru_locks osc
9694        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
9695        $LCTL set_param fail_loc=0x31b
9696        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
9697        $LCTL set_param fail_loc=0
9698        rm -r $DIR/$tdir
9699 }
9700 run_test 223 "osc reenqueue if without AGL lock granted ======================="
9701
9702 test_224a() { # LU-1039, MRP-303
9703         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
9704         $LCTL set_param fail_loc=0x508
9705         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
9706         $LCTL set_param fail_loc=0
9707         df $DIR
9708 }
9709 run_test 224a "Don't panic on bulk IO failure"
9710
9711 test_224b() { # LU-1039, MRP-303
9712         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
9713         cancel_lru_locks osc
9714         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
9715         $LCTL set_param fail_loc=0x515
9716         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
9717         $LCTL set_param fail_loc=0
9718         df $DIR
9719 }
9720 run_test 224b "Don't panic on bulk IO failure"
9721
9722 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
9723 test_225a () {
9724        if [ -z ${MDSSURVEY} ]; then
9725               skip_env "mds-survey not found" && return
9726        fi
9727        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9728             { skip "Need MDS version at least 2.2.51"; return; }
9729
9730        local mds=$(facet_host $SINGLEMDS)
9731        local target=$(do_nodes $mds 'lctl dl' | \
9732                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9733
9734        local cmd1="file_count=1000 thrhi=4"
9735        local cmd2="dir_count=2 layer=mdd stripe_count=0"
9736        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9737        local cmd="$cmd1 $cmd2 $cmd3"
9738
9739        rm -f ${TMP}/mds_survey*
9740        echo + $cmd
9741        eval $cmd || error "mds-survey with zero-stripe failed"
9742        cat ${TMP}/mds_survey*
9743        rm -f ${TMP}/mds_survey*
9744 }
9745 run_test 225a "Metadata survey sanity with zero-stripe"
9746
9747 test_225b () {
9748        if [ -z ${MDSSURVEY} ]; then
9749               skip_env "mds-survey not found" && return
9750        fi
9751        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9752             { skip "Need MDS version at least 2.2.51"; return; }
9753
9754        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
9755               skip_env "Need to mount OST to test" && return
9756        fi
9757
9758        local mds=$(facet_host $SINGLEMDS)
9759        local target=$(do_nodes $mds 'lctl dl' | \
9760                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9761
9762        local cmd1="file_count=1000 thrhi=4"
9763        local cmd2="dir_count=2 layer=mdd stripe_count=1"
9764        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9765        local cmd="$cmd1 $cmd2 $cmd3"
9766
9767        rm -f ${TMP}/mds_survey*
9768        echo + $cmd
9769        eval $cmd || error "mds-survey with stripe_count failed"
9770        cat ${TMP}/mds_survey*
9771        rm -f ${TMP}/mds_survey*
9772 }
9773 run_test 225b "Metadata survey sanity with stripe_count = 1"
9774
9775 mcreate_path2fid () {
9776         local mode=$1
9777         local major=$2
9778         local minor=$3
9779         local name=$4
9780         local desc=$5
9781         local path=$DIR/$tdir/$name
9782         local fid
9783         local rc
9784         local fid_path
9785
9786         $MCREATE --mode=$1 --major=$2 --minor=$3 $path || \
9787                 error "error: cannot create $desc"
9788
9789         fid=$($LFS path2fid $path)
9790         rc=$?
9791         [ $rc -ne 0 ] && error "error: cannot get fid of a $desc"
9792
9793         fid_path=$($LFS fid2path $DIR $fid)
9794         rc=$?
9795         [ $rc -ne 0 ] && error "error: cannot get path of a $desc by fid"
9796
9797         [ "$path" == "$fid_path" ] || \
9798                 error "error: fid2path returned \`$fid_path', expected \`$path'"
9799 }
9800
9801 test_226 () {
9802         rm -rf $DIR/$tdir
9803         mkdir -p $DIR/$tdir
9804
9805         mcreate_path2fid 0010666 0 0 fifo "FIFO"
9806         mcreate_path2fid 0020666 1 3 null "character special file (null)"
9807         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
9808         mcreate_path2fid 0040666 0 0 dir "directory"
9809         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
9810         mcreate_path2fid 0100666 0 0 file "regular file"
9811         mcreate_path2fid 0120666 0 0 link "symbolic link"
9812         mcreate_path2fid 0140666 0 0 sock "socket"
9813 }
9814 run_test 226 "call path2fid and fid2path on files of all type"
9815
9816 # LU-1299 Executing or running ldd on a truncated executable does not
9817 # cause an out-of-memory condition.
9818 test_227() {
9819         dd if=`which date` of=$MOUNT/date bs=1k count=1
9820         chmod +x $MOUNT/date
9821
9822         $MOUNT/date > /dev/null
9823         ldd $MOUNT/date > /dev/null
9824         rm -f $MOUNT/date
9825 }
9826 run_test 227 "running truncated executable does not cause OOM"
9827
9828 # LU-1512 try to reuse idle OI blocks
9829 test_228a() {
9830         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
9831                 skip "non-ldiskfs backend" && return
9832
9833         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9834         local myDIR=$DIR/$tdir
9835
9836         mkdir -p $myDIR
9837         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9838         $LCTL set_param fail_loc=0x80001002
9839         createmany -o $myDIR/t- 10000
9840         $LCTL set_param fail_loc=0
9841         # The guard is current the largest FID holder
9842         touch $myDIR/guard
9843         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9844                     tr -d '[')
9845         local IDX=$(($SEQ % 64))
9846
9847         do_facet $SINGLEMDS sync
9848         # Make sure journal flushed.
9849         sleep 6
9850         local blk1=$(do_facet $SINGLEMDS \
9851                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9852                      grep Blockcount | awk '{print $4}')
9853
9854         # Remove old files, some OI blocks will become idle.
9855         unlinkmany $myDIR/t- 10000
9856         # Create new files, idle OI blocks should be reused.
9857         createmany -o $myDIR/t- 2000
9858         do_facet $SINGLEMDS sync
9859         # Make sure journal flushed.
9860         sleep 6
9861         local blk2=$(do_facet $SINGLEMDS \
9862                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9863                      grep Blockcount | awk '{print $4}')
9864
9865         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9866 }
9867 run_test 228a "try to reuse idle OI blocks"
9868
9869 test_228b() {
9870         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
9871                 skip "non-ldiskfs backend" && return
9872
9873         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9874         local myDIR=$DIR/$tdir
9875
9876         mkdir -p $myDIR
9877         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9878         $LCTL set_param fail_loc=0x80001002
9879         createmany -o $myDIR/t- 10000
9880         $LCTL set_param fail_loc=0
9881         # The guard is current the largest FID holder
9882         touch $myDIR/guard
9883         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9884                     tr -d '[')
9885         local IDX=$(($SEQ % 64))
9886
9887         do_facet $SINGLEMDS sync
9888         # Make sure journal flushed.
9889         sleep 6
9890         local blk1=$(do_facet $SINGLEMDS \
9891                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9892                      grep Blockcount | awk '{print $4}')
9893
9894         # Remove old files, some OI blocks will become idle.
9895         unlinkmany $myDIR/t- 10000
9896
9897         # stop the MDT
9898         stop $SINGLEMDS || error "Fail to stop MDT."
9899         # remount the MDT
9900         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9901
9902         df $MOUNT || error "Fail to df."
9903         # Create new files, idle OI blocks should be reused.
9904         createmany -o $myDIR/t- 2000
9905         do_facet $SINGLEMDS sync
9906         # Make sure journal flushed.
9907         sleep 6
9908         local blk2=$(do_facet $SINGLEMDS \
9909                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9910                      grep Blockcount | awk '{print $4}')
9911
9912         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9913 }
9914 run_test 228b "idle OI blocks can be reused after MDT restart"
9915
9916 #LU-1881
9917 test_228c() {
9918         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
9919                 skip "non-ldiskfs backend" && return
9920
9921         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9922         local myDIR=$DIR/$tdir
9923
9924         mkdir -p $myDIR
9925         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9926         $LCTL set_param fail_loc=0x80001002
9927         # 20000 files can guarantee there are index nodes in the OI file
9928         createmany -o $myDIR/t- 20000
9929         $LCTL set_param fail_loc=0
9930         # The guard is current the largest FID holder
9931         touch $myDIR/guard
9932         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9933                     tr -d '[')
9934         local IDX=$(($SEQ % 64))
9935
9936         do_facet $SINGLEMDS sync
9937         # Make sure journal flushed.
9938         sleep 6
9939         local blk1=$(do_facet $SINGLEMDS \
9940                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9941                      grep Blockcount | awk '{print $4}')
9942
9943         # Remove old files, some OI blocks will become idle.
9944         unlinkmany $myDIR/t- 20000
9945         rm -f $myDIR/guard
9946         # The OI file should become empty now
9947
9948         # Create new files, idle OI blocks should be reused.
9949         createmany -o $myDIR/t- 2000
9950         do_facet $SINGLEMDS sync
9951         # Make sure journal flushed.
9952         sleep 6
9953         local blk2=$(do_facet $SINGLEMDS \
9954                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9955                      grep Blockcount | awk '{print $4}')
9956
9957         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9958 }
9959 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
9960
9961 #
9962 # tests that do cleanup/setup should be run at the end
9963 #
9964
9965 test_900() {
9966         local ls
9967         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
9968         $LCTL set_param fail_loc=0x903
9969         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
9970         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
9971                 echo "clear" > $ls
9972         done
9973         FAIL_ON_ERROR=true cleanup
9974         FAIL_ON_ERROR=true setup
9975 }
9976 run_test 900 "umount should not race with any mgc requeue thread"
9977
9978 complete $SECONDS
9979 check_and_cleanup_lustre
9980 if [ "$I_MOUNTED" != "yes" ]; then
9981         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
9982 fi
9983 exit_status