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