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