Whamcloud - gitweb
LU-1299 clio: a combo patch to fix cl_lock
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- mode: Bash; tab-width: 4; indent-tabs-mode: t; -*-
3 # vim:autoindent:shiftwidth=4:tabstop=4:
4 #
5 # Run select tests by setting ONLY, or as arguments to the script.
6 # Skip specific tests by setting EXCEPT.
7 #
8 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
9 set -e
10
11 ONLY=${ONLY:-"$*"}
12 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
13 ALWAYS_EXCEPT="                27u   42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
14 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
15
16 # Tests that fail on uml
17 CPU=`awk '/model/ {print $4}' /proc/cpuinfo`
18 #                                    buffer i/o errs             sock spc runas
19 [ "$CPU" = "UML" ] && EXCEPT="$EXCEPT 27m 27n 27o 27p 27q 27r 31d 54a  64b 99a 99b 99c 99d 99e 99f 101a"
20
21 case `uname -r` in
22 2.4*) FSTYPE=${FSTYPE:-ext3} ;;
23 2.6*) FSTYPE=${FSTYPE:-ldiskfs} ;;
24 *) error "unsupported kernel" ;;
25 esac
26
27 SRCDIR=$(cd $(dirname $0); echo $PWD)
28 export PATH=$PATH:/sbin
29
30 TMP=${TMP:-/tmp}
31
32 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
33 CREATETEST=${CREATETEST:-createtest}
34 LFS=${LFS:-lfs}
35 SETSTRIPE=${SETSTRIPE:-"$LFS setstripe"}
36 GETSTRIPE=${GETSTRIPE:-"$LFS getstripe"}
37 LSTRIPE=${LSTRIPE:-"$LFS setstripe"}
38 LFIND=${LFIND:-"$LFS find"}
39 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
40 LCTL=${LCTL:-lctl}
41 MCREATE=${MCREATE:-mcreate}
42 OPENFILE=${OPENFILE:-openfile}
43 OPENUNLINK=${OPENUNLINK:-openunlink}
44 READS=${READS:-"reads"}
45 MUNLINK=${MUNLINK:-munlink}
46 SOCKETSERVER=${SOCKETSERVER:-socketserver}
47 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
48 MEMHOG=${MEMHOG:-memhog}
49 DIRECTIO=${DIRECTIO:-directio}
50 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
51 UMOUNT=${UMOUNT:-"umount -d"}
52 STRIPES_PER_OBJ=-1
53 CHECK_GRANT=${CHECK_GRANT:-"yes"}
54 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
55
56 export NAME=${NAME:-local}
57
58 SAVE_PWD=$PWD
59
60 CLEANUP=${CLEANUP:-:}
61 SETUP=${SETUP:-:}
62 TRACE=${TRACE:-""}
63 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
64 . $LUSTRE/tests/test-framework.sh
65 init_test_env $@
66 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
67 init_logging
68
69 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 51c 60c 63 64b 68 71 73 77f 78 101a 103 115 120g 124b"
70
71 FAIL_ON_ERROR=false
72
73 cleanup() {
74         echo -n "cln.."
75         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
76         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
77 }
78 setup() {
79         echo -n "mnt.."
80         load_modules
81         setupall || exit 10
82         echo "done"
83 }
84
85 check_kernel_version() {
86         WANT_VER=$1
87         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
88         case $GOT_VER in
89         patchless|patchless_client) return 0;;
90         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
91         esac
92         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
93         return 1
94 }
95
96 if [ "$ONLY" == "cleanup" ]; then
97        sh llmountcleanup.sh
98        exit 0
99 fi
100
101 check_and_setup_lustre
102
103 DIR=${DIR:-$MOUNT}
104 assert_DIR
105
106 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
107     awk '{gsub(/_UUID/,""); print $1}' | head -1)
108 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
109 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
110 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
111 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
112 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
113 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
114
115 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
116 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
117 rm -rf $DIR/[Rdfs][0-9]*
118
119 # $RUNAS_ID may get set incorrectly somewhere else
120 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
121
122 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
123
124 build_test_filter
125
126 if [ "${ONLY}" = "MOUNT" ] ; then
127         echo "Lustre is up, please go on"
128         exit
129 fi
130
131 echo "preparing for tests involving mounts"
132 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
133 touch $EXT2_DEV
134 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
135 echo # add a newline after mke2fs.
136
137 umask 077
138
139 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
140 lctl set_param debug=-1 2> /dev/null || true
141 test_0() {
142         touch $DIR/$tfile
143         $CHECKSTAT -t file $DIR/$tfile || error
144         rm $DIR/$tfile
145         $CHECKSTAT -a $DIR/$tfile || error
146 }
147 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
148
149 test_0b() {
150         chmod 0755 $DIR || error
151         $CHECKSTAT -p 0755 $DIR || error
152 }
153 run_test 0b "chmod 0755 $DIR ============================="
154
155 test_0c() {
156     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
157     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
158 }
159 run_test 0c "check import proc ============================="
160
161 test_1a() {
162         mkdir $DIR/d1
163         mkdir $DIR/d1/d2
164         mkdir $DIR/d1/d2 && error "we expect EEXIST, but not returned"
165         $CHECKSTAT -t dir $DIR/d1/d2 || error
166 }
167 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
168
169 test_1b() {
170         rmdir $DIR/d1/d2
171         rmdir $DIR/d1
172         $CHECKSTAT -a $DIR/d1 || error
173 }
174 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
175
176 test_2a() {
177         mkdir $DIR/d2
178         touch $DIR/d2/f
179         $CHECKSTAT -t file $DIR/d2/f || error
180 }
181 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
182
183 test_2b() {
184         rm -r $DIR/d2
185         $CHECKSTAT -a $DIR/d2 || error
186 }
187 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
188
189 test_3a() {
190         mkdir $DIR/d3
191         $CHECKSTAT -t dir $DIR/d3 || error
192 }
193 run_test 3a "mkdir .../d3 ======================================"
194
195 test_3b() {
196         if [ ! -d $DIR/d3 ]; then
197                 mkdir $DIR/d3
198         fi
199         touch $DIR/d3/f
200         $CHECKSTAT -t file $DIR/d3/f || error
201 }
202 run_test 3b "touch .../d3/f ===================================="
203
204 test_3c() {
205         rm -r $DIR/d3
206         $CHECKSTAT -a $DIR/d3 || error
207 }
208 run_test 3c "rm -r .../d3 ======================================"
209
210 test_4a() {
211         mkdir $DIR/d4
212         $CHECKSTAT -t dir $DIR/d4 || error
213 }
214 run_test 4a "mkdir .../d4 ======================================"
215
216 test_4b() {
217         if [ ! -d $DIR/d4 ]; then
218                 mkdir $DIR/d4
219         fi
220         mkdir $DIR/d4/d2
221         $CHECKSTAT -t dir $DIR/d4/d2 || error
222 }
223 run_test 4b "mkdir .../d4/d2 ==================================="
224
225 test_5() {
226         mkdir $DIR/d5
227         mkdir $DIR/d5/d2
228         chmod 0707 $DIR/d5/d2
229         $CHECKSTAT -t dir -p 0707 $DIR/d5/d2 || error
230 }
231 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
232
233 test_6a() {
234         touch $DIR/f6a
235         chmod 0666 $DIR/f6a || error
236         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
237 }
238 run_test 6a "touch .../f6a; chmod .../f6a ======================"
239
240 test_6b() {
241         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
242         if [ ! -f $DIR/f6a ]; then
243                 touch $DIR/f6a
244                 chmod 0666 $DIR/f6a
245         fi
246         $RUNAS chmod 0444 $DIR/f6a && error
247         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
248 }
249 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
250
251 test_6c() {
252         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
253         touch $DIR/f6c
254         chown $RUNAS_ID $DIR/f6c || error
255         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
256 }
257 run_test 6c "touch .../f6c; chown .../f6c ======================"
258
259 test_6d() {
260         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
261         if [ ! -f $DIR/f6c ]; then
262                 touch $DIR/f6c
263                 chown $RUNAS_ID $DIR/f6c
264         fi
265         $RUNAS chown $UID $DIR/f6c && error
266         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
267 }
268 run_test 6d "$RUNAS chown .../f6c (should return error) =="
269
270 test_6e() {
271         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
272         touch $DIR/f6e
273         chgrp $RUNAS_ID $DIR/f6e || error
274         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
275 }
276 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
277
278 test_6f() {
279         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
280         if [ ! -f $DIR/f6e ]; then
281                 touch $DIR/f6e
282                 chgrp $RUNAS_ID $DIR/f6e
283         fi
284         $RUNAS chgrp $UID $DIR/f6e && error
285         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
286 }
287 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
288
289 test_6g() {
290         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
291         mkdir $DIR/d6g || error
292         chmod 777 $DIR/d6g || error
293         $RUNAS mkdir $DIR/d6g/d || error
294         chmod g+s $DIR/d6g/d || error
295         mkdir $DIR/d6g/d/subdir
296         $CHECKSTAT -g \#$RUNAS_GID $DIR/d6g/d/subdir || error
297 }
298 run_test 6g "Is new dir in sgid dir inheriting group?"
299
300 test_6h() { # bug 7331
301         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
302         touch $DIR/f6h || error "touch failed"
303         chown $RUNAS_ID:$RUNAS_GID $DIR/f6h || error "initial chown failed"
304         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/f6h && error "chown worked"
305         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/f6h || error
306 }
307 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
308
309 test_7a() {
310         mkdir $DIR/d7
311         $MCREATE $DIR/d7/f
312         chmod 0666 $DIR/d7/f
313         $CHECKSTAT -t file -p 0666 $DIR/d7/f || error
314 }
315 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
316
317 test_7b() {
318         if [ ! -d $DIR/d7 ]; then
319                 mkdir $DIR/d7
320         fi
321         $MCREATE $DIR/d7/f2
322         echo -n foo > $DIR/d7/f2
323         [ "`cat $DIR/d7/f2`" = "foo" ] || error
324         $CHECKSTAT -t file -s 3 $DIR/d7/f2 || error
325 }
326 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
327
328 test_8() {
329         mkdir $DIR/d8
330         touch $DIR/d8/f
331         chmod 0666 $DIR/d8/f
332         $CHECKSTAT -t file -p 0666 $DIR/d8/f || error
333 }
334 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
335
336 test_9() {
337         mkdir $DIR/d9
338         mkdir $DIR/d9/d2
339         mkdir $DIR/d9/d2/d3
340         $CHECKSTAT -t dir $DIR/d9/d2/d3 || error
341 }
342 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
343
344 test_10() {
345         mkdir $DIR/d10
346         mkdir $DIR/d10/d2
347         touch $DIR/d10/d2/f
348         $CHECKSTAT -t file $DIR/d10/d2/f || error
349 }
350 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
351
352 test_11() {
353         mkdir $DIR/d11
354         mkdir $DIR/d11/d2
355         chmod 0666 $DIR/d11/d2
356         chmod 0705 $DIR/d11/d2
357         $CHECKSTAT -t dir -p 0705 $DIR/d11/d2 || error
358 }
359 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
360
361 test_12() {
362         mkdir $DIR/d12
363         touch $DIR/d12/f
364         chmod 0666 $DIR/d12/f
365         chmod 0654 $DIR/d12/f
366         $CHECKSTAT -t file -p 0654 $DIR/d12/f || error
367 }
368 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
369
370 test_13() {
371         mkdir $DIR/d13
372         dd if=/dev/zero of=$DIR/d13/f count=10
373         >  $DIR/d13/f
374         $CHECKSTAT -t file -s 0 $DIR/d13/f || error
375 }
376 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
377
378 test_14() {
379         mkdir $DIR/d14
380         touch $DIR/d14/f
381         rm $DIR/d14/f
382         $CHECKSTAT -a $DIR/d14/f || error
383 }
384 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
385
386 test_15() {
387         mkdir $DIR/d15
388         touch $DIR/d15/f
389         mv $DIR/d15/f $DIR/d15/f2
390         $CHECKSTAT -t file $DIR/d15/f2 || error
391 }
392 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
393
394 test_16() {
395         mkdir $DIR/d16
396         touch $DIR/d16/f
397         rm -rf $DIR/d16/f
398         $CHECKSTAT -a $DIR/d16/f || error
399 }
400 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
401
402 test_17a() {
403         mkdir -p $DIR/d17
404         touch $DIR/d17/f
405         ln -s $DIR/d17/f $DIR/d17/l-exist
406         ls -l $DIR/d17
407         $CHECKSTAT -l $DIR/d17/f $DIR/d17/l-exist || error
408         $CHECKSTAT -f -t f $DIR/d17/l-exist || error
409         rm -f $DIR/d17/l-exist
410         $CHECKSTAT -a $DIR/d17/l-exist || error
411 }
412 run_test 17a "symlinks: create, remove (real) =================="
413
414 test_17b() {
415         mkdir -p $DIR/d17
416         ln -s no-such-file $DIR/d17/l-dangle
417         ls -l $DIR/d17
418         $CHECKSTAT -l no-such-file $DIR/d17/l-dangle || error
419         $CHECKSTAT -fa $DIR/d17/l-dangle || error
420         rm -f $DIR/d17/l-dangle
421         $CHECKSTAT -a $DIR/d17/l-dangle || error
422 }
423 run_test 17b "symlinks: create, remove (dangling) =============="
424
425 test_17c() { # bug 3440 - don't save failed open RPC for replay
426         mkdir -p $DIR/d17
427         ln -s foo $DIR/d17/f17c
428         cat $DIR/d17/f17c && error "opened non-existent symlink" || true
429 }
430 run_test 17c "symlinks: open dangling (should return error) ===="
431
432 test_17d() {
433         mkdir -p $DIR/d17
434         ln -s foo $DIR/d17/f17d
435         touch $DIR/d17/f17d || error "creating to new symlink"
436 }
437 run_test 17d "symlinks: create dangling ========================"
438
439 test_17e() {
440         mkdir -p $DIR/$tdir
441         local foo=$DIR/$tdir/$tfile
442         ln -s $foo $foo || error "create symlink failed"
443         ls -l $foo || error "ls -l failed"
444         ls $foo && error "ls not failed" || true
445 }
446 run_test 17e "symlinks: create recursive symlink (should return error) ===="
447
448 test_17f() {
449         mkdir -p $DIR/d17f
450         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
455         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
456         ls -l  $DIR/d17f
457 }
458 run_test 17f "symlinks: long and very long symlink name ========================"
459
460 # str_repeat(S, N) generate a string that is string S repeated N times
461 str_repeat() {
462         local s=$1
463         local n=$2
464         local ret=''
465         while [ $((n -= 1)) -ge 0 ]; do
466                 ret=$ret$s
467         done
468         echo $ret
469 }
470
471 # Long symlinks and LU-2241
472 test_17g() {
473         mkdir -p $DIR/$tdir
474         local TESTS="59 60 61 4094 4095"
475
476         for i in $TESTS; do
477                 local SYMNAME=$(str_repeat 'x' $i)
478                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
479                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
480         done
481 }
482 run_test 17g "symlinks: really long symlink name and inode boundaries"
483
484 test_17h() { #bug 17378
485         mkdir -p $DIR/$tdir
486         $SETSTRIPE $DIR/$tdir -c -1
487 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
488         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000141
489         touch $DIR/$tdir/$tfile || true
490 }
491 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
492
493 test_17i() { #bug 20018
494         mkdir -p $DIR/$tdir
495         local foo=$DIR/$tdir/$tfile
496         ln -s $foo $foo || error "create symlink failed"
497 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
498         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000143
499         ls -l $foo && error "error not detected"
500         return 0
501 }
502 run_test 17i "don't panic on short symlink"
503
504 test_17k() { #bug 22301
505         rsync --help | grep -q xattr ||
506                 skip_env "$(rsync --version| head -1) does not support xattrs"
507         mkdir -p $DIR/{$tdir,$tdir.new}
508         touch $DIR/$tdir/$tfile
509         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
510         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
511                 error "rsync failed with xattrs enabled"
512 }
513 run_test 17k "symlinks: rsync with xattrs enabled ========================="
514
515 # LU-1540
516 test_17m() {
517         local short_sym="0123456789"
518         local WDIR=$DIR/${tdir}m
519         local mds_index
520         local devname
521         local cmd
522         local i
523         local rc=0
524
525         mkdir -p $WDIR
526         long_sym=$short_sym
527         # create a long symlink file
528         for ((i = 0; i < 4; ++i)); do
529                 long_sym=${long_sym}${long_sym}
530         done
531
532         echo "create 512 short and long symlink files under $WDIR"
533         for ((i = 0; i < 256; ++i)); do
534                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
535                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
536         done
537
538         echo "erase them"
539         rm -f $WDIR/*
540         sync
541         sleep 2
542
543         echo "recreate the 512 symlink files with a shorter string"
544         for ((i = 0; i < 512; ++i)); do
545                 # rewrite the symlink file with a shorter string
546                 ln -sf ${long_sym} $WDIR/long-$i
547                 ln -sf ${short_sym} $WDIR/short-$i
548         done
549
550         mds_index=1
551         devname=$(mdsdevname $mds_index)
552         cmd="$E2FSCK -fnvd $devname"
553
554         echo "stop and checking mds${mds_index}: $cmd"
555         # e2fsck should not return error
556         stop mds${mds_index} -f
557         do_facet mds${mds_index} $cmd || rc=$?
558
559         start mds${mds_index} $devname $MDS_MOUNT_OPTS
560         df $MOUNT > /dev/null 2>&1
561         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
562                 "short/long symlink MDT: rc=$rc"
563         return $rc
564 }
565 run_test 17m "run e2fsck against MDT which contains short/long symlink"
566
567 test_18() {
568         touch $DIR/f
569         ls $DIR || error
570 }
571 run_test 18 "touch .../f ; ls ... =============================="
572
573 test_19a() {
574         touch $DIR/f19
575         ls -l $DIR
576         rm $DIR/f19
577         $CHECKSTAT -a $DIR/f19 || error
578 }
579 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
580
581 test_19b() {
582         ls -l $DIR/f19 && error || true
583 }
584 run_test 19b "ls -l .../f19 (should return error) =============="
585
586 test_19c() {
587         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
588         $RUNAS touch $DIR/f19 && error || true
589 }
590 run_test 19c "$RUNAS touch .../f19 (should return error) =="
591
592 test_19d() {
593         cat $DIR/f19 && error || true
594 }
595 run_test 19d "cat .../f19 (should return error) =============="
596
597 test_20() {
598         touch $DIR/f
599         rm $DIR/f
600         log "1 done"
601         touch $DIR/f
602         rm $DIR/f
603         log "2 done"
604         touch $DIR/f
605         rm $DIR/f
606         log "3 done"
607         $CHECKSTAT -a $DIR/f || error
608 }
609 run_test 20 "touch .../f ; ls -l ... ==========================="
610
611 test_21() {
612         mkdir $DIR/d21
613         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
614         ln -s dangle $DIR/d21/link
615         echo foo >> $DIR/d21/link
616         cat $DIR/d21/dangle
617         $CHECKSTAT -t link $DIR/d21/link || error
618         $CHECKSTAT -f -t file $DIR/d21/link || error
619 }
620 run_test 21 "write to dangling link ============================"
621
622 test_22() {
623         WDIR=$DIR/$tdir
624         mkdir -p $WDIR
625         chown $RUNAS_ID:$RUNAS_GID $WDIR
626         (cd $WDIR || error "cd $WDIR failed";
627         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
628         $RUNAS tar xf -)
629         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
630         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
631         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
632 }
633 run_test 22 "unpack tar archive as non-root user ==============="
634
635 # was test_23
636 test_23a() {
637         mkdir -p $DIR/$tdir
638         local file=$DIR/$tdir/$tfile
639
640         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
641         openfile -f O_CREAT:O_EXCL $file &&
642                 error "$file recreate succeeded" || true
643 }
644 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
645
646 test_23b() { # bug 18988
647         mkdir -p $DIR/$tdir
648         local file=$DIR/$tdir/$tfile
649
650         rm -f $file
651         echo foo > $file || error "write filed"
652         echo bar >> $file || error "append filed"
653         $CHECKSTAT -s 8 $file || error "wrong size"
654         rm $file
655 }
656 run_test 23b "O_APPEND check =========================="
657
658 test_24a() {
659         echo '== rename sanity =============================================='
660         echo '-- same directory rename'
661         mkdir $DIR/R1
662         touch $DIR/R1/f
663         mv $DIR/R1/f $DIR/R1/g
664         $CHECKSTAT -t file $DIR/R1/g || error
665 }
666 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
667
668 test_24b() {
669         mkdir $DIR/R2
670         touch $DIR/R2/{f,g}
671         mv $DIR/R2/f $DIR/R2/g
672         $CHECKSTAT -a $DIR/R2/f || error
673         $CHECKSTAT -t file $DIR/R2/g || error
674 }
675 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
676
677 test_24c() {
678         mkdir $DIR/R3
679         mkdir $DIR/R3/f
680         mv $DIR/R3/f $DIR/R3/g
681         $CHECKSTAT -a $DIR/R3/f || error
682         $CHECKSTAT -t dir $DIR/R3/g || error
683 }
684 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
685
686 test_24d() {
687         mkdir $DIR/R4
688         mkdir $DIR/R4/{f,g}
689         mrename $DIR/R4/f $DIR/R4/g
690         $CHECKSTAT -a $DIR/R4/f || error
691         $CHECKSTAT -t dir $DIR/R4/g || error
692 }
693 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
694
695 test_24e() {
696         echo '-- cross directory renames --'
697         mkdir $DIR/R5{a,b}
698         touch $DIR/R5a/f
699         mv $DIR/R5a/f $DIR/R5b/g
700         $CHECKSTAT -a $DIR/R5a/f || error
701         $CHECKSTAT -t file $DIR/R5b/g || error
702 }
703 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
704
705 test_24f() {
706         mkdir $DIR/R6{a,b}
707         touch $DIR/R6a/f $DIR/R6b/g
708         mv $DIR/R6a/f $DIR/R6b/g
709         $CHECKSTAT -a $DIR/R6a/f || error
710         $CHECKSTAT -t file $DIR/R6b/g || error
711 }
712 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
713
714 test_24g() {
715         mkdir $DIR/R7{a,b}
716         mkdir $DIR/R7a/d
717         mv $DIR/R7a/d $DIR/R7b/e
718         $CHECKSTAT -a $DIR/R7a/d || error
719         $CHECKSTAT -t dir $DIR/R7b/e || error
720 }
721 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
722
723 test_24h() {
724         mkdir $DIR/R8{a,b}
725         mkdir $DIR/R8a/d $DIR/R8b/e
726         mrename $DIR/R8a/d $DIR/R8b/e
727         $CHECKSTAT -a $DIR/R8a/d || error
728         $CHECKSTAT -t dir $DIR/R8b/e || error
729 }
730 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
731
732 test_24i() {
733         echo "-- rename error cases"
734         mkdir $DIR/R9
735         mkdir $DIR/R9/a
736         touch $DIR/R9/f
737         mrename $DIR/R9/f $DIR/R9/a
738         $CHECKSTAT -t file $DIR/R9/f || error
739         $CHECKSTAT -t dir  $DIR/R9/a || error
740         $CHECKSTAT -a $DIR/R9/a/f || error
741 }
742 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
743
744 test_24j() {
745         mkdir $DIR/R10
746         mrename $DIR/R10/f $DIR/R10/g
747         $CHECKSTAT -t dir $DIR/R10 || error
748         $CHECKSTAT -a $DIR/R10/f || error
749         $CHECKSTAT -a $DIR/R10/g || error
750 }
751 run_test 24j "source does not exist ============================"
752
753 test_24k() {
754         mkdir $DIR/R11a $DIR/R11a/d
755         touch $DIR/R11a/f
756         mv $DIR/R11a/f $DIR/R11a/d
757         $CHECKSTAT -a $DIR/R11a/f || error
758         $CHECKSTAT -t file $DIR/R11a/d/f || error
759 }
760 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
761
762 # bug 2429 - rename foo foo foo creates invalid file
763 test_24l() {
764         f="$DIR/f24l"
765         multiop $f OcNs || error
766 }
767 run_test 24l "Renaming a file to itself ========================"
768
769 test_24m() {
770         f="$DIR/f24m"
771         multiop $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
772         # on ext3 this does not remove either the source or target files
773         # though the "expected" operation would be to remove the source
774         $CHECKSTAT -t file ${f} || error "${f} missing"
775         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
776 }
777 run_test 24m "Renaming a file to a hard link to itself ========="
778
779 test_24n() {
780     f="$DIR/f24n"
781     # this stats the old file after it was renamed, so it should fail
782     touch ${f}
783     $CHECKSTAT ${f}
784     mv ${f} ${f}.rename
785     $CHECKSTAT ${f}.rename
786     $CHECKSTAT -a ${f}
787 }
788 run_test 24n "Statting the old file after renaming (Posix rename 2)"
789
790 test_24o() {
791         check_kernel_version 37 || return 0
792         mkdir -p $DIR/d24o
793         rename_many -s random -v -n 10 $DIR/d24o
794 }
795 run_test 24o "rename of files during htree split ==============="
796
797 test_24p() {
798         mkdir $DIR/R12{a,b}
799         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
800         mrename $DIR/R12a $DIR/R12b
801         $CHECKSTAT -a $DIR/R12a || error
802         $CHECKSTAT -t dir $DIR/R12b || error
803         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
804         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
805 }
806 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
807
808 test_24q() {
809         mkdir $DIR/R13{a,b}
810         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
811         multiop_bg_pause $DIR/R13b D_c || return 1
812         MULTIPID=$!
813
814         mrename $DIR/R13a $DIR/R13b
815         $CHECKSTAT -a $DIR/R13a || error
816         $CHECKSTAT -t dir $DIR/R13b || error
817         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
818         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
819         kill -USR1 $MULTIPID
820         wait $MULTIPID || error "multiop close failed"
821 }
822 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
823
824 test_24r() { #bug 3789
825         mkdir $DIR/R14a $DIR/R14a/b
826         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
827         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
828         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
829 }
830 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
831
832 test_24s() {
833         mkdir $DIR/R15a $DIR/R15a/b $DIR/R15a/b/c
834         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
835         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
836         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
837 }
838 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
839 test_24t() {
840         mkdir $DIR/R16a $DIR/R16a/b $DIR/R16a/b/c
841         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
842         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
843         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
844 }
845 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
846
847 test_24u() { # bug12192
848         multiop $DIR/$tfile C2w$((2048 * 1024))c || error
849         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
850 }
851 run_test 24u "create stripe file"
852
853 page_size() {
854         getconf PAGE_SIZE
855 }
856
857 test_24v() {
858         local NRFILES=100000
859         local FREE_INODES=`lfs df -i|grep "filesystem summary" | awk '{print $5}'`
860         [ $FREE_INODES -lt $NRFILES ] && \
861                 skip "not enough free inodes $FREE_INODES required $NRFILES" && \
862                 return
863
864         mkdir -p $DIR/d24v
865         createmany -m $DIR/d24v/$tfile $NRFILES
866
867         cancel_lru_locks mdc
868         lctl set_param mdc.*.stats clear
869
870         ls $DIR/d24v >/dev/null || error "error in listing large dir"
871
872         # LU-5 large readdir
873         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
874         #               8 bytes for name(filename is mostly 5 in this test) +
875         #               8 bytes for luda_type
876         # take into account of overhead in lu_dirpage header and end mark in
877         # each page, plus one in RPC_NUM calculation.
878         DIRENT_SIZE=48
879         RPC_SIZE=$(($(lctl get_param -n mdc.*.max_pages_per_rpc)*$(page_size)))
880         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
881         mds_readpage=`lctl get_param mdc.*.stats | \
882                                 awk '/^mds_readpage/ {print $2}'`
883         [ $mds_readpage -gt $RPC_NUM ] && \
884                 error "large readdir doesn't take effect"
885
886         rm $DIR/d24v -rf
887 }
888 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
889
890 test_24w() { # bug21506
891         SZ1=234852
892         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
893         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
894         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
895         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
896         [ "$SZ1" = "$SZ2" ] || \
897                 error "Error reading at the end of the file $tfile"
898 }
899 run_test 24w "Reading a file larger than 4Gb"
900
901 test_25a() {
902         echo '== symlink sanity ============================================='
903
904         mkdir $DIR/d25
905         ln -s d25 $DIR/s25
906         touch $DIR/s25/foo || error
907 }
908 run_test 25a "create file in symlinked directory ==============="
909
910 test_25b() {
911         [ ! -d $DIR/d25 ] && test_25a
912         $CHECKSTAT -t file $DIR/s25/foo || error
913 }
914 run_test 25b "lookup file in symlinked directory ==============="
915
916 test_26a() {
917         mkdir $DIR/d26
918         mkdir $DIR/d26/d26-2
919         ln -s d26/d26-2 $DIR/s26
920         touch $DIR/s26/foo || error
921 }
922 run_test 26a "multiple component symlink ======================="
923
924 test_26b() {
925         mkdir -p $DIR/d26b/d26-2
926         ln -s d26b/d26-2/foo $DIR/s26-2
927         touch $DIR/s26-2 || error
928 }
929 run_test 26b "multiple component symlink at end of lookup ======"
930
931 test_26c() {
932         mkdir $DIR/d26.2
933         touch $DIR/d26.2/foo
934         ln -s d26.2 $DIR/s26.2-1
935         ln -s s26.2-1 $DIR/s26.2-2
936         ln -s s26.2-2 $DIR/s26.2-3
937         chmod 0666 $DIR/s26.2-3/foo
938 }
939 run_test 26c "chain of symlinks ================================"
940
941 # recursive symlinks (bug 439)
942 test_26d() {
943         ln -s d26-3/foo $DIR/d26-3
944 }
945 run_test 26d "create multiple component recursive symlink ======"
946
947 test_26e() {
948         [ ! -h $DIR/d26-3 ] && test_26d
949         rm $DIR/d26-3
950 }
951 run_test 26e "unlink multiple component recursive symlink ======"
952
953 # recursive symlinks (bug 7022)
954 test_26f() {
955         mkdir -p $DIR/$tdir
956         mkdir $DIR/$tdir/$tfile        || error "mkdir $DIR/$tdir/$tfile failed"
957         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
958         mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
959         mkdir $tfile             || error "mkdir $tfile failed"
960         cd $tfile                || error "cd $tfile failed"
961         ln -s .. dotdot          || error "ln dotdot failed"
962         ln -s dotdot/lndir lndir || error "ln lndir failed"
963         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
964         output=`ls $tfile/$tfile/lndir/bar1`
965         [ "$output" = bar1 ] && error "unexpected output"
966         rm -r $tfile             || error "rm $tfile failed"
967         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
968 }
969 run_test 26f "rm -r of a directory which has recursive symlink ="
970
971 test_27a() {
972         echo '== stripe sanity =============================================='
973         mkdir -p $DIR/d27 || error "mkdir failed"
974         $GETSTRIPE $DIR/d27
975         $SETSTRIPE $DIR/d27/f0 -c 1 || error "lstripe failed"
976         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
977         pass
978         log "== test_27a: write to one stripe file ========================="
979         cp /etc/hosts $DIR/d27/f0 || error
980 }
981 run_test 27a "one stripe file =================================="
982
983 test_27c() {
984         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
985         mkdir -p $DIR/d27
986         $SETSTRIPE $DIR/d27/f01 -c 2 || error "lstripe failed"
987         [ `$GETSTRIPE $DIR/d27/f01 | grep -A 10 obdidx | wc -l` -eq 4 ] ||
988                 error "two-stripe file doesn't have two stripes"
989         pass
990         log "== test_27c: write to two stripe file file f01 ================"
991         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
992 }
993 run_test 27c "create two stripe file f01 ======================="
994
995 test_27d() {
996         mkdir -p $DIR/d27
997         $SETSTRIPE -c0 -i-1 -s0 $DIR/d27/fdef || error "lstripe failed"
998         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
999         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1000 }
1001 run_test 27d "create file with default settings ================"
1002
1003 test_27e() {
1004         mkdir -p $DIR/d27
1005         $SETSTRIPE $DIR/d27/f12 -c 2 || error "lstripe failed"
1006         $SETSTRIPE $DIR/d27/f12 -c 2 && error "lstripe succeeded twice"
1007         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1008 }
1009 run_test 27e "setstripe existing file (should return error) ======"
1010
1011 test_27f() {
1012         mkdir -p $DIR/d27
1013         $SETSTRIPE $DIR/d27/fbad -s 100 -i 0 -c 1 && error "lstripe failed"
1014         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1015         $GETSTRIPE $DIR/d27/fbad || error "lfs getstripe failed"
1016 }
1017 run_test 27f "setstripe with bad stripe size (should return error)"
1018
1019 test_27g() {
1020         mkdir -p $DIR/d27
1021         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1022         pass
1023         log "== test 27h: lfs getstripe with no objects ===================="
1024         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" || error "has object"
1025         pass
1026         log "== test 27i: lfs getstripe with some objects =================="
1027         touch $DIR/d27/fsome || error "touch failed"
1028         $GETSTRIPE $DIR/d27/fsome | grep obdidx || error "missing objects"
1029 }
1030 run_test 27g "test lfs getstripe ==========================================="
1031
1032 test_27j() {
1033         mkdir -p $DIR/d27
1034         $SETSTRIPE $DIR/d27/f27j -i $OSTCOUNT && error "lstripe failed"||true
1035 }
1036 run_test 27j "setstripe with bad stripe offset (should return error)"
1037
1038 test_27k() { # bug 2844
1039         mkdir -p $DIR/d27
1040         FILE=$DIR/d27/f27k
1041         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1042         [ ! -d $DIR/d27 ] && mkdir -p $DIR/d27
1043         $SETSTRIPE $FILE -s 67108864 || error "lstripe failed"
1044         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1045         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1046         dd if=/dev/zero of=$FILE bs=4k count=1
1047         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1048         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1049 }
1050 run_test 27k "limit i_blksize for broken user apps ============="
1051
1052 test_27l() {
1053         mkdir -p $DIR/d27
1054         mcreate $DIR/f27l || error "creating file"
1055         $RUNAS $SETSTRIPE $DIR/f27l -c 1 && \
1056                 error "lstripe should have failed" || true
1057 }
1058 run_test 27l "check setstripe permissions (should return error)"
1059
1060 test_27m() {
1061         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1062         if [ $ORIGFREE -gt $MAXFREE ]; then
1063                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1064                 return
1065         fi
1066         mkdir -p $DIR/d27
1067         $SETSTRIPE $DIR/d27/f27m_1 -i 0 -c 1
1068         dd if=/dev/zero of=$DIR/d27/f27m_1 bs=1024 count=$MAXFREE && \
1069                 error "dd should fill OST0"
1070         i=2
1071         while $SETSTRIPE $DIR/d27/f27m_$i -i 0 -c 1 ; do
1072                 i=`expr $i + 1`
1073                 [ $i -gt 256 ] && break
1074         done
1075         i=`expr $i + 1`
1076         touch $DIR/d27/f27m_$i
1077         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
1078                 error "OST0 was full but new created file still use it"
1079         i=`expr $i + 1`
1080         touch $DIR/d27/f27m_$i
1081         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
1082                 error "OST0 was full but new created file still use it"
1083         rm -r $DIR/d27
1084         sleep 15
1085 }
1086 run_test 27m "create file while OST0 was full =================="
1087
1088 sleep_maxage() {
1089         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1090         sleep $DELAY
1091 }
1092
1093 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1094 # if the OST isn't full anymore.
1095 reset_enospc() {
1096         local OSTIDX=${1:-""}
1097
1098         local list=$(comma_list $(osts_nodes))
1099         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1100
1101         do_nodes $list lctl set_param fail_loc=0
1102         sleep_maxage
1103 }
1104
1105 exhaust_precreations() {
1106         local OSTIDX=$1
1107         local FAILLOC=$2
1108         local FAILIDX=${3:-$OSTIDX}
1109
1110         mkdir -p $DIR/$tdir
1111         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1112         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1113
1114         local OST=$(lfs osts | grep ${OSTIDX}": " | \
1115                 awk '{print $2}' | sed -e 's/_UUID$//')
1116         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1117                           sed -e 's/_UUID$//;s/^.*-//')
1118
1119         # on the mdt's osc
1120         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1121         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1122         osc.$mdtosc_proc1.prealloc_last_id)
1123         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1124         osc.$mdtosc_proc1.prealloc_next_id)
1125
1126         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1127         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1128
1129         mkdir -p $DIR/$tdir/${OST}
1130         $SETSTRIPE $DIR/$tdir/${OST} -i $OSTIDX -c 1
1131 #define OBD_FAIL_OST_ENOSPC              0x215
1132         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1133         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1134         echo "Creating to objid $last_id on ost $OST..."
1135         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1136         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1137         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1138         sleep_maxage
1139 }
1140
1141 exhaust_all_precreations() {
1142         local i
1143         for (( i=0; i < OSTCOUNT; i++ )) ; do
1144                 exhaust_precreations $i $1 -1
1145         done
1146 }
1147
1148 test_27n() {
1149         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1150         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1151         remote_ost_nodsh && skip "remote OST with nodsh" && return
1152
1153         reset_enospc
1154         rm -f $DIR/$tdir/$tfile
1155         exhaust_precreations 0 0x80000215
1156         $SETSTRIPE -c -1 $DIR/$tdir
1157         touch $DIR/$tdir/$tfile || error
1158         $GETSTRIPE $DIR/$tdir/$tfile
1159         reset_enospc
1160 }
1161 run_test 27n "create file with some full OSTs =================="
1162
1163 test_27o() {
1164         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1165         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1166         remote_ost_nodsh && skip "remote OST with nodsh" && return
1167
1168         reset_enospc
1169         rm -f $DIR/$tdir/$tfile
1170         exhaust_all_precreations 0x215
1171
1172         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1173
1174         reset_enospc
1175         rm -rf $DIR/$tdir/*
1176 }
1177 run_test 27o "create file with all full OSTs (should error) ===="
1178
1179 test_27p() {
1180         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1181         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1182         remote_ost_nodsh && skip "remote OST with nodsh" && return
1183
1184         reset_enospc
1185         rm -f $DIR/$tdir/$tfile
1186         mkdir -p $DIR/$tdir
1187
1188         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1189         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1190         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1191
1192         exhaust_precreations 0 0x80000215
1193         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1194         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1195         $LFS getstripe $DIR/$tdir/$tfile
1196
1197         reset_enospc
1198 }
1199 run_test 27p "append to a truncated file with some full OSTs ==="
1200
1201 test_27q() {
1202         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1203         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1204         remote_ost_nodsh && skip "remote OST with nodsh" && return
1205
1206         reset_enospc
1207         rm -f $DIR/$tdir/$tfile
1208
1209         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1210         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1211         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1212
1213         exhaust_all_precreations 0x215
1214
1215         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1216         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1217
1218         reset_enospc
1219 }
1220 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1221
1222 test_27r() {
1223         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1224         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1225         remote_ost_nodsh && skip "remote OST with nodsh" && return
1226
1227         reset_enospc
1228         rm -f $DIR/$tdir/$tfile
1229         exhaust_precreations 0 0x80000215
1230
1231         $SETSTRIPE $DIR/$tdir/$tfile -i 0 -c 2 # && error
1232
1233         reset_enospc
1234 }
1235 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1236
1237 test_27s() { # bug 10725
1238         mkdir -p $DIR/$tdir
1239         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1240         local stripe_count=0
1241         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1242         $SETSTRIPE $DIR/$tdir -s $stripe_size -c $stripe_count && \
1243                 error "stripe width >= 2^32 succeeded" || true
1244
1245 }
1246 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1247
1248 test_27t() { # bug 10864
1249         WDIR=`pwd`
1250         WLFS=`which lfs`
1251         cd $DIR
1252         touch $tfile
1253         $WLFS getstripe $tfile
1254         cd $WDIR
1255 }
1256 run_test 27t "check that utils parse path correctly"
1257
1258 test_27u() { # bug 4900
1259         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1260         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1261
1262 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1263         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1264         mkdir -p $DIR/$tdir
1265         createmany -o $DIR/$tdir/t- 1000
1266         do_facet $SINGLEMDS lctl set_param fail_loc=0
1267
1268         TLOG=$DIR/$tfile.getstripe
1269         $GETSTRIPE $DIR/$tdir > $TLOG
1270         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1271         unlinkmany $DIR/$tdir/t- 1000
1272         [ $OBJS -gt 0 ] && \
1273                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1274 }
1275 run_test 27u "skip object creation on OSC w/o objects =========="
1276
1277 test_27v() { # bug 4900
1278         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1279         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1280         remote_ost_nodsh && skip "remote OST with nodsh" && return
1281
1282         exhaust_all_precreations 0x215
1283         reset_enospc
1284
1285         mkdir -p $DIR/$tdir
1286         $SETSTRIPE $DIR/$tdir -c 1         # 1 stripe / file
1287
1288         touch $DIR/$tdir/$tfile
1289         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1290         # all except ost1
1291         for (( i=0; i < OSTCOUNT; i++ )) ; do
1292                 do_facet ost$i lctl set_param fail_loc=0x705
1293         done
1294         local START=`date +%s`
1295         createmany -o $DIR/$tdir/$tfile 32
1296
1297         local FINISH=`date +%s`
1298         local TIMEOUT=`lctl get_param -n timeout`
1299         local PROCESS=$((FINISH - START))
1300         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1301                error "$FINISH - $START >= $TIMEOUT / 2"
1302         sleep $((TIMEOUT / 2 - PROCESS))
1303         reset_enospc
1304 }
1305 run_test 27v "skip object creation on slow OST ================="
1306
1307 test_27w() { # bug 10997
1308         mkdir -p $DIR/$tdir || error "mkdir failed"
1309         $LSTRIPE $DIR/$tdir/f0 -s 65536 || error "lstripe failed"
1310         size=`$GETSTRIPE $DIR/$tdir/f0 -s`
1311         [ $size -ne 65536 ] && error "stripe size $size != 65536" || true
1312         gsdir=$($LFS getstripe -d $DIR/$tdir)
1313         [ $(echo $gsdir | grep -c stripe_count) -ne 1 ] && error "$LFS getstripe -d $DIR/$tdir failed"
1314
1315         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping multiple stripe count/offset test" && return
1316         for i in `seq 1 $OSTCOUNT`; do
1317                 offset=$(($i-1))
1318                 $LSTRIPE $DIR/$tdir/f$i -c $i -i $offset || error "lstripe -c $i -i $offset failed"
1319                 count=`$GETSTRIPE -c $DIR/$tdir/f$i`
1320                 index=`$GETSTRIPE -o $DIR/$tdir/f$i`
1321                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1322                 [ $index -ne $offset ] && error "stripe offset $index != $offset" || true
1323         done
1324 }
1325 run_test 27w "check lfs setstripe -c -s -i options ============="
1326
1327 test_27x() {
1328         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1329         OFFSET=$(($OSTCOUNT - 1))
1330         OSTIDX=0
1331         local OST=$(lfs osts | awk '/'${OSTIDX}': / { print $2 }' | sed -e 's/_UUID$//')
1332
1333         mkdir -p $DIR/$tdir
1334         $SETSTRIPE $DIR/$tdir -c 1      # 1 stripe per file
1335         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1336         sleep_maxage
1337         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1338         for i in `seq 0 $OFFSET`; do
1339                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1340                 error "OST0 was degraded but new created file still use it"
1341         done
1342         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1343 }
1344 run_test 27x "create files while OST0 is degraded"
1345
1346 test_27y() {
1347         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1348         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1349
1350         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1351         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1352             osc.$mdtosc.prealloc_last_id)
1353         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1354             osc.$mdtosc.prealloc_next_id)
1355         local fcount=$((last_id - next_id))
1356         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1357         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1358
1359         MDS_OSCS=`do_facet $SINGLEMDS lctl dl | awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
1360         OFFSET=$(($OSTCOUNT-1))
1361         OST=-1
1362         for OSC in $MDS_OSCS; do
1363                 if [ $OST == -1 ]; then {
1364                         OST=`osc_to_ost $OSC`
1365                 } else {
1366                         echo $OSC "is Deactivate:"
1367                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1368                 } fi
1369         done
1370
1371         OSTIDX=$(lfs osts | grep ${OST} | awk '{print $1}' | sed -e 's/://')
1372         mkdir -p $DIR/$tdir
1373         $SETSTRIPE $DIR/$tdir -c 1      # 1 stripe / file
1374
1375         do_facet ost$OSTIDX lctl set_param -n obdfilter.$OST.degraded 1
1376         sleep_maxage
1377         createmany -o $DIR/$tdir/$tfile $fcount
1378         do_facet ost$OSTIDX lctl set_param -n obdfilter.$OST.degraded 0
1379
1380         for i in `seq 0 $OFFSET`; do
1381                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "$OSTIDX"` ] || \
1382                       error "files created on deactivated OSTs instead of degraded OST"
1383         done
1384         for OSC in $MDS_OSCS; do
1385                 [ `osc_to_ost $OSC` != $OST  ] && {
1386                         echo $OSC "is activate"
1387                         do_facet $SINGLEMDS lctl --device %$OSC activate
1388                 }
1389         done
1390 }
1391 run_test 27y "create files while OST0 is degraded and the rest inactive"
1392
1393 check_seq_oid()
1394 {
1395         echo check file $1
1396         local old_ifs="$IFS"
1397         IFS=$'\t\n :'
1398         lmm=($($GETSTRIPE -v $1))
1399
1400         IFS=$'[:]'
1401         fid=($($LFS path2fid $1))
1402         IFS="$old_ifs"
1403
1404         # compare lmm_seq and lu_fid->f_seq
1405         [ ${lmm[4]} = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1406         # compare lmm_object_id and lu_fid->oid
1407         [ ${lmm[6]} = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1408
1409         echo -e "\tseq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}\n\tstripe count: ${lmm[8]}"
1410
1411         [ "$FSTYPE" != "ldiskfs" ] && skip "can not check trusted.fid FSTYPE=$FSTYPE" && return 0
1412
1413         # check the trusted.fid attribute of the OST objects of the file
1414         for (( i=0, j=19; i < ${lmm[8]}; i++, j+=4 )); do
1415                 local obdidx=${lmm[$j]}
1416                 local devnum=$((obdidx + 1))
1417                 local objid=${lmm[$((j+1))]}
1418                 local group=${lmm[$((j+3))]}
1419                 local dev=$(ostdevname $devnum)
1420                 local dir=${MOUNT%/*}/ost$devnum
1421                 local mntpt=$(facet_mntpt ost$devnum)
1422
1423                 stop ost$devnum
1424                 do_facet ost$devnum mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1425                         { error "mounting $dev as $FSTYPE failed"; return 3; }
1426
1427                 obj_filename=$(do_facet ost$devnum find $dir/O/$group -name $objid)
1428                 local ff=$(do_facet ost$devnum $LL_DECODE_FILTER_FID $obj_filename)
1429                 IFS=$'/= [:]'
1430                 ff=($(echo $ff))
1431                 IFS="$old_ifs"
1432
1433                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1434                 [ ${ff[11]} = ${lmm[4]} ] || { error "parent SEQ mismatch"; return 4; }
1435                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1436                 [ ${ff[12]} = ${lmm[6]} ] || { error "parent OID mismatch"; return 5; }
1437                 let stripe=${ff[13]}
1438                 [ $stripe -eq $i ] || { error "stripe mismatch"; return 6; }
1439
1440                 echo -e "\t\tost $obdidx, objid $objid, group $group"
1441                 do_facet ost$devnum umount -d $mntpt
1442                 start ost$devnum $dev $OST_MOUNT_OPTS
1443         done
1444 }
1445
1446 test_27z() {
1447         mkdir -p $DIR/$tdir
1448         $SETSTRIPE $DIR/$tdir/$tfile-1 -c 1 -o 0 -s 1m ||
1449                 { error "setstripe -c -1 failed"; return 1; }
1450         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1451                 { error "dd 1 mb failed"; return 2; }
1452         $SETSTRIPE $DIR/$tdir/$tfile-2 -c -1 -o $(($OSTCOUNT - 1)) -s 1m ||
1453                 { error "setstripe -c 1 failed"; return 3; }
1454         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1455                 { error "dd $OSTCOUNT mb failed"; return 4; }
1456         sync
1457
1458         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1459         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1460 }
1461 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1462
1463 test_27A() { # b=19102
1464         local restore_size=`$GETSTRIPE -s $MOUNT`
1465         local restore_count=`$GETSTRIPE -c $MOUNT`
1466         local restore_offset=`$GETSTRIPE -o $MOUNT`
1467         $SETSTRIPE -c 0 -o -1 -s 0 $MOUNT
1468         local default_size=`$GETSTRIPE -s $MOUNT`
1469         local default_count=`$GETSTRIPE -c $MOUNT`
1470         local default_offset=`$GETSTRIPE -o $MOUNT`
1471         local dsize=$((1024 * 1024))
1472         [ $default_size -eq $dsize ] || error "stripe size $default_size != $dsize"
1473         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1474         [ $default_offset -eq -1 ] || error "stripe offset $default_offset != -1"
1475         $SETSTRIPE -c $restore_count -o $restore_offset -s $restore_size $MOUNT
1476 }
1477 run_test 27A "check filesystem-wide default LOV EA values"
1478
1479 # createtest also checks that device nodes are created and
1480 # then visible correctly (#2091)
1481 test_28() { # bug 2091
1482         mkdir $DIR/d28
1483         $CREATETEST $DIR/d28/ct || error
1484 }
1485 run_test 28 "create/mknod/mkdir with bad file types ============"
1486
1487 test_29() {
1488         cancel_lru_locks mdc
1489         mkdir $DIR/d29
1490         touch $DIR/d29/foo
1491         log 'first d29'
1492         ls -l $DIR/d29
1493
1494         declare -i LOCKCOUNTORIG=0
1495         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1496                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1497         done
1498         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1499
1500         declare -i LOCKUNUSEDCOUNTORIG=0
1501         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1502                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1503         done
1504
1505         log 'second d29'
1506         ls -l $DIR/d29
1507         log 'done'
1508
1509         declare -i LOCKCOUNTCURRENT=0
1510         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1511                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1512         done
1513
1514         declare -i LOCKUNUSEDCOUNTCURRENT=0
1515         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1516                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1517         done
1518
1519         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1520                 lctl set_param -n ldlm.dump_namespaces ""
1521                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1522                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1523                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1524                 return 2
1525         fi
1526         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1527                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1528                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1529                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1530                 return 3
1531         fi
1532 }
1533 run_test 29 "IT_GETATTR regression  ============================"
1534
1535 test_30a() { # was test_30
1536         cp `which ls` $DIR || cp /bin/ls $DIR
1537         $DIR/ls / || error
1538         rm $DIR/ls
1539 }
1540 run_test 30a "execute binary from Lustre (execve) =============="
1541
1542 test_30b() {
1543         cp `which ls` $DIR || cp /bin/ls $DIR
1544         chmod go+rx $DIR/ls
1545         $RUNAS $DIR/ls / || error
1546         rm $DIR/ls
1547 }
1548 run_test 30b "execute binary from Lustre as non-root ==========="
1549
1550 test_30c() { # b=22376
1551         cp `which ls` $DIR || cp /bin/ls $DIR
1552         chmod a-rw $DIR/ls
1553         cancel_lru_locks mdc
1554         cancel_lru_locks osc
1555         $RUNAS $DIR/ls / || error
1556         rm -f $DIR/ls
1557 }
1558 run_test 30c "execute binary from Lustre without read perms ===="
1559
1560 test_31a() {
1561         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1562         $CHECKSTAT -a $DIR/f31 || error
1563 }
1564 run_test 31a "open-unlink file =================================="
1565
1566 test_31b() {
1567         touch $DIR/f31 || error
1568         ln $DIR/f31 $DIR/f31b || error
1569         multiop $DIR/f31b Ouc || error
1570         $CHECKSTAT -t file $DIR/f31 || error
1571 }
1572 run_test 31b "unlink file with multiple links while open ======="
1573
1574 test_31c() {
1575         touch $DIR/f31 || error
1576         ln $DIR/f31 $DIR/f31c || error
1577         multiop_bg_pause $DIR/f31 O_uc || return 1
1578         MULTIPID=$!
1579         multiop $DIR/f31c Ouc
1580         kill -USR1 $MULTIPID
1581         wait $MULTIPID
1582 }
1583 run_test 31c "open-unlink file with multiple links ============="
1584
1585 test_31d() {
1586         opendirunlink $DIR/d31d $DIR/d31d || error
1587         $CHECKSTAT -a $DIR/d31d || error
1588 }
1589 run_test 31d "remove of open directory ========================="
1590
1591 test_31e() { # bug 2904
1592         check_kernel_version 34 || return 0
1593         openfilleddirunlink $DIR/d31e || error
1594 }
1595 run_test 31e "remove of open non-empty directory ==============="
1596
1597 test_31f() { # bug 4554
1598         set -vx
1599         mkdir $DIR/d31f
1600         $SETSTRIPE $DIR/d31f -s 1048576 -c 1
1601         cp /etc/hosts $DIR/d31f
1602         ls -l $DIR/d31f
1603         $GETSTRIPE $DIR/d31f/hosts
1604         multiop_bg_pause $DIR/d31f D_c || return 1
1605         MULTIPID=$!
1606
1607         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1608         mkdir $DIR/d31f
1609         $SETSTRIPE $DIR/d31f -s 1048576 -c 1
1610         cp /etc/hosts $DIR/d31f
1611         ls -l $DIR/d31f
1612         $GETSTRIPE $DIR/d31f/hosts
1613         multiop_bg_pause $DIR/d31f D_c || return 1
1614         MULTIPID2=$!
1615
1616         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1617         wait $MULTIPID || error "first opendir $MULTIPID failed"
1618
1619         sleep 6
1620
1621         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1622         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1623         set +vx
1624 }
1625 run_test 31f "remove of open directory with open-unlink file ==="
1626
1627 test_31g() {
1628         echo "-- cross directory link --"
1629         mkdir $DIR/d31g{a,b}
1630         touch $DIR/d31ga/f
1631         ln $DIR/d31ga/f $DIR/d31gb/g
1632         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1633         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1634         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1635         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1636 }
1637 run_test 31g "cross directory link==============="
1638
1639 test_31h() {
1640         echo "-- cross directory link --"
1641         mkdir $DIR/d31h
1642         mkdir $DIR/d31h/dir
1643         touch $DIR/d31h/f
1644         ln $DIR/d31h/f $DIR/d31h/dir/g
1645         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1646         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1647         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1648         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1649 }
1650 run_test 31h "cross directory link under child==============="
1651
1652 test_31i() {
1653         echo "-- cross directory link --"
1654         mkdir $DIR/d31i
1655         mkdir $DIR/d31i/dir
1656         touch $DIR/d31i/dir/f
1657         ln $DIR/d31i/dir/f $DIR/d31i/g
1658         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1659         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1660         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1661         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1662 }
1663 run_test 31i "cross directory link under parent==============="
1664
1665
1666 test_31j() {
1667         mkdir $DIR/d31j
1668         mkdir $DIR/d31j/dir1
1669         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1670         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1671         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1672         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1673         return 0
1674 }
1675 run_test 31j "link for directory==============="
1676
1677
1678 test_31k() {
1679         mkdir $DIR/d31k
1680         touch $DIR/d31k/s
1681         touch $DIR/d31k/exist
1682         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1683         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1684         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1685         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1686         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1687         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1688         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1689         return 0
1690 }
1691 run_test 31k "link to file: the same, non-existing, dir==============="
1692
1693 test_31m() {
1694         mkdir $DIR/d31m
1695         touch $DIR/d31m/s
1696         mkdir $DIR/d31m2
1697         touch $DIR/d31m2/exist
1698         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1699         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1700         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1701         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1702         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1703         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1704         return 0
1705 }
1706 run_test 31m "link to file: the same, non-existing, dir==============="
1707
1708 test_32a() {
1709         echo "== more mountpoints and symlinks ================="
1710         [ -e $DIR/d32a ] && rm -fr $DIR/d32a
1711         mkdir -p $DIR/d32a/ext2-mountpoint
1712         mount -t ext2 -o loop $EXT2_DEV $DIR/d32a/ext2-mountpoint || error
1713         $CHECKSTAT -t dir $DIR/d32a/ext2-mountpoint/.. || error
1714         $UMOUNT $DIR/d32a/ext2-mountpoint || error
1715 }
1716 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
1717
1718 test_32b() {
1719         [ -e $DIR/d32b ] && rm -fr $DIR/d32b
1720         mkdir -p $DIR/d32b/ext2-mountpoint
1721         mount -t ext2 -o loop $EXT2_DEV $DIR/d32b/ext2-mountpoint || error
1722         ls -al $DIR/d32b/ext2-mountpoint/.. || error
1723         $UMOUNT $DIR/d32b/ext2-mountpoint || error
1724 }
1725 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
1726
1727 test_32c() {
1728         [ -e $DIR/d32c ] && rm -fr $DIR/d32c
1729         mkdir -p $DIR/d32c/ext2-mountpoint
1730         mount -t ext2 -o loop $EXT2_DEV $DIR/d32c/ext2-mountpoint || error
1731         mkdir -p $DIR/d32c/d2/test_dir
1732         $CHECKSTAT -t dir $DIR/d32c/ext2-mountpoint/../d2/test_dir || error
1733         $UMOUNT $DIR/d32c/ext2-mountpoint || error
1734 }
1735 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
1736
1737 test_32d() {
1738         [ -e $DIR/d32d ] && rm -fr $DIR/d32d
1739         mkdir -p $DIR/d32d/ext2-mountpoint
1740         mount -t ext2 -o loop $EXT2_DEV $DIR/d32d/ext2-mountpoint || error
1741         mkdir -p $DIR/d32d/d2/test_dir
1742         ls -al $DIR/d32d/ext2-mountpoint/../d2/test_dir || error
1743         $UMOUNT $DIR/d32d/ext2-mountpoint || error
1744 }
1745 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
1746
1747 test_32e() {
1748         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
1749         mkdir -p $DIR/d32e/tmp
1750         TMP_DIR=$DIR/d32e/tmp
1751         ln -s $DIR/d32e $TMP_DIR/symlink11
1752         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1753         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
1754         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
1755 }
1756 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
1757
1758 test_32f() {
1759         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
1760         mkdir -p $DIR/d32f/tmp
1761         TMP_DIR=$DIR/d32f/tmp
1762         ln -s $DIR/d32f $TMP_DIR/symlink11
1763         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1764         ls $DIR/d32f/tmp/symlink11  || error
1765         ls $DIR/d32f/symlink01 || error
1766 }
1767 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
1768
1769 test_32g() {
1770         TMP_DIR=$DIR/$tdir/tmp
1771         mkdir -p $TMP_DIR $DIR/${tdir}2
1772         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1773         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1774         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
1775         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
1776         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
1777         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
1778 }
1779 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1780
1781 test_32h() {
1782         rm -fr $DIR/$tdir $DIR/${tdir}2
1783         TMP_DIR=$DIR/$tdir/tmp
1784         mkdir -p $TMP_DIR $DIR/${tdir}2
1785         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1786         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1787         ls $TMP_DIR/symlink12 || error
1788         ls $DIR/$tdir/symlink02  || error
1789 }
1790 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1791
1792 test_32i() {
1793         [ -e $DIR/d32i ] && rm -fr $DIR/d32i
1794         mkdir -p $DIR/d32i/ext2-mountpoint
1795         mount -t ext2 -o loop $EXT2_DEV $DIR/d32i/ext2-mountpoint || error
1796         touch $DIR/d32i/test_file
1797         $CHECKSTAT -t file $DIR/d32i/ext2-mountpoint/../test_file || error
1798         $UMOUNT $DIR/d32i/ext2-mountpoint || error
1799 }
1800 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
1801
1802 test_32j() {
1803         [ -e $DIR/d32j ] && rm -fr $DIR/d32j
1804         mkdir -p $DIR/d32j/ext2-mountpoint
1805         mount -t ext2 -o loop $EXT2_DEV $DIR/d32j/ext2-mountpoint || error
1806         touch $DIR/d32j/test_file
1807         cat $DIR/d32j/ext2-mountpoint/../test_file || error
1808         $UMOUNT $DIR/d32j/ext2-mountpoint || error
1809 }
1810 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
1811
1812 test_32k() {
1813         rm -fr $DIR/d32k
1814         mkdir -p $DIR/d32k/ext2-mountpoint
1815         mount -t ext2 -o loop $EXT2_DEV $DIR/d32k/ext2-mountpoint
1816         mkdir -p $DIR/d32k/d2
1817         touch $DIR/d32k/d2/test_file || error
1818         $CHECKSTAT -t file $DIR/d32k/ext2-mountpoint/../d2/test_file || error
1819         $UMOUNT $DIR/d32k/ext2-mountpoint || error
1820 }
1821 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
1822
1823 test_32l() {
1824         rm -fr $DIR/d32l
1825         mkdir -p $DIR/d32l/ext2-mountpoint
1826         mount -t ext2 -o loop $EXT2_DEV $DIR/d32l/ext2-mountpoint || error
1827         mkdir -p $DIR/d32l/d2
1828         touch $DIR/d32l/d2/test_file
1829         cat  $DIR/d32l/ext2-mountpoint/../d2/test_file || error
1830         $UMOUNT $DIR/d32l/ext2-mountpoint || error
1831 }
1832 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
1833
1834 test_32m() {
1835         rm -fr $DIR/d32m
1836         mkdir -p $DIR/d32m/tmp
1837         TMP_DIR=$DIR/d32m/tmp
1838         ln -s $DIR $TMP_DIR/symlink11
1839         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1840         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
1841         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
1842 }
1843 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
1844
1845 test_32n() {
1846         rm -fr $DIR/d32n
1847         mkdir -p $DIR/d32n/tmp
1848         TMP_DIR=$DIR/d32n/tmp
1849         ln -s $DIR $TMP_DIR/symlink11
1850         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1851         ls -l $DIR/d32n/tmp/symlink11  || error
1852         ls -l $DIR/d32n/symlink01 || error
1853 }
1854 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
1855
1856 test_32o() {
1857         rm -fr $DIR/d32o $DIR/$tfile
1858         touch $DIR/$tfile
1859         mkdir -p $DIR/d32o/tmp
1860         TMP_DIR=$DIR/d32o/tmp
1861         ln -s $DIR/$tfile $TMP_DIR/symlink12
1862         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1863         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
1864         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
1865         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
1866         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
1867 }
1868 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
1869
1870 test_32p() {
1871     log 32p_1
1872         rm -fr $DIR/d32p
1873     log 32p_2
1874         rm -f $DIR/$tfile
1875     log 32p_3
1876         touch $DIR/$tfile
1877     log 32p_4
1878         mkdir -p $DIR/d32p/tmp
1879     log 32p_5
1880         TMP_DIR=$DIR/d32p/tmp
1881     log 32p_6
1882         ln -s $DIR/$tfile $TMP_DIR/symlink12
1883     log 32p_7
1884         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1885     log 32p_8
1886         cat $DIR/d32p/tmp/symlink12 || error
1887     log 32p_9
1888         cat $DIR/d32p/symlink02 || error
1889     log 32p_10
1890 }
1891 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
1892
1893 test_32q() {
1894         [ -e $DIR/d32q ] && rm -fr $DIR/d32q
1895         mkdir -p $DIR/d32q
1896         touch $DIR/d32q/under_the_mount
1897         mount -t ext2 -o loop $EXT2_DEV $DIR/d32q
1898         ls $DIR/d32q/under_the_mount && error || true
1899         $UMOUNT $DIR/d32q || error
1900 }
1901 run_test 32q "stat follows mountpoints in Lustre (should return error)"
1902
1903 test_32r() {
1904         [ -e $DIR/d32r ] && rm -fr $DIR/d32r
1905         mkdir -p $DIR/d32r
1906         touch $DIR/d32r/under_the_mount
1907         mount -t ext2 -o loop $EXT2_DEV $DIR/d32r
1908         ls $DIR/d32r | grep -q under_the_mount && error || true
1909         $UMOUNT $DIR/d32r || error
1910 }
1911 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
1912
1913 test_33() {
1914         rm -f $DIR/$tfile
1915         touch $DIR/$tfile
1916         chmod 444 $DIR/$tfile
1917         chown $RUNAS_ID $DIR/$tfile
1918         log 33_1
1919         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
1920         log 33_2
1921 }
1922 run_test 33 "write file with mode 444 (should return error) ===="
1923
1924 test_33a() {
1925         rm -fr $DIR/d33
1926         mkdir -p $DIR/d33
1927         chown $RUNAS_ID $DIR/d33
1928         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
1929         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
1930                 error "open RDWR" || true
1931 }
1932 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
1933
1934 test_33b() {
1935         rm -fr $DIR/d33
1936         mkdir -p $DIR/d33
1937         chown $RUNAS_ID $DIR/d33
1938         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
1939 }
1940 run_test 33b "test open file with malformed flags (No panic and return error)"
1941
1942 test_33c() {
1943         local ostnum
1944         local ostname
1945         local write_bytes
1946         local all_zeros
1947
1948         all_zeros=:
1949         rm -fr $DIR/d33
1950         mkdir -p $DIR/d33
1951         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
1952
1953         sync
1954         for ostnum in $(seq $OSTCOUNT); do
1955                 # test-framework's OST numbering is one-based, while Lustre's
1956                 # is zero-based
1957                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
1958                 # Parsing llobdstat's output sucks; we could grep the /proc
1959                 # path, but that's likely to not be as portable as using the
1960                 # llobdstat utility.  So we parse lctl output instead.
1961                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
1962                         obdfilter/$ostname/stats |
1963                         awk '/^write_bytes/ {print $7}' )
1964                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
1965                 if (( ${write_bytes:-0} > 0 ))
1966                 then
1967                         all_zeros=false
1968                         break;
1969                 fi
1970         done
1971
1972         $all_zeros || return 0
1973
1974         # Write four bytes
1975         echo foo > $DIR/d33/bar
1976         # Really write them
1977         sync
1978
1979         # Total up write_bytes after writing.  We'd better find non-zeros.
1980         for ostnum in $(seq $OSTCOUNT); do
1981                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
1982                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
1983                         obdfilter/$ostname/stats |
1984                         awk '/^write_bytes/ {print $7}' )
1985                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
1986                 if (( ${write_bytes:-0} > 0 ))
1987                 then
1988                         all_zeros=false
1989                         break;
1990                 fi
1991         done
1992
1993         if $all_zeros
1994         then
1995                 for ostnum in $(seq $OSTCOUNT); do
1996                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
1997                         echo "Check that write_bytes is present in obdfilter/*/stats:"
1998                         do_facet ost$ostnum lctl get_param -n \
1999                                 obdfilter/$ostname/stats
2000                 done
2001                 error "OST not keeping write_bytes stats (b22312)"
2002         fi
2003 }
2004 run_test 33c "test llobdstat and write_bytes"
2005
2006 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2007 test_34a() {
2008         rm -f $DIR/f34
2009         $MCREATE $DIR/f34 || error
2010         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2011         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2012         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2013         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2014 }
2015 run_test 34a "truncate file that has not been opened ==========="
2016
2017 test_34b() {
2018         [ ! -f $DIR/f34 ] && test_34a
2019         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2020         $OPENFILE -f O_RDONLY $DIR/f34
2021         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2022         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2023 }
2024 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2025
2026 test_34c() {
2027         [ ! -f $DIR/f34 ] && test_34a
2028         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2029         $OPENFILE -f O_RDWR $DIR/f34
2030         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2031         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2032 }
2033 run_test 34c "O_RDWR opening file-with-size works =============="
2034
2035 test_34d() {
2036         [ ! -f $DIR/f34 ] && test_34a
2037         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2038         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2039         rm $DIR/f34
2040 }
2041 run_test 34d "write to sparse file ============================="
2042
2043 test_34e() {
2044         rm -f $DIR/f34e
2045         $MCREATE $DIR/f34e || error
2046         $TRUNCATE $DIR/f34e 1000 || error
2047         $CHECKSTAT -s 1000 $DIR/f34e || error
2048         $OPENFILE -f O_RDWR $DIR/f34e
2049         $CHECKSTAT -s 1000 $DIR/f34e || error
2050 }
2051 run_test 34e "create objects, some with size and some without =="
2052
2053 test_34f() { # bug 6242, 6243
2054         SIZE34F=48000
2055         rm -f $DIR/f34f
2056         $MCREATE $DIR/f34f || error
2057         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2058         dd if=$DIR/f34f of=$TMP/f34f
2059         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2060         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2061         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2062         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2063         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2064 }
2065 run_test 34f "read from a file with no objects until EOF ======="
2066
2067 test_34g() {
2068         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2069         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2070         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2071         cancel_lru_locks osc
2072         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2073                 error "wrong size after lock cancel"
2074
2075         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2076         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2077                 error "expanding truncate failed"
2078         cancel_lru_locks osc
2079         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2080                 error "wrong expanded size after lock cancel"
2081 }
2082 run_test 34g "truncate long file ==============================="
2083
2084 test_35a() {
2085         cp /bin/sh $DIR/f35a
2086         chmod 444 $DIR/f35a
2087         chown $RUNAS_ID $DIR/f35a
2088         $RUNAS $DIR/f35a && error || true
2089         rm $DIR/f35a
2090 }
2091 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2092
2093 test_36a() {
2094         rm -f $DIR/f36
2095         utime $DIR/f36 || error
2096 }
2097 run_test 36a "MDS utime check (mknod, utime) ==================="
2098
2099 test_36b() {
2100         echo "" > $DIR/f36
2101         utime $DIR/f36 || error
2102 }
2103 run_test 36b "OST utime check (open, utime) ===================="
2104
2105 test_36c() {
2106         rm -f $DIR/d36/f36
2107         mkdir $DIR/d36
2108         chown $RUNAS_ID $DIR/d36
2109         $RUNAS utime $DIR/d36/f36 || error
2110 }
2111 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2112
2113 test_36d() {
2114         [ ! -d $DIR/d36 ] && test_36c
2115         echo "" > $DIR/d36/f36
2116         $RUNAS utime $DIR/d36/f36 || error
2117 }
2118 run_test 36d "non-root OST utime check (open, utime) ==========="
2119
2120 test_36e() {
2121         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2122         mkdir -p $DIR/$tdir
2123         touch $DIR/$tdir/$tfile
2124         $RUNAS utime $DIR/$tdir/$tfile && \
2125                 error "utime worked, expected failure" || true
2126 }
2127 run_test 36e "utime on non-owned file (should return error) ===="
2128
2129 subr_36fh() {
2130         local fl="$1"
2131         local LANG_SAVE=$LANG
2132         local LC_LANG_SAVE=$LC_LANG
2133         export LANG=C LC_LANG=C # for date language
2134
2135         DATESTR="Dec 20  2000"
2136         mkdir -p $DIR/$tdir
2137         lctl set_param fail_loc=$fl
2138         date; date +%s
2139         cp /etc/hosts $DIR/$tdir/$tfile
2140         sync & # write RPC generated with "current" inode timestamp, but delayed
2141         sleep 1
2142         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2143         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2144         cancel_lru_locks osc
2145         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2146         date; date +%s
2147         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2148                 echo "BEFORE: $LS_BEFORE" && \
2149                 echo "AFTER : $LS_AFTER" && \
2150                 echo "WANT  : $DATESTR" && \
2151                 error "$DIR/$tdir/$tfile timestamps changed" || true
2152
2153         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2154 }
2155
2156 test_36f() {
2157         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2158         subr_36fh "0x80000214"
2159 }
2160 run_test 36f "utime on file racing with OST BRW write =========="
2161
2162 test_36g() {
2163         remote_ost_nodsh && skip "remote OST with nodsh" && return
2164         local fmd_max_age
2165         local fmd_before
2166         local fmd_after
2167
2168         mkdir -p $DIR/$tdir
2169         fmd_max_age=$(do_facet ost1 \
2170                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2171                 head -n 1")
2172
2173         fmd_before=$(do_facet ost1 \
2174                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2175         touch $DIR/$tdir/$tfile
2176         sleep $((fmd_max_age + 12))
2177         fmd_after=$(do_facet ost1 \
2178                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2179
2180         echo "fmd_before: $fmd_before"
2181         echo "fmd_after: $fmd_after"
2182         [ "$fmd_after" -gt "$fmd_before" ] && \
2183                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2184                 error "fmd didn't expire after ping" || true
2185 }
2186 run_test 36g "filter mod data cache expiry ====================="
2187
2188 test_36h() {
2189         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2190         subr_36fh "0x80000227"
2191 }
2192 run_test 36h "utime on file racing with OST BRW write =========="
2193
2194 test_37() {
2195         mkdir -p $DIR/$tdir
2196         echo f > $DIR/$tdir/fbugfile
2197         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2198         ls $DIR/$tdir | grep "\<fbugfile\>" && error
2199         $UMOUNT $DIR/$tdir || error
2200         rm -f $DIR/$tdir/fbugfile || error
2201 }
2202 run_test 37 "ls a mounted file system to check old content ====="
2203
2204 test_38() {
2205         local file=$DIR/$tfile
2206         touch $file
2207         openfile -f O_DIRECTORY $file
2208         local RC=$?
2209         local ENOTDIR=20
2210         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2211         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2212 }
2213 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2214
2215 test_39() {
2216         touch $DIR/$tfile
2217         touch $DIR/${tfile}2
2218 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2219 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2220 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2221         sleep 2
2222         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2223         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2224                 echo "mtime"
2225                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2226                 echo "atime"
2227                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2228                 echo "ctime"
2229                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2230                 error "O_TRUNC didn't change timestamps"
2231         fi
2232 }
2233 run_test 39 "mtime changed on create ==========================="
2234
2235 test_39b() {
2236         mkdir -p $DIR/$tdir
2237         cp -p /etc/passwd $DIR/$tdir/fopen
2238         cp -p /etc/passwd $DIR/$tdir/flink
2239         cp -p /etc/passwd $DIR/$tdir/funlink
2240         cp -p /etc/passwd $DIR/$tdir/frename
2241         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2242
2243         sleep 1
2244         echo "aaaaaa" >> $DIR/$tdir/fopen
2245         echo "aaaaaa" >> $DIR/$tdir/flink
2246         echo "aaaaaa" >> $DIR/$tdir/funlink
2247         echo "aaaaaa" >> $DIR/$tdir/frename
2248
2249         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2250         local link_new=`stat -c %Y $DIR/$tdir/flink`
2251         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2252         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2253
2254         cat $DIR/$tdir/fopen > /dev/null
2255         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2256         rm -f $DIR/$tdir/funlink2
2257         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2258
2259         for (( i=0; i < 2; i++ )) ; do
2260                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2261                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2262                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2263                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2264
2265                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2266                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2267                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2268                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2269
2270                 cancel_lru_locks osc
2271                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2272         done
2273 }
2274 run_test 39b "mtime change on open, link, unlink, rename  ======"
2275
2276 # this should be set to past
2277 TEST_39_MTIME=`date -d "1 year ago" +%s`
2278
2279 # bug 11063
2280 test_39c() {
2281         touch $DIR1/$tfile
2282         sleep 2
2283         local mtime0=`stat -c %Y $DIR1/$tfile`
2284
2285         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2286         local mtime1=`stat -c %Y $DIR1/$tfile`
2287         [ "$mtime1" = $TEST_39_MTIME ] || \
2288                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2289
2290         local d1=`date +%s`
2291         echo hello >> $DIR1/$tfile
2292         local d2=`date +%s`
2293         local mtime2=`stat -c %Y $DIR1/$tfile`
2294         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2295                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2296
2297         mv $DIR1/$tfile $DIR1/$tfile-1
2298
2299         for (( i=0; i < 2; i++ )) ; do
2300                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2301                 [ "$mtime2" = "$mtime3" ] || \
2302                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2303
2304                 cancel_lru_locks osc
2305                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2306         done
2307 }
2308 run_test 39c "mtime change on rename ==========================="
2309
2310 # bug 21114
2311 test_39d() {
2312         touch $DIR1/$tfile
2313
2314         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2315
2316         for (( i=0; i < 2; i++ )) ; do
2317                 local mtime=`stat -c %Y $DIR1/$tfile`
2318                 [ $mtime = $TEST_39_MTIME ] || \
2319                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2320
2321                 cancel_lru_locks osc
2322                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2323         done
2324 }
2325 run_test 39d "create, utime, stat =============================="
2326
2327 # bug 21114
2328 test_39e() {
2329         touch $DIR1/$tfile
2330         local mtime1=`stat -c %Y $DIR1/$tfile`
2331
2332         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2333
2334         for (( i=0; i < 2; i++ )) ; do
2335                 local mtime2=`stat -c %Y $DIR1/$tfile`
2336                 [ $mtime2 = $TEST_39_MTIME ] || \
2337                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2338
2339                 cancel_lru_locks osc
2340                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2341         done
2342 }
2343 run_test 39e "create, stat, utime, stat ========================"
2344
2345 # bug 21114
2346 test_39f() {
2347         touch $DIR1/$tfile
2348         mtime1=`stat -c %Y $DIR1/$tfile`
2349
2350         sleep 2
2351         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2352
2353         for (( i=0; i < 2; i++ )) ; do
2354                 local mtime2=`stat -c %Y $DIR1/$tfile`
2355                 [ $mtime2 = $TEST_39_MTIME ] || \
2356                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2357
2358                 cancel_lru_locks osc
2359                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2360         done
2361 }
2362 run_test 39f "create, stat, sleep, utime, stat ================="
2363
2364 # bug 11063
2365 test_39g() {
2366         echo hello >> $DIR1/$tfile
2367         local mtime1=`stat -c %Y $DIR1/$tfile`
2368
2369         sleep 2
2370         chmod o+r $DIR1/$tfile
2371
2372         for (( i=0; i < 2; i++ )) ; do
2373                 local mtime2=`stat -c %Y $DIR1/$tfile`
2374                 [ "$mtime1" = "$mtime2" ] || \
2375                         error "lost mtime: $mtime2, should be $mtime1"
2376
2377                 cancel_lru_locks osc
2378                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2379         done
2380 }
2381 run_test 39g "write, chmod, stat ==============================="
2382
2383 # bug 11063
2384 test_39h() {
2385         touch $DIR1/$tfile
2386         sleep 1
2387
2388         local d1=`date`
2389         echo hello >> $DIR1/$tfile
2390         local mtime1=`stat -c %Y $DIR1/$tfile`
2391
2392         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2393         local d2=`date`
2394         if [ "$d1" != "$d2" ]; then
2395                 echo "write and touch not within one second"
2396         else
2397                 for (( i=0; i < 2; i++ )) ; do
2398                         local mtime2=`stat -c %Y $DIR1/$tfile`
2399                         [ "$mtime2" = $TEST_39_MTIME ] || \
2400                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2401
2402                         cancel_lru_locks osc
2403                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2404                 done
2405         fi
2406 }
2407 run_test 39h "write, utime within one second, stat ============="
2408
2409 test_39i() {
2410         touch $DIR1/$tfile
2411         sleep 1
2412
2413         echo hello >> $DIR1/$tfile
2414         local mtime1=`stat -c %Y $DIR1/$tfile`
2415
2416         mv $DIR1/$tfile $DIR1/$tfile-1
2417
2418         for (( i=0; i < 2; i++ )) ; do
2419                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2420
2421                 [ "$mtime1" = "$mtime2" ] || \
2422                         error "lost mtime: $mtime2, should be $mtime1"
2423
2424                 cancel_lru_locks osc
2425                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2426         done
2427 }
2428 run_test 39i "write, rename, stat =============================="
2429
2430 test_39j() {
2431         touch $DIR1/$tfile
2432         sleep 1
2433
2434         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2435         local multipid=$!
2436         local mtime1=`stat -c %Y $DIR1/$tfile`
2437
2438         mv $DIR1/$tfile $DIR1/$tfile-1
2439
2440         kill -USR1 $multipid
2441         wait $multipid || error "multiop close failed"
2442
2443         for (( i=0; i < 2; i++ )) ; do
2444                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2445                 [ "$mtime1" = "$mtime2" ] || \
2446                         error "mtime is lost on close: $mtime2, should be $mtime1"
2447
2448                 cancel_lru_locks osc
2449                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2450         done
2451 }
2452 run_test 39j "write, rename, close, stat ======================="
2453
2454 test_39k() {
2455         touch $DIR1/$tfile
2456         sleep 1
2457
2458         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2459         local multipid=$!
2460         local mtime1=`stat -c %Y $DIR1/$tfile`
2461
2462         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2463
2464         kill -USR1 $multipid
2465         wait $multipid || error "multiop close failed"
2466
2467         for (( i=0; i < 2; i++ )) ; do
2468                 local mtime2=`stat -c %Y $DIR1/$tfile`
2469
2470                 [ "$mtime2" = $TEST_39_MTIME ] || \
2471                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2472
2473                 cancel_lru_locks osc
2474                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2475         done
2476 }
2477 run_test 39k "write, utime, close, stat ========================"
2478
2479 # this should be set to future
2480 TEST_39_ATIME=`date -d "1 year" +%s`
2481
2482 test_39l() {
2483         local atime_diff=$(do_facet $SINGLEMDS lctl get_param -n mdd.*.atime_diff)
2484
2485         mkdir -p $DIR/$tdir
2486
2487         # test setting directory atime to future
2488         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2489         local atime=$(stat -c %X $DIR/$tdir)
2490         [ "$atime" = $TEST_39_ATIME ] || \
2491                 error "atime is not set to future: $atime, should be $TEST_39_ATIME"
2492
2493         # test setting directory atime from future to now
2494         local d1=$(date +%s)
2495         ls $DIR/$tdir
2496         local d2=$(date +%s)
2497
2498         cancel_lru_locks mdc
2499         atime=$(stat -c %X $DIR/$tdir)
2500         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2501                 error "atime is not updated from future: $atime, should be $d1<atime<$d2"
2502
2503         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=2
2504         sleep 3
2505
2506         # test setting directory atime when now > dir atime + atime_diff
2507         d1=$(date +%s)
2508         ls $DIR/$tdir
2509         d2=$(date +%s)
2510         cancel_lru_locks mdc
2511         atime=$(stat -c %X $DIR/$tdir)
2512         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2513                 error "atime is not updated  : $atime, should be $d2"
2514
2515         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=60
2516         sleep 3
2517
2518         # test not setting directory atime when now < dir atime + atime_diff
2519         ls $DIR/$tdir
2520         cancel_lru_locks mdc
2521         atime=$(stat -c %X $DIR/$tdir)
2522         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2523                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2524
2525         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=$atime_diff
2526 }
2527 run_test 39l "directory atime update ==========================="
2528
2529 test_39m() {
2530         touch $DIR1/$tfile
2531         sleep 2
2532         local far_past_mtime=$(date -d "May 29 1953" +%s)
2533         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2534
2535         touch -m -d @$far_past_mtime $DIR1/$tfile
2536         touch -a -d @$far_past_atime $DIR1/$tfile
2537
2538         for (( i=0; i < 2; i++ )) ; do
2539                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2540                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2541                         error "atime or mtime set incorrectly"
2542
2543                 cancel_lru_locks osc
2544                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2545         done
2546 }
2547 run_test 39m "test atime and mtime before 1970"
2548
2549 test_40() {
2550         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2551         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2552         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2553 }
2554 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2555
2556 test_41() {
2557         # bug 1553
2558         small_write $DIR/f41 18
2559 }
2560 run_test 41 "test small file write + fstat ====================="
2561
2562 count_ost_writes() {
2563         lctl get_param -n osc.*.stats |
2564             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2565 }
2566
2567 # decent default
2568 WRITEBACK_SAVE=500
2569 DIRTY_RATIO_SAVE=40
2570 MAX_DIRTY_RATIO=50
2571 BG_DIRTY_RATIO_SAVE=10
2572 MAX_BG_DIRTY_RATIO=25
2573
2574 start_writeback() {
2575         trap 0
2576         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2577         # dirty_ratio, dirty_background_ratio
2578         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2579                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2580                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2581                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2582         else
2583                 # if file not here, we are a 2.4 kernel
2584                 kill -CONT `pidof kupdated`
2585         fi
2586 }
2587
2588 stop_writeback() {
2589         # setup the trap first, so someone cannot exit the test at the
2590         # exact wrong time and mess up a machine
2591         trap start_writeback EXIT
2592         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2593         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2594                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2595                 sysctl -w vm.dirty_writeback_centisecs=0
2596                 sysctl -w vm.dirty_writeback_centisecs=0
2597                 # save and increase /proc/sys/vm/dirty_ratio
2598                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2599                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2600                 # save and increase /proc/sys/vm/dirty_background_ratio
2601                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2602                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2603         else
2604                 # if file not here, we are a 2.4 kernel
2605                 kill -STOP `pidof kupdated`
2606         fi
2607 }
2608
2609 # ensure that all stripes have some grant before we test client-side cache
2610 setup_test42() {
2611         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2612                 dd if=/dev/zero of=$i bs=4k count=1
2613                 rm $i
2614         done
2615 }
2616
2617 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
2618 # file truncation, and file removal.
2619 test_42a() {
2620         setup_test42
2621         cancel_lru_locks osc
2622         stop_writeback
2623         sync; sleep 1; sync # just to be safe
2624         BEFOREWRITES=`count_ost_writes`
2625         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
2626         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
2627         AFTERWRITES=`count_ost_writes`
2628         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2629                 error "$BEFOREWRITES < $AFTERWRITES"
2630         start_writeback
2631 }
2632 run_test 42a "ensure that we don't flush on close =============="
2633
2634 test_42b() {
2635         setup_test42
2636         cancel_lru_locks osc
2637         stop_writeback
2638         sync
2639         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
2640         BEFOREWRITES=`count_ost_writes`
2641         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
2642         AFTERWRITES=`count_ost_writes`
2643         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2644                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
2645         fi
2646         BEFOREWRITES=`count_ost_writes`
2647         sync || error "sync: $?"
2648         AFTERWRITES=`count_ost_writes`
2649         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2650                 error "$BEFOREWRITES < $AFTERWRITES on sync"
2651         fi
2652         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
2653         start_writeback
2654         return 0
2655 }
2656 run_test 42b "test destroy of file with cached dirty data ======"
2657
2658 # if these tests just want to test the effect of truncation,
2659 # they have to be very careful.  consider:
2660 # - the first open gets a {0,EOF}PR lock
2661 # - the first write conflicts and gets a {0, count-1}PW
2662 # - the rest of the writes are under {count,EOF}PW
2663 # - the open for truncate tries to match a {0,EOF}PR
2664 #   for the filesize and cancels the PWs.
2665 # any number of fixes (don't get {0,EOF} on open, match
2666 # composite locks, do smarter file size management) fix
2667 # this, but for now we want these tests to verify that
2668 # the cancellation with truncate intent works, so we
2669 # start the file with a full-file pw lock to match against
2670 # until the truncate.
2671 trunc_test() {
2672         test=$1
2673         file=$DIR/$test
2674         offset=$2
2675         cancel_lru_locks osc
2676         stop_writeback
2677         # prime the file with 0,EOF PW to match
2678         touch $file
2679         $TRUNCATE $file 0
2680         sync; sync
2681         # now the real test..
2682         dd if=/dev/zero of=$file bs=1024 count=100
2683         BEFOREWRITES=`count_ost_writes`
2684         $TRUNCATE $file $offset
2685         cancel_lru_locks osc
2686         AFTERWRITES=`count_ost_writes`
2687         start_writeback
2688 }
2689
2690 test_42c() {
2691         trunc_test 42c 1024
2692         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
2693             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
2694         rm $file
2695 }
2696 run_test 42c "test partial truncate of file with cached dirty data"
2697
2698 test_42d() {
2699         trunc_test 42d 0
2700         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2701             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
2702         rm $file
2703 }
2704 run_test 42d "test complete truncate of file with cached dirty data"
2705
2706 test_42e() { # bug22074
2707         local TDIR=$DIR/${tdir}e
2708         local pagesz=$(page_size)
2709         local pages=16
2710         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
2711         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
2712         local max_dirty_mb
2713         local warmup_files
2714
2715         mkdir -p $TDIR
2716         $LFS setstripe -c 1 $TDIR
2717         createmany -o $TDIR/f $files
2718
2719         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
2720
2721         # we assume that with $OSTCOUNT files, at least one of them will
2722         # be allocated on OST0.
2723         warmup_files=$((OSTCOUNT * max_dirty_mb))
2724         createmany -o $TDIR/w $warmup_files
2725
2726         # write a large amount of data into one file and sync, to get good
2727         # avail_grant number from OST.
2728         for ((i=0; i<$warmup_files; i++)); do
2729                 idx=$($LFS getstripe -i $TDIR/w$i)
2730                 [ $idx -ne 0 ] && continue
2731                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
2732                 break
2733         done
2734         [ $i -gt $warmup_files ] && error "OST0 is still cold"
2735         sync
2736         $LCTL get_param $proc_osc0/cur_dirty_bytes
2737         $LCTL get_param $proc_osc0/cur_grant_bytes
2738
2739         # create as much dirty pages as we can while not to trigger the actual
2740         # RPCs directly. but depends on the env, VFS may trigger flush during this
2741         # period, hopefully we are good.
2742         for ((i=0; i<$warmup_files; i++)); do
2743                 idx=$($LFS getstripe -i $TDIR/w$i)
2744                 [ $idx -ne 0 ] && continue
2745                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
2746         done
2747         $LCTL get_param $proc_osc0/cur_dirty_bytes
2748         $LCTL get_param $proc_osc0/cur_grant_bytes
2749
2750         # perform the real test
2751         $LCTL set_param $proc_osc0/rpc_stats 0
2752         for ((;i<$files; i++)); do
2753                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
2754                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
2755         done
2756         sync
2757         $LCTL get_param $proc_osc0/rpc_stats
2758
2759         $LCTL get_param $proc_osc0/rpc_stats |
2760                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
2761                         [ "$PPR" != "16:" ] && continue
2762                         [ $WPCT -lt 85 ] && error "$pages-page write RPCs only $WPCT% < 85%"
2763                         break # we only want the "pages per rpc" stat
2764                 done
2765         rm -rf $TDIR
2766 }
2767 run_test 42e "verify sub-RPC writes are not done synchronously"
2768
2769 test_43() {
2770         mkdir -p $DIR/$tdir
2771         cp -p /bin/ls $DIR/$tdir/$tfile
2772         multiop $DIR/$tdir/$tfile Ow_c &
2773         pid=$!
2774         # give multiop a chance to open
2775         sleep 1
2776
2777         $DIR/$tdir/$tfile && error || true
2778         kill -USR1 $pid
2779 }
2780 run_test 43 "execution of file opened for write should return -ETXTBSY"
2781
2782 test_43a() {
2783         mkdir -p $DIR/d43
2784         cp -p `which multiop` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2785         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2786         MULTIOP_PID=$!
2787         multiop $DIR/d43/multiop Oc && error "expected error, got success"
2788         kill -USR1 $MULTIOP_PID || return 2
2789         wait $MULTIOP_PID || return 3
2790         rm $TMP/test43.junk
2791 }
2792 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
2793
2794 test_43b() {
2795         mkdir -p $DIR/d43
2796         cp -p `which multiop` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2797         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2798         MULTIOP_PID=$!
2799         $TRUNCATE $DIR/d43/multiop 0 && error "expected error, got success"
2800         kill -USR1 $MULTIOP_PID || return 2
2801         wait $MULTIOP_PID || return 3
2802         rm $TMP/test43.junk
2803 }
2804 run_test 43b "truncate of file being executed should return -ETXTBSY"
2805
2806 test_43c() {
2807         local testdir="$DIR/d43c"
2808         mkdir -p $testdir
2809         cp $SHELL $testdir/
2810         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
2811                 ( cd $testdir && md5sum -c)
2812 }
2813 run_test 43c "md5sum of copy into lustre========================"
2814
2815 test_44() {
2816         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
2817         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
2818         dd if=$DIR/f1 bs=4k count=1 > /dev/null
2819 }
2820 run_test 44 "zero length read from a sparse stripe ============="
2821
2822 test_44a() {
2823     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
2824                          awk '{print $2}'`
2825     [ -z "$nstripe" ] && skip "can't get stripe info" && return
2826     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
2827     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
2828                       awk '{print $2}'`
2829     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
2830         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
2831     fi
2832
2833     OFFSETS="0 $((stride/2)) $((stride-1))"
2834     for offset in $OFFSETS ; do
2835       for i in `seq 0 $((nstripe-1))`; do
2836         local GLOBALOFFSETS=""
2837         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
2838         local myfn=$DIR/d44a-$size
2839         echo "--------writing $myfn at $size"
2840         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
2841         GLOBALOFFSETS="$GLOBALOFFSETS $size"
2842         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2843                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2844
2845         for j in `seq 0 $((nstripe-1))`; do
2846             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
2847             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
2848             GLOBALOFFSETS="$GLOBALOFFSETS $size"
2849         done
2850         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2851                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2852         rm -f $myfn
2853       done
2854     done
2855 }
2856 run_test 44a "test sparse pwrite ==============================="
2857
2858 dirty_osc_total() {
2859         tot=0
2860         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
2861                 tot=$(($tot + $d))
2862         done
2863         echo $tot
2864 }
2865 do_dirty_record() {
2866         before=`dirty_osc_total`
2867         echo executing "\"$*\""
2868         eval $*
2869         after=`dirty_osc_total`
2870         echo before $before, after $after
2871 }
2872 test_45() {
2873         f="$DIR/f45"
2874         # Obtain grants from OST if it supports it
2875         echo blah > ${f}_grant
2876         stop_writeback
2877         sync
2878         do_dirty_record "echo blah > $f"
2879         [ $before -eq $after ] && error "write wasn't cached"
2880         do_dirty_record "> $f"
2881         [ $before -gt $after ] || error "truncate didn't lower dirty count"
2882         do_dirty_record "echo blah > $f"
2883         [ $before -eq $after ] && error "write wasn't cached"
2884         do_dirty_record "sync"
2885         [ $before -gt $after ] || error "writeback didn't lower dirty count"
2886         do_dirty_record "echo blah > $f"
2887         [ $before -eq $after ] && error "write wasn't cached"
2888         do_dirty_record "cancel_lru_locks osc"
2889         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
2890         start_writeback
2891 }
2892 run_test 45 "osc io page accounting ============================"
2893
2894 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
2895 # test tickles a bug where re-dirtying a page was failing to be mapped to the
2896 # objects offset and an assert hit when an rpc was built with 1023's mapped
2897 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
2898 test_46() {
2899         f="$DIR/f46"
2900         stop_writeback
2901         sync
2902         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2903         sync
2904         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
2905         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2906         sync
2907         start_writeback
2908 }
2909 run_test 46 "dirtying a previously written page ================"
2910
2911 # test_47 is removed "Device nodes check" is moved to test_28
2912
2913 test_48a() { # bug 2399
2914         check_kernel_version 34 || return 0
2915         mkdir -p $DIR/d48a
2916         cd $DIR/d48a
2917         mv $DIR/d48a $DIR/d48.new || error "move directory failed"
2918         mkdir $DIR/d48a || error "recreate directory failed"
2919         touch foo || error "'touch foo' failed after recreating cwd"
2920         mkdir bar || error "'mkdir foo' failed after recreating cwd"
2921         if check_kernel_version 44; then
2922                 touch .foo || error "'touch .foo' failed after recreating cwd"
2923                 mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
2924         fi
2925         ls . > /dev/null || error "'ls .' failed after recreating cwd"
2926         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
2927         cd . || error "'cd .' failed after recreating cwd"
2928         mkdir . && error "'mkdir .' worked after recreating cwd"
2929         rmdir . && error "'rmdir .' worked after recreating cwd"
2930         ln -s . baz || error "'ln -s .' failed after recreating cwd"
2931         cd .. || error "'cd ..' failed after recreating cwd"
2932 }
2933 run_test 48a "Access renamed working dir (should return errors)="
2934
2935 test_48b() { # bug 2399
2936         check_kernel_version 34 || return 0
2937         mkdir -p $DIR/d48b
2938         cd $DIR/d48b
2939         rmdir $DIR/d48b || error "remove cwd $DIR/d48b failed"
2940         touch foo && error "'touch foo' worked after removing cwd"
2941         mkdir foo && error "'mkdir foo' worked after removing cwd"
2942         if check_kernel_version 44; then
2943                 touch .foo && error "'touch .foo' worked after removing cwd"
2944                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
2945         fi
2946         ls . > /dev/null && error "'ls .' worked after removing cwd"
2947         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
2948         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
2949         mkdir . && error "'mkdir .' worked after removing cwd"
2950         rmdir . && error "'rmdir .' worked after removing cwd"
2951         ln -s . foo && error "'ln -s .' worked after removing cwd"
2952         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
2953 }
2954 run_test 48b "Access removed working dir (should return errors)="
2955
2956 test_48c() { # bug 2350
2957         check_kernel_version 36 || return 0
2958         #lctl set_param debug=-1
2959         #set -vx
2960         mkdir -p $DIR/d48c/dir
2961         cd $DIR/d48c/dir
2962         $TRACE rmdir $DIR/d48c/dir || error "remove cwd $DIR/d48c/dir failed"
2963         $TRACE touch foo && error "'touch foo' worked after removing cwd"
2964         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
2965         if check_kernel_version 44; then
2966                 touch .foo && error "'touch .foo' worked after removing cwd"
2967                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
2968         fi
2969         $TRACE ls . && error "'ls .' worked after removing cwd"
2970         $TRACE ls .. || error "'ls ..' failed after removing cwd"
2971         is_patchless || ( $TRACE cd . && error "'cd .' worked after removing cwd" )
2972         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
2973         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
2974         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
2975         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
2976 }
2977 run_test 48c "Access removed working subdir (should return errors)"
2978
2979 test_48d() { # bug 2350
2980         check_kernel_version 36 || return 0
2981         #lctl set_param debug=-1
2982         #set -vx
2983         mkdir -p $DIR/d48d/dir
2984         cd $DIR/d48d/dir
2985         $TRACE rmdir $DIR/d48d/dir || error "remove cwd $DIR/d48d/dir failed"
2986         $TRACE rmdir $DIR/d48d || error "remove parent $DIR/d48d failed"
2987         $TRACE touch foo && error "'touch foo' worked after removing parent"
2988         $TRACE mkdir foo && error "'mkdir foo' worked after removing parent"
2989         if check_kernel_version 44; then
2990                 touch .foo && error "'touch .foo' worked after removing parent"
2991                 mkdir .foo && error "'mkdir .foo' worked after removing parent"
2992         fi
2993         $TRACE ls . && error "'ls .' worked after removing parent"
2994         $TRACE ls .. && error "'ls ..' worked after removing parent"
2995         is_patchless || ( $TRACE cd . && error "'cd .' worked after recreate parent" )
2996         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
2997         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
2998         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
2999         is_patchless || ( $TRACE cd .. && error "'cd ..' worked after removing parent" || true )
3000 }
3001 run_test 48d "Access removed parent subdir (should return errors)"
3002
3003 test_48e() { # bug 4134
3004         check_kernel_version 41 || return 0
3005         #lctl set_param debug=-1
3006         #set -vx
3007         mkdir -p $DIR/d48e/dir
3008         cd $DIR/d48e/dir
3009         $TRACE rmdir $DIR/d48e/dir || error "remove cwd $DIR/d48e/dir failed"
3010         $TRACE rmdir $DIR/d48e || error "remove parent $DIR/d48e failed"
3011         $TRACE touch $DIR/d48e || error "'touch $DIR/d48e' failed"
3012         $TRACE chmod +x $DIR/d48e || error "'chmod +x $DIR/d48e' failed"
3013         # On a buggy kernel addition of "touch foo" after cd .. will
3014         # produce kernel oops in lookup_hash_it
3015         touch ../foo && error "'cd ..' worked after recreate parent"
3016         cd $DIR
3017         $TRACE rm $DIR/d48e || error "rm '$DIR/d48e' failed"
3018 }
3019 run_test 48e "Access to recreated parent subdir (should return errors)"
3020
3021 test_50() {
3022         # bug 1485
3023         mkdir $DIR/d50
3024         cd $DIR/d50
3025         ls /proc/$$/cwd || error
3026 }
3027 run_test 50 "special situations: /proc symlinks  ==============="
3028
3029 test_51a() {    # was test_51
3030         # bug 1516 - create an empty entry right after ".." then split dir
3031         mkdir $DIR/d51
3032         touch $DIR/d51/foo
3033         $MCREATE $DIR/d51/bar
3034         rm $DIR/d51/foo
3035         createmany -m $DIR/d51/longfile 201
3036         FNUM=202
3037         while [ `ls -sd $DIR/d51 | awk '{ print $1 }'` -eq 4 ]; do
3038                 $MCREATE $DIR/d51/longfile$FNUM
3039                 FNUM=$(($FNUM + 1))
3040                 echo -n "+"
3041         done
3042         echo
3043         ls -l $DIR/d51 > /dev/null || error
3044 }
3045 run_test 51a "special situations: split htree with empty entry =="
3046
3047 #export NUMTEST=70000
3048 # FIXME: I select a relatively small number to do basic test.
3049 # large number may give panic(). debugging on this is going on.
3050 export NUMTEST=70
3051 test_51b() {
3052         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3053         [ $NUMFREE -lt 21000 ] && \
3054                 skip "not enough free inodes ($NUMFREE)" && \
3055                 return
3056
3057         check_kernel_version 40 || NUMTEST=31000
3058         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 50))
3059
3060         mkdir -p $DIR/d51b
3061         createmany -d $DIR/d51b/t- $NUMTEST
3062 }
3063 run_test 51b "mkdir .../t-0 --- .../t-$NUMTEST ===================="
3064
3065 test_51bb() {
3066         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3067
3068         local ndirs=${TEST51BB_NDIRS:-10}
3069         local nfiles=${TEST51BB_NFILES:-100}
3070
3071         local numfree=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3072
3073         [ $numfree -lt $(( ndirs * nfiles)) ] && \
3074                 nfiles=$(( numfree / ndirs - 10 ))
3075
3076         local dir=$DIR/d51bb
3077         mkdir -p $dir
3078         local savePOLICY=$(lctl get_param -n lmv.*.placement)
3079         lctl set_param -n lmv.*.placement=CHAR
3080
3081         lfs df -i $dir
3082         local IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3083         OLDUSED=($IUSED)
3084
3085         declare -a dirs
3086         for ((i=0; i < $ndirs; i++)); do
3087                 dirs[i]=$dir/$RANDOM
3088                 echo Creating directory ${dirs[i]}
3089                 mkdir -p ${dirs[i]}
3090                 ls $dir
3091                 echo Creating $nfiles in dir ${dirs[i]} ...
3092                 echo "createmany -o ${dirs[i]}/$tfile- $nfiles"
3093                 createmany -o ${dirs[i]}/$tfile- $nfiles
3094         done
3095         ls $dir
3096
3097         sleep 1
3098
3099         IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3100         NEWUSED=($IUSED)
3101
3102         local rc=0
3103         for ((i=0; i<${#NEWUSED[@]}; i++)); do
3104                 echo "mds $i: inodes count OLD ${OLDUSED[$i]} NEW ${NEWUSED[$i]}"
3105                 [ ${OLDUSED[$i]} -lt ${NEWUSED[$i]} ] || rc=$((rc + 1))
3106         done
3107
3108         lctl set_param -n lmv.*.placement=$savePOLICY
3109
3110         [ $rc -ne $MDSCOUNT ] || \
3111                 error "Objects/inodes are not distributed over all mds servers"
3112 }
3113 run_test 51bb "mkdir createmany CMD $MDSCOUNT  ===================="
3114
3115
3116 test_51c() {
3117         [ ! -d $DIR/d51b ] && skip "$DIR/51b missing" && \
3118                 return
3119
3120         unlinkmany -d $DIR/d51b/t- $NUMTEST
3121 }
3122 run_test 51c "rmdir .../t-0 --- .../t-$NUMTEST ===================="
3123
3124 test_51d() {
3125         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3126         mkdir -p $DIR/d51d
3127         createmany -o $DIR/d51d/t- 1000
3128         $LFS getstripe $DIR/d51d > $TMP/files
3129         for N in `seq 0 $((OSTCOUNT - 1))`; do
3130             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3131             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3132             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3133         done
3134         unlinkmany $DIR/d51d/t- 1000
3135
3136         NLAST=0
3137         for N in `seq 1 $((OSTCOUNT - 1))`; do
3138             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3139                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3140             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3141                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3142
3143             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3144                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3145             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3146                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3147             NLAST=$N
3148         done
3149 }
3150 run_test 51d "check object distribution ===================="
3151
3152 test_52a() {
3153         [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
3154         mkdir -p $DIR/d52a
3155         touch $DIR/d52a/foo
3156         chattr +a $DIR/d52a/foo || error "chattr +a failed"
3157         echo bar >> $DIR/d52a/foo || error "append bar failed"
3158         cp /etc/hosts $DIR/d52a/foo && error "cp worked"
3159         rm -f $DIR/d52a/foo 2>/dev/null && error "rm worked"
3160         link $DIR/d52a/foo $DIR/d52a/foo_link 2>/dev/null && error "link worked"
3161         echo foo >> $DIR/d52a/foo || error "append foo failed"
3162         mrename $DIR/d52a/foo $DIR/d52a/foo_ren && error "rename worked"
3163         lsattr $DIR/d52a/foo | egrep -q "^-+a[-e]+ $DIR/d52a/foo" || error "lsattr"
3164         chattr -a $DIR/d52a/foo || error "chattr -a failed"
3165         cp -r $DIR/d52a /tmp/
3166         rm -fr $DIR/d52a || error "cleanup rm failed"
3167 }
3168 run_test 52a "append-only flag test (should return errors) ====="
3169
3170 test_52b() {
3171         [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
3172         mkdir -p $DIR/d52b
3173         touch $DIR/d52b/foo
3174         chattr +i $DIR/d52b/foo || error "chattr +i failed"
3175         cat test > $DIR/d52b/foo && error "cat test worked"
3176         cp /etc/hosts $DIR/d52b/foo && error "cp worked"
3177         rm -f $DIR/d52b/foo 2>/dev/null && error "rm worked"
3178         link $DIR/d52b/foo $DIR/d52b/foo_link 2>/dev/null && error  "link worked"
3179         echo foo >> $DIR/d52b/foo && error "echo worked"
3180         mrename $DIR/d52b/foo $DIR/d52b/foo_ren && error "rename worked"
3181         [ -f $DIR/d52b/foo ] || error
3182         [ -f $DIR/d52b/foo_ren ] && error
3183         lsattr $DIR/d52b/foo | egrep -q "^-+i[-e]+ $DIR/d52b/foo" || error "lsattr"
3184         chattr -i $DIR/d52b/foo || error "chattr failed"
3185
3186         rm -fr $DIR/d52b || error
3187 }
3188 run_test 52b "immutable flag test (should return errors) ======="
3189
3190 test_53() {
3191         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3192         remote_ost_nodsh && skip "remote OST with nodsh" && return
3193
3194         local param
3195         local ostname
3196         local mds_last
3197         local ost_last
3198         local ostnum
3199
3200         # only test MDT0000
3201         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3202         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3203                 param=`echo ${value[0]} | cut -d "=" -f1`
3204                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3205                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3206                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
3207                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id | head -n 1)
3208                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3209                 if [ $ost_last != $mds_last ]; then
3210                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3211                 fi
3212         done
3213 }
3214 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3215
3216 test_54a() {
3217         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3218         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3219         $SOCKETSERVER $DIR/socket
3220         $SOCKETCLIENT $DIR/socket || error
3221         $MUNLINK $DIR/socket
3222 }
3223 run_test 54a "unix domain socket test =========================="
3224
3225 test_54b() {
3226         f="$DIR/f54b"
3227         mknod $f c 1 3
3228         chmod 0666 $f
3229         dd if=/dev/zero of=$f bs=`page_size` count=1
3230 }
3231 run_test 54b "char device works in lustre ======================"
3232
3233 find_loop_dev() {
3234         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3235         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3236         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3237
3238         for i in `seq 3 7`; do
3239                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3240                 LOOPDEV=$LOOPBASE$i
3241                 LOOPNUM=$i
3242                 break
3243         done
3244 }
3245
3246 test_54c() {
3247         tfile="$DIR/f54c"
3248         tdir="$DIR/d54c"
3249         loopdev="$DIR/loop54c"
3250
3251         find_loop_dev
3252         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3253         mknod $loopdev b 7 $LOOPNUM
3254         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3255         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3256         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3257         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3258         mkdir -p $tdir
3259         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3260         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3261         df $tdir
3262         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3263         $UMOUNT $tdir
3264         losetup -d $loopdev
3265         rm $loopdev
3266 }
3267 run_test 54c "block device works in lustre ====================="
3268
3269 test_54d() {
3270         f="$DIR/f54d"
3271         string="aaaaaa"
3272         mknod $f p
3273         [ "$string" = `echo $string > $f | cat $f` ] || error
3274 }
3275 run_test 54d "fifo device works in lustre ======================"
3276
3277 test_54e() {
3278         check_kernel_version 46 || return 0
3279         f="$DIR/f54e"
3280         string="aaaaaa"
3281         cp -aL /dev/console $f
3282         echo $string > $f || error
3283 }
3284 run_test 54e "console/tty device works in lustre ======================"
3285
3286 #The test_55 used to be iopen test and it was removed by bz#24037.
3287 #run_test 55 "check iopen_connect_dentry() ======================"
3288
3289 test_56a() {    # was test_56
3290         rm -rf $DIR/d56
3291         $SETSTRIPE -d $DIR
3292         mkdir $DIR/d56
3293         mkdir $DIR/d56/dir
3294         NUMFILES=3
3295         NUMFILESx2=$(($NUMFILES * 2))
3296         for i in `seq 1 $NUMFILES` ; do
3297                 touch $DIR/d56/file$i
3298                 touch $DIR/d56/dir/file$i
3299         done
3300
3301         # test lfs getstripe with --recursive
3302         FILENUM=`$GETSTRIPE --recursive $DIR/d56 | grep -c obdidx`
3303         [ $FILENUM -eq $NUMFILESx2 ] || error \
3304                 "lfs getstripe --recursive $DIR/d56 wrong: found $FILENUM, expected $NUMFILESx2"
3305         FILENUM=`$GETSTRIPE $DIR/d56 | grep -c obdidx`
3306         [ $FILENUM -eq $NUMFILES ] || error \
3307                 "lfs getstripe $DIR/d56 without --recursive wrong: found $FILENUM, expected $NUMFILES"
3308         echo "lfs getstripe --recursive passed."
3309
3310         # test lfs getstripe with file instead of dir
3311         FILENUM=`$GETSTRIPE $DIR/d56/file1 | grep -c obdidx`
3312         [ $FILENUM  -eq 1 ] || error \
3313                  "lfs getstripe $DIR/d56/file1 wrong:found $FILENUM, expected 1"
3314         echo "lfs getstripe file passed."
3315
3316         #test lfs getstripe with --verbose
3317         [ `$GETSTRIPE --verbose $DIR/d56 | grep -c lmm_magic` -eq $NUMFILES ] ||\
3318                 error "lfs getstripe --verbose $DIR/d56 wrong: should find $NUMFILES lmm_magic info"
3319         [ `$GETSTRIPE $DIR/d56 | grep -c lmm_magic` -eq 0 ] || error \
3320                 "lfs getstripe $DIR/d56 without --verbose wrong: should not show lmm_magic info"
3321         echo "lfs getstripe --verbose passed."
3322
3323         #test lfs getstripe with --obd
3324         $GETSTRIPE --obd wrong_uuid $DIR/d56 2>&1 | grep -q "unknown obduuid" || \
3325                 error "lfs getstripe --obd wrong_uuid should return error message"
3326
3327         [  "$OSTCOUNT" -lt 2 ] && \
3328                 skip_env "skipping other lfs getstripe --obd test" && return
3329         OSTIDX=1
3330         OBDUUID=$(lfs osts | grep ${OSTIDX}": " | awk '{print $2}')
3331         FILENUM=`$GETSTRIPE -ir $DIR/d56 | grep -x $OSTIDX | wc -l`
3332         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/d56 | grep obdidx | wc -l`
3333         [ $FOUND -eq $FILENUM ] || \
3334                 error "lfs getstripe --obd wrong: found $FOUND, expected $FILENUM"
3335         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/d56 | \
3336                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |\
3337                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] || \
3338                 error "lfs getstripe --obd wrong: should not show file on other obd"
3339         echo "lfs getstripe --obd passed."
3340 }
3341 run_test 56a "check lfs getstripe ===================================="
3342
3343 NUMFILES=3
3344 NUMDIRS=3
3345 setup_56() {
3346         LOCAL_NUMFILES=$1
3347         LOCAL_NUMDIRS=$2
3348         if [ ! -d "$DIR/${tdir}g" ] ; then
3349                 mkdir -p $DIR/${tdir}g
3350                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3351                         touch $DIR/${tdir}g/file$i
3352                 done
3353                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3354                         mkdir $DIR/${tdir}g/dir$i
3355                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3356                                 touch $DIR/${tdir}g/dir$i/file$j
3357                         done
3358                 done
3359         fi
3360 }
3361
3362 setup_56_special() {
3363         LOCAL_NUMFILES=$1
3364         LOCAL_NUMDIRS=$2
3365         TDIR=$DIR/${tdir}g
3366         setup_56 $1 $2
3367         if [ ! -e "$TDIR/loop1b" ] ; then
3368                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3369                         mknod $TDIR/loop${i}b b 7 $i
3370                         mknod $TDIR/null${i}c c 1 3
3371                         ln -s $TDIR/file1 $TDIR/link${i}l
3372                 done
3373                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3374                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3375                         mknod $TDIR/dir$i/null${i}c c 1 3
3376                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3377                 done
3378         fi
3379 }
3380
3381 test_56g() {
3382         $LSTRIPE -d $DIR
3383
3384         setup_56 $NUMFILES $NUMDIRS
3385
3386         EXPECTED=$(($NUMDIRS + 2))
3387         # test lfs find with -name
3388         for i in `seq 1 $NUMFILES` ; do
3389                 NUMS=`$LFIND -name "*$i" $DIR/${tdir}g | wc -l`
3390                 [ $NUMS -eq $EXPECTED ] || error \
3391                         "lfs find -name \"*$i\" $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
3392         done
3393         echo "lfs find -name passed."
3394 }
3395 run_test 56g "check lfs find -name ============================="
3396
3397 test_56h() {
3398         $LSTRIPE -d $DIR
3399
3400         setup_56 $NUMFILES $NUMDIRS
3401
3402         EXPECTED=$((($NUMDIRS+1)*($NUMFILES-1)+$NUMFILES))
3403         # test lfs find with ! -name
3404         for i in `seq 1 $NUMFILES` ; do
3405                 NUMS=`$LFIND ! -name "*$i" $DIR/${tdir}g | wc -l`
3406                 [ $NUMS -eq $EXPECTED ] || error \
3407                         "lfs find ! -name \"*$i\" $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
3408         done
3409         echo "lfs find ! -name passed."
3410 }
3411 run_test 56h "check lfs find ! -name ============================="
3412
3413 test_56i() {
3414        tdir=${tdir}i
3415        mkdir -p $DIR/$tdir
3416        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3417        OUT=$($LFIND -obd $UUID $DIR/$tdir)
3418        [ "$OUT" ] && error "$LFIND returned directory '$OUT'" || true
3419 }
3420 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3421
3422 test_56j() {
3423         setup_56_special $NUMFILES $NUMDIRS
3424
3425         EXPECTED=$((NUMDIRS+1))
3426         NUMS=`$LFIND -type d $DIR/${tdir}g | wc -l`
3427         [ $NUMS -eq $EXPECTED ] || \
3428                 error "lfs find -type d $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
3429 }
3430 run_test 56j "check lfs find -type d ============================="
3431
3432 test_56k() {
3433         setup_56_special $NUMFILES $NUMDIRS
3434
3435         EXPECTED=$(((NUMDIRS+1) * NUMFILES))
3436         NUMS=`$LFIND -type f $DIR/${tdir}g | wc -l`
3437         [ $NUMS -eq $EXPECTED ] || \
3438                 error "lfs find -type f $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
3439 }
3440 run_test 56k "check lfs find -type f ============================="
3441
3442 test_56l() {
3443         setup_56_special $NUMFILES $NUMDIRS
3444
3445         EXPECTED=$((NUMDIRS + NUMFILES))
3446         NUMS=`$LFIND -type b $DIR/${tdir}g | wc -l`
3447         [ $NUMS -eq $EXPECTED ] || \
3448                 error "lfs find -type b $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
3449 }
3450 run_test 56l "check lfs find -type b ============================="
3451
3452 test_56m() {
3453         setup_56_special $NUMFILES $NUMDIRS
3454
3455         EXPECTED=$((NUMDIRS + NUMFILES))
3456         NUMS=`$LFIND -type c $DIR/${tdir}g | wc -l`
3457         [ $NUMS -eq $EXPECTED ] || \
3458                 error "lfs find -type c $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
3459 }
3460 run_test 56m "check lfs find -type c ============================="
3461
3462 test_56n() {
3463         setup_56_special $NUMFILES $NUMDIRS
3464
3465         EXPECTED=$((NUMDIRS + NUMFILES))
3466         NUMS=`$LFIND -type l $DIR/${tdir}g | wc -l`
3467         [ $NUMS -eq $EXPECTED ] || \
3468                 error "lfs find -type l $DIR/${tdir}g wrong: found $NUMS, expected $EXPECTED"
3469 }
3470 run_test 56n "check lfs find -type l ============================="
3471
3472 test_56o() {
3473         TDIR=$DIR/${tdir}g
3474         rm -rf $TDIR
3475         setup_56 $NUMFILES $NUMDIRS
3476
3477         utime $TDIR/file1 > /dev/null || error "utime (1)"
3478         utime $TDIR/file2 > /dev/null || error "utime (2)"
3479         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3480         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3481         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3482         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3483
3484         EXPECTED=4
3485         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3486         [ $NUMS -eq $EXPECTED ] || \
3487                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3488
3489         EXPECTED=12
3490         NUMS=`$LFIND -mtime 0 $TDIR | wc -l`
3491         [ $NUMS -eq $EXPECTED ] || \
3492                 error "lfs find -mtime 0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3493
3494 }
3495 run_test 56o "check lfs find -mtime for old files =========================="
3496
3497 test_56p() {
3498         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3499
3500         TDIR=$DIR/${tdir}g
3501         rm -rf $TDIR
3502
3503         setup_56 $NUMFILES $NUMDIRS
3504
3505         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3506         EXPECTED=$NUMFILES
3507         NUMS="`$LFIND -uid $RUNAS_ID $TDIR | wc -l`"
3508         [ $NUMS -eq $EXPECTED ] || \
3509                 error "lfs find -uid $TDIR wrong: found $NUMS, expected $EXPECTED"
3510
3511         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
3512         NUMS="`$LFIND ! -uid $RUNAS_ID $TDIR | wc -l`"
3513         [ $NUMS -eq $EXPECTED ] || \
3514                 error "lfs find ! -uid $TDIR wrong: found $NUMS, expected $EXPECTED"
3515
3516         echo "lfs find -uid and ! -uid passed."
3517 }
3518 run_test 56p "check lfs find -uid and ! -uid ==============================="
3519
3520 test_56q() {
3521         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3522
3523         TDIR=$DIR/${tdir}g
3524         rm -rf $TDIR
3525
3526         setup_56 $NUMFILES $NUMDIRS
3527
3528         chgrp $RUNAS_GID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3529         EXPECTED=$NUMFILES
3530         NUMS="`$LFIND -gid $RUNAS_GID $TDIR | wc -l`"
3531         [ $NUMS -eq $EXPECTED ] || \
3532                 error "lfs find -gid $TDIR wrong: found $NUMS, expected $EXPECTED"
3533
3534         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
3535         NUMS="`$LFIND ! -gid $RUNAS_GID $TDIR | wc -l`"
3536         [ $NUMS -eq $EXPECTED ] || \
3537                 error "lfs find ! -gid $TDIR wrong: found $NUMS, expected $EXPECTED"
3538
3539         echo "lfs find -gid and ! -gid passed."
3540 }
3541 run_test 56q "check lfs find -gid and ! -gid ==============================="
3542
3543 test_56r() {
3544         setup_56 $NUMFILES $NUMDIRS
3545         TDIR=$DIR/${tdir}g
3546
3547         EXPECTED=12
3548         NUMS=`$LFIND -size 0 -t f $TDIR | wc -l`
3549         [ $NUMS -eq $EXPECTED ] || \
3550                 error "lfs find $TDIR -size 0 wrong: found $NUMS, expected $EXPECTED"
3551         EXPECTED=0
3552         NUMS=`$LFIND ! -size 0 -t f $TDIR | wc -l`
3553         [ $NUMS -eq $EXPECTED ] || \
3554                 error "lfs find $TDIR ! -size 0 wrong: found $NUMS, expected $EXPECTED"
3555         echo "test" > $TDIR/56r && sync
3556         echo "test2" > $TDIR/56r2 && sync
3557         EXPECTED=1
3558         NUMS=`$LFIND -size 5 -t f $TDIR | wc -l`
3559         [ $NUMS -eq $EXPECTED ] || \
3560                 error "lfs find $TDIR -size 5 wrong: found $NUMS, expected $EXPECTED"
3561         EXPECTED=1
3562         NUMS=`$LFIND -size +5 -t f $TDIR | wc -l`
3563         [ $NUMS -eq $EXPECTED ] || \
3564                 error "lfs find $TDIR -size +5 wrong: found $NUMS, expected $EXPECTED"
3565         EXPECTED=2
3566         NUMS=`$LFIND -size +0 -t f $TDIR | wc -l`
3567         [ $NUMS -eq $EXPECTED ] || \
3568                 error "lfs find $TDIR -size +0 wrong: found $NUMS, expected $EXPECTED"
3569         EXPECTED=2
3570         NUMS=`$LFIND ! -size -5 -t f $TDIR | wc -l`
3571         [ $NUMS -eq $EXPECTED ] || \
3572                 error "lfs find $TDIR ! -size -5 wrong: found $NUMS, expected $EXPECTED"
3573         EXPECTED=12
3574         NUMS=`$LFIND -size -5 -t f $TDIR | wc -l`
3575         [ $NUMS -eq $EXPECTED ] || \
3576                 error "lfs find $TDIR -size -5 wrong: found $NUMS, expected $EXPECTED"
3577 }
3578
3579 run_test 56r "check lfs find -size works =========================="
3580
3581 test_57a() {
3582         # note test will not do anything if MDS is not local
3583         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3584         local MNTDEV="osd*.*MDT*.mntdev"
3585         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
3586         [ -z "$DEV" ] && error "can't access $MNTDEV"
3587         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
3588                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump || error "can't access $DEV"
3589                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
3590                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
3591                 rm $TMP/t57a.dump
3592         done
3593 }
3594 run_test 57a "verify MDS filesystem created with large inodes =="
3595
3596 test_57b() {
3597         local dir=$DIR/d57b
3598
3599         local FILECOUNT=100
3600         local FILE1=$dir/f1
3601         local FILEN=$dir/f$FILECOUNT
3602
3603         rm -rf $dir || error "removing $dir"
3604         mkdir -p $dir || error "creating $dir"
3605         local num=$(get_mds_dir $dir)
3606         local mymds=mds$num
3607
3608         echo "mcreating $FILECOUNT files"
3609         createmany -m $dir/f 1 $FILECOUNT || \
3610                 error "creating files in $dir"
3611
3612         # verify that files do not have EAs yet
3613         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
3614         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
3615
3616         sync
3617         sleep 1
3618         df $dir  #make sure we get new statfs data
3619         local MDSFREE=$(do_facet $mymds \
3620                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
3621         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
3622         echo "opening files to create objects/EAs"
3623         local FILE
3624         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
3625                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
3626         done
3627
3628         # verify that files have EAs now
3629         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
3630         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
3631
3632         sleep 1  #make sure we get new statfs data
3633         df $dir
3634         local MDSFREE2=$(do_facet $mymds \
3635                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
3636         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
3637         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
3638                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
3639                         error "MDC before $MDCFREE != after $MDCFREE2"
3640                 else
3641                         echo "MDC before $MDCFREE != after $MDCFREE2"
3642                         echo "unable to confirm if MDS has large inodes"
3643                 fi
3644         fi
3645         rm -rf $dir
3646 }
3647 run_test 57b "default LOV EAs are stored inside large inodes ==="
3648
3649 test_58() {
3650     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
3651     wiretest
3652 }
3653 run_test 58 "verify cross-platform wire constants =============="
3654
3655 test_59() {
3656         echo "touch 130 files"
3657         createmany -o $DIR/f59- 130
3658         echo "rm 130 files"
3659         unlinkmany $DIR/f59- 130
3660         sync
3661         sleep 2
3662         # wait for commitment of removal
3663 }
3664 run_test 59 "verify cancellation of llog records async ========="
3665
3666 TEST60_HEAD="test_60 run $RANDOM"
3667 test_60a() {
3668         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
3669         log "$TEST60_HEAD - from kernel mode"
3670         do_facet mgs sh run-llog.sh
3671 }
3672 run_test 60a "llog sanity tests run from kernel module =========="
3673
3674 test_60b() { # bug 6411
3675         dmesg > $DIR/$tfile
3676         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
3677                                  /llog.test/ {
3678                                          if (marker)
3679                                                  from_marker++
3680                                          from_begin++
3681                                  }
3682                                  END {
3683                                          if (marker)
3684                                                  print from_marker
3685                                          else
3686                                                  print from_begin
3687                                  }"`
3688         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
3689 }
3690 run_test 60b "limit repeated messages from CERROR/CWARN ========"
3691
3692 test_60c() {
3693         echo "create 5000 files"
3694         createmany -o $DIR/f60c- 5000
3695 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
3696         lctl set_param fail_loc=0x80000137
3697         unlinkmany $DIR/f60c- 5000
3698         lctl set_param fail_loc=0
3699 }
3700 run_test 60c "unlink file when mds full"
3701
3702 test_60d() {
3703         SAVEPRINTK=$(lctl get_param -n printk)
3704
3705         # verify "lctl mark" is even working"
3706         MESSAGE="test message ID $RANDOM $$"
3707         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
3708         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
3709
3710         lctl set_param printk=0 || error "set lnet.printk failed"
3711         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
3712         MESSAGE="new test message ID $RANDOM $$"
3713         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
3714         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
3715         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
3716
3717         lctl set_param -n printk="$SAVEPRINTK"
3718 }
3719 run_test 60d "test printk console message masking"
3720
3721 test_61() {
3722         f="$DIR/f61"
3723         dd if=/dev/zero of=$f bs=`page_size` count=1
3724         cancel_lru_locks osc
3725         multiop $f OSMWUc || error
3726         sync
3727 }
3728 run_test 61 "mmap() writes don't make sync hang ================"
3729
3730 # bug 2330 - insufficient obd_match error checking causes LBUG
3731 test_62() {
3732         f="$DIR/f62"
3733         echo foo > $f
3734         cancel_lru_locks osc
3735         lctl set_param fail_loc=0x405
3736         cat $f && error "cat succeeded, expect -EIO"
3737         lctl set_param fail_loc=0
3738 }
3739 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
3740 # match every page all of the time.
3741 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
3742
3743 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
3744 test_63a() {    # was test_63
3745         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
3746         lctl set_param -n osc.*.max_dirty_mb 0
3747         for i in `seq 10` ; do
3748                 dd if=/dev/zero of=$DIR/f63 bs=8k &
3749                 sleep 5
3750                 kill $!
3751                 sleep 1
3752         done
3753
3754         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
3755         rm -f $DIR/f63 || true
3756 }
3757 run_test 63a "Verify oig_wait interruption does not crash ======="
3758
3759 # bug 2248 - async write errors didn't return to application on sync
3760 # bug 3677 - async write errors left page locked
3761 test_63b() {
3762         debugsave
3763         lctl set_param debug=-1
3764
3765         # ensure we have a grant to do async writes
3766         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
3767         rm $DIR/$tfile
3768
3769         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
3770         lctl set_param fail_loc=0x80000406
3771         multiop $DIR/$tfile Owy && \
3772                 error "sync didn't return ENOMEM"
3773         sync; sleep 2; sync     # do a real sync this time to flush page
3774         lctl get_param -n llite.*.dump_page_cache | grep locked && \
3775                 error "locked page left in cache after async error" || true
3776         debugrestore
3777 }
3778 run_test 63b "async write errors should be returned to fsync ==="
3779
3780 test_64a () {
3781         df $DIR
3782         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
3783 }
3784 run_test 64a "verify filter grant calculations (in kernel) ====="
3785
3786 test_64b () {
3787         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
3788         sh oos.sh $MOUNT
3789 }
3790 run_test 64b "check out-of-space detection on client ==========="
3791
3792 # bug 1414 - set/get directories' stripe info
3793 test_65a() {
3794         mkdir -p $DIR/d65
3795         touch $DIR/d65/f1
3796         $LVERIFY $DIR/d65 $DIR/d65/f1 || error "lverify failed"
3797 }
3798 run_test 65a "directory with no stripe info ===================="
3799
3800 test_65b() {
3801         mkdir -p $DIR/d65
3802         $SETSTRIPE $DIR/d65 -s $(($STRIPESIZE * 2)) -i 0 -c 1 || error "setstripe"
3803         touch $DIR/d65/f2
3804         $LVERIFY $DIR/d65 $DIR/d65/f2 || error "lverify failed"
3805 }
3806 run_test 65b "directory setstripe $(($STRIPESIZE * 2)) 0 1 ==============="
3807
3808 test_65c() {
3809         if [ $OSTCOUNT -gt 1 ]; then
3810                 mkdir -p $DIR/d65
3811                 $SETSTRIPE $DIR/d65 -s $(($STRIPESIZE * 4)) -i 1 \
3812                         -c $(($OSTCOUNT - 1)) || error "setstripe"
3813                 touch $DIR/d65/f3
3814                 $LVERIFY $DIR/d65 $DIR/d65/f3 || error "lverify failed"
3815         fi
3816 }
3817 run_test 65c "directory setstripe $(($STRIPESIZE * 4)) 1 $(($OSTCOUNT - 1))"
3818
3819 test_65d() {
3820         mkdir -p $DIR/d65
3821         if [ $STRIPECOUNT -le 0 ]; then
3822                 sc=1
3823         elif [ $STRIPECOUNT -gt 160 ]; then
3824 #LOV_MAX_STRIPE_COUNT is 160
3825                 [ $OSTCOUNT -gt 160 ] && sc=160 || sc=$(($OSTCOUNT - 1))
3826         else
3827                 sc=$(($STRIPECOUNT - 1))
3828         fi
3829         $SETSTRIPE $DIR/d65 -s $STRIPESIZE -c $sc || error "setstripe"
3830         touch $DIR/d65/f4 $DIR/d65/f5
3831         $LVERIFY $DIR/d65 $DIR/d65/f4 $DIR/d65/f5 || error "lverify failed"
3832 }
3833 run_test 65d "directory setstripe $STRIPESIZE -1 stripe_count =============="
3834
3835 test_65e() {
3836         mkdir -p $DIR/d65
3837
3838         $SETSTRIPE $DIR/d65 || error "setstripe"
3839         $GETSTRIPE -v $DIR/d65 | grep "Default" || error "no stripe info failed"
3840         touch $DIR/d65/f6
3841         $LVERIFY $DIR/d65 $DIR/d65/f6 || error "lverify failed"
3842 }
3843 run_test 65e "directory setstripe defaults ======================="
3844
3845 test_65f() {
3846         mkdir -p $DIR/d65f
3847         $RUNAS $SETSTRIPE $DIR/d65f && error "setstripe succeeded" || true
3848 }
3849 run_test 65f "dir setstripe permission (should return error) ==="
3850
3851 test_65g() {
3852         mkdir -p $DIR/d65
3853         $SETSTRIPE $DIR/d65 -s $(($STRIPESIZE * 2)) -i 0 -c 1 || error "setstripe"
3854         $SETSTRIPE -d $DIR/d65 || error "setstripe"
3855         $GETSTRIPE -v $DIR/d65 | grep "Default" || \
3856                 error "delete default stripe failed"
3857 }
3858 run_test 65g "directory setstripe -d ==========================="
3859
3860 test_65h() {
3861         mkdir -p $DIR/d65
3862         $SETSTRIPE $DIR/d65 -s $(($STRIPESIZE * 2)) -i 0 -c 1 || error "setstripe"
3863         mkdir -p $DIR/d65/dd1
3864         [ "`$GETSTRIPE -v $DIR/d65 | grep "^count"`" == \
3865           "`$GETSTRIPE -v $DIR/d65/dd1 | grep "^count"`" ] || error "stripe info inherit failed"
3866 }
3867 run_test 65h "directory stripe info inherit ===================="
3868
3869 test_65i() { # bug6367
3870         $SETSTRIPE $MOUNT -s 65536 -c -1
3871 }
3872 run_test 65i "set non-default striping on root directory (bug 6367)="
3873
3874 test_65ia() { # bug12836
3875         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
3876 }
3877 run_test 65ia "getstripe on -1 default directory striping"
3878
3879 test_65ib() { # bug12836
3880         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
3881 }
3882 run_test 65ib "getstripe -v on -1 default directory striping"
3883
3884 test_65ic() { # bug12836
3885         $LFS find -mtime -1 $MOUNT || error "find $MOUNT failed"
3886 }
3887 run_test 65ic "new find on -1 default directory striping"
3888
3889 test_65j() { # bug6367
3890         sync; sleep 1
3891         # if we aren't already remounting for each test, do so for this test
3892         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
3893                 cleanup || error "failed to unmount"
3894                 setup
3895         fi
3896         $SETSTRIPE -d $MOUNT || error "setstripe failed"
3897 }
3898 run_test 65j "set default striping on root directory (bug 6367)="
3899
3900 test_65k() { # bug11679
3901         [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
3902         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3903
3904         echo "Check OST status: "
3905         MDS_OSCS=`do_facet $SINGLEMDS lctl dl | awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
3906         for OSC in $MDS_OSCS; do
3907                 echo $OSC "is activate"
3908                 do_facet $SINGLEMDS lctl --device %$OSC activate
3909         done
3910         do_facet client mkdir -p $DIR/$tdir
3911         for INACTIVE_OSC in $MDS_OSCS; do
3912                 echo $INACTIVE_OSC "is Deactivate:"
3913                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC deactivate
3914                 for STRIPE_OSC in $MDS_OSCS; do
3915                         STRIPE_OST=`osc_to_ost $STRIPE_OSC`
3916                         STRIPE_INDEX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
3917                                       grep $STRIPE_OST | awk -F: '{print $1}' | head -n 1`
3918
3919                 [ -f $DIR/$tdir/${STRIPE_INDEX} ] && continue
3920                         echo "$SETSTRIPE $DIR/$tdir/${STRIPE_INDEX} -i ${STRIPE_INDEX} -c 1"
3921                         do_facet client $SETSTRIPE $DIR/$tdir/${STRIPE_INDEX} -i ${STRIPE_INDEX} -c 1
3922                         RC=$?
3923                         [ $RC -ne 0 ] && error "setstripe should have succeeded"
3924                 done
3925                 do_facet client rm -f $DIR/$tdir/*
3926                 echo $INACTIVE_OSC "is Activate."
3927                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
3928         done
3929 }
3930 run_test 65k "validate manual striping works properly with deactivated OSCs"
3931
3932 test_65l() { # bug 12836
3933         mkdir -p $DIR/$tdir/test_dir
3934         $SETSTRIPE $DIR/$tdir/test_dir -c -1
3935         $LFS find -mtime -1 $DIR/$tdir >/dev/null
3936 }
3937 run_test 65l "lfs find on -1 stripe dir ========================"
3938
3939 # bug 2543 - update blocks count on client
3940 test_66() {
3941         COUNT=${COUNT:-8}
3942         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
3943         sync; sleep 1; sync
3944         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
3945         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
3946 }
3947 run_test 66 "update inode blocks count on client ==============="
3948
3949 LLOOP=
3950 LLITELOOPLOAD=
3951 cleanup_68() {
3952         trap 0
3953         if [ ! -z "$LLOOP" ]; then
3954                 if swapon -s | grep -q $LLOOP; then
3955                         swapoff $LLOOP || error "swapoff failed"
3956                 fi
3957
3958                 $LCTL blockdev_detach $LLOOP || error "detach failed"
3959                 rm -f $LLOOP
3960                 unset LLOOP
3961         fi
3962         if [ ! -z "$LLITELOOPLOAD" ]; then
3963                 rmmod llite_lloop
3964                 unset LLITELOOPLOAD
3965         fi
3966         rm -f $DIR/f68*
3967 }
3968
3969 meminfo() {
3970         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
3971 }
3972
3973 swap_used() {
3974         swapon -s | awk '($1 == "'$1'") { print $4 }'
3975 }
3976
3977 # test case for lloop driver, basic function
3978 test_68a() {
3979         [ "$UID" != 0 ] && skip_env "must run as root" && return
3980         llite_lloop_enabled || \
3981                 { skip_env "llite_lloop module disabled" && return; }
3982
3983         trap cleanup_68 EXIT
3984
3985         if ! module_loaded llite_lloop; then
3986                 if load_module llite/llite_lloop; then
3987                         LLITELOOPLOAD=yes
3988                 else
3989                         skip_env "can't find module llite_lloop"
3990                         return
3991                 fi
3992         fi
3993
3994         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
3995         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
3996         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
3997
3998         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
3999         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4000
4001         cleanup_68
4002 }
4003 run_test 68a "lloop driver - basic test ========================"
4004
4005 # excercise swapping to lustre by adding a high priority swapfile entry
4006 # and then consuming memory until it is used.
4007 test_68b() {  # was test_68
4008         [ "$UID" != 0 ] && skip_env "must run as root" && return
4009         lctl get_param -n devices | grep -q obdfilter && \
4010                 skip "local OST" && return
4011
4012         grep -q llite_lloop /proc/modules
4013         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4014
4015         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4016                 skip "can't reliably test swap with TCP" && return
4017
4018         MEMTOTAL=`meminfo MemTotal`
4019         NR_BLOCKS=$((MEMTOTAL>>8))
4020         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4021
4022         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4023         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4024         mkswap $DIR/f68b
4025
4026         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4027
4028         trap cleanup_68 EXIT
4029
4030         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4031
4032         echo "before: `swapon -s | grep $LLOOP`"
4033         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4034         echo "after: `swapon -s | grep $LLOOP`"
4035         SWAPUSED=`swap_used $LLOOP`
4036
4037         cleanup_68
4038
4039         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4040 }
4041 run_test 68b "support swapping to Lustre ========================"
4042
4043 # bug5265, obdfilter oa2dentry return -ENOENT
4044 # #define OBD_FAIL_OST_ENOENT 0x217
4045 test_69() {
4046         remote_ost_nodsh && skip "remote OST with nodsh" && return
4047
4048         f="$DIR/$tfile"
4049         $SETSTRIPE $f -c 1 -i 0
4050
4051         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4052
4053         do_facet ost1 lctl set_param fail_loc=0x217
4054         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4055         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4056
4057         do_facet ost1 lctl set_param fail_loc=0
4058         $DIRECTIO write $f 0 2 || error "write error"
4059
4060         cancel_lru_locks osc
4061         $DIRECTIO read $f 0 1 || error "read error"
4062
4063         do_facet ost1 lctl set_param fail_loc=0x217
4064         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4065
4066         do_facet ost1 lctl set_param fail_loc=0
4067         rm -f $f
4068 }
4069 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4070
4071 test_71() {
4072     mkdir -p $DIR/$tdir
4073     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4074 }
4075 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4076
4077 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4078         check_kernel_version 43 || return 0
4079         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4080
4081         # Check that testing environment is properly set up. Skip if not
4082         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4083                 skip_env "User $RUNAS_ID does not exist - skipping"
4084                 return 0
4085         }
4086         # We had better clear the $DIR to get enough space for dd
4087         rm -rf $DIR/*
4088         touch $DIR/f72
4089         chmod 777 $DIR/f72
4090         chmod ug+s $DIR/f72
4091         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4092         # See if we are still setuid/sgid
4093         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4094         # Now test that MDS is updated too
4095         cancel_lru_locks mdc
4096         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4097         true
4098         rm -f $DIR/f72
4099 }
4100 run_test 72a "Test that remove suid works properly (bug5695) ===="
4101
4102 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4103         local perm
4104
4105         [ "$RUNAS_ID" = "$UID" ] && \
4106                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4107         [ "$RUNAS_ID" -eq 0 ] && \
4108                 skip_env "RUNAS_ID = 0 -- skipping" && return
4109
4110         # Check that testing environment is properly set up. Skip if not
4111         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4112                 skip_env "User $RUNAS_ID does not exist - skipping"
4113                 return 0
4114         }
4115         touch $DIR/${tfile}-f{g,u}
4116         mkdir $DIR/${tfile}-d{g,u}
4117         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4118         chmod g+s $DIR/${tfile}-{f,d}g
4119         chmod u+s $DIR/${tfile}-{f,d}u
4120         for perm in 777 2777 4777; do
4121                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4122                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4123                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4124                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4125         done
4126         true
4127 }
4128 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4129
4130 # bug 3462 - multiple simultaneous MDC requests
4131 test_73() {
4132         mkdir $DIR/d73-1
4133         mkdir $DIR/d73-2
4134         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4135         pid1=$!
4136
4137         lctl set_param fail_loc=0x80000129
4138         multiop $DIR/d73-1/f73-2 Oc &
4139         sleep 1
4140         lctl set_param fail_loc=0
4141
4142         multiop $DIR/d73-2/f73-3 Oc &
4143         pid3=$!
4144
4145         kill -USR1 $pid1
4146         wait $pid1 || return 1
4147
4148         sleep 25
4149
4150         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4151         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4152         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4153
4154         rm -rf $DIR/d73-*
4155 }
4156 run_test 73 "multiple MDC requests (should not deadlock)"
4157
4158 test_74a() { # bug 6149, 6184
4159         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4160         #
4161         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4162         # will spin in a tight reconnection loop
4163         touch $DIR/f74a
4164         lctl set_param fail_loc=0x8000030e
4165         # get any lock that won't be difficult - lookup works.
4166         ls $DIR/f74a
4167         lctl set_param fail_loc=0
4168         true
4169         rm -f $DIR/f74a
4170 }
4171 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4172
4173 test_74b() { # bug 13310
4174         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4175         #
4176         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4177         # will spin in a tight reconnection loop
4178         lctl set_param fail_loc=0x8000030e
4179         # get a "difficult" lock
4180         touch $DIR/f74b
4181         lctl set_param fail_loc=0
4182         true
4183         rm -f $DIR/f74b
4184 }
4185 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4186
4187 test_74c() {
4188 #define OBD_FAIL_LDLM_NEW_LOCK
4189         lctl set_param fail_loc=0x80000319
4190         touch $DIR/$tfile && error "Touch successful"
4191         true
4192 }
4193 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4194
4195 num_inodes() {
4196         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4197 }
4198
4199 get_inode_slab_tunables() {
4200         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4201 }
4202
4203 set_inode_slab_tunables() {
4204         echo "lustre_inode_cache $1" > /proc/slabinfo
4205 }
4206
4207 test_76() { # Now for bug 20433, added originally in bug 1443
4208         local SLAB_SETTINGS=`get_inode_slab_tunables`
4209         local CPUS=`getconf _NPROCESSORS_ONLN`
4210         # we cannot set limit below 1 which means 1 inode in each
4211         # per-cpu cache is still allowed
4212         set_inode_slab_tunables "1 1 0"
4213         cancel_lru_locks osc
4214         BEFORE_INODES=`num_inodes`
4215         echo "before inodes: $BEFORE_INODES"
4216         local COUNT=1000
4217         [ "$SLOW" = "no" ] && COUNT=100
4218         for i in `seq $COUNT`; do
4219                 touch $DIR/$tfile
4220                 rm -f $DIR/$tfile
4221         done
4222         cancel_lru_locks osc
4223         AFTER_INODES=`num_inodes`
4224         echo "after inodes: $AFTER_INODES"
4225         local wait=0
4226         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
4227                 sleep 2
4228                 AFTER_INODES=`num_inodes`
4229                 wait=$((wait+2))
4230                 echo "wait $wait seconds inodes: $AFTER_INODES"
4231                 if [ $wait -gt 30 ]; then
4232                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
4233                 fi
4234         done
4235         set_inode_slab_tunables "$SLAB_SETTINGS"
4236 }
4237 run_test 76 "confirm clients recycle inodes properly ===="
4238
4239
4240 export ORIG_CSUM=""
4241 set_checksums()
4242 {
4243         # Note: in sptlrpc modes which enable its own bulk checksum, the
4244         # original crc32_le bulk checksum will be automatically disabled,
4245         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
4246         # will be checked by sptlrpc code against sptlrpc bulk checksum.
4247         # In this case set_checksums() will not be no-op, because sptlrpc
4248         # bulk checksum will be enabled all through the test.
4249
4250         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
4251         lctl set_param -n osc.*.checksums $1
4252         return 0
4253 }
4254
4255 export ORIG_CSUM_TYPE=""
4256 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
4257 set_checksum_type()
4258 {
4259         [ "$ORIG_CSUM_TYPE" ] || \
4260                 ORIG_CSUM_TYPE=`lctl get_param -n osc/*osc-[^mM]*/checksum_type |
4261                                 sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`
4262         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
4263         log "set checksum type to $1"
4264         return 0
4265 }
4266 F77_TMP=$TMP/f77-temp
4267 F77SZ=8
4268 setup_f77() {
4269         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
4270                 error "error writing to $F77_TMP"
4271 }
4272
4273 test_77a() { # bug 10889
4274         $GSS && skip "could not run with gss" && return
4275         [ ! -f $F77_TMP ] && setup_f77
4276         set_checksums 1
4277         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
4278         set_checksums 0
4279         rm -f $DIR/$tfile
4280 }
4281 run_test 77a "normal checksum read/write operation ============="
4282
4283 test_77b() { # bug 10889
4284         $GSS && skip "could not run with gss" && return
4285         [ ! -f $F77_TMP ] && setup_f77
4286         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4287         lctl set_param fail_loc=0x80000409
4288         set_checksums 1
4289         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
4290                 error "dd error: $?"
4291         lctl set_param fail_loc=0
4292         set_checksums 0
4293 }
4294 run_test 77b "checksum error on client write ===================="
4295
4296 test_77c() { # bug 10889
4297         $GSS && skip "could not run with gss" && return
4298         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
4299         set_checksums 1
4300         for algo in $CKSUM_TYPES; do
4301                 cancel_lru_locks osc
4302                 set_checksum_type $algo
4303                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4304                 lctl set_param fail_loc=0x80000408
4305                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
4306                 lctl set_param fail_loc=0
4307         done
4308         set_checksums 0
4309         set_checksum_type $ORIG_CSUM_TYPE
4310         rm -f $DIR/f77b
4311 }
4312 run_test 77c "checksum error on client read ==================="
4313
4314 test_77d() { # bug 10889
4315         $GSS && skip "could not run with gss" && return
4316         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4317         lctl set_param fail_loc=0x80000409
4318         set_checksums 1
4319         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4320                 error "direct write: rc=$?"
4321         lctl set_param fail_loc=0
4322         set_checksums 0
4323 }
4324 run_test 77d "checksum error on OST direct write ==============="
4325
4326 test_77e() { # bug 10889
4327         $GSS && skip "could not run with gss" && return
4328         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
4329         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4330         lctl set_param fail_loc=0x80000408
4331         set_checksums 1
4332         cancel_lru_locks osc
4333         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4334                 error "direct read: rc=$?"
4335         lctl set_param fail_loc=0
4336         set_checksums 0
4337 }
4338 run_test 77e "checksum error on OST direct read ================"
4339
4340 test_77f() { # bug 10889
4341         $GSS && skip "could not run with gss" && return
4342         set_checksums 1
4343         for algo in $CKSUM_TYPES; do
4344                 cancel_lru_locks osc
4345                 set_checksum_type $algo
4346                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4347                 lctl set_param fail_loc=0x409
4348                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
4349                         error "direct write succeeded"
4350                 lctl set_param fail_loc=0
4351         done
4352         set_checksum_type $ORIG_CSUM_TYPE
4353         set_checksums 0
4354 }
4355 run_test 77f "repeat checksum error on write (expect error) ===="
4356
4357 test_77g() { # bug 10889
4358         $GSS && skip "could not run with gss" && return
4359         remote_ost_nodsh && skip "remote OST with nodsh" && return
4360
4361         [ ! -f $F77_TMP ] && setup_f77
4362
4363         $SETSTRIPE $DIR/f77g -c 1 -i 0
4364         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
4365         do_facet ost1 lctl set_param fail_loc=0x8000021a
4366         set_checksums 1
4367         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
4368                 error "write error: rc=$?"
4369         do_facet ost1 lctl set_param fail_loc=0
4370         set_checksums 0
4371 }
4372 run_test 77g "checksum error on OST write ======================"
4373
4374 test_77h() { # bug 10889
4375         $GSS && skip "could not run with gss" && return
4376         remote_ost_nodsh && skip "remote OST with nodsh" && return
4377
4378         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
4379         cancel_lru_locks osc
4380         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
4381         do_facet ost1 lctl set_param fail_loc=0x8000021b
4382         set_checksums 1
4383         cmp $F77_TMP $DIR/f77g || error "file compare failed"
4384         do_facet ost1 lctl set_param fail_loc=0
4385         set_checksums 0
4386 }
4387 run_test 77h "checksum error on OST read ======================="
4388
4389 test_77i() { # bug 13805
4390         $GSS && skip "could not run with gss" && return
4391         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
4392         lctl set_param fail_loc=0x40b
4393         remount_client $MOUNT
4394         lctl set_param fail_loc=0
4395         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4396                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4397                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4398                 [ "$algo" = "crc32" ] || error "algo set to $algo instead of crc32"
4399         done
4400         remount_client $MOUNT
4401 }
4402 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
4403
4404 test_77j() { # bug 13805
4405         $GSS && skip "could not run with gss" && return
4406         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
4407         lctl set_param fail_loc=0x40c
4408         remount_client $MOUNT
4409         lctl set_param fail_loc=0
4410         sleep 2 # wait async osc connect to finish
4411         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4412                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4413                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4414                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4415         done
4416         remount_client $MOUNT
4417 }
4418 run_test 77j "client only supporting ADLER32 ===================="
4419
4420 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
4421 rm -f $F77_TMP
4422 unset F77_TMP
4423
4424 test_78() { # bug 10901
4425         remote_ost || { skip_env "local OST" && return; }
4426
4427         NSEQ=5
4428         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
4429         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
4430         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
4431         echo "MemTotal: $MEMTOTAL"
4432 # reserve 256MB of memory for the kernel and other running processes,
4433 # and then take 1/2 of the remaining memory for the read/write buffers.
4434     if [ $MEMTOTAL -gt 512 ] ;then
4435         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
4436     else
4437         # for those poor memory-starved high-end clusters...
4438         MEMTOTAL=$((MEMTOTAL / 2))
4439     fi
4440         echo "Mem to use for directio: $MEMTOTAL"
4441         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
4442         [ $F78SIZE -gt 512 ] && F78SIZE=512
4443         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
4444         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
4445         echo "Smallest OST: $SMALLESTOST"
4446         [ $SMALLESTOST -lt 10240 ] && \
4447                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
4448
4449         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
4450                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
4451
4452         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
4453         echo "File size: $F78SIZE"
4454         $SETSTRIPE $DIR/$tfile -c $OSTCOUNT || error "setstripe failed"
4455         for i in `seq 1 $NSEQ`
4456         do
4457                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
4458                 echo directIO rdwr round $i of $NSEQ
4459                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
4460         done
4461
4462         rm -f $DIR/$tfile
4463 }
4464 run_test 78 "handle large O_DIRECT writes correctly ============"
4465
4466 test_79() { # bug 12743
4467         wait_delete_completed
4468
4469         BKTOTAL=$(calc_osc_kbytes kbytestotal)
4470         BKFREE=$(calc_osc_kbytes kbytesfree)
4471         BKAVAIL=$(calc_osc_kbytes kbytesavail)
4472
4473         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
4474         DFTOTAL=`echo $STRING | cut -d, -f1`
4475         DFUSED=`echo $STRING  | cut -d, -f2`
4476         DFAVAIL=`echo $STRING | cut -d, -f3`
4477         DFFREE=$(($DFTOTAL - $DFUSED))
4478
4479         ALLOWANCE=$((64 * $OSTCOUNT))
4480
4481         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
4482            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
4483                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
4484         fi
4485         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
4486            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
4487                 error "df free($DFFREE) mismatch OST free($BKFREE)"
4488         fi
4489         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
4490            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
4491                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
4492         fi
4493 }
4494 run_test 79 "df report consistency check ======================="
4495
4496 test_80() { # bug 10718
4497         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
4498         sync; sleep 1; sync
4499         local BEFORE=`date +%s`
4500         cancel_lru_locks osc
4501         local AFTER=`date +%s`
4502         local DIFF=$((AFTER-BEFORE))
4503         if [ $DIFF -gt 1 ] ; then
4504                 error "elapsed for 1M@1T = $DIFF"
4505         fi
4506         true
4507         rm -f $DIR/$tfile
4508 }
4509 run_test 80 "Page eviction is equally fast at high offsets too  ===="
4510
4511 test_81a() { # LU-456
4512         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4513         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
4514         do_facet ost0 lctl set_param fail_loc=0x80000228
4515
4516         # write should trigger a retry and success
4517         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4518         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4519         RC=$?
4520         if [ $RC -ne 0 ] ; then
4521                 error "write should success, but failed for $RC"
4522         fi
4523 }
4524 run_test 81a "OST should retry write when get -ENOSPC ==============="
4525
4526 test_81b() { # LU-456
4527         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4528         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
4529         do_facet ost0 lctl set_param fail_loc=0x228
4530
4531         # write should retry several times and return -ENOSPC finally
4532         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4533         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4534         RC=$?
4535         ENOSPC=28
4536         if [ $RC -ne $ENOSPC ] ; then
4537                 error "dd should fail for -ENOSPC, but succeed."
4538         fi
4539 }
4540 run_test 81b "OST should return -ENOSPC when retry still fails ======="
4541
4542
4543 test_99a() {
4544         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
4545             return
4546         mkdir -p $DIR/d99cvsroot
4547         chown $RUNAS_ID $DIR/d99cvsroot
4548         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
4549         cd $TMP
4550
4551         $RUNAS cvs -d $DIR/d99cvsroot init || error
4552         cd $oldPWD
4553 }
4554 run_test 99a "cvs init ========================================="
4555
4556 test_99b() {
4557         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4558         [ ! -d $DIR/d99cvsroot ] && test_99a
4559         cd /etc/init.d
4560         # some versions of cvs import exit(1) when asked to import links or
4561         # files they can't read.  ignore those files.
4562         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
4563                         ! -perm +4 -printf '-I %f\n')
4564         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
4565                 d99reposname vtag rtag
4566 }
4567 run_test 99b "cvs import ======================================="
4568
4569 test_99c() {
4570         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4571         [ ! -d $DIR/d99cvsroot ] && test_99b
4572         cd $DIR
4573         mkdir -p $DIR/d99reposname
4574         chown $RUNAS_ID $DIR/d99reposname
4575         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
4576 }
4577 run_test 99c "cvs checkout ====================================="
4578
4579 test_99d() {
4580         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4581         [ ! -d $DIR/d99cvsroot ] && test_99c
4582         cd $DIR/d99reposname
4583         $RUNAS touch foo99
4584         $RUNAS cvs add -m 'addmsg' foo99
4585 }
4586 run_test 99d "cvs add =========================================="
4587
4588 test_99e() {
4589         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4590         [ ! -d $DIR/d99cvsroot ] && test_99c
4591         cd $DIR/d99reposname
4592         $RUNAS cvs update
4593 }
4594 run_test 99e "cvs update ======================================="
4595
4596 test_99f() {
4597         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4598         [ ! -d $DIR/d99cvsroot ] && test_99d
4599         cd $DIR/d99reposname
4600         $RUNAS cvs commit -m 'nomsg' foo99
4601     rm -fr $DIR/d99cvsroot
4602 }
4603 run_test 99f "cvs commit ======================================="
4604
4605 test_100() {
4606         [ "$NETTYPE" = tcp ] || \
4607                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
4608                         return ; }
4609
4610         remote_ost_nodsh && skip "remote OST with nodsh" && return
4611         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4612         remote_servers || \
4613                 { skip "useless for local single node setup" && return; }
4614
4615         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
4616                 [ "$PROT" != "tcp" ] && continue
4617                 RPORT=$(echo $REMOTE | cut -d: -f2)
4618                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
4619
4620                 rc=0
4621                 LPORT=`echo $LOCAL | cut -d: -f2`
4622                 if [ $LPORT -ge 1024 ]; then
4623                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
4624                         netstat -tna
4625                         error_exit "local: $LPORT > 1024, remote: $RPORT"
4626                 fi
4627         done
4628         [ "$rc" = 0 ] || error_exit "privileged port not found" )
4629 }
4630 run_test 100 "check local port using privileged port ==========="
4631
4632 function get_named_value()
4633 {
4634     local tag
4635
4636     tag=$1
4637     while read ;do
4638         line=$REPLY
4639         case $line in
4640         $tag*)
4641             echo $line | sed "s/^$tag[ ]*//"
4642             break
4643             ;;
4644         esac
4645     done
4646 }
4647
4648 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
4649 cleanup_101a() {
4650         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
4651         trap 0
4652 }
4653
4654 test_101a() {
4655         local s
4656         local discard
4657         local nreads=10000
4658         [ "$CPU" = "UML" ] && nreads=1000
4659         local cache_limit=32
4660
4661         $LCTL set_param -n osc.*-osc*.rpc_stats 0
4662         trap cleanup_101a EXIT
4663         $LCTL set_param -n llite.*.read_ahead_stats 0
4664         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
4665
4666         #
4667         # randomly read 10000 of 64K chunks from file 3x 32MB in size
4668         #
4669         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
4670         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
4671
4672         discard=0
4673         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
4674                 get_named_value 'read but discarded' | cut -d" " -f1`; do
4675                         discard=$(($discard + $s))
4676         done
4677         cleanup_101a
4678
4679         if [ $(($discard * 10)) -gt $nreads ] ;then
4680                 $LCTL get_param osc.*-osc*.rpc_stats
4681                 $LCTL get_param llite.*.read_ahead_stats
4682                 error "too many ($discard) discarded pages"
4683         fi
4684         rm -f $DIR/$tfile || true
4685 }
4686 run_test 101a "check read-ahead for random reads ================"
4687
4688 setup_test101bc() {
4689         mkdir -p $DIR/$tdir
4690         STRIPE_SIZE=1048576
4691         STRIPE_COUNT=$OSTCOUNT
4692         STRIPE_OFFSET=0
4693
4694         local list=$(comma_list $(osts_nodes))
4695         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=0
4696         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=0
4697
4698         trap cleanup_test101bc EXIT
4699         # prepare the read-ahead file
4700         $SETSTRIPE $DIR/$tfile -s $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT
4701
4702         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
4703 }
4704
4705 cleanup_test101bc() {
4706         trap 0
4707         rm -rf $DIR/$tdir
4708         rm -f $DIR/$tfile
4709
4710         local list=$(comma_list $(osts_nodes))
4711         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
4712         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=1
4713 }
4714
4715 calc_total() {
4716         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
4717 }
4718
4719 ra_check_101() {
4720         local READ_SIZE=$1
4721         local STRIPE_SIZE=1048576
4722         local RA_INC=1048576
4723         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
4724         local FILE_LENGTH=$((64*100))
4725         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
4726                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
4727         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
4728                         get_named_value 'read but discarded' | \
4729                         cut -d" " -f1 | calc_total`
4730         if [ $DISCARD -gt $discard_limit ]; then
4731                 $LCTL get_param llite.*.read_ahead_stats
4732                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
4733         else
4734                 echo "Read-ahead success for size ${READ_SIZE}"
4735         fi
4736 }
4737
4738 test_101b() {
4739         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
4740         local STRIPE_SIZE=1048576
4741         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
4742         local FILE_LENGTH=$((STRIPE_SIZE*100))
4743         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
4744         # prepare the read-ahead file
4745         setup_test101bc
4746         cancel_lru_locks osc
4747         for BIDX in 2 4 8 16 32 64 128 256
4748         do
4749                 local BSIZE=$((BIDX*4096))
4750                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
4751                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
4752                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
4753                 $LCTL set_param -n llite.*.read_ahead_stats 0
4754                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
4755                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
4756                 cancel_lru_locks osc
4757                 ra_check_101 $BSIZE
4758         done
4759         cleanup_test101bc
4760         true
4761 }
4762 run_test 101b "check stride-io mode read-ahead ================="
4763
4764 test_101c() {
4765     local STRIPE_SIZE=1048576
4766     local FILE_LENGTH=$((STRIPE_SIZE*100))
4767     local nreads=10000
4768     local osc
4769
4770     setup_test101bc
4771
4772     cancel_lru_locks osc
4773     $LCTL set_param osc.*.rpc_stats 0
4774     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
4775     for osc in $($LCTL get_param -N osc.*); do
4776         if [ "$osc" == "osc.num_refs" ]; then
4777             continue
4778         fi
4779
4780         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
4781         if [ $lines -le 20 ]; then
4782             continue
4783         fi
4784
4785         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
4786                                      awk '$1 == "1:" { print $2; exit; }')
4787         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
4788                                      awk '$1 == "2:" { print $2; exit; }')
4789         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
4790                                      awk '$1 == "4:" { print $2; exit; }')
4791         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
4792                                      awk '$1 == "8:" { print $2; exit; }')
4793
4794         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
4795         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
4796         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
4797         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
4798         echo "${osc} rpc check passed!"
4799     done
4800     cleanup_test101bc
4801     true
4802 }
4803 run_test 101c "check stripe_size aligned read-ahead ================="
4804
4805 set_read_ahead() {
4806    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
4807    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
4808 }
4809
4810 test_101d() {
4811     local file=$DIR/$tfile
4812     local size=${FILESIZE_101c:-500}
4813     local ra_MB=${READAHEAD_MB:-40}
4814
4815     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
4816     [ $space -gt $((size / 1024)) ] ||
4817         { skip "Need free space ${size}M, have $space" && return; }
4818
4819     echo Creating ${size}M test file $file
4820     dd if=/dev/zero of=$file bs=1M count=$size
4821     echo Cancel LRU locks on lustre client to flush the client cache
4822     cancel_lru_locks osc
4823
4824     echo Disable read-ahead
4825     local old_READAHEAD=$(set_read_ahead 0)
4826
4827     echo Reading the test file $file with read-ahead disabled
4828     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
4829
4830     echo Cancel LRU locks on lustre client to flush the client cache
4831     cancel_lru_locks osc
4832     echo Enable read-ahead with ${ra_MB}MB
4833     set_read_ahead $ra_MB
4834
4835     echo Reading the test file $file with read-ahead enabled
4836     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
4837
4838     echo read-ahead disabled time read $time_ra_OFF
4839     echo read-ahead enabled  time read $time_ra_ON
4840
4841     set_read_ahead $old_READAHEAD
4842     rm -f $file
4843
4844     [ $time_ra_ON -lt $time_ra_OFF ] ||
4845         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
4846                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
4847 }
4848 run_test 101d "file read with and without read-ahead enabled  ================="
4849
4850 test_101e() {
4851     local file=$DIR/$tfile
4852     local size=500  #KB
4853     local count=100
4854     local blksize=1024
4855
4856     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
4857     local need_space=$((count * size))
4858     [ $space -gt $need_space ] ||
4859         { skip_env "Need free space $need_space, have $space" && return; }
4860
4861     echo Creating $count ${size}K test files
4862     for ((i = 0; i < $count; i++)); do
4863         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
4864     done
4865
4866     echo Cancel LRU locks on lustre client to flush the client cache
4867     cancel_lru_locks osc
4868
4869     echo Reset readahead stats
4870     $LCTL set_param -n llite.*.read_ahead_stats 0
4871
4872     for ((i = 0; i < $count; i++)); do
4873         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
4874     done
4875
4876     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
4877           get_named_value 'misses' | cut -d" " -f1 | calc_total)
4878
4879     for ((i = 0; i < $count; i++)); do
4880         rm -rf ${file}_${i} 2>/dev/null
4881     done
4882
4883     #10000 means 20% reads are missing in readahead
4884     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
4885 }
4886 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
4887
4888 cleanup_test101f() {
4889     trap 0
4890     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
4891     rm -rf $DIR/$tfile 2>/dev/null
4892 }
4893
4894 test_101f() {
4895     local file=$DIR/$tfile
4896     local nreads=1000
4897
4898     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
4899     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
4900     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
4901     trap cleanup_test101f EXIT
4902
4903     echo Cancel LRU locks on lustre client to flush the client cache
4904     cancel_lru_locks osc
4905
4906     echo Reset readahead stats
4907     $LCTL set_param -n llite.*.read_ahead_stats 0
4908     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
4909     # readahead should read in 2M file on second read, so only miss
4910     # 2 pages.
4911     echo Random 4K reads on 2M file for 1000 times
4912     $READS -f $file -s 2097152 -b 4096 -n $nreads
4913
4914     echo checking missing pages
4915     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
4916           get_named_value 'misses' | cut -d" " -f1 | calc_total)
4917
4918     [ $miss -lt 3 ] || error "misses too much pages!"
4919     cleanup_test101f
4920 }
4921 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
4922
4923 setup_test102() {
4924         mkdir -p $DIR/$tdir
4925         chown $RUNAS_ID $DIR/$tdir
4926         STRIPE_SIZE=65536
4927         STRIPE_OFFSET=1
4928         STRIPE_COUNT=$OSTCOUNT
4929         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
4930
4931         trap cleanup_test102 EXIT
4932         cd $DIR
4933         $1 $SETSTRIPE $tdir -s $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT
4934         cd $DIR/$tdir
4935         for num in 1 2 3 4
4936         do
4937                 for count in `seq 1 $STRIPE_COUNT`
4938                 do
4939                         for offset in `seq 0 $[$STRIPE_COUNT - 1]`
4940                         do
4941                                 local stripe_size=`expr $STRIPE_SIZE \* $num`
4942                                 local file=file"$num-$offset-$count"
4943                                 $1 $SETSTRIPE $file -s $stripe_size -i $offset -c $count
4944                         done
4945                 done
4946         done
4947
4948         cd $DIR
4949         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
4950 }
4951
4952 cleanup_test102() {
4953         trap 0
4954         rm -f $TMP/f102.tar
4955         rm -rf $DIR/d0.sanity/d102
4956 }
4957
4958 test_102a() {
4959         local testfile=$DIR/xattr_testfile
4960
4961         rm -f $testfile
4962         touch $testfile
4963
4964         [ "$UID" != 0 ] && skip_env "must run as root" && return
4965         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] && skip_env "must have user_xattr" && return
4966
4967         [ -z "$(which setfattr 2>/dev/null)" ] && skip_env "could not find setfattr" && return
4968
4969         echo "set/get xattr..."
4970         setfattr -n trusted.name1 -v value1 $testfile || error
4971         [ "`getfattr -n trusted.name1 $testfile 2> /dev/null | \
4972         grep "trusted.name1"`" == "trusted.name1=\"value1\"" ] || error
4973
4974         setfattr -n user.author1 -v author1 $testfile || error
4975         [ "`getfattr -n user.author1 $testfile 2> /dev/null | \
4976         grep "user.author1"`" == "user.author1=\"author1\"" ] || error
4977
4978         echo "listxattr..."
4979         setfattr -n trusted.name2 -v value2 $testfile || error
4980         setfattr -n trusted.name3 -v value3 $testfile || error
4981         [ `getfattr -d -m "^trusted" $testfile 2> /dev/null | \
4982         grep "trusted.name" | wc -l` -eq 3 ] || error
4983
4984
4985         setfattr -n user.author2 -v author2 $testfile || error
4986         setfattr -n user.author3 -v author3 $testfile || error
4987         [ `getfattr -d -m "^user" $testfile 2> /dev/null | \
4988         grep "user" | wc -l` -eq 3 ] || error
4989
4990         echo "remove xattr..."
4991         setfattr -x trusted.name1 $testfile || error
4992         getfattr -d -m trusted $testfile 2> /dev/null | \
4993         grep "trusted.name1" && error || true
4994
4995         setfattr -x user.author1 $testfile || error
4996         getfattr -d -m user $testfile 2> /dev/null | \
4997         grep "user.author1" && error || true
4998
4999         # b10667: setting lustre special xattr be silently discarded
5000         echo "set lustre special xattr ..."
5001         setfattr -n "trusted.lov" -v "invalid value" $testfile || error
5002
5003         rm -f $testfile
5004 }
5005 run_test 102a "user xattr test =================================="
5006
5007 test_102b() {
5008         # b10930: get/set/list trusted.lov xattr
5009         echo "get/set/list trusted.lov xattr ..."
5010         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5011         local testfile=$DIR/$tfile
5012         $SETSTRIPE -s 65536 -i 1 -c 2 $testfile || error "setstripe failed"
5013         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5014         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5015
5016         local testfile2=${testfile}2
5017         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5018                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5019
5020         $MCREATE $testfile2
5021         setfattr -n trusted.lov -v $value $testfile2
5022         local tmp_file=${testfile}3
5023         $GETSTRIPE -v $testfile2 > $tmp_file
5024         local stripe_size=`grep "size"  $tmp_file| awk '{print $2}'`
5025         local stripe_count=`grep "count"  $tmp_file| awk '{print $2}'`
5026         [ "$stripe_size" -eq 65536 ] || error "stripe size $stripe_size != 65536"
5027         [ "$stripe_count" -eq 2 ] || error "stripe count $stripe_count != 2"
5028         rm -f $DIR/$tfile
5029 }
5030 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5031
5032 test_102c() {
5033         # b10930: get/set/list lustre.lov xattr
5034         echo "get/set/list lustre.lov xattr ..."
5035         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5036         mkdir -p $DIR/$tdir
5037         chown $RUNAS_ID $DIR/$tdir
5038         local testfile=$DIR/$tdir/$tfile
5039         $RUNAS $SETSTRIPE -s 65536 -i 1 -c 2 $testfile||error "setstripe failed"
5040         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5041         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5042
5043         local testfile2=${testfile}2
5044         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5045                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5046
5047         $RUNAS $MCREATE $testfile2
5048         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5049         local tmp_file=${testfile}3
5050         $RUNAS $GETSTRIPE -v $testfile2 > $tmp_file
5051         local stripe_size=`grep "size"  $tmp_file| awk '{print $2}'`
5052         local stripe_count=`grep "count"  $tmp_file| awk '{print $2}'`
5053         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5054         [ $stripe_count -eq 2 ] || error "stripe count $stripe_count != 2"
5055 }
5056 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5057
5058 compare_stripe_info1() {
5059         local stripe_index_all_zero=1
5060
5061         for num in 1 2 3 4
5062         do
5063                 for count in `seq 1 $STRIPE_COUNT`
5064                 do
5065                         for offset in `seq 0 $[$STRIPE_COUNT - 1]`
5066                         do
5067                                 local size=`expr $STRIPE_SIZE \* $num`
5068                                 local file=file"$num-$offset-$count"
5069                                 get_stripe_info client $PWD/$file "$1"
5070                                 if [ $stripe_size -ne $size ]; then
5071                                         error "$file: different stripe size $stripe_size, expected $size" && return
5072                                 fi
5073                                 if [ $stripe_count -ne $count ]; then
5074                                         error "$file: different stripe count $stripe_count, expected $count" && return
5075                                 fi
5076                                 if [ $stripe_index -ne 0 ]; then
5077                                        stripe_index_all_zero=0
5078                                 fi
5079                         done
5080                 done
5081         done
5082         [ $stripe_index_all_zero -eq 1 ] && error "all files are being extracted starting from OST index 0"
5083         return 0
5084 }
5085
5086 compare_stripe_info2() {
5087         for num in 1 2 3 4
5088         do
5089                 for count in `seq 1 $STRIPE_COUNT`
5090                 do
5091                         for offset in `seq 0 $[$STRIPE_COUNT - 1]`
5092                         do
5093                                 local size=`expr $STRIPE_SIZE \* $num`
5094                                 local file=file"$num-$offset-$count"
5095                                 get_stripe_info client $PWD/$file
5096                                 if [ $stripe_size -ne $size ]; then
5097                                         error "$file: different stripe size $stripe_size, expected $size" && return
5098                                 fi
5099                                 if [ $stripe_count -ne $count ]; then
5100                                         error "$file: different stripe count $stripe_count, expected $count" && return
5101                                 fi
5102                                 if [ $stripe_index -ne $offset ]; then
5103                                         error "$file: different stripe offset $stripe_index, expected $offset" && return
5104                                 fi
5105                         done
5106                 done
5107         done
5108 }
5109
5110 find_lustre_tar() {
5111         [ -n "$(which tar 2>/dev/null)" ] && strings $(which tar) | grep -q lustre && echo tar
5112 }
5113
5114 test_102d() {
5115         # b10930: tar test for trusted.lov xattr
5116         TAR=$(find_lustre_tar)
5117         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5118         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5119         setup_test102
5120         mkdir -p $DIR/d102d
5121         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5122         cd $DIR/d102d/$tdir
5123         compare_stripe_info1
5124 }
5125 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5126
5127 test_102f() {
5128         # b10930: tar test for trusted.lov xattr
5129         TAR=$(find_lustre_tar)
5130         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5131         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5132         setup_test102
5133         mkdir -p $DIR/d102f
5134         cd $DIR
5135         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5136         cd $DIR/d102f/$tdir
5137         compare_stripe_info1
5138 }
5139 run_test 102f "tar copy files, not keep osts ==========="
5140
5141 test_102h() { # bug 15777
5142         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5143                 skip "must have user_xattr" && return
5144         [ -z "$(which setfattr 2>/dev/null)" ] &&
5145                 skip_env "could not find setfattr" && return
5146
5147         XBIG=trusted.big
5148         XSIZE=1024
5149         touch $DIR/$tfile
5150         VALUE=datadatadatadatadatadatadatadata
5151         while [ $(echo $VALUE | wc -c) -lt $XSIZE ]; do
5152                 VALUE="$VALUE$VALUE"
5153         done
5154         log "save $XBIG on $DIR/$tfile"
5155         setfattr -n $XBIG -v "$VALUE" $DIR/$tfile ||
5156                 error "saving $XBIG on $DIR/$tfile failed"
5157         ORIG=$(getfattr -n $XBIG $DIR/$tfile 2> /dev/null | grep $XBIG)
5158         OSIZE=$(echo $ORIG | wc -c)
5159         [ $OSIZE -lt $XSIZE ] && error "set $XBIG too small ($OSIZE < $XSIZE)"
5160
5161         XSML=trusted.sml
5162         log "save $XSML on $DIR/$tfile"
5163         setfattr -n $XSML -v val $DIR/$tfile ||
5164                 error "saving $XSML on $DIR/$tfile failed"
5165         NEW=$(getfattr -n $XBIG $DIR/$tfile 2> /dev/null | grep $XBIG)
5166         if [ "$NEW" != "$ORIG" ]; then
5167                 log "orig: $ORIG"
5168                 log "new: $NEW"
5169                 error "$XBIG different after saving $XSML"
5170         fi
5171
5172         log "grow $XSML on $DIR/$tfile"
5173         setfattr -n $XSML -v "$VALUE" $DIR/$tfile ||
5174                 error "growing $XSML on $DIR/$tfile failed"
5175         NEW=$(getfattr -n $XBIG $DIR/$tfile 2> /dev/null | grep $XBIG)
5176         if [ "$NEW" != "$ORIG" ]; then
5177                 log "orig: $ORIG"
5178                 log "new: $NEW"
5179                 error "$XBIG different after growing $XSML"
5180         fi
5181         log "$XBIG still valid after growing $XSML"
5182         rm -f $file
5183 }
5184 run_test 102h "grow xattr from inside inode to external block"
5185
5186 test_102i() { # bug 17038
5187         touch $DIR/$tfile
5188         ln -s $DIR/$tfile $DIR/${tfile}link
5189         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
5190         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"
5191         rm -f $DIR/$tfile $DIR/${tfile}link
5192 }
5193 run_test 102i "lgetxattr test on symbolic link ============"
5194
5195 test_102j() {
5196         TAR=$(find_lustre_tar)
5197         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5198         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5199         setup_test102 "$RUNAS"
5200         mkdir -p $DIR/d102j
5201         chown $RUNAS_ID $DIR/d102j
5202         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
5203         cd $DIR/d102j/$tdir
5204         compare_stripe_info1 "$RUNAS"
5205 }
5206 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
5207
5208 test_102k() {
5209         touch $DIR/$tfile
5210         # b22187 just check that does not crash for regular file.
5211         setfattr -n trusted.lov $DIR/$tfile
5212         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
5213         local test_kdir=$DIR/d102k
5214         mkdir $test_kdir
5215         local default_size=`$GETSTRIPE -s $test_kdir`
5216         local default_count=`$GETSTRIPE -c $test_kdir`
5217         local default_offset=`$GETSTRIPE -o $test_kdir`
5218         $SETSTRIPE -s 65536 -i 1 -c $OSTCOUNT $test_kdir || error 'dir setstripe failed'
5219         setfattr -n trusted.lov $test_kdir
5220         local stripe_size=`$GETSTRIPE -s $test_kdir`
5221         local stripe_count=`$GETSTRIPE -c $test_kdir`
5222         local stripe_offset=`$GETSTRIPE -o $test_kdir`
5223         [ $stripe_size -eq $default_size ] || error "stripe size $stripe_size != $default_size"
5224         [ $stripe_count -eq $default_count ] || error "stripe count $stripe_count != $default_count"
5225         [ $stripe_offset -eq $default_offset ] || error "stripe offset $stripe_offset != $default_offset"
5226         rm -rf $DIR/$tfile $test_kdir
5227 }
5228 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
5229
5230 test_102l() {
5231         # LU-532 trusted. xattr is invisible to non-root
5232         local testfile=$DIR/$tfile
5233
5234         touch $testfile
5235
5236         echo "listxattr as user..."
5237         chown $RUNAS_ID $testfile
5238         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
5239             grep -q "trusted" &&
5240                 error "$testfile trusted xattrs are user visible"
5241
5242         return 0;
5243 }
5244 run_test 102l "listxattr filter test =================================="
5245
5246 cleanup_test102
5247
5248 run_acl_subtest()
5249 {
5250     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
5251     return $?
5252 }
5253
5254 test_103 () {
5255     [ "$UID" != 0 ] && skip_env "must run as root" && return
5256     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5257     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
5258     $GSS && skip "could not run under gss" && return
5259
5260     declare -a identity_old
5261
5262     for num in `seq $MDSCOUNT`; do
5263         switch_identity $num true || identity_old[$num]=$?
5264     done
5265
5266     SAVE_UMASK=`umask`
5267     umask 0022
5268     cd $DIR
5269
5270     echo "performing cp ..."
5271     run_acl_subtest cp || error
5272     echo "performing getfacl-noacl..."
5273     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
5274     echo "performing misc..."
5275     run_acl_subtest misc || error  "misc test failed"
5276     echo "performing permissions..."
5277     run_acl_subtest permissions || error "permissions failed"
5278     echo "performing setfacl..."
5279     run_acl_subtest setfacl || error  "setfacl test failed"
5280
5281     # inheritance test got from HP
5282     echo "performing inheritance..."
5283     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
5284     chmod +x make-tree || error "chmod +x failed"
5285     run_acl_subtest inheritance || error "inheritance test failed"
5286     rm -f make-tree
5287
5288     echo "LU-974 ignore umask when acl is enabled..."
5289     mkdir $DIR/974
5290     cd $DIR/974
5291     run_acl_subtest 974 || error "LU-974 test failed"
5292     rm -rf $DIR/974
5293
5294     cd $SAVE_PWD
5295     umask $SAVE_UMASK
5296
5297     for num in `seq $MDSCOUNT`; do
5298         if [ "${identity_old[$num]}" = 1 ]; then
5299             switch_identity $num false || identity_old[$num]=$?
5300         fi
5301     done
5302 }
5303 run_test 103 "acl test ========================================="
5304
5305 test_104a() {
5306         touch $DIR/$tfile
5307         lfs df || error "lfs df failed"
5308         lfs df -ih || error "lfs df -ih failed"
5309         lfs df -h $DIR || error "lfs df -h $DIR failed"
5310         lfs df -i $DIR || error "lfs df -i $DIR failed"
5311         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
5312         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
5313
5314         OSC=`lctl get_param -n devices | awk '/-osc-/ {print $4}' | head -n 1`
5315         lctl --device %$OSC deactivate
5316         lfs df || error "lfs df with deactivated OSC failed"
5317         lctl --device %$OSC recover
5318         lfs df || error "lfs df with reactivated OSC failed"
5319         rm -f $DIR/$tfile
5320 }
5321 run_test 104a "lfs df [-ih] [path] test ========================="
5322
5323 test_104b() {
5324         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5325         chmod 666 /dev/obd
5326         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
5327         if [ $denied_cnt -ne 0 ];
5328         then
5329                     error "lfs check servers test failed"
5330         fi
5331 }
5332 run_test 104b "$RUNAS lfs check servers test ===================="
5333
5334 test_105a() {
5335         # doesn't work on 2.4 kernels
5336         touch $DIR/$tfile
5337         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5338         then
5339                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
5340         else
5341                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
5342         fi
5343         rm -f $DIR/$tfile
5344 }
5345 run_test 105a "flock when mounted without -o flock test ========"
5346
5347 test_105b() {
5348         touch $DIR/$tfile
5349         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5350         then
5351                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
5352         else
5353                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
5354         fi
5355         rm -f $DIR/$tfile
5356 }
5357 run_test 105b "fcntl when mounted without -o flock test ========"
5358
5359 test_105c() {
5360         touch $DIR/$tfile
5361         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5362         then
5363                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
5364         else
5365                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
5366         fi
5367         rm -f $DIR/$tfile
5368 }
5369 run_test 105c "lockf when mounted without -o flock test ========"
5370
5371 test_105d() { # bug 15924
5372         mkdir -p $DIR/$tdir
5373         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5374                 skip "mount w/o flock enabled" && return
5375         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
5376         $LCTL set_param fail_loc=0x80000315
5377         flocks_test 2 $DIR/$tdir
5378 }
5379 run_test 105d "flock race (should not freeze) ========"
5380
5381 test_105e() { # bug 22660 && 22040
5382         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5383                 skip "mount w/o flock enabled" && return
5384         touch $DIR/$tfile
5385         flocks_test 3 $DIR/$tfile
5386 }
5387 run_test 105e "Two conflicting flocks from same process ======="
5388
5389 test_106() { #bug 10921
5390         mkdir -p $DIR/$tdir
5391         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
5392         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
5393 }
5394 run_test 106 "attempt exec of dir followed by chown of that dir"
5395
5396 test_107() {
5397         CDIR=`pwd`
5398         cd $DIR
5399
5400         local file=core
5401         rm -f $file
5402
5403         local save_pattern=$(sysctl -n kernel.core_pattern)
5404         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
5405         sysctl -w kernel.core_pattern=$file
5406         sysctl -w kernel.core_uses_pid=0
5407
5408         ulimit -c unlimited
5409         sleep 60 &
5410         SLEEPPID=$!
5411
5412         sleep 1
5413
5414         kill -s 11 $SLEEPPID
5415         wait $SLEEPPID
5416         if [ -e $file ]; then
5417                 size=`stat -c%s $file`
5418                 [ $size -eq 0 ] && error "Fail to create core file $file"
5419         else
5420                 error "Fail to create core file $file"
5421         fi
5422         rm -f $file
5423         sysctl -w kernel.core_pattern=$save_pattern
5424         sysctl -w kernel.core_uses_pid=$save_uses_pid
5425         cd $CDIR
5426 }
5427 run_test 107 "Coredump on SIG"
5428
5429 test_110() {
5430         mkdir -p $DIR/d110
5431         mkdir $DIR/d110/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || error "mkdir with 255 char fail"
5432         mkdir $DIR/d110/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && error "mkdir with 256 char should fail, but not"
5433         touch $DIR/d110/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx || error "create with 255 char fail"
5434         touch $DIR/d110/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy && error ""create with 256 char should fail, but not
5435
5436         ls -l $DIR/d110
5437     rm -fr $DIR/d110
5438 }
5439 run_test 110 "filename length checking"
5440
5441 test_115() {
5442         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5443             cut -c11-20)
5444         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
5445             return
5446         echo "Starting with $OSTIO_pre threads"
5447
5448         NUMTEST=20000
5449         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
5450         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
5451         echo "$NUMTEST creates/unlinks"
5452         mkdir -p $DIR/$tdir
5453         createmany -o $DIR/$tdir/$tfile $NUMTEST
5454         unlinkmany $DIR/$tdir/$tfile $NUMTEST
5455
5456         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5457             cut -c11-20)
5458
5459         # don't return an error
5460         [ $OSTIO_post -eq $OSTIO_pre ] && echo \
5461             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&\
5462             echo "This may be fine, depending on what ran before this test" &&\
5463             echo "and how fast this system is." && return
5464
5465         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
5466 }
5467 run_test 115 "verify dynamic thread creation===================="
5468
5469 free_min_max () {
5470         wait_delete_completed
5471         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
5472         echo OST kbytes available: ${AVAIL[@]}
5473         MAXI=0; MAXV=${AVAIL[0]}
5474         MINI=0; MINV=${AVAIL[0]}
5475         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
5476             #echo OST $i: ${AVAIL[i]}kb
5477             if [ ${AVAIL[i]} -gt $MAXV ]; then
5478                 MAXV=${AVAIL[i]}; MAXI=$i
5479             fi
5480             if [ ${AVAIL[i]} -lt $MINV ]; then
5481                 MINV=${AVAIL[i]}; MINI=$i
5482             fi
5483         done
5484         echo Min free space: OST $MINI: $MINV
5485         echo Max free space: OST $MAXI: $MAXV
5486 }
5487
5488 test_116() {
5489         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
5490
5491         echo -n "Free space priority "
5492         lctl get_param -n lov.*-clilov-*.qos_prio_free
5493         declare -a AVAIL
5494         free_min_max
5495         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
5496                 return
5497
5498         # generate uneven OSTs
5499         mkdir -p $DIR/$tdir/OST${MINI}
5500         declare -i FILL
5501         FILL=$(($MINV / 4))
5502         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
5503         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
5504         i=0
5505         while [ $FILL -gt 0 ]; do
5506             i=$(($i + 1))
5507             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
5508             FILL=$(($FILL - 2048))
5509             echo -n .
5510         done
5511         FILL=$(($MINV / 4))
5512         sync
5513         sleep_maxage
5514
5515         free_min_max
5516         DIFF=$(($MAXV - $MINV))
5517         DIFF2=$(($DIFF * 100 / $MINV))
5518         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
5519         if [ $DIFF2 -gt 20 ]; then
5520             echo "ok"
5521         else
5522             echo "failed - QOS mode won't be used"
5523             error_ignore "QOS imbalance criteria not met"
5524             return
5525         fi
5526
5527         MINI1=$MINI; MINV1=$MINV
5528         MAXI1=$MAXI; MAXV1=$MAXV
5529
5530         # now fill using QOS
5531         echo writing a bunch of files to QOS-assigned OSTs
5532         $SETSTRIPE $DIR/$tdir -c 1
5533         i=0
5534         while [ $FILL -gt 0 ]; do
5535             i=$(($i + 1))
5536             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
5537             FILL=$(($FILL - 200))
5538             echo -n .
5539         done
5540         echo "wrote $i 200k files"
5541         sync
5542         sleep_maxage
5543
5544         echo "Note: free space may not be updated, so measurements might be off"
5545         free_min_max
5546         DIFF2=$(($MAXV - $MINV))
5547         echo "free space delta: orig $DIFF final $DIFF2"
5548         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
5549         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
5550         echo "Wrote $DIFF to smaller OST $MINI1"
5551         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
5552         echo "Wrote $DIFF2 to larger OST $MAXI1"
5553         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
5554
5555         # Figure out which files were written where
5556         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
5557                awk '/'$MINI1': / {print $2; exit}')
5558         echo $UUID
5559         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
5560         echo "$MINC files created on smaller OST $MINI1"
5561         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
5562                awk '/'$MAXI1': / {print $2; exit}')
5563         echo $UUID
5564         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
5565         echo "$MAXC files created on larger OST $MAXI1"
5566         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
5567         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
5568
5569         rm -rf $DIR/$tdir
5570 }
5571 run_test 116 "stripe QOS: free space balance ==================="
5572
5573 test_117() # bug 10891
5574 {
5575         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
5576         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
5577         lctl set_param fail_loc=0x21e
5578         > $DIR/$tfile || error "truncate failed"
5579         lctl set_param fail_loc=0
5580         echo "Truncate succeeded."
5581         rm -f $DIR/$tfile
5582 }
5583 run_test 117 "verify fsfilt_extend =========="
5584
5585 export OLD_RESENDCOUNT=""
5586 set_resend_count () {
5587         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
5588         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
5589         lctl set_param -n $PROC_RESENDCOUNT $1
5590         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
5591 }
5592
5593 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
5594
5595 # Reset async IO behavior after error case
5596 reset_async() {
5597         FILE=$DIR/reset_async
5598
5599         # Ensure all OSCs are cleared
5600         $LSTRIPE -c -1 $FILE
5601         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
5602         sync
5603         rm $FILE
5604 }
5605
5606 test_118a() #bug 11710
5607 {
5608         reset_async
5609
5610         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5611         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5612         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
5613
5614         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5615                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5616                 return 1;
5617         fi
5618         rm -f $DIR/$tfile
5619 }
5620 run_test 118a "verify O_SYNC works =========="
5621
5622 test_118b()
5623 {
5624         remote_ost_nodsh && skip "remote OST with nodsh" && return
5625
5626         reset_async
5627
5628         #define OBD_FAIL_OST_ENOENT 0x217
5629         set_nodes_failloc "$(osts_nodes)" 0x217
5630         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5631         RC=$?
5632         set_nodes_failloc "$(osts_nodes)" 0
5633         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5634         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5635                     grep -c writeback)
5636
5637         if [[ $RC -eq 0 ]]; then
5638                 error "Must return error due to dropped pages, rc=$RC"
5639                 return 1;
5640         fi
5641
5642         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5643                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5644                 return 1;
5645         fi
5646
5647         echo "Dirty pages not leaked on ENOENT"
5648
5649         # Due to the above error the OSC will issue all RPCs syncronously
5650         # until a subsequent RPC completes successfully without error.
5651         multiop $DIR/$tfile Ow4096yc
5652         rm -f $DIR/$tfile
5653
5654         return 0
5655 }
5656 run_test 118b "Reclaim dirty pages on fatal error =========="
5657
5658 test_118c()
5659 {
5660         remote_ost_nodsh && skip "remote OST with nodsh" && return
5661
5662         reset_async
5663
5664         #define OBD_FAIL_OST_EROFS               0x216
5665         set_nodes_failloc "$(osts_nodes)" 0x216
5666
5667         # multiop should block due to fsync until pages are written
5668         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
5669         MULTIPID=$!
5670         sleep 1
5671
5672         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
5673                 error "Multiop failed to block on fsync, pid=$MULTIPID"
5674         fi
5675
5676         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5677                     grep -c writeback)
5678         if [[ $WRITEBACK -eq 0 ]]; then
5679                 error "No page in writeback, writeback=$WRITEBACK"
5680         fi
5681
5682         set_nodes_failloc "$(osts_nodes)" 0
5683         wait $MULTIPID
5684         RC=$?
5685         if [[ $RC -ne 0 ]]; then
5686                 error "Multiop fsync failed, rc=$RC"
5687         fi
5688
5689         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5690         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5691                     grep -c writeback)
5692         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5693                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5694         fi
5695
5696         rm -f $DIR/$tfile
5697         echo "Dirty pages flushed via fsync on EROFS"
5698         return 0
5699 }
5700 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
5701
5702 test_118d()
5703 {
5704         remote_ost_nodsh && skip "remote OST with nodsh" && return
5705
5706         reset_async
5707
5708         #define OBD_FAIL_OST_BRW_PAUSE_BULK
5709         set_nodes_failloc "$(osts_nodes)" 0x214
5710         # multiop should block due to fsync until pages are written
5711         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
5712         MULTIPID=$!
5713         sleep 1
5714
5715         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
5716                 error "Multiop failed to block on fsync, pid=$MULTIPID"
5717         fi
5718
5719         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5720                     grep -c writeback)
5721         if [[ $WRITEBACK -eq 0 ]]; then
5722                 error "No page in writeback, writeback=$WRITEBACK"
5723         fi
5724
5725         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
5726         set_nodes_failloc "$(osts_nodes)" 0
5727
5728         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5729         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5730                     grep -c writeback)
5731         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5732                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5733         fi
5734
5735         rm -f $DIR/$tfile
5736         echo "Dirty pages gaurenteed flushed via fsync"
5737         return 0
5738 }
5739 run_test 118d "Fsync validation inject a delay of the bulk =========="
5740
5741 test_118f() {
5742         reset_async
5743
5744         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
5745         lctl set_param fail_loc=0x8000040a
5746
5747         # Should simulate EINVAL error which is fatal
5748         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5749         RC=$?
5750         if [[ $RC -eq 0 ]]; then
5751                 error "Must return error due to dropped pages, rc=$RC"
5752         fi
5753
5754         lctl set_param fail_loc=0x0
5755
5756         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
5757         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5758         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5759                     grep -c writeback)
5760         if [[ $LOCKED -ne 0 ]]; then
5761                 error "Locked pages remain in cache, locked=$LOCKED"
5762         fi
5763
5764         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5765                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5766         fi
5767
5768         rm -f $DIR/$tfile
5769         echo "No pages locked after fsync"
5770
5771         reset_async
5772         return 0
5773 }
5774 run_test 118f "Simulate unrecoverable OSC side error =========="
5775
5776 test_118g() {
5777         reset_async
5778
5779         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5780         lctl set_param fail_loc=0x406
5781
5782         # simulate local -ENOMEM
5783         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5784         RC=$?
5785
5786         lctl set_param fail_loc=0
5787         if [[ $RC -eq 0 ]]; then
5788                 error "Must return error due to dropped pages, rc=$RC"
5789         fi
5790
5791         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
5792         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5793         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5794                         grep -c writeback)
5795         if [[ $LOCKED -ne 0 ]]; then
5796                 error "Locked pages remain in cache, locked=$LOCKED"
5797         fi
5798
5799         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5800                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5801         fi
5802
5803         rm -f $DIR/$tfile
5804         echo "No pages locked after fsync"
5805
5806         reset_async
5807         return 0
5808 }
5809 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
5810
5811 test_118h() {
5812         remote_ost_nodsh && skip "remote OST with nodsh" && return
5813
5814         reset_async
5815
5816         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
5817         set_nodes_failloc "$(osts_nodes)" 0x20e
5818         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
5819         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5820         RC=$?
5821
5822         set_nodes_failloc "$(osts_nodes)" 0
5823         if [[ $RC -eq 0 ]]; then
5824                 error "Must return error due to dropped pages, rc=$RC"
5825         fi
5826
5827         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
5828         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5829         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5830                     grep -c writeback)
5831         if [[ $LOCKED -ne 0 ]]; then
5832                 error "Locked pages remain in cache, locked=$LOCKED"
5833         fi
5834
5835         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5836                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5837         fi
5838
5839         rm -f $DIR/$tfile
5840         echo "No pages locked after fsync"
5841
5842         return 0
5843 }
5844 run_test 118h "Verify timeout in handling recoverables errors  =========="
5845
5846 test_118i() {
5847         remote_ost_nodsh && skip "remote OST with nodsh" && return
5848
5849         reset_async
5850
5851         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
5852         set_nodes_failloc "$(osts_nodes)" 0x20e
5853
5854         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
5855         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
5856         PID=$!
5857         sleep 5
5858         set_nodes_failloc "$(osts_nodes)" 0
5859
5860         wait $PID
5861         RC=$?
5862         if [[ $RC -ne 0 ]]; then
5863                 error "got error, but should be not, rc=$RC"
5864         fi
5865
5866         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
5867         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5868         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
5869         if [[ $LOCKED -ne 0 ]]; then
5870                 error "Locked pages remain in cache, locked=$LOCKED"
5871         fi
5872
5873         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5874                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5875         fi
5876
5877         rm -f $DIR/$tfile
5878         echo "No pages locked after fsync"
5879
5880         return 0
5881 }
5882 run_test 118i "Fix error before timeout in recoverable error  =========="
5883
5884 test_118j() {
5885         remote_ost_nodsh && skip "remote OST with nodsh" && return
5886
5887         reset_async
5888
5889         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
5890         set_nodes_failloc "$(osts_nodes)" 0x220
5891
5892         # return -EIO from OST
5893         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5894         RC=$?
5895         set_nodes_failloc "$(osts_nodes)" 0x0
5896         if [[ $RC -eq 0 ]]; then
5897                 error "Must return error due to dropped pages, rc=$RC"
5898         fi
5899
5900         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
5901         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5902         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
5903         if [[ $LOCKED -ne 0 ]]; then
5904                 error "Locked pages remain in cache, locked=$LOCKED"
5905         fi
5906
5907         # in recoverable error on OST we want resend and stay until it finished
5908         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5909                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5910         fi
5911
5912         rm -f $DIR/$tfile
5913         echo "No pages locked after fsync"
5914
5915         return 0
5916 }
5917 run_test 118j "Simulate unrecoverable OST side error =========="
5918
5919 test_118k()
5920 {
5921         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
5922
5923         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
5924         set_nodes_failloc "$(osts_nodes)" 0x20e
5925         mkdir -p $DIR/$tdir
5926
5927         for ((i=0;i<10;i++)); do
5928                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
5929                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
5930                 SLEEPPID=$!
5931                 sleep 0.500s
5932                 kill $SLEEPPID
5933                 wait $SLEEPPID
5934         done
5935
5936         set_nodes_failloc "$(osts_nodes)" 0
5937         rm -rf $DIR/$tdir
5938 }
5939 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
5940
5941 test_118l()
5942 {
5943         # LU-646
5944         mkdir -p $DIR/$tdir
5945         multiop $DIR/$tdir Dy || error "fsync dir failed"
5946         rm -rf $DIR/$tdir
5947 }
5948 run_test 118l "fsync dir ========="
5949
5950 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
5951
5952 test_119a() # bug 11737
5953 {
5954         BSIZE=$((512 * 1024))
5955         directio write $DIR/$tfile 0 1 $BSIZE
5956         # We ask to read two blocks, which is more than a file size.
5957         # directio will indicate an error when requested and actual
5958         # sizes aren't equeal (a normal situation in this case) and
5959         # print actual read amount.
5960         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
5961         if [ "$NOB" != "$BSIZE" ]; then
5962                 error "read $NOB bytes instead of $BSIZE"
5963         fi
5964         rm -f $DIR/$tfile
5965 }
5966 run_test 119a "Short directIO read must return actual read amount"
5967
5968 test_119b() # bug 11737
5969 {
5970         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5971
5972         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
5973         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
5974         sync
5975         multiop $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
5976                 error "direct read failed"
5977         rm -f $DIR/$tfile
5978 }
5979 run_test 119b "Sparse directIO read must return actual read amount"
5980
5981 test_119c() # bug 13099
5982 {
5983         BSIZE=1048576
5984         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
5985         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
5986         rm -f $DIR/$tfile
5987 }
5988 run_test 119c "Testing for direct read hitting hole"
5989
5990 test_119d() # bug 15950
5991 {
5992         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
5993         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
5994         BSIZE=1048576
5995         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
5996         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
5997         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
5998         lctl set_param fail_loc=0x40d
5999         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6000         pid_dio=$!
6001         sleep 1
6002         cat $DIR/$tfile > /dev/null &
6003         lctl set_param fail_loc=0
6004         pid_reads=$!
6005         wait $pid_dio
6006         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6007         sleep 2
6008         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6009         error "the read rpcs have not completed in 2s"
6010         rm -f $DIR/$tfile
6011         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6012 }
6013 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6014
6015 test_120a() {
6016         mkdir -p $DIR/$tdir
6017         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6018                skip "no early lock cancel on server" && return 0
6019         lru_resize_disable mdc
6020         lru_resize_disable osc
6021         cancel_lru_locks mdc
6022         stat $DIR/$tdir > /dev/null
6023         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6024         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6025         mkdir $DIR/$tdir/d1
6026         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6027         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6028         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6029         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6030         lru_resize_enable mdc
6031         lru_resize_enable osc
6032 }
6033 run_test 120a "Early Lock Cancel: mkdir test"
6034
6035 test_120b() {
6036         mkdir -p $DIR/$tdir
6037         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6038                skip "no early lock cancel on server" && return 0
6039         lru_resize_disable mdc
6040         lru_resize_disable osc
6041         cancel_lru_locks mdc
6042         stat $DIR/$tdir > /dev/null
6043         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6044         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6045         touch $DIR/$tdir/f1
6046         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6047         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6048         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6049         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6050         lru_resize_enable mdc
6051         lru_resize_enable osc
6052 }
6053 run_test 120b "Early Lock Cancel: create test"
6054
6055 test_120c() {
6056         mkdir -p $DIR/$tdir
6057         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6058                skip "no early lock cancel on server" && return 0
6059         lru_resize_disable mdc
6060         lru_resize_disable osc
6061         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6062         touch $DIR/$tdir/d1/f1
6063         cancel_lru_locks mdc
6064         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6065         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6066         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6067         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6068         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6069         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6070         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6071         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6072         lru_resize_enable mdc
6073         lru_resize_enable osc
6074 }
6075 run_test 120c "Early Lock Cancel: link test"
6076
6077 test_120d() {
6078         mkdir -p $DIR/$tdir
6079         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6080                skip "no early lock cancel on server" && return 0
6081         lru_resize_disable mdc
6082         lru_resize_disable osc
6083         touch $DIR/$tdir
6084         cancel_lru_locks mdc
6085         stat $DIR/$tdir > /dev/null
6086         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6087         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6088         chmod a+x $DIR/$tdir
6089         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6090         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6091         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6092         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6093         lru_resize_enable mdc
6094         lru_resize_enable osc
6095 }
6096 run_test 120d "Early Lock Cancel: setattr test"
6097
6098 test_120e() {
6099         mkdir -p $DIR/$tdir
6100         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6101                skip "no early lock cancel on server" && return 0
6102         lru_resize_disable mdc
6103         lru_resize_disable osc
6104         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6105         cancel_lru_locks mdc
6106         cancel_lru_locks osc
6107         dd if=$DIR/$tdir/f1 of=/dev/null
6108         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
6109         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6110               awk '/ldlm_cancel/ {print $2}'`
6111         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6112               awk '/ldlm_bl_callback/ {print $2}'`
6113         unlink $DIR/$tdir/f1
6114         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6115               awk '/ldlm_cancel/ {print $2}'`
6116         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6117               awk '/ldlm_bl_callback/ {print $2}'`
6118         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6119         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6120         lru_resize_enable mdc
6121         lru_resize_enable osc
6122 }
6123 run_test 120e "Early Lock Cancel: unlink test"
6124
6125 test_120f() {
6126         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6127                skip "no early lock cancel on server" && return 0
6128         mkdir -p $DIR/$tdir
6129         lru_resize_disable mdc
6130         lru_resize_disable osc
6131         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6132         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
6133         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
6134         cancel_lru_locks mdc
6135         cancel_lru_locks osc
6136         dd if=$DIR/$tdir/d1/f1 of=/dev/null
6137         dd if=$DIR/$tdir/d2/f2 of=/dev/null
6138         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
6139         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6140               awk '/ldlm_cancel/ {print $2}'`
6141         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6142               awk '/ldlm_bl_callback/ {print $2}'`
6143         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6144         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6145               awk '/ldlm_cancel/ {print $2}'`
6146         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6147               awk '/ldlm_bl_callback/ {print $2}'`
6148         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6149         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6150         lru_resize_enable mdc
6151         lru_resize_enable osc
6152 }
6153 run_test 120f "Early Lock Cancel: rename test"
6154
6155 test_120g() {
6156         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6157                skip "no early lock cancel on server" && return 0
6158         lru_resize_disable mdc
6159         lru_resize_disable osc
6160         count=10000
6161         echo create $count files
6162         mkdir -p $DIR/$tdir
6163         cancel_lru_locks mdc
6164         cancel_lru_locks osc
6165         t0=`date +%s`
6166
6167         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6168               awk '/ldlm_cancel/ {print $2}'`
6169         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6170               awk '/ldlm_bl_callback/ {print $2}'`
6171         createmany -o $DIR/$tdir/f $count
6172         sync
6173         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6174               awk '/ldlm_cancel/ {print $2}'`
6175         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6176               awk '/ldlm_bl_callback/ {print $2}'`
6177         t1=`date +%s`
6178         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
6179         echo rm $count files
6180         rm -r $DIR/$tdir
6181         sync
6182         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6183               awk '/ldlm_cancel/ {print $2}'`
6184         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6185               awk '/ldlm_bl_callback/ {print $2}'`
6186         t2=`date +%s`
6187         echo total: $count removes in $((t2-t1))
6188         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
6189         sleep 2
6190         # wait for commitment of removal
6191         lru_resize_enable mdc
6192         lru_resize_enable osc
6193 }
6194 run_test 120g "Early Lock Cancel: performance test"
6195
6196 test_121() { #bug #10589
6197         rm -rf $DIR/$tfile
6198         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
6199 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
6200         lctl set_param fail_loc=0x310
6201         cancel_lru_locks osc > /dev/null
6202         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
6203         lctl set_param fail_loc=0
6204         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
6205 }
6206 run_test 121 "read cancel race ========="
6207
6208 test_123a() { # was test 123, statahead(bug 11401)
6209         SLOWOK=0
6210         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
6211             log "testing on UP system. Performance may be not as good as expected."
6212                         SLOWOK=1
6213         fi
6214
6215         rm -rf $DIR/$tdir
6216         mkdir -p $DIR/$tdir
6217         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6218         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
6219         MULT=10
6220         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
6221                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
6222
6223                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6224                 lctl set_param -n llite.*.statahead_max 0
6225                 lctl get_param llite.*.statahead_max
6226                 cancel_lru_locks mdc
6227                 cancel_lru_locks osc
6228                 stime=`date +%s`
6229                 time ls -l $DIR/$tdir | wc -l
6230                 etime=`date +%s`
6231                 delta=$((etime - stime))
6232                 log "ls $i files without statahead: $delta sec"
6233                 lctl set_param llite.*.statahead_max=$max
6234
6235                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6236                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
6237                 cancel_lru_locks mdc
6238                 cancel_lru_locks osc
6239                 stime=`date +%s`
6240                 time ls -l $DIR/$tdir | wc -l
6241                 etime=`date +%s`
6242                 delta_sa=$((etime - stime))
6243                 log "ls $i files with statahead: $delta_sa sec"
6244                 lctl get_param -n llite.*.statahead_stats
6245                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6246
6247                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
6248                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
6249
6250                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6251                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6252                     lctl set_param -n llite.*.statahead_max 0
6253                     lctl get_param llite.*.statahead_max
6254                     cancel_lru_locks mdc
6255                     cancel_lru_locks osc
6256                     stime=`date +%s`
6257                     time ls -l $DIR/$tdir | wc -l
6258                     etime=`date +%s`
6259                     delta=$((etime - stime))
6260                     log "ls $i files again without statahead: $delta sec"
6261                     lctl set_param llite.*.statahead_max=$max
6262                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6263                         if [  $SLOWOK -eq 0 ]; then
6264                                 error "ls $i files is slower with statahead!"
6265                         else
6266                                 log "ls $i files is slower with statahead!"
6267                         fi
6268                         break
6269                     fi
6270                 fi
6271
6272                 [ $delta -gt 20 ] && break
6273                 [ $delta -gt 8 ] && MULT=$((50 / delta))
6274                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
6275         done
6276         log "ls done"
6277
6278         stime=`date +%s`
6279         rm -r $DIR/$tdir
6280         sync
6281         etime=`date +%s`
6282         delta=$((etime - stime))
6283         log "rm -r $DIR/$tdir/: $delta seconds"
6284         log "rm done"
6285         lctl get_param -n llite.*.statahead_stats
6286 }
6287 run_test 123a "verify statahead work"
6288
6289 test_123b () { # statahead(bug 15027)
6290         mkdir -p $DIR/$tdir
6291         createmany -o $DIR/$tdir/$tfile-%d 1000
6292
6293         cancel_lru_locks mdc
6294         cancel_lru_locks osc
6295
6296 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
6297         lctl set_param fail_loc=0x80000803
6298         ls -lR $DIR/$tdir > /dev/null
6299         log "ls done"
6300         lctl set_param fail_loc=0x0
6301         lctl get_param -n llite.*.statahead_stats
6302         rm -r $DIR/$tdir
6303         sync
6304
6305 }
6306 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
6307
6308 test_124a() {
6309         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6310                skip "no lru resize on server" && return 0
6311         local NR=2000
6312         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
6313
6314         log "create $NR files at $DIR/$tdir"
6315         createmany -o $DIR/$tdir/f $NR ||
6316                 error "failed to create $NR files in $DIR/$tdir"
6317
6318         cancel_lru_locks mdc
6319         ls -l $DIR/$tdir > /dev/null
6320
6321         local NSDIR=""
6322         local LRU_SIZE=0
6323         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
6324                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6325                 LRU_SIZE=$(lctl get_param -n $PARAM)
6326                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
6327                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
6328                                                 log "NSDIR=$NSDIR"
6329                         log "NS=$(basename $NSDIR)"
6330                         break
6331                 fi
6332         done
6333
6334         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
6335                 skip "Not enough cached locks created!"
6336                 return 0
6337         fi
6338         log "LRU=$LRU_SIZE"
6339
6340         local SLEEP=30
6341
6342         # We know that lru resize allows one client to hold $LIMIT locks
6343         # for 10h. After that locks begin to be killed by client.
6344         local MAX_HRS=10
6345         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
6346                 log "LIMIT=$LIMIT"
6347
6348         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
6349         # killing locks. Some time was spent for creating locks. This means
6350         # that up to the moment of sleep finish we must have killed some of
6351         # them (10-100 locks). This depends on how fast ther were created.
6352         # Many of them were touched in almost the same moment and thus will
6353         # be killed in groups.
6354         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
6355
6356         # Use $LRU_SIZE_B here to take into account real number of locks
6357         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
6358         local LRU_SIZE_B=$LRU_SIZE
6359         log "LVF=$LVF"
6360         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
6361                 log "OLD_LVF=$OLD_LVF"
6362         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
6363
6364         # Let's make sure that we really have some margin. Client checks
6365         # cached locks every 10 sec.
6366         SLEEP=$((SLEEP+20))
6367         log "Sleep ${SLEEP} sec"
6368         local SEC=0
6369         while ((SEC<$SLEEP)); do
6370                 echo -n "..."
6371                 sleep 5
6372                 SEC=$((SEC+5))
6373                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
6374                 echo -n "$LRU_SIZE"
6375         done
6376         echo ""
6377         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
6378         local LRU_SIZE_A=`lctl get_param -n $NSDIR/lru_size`
6379
6380         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
6381                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
6382                 unlinkmany $DIR/$tdir/f $NR
6383                 return
6384         }
6385
6386         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
6387         log "unlink $NR files at $DIR/$tdir"
6388         unlinkmany $DIR/$tdir/f $NR
6389 }
6390 run_test 124a "lru resize ======================================="
6391
6392 get_max_pool_limit()
6393 {
6394         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
6395         local max=0
6396         for l in $limit; do
6397                 if test $l -gt $max; then
6398                         max=$l
6399                 fi
6400         done
6401         echo $max
6402 }
6403
6404 test_124b() {
6405         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6406                skip "no lru resize on server" && return 0
6407
6408         LIMIT=`get_max_pool_limit`
6409
6410         NR=$(($(default_lru_size)*20))
6411         if [ $NR -gt $LIMIT ]; then
6412                 log "Limit lock number by $LIMIT locks"
6413                 NR=$LIMIT
6414         fi
6415         lru_resize_disable mdc
6416         mkdir -p $DIR/$tdir/disable_lru_resize ||
6417                 error "failed to create $DIR/$tdir/disable_lru_resize"
6418
6419         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
6420         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
6421         cancel_lru_locks mdc
6422         stime=`date +%s`
6423         PID=""
6424         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6425         PID="$PID $!"
6426         sleep 2
6427         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6428         PID="$PID $!"
6429         sleep 2
6430         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6431         PID="$PID $!"
6432         wait $PID
6433         etime=`date +%s`
6434         nolruresize_delta=$((etime-stime))
6435         log "ls -la time: $nolruresize_delta seconds"
6436         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6437         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
6438
6439         lru_resize_enable mdc
6440         mkdir -p $DIR/$tdir/enable_lru_resize ||
6441                 error "failed to create $DIR/$tdir/enable_lru_resize"
6442
6443         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
6444         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
6445         cancel_lru_locks mdc
6446         stime=`date +%s`
6447         PID=""
6448         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6449         PID="$PID $!"
6450         sleep 2
6451         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6452         PID="$PID $!"
6453         sleep 2
6454         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6455         PID="$PID $!"
6456         wait $PID
6457         etime=`date +%s`
6458         lruresize_delta=$((etime-stime))
6459         log "ls -la time: $lruresize_delta seconds"
6460         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6461
6462         if [ $lruresize_delta -gt $nolruresize_delta ]; then
6463                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
6464         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
6465                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
6466         else
6467                 log "lru resize performs the same with no lru resize"
6468         fi
6469         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
6470 }
6471 run_test 124b "lru resize (performance test) ======================="
6472
6473 test_125() { # 13358
6474         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6475         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6476         mkdir -p $DIR/d125 || error "mkdir failed"
6477         $SETSTRIPE $DIR/d125 -s 65536 -c -1 || error "setstripe failed"
6478         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
6479         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
6480 }
6481 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
6482
6483 test_126() { # bug 12829/13455
6484         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6485         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
6486         $GSS && skip "must run as gss disabled" && return
6487
6488         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
6489         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
6490         rm -f $DIR/$tfile
6491         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
6492 }
6493 run_test 126 "check that the fsgid provided by the client is taken into account"
6494
6495 test_127a() { # bug 15521
6496         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
6497         $LCTL set_param osc.*.stats=0
6498         FSIZE=$((2048 * 1024))
6499         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6500         cancel_lru_locks osc
6501         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
6502
6503         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
6504         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
6505                 echo "got $COUNT $NAME"
6506                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
6507                 eval $NAME=$COUNT || error "Wrong proc format"
6508
6509                 case $NAME in
6510                         read_bytes|write_bytes)
6511                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
6512                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
6513                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
6514                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
6515                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
6516                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
6517                                 error "sumsquare is too small: $SUMSQ"
6518                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
6519                                 error "sumsquare is too big: $SUMSQ"
6520                         ;;
6521                         *) ;;
6522                 esac
6523         done < $DIR/${tfile}.tmp
6524
6525         #check that we actually got some stats
6526         [ "$read_bytes" ] || error "Missing read_bytes stats"
6527         [ "$write_bytes" ] || error "Missing write_bytes stats"
6528         [ "$read_bytes" != 0 ] || error "no read done"
6529         [ "$write_bytes" != 0 ] || error "no write done"
6530 }
6531 run_test 127a "verify the client stats are sane"
6532
6533 test_127b() { # bug LU-333
6534         $LCTL set_param llite.*.stats=0
6535         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
6536         # perform 2 reads and writes so MAX is different from SUM.
6537         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6538         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6539         cancel_lru_locks osc
6540         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
6541         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
6542
6543         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
6544         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
6545                 echo "got $COUNT $NAME"
6546                 eval $NAME=$COUNT || error "Wrong proc format"
6547
6548         case $NAME in
6549                 read_bytes)
6550                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
6551                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
6552                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
6553                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
6554                         ;;
6555                 write_bytes)
6556                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
6557                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
6558                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
6559                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
6560                         ;;
6561                         *) ;;
6562                 esac
6563         done < $TMP/${tfile}.tmp
6564
6565         #check that we actually got some stats
6566         [ "$read_bytes" ] || error "Missing read_bytes stats"
6567         [ "$write_bytes" ] || error "Missing write_bytes stats"
6568         [ "$read_bytes" != 0 ] || error "no read done"
6569         [ "$write_bytes" != 0 ] || error "no write done"
6570 }
6571 run_test 127b "verify the llite client stats are sane"
6572
6573 test_128() { # bug 15212
6574         touch $DIR/$tfile
6575         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
6576                 find $DIR/$tfile
6577                 find $DIR/$tfile
6578         EOF
6579
6580         result=$(grep error $TMP/$tfile.log)
6581         rm -f $DIR/$tfile
6582         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
6583 }
6584 run_test 128 "interactive lfs for 2 consecutive find's"
6585
6586 set_dir_limits () {
6587         local mntdev
6588         local canondev
6589         local node
6590
6591         local LDPROC=/proc/fs/ldiskfs
6592
6593         for facet in $(get_facets MDS); do
6594                 canondev=$(ldiskfs_canon *.$(convert_facet2label $facet).mntdev $facet)
6595                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" || LDPROC=/sys/fs/ldiskfs
6596                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
6597         done
6598 }
6599 test_129() {
6600         [ "$FSTYPE" != "ldiskfs" ] && skip "not needed for FSTYPE=$FSTYPE" && return 0
6601         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6602
6603         EFBIG=27
6604         MAX=16384
6605
6606         set_dir_limits $MAX
6607
6608         mkdir -p $DIR/$tdir
6609
6610         I=0
6611         J=0
6612         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
6613                 multiop $DIR/$tdir/$J Oc
6614                 rc=$?
6615                 if [ $rc -eq $EFBIG ]; then
6616                         set_dir_limits 0
6617                         echo "return code $rc received as expected"
6618                         return 0
6619                 elif [ $rc -ne 0 ]; then
6620                         set_dir_limits 0
6621                         error_exit "return code $rc received instead of expected $EFBIG"
6622                 fi
6623                 J=$((J+1))
6624                 I=$(stat -c%s "$DIR/$tdir")
6625         done
6626
6627         set_dir_limits 0
6628         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
6629 }
6630 run_test 129 "test directory size limit ========================"
6631
6632 OLDIFS="$IFS"
6633 cleanup_130() {
6634         trap 0
6635         IFS="$OLDIFS"
6636 }
6637
6638 test_130a() {
6639         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
6640         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
6641
6642         trap cleanup_130 EXIT RETURN
6643
6644         local fm_file=$DIR/$tfile
6645         lfs setstripe -s 65536 -c 1 $fm_file || error "setstripe failed on $fm_file"
6646         dd if=/dev/zero of=$fm_file bs=65536 count=1 || error "dd failed for $fm_file"
6647
6648         filefrag -ves $fm_file || error "filefrag $fm_file failed"
6649         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
6650
6651         lun=`$GETSTRIPE $fm_file  | grep -A 10 obdidx | awk '{print $1}' | grep -v "obdidx"`
6652
6653         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
6654         IFS=$'\n'
6655         tot_len=0
6656         for line in $filefrag_op
6657         do
6658                 frag_lun=`echo $line | cut -d: -f5`
6659                 ext_len=`echo $line | cut -d: -f4`
6660                 if (( $frag_lun != $lun )); then
6661                         cleanup_130
6662                         error "FIEMAP on 1-stripe file($fm_file) failed"
6663                         return
6664                 fi
6665                 (( tot_len += ext_len ))
6666         done
6667
6668         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
6669                 cleanup_130
6670                 error "FIEMAP on 1-stripe file($fm_file) failed;"
6671                 return
6672         fi
6673
6674         cleanup_130
6675
6676         echo "FIEMAP on single striped file succeeded"
6677 }
6678 run_test 130a "FIEMAP (1-stripe file)"
6679
6680 test_130b() {
6681         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping FIEMAP on 2-stripe file test" && return
6682
6683         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
6684         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
6685
6686         trap cleanup_130 EXIT RETURN
6687
6688         local fm_file=$DIR/$tfile
6689         lfs setstripe -s 65536 -c 2 $fm_file || error "setstripe failed on $fm_file"
6690         dd if=/dev/zero of=$fm_file bs=1M count=2 || error "dd failed on $fm_file"
6691
6692         filefrag -ves $fm_file || error "filefrag $fm_file failed"
6693         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
6694
6695         last_lun=`echo $filefrag_op | cut -d: -f5`
6696
6697         IFS=$'\n'
6698         tot_len=0
6699         num_luns=1
6700         for line in $filefrag_op
6701         do
6702                 frag_lun=`echo $line | cut -d: -f5`
6703                 ext_len=`echo $line | cut -d: -f4`
6704                 if (( $frag_lun != $last_lun )); then
6705                         if (( tot_len != 1024 )); then
6706                                 cleanup_130
6707                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
6708                                 return
6709                         else
6710                                 (( num_luns += 1 ))
6711                                 tot_len=0
6712                         fi
6713                 fi
6714                 (( tot_len += ext_len ))
6715                 last_lun=$frag_lun
6716         done
6717         if (( num_luns != 2 || tot_len != 1024 )); then
6718                 cleanup_130
6719                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
6720                 return
6721         fi
6722
6723         cleanup_130
6724
6725         echo "FIEMAP on 2-stripe file succeeded"
6726 }
6727 run_test 130b "FIEMAP (2-stripe file)"
6728
6729 test_130c() {
6730         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping FIEMAP on 2-stripe file with hole test" && return
6731
6732         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
6733         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
6734
6735         trap cleanup_130 EXIT RETURN
6736
6737         local fm_file=$DIR/$tfile
6738         lfs setstripe -s 65536 -c 2 $fm_file || error "setstripe failed on $fm_file"
6739         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
6740
6741         filefrag -ves $fm_file || error "filefrag $fm_file failed"
6742         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
6743
6744         last_lun=`echo $filefrag_op | cut -d: -f5`
6745
6746         IFS=$'\n'
6747         tot_len=0
6748         num_luns=1
6749         for line in $filefrag_op
6750         do
6751                 frag_lun=`echo $line | cut -d: -f5`
6752                 ext_len=`echo $line | cut -d: -f4`
6753                 if (( $frag_lun != $last_lun )); then
6754                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
6755                         if (( logical != 512 )); then
6756                                 cleanup_130
6757                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
6758                                 return
6759                         fi
6760                         if (( tot_len != 512 )); then
6761                                 cleanup_130
6762                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
6763                                 return
6764                         else
6765                                 (( num_luns += 1 ))
6766                                 tot_len=0
6767                         fi
6768                 fi
6769                 (( tot_len += ext_len ))
6770                 last_lun=$frag_lun
6771         done
6772         if (( num_luns != 2 || tot_len != 512 )); then
6773                 cleanup_130
6774                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
6775                 return
6776         fi
6777
6778         cleanup_130
6779
6780         echo "FIEMAP on 2-stripe file with hole succeeded"
6781 }
6782 run_test 130c "FIEMAP (2-stripe file with hole)"
6783
6784 test_130d() {
6785         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
6786
6787         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
6788         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
6789
6790         trap cleanup_130 EXIT RETURN
6791
6792         local fm_file=$DIR/$tfile
6793         lfs setstripe -s 65536 -c $OSTCOUNT $fm_file || error "setstripe failed on $fm_file"
6794         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
6795
6796         filefrag -ves $fm_file || error "filefrag $fm_file failed"
6797         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
6798
6799         last_lun=`echo $filefrag_op | cut -d: -f5`
6800
6801         IFS=$'\n'
6802         tot_len=0
6803         num_luns=1
6804         for line in $filefrag_op
6805         do
6806                 frag_lun=`echo $line | cut -d: -f5`
6807                 ext_len=`echo $line | cut -d: -f4`
6808                 if (( $frag_lun != $last_lun )); then
6809                         if (( tot_len != 1024 )); then
6810                                 cleanup_130
6811                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
6812                                 return
6813                         else
6814                                 (( num_luns += 1 ))
6815                                 tot_len=0
6816                         fi
6817                 fi
6818                 (( tot_len += ext_len ))
6819                 last_lun=$frag_lun
6820         done
6821         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
6822                 cleanup_130
6823                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
6824                 return
6825         fi
6826
6827         cleanup_130
6828
6829         echo "FIEMAP on N-stripe file succeeded"
6830 }
6831 run_test 130d "FIEMAP (N-stripe file)"
6832
6833 test_130e() {
6834         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
6835
6836         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
6837         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
6838
6839         trap cleanup_130 EXIT RETURN
6840
6841         local fm_file=$DIR/$tfile
6842         lfs setstripe -s 131072 -c 2 $fm_file || error "setstripe failed on $fm_file"
6843         NUM_BLKS=512
6844         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
6845         for ((i = 0; i < $NUM_BLKS; i++))
6846         do
6847                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
6848         done
6849
6850         filefrag -ves $fm_file || error "filefrag $fm_file failed"
6851         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
6852
6853         last_lun=`echo $filefrag_op | cut -d: -f5`
6854
6855         IFS=$'\n'
6856         tot_len=0
6857         num_luns=1
6858         for line in $filefrag_op
6859         do
6860                 frag_lun=`echo $line | cut -d: -f5`
6861                 ext_len=`echo $line | cut -d: -f4`
6862                 if (( $frag_lun != $last_lun )); then
6863                         if (( tot_len != $EXPECTED_LEN )); then
6864                                 cleanup_130
6865                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
6866                                 return
6867                         else
6868                                 (( num_luns += 1 ))
6869                                 tot_len=0
6870                         fi
6871                 fi
6872                 (( tot_len += ext_len ))
6873                 last_lun=$frag_lun
6874         done
6875         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
6876                 cleanup_130
6877                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
6878                 return
6879         fi
6880
6881         cleanup_130
6882
6883         echo "FIEMAP with continuation calls succeeded"
6884 }
6885 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
6886
6887 # Test for writev/readv
6888 test_131a() {
6889         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
6890         error "writev test failed"
6891         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
6892         error "readv failed"
6893         rm -f $DIR/$tfile
6894 }
6895 run_test 131a "test iov's crossing stripe boundary for writev/readv"
6896
6897 test_131b() {
6898         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
6899         error "append writev test failed"
6900         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
6901         error "append writev test failed"
6902         rm -f $DIR/$tfile
6903 }
6904 run_test 131b "test append writev"
6905
6906 test_131c() {
6907         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
6908         error "NOT PASS"
6909 }
6910 run_test 131c "test read/write on file w/o objects"
6911
6912 test_131d() {
6913         rwv -f $DIR/$tfile -w -n 1 1572864
6914         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
6915         if [ "$NOB" != 1572864 ]; then
6916                 error "Short read filed: read $NOB bytes instead of 1572864"
6917         fi
6918         rm -f $DIR/$tfile
6919 }
6920 run_test 131d "test short read"
6921
6922 test_131e() {
6923         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
6924         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
6925         error "read hitting hole failed"
6926         rm -f $DIR/$tfile
6927 }
6928 run_test 131e "test read hitting hole"
6929
6930 get_ost_param() {
6931         local token=$1
6932         local gl_sum=0
6933         for node in $(osts_nodes); do
6934                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
6935                 [ x$gl = x"" ] && gl=0
6936                 gl_sum=$((gl_sum + gl))
6937         done
6938         echo $gl
6939 }
6940
6941 som_mode_switch() {
6942         local som=$1
6943         local gl1=$2
6944         local gl2=$3
6945
6946         if [ x$som = x"enabled" ]; then
6947                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
6948                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
6949                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
6950         else
6951                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
6952                 MOUNTOPT="$MOUNTOPT,som_preview"
6953                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
6954         fi
6955
6956         # do remount to make new mount-conf parameters actual
6957         echo remounting...
6958         sync
6959         stopall
6960         setupall
6961 }
6962
6963 test_132() { #1028, SOM
6964         local num=$(get_mds_dir $DIR)
6965         local mymds=mds${num}
6966         local MOUNTOPT_SAVE=$MOUNTOPT
6967
6968         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
6969         cancel_lru_locks osc
6970
6971         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
6972
6973         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
6974         stat $DIR/$tfile >/dev/null
6975         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
6976         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
6977         rm $DIR/$tfile
6978         som_mode_switch $som1 $gl1 $gl2
6979
6980         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
6981         cancel_lru_locks osc
6982
6983         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
6984         if [ $som1 == $som2 ]; then
6985             error "som is still "$som2
6986             if [ x$som2 = x"enabled" ]; then
6987                 som2="disabled"
6988             else
6989                 som2="enabled"
6990             fi
6991         fi
6992
6993         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
6994         stat $DIR/$tfile >/dev/null
6995         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
6996         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
6997         som_mode_switch $som2 $gl1 $gl2
6998         MOUNTOPT=$MOUNTOPT_SAVE
6999 }
7000 run_test 132 "som avoids glimpse rpc"
7001
7002 check_stats() {
7003         local res
7004         local count
7005         case $1 in
7006         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7007                  ;;
7008         ost) res=`do_facet ost $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7009                  ;;
7010         *) error "Wrong argument $1" ;;
7011         esac
7012         echo $res
7013         count=`echo $res | awk '{print $2}'`
7014         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7015         # if the argument $3 is zero, it means any stat increment is ok.
7016         if [ $3 -gt 0 ] ; then
7017                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7018         fi
7019 }
7020
7021 test_133a() {
7022         local testdir=$DIR/${tdir}/stats_testdir
7023         mkdir -p $DIR/${tdir}
7024
7025         # clear stats.
7026         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7027         do_facet ost $LCTL set_param obdfilter.*.stats=clear
7028
7029         # verify mdt stats first.
7030         mkdir ${testdir} || error "mkdir failed"
7031         check_stats $SINGLEMDS "mkdir" 1
7032         touch ${testdir}/${tfile} || "touch failed"
7033         check_stats $SINGLEMDS "open" 1
7034         check_stats $SINGLEMDS "close" 1
7035         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7036         check_stats $SINGLEMDS "mknod" 1
7037         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7038         check_stats $SINGLEMDS "unlink" 1
7039         rm -f ${testdir}/${tfile} || error "file remove failed"
7040         check_stats $SINGLEMDS "unlink" 2
7041
7042         # remove working dir and check mdt stats again.
7043         rmdir ${testdir} || error "rmdir failed"
7044         check_stats $SINGLEMDS "rmdir" 1
7045
7046         rm -rf $DIR/${tdir}
7047 }
7048 run_test 133a "Verifying MDT stats ========================================"
7049
7050 test_133b() {
7051         local testdir=$DIR/${tdir}/stats_testdir
7052         mkdir -p ${testdir} || error "mkdir failed"
7053         touch ${testdir}/${tfile} || "touch failed"
7054         cancel_lru_locks mdc
7055
7056         # clear stats.
7057         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7058         do_facet ost $LCTL set_param obdfilter.*.stats=clear
7059
7060         # extra mdt stats verification.
7061         chmod 444 ${testdir}/${tfile} || error "chmod failed"
7062         check_stats $SINGLEMDS "setattr" 1
7063         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7064         ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
7065         check_stats $SINGLEMDS "getattr" 1
7066         $LFS df || error "lfs failed"
7067         check_stats $SINGLEMDS "statfs" 1
7068
7069         rm -rf $DIR/${tdir}
7070 }
7071 run_test 133b "Verifying extra MDT stats =================================="
7072
7073 test_133c() {
7074         local testdir=$DIR/${tdir}/stats_testdir
7075         mkdir -p ${testdir} || error "mkdir failed"
7076
7077         # verify obdfilter stats.
7078         $LFS setstripe -c 1 -o 0 ${testdir}/${tfile}
7079         sync
7080         cancel_lru_locks osc
7081
7082         # clear stats.
7083         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7084         do_facet ost $LCTL set_param obdfilter.*.stats=clear
7085
7086         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=1024k count=1 || error "dd failed"
7087         sync
7088         cancel_lru_locks osc
7089         check_stats ost "write" 1
7090
7091         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
7092         check_stats ost "read" 1
7093
7094         > ${testdir}/${tfile} || error "truncate failed"
7095         check_stats ost "punch" 1
7096
7097         rm -f ${testdir}/${tfile} || error "file remove failed"
7098         check_stats ost "destroy" 1
7099
7100         rm -rf $DIR/${tdir}
7101 }
7102 run_test 133c "Verifying OST stats ========================================"
7103
7104 test_140() { #bug-17379
7105         mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
7106         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
7107         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
7108
7109         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
7110         local i=0
7111         while i=`expr $i + 1`; do
7112                 mkdir -p $i || error "Creating dir $i"
7113                 cd $i || error "Changing to $i"
7114                 ln -s ../stat stat || error "Creating stat symlink"
7115                 # Read the symlink until ELOOP present,
7116                 # not LBUGing the system is considered success,
7117                 # we didn't overrun the stack.
7118                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
7119                 [ $ret -ne 0 ] && {
7120                         if [ $ret -eq 40 ]; then
7121                                 break  # -ELOOP
7122                         else
7123                                 error "Open stat symlink"
7124                                 return
7125                         fi
7126                 }
7127         done
7128         i=`expr $i - 1`
7129         echo "The symlink depth = $i"
7130         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 ] || error "Invalid symlink depth"
7131 }
7132 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
7133
7134 test_150() {
7135         local TF="$TMP/$tfile"
7136
7137         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7138         cp $TF $DIR/$tfile
7139         cancel_lru_locks osc
7140         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
7141         remount_client $MOUNT
7142         df -P $MOUNT
7143         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
7144
7145         $TRUNCATE $TF 6000
7146         $TRUNCATE $DIR/$tfile 6000
7147         cancel_lru_locks osc
7148         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
7149
7150         echo "12345" >>$TF
7151         echo "12345" >>$DIR/$tfile
7152         cancel_lru_locks osc
7153         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
7154
7155         echo "12345" >>$TF
7156         echo "12345" >>$DIR/$tfile
7157         cancel_lru_locks osc
7158         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
7159
7160         rm -f $TF
7161         true
7162 }
7163 run_test 150 "truncate/append tests"
7164
7165 function roc_hit() {
7166     local list=$(comma_list $(osts_nodes))
7167
7168     ACCNUM=$(do_nodes $list $LCTL get_param -n obdfilter.*.stats | \
7169         awk '/'cache_hit'/ {sum+=$2} END {print sum}')
7170     echo $ACCNUM
7171 }
7172
7173 function set_cache() {
7174     local on=1
7175
7176     if [ "$2" == "off" ]; then
7177         on=0;
7178     fi
7179     local list=$(comma_list $(osts_nodes))
7180     do_nodes $list lctl set_param obdfilter.*.${1}_cache_enable $on
7181
7182     cancel_lru_locks osc
7183 }
7184
7185 test_151() {
7186         remote_ost_nodsh && skip "remote OST with nodsh" && return
7187
7188         local CPAGES=3
7189         local list=$(comma_list $(osts_nodes))
7190
7191         # check whether obdfilter is cache capable at all
7192         if ! do_nodes $list $LCTL get_param -n obdfilter.*.read_cache_enable > /dev/null; then
7193                 echo "not cache-capable obdfilter"
7194                 return 0
7195         fi
7196
7197         # check cache is enabled on all obdfilters
7198         if do_nodes $list $LCTL get_param -n obdfilter.*.read_cache_enable | grep 0 >&/dev/null; then
7199                 echo "oss cache is disabled"
7200                 return 0
7201         fi
7202
7203         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable 1
7204
7205         # pages should be in the case right after write
7206         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
7207         local BEFORE=`roc_hit`
7208         cancel_lru_locks osc
7209         cat $DIR/$tfile >/dev/null
7210         local AFTER=`roc_hit`
7211         if ! let "AFTER - BEFORE == CPAGES"; then
7212                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7213         fi
7214
7215         # the following read invalidates the cache
7216         cancel_lru_locks osc
7217         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable 0
7218         cat $DIR/$tfile >/dev/null
7219
7220         # now data shouldn't be found in the cache
7221         BEFORE=`roc_hit`
7222         cancel_lru_locks osc
7223         cat $DIR/$tfile >/dev/null
7224         AFTER=`roc_hit`
7225         if let "AFTER - BEFORE != 0"; then
7226                 error "IN CACHE: before: $BEFORE, after: $AFTER"
7227         fi
7228
7229         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable 1
7230         rm -f $DIR/$tfile
7231 }
7232 run_test 151 "test cache on oss and controls ==============================="
7233
7234 test_152() {
7235         local TF="$TMP/$tfile"
7236
7237         # simulate ENOMEM during write
7238 #define OBD_FAIL_OST_NOMEM      0x226
7239         lctl set_param fail_loc=0x80000226
7240         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7241         cp $TF $DIR/$tfile
7242         sync || error "sync failed"
7243         lctl set_param fail_loc=0
7244
7245         # discard client's cache
7246         cancel_lru_locks osc
7247
7248         # simulate ENOMEM during read
7249         lctl set_param fail_loc=0x80000226
7250         cmp $TF $DIR/$tfile || error "cmp failed"
7251         lctl set_param fail_loc=0
7252
7253         rm -f $TF
7254 }
7255 run_test 152 "test read/write with enomem ============================"
7256
7257 test_153() {
7258         multiop $DIR/$tfile Ow4096Ycu || error "multiop failed"
7259 }
7260 run_test 153 "test if fdatasync does not crash ======================="
7261
7262 test_154() {
7263         cp /etc/hosts $DIR/$tfile
7264
7265         fid=$($LFS path2fid $DIR/$tfile)
7266         rc=$?
7267         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7268
7269         echo "open fid $fid"
7270         diff /etc/hosts $DIR/.lustre/fid/$fid || error "open by fid failed: did not find expected data in file."
7271
7272         echo "Opening a file by FID succeeded"
7273 }
7274 run_test 154 "Opening a file by FID"
7275
7276 test_155_small_load() {
7277     local temp=$TMP/$tfile
7278     local file=$DIR/$tfile
7279
7280     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
7281         error "dd of=$temp bs=6096 count=1 failed"
7282     cp $temp $file
7283     cancel_lru_locks osc
7284     cmp $temp $file || error "$temp $file differ"
7285
7286     $TRUNCATE $temp 6000
7287     $TRUNCATE $file 6000
7288     cmp $temp $file || error "$temp $file differ (truncate1)"
7289
7290     echo "12345" >>$temp
7291     echo "12345" >>$file
7292     cmp $temp $file || error "$temp $file differ (append1)"
7293
7294     echo "12345" >>$temp
7295     echo "12345" >>$file
7296     cmp $temp $file || error "$temp $file differ (append2)"
7297
7298     rm -f $temp $file
7299     true
7300 }
7301
7302 test_155_big_load() {
7303     local temp=$TMP/$tfile
7304     local file=$DIR/$tfile
7305
7306     free_min_max
7307     local cache_size=$(do_facet ost$((MAXI+1)) \
7308         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
7309     local large_file_size=$((cache_size * 2))
7310
7311     echo "OSS cache size: $cache_size KB"
7312     echo "Large file size: $large_file_size KB"
7313
7314     [ $MAXV -le $large_file_size ] && \
7315         skip_env "max available OST size needs > $large_file_size KB" && \
7316         return 0
7317
7318     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
7319
7320     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
7321         error "dd of=$temp bs=$large_file_size count=1k failed"
7322     cp $temp $file
7323     ls -lh $temp $file
7324     cancel_lru_locks osc
7325     cmp $temp $file || error "$temp $file differ"
7326
7327     rm -f $temp $file
7328     true
7329 }
7330
7331 test_155a() {
7332     set_cache read on
7333     set_cache writethrough on
7334     test_155_small_load
7335 }
7336 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
7337
7338 test_155b() {
7339     set_cache read on
7340     set_cache writethrough off
7341     test_155_small_load
7342 }
7343 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
7344
7345 test_155c() {
7346     set_cache read off
7347     set_cache writethrough on
7348     test_155_small_load
7349 }
7350 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
7351
7352 test_155d() {
7353     set_cache read off
7354     set_cache writethrough off
7355     test_155_small_load
7356 }
7357 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
7358
7359 test_155e() {
7360     set_cache read on
7361     set_cache writethrough on
7362     test_155_big_load
7363 }
7364 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
7365
7366 test_155f() {
7367     set_cache read on
7368     set_cache writethrough off
7369     test_155_big_load
7370 }
7371 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
7372
7373 test_155g() {
7374     set_cache read off
7375     set_cache writethrough on
7376     test_155_big_load
7377 }
7378 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
7379
7380 test_155h() {
7381     set_cache read off
7382     set_cache writethrough off
7383     test_155_big_load
7384 }
7385 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
7386
7387 test_156() {
7388     local CPAGES=3
7389     local BEFORE
7390     local AFTER
7391     local file="$DIR/$tfile"
7392
7393     log "Turn on read and write cache"
7394     set_cache read on
7395     set_cache writethrough on
7396
7397     log "Write data and read it back."
7398     log "Read should be satisfied from the cache."
7399     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
7400     BEFORE=`roc_hit`
7401     cancel_lru_locks osc
7402     cat $file >/dev/null
7403     AFTER=`roc_hit`
7404     if ! let "AFTER - BEFORE == CPAGES"; then
7405         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7406     else
7407         log "cache hits:: before: $BEFORE, after: $AFTER"
7408     fi
7409
7410     log "Read again; it should be satisfied from the cache."
7411     BEFORE=$AFTER
7412     cancel_lru_locks osc
7413     cat $file >/dev/null
7414     AFTER=`roc_hit`
7415     if ! let "AFTER - BEFORE == CPAGES"; then
7416         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7417     else
7418         log "cache hits:: before: $BEFORE, after: $AFTER"
7419     fi
7420
7421
7422     log "Turn off the read cache and turn on the write cache"
7423     set_cache read off
7424     set_cache writethrough on
7425
7426     log "Read again; it should be satisfied from the cache."
7427     BEFORE=`roc_hit`
7428     cancel_lru_locks osc
7429     cat $file >/dev/null
7430     AFTER=`roc_hit`
7431     if ! let "AFTER - BEFORE == CPAGES"; then
7432         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7433     else
7434         log "cache hits:: before: $BEFORE, after: $AFTER"
7435     fi
7436
7437     log "Read again; it should not be satisfied from the cache."
7438     BEFORE=$AFTER
7439     cancel_lru_locks osc
7440     cat $file >/dev/null
7441     AFTER=`roc_hit`
7442     if ! let "AFTER - BEFORE == 0"; then
7443         error "IN CACHE: before: $BEFORE, after: $AFTER"
7444     else
7445         log "cache hits:: before: $BEFORE, after: $AFTER"
7446     fi
7447
7448     log "Write data and read it back."
7449     log "Read should be satisfied from the cache."
7450     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
7451     BEFORE=`roc_hit`
7452     cancel_lru_locks osc
7453     cat $file >/dev/null
7454     AFTER=`roc_hit`
7455     if ! let "AFTER - BEFORE == CPAGES"; then
7456         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7457     else
7458         log "cache hits:: before: $BEFORE, after: $AFTER"
7459     fi
7460
7461     log "Read again; it should not be satisfied from the cache."
7462     BEFORE=$AFTER
7463     cancel_lru_locks osc
7464     cat $file >/dev/null
7465     AFTER=`roc_hit`
7466     if ! let "AFTER - BEFORE == 0"; then
7467         error "IN CACHE: before: $BEFORE, after: $AFTER"
7468     else
7469         log "cache hits:: before: $BEFORE, after: $AFTER"
7470     fi
7471
7472
7473     log "Turn off read and write cache"
7474     set_cache read off
7475     set_cache writethrough off
7476
7477     log "Write data and read it back"
7478     log "It should not be satisfied from the cache."
7479     rm -f $file
7480     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
7481     cancel_lru_locks osc
7482     BEFORE=`roc_hit`
7483     cat $file >/dev/null
7484     AFTER=`roc_hit`
7485     if ! let "AFTER - BEFORE == 0"; then
7486         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
7487     else
7488         log "cache hits:: before: $BEFORE, after: $AFTER"
7489     fi
7490
7491
7492     log "Turn on the read cache and turn off the write cache"
7493     set_cache read on
7494     set_cache writethrough off
7495
7496     log "Write data and read it back"
7497     log "It should not be satisfied from the cache."
7498     rm -f $file
7499     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
7500     BEFORE=`roc_hit`
7501     cancel_lru_locks osc
7502     cat $file >/dev/null
7503     AFTER=`roc_hit`
7504     if ! let "AFTER - BEFORE == 0"; then
7505         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
7506     else
7507         log "cache hits:: before: $BEFORE, after: $AFTER"
7508     fi
7509
7510     log "Read again; it should be satisfied from the cache."
7511     BEFORE=`roc_hit`
7512     cancel_lru_locks osc
7513     cat $file >/dev/null
7514     AFTER=`roc_hit`
7515     if ! let "AFTER - BEFORE == CPAGES"; then
7516         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7517     else
7518         log "cache hits:: before: $BEFORE, after: $AFTER"
7519     fi
7520
7521     rm -f $file
7522 }
7523 run_test 156 "Verification of tunables ============================"
7524
7525 #Changelogs
7526 err17935 () {
7527     if [ $MDSCOUNT -gt 1 ]; then
7528         error_ignore 17935 $*
7529     else
7530         error $*
7531     fi
7532 }
7533 test_160() {
7534     USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
7535     echo "Registered as changelog user $USER"
7536     do_facet $SINGLEMDS lctl get_param -n mdd.$MDT0.changelog_users | \
7537         grep -q $USER || error "User $USER not found in changelog_users"
7538
7539     # change something
7540     mkdir -p $DIR/$tdir/pics/2008/zachy
7541     touch $DIR/$tdir/pics/2008/zachy/timestamp
7542     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
7543     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
7544     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
7545     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
7546     rm $DIR/$tdir/pics/desktop.jpg
7547
7548     $LFS changelog $MDT0 | tail -5
7549
7550     echo "verifying changelog mask"
7551     do_facet $SINGLEMDS lctl set_param mdd.$MDT0.changelog_mask="-mkdir"
7552     mkdir -p $DIR/$tdir/pics/2009/sofia
7553     do_facet $SINGLEMDS lctl set_param mdd.$MDT0.changelog_mask="+mkdir"
7554     mkdir $DIR/$tdir/pics/2009/zachary
7555     DIRS=$($LFS changelog $MDT0 | tail -5 | grep -c MKDIR)
7556     [ $DIRS -eq 1 ] || err17935 "changelog mask count $DIRS != 1"
7557
7558     # verify contents
7559     echo "verifying target fid"
7560     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
7561         tail -1 | awk '{print $6}')
7562     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
7563     [ "$fidc" == "t=$fidf" ] || \
7564         err17935 "fid in changelog $fidc != file fid $fidf"
7565     echo "verifying parent fid"
7566     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
7567         tail -1 | awk '{print $7}')
7568     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
7569     [ "$fidc" == "p=$fidf" ] || \
7570         err17935 "pfid in changelog $fidc != dir fid $fidf"
7571
7572     USER_REC1=$(do_facet $SINGLEMDS lctl get_param -n \
7573         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
7574     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
7575     USER_REC2=$(do_facet $SINGLEMDS lctl get_param -n \
7576         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
7577     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
7578     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
7579         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
7580
7581     MIN_REC=$(do_facet $SINGLEMDS lctl get_param mdd.$MDT0.changelog_users | \
7582         awk 'min == "" || $2 < min {min = $2}; END {print min}')
7583     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
7584     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
7585     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
7586         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
7587
7588     echo "verifying user deregister"
7589     do_facet $SINGLEMDS lctl --device $MDT0 changelog_deregister $USER
7590     do_facet $SINGLEMDS lctl get_param -n mdd.$MDT0.changelog_users | \
7591         grep -q $USER && error "User $USER still found in changelog_users"
7592
7593     USERS=$(( $(do_facet $SINGLEMDS lctl get_param -n \
7594         mdd.$MDT0.changelog_users | wc -l) - 2 ))
7595     if [ $USERS -eq 0 ]; then
7596         LAST_REC1=$(do_facet $SINGLEMDS lctl get_param -n \
7597             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
7598         touch $DIR/$tdir/chloe
7599         LAST_REC2=$(do_facet $SINGLEMDS lctl get_param -n \
7600             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
7601         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
7602         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
7603     else
7604         echo "$USERS other changelog users; can't verify off"
7605     fi
7606 }
7607 run_test 160 "changelog sanity"
7608
7609 test_161() {
7610     mkdir -p $DIR/$tdir
7611     cp /etc/hosts $DIR/$tdir/$tfile
7612     mkdir $DIR/$tdir/foo1
7613     mkdir $DIR/$tdir/foo2
7614     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
7615     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
7616     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
7617     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
7618     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
7619     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
7620         $LFS fid2path $DIR $FID
7621         err17935 "bad link ea"
7622     fi
7623     # middle
7624     rm $DIR/$tdir/foo2/zachary
7625     # last
7626     rm $DIR/$tdir/foo2/thor
7627     # first
7628     rm $DIR/$tdir/$tfile
7629     # rename
7630     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
7631     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
7632         then
7633         $LFS fid2path $DIR $FID
7634         err17935 "bad link rename"
7635     fi
7636     rm $DIR/$tdir/foo2/maggie
7637
7638     # overflow the EA
7639     local longname=filename_avg_len_is_thirty_two_
7640     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
7641         error "failed to hardlink many files"
7642     links=$($LFS fid2path $DIR $FID | wc -l)
7643     echo -n "${links}/1000 links in link EA"
7644     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
7645     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
7646         error "failed to unlink many hardlinks"
7647 }
7648 run_test 161 "link ea sanity"
7649
7650 check_path() {
7651     local expected=$1
7652     shift
7653     local fid=$2
7654
7655     local path=$(${LFS} fid2path $*)
7656     RC=$?
7657
7658     if [ $RC -ne 0 ]; then
7659         err17935 "path looked up of $expected failed. Error $RC"
7660         return $RC
7661     elif [ "${path}" != "${expected}" ]; then
7662         err17935 "path looked up \"${path}\" instead of \"${expected}\""
7663         return 2
7664     fi
7665     echo "fid $fid resolves to path $path (expected $expected)"
7666 }
7667
7668 test_162() {
7669         # Make changes to filesystem
7670         mkdir -p $DIR/$tdir/d2
7671         touch $DIR/$tdir/d2/$tfile
7672         touch $DIR/$tdir/d2/x1
7673         touch $DIR/$tdir/d2/x2
7674         mkdir -p $DIR/$tdir/d2/a/b/c
7675         mkdir -p $DIR/$tdir/d2/p/q/r
7676         # regular file
7677         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
7678         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
7679
7680         # softlink
7681         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
7682         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
7683         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
7684
7685         # softlink to wrong file
7686         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
7687         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
7688         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
7689
7690         # hardlink
7691         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
7692         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
7693         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
7694         # fid2path dir/fsname should both work
7695         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
7696         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
7697
7698         # hardlink count: check that there are 2 links
7699         # Doesnt work with CMD yet: 17935
7700         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
7701                 err17935 "expected 2 links"
7702
7703         # hardlink indexing: remove the first link
7704         rm $DIR/$tdir/d2/p/q/r/hlink
7705         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
7706
7707         return 0
7708 }
7709 run_test 162 "path lookup sanity"
7710
7711 test_163() {
7712         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7713         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
7714         copytool $FSNAME &
7715         sleep 1
7716         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
7717         # this proc file is temporary and linux-only
7718         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
7719          error "kernel->userspace send failed"
7720         kill -INT $!
7721 }
7722 run_test 163 "kernel <-> userspace comms"
7723
7724 test_169() {
7725         # do directio so as not to populate the page cache
7726         log "creating a 10 Mb file"
7727         multiop $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
7728         log "starting reads"
7729         dd if=$DIR/$tfile of=/dev/null bs=4096 &
7730         log "truncating the file"
7731         multiop $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
7732         log "killing dd"
7733         kill %+ || true # reads might have finished
7734         echo "wait until dd is finished"
7735         wait
7736         log "removing the temporary file"
7737         rm -rf $DIR/$tfile || error "tmp file removal failed"
7738 }
7739 run_test 169 "parallel read and truncate should not deadlock"
7740
7741 test_170() {
7742         $LCTL clear     # bug 18514
7743         $LCTL debug_daemon start $TMP/${tfile}_log_good
7744         touch $DIR/$tfile
7745         $LCTL debug_daemon stop
7746         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
7747                error "sed failed to read log_good"
7748
7749         $LCTL debug_daemon start $TMP/${tfile}_log_good
7750         rm -rf $DIR/$tfile
7751         $LCTL debug_daemon stop
7752
7753         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
7754                error "lctl df log_bad failed"
7755
7756         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
7757         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
7758
7759         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
7760         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
7761
7762         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
7763                 error "bad_line good_line1 good_line2 are empty"
7764
7765         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
7766         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
7767         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
7768
7769         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
7770         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
7771         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
7772
7773         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
7774                 error "bad_line_new good_line_new are empty"
7775
7776         local expected_good=$((good_line1 + good_line2*2))
7777
7778         rm -f $TMP/${tfile}*
7779         # LU-231, short malformed line may not be counted into bad lines
7780         if [ $bad_line -ne $bad_line_new ] &&
7781                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
7782                 error "expected $bad_line bad lines, but got $bad_line_new"
7783                 return 1
7784         fi
7785
7786         if [ $expected_good -ne $good_line_new ]; then
7787                 error "expected $expected_good good lines, but got $good_line_new"
7788                 return 2
7789         fi
7790         true
7791 }
7792 run_test 170 "test lctl df to handle corrupted log ====================="
7793
7794 test_171() { # bug20592
7795 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
7796         $LCTL set_param fail_loc=0x50e
7797         $LCTL set_param fail_val=3000
7798         multiop_bg_pause $DIR/$tfile O_s || true
7799         local MULTIPID=$!
7800         kill -USR1 $MULTIPID
7801         # cause log dump
7802         sleep 3
7803         wait $MULTIPID
7804         if dmesg | grep "recursive fault"; then
7805                 error "caught a recursive fault"
7806         fi
7807         $LCTL set_param fail_loc=0
7808         true
7809 }
7810 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
7811
7812 # it would be good to share it with obdfilter-survey/libecho code
7813 setup_obdecho_osc () {
7814         local rc=0
7815         local ost_nid=$1
7816         local obdfilter_name=$2
7817         echo "Creating new osc for $obdfilter_name on $ost_nid"
7818         # make sure we can find loopback nid
7819         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
7820
7821         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
7822                            ${obdfilter_name}_osc_UUID || rc=2; }
7823         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
7824                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
7825         return $rc
7826 }
7827
7828 cleanup_obdecho_osc () {
7829         local obdfilter_name=$1
7830         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
7831         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
7832         return 0
7833 }
7834
7835 obdecho_create_test() {
7836         local OBD=$1
7837         local node=$2
7838         local rc=0
7839         local id
7840         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
7841         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
7842                            rc=2; }
7843         if [ $rc -eq 0 ]; then
7844             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
7845             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
7846         fi
7847         echo "New object id is $id"
7848         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
7849                            rc=4; }
7850         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
7851                                         "cleanup" || rc=5; }
7852         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
7853                                         "detach" || rc=6; }
7854         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
7855         return $rc
7856 }
7857
7858 test_180a() {
7859         local rc=0
7860         local rmmod_local=0
7861
7862         if ! module_loaded obdecho; then
7863             load_module obdecho/obdecho
7864             rmmod_local=1
7865         fi
7866
7867         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
7868         local host=$(lctl get_param -n osc.$osc.import |
7869                              awk '/current_connection:/ {print $2}' )
7870         local target=$(lctl get_param -n osc.$osc.import |
7871                              awk '/target:/ {print $2}' )
7872         target=${target%_UUID}
7873
7874         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
7875         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
7876         [[ -n $target ]] && cleanup_obdecho_osc $target
7877         [ $rmmod_local -eq 1 ] && rmmod obdecho
7878         return $rc
7879 }
7880 run_test 180a "test obdecho on osc"
7881
7882 test_180b() {
7883         local rc=0
7884         local rmmod_remote=0
7885
7886         do_facet ost "lsmod | grep -q obdecho || "                      \
7887                      "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
7888                      "modprobe obdecho; }" && rmmod_remote=1
7889         target=$(do_facet ost $LCTL dl | awk '/obdfilter/ {print $4;exit}')
7890         [[ -n $target ]] && { obdecho_create_test $target ost || rc=1; }
7891         [ $rmmod_remote -eq 1 ] && do_facet ost "rmmod obdecho"
7892         return $rc
7893 }
7894 run_test 180b "test obdecho directly on obdfilter"
7895
7896 test_181() { # bug 22177
7897         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
7898         # create enough files to index the directory
7899         createmany -o $DIR/$tdir/foobar 4000
7900         # print attributes for debug purpose
7901         lsattr -d .
7902         # open dir
7903         multiop_bg_pause $DIR/$tdir D_Sc || return 1
7904         MULTIPID=$!
7905         # remove the files & current working dir
7906         unlinkmany $DIR/$tdir/foobar 4000
7907         rmdir $DIR/$tdir
7908         kill -USR1 $MULTIPID
7909         wait $MULTIPID
7910         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
7911         return 0
7912 }
7913 run_test 181 "Test open-unlinked dir ========================"
7914
7915 # OST pools tests
7916 POOL=${POOL:-cea1}
7917 TGT_COUNT=$OSTCOUNT
7918 TGTPOOL_FIRST=1
7919 TGTPOOL_MAX=$(($TGT_COUNT - 1))
7920 TGTPOOL_STEP=2
7921 TGTPOOL_LIST=`seq $TGTPOOL_FIRST $TGTPOOL_STEP $TGTPOOL_MAX`
7922 POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
7923 POOL_DIR_NAME=dir_tst
7924 POOL_DIR=$POOL_ROOT/$POOL_DIR_NAME
7925 POOL_FILE=$POOL_ROOT/file_tst
7926
7927 check_file_in_pool()
7928 {
7929         file=$1
7930         res=$($GETSTRIPE $file | grep 0x | cut -f2)
7931         for i in $res
7932         do
7933                 found=$(echo :$TGTPOOL_LIST: | tr " " ":"  | grep :$i:)
7934                 if [[ "$found" == "" ]]
7935                 then
7936                         echo "pool list: $TGTPOOL_LIST"
7937                         echo "striping: $res"
7938                         error "$file not allocated in $POOL"
7939                         return 1
7940                 fi
7941         done
7942         return 0
7943 }
7944
7945 trap "cleanup_pools $FSNAME" EXIT
7946
7947 test_200a() {
7948         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
7949     create_pool $FSNAME.$POOL || return $?
7950         [ $($LFS pool_list $FSNAME | grep -c $POOL) -eq 1 ] ||
7951                 error "$POOL not in lfs pool_list"
7952 }
7953 run_test 200a "Create new pool =========================================="
7954
7955 test_200b() {
7956         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
7957         TGT=$(for i in $TGTPOOL_LIST; do printf "$FSNAME-OST%04x_UUID " $i; done)
7958         do_facet mgs $LCTL pool_add $FSNAME.$POOL \
7959                 $FSNAME-OST[$TGTPOOL_FIRST-$TGTPOOL_MAX/$TGTPOOL_STEP]
7960         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL | sort -u | tr '\n' ' ' " "$TGT" ||
7961                 error "Add to pool failed"
7962         local lfscount=$($LFS pool_list $FSNAME.$POOL | grep -c "\-OST")
7963         local addcount=$((($TGTPOOL_MAX - $TGTPOOL_FIRST) / $TGTPOOL_STEP + 1))
7964         [ $lfscount -eq $addcount ] ||
7965                 error "lfs pool_list bad ost count $lfscount != $addcount"
7966 }
7967 run_test 200b "Add targets to a pool ===================================="
7968
7969 test_200c() {
7970         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
7971         mkdir -p $POOL_DIR
7972         $SETSTRIPE -c 2 -p $POOL $POOL_DIR
7973         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR"
7974         # b-19919 test relative path works well
7975         mkdir -p $POOL_DIR/$POOL_DIR_NAME
7976         cd $POOL_DIR
7977         $SETSTRIPE -c 2 -p $POOL $POOL_DIR_NAME
7978         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/$POOL_DIR_NAME"
7979         $SETSTRIPE -c 2 -p $POOL ./$POOL_DIR_NAME
7980         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/./$POOL_DIR_NAME"
7981         $SETSTRIPE -c 2 -p $POOL ../$POOL_DIR_NAME
7982         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/../$POOL_DIR_NAME"
7983         $SETSTRIPE -c 2 -p $POOL ../$POOL_DIR_NAME/$POOL_DIR_NAME
7984         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/../$POOL_DIR_NAME/$POOL_DIR_NAME"
7985         rm -rf $POOL_DIR_NAME; cd -
7986 }
7987 run_test 200c "Set pool on a directory ================================="
7988
7989 test_200d() {
7990         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
7991         res=$($GETSTRIPE --pool $POOL_DIR)
7992         [ $res = $POOL ] || error "Pool on $POOL_DIR is $res, not $POOL"
7993 }
7994 run_test 200d "Check pool on a directory ==============================="
7995
7996 test_200e() {
7997         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
7998         failed=0
7999         for i in $(seq -w 1 $(($TGT_COUNT * 3)))
8000         do
8001                 file=$POOL_DIR/file-$i
8002                 touch $file
8003                 check_file_in_pool $file
8004                 if [[ $? != 0 ]]
8005                 then
8006                         failed=$(($failed + 1))
8007                 fi
8008         done
8009         [ "$failed" = 0 ] || error "$failed files not allocated in $POOL"
8010 }
8011 run_test 200e "Check files allocation from directory pool =============="
8012
8013 test_200f() {
8014         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8015         mkdir -p $POOL_FILE
8016         failed=0
8017         for i in $(seq -w 1 $(($TGT_COUNT * 3)))
8018         do
8019                 file=$POOL_FILE/spoo-$i
8020                 $SETSTRIPE -p $POOL $file
8021                 check_file_in_pool $file
8022                 if [[ $? != 0 ]]
8023                 then
8024                         failed=$(($failed + 1))
8025                 fi
8026         done
8027         [ "$failed" = 0 ] || error "$failed files not allocated in $POOL"
8028 }
8029 run_test 200f "Create files in a pool ==================================="
8030
8031 test_200g() {
8032         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8033         TGT=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$POOL | tr '\n' ' ')
8034         res=$($LFS df --pool $FSNAME.$POOL | awk '{print $1}' | grep "$FSNAME-OST" | tr '\n' ' ')
8035         [ "$res" = "$TGT" ] || error "Pools OSTs '$TGT' is not '$res' that lfs df reports"
8036 }
8037 run_test 200g "lfs df a pool ============================================"
8038
8039 test_200h() { # b=24039
8040         mkdir -p $POOL_DIR || error "unable to create $POOL_DIR"
8041
8042         local file="/..$POOL_DIR/$tfile-1"
8043         $SETSTRIPE -p $POOL $file || error "unable to create $file"
8044
8045         cd $POOL_DIR
8046         $SETSTRIPE -p $POOL $tfile-2 || \
8047                 error "unable to create $tfile-2 in $POOL_DIR"
8048 }
8049 run_test 200h "Create files in a pool with relative pathname ============"
8050
8051 test_201a() {
8052         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8053         TGT=$($LCTL get_param -n lov.$FSNAME-*.pools.$POOL | head -1)
8054         do_facet mgs $LCTL pool_remove $FSNAME.$POOL $TGT
8055         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL | grep $TGT" "" ||
8056                 error "$TGT not removed from $FSNAME.$POOL"
8057 }
8058 run_test 201a "Remove a target from a pool ============================="
8059
8060 test_201b() {
8061         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8062         for TGT in $($LCTL get_param -n lov.$FSNAME-*.pools.$POOL | sort -u)
8063         do
8064                 do_facet mgs $LCTL pool_remove $FSNAME.$POOL $TGT
8065         done
8066         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL" "" ||
8067                 error "Pool $FSNAME.$POOL cannot be drained"
8068         # striping on an empty/nonexistant pool should fall back to "pool of everything"
8069         touch ${POOL_DIR}/$tfile || error "failed to use fallback striping for empty pool"
8070         # setstripe on an empty pool should fail
8071         $SETSTRIPE -p $POOL ${POOL_FILE}/$tfile 2>/dev/null && \
8072                 error "expected failure when creating file with empty pool"
8073         return 0
8074 }
8075 run_test 201b "Remove all targets from a pool =========================="
8076
8077 test_201c() {
8078         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8079         do_facet mgs $LCTL pool_destroy $FSNAME.$POOL
8080
8081         sleep 2
8082     # striping on an empty/nonexistant pool should fall back to "pool of everything"
8083         touch ${POOL_DIR}/$tfile || error "failed to use fallback striping for missing pool"
8084         # setstripe on an empty pool should fail
8085         $SETSTRIPE -p $POOL ${POOL_FILE}/$tfile 2>/dev/null && \
8086                 error "expected failure when creating file with missing pool"
8087
8088         # get param should return err once pool is gone
8089         if wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL 2>/dev/null ||
8090                         echo foo" "foo"; then
8091                 remove_pool_from_list $FSNAME.$POOL
8092                 return 0
8093         fi
8094         error "Pool $FSNAME.$POOL is not destroyed"
8095 }
8096 run_test 201c "Remove a pool ============================================"
8097
8098 cleanup_pools $FSNAME
8099
8100 # usage: default_attr <count | size | offset>
8101 default_attr() {
8102         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
8103 }
8104
8105 # usage: trim <string>
8106 # Trims leading and trailing whitespace from the parameter string
8107 trim() {
8108     echo $@
8109 }
8110
8111 # usage: check_default_stripe_attr <count | size | offset>
8112 check_default_stripe_attr() {
8113         # $GETSTRIPE returns trailing whitespace which needs to be trimmed off
8114         ACTUAL=$(trim $($GETSTRIPE --$1 $DIR/$tdir))
8115         if [ $1 = "count" -o $1 = "size" ]; then
8116                 EXPECTED=`default_attr $1`;
8117         else
8118                 # the 'stripeoffset' parameter prints as an unsigned int, so
8119                 # until this is fixed we hard-code -1 here
8120                 EXPECTED=-1;
8121         fi
8122         [ "x$ACTUAL" != "x$EXPECTED" ] &&
8123                 error "$DIR/$tdir has stripe $1 '$ACTUAL', not '$EXPECTED'"
8124 }
8125
8126 # usage: check_raw_stripe_attr <count | size | offset>
8127 check_raw_stripe_attr() {
8128         # $GETSTRIPE returns trailing whitespace which needs to be trimmed off
8129         ACTUAL=$(trim $($GETSTRIPE --raw --$1 $DIR/$tdir))
8130         if [ $1 = "count" -o $1 = "size" ]; then
8131                 EXPECTED=0;
8132         else
8133                 EXPECTED=-1;
8134         fi
8135         [ "x$ACTUAL" != "x$EXPECTED" ] &&
8136                 error "$DIR/$tdir has raw stripe $1 '$ACTUAL', not '$EXPECTED'"
8137 }
8138
8139
8140 test_204a() {
8141         mkdir -p $DIR/$tdir
8142         $SETSTRIPE --count 0 --size 0 --offset -1 $DIR/$tdir
8143
8144         check_default_stripe_attr count
8145         check_default_stripe_attr size
8146         check_default_stripe_attr offset
8147
8148         return 0
8149 }
8150 run_test 204a "Print default stripe attributes ================="
8151
8152 test_204b() {
8153         mkdir -p $DIR/$tdir
8154         $SETSTRIPE --count 1 $DIR/$tdir
8155
8156         check_default_stripe_attr size
8157         check_default_stripe_attr offset
8158
8159         return 0
8160 }
8161 run_test 204b "Print default stripe size and offset  ==========="
8162
8163 test_204c() {
8164         mkdir -p $DIR/$tdir
8165         $SETSTRIPE --size 65536 $DIR/$tdir
8166
8167         check_default_stripe_attr count
8168         check_default_stripe_attr offset
8169
8170         return 0
8171 }
8172 run_test 204c "Print default stripe count and offset ==========="
8173
8174 test_204d() {
8175         mkdir -p $DIR/$tdir
8176         $SETSTRIPE --offset 0 $DIR/$tdir
8177
8178         check_default_stripe_attr count
8179         check_default_stripe_attr size
8180
8181         return 0
8182 }
8183 run_test 204d "Print default stripe count and size ============="
8184
8185 test_204e() {
8186         mkdir -p $DIR/$tdir
8187         $SETSTRIPE -d $DIR/$tdir
8188
8189         check_raw_stripe_attr count
8190         check_raw_stripe_attr size
8191         check_raw_stripe_attr offset
8192
8193         return 0
8194 }
8195 run_test 204e "Print raw stripe attributes ================="
8196
8197 test_204f() {
8198         mkdir -p $DIR/$tdir
8199         $SETSTRIPE --count 1 $DIR/$tdir
8200
8201         check_raw_stripe_attr size
8202         check_raw_stripe_attr offset
8203
8204         return 0
8205 }
8206 run_test 204f "Print raw stripe size and offset  ==========="
8207
8208 test_204g() {
8209         mkdir -p $DIR/$tdir
8210         $SETSTRIPE --size 65536 $DIR/$tdir
8211
8212         check_raw_stripe_attr count
8213         check_raw_stripe_attr offset
8214
8215         return 0
8216 }
8217 run_test 204g "Print raw stripe count and offset ==========="
8218
8219 test_204h() {
8220         mkdir -p $DIR/$tdir
8221         $SETSTRIPE --offset 0 $DIR/$tdir
8222
8223         check_raw_stripe_attr count
8224         check_raw_stripe_attr size
8225
8226         return 0
8227 }
8228 run_test 204h "Print raw stripe count and size ============="
8229
8230 test_212() {
8231         size=`date +%s`
8232         size=$((size % 8192 + 1))
8233         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
8234         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
8235         rm -f $DIR/f212 $DIR/f212.xyz
8236 }
8237 run_test 212 "Sendfile test ============================================"
8238
8239 test_213() {
8240         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
8241         cancel_lru_locks osc
8242         lctl set_param fail_loc=0x8000040f
8243         # generate a read lock
8244         cat $DIR/$tfile > /dev/null
8245         # write to the file, it will try to cancel the above read lock.
8246         cat /etc/hosts >> $DIR/$tfile
8247 }
8248 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
8249
8250 test_214() { # for bug 20133
8251         mkdir -p $DIR/d214p/d214c
8252         for (( i=0; i < 340; i++ )) ; do
8253                 touch $DIR/d214p/d214c/a$i
8254         done
8255
8256         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
8257         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
8258         ls $DIR/d214c || error "ls $DIR/d214c failed"
8259         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
8260 }
8261 run_test 214 "hash-indexed directory test - bug 20133"
8262
8263 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
8264 create_lnet_proc_files() {
8265         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
8266         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
8267
8268         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
8269         rm -f "$TMP/lnet_$1.sys_tmp"
8270 }
8271
8272 # counterpart of create_lnet_proc_files
8273 remove_lnet_proc_files() {
8274         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
8275 }
8276
8277 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
8278 # 3rd arg as regexp for body
8279 check_lnet_proc_stats() {
8280         local l=$(cat "$TMP/lnet_$1" |wc -l)
8281         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
8282
8283         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
8284 }
8285
8286 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
8287 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
8288 # optional and can be regexp for 2nd line (lnet.routes case)
8289 check_lnet_proc_entry() {
8290         local blp=2            # blp stands for 'position of 1st line of body'
8291         [ "$5" = "" ] || blp=3 # lnet.routes case
8292
8293         local l=$(cat "$TMP/lnet_$1" |wc -l)
8294         # subtracting one from $blp because the body can be empty
8295         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
8296
8297         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
8298                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
8299
8300         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
8301                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
8302
8303         # bail out if any unexpected line happened
8304         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
8305         [ "$?" != 0 ] || error "$2 misformatted"
8306 }
8307
8308 test_215() { # for bugs 18102, 21079, 21517
8309         local N='(0|[1-9][0-9]*)'       # non-negative numeric
8310         local P='[1-9][0-9]*'           # positive numeric
8311         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
8312         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
8313         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
8314         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
8315
8316         local L1 # regexp for 1st line
8317         local L2 # regexp for 2nd line (optional)
8318         local BR # regexp for the rest (body)
8319
8320         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
8321         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
8322         create_lnet_proc_files "stats"
8323         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
8324         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
8325         remove_lnet_proc_files "stats"
8326
8327         # /proc/sys/lnet/routes should look like this:
8328         # Routing disabled/enabled
8329         # net hops state router
8330         # where net is a string like tcp0, hops >= 0, state is up/down,
8331         # router is a string like 192.168.1.1@tcp2
8332         L1="^Routing (disabled|enabled)$"
8333         L2="^net +hops +state +router$"
8334         BR="^$NET +$N +(up|down) +$NID$"
8335         create_lnet_proc_files "routes"
8336         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
8337         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
8338         remove_lnet_proc_files "routes"
8339
8340         # /proc/sys/lnet/routers should look like this:
8341         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
8342         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
8343         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
8344         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
8345         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
8346         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
8347         create_lnet_proc_files "routers"
8348         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
8349         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
8350         remove_lnet_proc_files "routers"
8351
8352         # /proc/sys/lnet/peers should look like this:
8353         # nid refs state last max rtr min tx min queue
8354         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
8355         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
8356         # numeric (0 or >0 or <0), queue >= 0.
8357         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
8358         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
8359         create_lnet_proc_files "peers"
8360         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
8361         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
8362         remove_lnet_proc_files "peers"
8363
8364         # /proc/sys/lnet/buffers  should look like this:
8365         # pages count credits min
8366         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
8367         L1="^pages +count +credits +min$"
8368         BR="^ +$N +$N +$I +$I$"
8369         create_lnet_proc_files "buffers"
8370         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
8371         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
8372         remove_lnet_proc_files "buffers"
8373
8374         # /proc/sys/lnet/nis should look like this:
8375         # nid status alive refs peer rtr max tx min
8376         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
8377         # alive is numeric (0 or >0 or <0), refs > 0, peer >= 0,
8378         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
8379         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
8380         BR="^$NID +(up|down) +$I +$P +$N +$N +$N +$I +$I$"
8381         create_lnet_proc_files "nis"
8382         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
8383         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
8384         remove_lnet_proc_files "nis"
8385
8386         # can we successfully write to /proc/sys/lnet/stats?
8387         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
8388         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
8389 }
8390 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
8391
8392 test_216() { # bug 20317
8393         local node
8394         local p="$TMP/sanityN-$TESTNAME.parameters"
8395         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
8396         for node in $(osts_nodes); do
8397                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
8398                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
8399                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
8400         done
8401         clear_osc_stats
8402
8403         # agressive lockless i/o settings
8404         for node in $(osts_nodes); do
8405                 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'
8406         done
8407         lctl set_param -n osc.*.contention_seconds 60
8408
8409         $DIRECTIO write $DIR/$tfile 0 10 4096
8410         $CHECKSTAT -s 40960 $DIR/$tfile
8411
8412         # disable lockless i/o
8413         for node in $(osts_nodes); do
8414                 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'
8415         done
8416         lctl set_param -n osc.*.contention_seconds 0
8417         clear_osc_stats
8418
8419         dd if=/dev/zero of=$DIR/$tfile count=0
8420         $CHECKSTAT -s 0 $DIR/$tfile
8421
8422         restore_lustre_params <$p
8423         rm -f $p
8424         rm $DIR/$tfile
8425 }
8426 run_test 216 "check lockless direct write works and updates file size and kms correctly"
8427
8428 test_217() { # bug 22430
8429         local node
8430         for node in $(nodes_list); do
8431                 if [[ $node = *-* ]] ; then
8432                         echo "lctl ping $node@$NETTYPE"
8433                         lctl ping $node@$NETTYPE
8434                 else
8435                         echo "skipping $node (no hiphen detected)"
8436                 fi
8437         done
8438 }
8439 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
8440
8441 test_218() {
8442        # do directio so as not to populate the page cache
8443        log "creating a 10 Mb file"
8444        multiop $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8445        log "starting reads"
8446        dd if=$DIR/$tfile of=/dev/null bs=4096 &
8447        log "truncating the file"
8448        multiop $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8449        log "killing dd"
8450        kill %+ || true # reads might have finished
8451        echo "wait until dd is finished"
8452        wait
8453        log "removing the temporary file"
8454        rm -rf $DIR/$tfile || error "tmp file removal failed"
8455 }
8456 run_test 218 "parallel read and truncate should not deadlock ======================="
8457
8458 test_219() {
8459         # write one partial page
8460         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
8461         # set no grant so vvp_io_commit_write will do sync write
8462         $LCTL set_param fail_loc=0x411
8463         # write a full page at the end of file
8464         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
8465
8466         $LCTL set_param fail_loc=0
8467         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
8468         $LCTL set_param fail_loc=0x411
8469         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
8470 }
8471 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
8472
8473 test_220() { #LU-325
8474         remote_ost_nodsh && skip "remote OST with nodsh" && return
8475         local OSTIDX=0
8476
8477         mkdir -p $DIR/$tdir
8478         local OST=$(lfs osts | grep ${OSTIDX}": " | \
8479                 awk '{print $2}' | sed -e 's/_UUID$//')
8480
8481         # on the mdt's osc
8482         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
8483         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
8484                         osc.$mdtosc_proc1.prealloc_last_id)
8485         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
8486                         osc.$mdtosc_proc1.prealloc_next_id)
8487
8488         $LFS df -i
8489
8490         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
8491         #define OBD_FAIL_OST_ENOINO              0x229
8492         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
8493         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
8494         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
8495
8496         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
8497
8498         MDSOBJS=$((last_id - next_id))
8499         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
8500
8501         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
8502         echo "OST still has $count kbytes free"
8503
8504         echo "create $MDSOBJS files @next_id..."
8505         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
8506
8507         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
8508                         osc.$mdtosc_proc1.prealloc_last_id)
8509         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
8510                         osc.$mdtosc_proc1.prealloc_next_id)
8511
8512         echo "after creation, last_id=$last_id2, next_id=$next_id2"
8513         $LFS df -i
8514
8515         echo "cleanup..."
8516
8517         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
8518         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
8519
8520         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
8521         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
8522         echo "unlink $MDSOBJS files @$next_id..."
8523         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
8524 }
8525 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
8526
8527 test_221() {
8528         cp `which date` $MOUNT
8529
8530         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
8531         $LCTL set_param fail_loc=0x80001401
8532
8533         $MOUNT/date > /dev/null
8534         rm -f $MOUNT/date
8535 }
8536 run_test 221 "make sure fault and truncate race to not cause OOM"
8537
8538 test_222a() { # LU-1039, MRP-303
8539         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
8540         $LCTL set_param fail_loc=0x508
8541         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
8542         $LCTL set_param fail_loc=0
8543         df $DIR
8544 }
8545 run_test 222a "Don't panic on bulk IO failure"
8546
8547 test_222b() { # LU-1039, MRP-303
8548         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
8549         cancel_lru_locks osc
8550         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
8551         $LCTL set_param fail_loc=0x515
8552         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
8553         $LCTL set_param fail_loc=0
8554         df $DIR
8555 }
8556 run_test 222b "Don't panic on bulk IO failure"
8557
8558 # LU-1299 Executing or running ldd on a truncated executable does not
8559 # cause an out-of-memory condition.
8560 test_222() {
8561         dd if=`which date` of=$MOUNT/date bs=1k count=1
8562         chmod +x $MOUNT/date
8563
8564         $MOUNT/date > /dev/null
8565         ldd $MOUNT/date > /dev/null
8566         rm -f $MOUNT/date
8567 }
8568 run_test 222 "running truncated executable does not cause OOM"
8569
8570 #
8571 # tests that do cleanup/setup should be run at the end
8572 #
8573
8574 test_900() {
8575         local ls
8576         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
8577         $LCTL set_param fail_loc=0x903
8578         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
8579         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
8580                 echo "clear" > $ls
8581         done
8582         FAIL_ON_ERROR=true cleanup
8583         FAIL_ON_ERROR=true setup
8584 }
8585 run_test 900 "umount should not race with any mgc requeue thread"
8586
8587 complete $(basename $0) $SECONDS
8588 check_and_cleanup_lustre
8589 if [ "$I_MOUNTED" != "yes" ]; then
8590         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
8591 fi
8592 exit_status