Whamcloud - gitweb
LU-1110 fid: add full support for open-by-fid
[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_35a() {
2065         cp /bin/sh $DIR/f35a
2066         chmod 444 $DIR/f35a
2067         chown $RUNAS_ID $DIR/f35a
2068         $RUNAS $DIR/f35a && error || true
2069         rm $DIR/f35a
2070 }
2071 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2072
2073 test_36a() {
2074         rm -f $DIR/f36
2075         utime $DIR/f36 || error
2076 }
2077 run_test 36a "MDS utime check (mknod, utime) ==================="
2078
2079 test_36b() {
2080         echo "" > $DIR/f36
2081         utime $DIR/f36 || error
2082 }
2083 run_test 36b "OST utime check (open, utime) ===================="
2084
2085 test_36c() {
2086         rm -f $DIR/d36/f36
2087         mkdir $DIR/d36
2088         chown $RUNAS_ID $DIR/d36
2089         $RUNAS utime $DIR/d36/f36 || error
2090 }
2091 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2092
2093 test_36d() {
2094         [ ! -d $DIR/d36 ] && test_36c
2095         echo "" > $DIR/d36/f36
2096         $RUNAS utime $DIR/d36/f36 || error
2097 }
2098 run_test 36d "non-root OST utime check (open, utime) ==========="
2099
2100 test_36e() {
2101         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2102         mkdir -p $DIR/$tdir
2103         touch $DIR/$tdir/$tfile
2104         $RUNAS utime $DIR/$tdir/$tfile && \
2105                 error "utime worked, expected failure" || true
2106 }
2107 run_test 36e "utime on non-owned file (should return error) ===="
2108
2109 subr_36fh() {
2110         local fl="$1"
2111         local LANG_SAVE=$LANG
2112         local LC_LANG_SAVE=$LC_LANG
2113         export LANG=C LC_LANG=C # for date language
2114
2115         DATESTR="Dec 20  2000"
2116         mkdir -p $DIR/$tdir
2117         lctl set_param fail_loc=$fl
2118         date; date +%s
2119         cp /etc/hosts $DIR/$tdir/$tfile
2120         sync & # write RPC generated with "current" inode timestamp, but delayed
2121         sleep 1
2122         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2123         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2124         cancel_lru_locks osc
2125         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2126         date; date +%s
2127         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2128                 echo "BEFORE: $LS_BEFORE" && \
2129                 echo "AFTER : $LS_AFTER" && \
2130                 echo "WANT  : $DATESTR" && \
2131                 error "$DIR/$tdir/$tfile timestamps changed" || true
2132
2133         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2134 }
2135
2136 test_36f() {
2137         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2138         subr_36fh "0x80000214"
2139 }
2140 run_test 36f "utime on file racing with OST BRW write =========="
2141
2142 test_36g() {
2143         remote_ost_nodsh && skip "remote OST with nodsh" && return
2144         local fmd_max_age
2145         local fmd_before
2146         local fmd_after
2147
2148         mkdir -p $DIR/$tdir
2149         fmd_max_age=$(do_facet ost1 \
2150                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2151                 head -n 1")
2152
2153         fmd_before=$(do_facet ost1 \
2154                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2155         touch $DIR/$tdir/$tfile
2156         sleep $((fmd_max_age + 12))
2157         fmd_after=$(do_facet ost1 \
2158                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2159
2160         echo "fmd_before: $fmd_before"
2161         echo "fmd_after: $fmd_after"
2162         [ "$fmd_after" -gt "$fmd_before" ] && \
2163                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2164                 error "fmd didn't expire after ping" || true
2165 }
2166 run_test 36g "filter mod data cache expiry ====================="
2167
2168 test_36h() {
2169         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2170         subr_36fh "0x80000227"
2171 }
2172 run_test 36h "utime on file racing with OST BRW write =========="
2173
2174 test_37() {
2175         mkdir -p $DIR/$tdir
2176         echo f > $DIR/$tdir/fbugfile
2177         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2178         ls $DIR/$tdir | grep "\<fbugfile\>" && error
2179         $UMOUNT $DIR/$tdir || error
2180         rm -f $DIR/$tdir/fbugfile || error
2181 }
2182 run_test 37 "ls a mounted file system to check old content ====="
2183
2184 test_38() {
2185         local file=$DIR/$tfile
2186         touch $file
2187         openfile -f O_DIRECTORY $file
2188         local RC=$?
2189         local ENOTDIR=20
2190         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2191         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2192 }
2193 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2194
2195 test_39() {
2196         touch $DIR/$tfile
2197         touch $DIR/${tfile}2
2198 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2199 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2200 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2201         sleep 2
2202         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2203         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2204                 echo "mtime"
2205                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2206                 echo "atime"
2207                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2208                 echo "ctime"
2209                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2210                 error "O_TRUNC didn't change timestamps"
2211         fi
2212 }
2213 run_test 39 "mtime changed on create ==========================="
2214
2215 test_39b() {
2216         mkdir -p $DIR/$tdir
2217         cp -p /etc/passwd $DIR/$tdir/fopen
2218         cp -p /etc/passwd $DIR/$tdir/flink
2219         cp -p /etc/passwd $DIR/$tdir/funlink
2220         cp -p /etc/passwd $DIR/$tdir/frename
2221         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2222
2223         sleep 1
2224         echo "aaaaaa" >> $DIR/$tdir/fopen
2225         echo "aaaaaa" >> $DIR/$tdir/flink
2226         echo "aaaaaa" >> $DIR/$tdir/funlink
2227         echo "aaaaaa" >> $DIR/$tdir/frename
2228
2229         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2230         local link_new=`stat -c %Y $DIR/$tdir/flink`
2231         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2232         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2233
2234         cat $DIR/$tdir/fopen > /dev/null
2235         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2236         rm -f $DIR/$tdir/funlink2
2237         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2238
2239         for (( i=0; i < 2; i++ )) ; do
2240                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2241                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2242                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2243                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2244
2245                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2246                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2247                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2248                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2249
2250                 cancel_lru_locks osc
2251                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2252         done
2253 }
2254 run_test 39b "mtime change on open, link, unlink, rename  ======"
2255
2256 # this should be set to past
2257 TEST_39_MTIME=`date -d "1 year ago" +%s`
2258
2259 # bug 11063
2260 test_39c() {
2261         touch $DIR1/$tfile
2262         sleep 2
2263         local mtime0=`stat -c %Y $DIR1/$tfile`
2264
2265         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2266         local mtime1=`stat -c %Y $DIR1/$tfile`
2267         [ "$mtime1" = $TEST_39_MTIME ] || \
2268                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2269
2270         local d1=`date +%s`
2271         echo hello >> $DIR1/$tfile
2272         local d2=`date +%s`
2273         local mtime2=`stat -c %Y $DIR1/$tfile`
2274         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2275                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2276
2277         mv $DIR1/$tfile $DIR1/$tfile-1
2278
2279         for (( i=0; i < 2; i++ )) ; do
2280                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2281                 [ "$mtime2" = "$mtime3" ] || \
2282                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2283
2284                 cancel_lru_locks osc
2285                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2286         done
2287 }
2288 run_test 39c "mtime change on rename ==========================="
2289
2290 # bug 21114
2291 test_39d() {
2292         touch $DIR1/$tfile
2293
2294         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2295
2296         for (( i=0; i < 2; i++ )) ; do
2297                 local mtime=`stat -c %Y $DIR1/$tfile`
2298                 [ $mtime = $TEST_39_MTIME ] || \
2299                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2300
2301                 cancel_lru_locks osc
2302                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2303         done
2304 }
2305 run_test 39d "create, utime, stat =============================="
2306
2307 # bug 21114
2308 test_39e() {
2309         touch $DIR1/$tfile
2310         local mtime1=`stat -c %Y $DIR1/$tfile`
2311
2312         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2313
2314         for (( i=0; i < 2; i++ )) ; do
2315                 local mtime2=`stat -c %Y $DIR1/$tfile`
2316                 [ $mtime2 = $TEST_39_MTIME ] || \
2317                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2318
2319                 cancel_lru_locks osc
2320                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2321         done
2322 }
2323 run_test 39e "create, stat, utime, stat ========================"
2324
2325 # bug 21114
2326 test_39f() {
2327         touch $DIR1/$tfile
2328         mtime1=`stat -c %Y $DIR1/$tfile`
2329
2330         sleep 2
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 39f "create, stat, sleep, utime, stat ================="
2343
2344 # bug 11063
2345 test_39g() {
2346         echo hello >> $DIR1/$tfile
2347         local mtime1=`stat -c %Y $DIR1/$tfile`
2348
2349         sleep 2
2350         chmod o+r $DIR1/$tfile
2351
2352         for (( i=0; i < 2; i++ )) ; do
2353                 local mtime2=`stat -c %Y $DIR1/$tfile`
2354                 [ "$mtime1" = "$mtime2" ] || \
2355                         error "lost mtime: $mtime2, should be $mtime1"
2356
2357                 cancel_lru_locks osc
2358                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2359         done
2360 }
2361 run_test 39g "write, chmod, stat ==============================="
2362
2363 # bug 11063
2364 test_39h() {
2365         touch $DIR1/$tfile
2366         sleep 1
2367
2368         local d1=`date`
2369         echo hello >> $DIR1/$tfile
2370         local mtime1=`stat -c %Y $DIR1/$tfile`
2371
2372         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2373         local d2=`date`
2374         if [ "$d1" != "$d2" ]; then
2375                 echo "write and touch not within one second"
2376         else
2377                 for (( i=0; i < 2; i++ )) ; do
2378                         local mtime2=`stat -c %Y $DIR1/$tfile`
2379                         [ "$mtime2" = $TEST_39_MTIME ] || \
2380                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2381
2382                         cancel_lru_locks osc
2383                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2384                 done
2385         fi
2386 }
2387 run_test 39h "write, utime within one second, stat ============="
2388
2389 test_39i() {
2390         touch $DIR1/$tfile
2391         sleep 1
2392
2393         echo hello >> $DIR1/$tfile
2394         local mtime1=`stat -c %Y $DIR1/$tfile`
2395
2396         mv $DIR1/$tfile $DIR1/$tfile-1
2397
2398         for (( i=0; i < 2; i++ )) ; do
2399                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2400
2401                 [ "$mtime1" = "$mtime2" ] || \
2402                         error "lost mtime: $mtime2, should be $mtime1"
2403
2404                 cancel_lru_locks osc
2405                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2406         done
2407 }
2408 run_test 39i "write, rename, stat =============================="
2409
2410 test_39j() {
2411         touch $DIR1/$tfile
2412         sleep 1
2413
2414         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2415         local multipid=$!
2416         local mtime1=`stat -c %Y $DIR1/$tfile`
2417
2418         mv $DIR1/$tfile $DIR1/$tfile-1
2419
2420         kill -USR1 $multipid
2421         wait $multipid || error "multiop close failed"
2422
2423         for (( i=0; i < 2; i++ )) ; do
2424                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2425                 [ "$mtime1" = "$mtime2" ] || \
2426                         error "mtime is lost on close: $mtime2, should be $mtime1"
2427
2428                 cancel_lru_locks osc
2429                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2430         done
2431 }
2432 run_test 39j "write, rename, close, stat ======================="
2433
2434 test_39k() {
2435         touch $DIR1/$tfile
2436         sleep 1
2437
2438         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2439         local multipid=$!
2440         local mtime1=`stat -c %Y $DIR1/$tfile`
2441
2442         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2443
2444         kill -USR1 $multipid
2445         wait $multipid || error "multiop close failed"
2446
2447         for (( i=0; i < 2; i++ )) ; do
2448                 local mtime2=`stat -c %Y $DIR1/$tfile`
2449
2450                 [ "$mtime2" = $TEST_39_MTIME ] || \
2451                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2452
2453                 cancel_lru_locks osc
2454                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2455         done
2456 }
2457 run_test 39k "write, utime, close, stat ========================"
2458
2459 # this should be set to future
2460 TEST_39_ATIME=`date -d "1 year" +%s`
2461
2462 test_39l() {
2463         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2464         local atime_diff=$(do_facet $SINGLEMDS lctl get_param -n mdd.*.atime_diff)
2465
2466         mkdir -p $DIR/$tdir
2467
2468         # test setting directory atime to future
2469         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2470         local atime=$(stat -c %X $DIR/$tdir)
2471         [ "$atime" = $TEST_39_ATIME ] || \
2472                 error "atime is not set to future: $atime, should be $TEST_39_ATIME"
2473
2474         # test setting directory atime from future to now
2475         local d1=$(date +%s)
2476         ls $DIR/$tdir
2477         local d2=$(date +%s)
2478
2479         cancel_lru_locks mdc
2480         atime=$(stat -c %X $DIR/$tdir)
2481         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2482                 error "atime is not updated from future: $atime, should be $d1<atime<$d2"
2483
2484         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=2
2485         sleep 3
2486
2487         # test setting directory atime when now > dir atime + atime_diff
2488         d1=$(date +%s)
2489         ls $DIR/$tdir
2490         d2=$(date +%s)
2491         cancel_lru_locks mdc
2492         atime=$(stat -c %X $DIR/$tdir)
2493         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2494                 error "atime is not updated  : $atime, should be $d2"
2495
2496         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=60
2497         sleep 3
2498
2499         # test not setting directory atime when now < dir atime + atime_diff
2500         ls $DIR/$tdir
2501         cancel_lru_locks mdc
2502         atime=$(stat -c %X $DIR/$tdir)
2503         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2504                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2505
2506         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=$atime_diff
2507 }
2508 run_test 39l "directory atime update ==========================="
2509
2510 test_39m() {
2511         touch $DIR1/$tfile
2512         sleep 2
2513         local far_past_mtime=$(date -d "May 29 1953" +%s)
2514         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2515
2516         touch -m -d @$far_past_mtime $DIR1/$tfile
2517         touch -a -d @$far_past_atime $DIR1/$tfile
2518
2519         for (( i=0; i < 2; i++ )) ; do
2520                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2521                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2522                         error "atime or mtime set incorrectly"
2523
2524                 cancel_lru_locks osc
2525                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2526         done
2527 }
2528 run_test 39m "test atime and mtime before 1970"
2529
2530 test_40() {
2531         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2532         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2533         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2534 }
2535 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2536
2537 test_41() {
2538         # bug 1553
2539         small_write $DIR/f41 18
2540 }
2541 run_test 41 "test small file write + fstat ====================="
2542
2543 count_ost_writes() {
2544         lctl get_param -n osc.*.stats |
2545             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2546 }
2547
2548 # decent default
2549 WRITEBACK_SAVE=500
2550 DIRTY_RATIO_SAVE=40
2551 MAX_DIRTY_RATIO=50
2552 BG_DIRTY_RATIO_SAVE=10
2553 MAX_BG_DIRTY_RATIO=25
2554
2555 start_writeback() {
2556         trap 0
2557         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2558         # dirty_ratio, dirty_background_ratio
2559         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2560                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2561                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2562                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2563         else
2564                 # if file not here, we are a 2.4 kernel
2565                 kill -CONT `pidof kupdated`
2566         fi
2567 }
2568
2569 stop_writeback() {
2570         # setup the trap first, so someone cannot exit the test at the
2571         # exact wrong time and mess up a machine
2572         trap start_writeback EXIT
2573         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2574         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2575                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2576                 sysctl -w vm.dirty_writeback_centisecs=0
2577                 sysctl -w vm.dirty_writeback_centisecs=0
2578                 # save and increase /proc/sys/vm/dirty_ratio
2579                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2580                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2581                 # save and increase /proc/sys/vm/dirty_background_ratio
2582                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2583                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2584         else
2585                 # if file not here, we are a 2.4 kernel
2586                 kill -STOP `pidof kupdated`
2587         fi
2588 }
2589
2590 # ensure that all stripes have some grant before we test client-side cache
2591 setup_test42() {
2592         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2593                 dd if=/dev/zero of=$i bs=4k count=1
2594                 rm $i
2595         done
2596 }
2597
2598 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
2599 # file truncation, and file removal.
2600 test_42a() {
2601         setup_test42
2602         cancel_lru_locks osc
2603         stop_writeback
2604         sync; sleep 1; sync # just to be safe
2605         BEFOREWRITES=`count_ost_writes`
2606         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
2607         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
2608         AFTERWRITES=`count_ost_writes`
2609         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2610                 error "$BEFOREWRITES < $AFTERWRITES"
2611         start_writeback
2612 }
2613 run_test 42a "ensure that we don't flush on close =============="
2614
2615 test_42b() {
2616         setup_test42
2617         cancel_lru_locks osc
2618         stop_writeback
2619         sync
2620         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
2621         BEFOREWRITES=`count_ost_writes`
2622         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
2623         AFTERWRITES=`count_ost_writes`
2624         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2625                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
2626         fi
2627         BEFOREWRITES=`count_ost_writes`
2628         sync || error "sync: $?"
2629         AFTERWRITES=`count_ost_writes`
2630         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2631                 error "$BEFOREWRITES < $AFTERWRITES on sync"
2632         fi
2633         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
2634         start_writeback
2635         return 0
2636 }
2637 run_test 42b "test destroy of file with cached dirty data ======"
2638
2639 # if these tests just want to test the effect of truncation,
2640 # they have to be very careful.  consider:
2641 # - the first open gets a {0,EOF}PR lock
2642 # - the first write conflicts and gets a {0, count-1}PW
2643 # - the rest of the writes are under {count,EOF}PW
2644 # - the open for truncate tries to match a {0,EOF}PR
2645 #   for the filesize and cancels the PWs.
2646 # any number of fixes (don't get {0,EOF} on open, match
2647 # composite locks, do smarter file size management) fix
2648 # this, but for now we want these tests to verify that
2649 # the cancellation with truncate intent works, so we
2650 # start the file with a full-file pw lock to match against
2651 # until the truncate.
2652 trunc_test() {
2653         test=$1
2654         file=$DIR/$test
2655         offset=$2
2656         cancel_lru_locks osc
2657         stop_writeback
2658         # prime the file with 0,EOF PW to match
2659         touch $file
2660         $TRUNCATE $file 0
2661         sync; sync
2662         # now the real test..
2663         dd if=/dev/zero of=$file bs=1024 count=100
2664         BEFOREWRITES=`count_ost_writes`
2665         $TRUNCATE $file $offset
2666         cancel_lru_locks osc
2667         AFTERWRITES=`count_ost_writes`
2668         start_writeback
2669 }
2670
2671 test_42c() {
2672         trunc_test 42c 1024
2673         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
2674             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
2675         rm $file
2676 }
2677 run_test 42c "test partial truncate of file with cached dirty data"
2678
2679 test_42d() {
2680         trunc_test 42d 0
2681         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2682             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
2683         rm $file
2684 }
2685 run_test 42d "test complete truncate of file with cached dirty data"
2686
2687 test_42e() { # bug22074
2688         local TDIR=$DIR/${tdir}e
2689         local pagesz=$(page_size)
2690         local pages=16 # hardcoded 16 pages, don't change it.
2691         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
2692         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
2693         local max_dirty_mb
2694         local warmup_files
2695
2696         mkdir -p $TDIR
2697         $SETSTRIPE -c 1 $TDIR
2698         createmany -o $TDIR/f $files
2699
2700         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
2701
2702         # we assume that with $OSTCOUNT files, at least one of them will
2703         # be allocated on OST0.
2704         warmup_files=$((OSTCOUNT * max_dirty_mb))
2705         createmany -o $TDIR/w $warmup_files
2706
2707         # write a large amount of data into one file and sync, to get good
2708         # avail_grant number from OST.
2709         for ((i=0; i<$warmup_files; i++)); do
2710                 idx=$($GETSTRIPE -i $TDIR/w$i)
2711                 [ $idx -ne 0 ] && continue
2712                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
2713                 break
2714         done
2715         [ $i -gt $warmup_files ] && error "OST0 is still cold"
2716         sync
2717         $LCTL get_param $proc_osc0/cur_dirty_bytes
2718         $LCTL get_param $proc_osc0/cur_grant_bytes
2719
2720         # create as much dirty pages as we can while not to trigger the actual
2721         # RPCs directly. but depends on the env, VFS may trigger flush during this
2722         # period, hopefully we are good.
2723         for ((i=0; i<$warmup_files; i++)); do
2724                 idx=$($GETSTRIPE -i $TDIR/w$i)
2725                 [ $idx -ne 0 ] && continue
2726                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
2727         done
2728         $LCTL get_param $proc_osc0/cur_dirty_bytes
2729         $LCTL get_param $proc_osc0/cur_grant_bytes
2730
2731         # perform the real test
2732         $LCTL set_param $proc_osc0/rpc_stats 0
2733         for ((;i<$files; i++)); do
2734                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
2735                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
2736         done
2737         sync
2738         $LCTL get_param $proc_osc0/rpc_stats
2739
2740         local percent=0
2741         local have_ppr=false
2742         $LCTL get_param $proc_osc0/rpc_stats |
2743                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
2744                         # skip lines until we are at the RPC histogram data
2745                         [ "$PPR" == "pages" ] && have_ppr=true && continue
2746                         $have_ppr || continue
2747
2748                         # we only want the percent stat for < 16 pages
2749                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
2750
2751                         percent=$((percent + WPCT))
2752                         if [ $percent -gt 15 ]; then
2753                                 error "less than 16-pages write RPCs" \
2754                                       "$percent% > 15%"
2755                                 break
2756                         fi
2757                 done
2758         rm -rf $TDIR
2759 }
2760 run_test 42e "verify sub-RPC writes are not done synchronously"
2761
2762 test_43() {
2763         mkdir -p $DIR/$tdir
2764         cp -p /bin/ls $DIR/$tdir/$tfile
2765         multiop $DIR/$tdir/$tfile Ow_c &
2766         pid=$!
2767         # give multiop a chance to open
2768         sleep 1
2769
2770         $DIR/$tdir/$tfile && error || true
2771         kill -USR1 $pid
2772 }
2773 run_test 43 "execution of file opened for write should return -ETXTBSY"
2774
2775 test_43a() {
2776         mkdir -p $DIR/d43
2777         cp -p `which multiop` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2778         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2779         MULTIOP_PID=$!
2780         multiop $DIR/d43/multiop Oc && error "expected error, got success"
2781         kill -USR1 $MULTIOP_PID || return 2
2782         wait $MULTIOP_PID || return 3
2783         rm $TMP/test43.junk
2784 }
2785 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
2786
2787 test_43b() {
2788         mkdir -p $DIR/d43
2789         cp -p `which multiop` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2790         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2791         MULTIOP_PID=$!
2792         $TRUNCATE $DIR/d43/multiop 0 && error "expected error, got success"
2793         kill -USR1 $MULTIOP_PID || return 2
2794         wait $MULTIOP_PID || return 3
2795         rm $TMP/test43.junk
2796 }
2797 run_test 43b "truncate of file being executed should return -ETXTBSY"
2798
2799 test_43c() {
2800         local testdir="$DIR/d43c"
2801         mkdir -p $testdir
2802         cp $SHELL $testdir/
2803         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
2804                 ( cd $testdir && md5sum -c)
2805 }
2806 run_test 43c "md5sum of copy into lustre========================"
2807
2808 test_44() {
2809         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
2810         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
2811         dd if=$DIR/f1 bs=4k count=1 > /dev/null
2812 }
2813 run_test 44 "zero length read from a sparse stripe ============="
2814
2815 test_44a() {
2816     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
2817                          awk '{print $2}'`
2818     [ -z "$nstripe" ] && skip "can't get stripe info" && return
2819     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
2820     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
2821                       awk '{print $2}'`
2822     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
2823         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
2824     fi
2825
2826     OFFSETS="0 $((stride/2)) $((stride-1))"
2827     for offset in $OFFSETS ; do
2828       for i in `seq 0 $((nstripe-1))`; do
2829         local GLOBALOFFSETS=""
2830         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
2831         local myfn=$DIR/d44a-$size
2832         echo "--------writing $myfn at $size"
2833         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
2834         GLOBALOFFSETS="$GLOBALOFFSETS $size"
2835         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2836                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2837
2838         for j in `seq 0 $((nstripe-1))`; do
2839             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
2840             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
2841             GLOBALOFFSETS="$GLOBALOFFSETS $size"
2842         done
2843         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2844                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2845         rm -f $myfn
2846       done
2847     done
2848 }
2849 run_test 44a "test sparse pwrite ==============================="
2850
2851 dirty_osc_total() {
2852         tot=0
2853         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
2854                 tot=$(($tot + $d))
2855         done
2856         echo $tot
2857 }
2858 do_dirty_record() {
2859         before=`dirty_osc_total`
2860         echo executing "\"$*\""
2861         eval $*
2862         after=`dirty_osc_total`
2863         echo before $before, after $after
2864 }
2865 test_45() {
2866         f="$DIR/f45"
2867         # Obtain grants from OST if it supports it
2868         echo blah > ${f}_grant
2869         stop_writeback
2870         sync
2871         do_dirty_record "echo blah > $f"
2872         [ $before -eq $after ] && error "write wasn't cached"
2873         do_dirty_record "> $f"
2874         [ $before -gt $after ] || error "truncate didn't lower dirty count"
2875         do_dirty_record "echo blah > $f"
2876         [ $before -eq $after ] && error "write wasn't cached"
2877         do_dirty_record "sync"
2878         [ $before -gt $after ] || error "writeback didn't lower dirty count"
2879         do_dirty_record "echo blah > $f"
2880         [ $before -eq $after ] && error "write wasn't cached"
2881         do_dirty_record "cancel_lru_locks osc"
2882         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
2883         start_writeback
2884 }
2885 run_test 45 "osc io page accounting ============================"
2886
2887 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
2888 # test tickles a bug where re-dirtying a page was failing to be mapped to the
2889 # objects offset and an assert hit when an rpc was built with 1023's mapped
2890 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
2891 test_46() {
2892         f="$DIR/f46"
2893         stop_writeback
2894         sync
2895         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2896         sync
2897         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
2898         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2899         sync
2900         start_writeback
2901 }
2902 run_test 46 "dirtying a previously written page ================"
2903
2904 # test_47 is removed "Device nodes check" is moved to test_28
2905
2906 test_48a() { # bug 2399
2907         check_kernel_version 34 || return 0
2908         mkdir -p $DIR/d48a
2909         cd $DIR/d48a
2910         mv $DIR/d48a $DIR/d48.new || error "move directory failed"
2911         mkdir $DIR/d48a || error "recreate directory failed"
2912         touch foo || error "'touch foo' failed after recreating cwd"
2913         mkdir bar || error "'mkdir foo' failed after recreating cwd"
2914         if check_kernel_version 44; then
2915                 touch .foo || error "'touch .foo' failed after recreating cwd"
2916                 mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
2917         fi
2918         ls . > /dev/null || error "'ls .' failed after recreating cwd"
2919         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
2920         cd . || error "'cd .' failed after recreating cwd"
2921         mkdir . && error "'mkdir .' worked after recreating cwd"
2922         rmdir . && error "'rmdir .' worked after recreating cwd"
2923         ln -s . baz || error "'ln -s .' failed after recreating cwd"
2924         cd .. || error "'cd ..' failed after recreating cwd"
2925 }
2926 run_test 48a "Access renamed working dir (should return errors)="
2927
2928 test_48b() { # bug 2399
2929         check_kernel_version 34 || return 0
2930         mkdir -p $DIR/d48b
2931         cd $DIR/d48b
2932         rmdir $DIR/d48b || error "remove cwd $DIR/d48b failed"
2933         touch foo && error "'touch foo' worked after removing cwd"
2934         mkdir foo && error "'mkdir foo' worked after removing cwd"
2935         if check_kernel_version 44; then
2936                 touch .foo && error "'touch .foo' worked after removing cwd"
2937                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
2938         fi
2939         ls . > /dev/null && error "'ls .' worked after removing cwd"
2940         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
2941         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
2942         mkdir . && error "'mkdir .' worked after removing cwd"
2943         rmdir . && error "'rmdir .' worked after removing cwd"
2944         ln -s . foo && error "'ln -s .' worked after removing cwd"
2945         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
2946 }
2947 run_test 48b "Access removed working dir (should return errors)="
2948
2949 test_48c() { # bug 2350
2950         check_kernel_version 36 || return 0
2951         #lctl set_param debug=-1
2952         #set -vx
2953         mkdir -p $DIR/d48c/dir
2954         cd $DIR/d48c/dir
2955         $TRACE rmdir $DIR/d48c/dir || error "remove cwd $DIR/d48c/dir failed"
2956         $TRACE touch foo && error "'touch foo' worked after removing cwd"
2957         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
2958         if check_kernel_version 44; then
2959                 touch .foo && error "'touch .foo' worked after removing cwd"
2960                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
2961         fi
2962         $TRACE ls . && error "'ls .' worked after removing cwd"
2963         $TRACE ls .. || error "'ls ..' failed after removing cwd"
2964         is_patchless || ( $TRACE cd . && error "'cd .' worked after removing cwd" )
2965         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
2966         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
2967         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
2968         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
2969 }
2970 run_test 48c "Access removed working subdir (should return errors)"
2971
2972 test_48d() { # bug 2350
2973         check_kernel_version 36 || return 0
2974         #lctl set_param debug=-1
2975         #set -vx
2976         mkdir -p $DIR/d48d/dir
2977         cd $DIR/d48d/dir
2978         $TRACE rmdir $DIR/d48d/dir || error "remove cwd $DIR/d48d/dir failed"
2979         $TRACE rmdir $DIR/d48d || error "remove parent $DIR/d48d failed"
2980         $TRACE touch foo && error "'touch foo' worked after removing parent"
2981         $TRACE mkdir foo && error "'mkdir foo' worked after removing parent"
2982         if check_kernel_version 44; then
2983                 touch .foo && error "'touch .foo' worked after removing parent"
2984                 mkdir .foo && error "'mkdir .foo' worked after removing parent"
2985         fi
2986         $TRACE ls . && error "'ls .' worked after removing parent"
2987         $TRACE ls .. && error "'ls ..' worked after removing parent"
2988         is_patchless || ( $TRACE cd . && error "'cd .' worked after recreate parent" )
2989         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
2990         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
2991         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
2992         is_patchless || ( $TRACE cd .. && error "'cd ..' worked after removing parent" || true )
2993 }
2994 run_test 48d "Access removed parent subdir (should return errors)"
2995
2996 test_48e() { # bug 4134
2997         check_kernel_version 41 || return 0
2998         #lctl set_param debug=-1
2999         #set -vx
3000         mkdir -p $DIR/d48e/dir
3001         cd $DIR/d48e/dir
3002         $TRACE rmdir $DIR/d48e/dir || error "remove cwd $DIR/d48e/dir failed"
3003         $TRACE rmdir $DIR/d48e || error "remove parent $DIR/d48e failed"
3004         $TRACE touch $DIR/d48e || error "'touch $DIR/d48e' failed"
3005         $TRACE chmod +x $DIR/d48e || error "'chmod +x $DIR/d48e' failed"
3006         # On a buggy kernel addition of "touch foo" after cd .. will
3007         # produce kernel oops in lookup_hash_it
3008         touch ../foo && error "'cd ..' worked after recreate parent"
3009         cd $DIR
3010         $TRACE rm $DIR/d48e || error "rm '$DIR/d48e' failed"
3011 }
3012 run_test 48e "Access to recreated parent subdir (should return errors)"
3013
3014 test_50() {
3015         # bug 1485
3016         mkdir $DIR/d50
3017         cd $DIR/d50
3018         ls /proc/$$/cwd || error
3019 }
3020 run_test 50 "special situations: /proc symlinks  ==============="
3021
3022 test_51a() {    # was test_51
3023         # bug 1516 - create an empty entry right after ".." then split dir
3024         mkdir $DIR/d51
3025         touch $DIR/d51/foo
3026         $MCREATE $DIR/d51/bar
3027         rm $DIR/d51/foo
3028         createmany -m $DIR/d51/longfile 201
3029         FNUM=202
3030         while [ `ls -sd $DIR/d51 | awk '{ print $1 }'` -eq 4 ]; do
3031                 $MCREATE $DIR/d51/longfile$FNUM
3032                 FNUM=$(($FNUM + 1))
3033                 echo -n "+"
3034         done
3035         echo
3036         ls -l $DIR/d51 > /dev/null || error
3037 }
3038 run_test 51a "special situations: split htree with empty entry =="
3039
3040 export NUMTEST=70000
3041 test_51b() {
3042         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3043         [ $NUMFREE -lt 21000 ] && \
3044                 skip "not enough free inodes ($NUMFREE)" && \
3045                 return
3046
3047         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 50))
3048
3049         mkdir -p $DIR/d51b
3050         createmany -d $DIR/d51b/t- $NUMTEST
3051 }
3052 run_test 51b "mkdir .../t-0 --- .../t-$NUMTEST ===================="
3053
3054 test_51ba() { # LU-993
3055         local BASE=$DIR/d51b
3056         # unlink all but 100 subdirectories, then check it still works
3057         local LEFT=100
3058         local DELETE=$((NUMTEST - LEFT))
3059
3060         ! [ -d "${BASE}/t-$DELETE" ] && skip "test_51b() not run" && return 0
3061
3062         # for ldiskfs the nlink count should be 1, but this is OSD specific
3063         # and so this is listed for informational purposes only
3064         log "nlink before: $(stat -c %h $BASE)"
3065         unlinkmany -d $BASE/t- $DELETE ||
3066                 error "unlink of first $DELETE subdirs failed"
3067
3068         log "nlink between: $(stat -c %h $BASE)"
3069         local FOUND=$(ls -l ${BASE} | wc -l)
3070         FOUND=$((FOUND - 1))  # trim the first line of ls output
3071         [ $FOUND -ne $LEFT ] &&
3072                 error "can't find subdirs: found only $FOUND/$LEFT"
3073
3074         unlinkmany -d $BASE/t- $DELETE $LEFT ||
3075                 error "unlink of second $LEFT subdirs failed"
3076         log "nlink after: $(stat -c %h $BASE)"
3077 }
3078 run_test 51ba "rmdir .../t-0 --- .../t-$NUMTEST"
3079
3080 test_51bb() {
3081         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3082
3083         local ndirs=${TEST51BB_NDIRS:-10}
3084         local nfiles=${TEST51BB_NFILES:-100}
3085
3086         local numfree=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3087
3088         [ $numfree -lt $(( ndirs * nfiles)) ] && \
3089                 nfiles=$(( numfree / ndirs - 10 ))
3090
3091         local dir=$DIR/d51bb
3092         mkdir -p $dir
3093         local savePOLICY=$(lctl get_param -n lmv.*.placement)
3094         lctl set_param -n lmv.*.placement=CHAR
3095
3096         lfs df -i $dir
3097         local IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3098         OLDUSED=($IUSED)
3099
3100         declare -a dirs
3101         for ((i=0; i < $ndirs; i++)); do
3102                 dirs[i]=$dir/$RANDOM
3103                 echo Creating directory ${dirs[i]}
3104                 mkdir -p ${dirs[i]}
3105                 ls $dir
3106                 echo Creating $nfiles in dir ${dirs[i]} ...
3107                 echo "createmany -o ${dirs[i]}/$tfile- $nfiles"
3108                 createmany -o ${dirs[i]}/$tfile- $nfiles
3109         done
3110         ls $dir
3111
3112         sleep 1
3113
3114         IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3115         NEWUSED=($IUSED)
3116
3117         local rc=0
3118         for ((i=0; i<${#NEWUSED[@]}; i++)); do
3119                 echo "mds $i: inodes count OLD ${OLDUSED[$i]} NEW ${NEWUSED[$i]}"
3120                 [ ${OLDUSED[$i]} -lt ${NEWUSED[$i]} ] || rc=$((rc + 1))
3121         done
3122
3123         lctl set_param -n lmv.*.placement=$savePOLICY
3124
3125         [ $rc -ne $MDSCOUNT ] || \
3126                 error "Objects/inodes are not distributed over all mds servers"
3127 }
3128 run_test 51bb "mkdir createmany CMD $MDSCOUNT  ===================="
3129
3130 test_51d() {
3131         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3132         mkdir -p $DIR/d51d
3133         createmany -o $DIR/d51d/t- 1000
3134         $GETSTRIPE $DIR/d51d > $TMP/files
3135         for N in `seq 0 $((OSTCOUNT - 1))`; do
3136             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3137             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3138             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3139         done
3140         unlinkmany $DIR/d51d/t- 1000
3141
3142         NLAST=0
3143         for N in `seq 1 $((OSTCOUNT - 1))`; do
3144             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3145                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3146             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3147                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3148
3149             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3150                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3151             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3152                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3153             NLAST=$N
3154         done
3155 }
3156 run_test 51d "check object distribution ===================="
3157
3158 test_52a() {
3159         [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
3160         mkdir -p $DIR/d52a
3161         touch $DIR/d52a/foo
3162         chattr +a $DIR/d52a/foo || error "chattr +a failed"
3163         echo bar >> $DIR/d52a/foo || error "append bar failed"
3164         cp /etc/hosts $DIR/d52a/foo && error "cp worked"
3165         rm -f $DIR/d52a/foo 2>/dev/null && error "rm worked"
3166         link $DIR/d52a/foo $DIR/d52a/foo_link 2>/dev/null && error "link worked"
3167         echo foo >> $DIR/d52a/foo || error "append foo failed"
3168         mrename $DIR/d52a/foo $DIR/d52a/foo_ren && error "rename worked"
3169         lsattr $DIR/d52a/foo | egrep -q "^-+a[-e]+ $DIR/d52a/foo" || error "lsattr"
3170         chattr -a $DIR/d52a/foo || error "chattr -a failed"
3171         cp -r $DIR/d52a /tmp/
3172         rm -fr $DIR/d52a || error "cleanup rm failed"
3173 }
3174 run_test 52a "append-only flag test (should return errors) ====="
3175
3176 test_52b() {
3177         [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
3178         mkdir -p $DIR/d52b
3179         touch $DIR/d52b/foo
3180         chattr +i $DIR/d52b/foo || error "chattr +i failed"
3181         cat test > $DIR/d52b/foo && error "cat test worked"
3182         cp /etc/hosts $DIR/d52b/foo && error "cp worked"
3183         rm -f $DIR/d52b/foo 2>/dev/null && error "rm worked"
3184         link $DIR/d52b/foo $DIR/d52b/foo_link 2>/dev/null && error  "link worked"
3185         echo foo >> $DIR/d52b/foo && error "echo worked"
3186         mrename $DIR/d52b/foo $DIR/d52b/foo_ren && error "rename worked"
3187         [ -f $DIR/d52b/foo ] || error
3188         [ -f $DIR/d52b/foo_ren ] && error
3189         lsattr $DIR/d52b/foo | egrep -q "^-+i[-e]+ $DIR/d52b/foo" || error "lsattr"
3190         chattr -i $DIR/d52b/foo || error "chattr failed"
3191
3192         rm -fr $DIR/d52b || error
3193 }
3194 run_test 52b "immutable flag test (should return errors) ======="
3195
3196 test_53() {
3197         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3198         remote_ost_nodsh && skip "remote OST with nodsh" && return
3199
3200         local param
3201         local ostname
3202         local mds_last
3203         local ost_last
3204         local ostnum
3205
3206         # only test MDT0000
3207         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3208         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3209                 param=`echo ${value[0]} | cut -d "=" -f1`
3210                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3211                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3212                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
3213                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id | head -n 1)
3214                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3215                 if [ $ost_last != $mds_last ]; then
3216                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3217                 fi
3218         done
3219 }
3220 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3221
3222 test_54a() {
3223         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3224         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3225         $SOCKETSERVER $DIR/socket
3226         $SOCKETCLIENT $DIR/socket || error
3227         $MUNLINK $DIR/socket
3228 }
3229 run_test 54a "unix domain socket test =========================="
3230
3231 test_54b() {
3232         f="$DIR/f54b"
3233         mknod $f c 1 3
3234         chmod 0666 $f
3235         dd if=/dev/zero of=$f bs=`page_size` count=1
3236 }
3237 run_test 54b "char device works in lustre ======================"
3238
3239 find_loop_dev() {
3240         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3241         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3242         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3243
3244         for i in `seq 3 7`; do
3245                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3246                 LOOPDEV=$LOOPBASE$i
3247                 LOOPNUM=$i
3248                 break
3249         done
3250 }
3251
3252 test_54c() {
3253         tfile="$DIR/f54c"
3254         tdir="$DIR/d54c"
3255         loopdev="$DIR/loop54c"
3256
3257         find_loop_dev
3258         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3259         mknod $loopdev b 7 $LOOPNUM
3260         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3261         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3262         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3263         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3264         mkdir -p $tdir
3265         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3266         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3267         df $tdir
3268         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3269         $UMOUNT $tdir
3270         losetup -d $loopdev
3271         rm $loopdev
3272 }
3273 run_test 54c "block device works in lustre ====================="
3274
3275 test_54d() {
3276         f="$DIR/f54d"
3277         string="aaaaaa"
3278         mknod $f p
3279         [ "$string" = `echo $string > $f | cat $f` ] || error
3280 }
3281 run_test 54d "fifo device works in lustre ======================"
3282
3283 test_54e() {
3284         check_kernel_version 46 || return 0
3285         f="$DIR/f54e"
3286         string="aaaaaa"
3287         cp -aL /dev/console $f
3288         echo $string > $f || error
3289 }
3290 run_test 54e "console/tty device works in lustre ======================"
3291
3292 #The test_55 used to be iopen test and it was removed by bz#24037.
3293 #run_test 55 "check iopen_connect_dentry() ======================"
3294
3295 test_56a() {    # was test_56
3296         rm -rf $DIR/d56
3297         $SETSTRIPE -d $DIR
3298         mkdir $DIR/d56
3299         mkdir $DIR/d56/dir
3300         NUMFILES=3
3301         NUMFILESx2=$(($NUMFILES * 2))
3302         for i in `seq 1 $NUMFILES` ; do
3303                 touch $DIR/d56/file$i
3304                 touch $DIR/d56/dir/file$i
3305         done
3306
3307         # test lfs getstripe with --recursive
3308         FILENUM=`$GETSTRIPE --recursive $DIR/d56 | grep -c obdidx`
3309         [ $FILENUM -eq $NUMFILESx2 ] ||
3310                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3311         FILENUM=`$GETSTRIPE $DIR/d56 | grep -c obdidx`
3312         [ $FILENUM -eq $NUMFILES ] ||
3313                 error "$GETSTRIPE $DIR/d56: found $FILENUM, not $NUMFILES"
3314         echo "$GETSTRIPE --recursive passed."
3315
3316         # test lfs getstripe with file instead of dir
3317         FILENUM=`$GETSTRIPE $DIR/d56/file1 | grep -c obdidx`
3318         [ $FILENUM  -eq 1 ] || error \
3319                  "$GETSTRIPE $DIR/d56/file1: found $FILENUM, not 1"
3320         echo "$GETSTRIPE file1 passed."
3321
3322         #test lfs getstripe with --verbose
3323         [ `$GETSTRIPE --verbose $DIR/d56 | grep -c lmm_magic` -eq $NUMFILES ] ||
3324                 error "$GETSTRIPE --verbose $DIR/d56: want $NUMFILES lmm_magic"
3325         [ `$GETSTRIPE $DIR/d56 | grep -c lmm_magic` -eq 0 ] ||
3326             error "$GETSTRIPE $DIR/d56: showed lmm_magic"
3327         echo "$GETSTRIPE --verbose passed."
3328
3329         #test lfs getstripe with --obd
3330         $GETSTRIPE --obd wrong_uuid $DIR/d56 2>&1 | grep -q "unknown obduuid" ||
3331                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3332
3333         [  "$OSTCOUNT" -lt 2 ] &&
3334                 skip_env "skipping other $GETSTRIPE --obd test" && return
3335
3336         OSTIDX=1
3337         OBDUUID=$(ostuuid_from_index $OSTIDX)
3338         FILENUM=`$GETSTRIPE -ir $DIR/d56 | grep -x $OSTIDX | wc -l`
3339         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/d56 | grep obdidx | wc -l`
3340         [ $FOUND -eq $FILENUM ] ||
3341                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3342         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/d56 |
3343                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3344                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3345                 error "$GETSTRIPE --obd: should not show file on other obd"
3346         echo "$GETSTRIPE --obd passed"
3347 }
3348 run_test 56a "check $GETSTRIPE"
3349
3350 NUMFILES=3
3351 NUMDIRS=3
3352 setup_56() {
3353         local LOCAL_NUMFILES="$1"
3354         local LOCAL_NUMDIRS="$2"
3355         local MKDIR_PARAMS="$3"
3356
3357         if [ ! -d "$TDIR" ] ; then
3358                 mkdir -p $TDIR
3359                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3360                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3361                         touch $TDIR/file$i
3362                 done
3363                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3364                         mkdir $TDIR/dir$i
3365                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3366                                 touch $TDIR/dir$i/file$j
3367                         done
3368                 done
3369         fi
3370 }
3371
3372 setup_56_special() {
3373         LOCAL_NUMFILES=$1
3374         LOCAL_NUMDIRS=$2
3375         setup_56 $1 $2
3376         if [ ! -e "$TDIR/loop1b" ] ; then
3377                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3378                         mknod $TDIR/loop${i}b b 7 $i
3379                         mknod $TDIR/null${i}c c 1 3
3380                         ln -s $TDIR/file1 $TDIR/link${i}l
3381                 done
3382                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3383                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3384                         mknod $TDIR/dir$i/null${i}c c 1 3
3385                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3386                 done
3387         fi
3388 }
3389
3390 test_56g() {
3391         $SETSTRIPE -d $DIR
3392
3393         TDIR=$DIR/${tdir}g
3394         setup_56 $NUMFILES $NUMDIRS
3395
3396         EXPECTED=$(($NUMDIRS + 2))
3397         # test lfs find with -name
3398         for i in $(seq 1 $NUMFILES) ; do
3399                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3400                 [ $NUMS -eq $EXPECTED ] ||
3401                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3402                               "found $NUMS, expected $EXPECTED"
3403         done
3404 }
3405 run_test 56g "check lfs find -name ============================="
3406
3407 test_56h() {
3408         $SETSTRIPE -d $DIR
3409
3410         TDIR=$DIR/${tdir}g
3411         setup_56 $NUMFILES $NUMDIRS
3412
3413         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3414         # test lfs find with ! -name
3415         for i in $(seq 1 $NUMFILES) ; do
3416                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3417                 [ $NUMS -eq $EXPECTED ] ||
3418                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3419                               "found $NUMS, expected $EXPECTED"
3420         done
3421 }
3422 run_test 56h "check lfs find ! -name ============================="
3423
3424 test_56i() {
3425        tdir=${tdir}i
3426        mkdir -p $DIR/$tdir
3427        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3428        CMD="$LFIND -ost $UUID $DIR/$tdir"
3429        OUT=$($CMD)
3430        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3431 }
3432 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3433
3434 test_56j() {
3435         TDIR=$DIR/${tdir}g
3436         setup_56_special $NUMFILES $NUMDIRS
3437
3438         EXPECTED=$((NUMDIRS + 1))
3439         CMD="$LFIND -type d $TDIR"
3440         NUMS=$($CMD | wc -l)
3441         [ $NUMS -eq $EXPECTED ] ||
3442                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3443 }
3444 run_test 56j "check lfs find -type d ============================="
3445
3446 test_56k() {
3447         TDIR=$DIR/${tdir}g
3448         setup_56_special $NUMFILES $NUMDIRS
3449
3450         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3451         CMD="$LFIND -type f $TDIR"
3452         NUMS=$($CMD | wc -l)
3453         [ $NUMS -eq $EXPECTED ] ||
3454                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3455 }
3456 run_test 56k "check lfs find -type f ============================="
3457
3458 test_56l() {
3459         TDIR=$DIR/${tdir}g
3460         setup_56_special $NUMFILES $NUMDIRS
3461
3462         EXPECTED=$((NUMDIRS + NUMFILES))
3463         CMD="$LFIND -type b $TDIR"
3464         NUMS=$($CMD | wc -l)
3465         [ $NUMS -eq $EXPECTED ] ||
3466                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3467 }
3468 run_test 56l "check lfs find -type b ============================="
3469
3470 test_56m() {
3471         TDIR=$DIR/${tdir}g
3472         setup_56_special $NUMFILES $NUMDIRS
3473
3474         EXPECTED=$((NUMDIRS + NUMFILES))
3475         CMD="$LFIND -type c $TDIR"
3476         NUMS=$($CMD | wc -l)
3477         [ $NUMS -eq $EXPECTED ] ||
3478                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3479 }
3480 run_test 56m "check lfs find -type c ============================="
3481
3482 test_56n() {
3483         TDIR=$DIR/${tdir}g
3484         setup_56_special $NUMFILES $NUMDIRS
3485
3486         EXPECTED=$((NUMDIRS + NUMFILES))
3487         CMD="$LFIND -type l $TDIR"
3488         NUMS=$($CMD | wc -l)
3489         [ $NUMS -eq $EXPECTED ] ||
3490                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3491 }
3492 run_test 56n "check lfs find -type l ============================="
3493
3494 test_56o() {
3495         TDIR=$DIR/${tdir}o
3496         setup_56 $NUMFILES $NUMDIRS
3497
3498         utime $TDIR/file1 > /dev/null || error "utime (1)"
3499         utime $TDIR/file2 > /dev/null || error "utime (2)"
3500         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3501         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3502         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3503         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3504
3505         EXPECTED=4
3506         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3507         [ $NUMS -eq $EXPECTED ] || \
3508                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3509
3510         EXPECTED=12
3511         CMD="$LFIND -mtime 0 $TDIR"
3512         NUMS=$($CMD | wc -l)
3513         [ $NUMS -eq $EXPECTED ] ||
3514                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3515 }
3516 run_test 56o "check lfs find -mtime for old files =========================="
3517
3518 test_56p() {
3519         [ $RUNAS_ID -eq $UID ] &&
3520                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3521
3522         TDIR=$DIR/${tdir}p
3523         setup_56 $NUMFILES $NUMDIRS
3524
3525         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3526         EXPECTED=$NUMFILES
3527         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3528         NUMS=$($CMD | wc -l)
3529         [ $NUMS -eq $EXPECTED ] || \
3530                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3531
3532         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3533         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3534         NUMS=$($CMD | wc -l)
3535         [ $NUMS -eq $EXPECTED ] || \
3536                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3537 }
3538 run_test 56p "check lfs find -uid and ! -uid ==============================="
3539
3540 test_56q() {
3541         [ $RUNAS_ID -eq $UID ] &&
3542                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3543
3544         TDIR=$DIR/${tdir}q
3545         setup_56 $NUMFILES $NUMDIRS
3546
3547         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3548
3549         EXPECTED=$NUMFILES
3550         CMD="$LFIND -gid $RUNAS_GID $TDIR"
3551         NUMS=$($CMD | wc -l)
3552         [ $NUMS -eq $EXPECTED ] ||
3553                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3554
3555         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
3556         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
3557         NUMS=$($CMD | wc -l)
3558         [ $NUMS -eq $EXPECTED ] ||
3559                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3560 }
3561 run_test 56q "check lfs find -gid and ! -gid ==============================="
3562
3563 test_56r() {
3564         TDIR=$DIR/${tdir}r
3565         setup_56 $NUMFILES $NUMDIRS
3566
3567         EXPECTED=12
3568         CMD="$LFIND -size 0 -type f $TDIR"
3569         NUMS=$($CMD | wc -l)
3570         [ $NUMS -eq $EXPECTED ] ||
3571                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3572         EXPECTED=0
3573         CMD="$LFIND ! -size 0 -type f $TDIR"
3574         NUMS=$($CMD | wc -l)
3575         [ $NUMS -eq $EXPECTED ] ||
3576                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3577         echo "test" > $TDIR/$tfile
3578         echo "test2" > $TDIR/$tfile.2 && sync
3579         EXPECTED=1
3580         CMD="$LFIND -size 5 -type f $TDIR"
3581         NUMS=$($CMD | wc -l)
3582         [ $NUMS -eq $EXPECTED ] ||
3583                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3584         EXPECTED=1
3585         CMD="$LFIND -size +5 -type f $TDIR"
3586         NUMS=$($CMD | wc -l)
3587         [ $NUMS -eq $EXPECTED ] ||
3588                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3589         EXPECTED=2
3590         CMD="$LFIND -size +0 -type f $TDIR"
3591         NUMS=$($CMD | wc -l)
3592         [ $NUMS -eq $EXPECTED ] ||
3593                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3594         EXPECTED=2
3595         CMD="$LFIND ! -size -5 -type f $TDIR"
3596         NUMS=$($CMD | wc -l)
3597         [ $NUMS -eq $EXPECTED ] ||
3598                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3599         EXPECTED=12
3600         CMD="$LFIND -size -5 -type f $TDIR"
3601         NUMS=$($CMD | wc -l)
3602         [ $NUMS -eq $EXPECTED ] ||
3603                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3604 }
3605 run_test 56r "check lfs find -size works =========================="
3606
3607 test_56s() { # LU-611
3608         TDIR=$DIR/${tdir}s
3609         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3610
3611         if [ $OSTCOUNT -gt 1 ]; then
3612                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
3613                 ONESTRIPE=4
3614                 EXTRA=4
3615         else
3616                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
3617                 EXTRA=0
3618         fi
3619
3620         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3621         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
3622         NUMS=$($CMD | wc -l)
3623         [ $NUMS -eq $EXPECTED ] ||
3624                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3625
3626         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
3627         CMD="$LFIND -stripe-count +0 -type f $TDIR"
3628         NUMS=$($CMD | wc -l)
3629         [ $NUMS -eq $EXPECTED ] ||
3630                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3631
3632         EXPECTED=$ONESTRIPE
3633         CMD="$LFIND -stripe-count 1 -type f $TDIR"
3634         NUMS=$($CMD | wc -l)
3635         [ $NUMS -eq $EXPECTED ] ||
3636                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3637
3638         CMD="$LFIND -stripe-count -2 -type f $TDIR"
3639         NUMS=$($CMD | wc -l)
3640         [ $NUMS -eq $EXPECTED ] ||
3641                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3642
3643         EXPECTED=0
3644         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
3645         NUMS=$($CMD | wc -l)
3646         [ $NUMS -eq $EXPECTED ] ||
3647                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3648 }
3649 run_test 56s "check lfs find -stripe-count works"
3650
3651 test_56t() { # LU-611
3652         TDIR=$DIR/${tdir}t
3653         setup_56 $NUMFILES $NUMDIRS "-s 512k"
3654
3655         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
3656
3657         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3658         CMD="$LFIND -stripe-size 512k -type f $TDIR"
3659         NUMS=$($CMD | wc -l)
3660         [ $NUMS -eq $EXPECTED ] ||
3661                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3662
3663         CMD="$LFIND -stripe-size +320k -type f $TDIR"
3664         NUMS=$($CMD | wc -l)
3665         [ $NUMS -eq $EXPECTED ] ||
3666                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3667
3668         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
3669         CMD="$LFIND -stripe-size +200k -type f $TDIR"
3670         NUMS=$($CMD | wc -l)
3671         [ $NUMS -eq $EXPECTED ] ||
3672                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3673
3674         CMD="$LFIND -stripe-size -640k -type f $TDIR"
3675         NUMS=$($CMD | wc -l)
3676         [ $NUMS -eq $EXPECTED ] ||
3677                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3678
3679         EXPECTED=4
3680         CMD="$LFIND -stripe-size 256k -type f $TDIR"
3681         NUMS=$($CMD | wc -l)
3682         [ $NUMS -eq $EXPECTED ] ||
3683                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3684
3685         CMD="$LFIND -stripe-size -320k -type f $TDIR"
3686         NUMS=$($CMD | wc -l)
3687         [ $NUMS -eq $EXPECTED ] ||
3688                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3689
3690         EXPECTED=0
3691         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
3692         NUMS=$($CMD | wc -l)
3693         [ $NUMS -eq $EXPECTED ] ||
3694                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3695 }
3696 run_test 56t "check lfs find -stripe-size works"
3697
3698 test_56u() { # LU-611
3699         TDIR=$DIR/${tdir}u
3700         setup_56 $NUMFILES $NUMDIRS "-i 0"
3701
3702         if [ $OSTCOUNT -gt 1 ]; then
3703                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
3704                 ONESTRIPE=4
3705         else
3706                 ONESTRIPE=0
3707         fi
3708
3709         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3710         CMD="$LFIND -stripe-index 0 -type f $TDIR"
3711         NUMS=$($CMD | wc -l)
3712         [ $NUMS -eq $EXPECTED ] ||
3713                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3714
3715         EXPECTED=$ONESTRIPE
3716         CMD="$LFIND -stripe-index 1 -type f $TDIR"
3717         NUMS=$($CMD | wc -l)
3718         [ $NUMS -eq $EXPECTED ] ||
3719                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3720
3721         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
3722         NUMS=$($CMD | wc -l)
3723         [ $NUMS -eq $EXPECTED ] ||
3724                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3725
3726         EXPECTED=0
3727         # This should produce an error and not return any files
3728         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
3729         NUMS=$($CMD 2>/dev/null | wc -l)
3730         [ $NUMS -eq $EXPECTED ] ||
3731                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3732
3733         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
3734         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
3735         NUMS=$($CMD | wc -l)
3736         [ $NUMS -eq $EXPECTED ] ||
3737                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3738 }
3739 run_test 56u "check lfs find -stripe-index works"
3740
3741 test_56v() {
3742     local MDT_IDX=0
3743
3744     TDIR=$DIR/${tdir}v
3745     rm -rf $TDIR
3746     setup_56 $NUMFILES $NUMDIRS
3747
3748     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
3749     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
3750
3751     for file in $($LFIND -mdt $UUID $TDIR); do
3752         file_mdt_idx=$($GETSTRIPE -M $file)
3753         [ $file_mdt_idx -eq $MDT_IDX ] ||
3754             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
3755     done
3756 }
3757 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
3758
3759 test_57a() {
3760         # note test will not do anything if MDS is not local
3761         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3762         local MNTDEV="osd*.*MDT*.mntdev"
3763         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
3764         [ -z "$DEV" ] && error "can't access $MNTDEV"
3765         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
3766                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
3767                         error "can't access $DEV"
3768                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
3769                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
3770                 rm $TMP/t57a.dump
3771         done
3772 }
3773 run_test 57a "verify MDS filesystem created with large inodes =="
3774
3775 test_57b() {
3776         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3777         local dir=$DIR/d57b
3778
3779         local FILECOUNT=100
3780         local FILE1=$dir/f1
3781         local FILEN=$dir/f$FILECOUNT
3782
3783         rm -rf $dir || error "removing $dir"
3784         mkdir -p $dir || error "creating $dir"
3785         local num=$(get_mds_dir $dir)
3786         local mymds=mds$num
3787
3788         echo "mcreating $FILECOUNT files"
3789         createmany -m $dir/f 1 $FILECOUNT || \
3790                 error "creating files in $dir"
3791
3792         # verify that files do not have EAs yet
3793         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
3794         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
3795
3796         sync
3797         sleep 1
3798         df $dir  #make sure we get new statfs data
3799         local MDSFREE=$(do_facet $mymds \
3800                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
3801         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
3802         echo "opening files to create objects/EAs"
3803         local FILE
3804         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
3805                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
3806         done
3807
3808         # verify that files have EAs now
3809         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
3810         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
3811
3812         sleep 1  #make sure we get new statfs data
3813         df $dir
3814         local MDSFREE2=$(do_facet $mymds \
3815                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
3816         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
3817         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
3818                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
3819                         error "MDC before $MDCFREE != after $MDCFREE2"
3820                 else
3821                         echo "MDC before $MDCFREE != after $MDCFREE2"
3822                         echo "unable to confirm if MDS has large inodes"
3823                 fi
3824         fi
3825         rm -rf $dir
3826 }
3827 run_test 57b "default LOV EAs are stored inside large inodes ==="
3828
3829 test_58() {
3830     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
3831     wiretest
3832 }
3833 run_test 58 "verify cross-platform wire constants =============="
3834
3835 test_59() {
3836         echo "touch 130 files"
3837         createmany -o $DIR/f59- 130
3838         echo "rm 130 files"
3839         unlinkmany $DIR/f59- 130
3840         sync
3841         sleep 2
3842         # wait for commitment of removal
3843 }
3844 run_test 59 "verify cancellation of llog records async ========="
3845
3846 TEST60_HEAD="test_60 run $RANDOM"
3847 test_60a() {
3848         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
3849         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
3850         log "$TEST60_HEAD - from kernel mode"
3851         do_facet mgs sh run-llog.sh
3852 }
3853 run_test 60a "llog sanity tests run from kernel module =========="
3854
3855 test_60b() { # bug 6411
3856         dmesg > $DIR/$tfile
3857         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
3858                                  /llog.test/ {
3859                                          if (marker)
3860                                                  from_marker++
3861                                          from_begin++
3862                                  }
3863                                  END {
3864                                          if (marker)
3865                                                  print from_marker
3866                                          else
3867                                                  print from_begin
3868                                  }"`
3869         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
3870 }
3871 run_test 60b "limit repeated messages from CERROR/CWARN ========"
3872
3873 test_60c() {
3874         echo "create 5000 files"
3875         createmany -o $DIR/f60c- 5000
3876 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
3877         lctl set_param fail_loc=0x80000137
3878         unlinkmany $DIR/f60c- 5000
3879         lctl set_param fail_loc=0
3880 }
3881 run_test 60c "unlink file when mds full"
3882
3883 test_60d() {
3884         SAVEPRINTK=$(lctl get_param -n printk)
3885
3886         # verify "lctl mark" is even working"
3887         MESSAGE="test message ID $RANDOM $$"
3888         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
3889         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
3890
3891         lctl set_param printk=0 || error "set lnet.printk failed"
3892         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
3893         MESSAGE="new test message ID $RANDOM $$"
3894         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
3895         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
3896         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
3897
3898         lctl set_param -n printk="$SAVEPRINTK"
3899 }
3900 run_test 60d "test printk console message masking"
3901
3902 test_61() {
3903         f="$DIR/f61"
3904         dd if=/dev/zero of=$f bs=`page_size` count=1
3905         cancel_lru_locks osc
3906         multiop $f OSMWUc || error
3907         sync
3908 }
3909 run_test 61 "mmap() writes don't make sync hang ================"
3910
3911 # bug 2330 - insufficient obd_match error checking causes LBUG
3912 test_62() {
3913         f="$DIR/f62"
3914         echo foo > $f
3915         cancel_lru_locks osc
3916         lctl set_param fail_loc=0x405
3917         cat $f && error "cat succeeded, expect -EIO"
3918         lctl set_param fail_loc=0
3919 }
3920 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
3921 # match every page all of the time.
3922 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
3923
3924 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
3925 test_63a() {    # was test_63
3926         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
3927         lctl set_param -n osc.*.max_dirty_mb 0
3928         for i in `seq 10` ; do
3929                 dd if=/dev/zero of=$DIR/f63 bs=8k &
3930                 sleep 5
3931                 kill $!
3932                 sleep 1
3933         done
3934
3935         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
3936         rm -f $DIR/f63 || true
3937 }
3938 run_test 63a "Verify oig_wait interruption does not crash ======="
3939
3940 # bug 2248 - async write errors didn't return to application on sync
3941 # bug 3677 - async write errors left page locked
3942 test_63b() {
3943         debugsave
3944         lctl set_param debug=-1
3945
3946         # ensure we have a grant to do async writes
3947         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
3948         rm $DIR/$tfile
3949
3950         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
3951         lctl set_param fail_loc=0x80000406
3952         multiop $DIR/$tfile Owy && \
3953                 error "sync didn't return ENOMEM"
3954         sync; sleep 2; sync     # do a real sync this time to flush page
3955         lctl get_param -n llite.*.dump_page_cache | grep locked && \
3956                 error "locked page left in cache after async error" || true
3957         debugrestore
3958 }
3959 run_test 63b "async write errors should be returned to fsync ==="
3960
3961 test_64a () {
3962         df $DIR
3963         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
3964 }
3965 run_test 64a "verify filter grant calculations (in kernel) ====="
3966
3967 test_64b () {
3968         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
3969         sh oos.sh $MOUNT
3970 }
3971 run_test 64b "check out-of-space detection on client ==========="
3972
3973 # bug 1414 - set/get directories' stripe info
3974 test_65a() {
3975         mkdir -p $DIR/d65
3976         touch $DIR/d65/f1
3977         $LVERIFY $DIR/d65 $DIR/d65/f1 || error "lverify failed"
3978 }
3979 run_test 65a "directory with no stripe info ===================="
3980
3981 test_65b() {
3982         mkdir -p $DIR/d65
3983         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65||error "setstripe"
3984         touch $DIR/d65/f2
3985         $LVERIFY $DIR/d65 $DIR/d65/f2 || error "lverify failed"
3986 }
3987 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
3988
3989 test_65c() {
3990         if [ $OSTCOUNT -gt 1 ]; then
3991                 mkdir -p $DIR/d65
3992                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
3993                         -c $(($OSTCOUNT - 1)) $DIR/d65 || error "setstripe"
3994                 touch $DIR/d65/f3
3995                 $LVERIFY $DIR/d65 $DIR/d65/f3 || error "lverify failed"
3996         fi
3997 }
3998 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
3999
4000 test_65d() {
4001         mkdir -p $DIR/d65
4002         if [ $STRIPECOUNT -le 0 ]; then
4003                 sc=1
4004         elif [ $STRIPECOUNT -gt 2000 ]; then
4005 #LOV_MAX_STRIPE_COUNT is 2000
4006                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4007         else
4008                 sc=$(($STRIPECOUNT - 1))
4009         fi
4010         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/d65 || error "setstripe"
4011         touch $DIR/d65/f4 $DIR/d65/f5
4012         $LVERIFY $DIR/d65 $DIR/d65/f4 $DIR/d65/f5 || error "lverify failed"
4013 }
4014 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4015
4016 test_65e() {
4017         mkdir -p $DIR/d65
4018
4019         $SETSTRIPE $DIR/d65 || error "setstripe"
4020         $GETSTRIPE -v $DIR/d65 | grep "Default" || error "no stripe info failed"
4021         touch $DIR/d65/f6
4022         $LVERIFY $DIR/d65 $DIR/d65/f6 || error "lverify failed"
4023 }
4024 run_test 65e "directory setstripe defaults ======================="
4025
4026 test_65f() {
4027         mkdir -p $DIR/d65f
4028         $RUNAS $SETSTRIPE $DIR/d65f && error "setstripe succeeded" || true
4029 }
4030 run_test 65f "dir setstripe permission (should return error) ==="
4031
4032 test_65g() {
4033         mkdir -p $DIR/d65
4034         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4035         $SETSTRIPE -d $DIR/d65 || error "setstripe"
4036         $GETSTRIPE -v $DIR/d65 | grep "Default" || \
4037                 error "delete default stripe failed"
4038 }
4039 run_test 65g "directory setstripe -d ==========================="
4040
4041 test_65h() {
4042         mkdir -p $DIR/d65
4043         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4044         mkdir -p $DIR/d65/dd1
4045         [ $($GETSTRIPE -c $DIR/d65) == $($GETSTRIPE -c $DIR/d65/dd1) ] ||
4046                 error "stripe info inherit failed"
4047 }
4048 run_test 65h "directory stripe info inherit ===================="
4049
4050 test_65i() { # bug6367
4051         $SETSTRIPE -S 65536 -c -1 $MOUNT
4052 }
4053 run_test 65i "set non-default striping on root directory (bug 6367)="
4054
4055 test_65ia() { # bug12836
4056         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4057 }
4058 run_test 65ia "getstripe on -1 default directory striping"
4059
4060 test_65ib() { # bug12836
4061         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4062 }
4063 run_test 65ib "getstripe -v on -1 default directory striping"
4064
4065 test_65ic() { # bug12836
4066         $LFS find -mtime -1 $MOUNT || error "find $MOUNT failed"
4067 }
4068 run_test 65ic "new find on -1 default directory striping"
4069
4070 test_65j() { # bug6367
4071         sync; sleep 1
4072         # if we aren't already remounting for each test, do so for this test
4073         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4074                 cleanup || error "failed to unmount"
4075                 setup
4076         fi
4077         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4078 }
4079 run_test 65j "set default striping on root directory (bug 6367)="
4080
4081 test_65k() { # bug11679
4082     [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4083     remote_mds_nodsh && skip "remote MDS with nodsh" && return
4084
4085     echo "Check OST status: "
4086     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4087               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4088
4089     for OSC in $MDS_OSCS; do
4090         echo $OSC "is activate"
4091         do_facet $SINGLEMDS lctl --device %$OSC activate
4092     done
4093
4094     do_facet client mkdir -p $DIR/$tdir
4095     for INACTIVE_OSC in $MDS_OSCS; do
4096         echo "Deactivate: " $INACTIVE_OSC
4097         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4098         for STRIPE_OSC in $MDS_OSCS; do
4099             OST=`osc_to_ost $STRIPE_OSC`
4100             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4101                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4102
4103             [ -f $DIR/$tdir/$IDX ] && continue
4104             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4105             do_facet client $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4106             RC=$?
4107             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4108         done
4109         do_facet client rm -f $DIR/$tdir/*
4110         echo $INACTIVE_OSC "is Activate."
4111         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4112     done
4113 }
4114 run_test 65k "validate manual striping works properly with deactivated OSCs"
4115
4116 test_65l() { # bug 12836
4117         mkdir -p $DIR/$tdir/test_dir
4118         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4119         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4120 }
4121 run_test 65l "lfs find on -1 stripe dir ========================"
4122
4123 # bug 2543 - update blocks count on client
4124 test_66() {
4125         COUNT=${COUNT:-8}
4126         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4127         sync; sleep 1; sync
4128         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4129         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4130 }
4131 run_test 66 "update inode blocks count on client ==============="
4132
4133 LLOOP=
4134 LLITELOOPLOAD=
4135 cleanup_68() {
4136         trap 0
4137         if [ ! -z "$LLOOP" ]; then
4138                 if swapon -s | grep -q $LLOOP; then
4139                         swapoff $LLOOP || error "swapoff failed"
4140                 fi
4141
4142                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4143                 rm -f $LLOOP
4144                 unset LLOOP
4145         fi
4146         if [ ! -z "$LLITELOOPLOAD" ]; then
4147                 rmmod llite_lloop
4148                 unset LLITELOOPLOAD
4149         fi
4150         rm -f $DIR/f68*
4151 }
4152
4153 meminfo() {
4154         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4155 }
4156
4157 swap_used() {
4158         swapon -s | awk '($1 == "'$1'") { print $4 }'
4159 }
4160
4161 # test case for lloop driver, basic function
4162 test_68a() {
4163         [ "$UID" != 0 ] && skip_env "must run as root" && return
4164         llite_lloop_enabled || \
4165                 { skip_env "llite_lloop module disabled" && return; }
4166
4167         trap cleanup_68 EXIT
4168
4169         if ! module_loaded llite_lloop; then
4170                 if load_module llite/llite_lloop; then
4171                         LLITELOOPLOAD=yes
4172                 else
4173                         skip_env "can't find module llite_lloop"
4174                         return
4175                 fi
4176         fi
4177
4178         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4179         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4180         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4181
4182         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4183         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4184
4185         cleanup_68
4186 }
4187 run_test 68a "lloop driver - basic test ========================"
4188
4189 # excercise swapping to lustre by adding a high priority swapfile entry
4190 # and then consuming memory until it is used.
4191 test_68b() {  # was test_68
4192         [ "$UID" != 0 ] && skip_env "must run as root" && return
4193         lctl get_param -n devices | grep -q obdfilter && \
4194                 skip "local OST" && return
4195
4196         grep -q llite_lloop /proc/modules
4197         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4198
4199         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4200                 skip "can't reliably test swap with TCP" && return
4201
4202         MEMTOTAL=`meminfo MemTotal`
4203         NR_BLOCKS=$((MEMTOTAL>>8))
4204         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4205
4206         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4207         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4208         mkswap $DIR/f68b
4209
4210         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4211
4212         trap cleanup_68 EXIT
4213
4214         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4215
4216         echo "before: `swapon -s | grep $LLOOP`"
4217         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4218         echo "after: `swapon -s | grep $LLOOP`"
4219         SWAPUSED=`swap_used $LLOOP`
4220
4221         cleanup_68
4222
4223         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4224 }
4225 run_test 68b "support swapping to Lustre ========================"
4226
4227 # bug5265, obdfilter oa2dentry return -ENOENT
4228 # #define OBD_FAIL_OST_ENOENT 0x217
4229 test_69() {
4230         remote_ost_nodsh && skip "remote OST with nodsh" && return
4231
4232         f="$DIR/$tfile"
4233         $SETSTRIPE -c 1 -i 0 $f
4234
4235         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4236
4237         do_facet ost1 lctl set_param fail_loc=0x217
4238         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4239         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4240
4241         do_facet ost1 lctl set_param fail_loc=0
4242         $DIRECTIO write $f 0 2 || error "write error"
4243
4244         cancel_lru_locks osc
4245         $DIRECTIO read $f 0 1 || error "read error"
4246
4247         do_facet ost1 lctl set_param fail_loc=0x217
4248         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4249
4250         do_facet ost1 lctl set_param fail_loc=0
4251         rm -f $f
4252 }
4253 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4254
4255 test_71() {
4256     mkdir -p $DIR/$tdir
4257     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4258 }
4259 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4260
4261 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4262         check_kernel_version 43 || return 0
4263         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4264
4265         # Check that testing environment is properly set up. Skip if not
4266         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4267                 skip_env "User $RUNAS_ID does not exist - skipping"
4268                 return 0
4269         }
4270         # We had better clear the $DIR to get enough space for dd
4271         rm -rf $DIR/*
4272         touch $DIR/f72
4273         chmod 777 $DIR/f72
4274         chmod ug+s $DIR/f72
4275         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4276         # See if we are still setuid/sgid
4277         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4278         # Now test that MDS is updated too
4279         cancel_lru_locks mdc
4280         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4281         true
4282         rm -f $DIR/f72
4283 }
4284 run_test 72a "Test that remove suid works properly (bug5695) ===="
4285
4286 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4287         local perm
4288
4289         [ "$RUNAS_ID" = "$UID" ] && \
4290                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4291         [ "$RUNAS_ID" -eq 0 ] && \
4292                 skip_env "RUNAS_ID = 0 -- skipping" && return
4293
4294         # Check that testing environment is properly set up. Skip if not
4295         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4296                 skip_env "User $RUNAS_ID does not exist - skipping"
4297                 return 0
4298         }
4299         touch $DIR/${tfile}-f{g,u}
4300         mkdir $DIR/${tfile}-d{g,u}
4301         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4302         chmod g+s $DIR/${tfile}-{f,d}g
4303         chmod u+s $DIR/${tfile}-{f,d}u
4304         for perm in 777 2777 4777; do
4305                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4306                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4307                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4308                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4309         done
4310         true
4311 }
4312 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4313
4314 # bug 3462 - multiple simultaneous MDC requests
4315 test_73() {
4316         mkdir $DIR/d73-1
4317         mkdir $DIR/d73-2
4318         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4319         pid1=$!
4320
4321         lctl set_param fail_loc=0x80000129
4322         multiop $DIR/d73-1/f73-2 Oc &
4323         sleep 1
4324         lctl set_param fail_loc=0
4325
4326         multiop $DIR/d73-2/f73-3 Oc &
4327         pid3=$!
4328
4329         kill -USR1 $pid1
4330         wait $pid1 || return 1
4331
4332         sleep 25
4333
4334         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4335         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4336         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4337
4338         rm -rf $DIR/d73-*
4339 }
4340 run_test 73 "multiple MDC requests (should not deadlock)"
4341
4342 test_74a() { # bug 6149, 6184
4343         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4344         #
4345         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4346         # will spin in a tight reconnection loop
4347         touch $DIR/f74a
4348         lctl set_param fail_loc=0x8000030e
4349         # get any lock that won't be difficult - lookup works.
4350         ls $DIR/f74a
4351         lctl set_param fail_loc=0
4352         true
4353         rm -f $DIR/f74a
4354 }
4355 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4356
4357 test_74b() { # bug 13310
4358         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4359         #
4360         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4361         # will spin in a tight reconnection loop
4362         lctl set_param fail_loc=0x8000030e
4363         # get a "difficult" lock
4364         touch $DIR/f74b
4365         lctl set_param fail_loc=0
4366         true
4367         rm -f $DIR/f74b
4368 }
4369 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4370
4371 test_74c() {
4372 #define OBD_FAIL_LDLM_NEW_LOCK
4373         lctl set_param fail_loc=0x80000319
4374         touch $DIR/$tfile && error "Touch successful"
4375         true
4376 }
4377 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4378
4379 num_inodes() {
4380         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4381 }
4382
4383 get_inode_slab_tunables() {
4384         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4385 }
4386
4387 set_inode_slab_tunables() {
4388         echo "lustre_inode_cache $1" > /proc/slabinfo
4389 }
4390
4391 test_76() { # Now for bug 20433, added originally in bug 1443
4392         local SLAB_SETTINGS=`get_inode_slab_tunables`
4393         local CPUS=`getconf _NPROCESSORS_ONLN`
4394         # we cannot set limit below 1 which means 1 inode in each
4395         # per-cpu cache is still allowed
4396         set_inode_slab_tunables "1 1 0"
4397         cancel_lru_locks osc
4398         BEFORE_INODES=`num_inodes`
4399         echo "before inodes: $BEFORE_INODES"
4400         local COUNT=1000
4401         [ "$SLOW" = "no" ] && COUNT=100
4402         for i in `seq $COUNT`; do
4403                 touch $DIR/$tfile
4404                 rm -f $DIR/$tfile
4405         done
4406         cancel_lru_locks osc
4407         AFTER_INODES=`num_inodes`
4408         echo "after inodes: $AFTER_INODES"
4409         local wait=0
4410         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
4411                 sleep 2
4412                 AFTER_INODES=`num_inodes`
4413                 wait=$((wait+2))
4414                 echo "wait $wait seconds inodes: $AFTER_INODES"
4415                 if [ $wait -gt 30 ]; then
4416                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
4417                 fi
4418         done
4419         set_inode_slab_tunables "$SLAB_SETTINGS"
4420 }
4421 run_test 76 "confirm clients recycle inodes properly ===="
4422
4423
4424 export ORIG_CSUM=""
4425 set_checksums()
4426 {
4427         # Note: in sptlrpc modes which enable its own bulk checksum, the
4428         # original crc32_le bulk checksum will be automatically disabled,
4429         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
4430         # will be checked by sptlrpc code against sptlrpc bulk checksum.
4431         # In this case set_checksums() will not be no-op, because sptlrpc
4432         # bulk checksum will be enabled all through the test.
4433
4434         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
4435         lctl set_param -n osc.*.checksums $1
4436         return 0
4437 }
4438
4439 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
4440                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
4441 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
4442 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
4443 set_checksum_type()
4444 {
4445         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
4446         log "set checksum type to $1"
4447         return 0
4448 }
4449 F77_TMP=$TMP/f77-temp
4450 F77SZ=8
4451 setup_f77() {
4452         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
4453                 error "error writing to $F77_TMP"
4454 }
4455
4456 test_77a() { # bug 10889
4457         $GSS && skip "could not run with gss" && return
4458         [ ! -f $F77_TMP ] && setup_f77
4459         set_checksums 1
4460         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
4461         set_checksums 0
4462         rm -f $DIR/$tfile
4463 }
4464 run_test 77a "normal checksum read/write operation ============="
4465
4466 test_77b() { # bug 10889
4467         $GSS && skip "could not run with gss" && return
4468         [ ! -f $F77_TMP ] && setup_f77
4469         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4470         lctl set_param fail_loc=0x80000409
4471         set_checksums 1
4472         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
4473                 error "dd error: $?"
4474         lctl set_param fail_loc=0
4475         set_checksums 0
4476 }
4477 run_test 77b "checksum error on client write ===================="
4478
4479 test_77c() { # bug 10889
4480         $GSS && skip "could not run with gss" && return
4481         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
4482         set_checksums 1
4483         for algo in $CKSUM_TYPES; do
4484                 cancel_lru_locks osc
4485                 set_checksum_type $algo
4486                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4487                 lctl set_param fail_loc=0x80000408
4488                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
4489                 lctl set_param fail_loc=0
4490         done
4491         set_checksums 0
4492         set_checksum_type $ORIG_CSUM_TYPE
4493         rm -f $DIR/f77b
4494 }
4495 run_test 77c "checksum error on client read ==================="
4496
4497 test_77d() { # bug 10889
4498         $GSS && skip "could not run with gss" && return
4499         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4500         lctl set_param fail_loc=0x80000409
4501         set_checksums 1
4502         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4503                 error "direct write: rc=$?"
4504         lctl set_param fail_loc=0
4505         set_checksums 0
4506 }
4507 run_test 77d "checksum error on OST direct write ==============="
4508
4509 test_77e() { # bug 10889
4510         $GSS && skip "could not run with gss" && return
4511         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
4512         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4513         lctl set_param fail_loc=0x80000408
4514         set_checksums 1
4515         cancel_lru_locks osc
4516         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4517                 error "direct read: rc=$?"
4518         lctl set_param fail_loc=0
4519         set_checksums 0
4520 }
4521 run_test 77e "checksum error on OST direct read ================"
4522
4523 test_77f() { # bug 10889
4524         $GSS && skip "could not run with gss" && return
4525         set_checksums 1
4526         for algo in $CKSUM_TYPES; do
4527                 cancel_lru_locks osc
4528                 set_checksum_type $algo
4529                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4530                 lctl set_param fail_loc=0x409
4531                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
4532                         error "direct write succeeded"
4533                 lctl set_param fail_loc=0
4534         done
4535         set_checksum_type $ORIG_CSUM_TYPE
4536         set_checksums 0
4537 }
4538 run_test 77f "repeat checksum error on write (expect error) ===="
4539
4540 test_77g() { # bug 10889
4541         $GSS && skip "could not run with gss" && return
4542         remote_ost_nodsh && skip "remote OST with nodsh" && return
4543
4544         [ ! -f $F77_TMP ] && setup_f77
4545
4546         $SETSTRIPE -c 1 -i 0 $DIR/f77g
4547         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
4548         do_facet ost1 lctl set_param fail_loc=0x8000021a
4549         set_checksums 1
4550         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
4551                 error "write error: rc=$?"
4552         do_facet ost1 lctl set_param fail_loc=0
4553         set_checksums 0
4554 }
4555 run_test 77g "checksum error on OST write ======================"
4556
4557 test_77h() { # bug 10889
4558         $GSS && skip "could not run with gss" && return
4559         remote_ost_nodsh && skip "remote OST with nodsh" && return
4560
4561         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
4562         cancel_lru_locks osc
4563         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
4564         do_facet ost1 lctl set_param fail_loc=0x8000021b
4565         set_checksums 1
4566         cmp $F77_TMP $DIR/f77g || error "file compare failed"
4567         do_facet ost1 lctl set_param fail_loc=0
4568         set_checksums 0
4569 }
4570 run_test 77h "checksum error on OST read ======================="
4571
4572 test_77i() { # bug 13805
4573         $GSS && skip "could not run with gss" && return
4574         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
4575         lctl set_param fail_loc=0x40b
4576         remount_client $MOUNT
4577         lctl set_param fail_loc=0
4578         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4579                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4580                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4581                 [ "$algo" = "crc32" ] || error "algo set to $algo instead of crc32"
4582         done
4583         remount_client $MOUNT
4584 }
4585 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
4586
4587 test_77j() { # bug 13805
4588         $GSS && skip "could not run with gss" && return
4589         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
4590         lctl set_param fail_loc=0x40c
4591         remount_client $MOUNT
4592         lctl set_param fail_loc=0
4593         sleep 2 # wait async osc connect to finish
4594         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4595                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4596                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4597                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4598         done
4599         remount_client $MOUNT
4600 }
4601 run_test 77j "client only supporting ADLER32 ===================="
4602
4603 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
4604 rm -f $F77_TMP
4605 unset F77_TMP
4606
4607 test_78() { # bug 10901
4608         remote_ost || { skip_env "local OST" && return; }
4609
4610         NSEQ=5
4611         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
4612         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
4613         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
4614         echo "MemTotal: $MEMTOTAL"
4615 # reserve 256MB of memory for the kernel and other running processes,
4616 # and then take 1/2 of the remaining memory for the read/write buffers.
4617     if [ $MEMTOTAL -gt 512 ] ;then
4618         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
4619     else
4620         # for those poor memory-starved high-end clusters...
4621         MEMTOTAL=$((MEMTOTAL / 2))
4622     fi
4623         echo "Mem to use for directio: $MEMTOTAL"
4624         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
4625         [ $F78SIZE -gt 512 ] && F78SIZE=512
4626         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
4627         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
4628         echo "Smallest OST: $SMALLESTOST"
4629         [ $SMALLESTOST -lt 10240 ] && \
4630                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
4631
4632         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
4633                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
4634
4635         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
4636         echo "File size: $F78SIZE"
4637         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
4638         for i in `seq 1 $NSEQ`
4639         do
4640                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
4641                 echo directIO rdwr round $i of $NSEQ
4642                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
4643         done
4644
4645         rm -f $DIR/$tfile
4646 }
4647 run_test 78 "handle large O_DIRECT writes correctly ============"
4648
4649 test_79() { # bug 12743
4650         wait_delete_completed
4651
4652         BKTOTAL=$(calc_osc_kbytes kbytestotal)
4653         BKFREE=$(calc_osc_kbytes kbytesfree)
4654         BKAVAIL=$(calc_osc_kbytes kbytesavail)
4655
4656         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
4657         DFTOTAL=`echo $STRING | cut -d, -f1`
4658         DFUSED=`echo $STRING  | cut -d, -f2`
4659         DFAVAIL=`echo $STRING | cut -d, -f3`
4660         DFFREE=$(($DFTOTAL - $DFUSED))
4661
4662         ALLOWANCE=$((64 * $OSTCOUNT))
4663
4664         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
4665            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
4666                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
4667         fi
4668         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
4669            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
4670                 error "df free($DFFREE) mismatch OST free($BKFREE)"
4671         fi
4672         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
4673            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
4674                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
4675         fi
4676 }
4677 run_test 79 "df report consistency check ======================="
4678
4679 test_80() { # bug 10718
4680         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
4681         sync; sleep 1; sync
4682         local BEFORE=`date +%s`
4683         cancel_lru_locks osc
4684         local AFTER=`date +%s`
4685         local DIFF=$((AFTER-BEFORE))
4686         if [ $DIFF -gt 1 ] ; then
4687                 error "elapsed for 1M@1T = $DIFF"
4688         fi
4689         true
4690         rm -f $DIR/$tfile
4691 }
4692 run_test 80 "Page eviction is equally fast at high offsets too  ===="
4693
4694 test_81a() { # LU-456
4695         remote_ost_nodsh && skip "remote OST with nodsh" && return
4696         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4697         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
4698         do_facet ost0 lctl set_param fail_loc=0x80000228
4699
4700         # write should trigger a retry and success
4701         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4702         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4703         RC=$?
4704         if [ $RC -ne 0 ] ; then
4705                 error "write should success, but failed for $RC"
4706         fi
4707 }
4708 run_test 81a "OST should retry write when get -ENOSPC ==============="
4709
4710 test_81b() { # LU-456
4711         remote_ost_nodsh && skip "remote OST with nodsh" && return
4712         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4713         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
4714         do_facet ost0 lctl set_param fail_loc=0x228
4715
4716         # write should retry several times and return -ENOSPC finally
4717         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4718         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4719         RC=$?
4720         ENOSPC=28
4721         if [ $RC -ne $ENOSPC ] ; then
4722                 error "dd should fail for -ENOSPC, but succeed."
4723         fi
4724 }
4725 run_test 81b "OST should return -ENOSPC when retry still fails ======="
4726
4727
4728 test_99a() {
4729         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
4730             return
4731         mkdir -p $DIR/d99cvsroot
4732         chown $RUNAS_ID $DIR/d99cvsroot
4733         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
4734         cd $TMP
4735
4736         $RUNAS cvs -d $DIR/d99cvsroot init || error
4737         cd $oldPWD
4738 }
4739 run_test 99a "cvs init ========================================="
4740
4741 test_99b() {
4742         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4743         [ ! -d $DIR/d99cvsroot ] && test_99a
4744         cd /etc/init.d
4745         # some versions of cvs import exit(1) when asked to import links or
4746         # files they can't read.  ignore those files.
4747         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
4748                         ! -perm +4 -printf '-I %f\n')
4749         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
4750                 d99reposname vtag rtag
4751 }
4752 run_test 99b "cvs import ======================================="
4753
4754 test_99c() {
4755         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4756         [ ! -d $DIR/d99cvsroot ] && test_99b
4757         cd $DIR
4758         mkdir -p $DIR/d99reposname
4759         chown $RUNAS_ID $DIR/d99reposname
4760         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
4761 }
4762 run_test 99c "cvs checkout ====================================="
4763
4764 test_99d() {
4765         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4766         [ ! -d $DIR/d99cvsroot ] && test_99c
4767         cd $DIR/d99reposname
4768         $RUNAS touch foo99
4769         $RUNAS cvs add -m 'addmsg' foo99
4770 }
4771 run_test 99d "cvs add =========================================="
4772
4773 test_99e() {
4774         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4775         [ ! -d $DIR/d99cvsroot ] && test_99c
4776         cd $DIR/d99reposname
4777         $RUNAS cvs update
4778 }
4779 run_test 99e "cvs update ======================================="
4780
4781 test_99f() {
4782         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4783         [ ! -d $DIR/d99cvsroot ] && test_99d
4784         cd $DIR/d99reposname
4785         $RUNAS cvs commit -m 'nomsg' foo99
4786     rm -fr $DIR/d99cvsroot
4787 }
4788 run_test 99f "cvs commit ======================================="
4789
4790 test_100() {
4791         [ "$NETTYPE" = tcp ] || \
4792                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
4793                         return ; }
4794
4795         remote_ost_nodsh && skip "remote OST with nodsh" && return
4796         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4797         remote_servers || \
4798                 { skip "useless for local single node setup" && return; }
4799
4800         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
4801                 [ "$PROT" != "tcp" ] && continue
4802                 RPORT=$(echo $REMOTE | cut -d: -f2)
4803                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
4804
4805                 rc=0
4806                 LPORT=`echo $LOCAL | cut -d: -f2`
4807                 if [ $LPORT -ge 1024 ]; then
4808                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
4809                         netstat -tna
4810                         error_exit "local: $LPORT > 1024, remote: $RPORT"
4811                 fi
4812         done
4813         [ "$rc" = 0 ] || error_exit "privileged port not found" )
4814 }
4815 run_test 100 "check local port using privileged port ==========="
4816
4817 function get_named_value()
4818 {
4819     local tag
4820
4821     tag=$1
4822     while read ;do
4823         line=$REPLY
4824         case $line in
4825         $tag*)
4826             echo $line | sed "s/^$tag[ ]*//"
4827             break
4828             ;;
4829         esac
4830     done
4831 }
4832
4833 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
4834 cleanup_101a() {
4835         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
4836         trap 0
4837 }
4838
4839 test_101a() {
4840         local s
4841         local discard
4842         local nreads=10000
4843         [ "$CPU" = "UML" ] && nreads=1000
4844         local cache_limit=32
4845
4846         $LCTL set_param -n osc.*-osc*.rpc_stats 0
4847         trap cleanup_101a EXIT
4848         $LCTL set_param -n llite.*.read_ahead_stats 0
4849         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
4850
4851         #
4852         # randomly read 10000 of 64K chunks from file 3x 32MB in size
4853         #
4854         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
4855         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
4856
4857         discard=0
4858         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
4859                 get_named_value 'read but discarded' | cut -d" " -f1`; do
4860                         discard=$(($discard + $s))
4861         done
4862         cleanup_101a
4863
4864         if [ $(($discard * 10)) -gt $nreads ] ;then
4865                 $LCTL get_param osc.*-osc*.rpc_stats
4866                 $LCTL get_param llite.*.read_ahead_stats
4867                 error "too many ($discard) discarded pages"
4868         fi
4869         rm -f $DIR/$tfile || true
4870 }
4871 run_test 101a "check read-ahead for random reads ================"
4872
4873 setup_test101bc() {
4874         mkdir -p $DIR/$tdir
4875         STRIPE_SIZE=1048576
4876         STRIPE_COUNT=$OSTCOUNT
4877         STRIPE_OFFSET=0
4878
4879         local list=$(comma_list $(osts_nodes))
4880         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=0
4881         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=0
4882
4883         trap cleanup_test101bc EXIT
4884         # prepare the read-ahead file
4885         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
4886
4887         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
4888 }
4889
4890 cleanup_test101bc() {
4891         trap 0
4892         rm -rf $DIR/$tdir
4893         rm -f $DIR/$tfile
4894
4895         local list=$(comma_list $(osts_nodes))
4896         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
4897         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=1
4898 }
4899
4900 calc_total() {
4901         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
4902 }
4903
4904 ra_check_101() {
4905         local READ_SIZE=$1
4906         local STRIPE_SIZE=1048576
4907         local RA_INC=1048576
4908         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
4909         local FILE_LENGTH=$((64*100))
4910         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
4911                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
4912         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
4913                         get_named_value 'read but discarded' | \
4914                         cut -d" " -f1 | calc_total`
4915         if [ $DISCARD -gt $discard_limit ]; then
4916                 $LCTL get_param llite.*.read_ahead_stats
4917                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
4918         else
4919                 echo "Read-ahead success for size ${READ_SIZE}"
4920         fi
4921 }
4922
4923 test_101b() {
4924         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
4925         local STRIPE_SIZE=1048576
4926         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
4927         local FILE_LENGTH=$((STRIPE_SIZE*100))
4928         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
4929         # prepare the read-ahead file
4930         setup_test101bc
4931         cancel_lru_locks osc
4932         for BIDX in 2 4 8 16 32 64 128 256
4933         do
4934                 local BSIZE=$((BIDX*4096))
4935                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
4936                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
4937                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
4938                 $LCTL set_param -n llite.*.read_ahead_stats 0
4939                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
4940                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
4941                 cancel_lru_locks osc
4942                 ra_check_101 $BSIZE
4943         done
4944         cleanup_test101bc
4945         true
4946 }
4947 run_test 101b "check stride-io mode read-ahead ================="
4948
4949 test_101c() {
4950     local STRIPE_SIZE=1048576
4951     local FILE_LENGTH=$((STRIPE_SIZE*100))
4952     local nreads=10000
4953     local osc
4954
4955     setup_test101bc
4956
4957     cancel_lru_locks osc
4958     $LCTL set_param osc.*.rpc_stats 0
4959     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
4960     for osc in $($LCTL get_param -N osc.*); do
4961         if [ "$osc" == "osc.num_refs" ]; then
4962             continue
4963         fi
4964
4965         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
4966         if [ $lines -le 20 ]; then
4967             continue
4968         fi
4969
4970         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
4971                                      awk '$1 == "1:" { print $2; exit; }')
4972         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
4973                                      awk '$1 == "2:" { print $2; exit; }')
4974         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
4975                                      awk '$1 == "4:" { print $2; exit; }')
4976         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
4977                                      awk '$1 == "8:" { print $2; exit; }')
4978
4979         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
4980         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
4981         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
4982         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
4983         echo "${osc} rpc check passed!"
4984     done
4985     cleanup_test101bc
4986     true
4987 }
4988 run_test 101c "check stripe_size aligned read-ahead ================="
4989
4990 set_read_ahead() {
4991    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
4992    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
4993 }
4994
4995 test_101d() {
4996     local file=$DIR/$tfile
4997     local size=${FILESIZE_101c:-500}
4998     local ra_MB=${READAHEAD_MB:-40}
4999
5000     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5001     [ $space -gt $((size / 1024)) ] ||
5002         { skip "Need free space ${size}M, have $space" && return; }
5003
5004     echo Creating ${size}M test file $file
5005     dd if=/dev/zero of=$file bs=1M count=$size
5006     echo Cancel LRU locks on lustre client to flush the client cache
5007     cancel_lru_locks osc
5008
5009     echo Disable read-ahead
5010     local old_READAHEAD=$(set_read_ahead 0)
5011
5012     echo Reading the test file $file with read-ahead disabled
5013     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5014
5015     echo Cancel LRU locks on lustre client to flush the client cache
5016     cancel_lru_locks osc
5017     echo Enable read-ahead with ${ra_MB}MB
5018     set_read_ahead $ra_MB
5019
5020     echo Reading the test file $file with read-ahead enabled
5021     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5022
5023     echo read-ahead disabled time read $time_ra_OFF
5024     echo read-ahead enabled  time read $time_ra_ON
5025
5026     set_read_ahead $old_READAHEAD
5027     rm -f $file
5028
5029     [ $time_ra_ON -lt $time_ra_OFF ] ||
5030         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5031                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5032 }
5033 run_test 101d "file read with and without read-ahead enabled  ================="
5034
5035 test_101e() {
5036     local file=$DIR/$tfile
5037     local size=500  #KB
5038     local count=100
5039     local blksize=1024
5040
5041     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5042     local need_space=$((count * size))
5043     [ $space -gt $need_space ] ||
5044         { skip_env "Need free space $need_space, have $space" && return; }
5045
5046     echo Creating $count ${size}K test files
5047     for ((i = 0; i < $count; i++)); do
5048         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5049     done
5050
5051     echo Cancel LRU locks on lustre client to flush the client cache
5052     cancel_lru_locks osc
5053
5054     echo Reset readahead stats
5055     $LCTL set_param -n llite.*.read_ahead_stats 0
5056
5057     for ((i = 0; i < $count; i++)); do
5058         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5059     done
5060
5061     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5062           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5063
5064     for ((i = 0; i < $count; i++)); do
5065         rm -rf ${file}_${i} 2>/dev/null
5066     done
5067
5068     #10000 means 20% reads are missing in readahead
5069     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5070 }
5071 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5072
5073 cleanup_test101f() {
5074     trap 0
5075     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5076     rm -rf $DIR/$tfile 2>/dev/null
5077 }
5078
5079 test_101f() {
5080     local file=$DIR/$tfile
5081     local nreads=1000
5082
5083     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5084     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5085     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5086     trap cleanup_test101f EXIT
5087
5088     echo Cancel LRU locks on lustre client to flush the client cache
5089     cancel_lru_locks osc
5090
5091     echo Reset readahead stats
5092     $LCTL set_param -n llite.*.read_ahead_stats 0
5093     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5094     # readahead should read in 2M file on second read, so only miss
5095     # 2 pages.
5096     echo Random 4K reads on 2M file for 1000 times
5097     $READS -f $file -s 2097152 -b 4096 -n $nreads
5098
5099     echo checking missing pages
5100     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5101           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5102
5103     [ $miss -lt 3 ] || error "misses too much pages!"
5104     cleanup_test101f
5105 }
5106 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5107
5108 setup_test102() {
5109         mkdir -p $DIR/$tdir
5110         chown $RUNAS_ID $DIR/$tdir
5111         STRIPE_SIZE=65536
5112         STRIPE_OFFSET=1
5113         STRIPE_COUNT=$OSTCOUNT
5114         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5115
5116         trap cleanup_test102 EXIT
5117         cd $DIR
5118         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5119         cd $DIR/$tdir
5120         for num in 1 2 3 4; do
5121                 for count in $(seq 1 $STRIPE_COUNT); do
5122                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5123                                 local size=`expr $STRIPE_SIZE \* $num`
5124                                 local file=file"$num-$idx-$count"
5125                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5126                         done
5127                 done
5128         done
5129
5130         cd $DIR
5131         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5132 }
5133
5134 cleanup_test102() {
5135         trap 0
5136         rm -f $TMP/f102.tar
5137         rm -rf $DIR/d0.sanity/d102
5138 }
5139
5140 test_102a() {
5141         local testfile=$DIR/xattr_testfile
5142
5143         rm -f $testfile
5144         touch $testfile
5145
5146         [ "$UID" != 0 ] && skip_env "must run as root" && return
5147         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5148                 skip_env "must have user_xattr" && return
5149
5150         [ -z "$(which setfattr 2>/dev/null)" ] &&
5151                 skip_env "could not find setfattr" && return
5152
5153         echo "set/get xattr..."
5154         setfattr -n trusted.name1 -v value1 $testfile || error
5155         [ "`getfattr -n trusted.name1 $testfile 2> /dev/null | \
5156         grep "trusted.name1"`" == "trusted.name1=\"value1\"" ] || error
5157
5158         setfattr -n user.author1 -v author1 $testfile || error
5159         [ "`getfattr -n user.author1 $testfile 2> /dev/null | \
5160         grep "user.author1"`" == "user.author1=\"author1\"" ] || error
5161
5162         echo "listxattr..."
5163         setfattr -n trusted.name2 -v value2 $testfile || error
5164         setfattr -n trusted.name3 -v value3 $testfile || error
5165         [ `getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5166         grep "trusted.name" | wc -l` -eq 3 ] || error
5167
5168
5169         setfattr -n user.author2 -v author2 $testfile || error
5170         setfattr -n user.author3 -v author3 $testfile || error
5171         [ `getfattr -d -m "^user" $testfile 2> /dev/null | \
5172         grep "user" | wc -l` -eq 3 ] || error
5173
5174         echo "remove xattr..."
5175         setfattr -x trusted.name1 $testfile || error
5176         getfattr -d -m trusted $testfile 2> /dev/null | \
5177         grep "trusted.name1" && error || true
5178
5179         setfattr -x user.author1 $testfile || error
5180         getfattr -d -m user $testfile 2> /dev/null | \
5181         grep "user.author1" && error || true
5182
5183         # b10667: setting lustre special xattr be silently discarded
5184         echo "set lustre special xattr ..."
5185         setfattr -n "trusted.lov" -v "invalid value" $testfile || error
5186
5187         rm -f $testfile
5188 }
5189 run_test 102a "user xattr test =================================="
5190
5191 test_102b() {
5192         # b10930: get/set/list trusted.lov xattr
5193         echo "get/set/list trusted.lov xattr ..."
5194         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5195         local testfile=$DIR/$tfile
5196         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5197                 error "setstripe failed"
5198         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5199                 error "getstripe failed"
5200         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5201         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5202
5203         local testfile2=${testfile}2
5204         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5205                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5206
5207         $MCREATE $testfile2
5208         setfattr -n trusted.lov -v $value $testfile2
5209         local stripe_size=$($GETSTRIPE -S $testfile2)
5210         local stripe_count=$($GETSTRIPE -c $testfile2)
5211         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5212         [ $stripe_count -eq $STRIPECOUNT ] ||
5213                 error "stripe count $stripe_count != $STRIPECOUNT"
5214         rm -f $DIR/$tfile
5215 }
5216 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5217
5218 test_102c() {
5219         # b10930: get/set/list lustre.lov xattr
5220         echo "get/set/list lustre.lov xattr ..."
5221         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5222         mkdir -p $DIR/$tdir
5223         chown $RUNAS_ID $DIR/$tdir
5224         local testfile=$DIR/$tdir/$tfile
5225         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5226                 error "setstripe failed"
5227         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5228                 error "getstripe failed"
5229         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5230         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5231
5232         local testfile2=${testfile}2
5233         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5234                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5235
5236         $RUNAS $MCREATE $testfile2
5237         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5238         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5239         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5240         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5241         [ $stripe_count -eq $STRIPECOUNT ] ||
5242                 error "stripe count $stripe_count != $STRIPECOUNT"
5243 }
5244 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5245
5246 compare_stripe_info1() {
5247         local stripe_index_all_zero=1
5248
5249         for num in 1 2 3 4
5250         do
5251                 for count in `seq 1 $STRIPE_COUNT`
5252                 do
5253                         for offset in `seq 0 $[$STRIPE_COUNT - 1]`
5254                         do
5255                                 local size=`expr $STRIPE_SIZE \* $num`
5256                                 local file=file"$num-$offset-$count"
5257                                 get_stripe_info client $PWD/$file "$1"
5258                                 if [ $stripe_size -ne $size ]; then
5259                                         error "$file: different stripe size $stripe_size, expected $size" && return
5260                                 fi
5261                                 if [ $stripe_count -ne $count ]; then
5262                                         error "$file: different stripe count $stripe_count, expected $count" && return
5263                                 fi
5264                                 if [ $stripe_index -ne 0 ]; then
5265                                        stripe_index_all_zero=0
5266                                 fi
5267                         done
5268                 done
5269         done
5270         [ $stripe_index_all_zero -eq 1 ] && error "all files are being extracted starting from OST index 0"
5271         return 0
5272 }
5273
5274 compare_stripe_info2() {
5275         for num in 1 2 3 4
5276         do
5277                 for count in `seq 1 $STRIPE_COUNT`
5278                 do
5279                         for offset in `seq 0 $[$STRIPE_COUNT - 1]`
5280                         do
5281                                 local size=`expr $STRIPE_SIZE \* $num`
5282                                 local file=file"$num-$offset-$count"
5283                                 get_stripe_info client $PWD/$file
5284                                 if [ $stripe_size -ne $size ]; then
5285                                         error "$file: different stripe size $stripe_size, expected $size" && return
5286                                 fi
5287                                 if [ $stripe_count -ne $count ]; then
5288                                         error "$file: different stripe count $stripe_count, expected $count" && return
5289                                 fi
5290                                 if [ $stripe_index -ne $offset ]; then
5291                                         error "$file: different stripe offset $stripe_index, expected $offset" && return
5292                                 fi
5293                         done
5294                 done
5295         done
5296 }
5297
5298 find_lustre_tar() {
5299         [ -n "$(which tar 2>/dev/null)" ] && strings $(which tar) | grep -q lustre && echo tar
5300 }
5301
5302 test_102d() {
5303         # b10930: tar test for trusted.lov xattr
5304         TAR=$(find_lustre_tar)
5305         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5306         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5307         setup_test102
5308         mkdir -p $DIR/d102d
5309         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5310         cd $DIR/d102d/$tdir
5311         compare_stripe_info1
5312 }
5313 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5314
5315 test_102f() {
5316         # b10930: tar test for trusted.lov xattr
5317         TAR=$(find_lustre_tar)
5318         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5319         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5320         setup_test102
5321         mkdir -p $DIR/d102f
5322         cd $DIR
5323         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5324         cd $DIR/d102f/$tdir
5325         compare_stripe_info1
5326 }
5327 run_test 102f "tar copy files, not keep osts ==========="
5328
5329 grow_xattr() {
5330         local xsize=${1:-1024}  # in bytes
5331         local file=$DIR/$tfile
5332
5333         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5334                 skip "must have user_xattr" && return 0
5335         [ -z "$(which setfattr 2>/dev/null)" ] &&
5336                 skip_env "could not find setfattr" && return 0
5337         [ -z "$(which getfattr 2>/dev/null)" ] &&
5338                 skip_env "could not find getfattr" && return 0
5339
5340         touch $file
5341
5342         local value="$(generate_string $xsize)"
5343
5344         local xbig=trusted.big
5345         log "save $xbig on $file"
5346         setfattr -n $xbig -v $value $file ||
5347                 error "saving $xbig on $file failed"
5348
5349         local orig=$(get_xattr_value $xbig $file)
5350         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
5351
5352         local xsml=trusted.sml
5353         log "save $xsml on $file"
5354         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
5355
5356         local new=$(get_xattr_value $xbig $file)
5357         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
5358
5359         log "grow $xsml on $file"
5360         setfattr -n $xsml -v "$value" $file ||
5361                 error "growing $xsml on $file failed"
5362
5363         new=$(get_xattr_value $xbig $file)
5364         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
5365         log "$xbig still valid after growing $xsml"
5366
5367         rm -f $file
5368 }
5369
5370 test_102h() { # bug 15777
5371         grow_xattr 1024
5372 }
5373 run_test 102h "grow xattr from inside inode to external block"
5374
5375 test_102ha() {
5376         large_xattr_enabled || { skip "large_xattr disabled" && return; }
5377         grow_xattr $(max_xattr_size)
5378 }
5379 run_test 102ha "grow xattr from inside inode to external inode"
5380
5381 test_102i() { # bug 17038
5382         touch $DIR/$tfile
5383         ln -s $DIR/$tfile $DIR/${tfile}link
5384         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
5385         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"
5386         rm -f $DIR/$tfile $DIR/${tfile}link
5387 }
5388 run_test 102i "lgetxattr test on symbolic link ============"
5389
5390 test_102j() {
5391         TAR=$(find_lustre_tar)
5392         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5393         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5394         setup_test102 "$RUNAS"
5395         mkdir -p $DIR/d102j
5396         chown $RUNAS_ID $DIR/d102j
5397         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
5398         cd $DIR/d102j/$tdir
5399         compare_stripe_info1 "$RUNAS"
5400 }
5401 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
5402
5403 test_102k() {
5404         touch $DIR/$tfile
5405         # b22187 just check that does not crash for regular file.
5406         setfattr -n trusted.lov $DIR/$tfile
5407         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
5408         local test_kdir=$DIR/d102k
5409         mkdir $test_kdir
5410         local default_size=`$GETSTRIPE -S $test_kdir`
5411         local default_count=`$GETSTRIPE -c $test_kdir`
5412         local default_offset=`$GETSTRIPE -i $test_kdir`
5413         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
5414                 error 'dir setstripe failed'
5415         setfattr -n trusted.lov $test_kdir
5416         local stripe_size=`$GETSTRIPE -S $test_kdir`
5417         local stripe_count=`$GETSTRIPE -c $test_kdir`
5418         local stripe_offset=`$GETSTRIPE -i $test_kdir`
5419         [ $stripe_size -eq $default_size ] ||
5420                 error "stripe size $stripe_size != $default_size"
5421         [ $stripe_count -eq $default_count ] ||
5422                 error "stripe count $stripe_count != $default_count"
5423         [ $stripe_offset -eq $default_offset ] ||
5424                 error "stripe offset $stripe_offset != $default_offset"
5425         rm -rf $DIR/$tfile $test_kdir
5426 }
5427 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
5428
5429 cleanup_test102
5430
5431 run_acl_subtest()
5432 {
5433     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
5434     return $?
5435 }
5436
5437 test_103 () {
5438     [ "$UID" != 0 ] && skip_env "must run as root" && return
5439     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5440     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
5441     $GSS && skip "could not run under gss" && return
5442
5443     declare -a identity_old
5444
5445     for num in `seq $MDSCOUNT`; do
5446         switch_identity $num true || identity_old[$num]=$?
5447     done
5448
5449     SAVE_UMASK=`umask`
5450     umask 0022
5451     cd $DIR
5452
5453     echo "performing cp ..."
5454     run_acl_subtest cp || error
5455     echo "performing getfacl-noacl..."
5456     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
5457     echo "performing misc..."
5458     run_acl_subtest misc || error  "misc test failed"
5459     echo "performing permissions..."
5460     run_acl_subtest permissions || error "permissions failed"
5461     echo "performing setfacl..."
5462     run_acl_subtest setfacl || error  "setfacl test failed"
5463
5464     # inheritance test got from HP
5465     echo "performing inheritance..."
5466     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
5467     chmod +x make-tree || error "chmod +x failed"
5468     run_acl_subtest inheritance || error "inheritance test failed"
5469     rm -f make-tree
5470
5471     cd $SAVE_PWD
5472     umask $SAVE_UMASK
5473
5474     for num in `seq $MDSCOUNT`; do
5475         if [ "${identity_old[$num]}" = 1 ]; then
5476             switch_identity $num false || identity_old[$num]=$?
5477         fi
5478     done
5479 }
5480 run_test 103 "acl test ========================================="
5481
5482 test_104a() {
5483         touch $DIR/$tfile
5484         lfs df || error "lfs df failed"
5485         lfs df -ih || error "lfs df -ih failed"
5486         lfs df -h $DIR || error "lfs df -h $DIR failed"
5487         lfs df -i $DIR || error "lfs df -i $DIR failed"
5488         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
5489         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
5490
5491         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
5492         lctl --device %$OSC deactivate
5493         lfs df || error "lfs df with deactivated OSC failed"
5494         lctl --device %$OSC activate
5495         # wait the osc back to normal
5496         wait_osc_import_state client ost FULL
5497
5498         lfs df || error "lfs df with reactivated OSC failed"
5499         rm -f $DIR/$tfile
5500 }
5501 run_test 104a "lfs df [-ih] [path] test ========================="
5502
5503 test_104b() {
5504         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5505         chmod 666 /dev/obd
5506         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
5507         if [ $denied_cnt -ne 0 ];
5508         then
5509                     error "lfs check servers test failed"
5510         fi
5511 }
5512 run_test 104b "$RUNAS lfs check servers test ===================="
5513
5514 test_105a() {
5515         # doesn't work on 2.4 kernels
5516         touch $DIR/$tfile
5517         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5518         then
5519                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
5520         else
5521                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
5522         fi
5523         rm -f $DIR/$tfile
5524 }
5525 run_test 105a "flock when mounted without -o flock test ========"
5526
5527 test_105b() {
5528         touch $DIR/$tfile
5529         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5530         then
5531                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
5532         else
5533                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
5534         fi
5535         rm -f $DIR/$tfile
5536 }
5537 run_test 105b "fcntl when mounted without -o flock test ========"
5538
5539 test_105c() {
5540         touch $DIR/$tfile
5541         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5542         then
5543                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
5544         else
5545                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
5546         fi
5547         rm -f $DIR/$tfile
5548 }
5549 run_test 105c "lockf when mounted without -o flock test ========"
5550
5551 test_105d() { # bug 15924
5552         mkdir -p $DIR/$tdir
5553         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5554                 skip "mount w/o flock enabled" && return
5555         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
5556         $LCTL set_param fail_loc=0x80000315
5557         flocks_test 2 $DIR/$tdir
5558 }
5559 run_test 105d "flock race (should not freeze) ========"
5560
5561 test_105e() { # bug 22660 && 22040
5562         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5563                 skip "mount w/o flock enabled" && return
5564         touch $DIR/$tfile
5565         flocks_test 3 $DIR/$tfile
5566 }
5567 run_test 105e "Two conflicting flocks from same process ======="
5568
5569 test_106() { #bug 10921
5570         mkdir -p $DIR/$tdir
5571         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
5572         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
5573 }
5574 run_test 106 "attempt exec of dir followed by chown of that dir"
5575
5576 test_107() {
5577         CDIR=`pwd`
5578         cd $DIR
5579
5580         local file=core
5581         rm -f $file
5582
5583         local save_pattern=$(sysctl -n kernel.core_pattern)
5584         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
5585         sysctl -w kernel.core_pattern=$file
5586         sysctl -w kernel.core_uses_pid=0
5587
5588         ulimit -c unlimited
5589         sleep 60 &
5590         SLEEPPID=$!
5591
5592         sleep 1
5593
5594         kill -s 11 $SLEEPPID
5595         wait $SLEEPPID
5596         if [ -e $file ]; then
5597                 size=`stat -c%s $file`
5598                 [ $size -eq 0 ] && error "Fail to create core file $file"
5599         else
5600                 error "Fail to create core file $file"
5601         fi
5602         rm -f $file
5603         sysctl -w kernel.core_pattern=$save_pattern
5604         sysctl -w kernel.core_uses_pid=$save_uses_pid
5605         cd $CDIR
5606 }
5607 run_test 107 "Coredump on SIG"
5608
5609 test_110() {
5610         mkdir -p $DIR/d110
5611         mkdir $DIR/d110/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || error "mkdir with 255 char fail"
5612         mkdir $DIR/d110/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && error "mkdir with 256 char should fail, but not"
5613         touch $DIR/d110/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx || error "create with 255 char fail"
5614         touch $DIR/d110/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy && error ""create with 256 char should fail, but not
5615
5616         ls -l $DIR/d110
5617     rm -fr $DIR/d110
5618 }
5619 run_test 110 "filename length checking"
5620
5621 test_115() {
5622         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5623             cut -c11-20)
5624         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
5625             return
5626         echo "Starting with $OSTIO_pre threads"
5627
5628         NUMTEST=20000
5629         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
5630         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
5631         echo "$NUMTEST creates/unlinks"
5632         mkdir -p $DIR/$tdir
5633         createmany -o $DIR/$tdir/$tfile $NUMTEST
5634         unlinkmany $DIR/$tdir/$tfile $NUMTEST
5635
5636         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5637             cut -c11-20)
5638
5639         # don't return an error
5640         [ $OSTIO_post -eq $OSTIO_pre ] && echo \
5641             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&\
5642             echo "This may be fine, depending on what ran before this test" &&\
5643             echo "and how fast this system is." && return
5644
5645         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
5646 }
5647 run_test 115 "verify dynamic thread creation===================="
5648
5649 free_min_max () {
5650         wait_delete_completed
5651         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
5652         echo OST kbytes available: ${AVAIL[@]}
5653         MAXI=0; MAXV=${AVAIL[0]}
5654         MINI=0; MINV=${AVAIL[0]}
5655         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
5656             #echo OST $i: ${AVAIL[i]}kb
5657             if [ ${AVAIL[i]} -gt $MAXV ]; then
5658                 MAXV=${AVAIL[i]}; MAXI=$i
5659             fi
5660             if [ ${AVAIL[i]} -lt $MINV ]; then
5661                 MINV=${AVAIL[i]}; MINI=$i
5662             fi
5663         done
5664         echo Min free space: OST $MINI: $MINV
5665         echo Max free space: OST $MAXI: $MAXV
5666 }
5667
5668 test_116() {
5669         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
5670
5671         echo -n "Free space priority "
5672         lctl get_param -n lov.*-clilov-*.qos_prio_free
5673         declare -a AVAIL
5674         free_min_max
5675         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
5676                 return
5677
5678         # generate uneven OSTs
5679         mkdir -p $DIR/$tdir/OST${MINI}
5680         declare -i FILL
5681         FILL=$(($MINV / 4))
5682         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
5683         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
5684         i=0
5685         while [ $FILL -gt 0 ]; do
5686             i=$(($i + 1))
5687             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
5688             FILL=$(($FILL - 2048))
5689             echo -n .
5690         done
5691         FILL=$(($MINV / 4))
5692         sync
5693         sleep_maxage
5694
5695         free_min_max
5696         DIFF=$(($MAXV - $MINV))
5697         DIFF2=$(($DIFF * 100 / $MINV))
5698         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
5699         if [ $DIFF2 -gt 20 ]; then
5700             echo "ok"
5701         else
5702             echo "failed - QOS mode won't be used"
5703             error_ignore "QOS imbalance criteria not met"
5704             return
5705         fi
5706
5707         MINI1=$MINI; MINV1=$MINV
5708         MAXI1=$MAXI; MAXV1=$MAXV
5709
5710         # now fill using QOS
5711         echo writing a bunch of files to QOS-assigned OSTs
5712         $SETSTRIPE -c 1 $DIR/$tdir
5713         i=0
5714         while [ $FILL -gt 0 ]; do
5715             i=$(($i + 1))
5716             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
5717             FILL=$(($FILL - 200))
5718             echo -n .
5719         done
5720         echo "wrote $i 200k files"
5721         sync
5722         sleep_maxage
5723
5724         echo "Note: free space may not be updated, so measurements might be off"
5725         free_min_max
5726         DIFF2=$(($MAXV - $MINV))
5727         echo "free space delta: orig $DIFF final $DIFF2"
5728         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
5729         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
5730         echo "Wrote $DIFF to smaller OST $MINI1"
5731         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
5732         echo "Wrote $DIFF2 to larger OST $MAXI1"
5733         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
5734
5735         # Figure out which files were written where
5736         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
5737                awk '/'$MINI1': / {print $2; exit}')
5738         echo $UUID
5739         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
5740         echo "$MINC files created on smaller OST $MINI1"
5741         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
5742                awk '/'$MAXI1': / {print $2; exit}')
5743         echo $UUID
5744         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
5745         echo "$MAXC files created on larger OST $MAXI1"
5746         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
5747         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
5748
5749         rm -rf $DIR/$tdir
5750 }
5751 run_test 116 "stripe QOS: free space balance ==================="
5752
5753 test_117() # bug 10891
5754 {
5755         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
5756         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
5757         lctl set_param fail_loc=0x21e
5758         > $DIR/$tfile || error "truncate failed"
5759         lctl set_param fail_loc=0
5760         echo "Truncate succeeded."
5761         rm -f $DIR/$tfile
5762 }
5763 run_test 117 "verify fsfilt_extend =========="
5764
5765 export OLD_RESENDCOUNT=""
5766 set_resend_count () {
5767         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
5768         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
5769         lctl set_param -n $PROC_RESENDCOUNT $1
5770         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
5771 }
5772
5773 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
5774
5775 # Reset async IO behavior after error case
5776 reset_async() {
5777         FILE=$DIR/reset_async
5778
5779         # Ensure all OSCs are cleared
5780         $SETSTRIPE -c -1 $FILE
5781         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
5782         sync
5783         rm $FILE
5784 }
5785
5786 test_118a() #bug 11710
5787 {
5788         reset_async
5789
5790         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5791         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5792         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
5793
5794         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5795                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5796                 return 1;
5797         fi
5798         rm -f $DIR/$tfile
5799 }
5800 run_test 118a "verify O_SYNC works =========="
5801
5802 test_118b()
5803 {
5804         remote_ost_nodsh && skip "remote OST with nodsh" && return
5805
5806         reset_async
5807
5808         #define OBD_FAIL_OST_ENOENT 0x217
5809         set_nodes_failloc "$(osts_nodes)" 0x217
5810         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5811         RC=$?
5812         set_nodes_failloc "$(osts_nodes)" 0
5813         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5814         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5815                     grep -c writeback)
5816
5817         if [[ $RC -eq 0 ]]; then
5818                 error "Must return error due to dropped pages, rc=$RC"
5819                 return 1;
5820         fi
5821
5822         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5823                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5824                 return 1;
5825         fi
5826
5827         echo "Dirty pages not leaked on ENOENT"
5828
5829         # Due to the above error the OSC will issue all RPCs syncronously
5830         # until a subsequent RPC completes successfully without error.
5831         multiop $DIR/$tfile Ow4096yc
5832         rm -f $DIR/$tfile
5833
5834         return 0
5835 }
5836 run_test 118b "Reclaim dirty pages on fatal error =========="
5837
5838 test_118c()
5839 {
5840         remote_ost_nodsh && skip "remote OST with nodsh" && return
5841
5842         reset_async
5843
5844         #define OBD_FAIL_OST_EROFS               0x216
5845         set_nodes_failloc "$(osts_nodes)" 0x216
5846
5847         # multiop should block due to fsync until pages are written
5848         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
5849         MULTIPID=$!
5850         sleep 1
5851
5852         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
5853                 error "Multiop failed to block on fsync, pid=$MULTIPID"
5854         fi
5855
5856         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5857                     grep -c writeback)
5858         if [[ $WRITEBACK -eq 0 ]]; then
5859                 error "No page in writeback, writeback=$WRITEBACK"
5860         fi
5861
5862         set_nodes_failloc "$(osts_nodes)" 0
5863         wait $MULTIPID
5864         RC=$?
5865         if [[ $RC -ne 0 ]]; then
5866                 error "Multiop fsync failed, rc=$RC"
5867         fi
5868
5869         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5870         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5871                     grep -c writeback)
5872         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5873                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5874         fi
5875
5876         rm -f $DIR/$tfile
5877         echo "Dirty pages flushed via fsync on EROFS"
5878         return 0
5879 }
5880 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
5881
5882 test_118d()
5883 {
5884         remote_ost_nodsh && skip "remote OST with nodsh" && return
5885
5886         reset_async
5887
5888         #define OBD_FAIL_OST_BRW_PAUSE_BULK
5889         set_nodes_failloc "$(osts_nodes)" 0x214
5890         # multiop should block due to fsync until pages are written
5891         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
5892         MULTIPID=$!
5893         sleep 1
5894
5895         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
5896                 error "Multiop failed to block on fsync, pid=$MULTIPID"
5897         fi
5898
5899         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5900                     grep -c writeback)
5901         if [[ $WRITEBACK -eq 0 ]]; then
5902                 error "No page in writeback, writeback=$WRITEBACK"
5903         fi
5904
5905         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
5906         set_nodes_failloc "$(osts_nodes)" 0
5907
5908         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5909         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5910                     grep -c writeback)
5911         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5912                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5913         fi
5914
5915         rm -f $DIR/$tfile
5916         echo "Dirty pages gaurenteed flushed via fsync"
5917         return 0
5918 }
5919 run_test 118d "Fsync validation inject a delay of the bulk =========="
5920
5921 test_118f() {
5922         reset_async
5923
5924         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
5925         lctl set_param fail_loc=0x8000040a
5926
5927         # Should simulate EINVAL error which is fatal
5928         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5929         RC=$?
5930         if [[ $RC -eq 0 ]]; then
5931                 error "Must return error due to dropped pages, rc=$RC"
5932         fi
5933
5934         lctl set_param fail_loc=0x0
5935
5936         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
5937         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5938         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5939                     grep -c writeback)
5940         if [[ $LOCKED -ne 0 ]]; then
5941                 error "Locked pages remain in cache, locked=$LOCKED"
5942         fi
5943
5944         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5945                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5946         fi
5947
5948         rm -f $DIR/$tfile
5949         echo "No pages locked after fsync"
5950
5951         reset_async
5952         return 0
5953 }
5954 run_test 118f "Simulate unrecoverable OSC side error =========="
5955
5956 test_118g() {
5957         reset_async
5958
5959         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5960         lctl set_param fail_loc=0x406
5961
5962         # simulate local -ENOMEM
5963         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5964         RC=$?
5965
5966         lctl set_param fail_loc=0
5967         if [[ $RC -eq 0 ]]; then
5968                 error "Must return error due to dropped pages, rc=$RC"
5969         fi
5970
5971         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
5972         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
5973         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
5974                         grep -c writeback)
5975         if [[ $LOCKED -ne 0 ]]; then
5976                 error "Locked pages remain in cache, locked=$LOCKED"
5977         fi
5978
5979         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
5980                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
5981         fi
5982
5983         rm -f $DIR/$tfile
5984         echo "No pages locked after fsync"
5985
5986         reset_async
5987         return 0
5988 }
5989 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
5990
5991 test_118h() {
5992         remote_ost_nodsh && skip "remote OST with nodsh" && return
5993
5994         reset_async
5995
5996         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
5997         set_nodes_failloc "$(osts_nodes)" 0x20e
5998         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
5999         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6000         RC=$?
6001
6002         set_nodes_failloc "$(osts_nodes)" 0
6003         if [[ $RC -eq 0 ]]; then
6004                 error "Must return error due to dropped pages, rc=$RC"
6005         fi
6006
6007         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6008         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6009         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6010                     grep -c writeback)
6011         if [[ $LOCKED -ne 0 ]]; then
6012                 error "Locked pages remain in cache, locked=$LOCKED"
6013         fi
6014
6015         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6016                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6017         fi
6018
6019         rm -f $DIR/$tfile
6020         echo "No pages locked after fsync"
6021
6022         return 0
6023 }
6024 run_test 118h "Verify timeout in handling recoverables errors  =========="
6025
6026 test_118i() {
6027         remote_ost_nodsh && skip "remote OST with nodsh" && return
6028
6029         reset_async
6030
6031         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6032         set_nodes_failloc "$(osts_nodes)" 0x20e
6033
6034         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6035         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6036         PID=$!
6037         sleep 5
6038         set_nodes_failloc "$(osts_nodes)" 0
6039
6040         wait $PID
6041         RC=$?
6042         if [[ $RC -ne 0 ]]; then
6043                 error "got error, but should be not, rc=$RC"
6044         fi
6045
6046         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6047         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6048         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6049         if [[ $LOCKED -ne 0 ]]; then
6050                 error "Locked pages remain in cache, locked=$LOCKED"
6051         fi
6052
6053         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6054                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6055         fi
6056
6057         rm -f $DIR/$tfile
6058         echo "No pages locked after fsync"
6059
6060         return 0
6061 }
6062 run_test 118i "Fix error before timeout in recoverable error  =========="
6063
6064 test_118j() {
6065         remote_ost_nodsh && skip "remote OST with nodsh" && return
6066
6067         reset_async
6068
6069         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6070         set_nodes_failloc "$(osts_nodes)" 0x220
6071
6072         # return -EIO from OST
6073         multiop $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6074         RC=$?
6075         set_nodes_failloc "$(osts_nodes)" 0x0
6076         if [[ $RC -eq 0 ]]; then
6077                 error "Must return error due to dropped pages, rc=$RC"
6078         fi
6079
6080         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6081         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6082         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6083         if [[ $LOCKED -ne 0 ]]; then
6084                 error "Locked pages remain in cache, locked=$LOCKED"
6085         fi
6086
6087         # in recoverable error on OST we want resend and stay until it finished
6088         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6089                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6090         fi
6091
6092         rm -f $DIR/$tfile
6093         echo "No pages locked after fsync"
6094
6095         return 0
6096 }
6097 run_test 118j "Simulate unrecoverable OST side error =========="
6098
6099 test_118k()
6100 {
6101         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6102
6103         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6104         set_nodes_failloc "$(osts_nodes)" 0x20e
6105         mkdir -p $DIR/$tdir
6106
6107         for ((i=0;i<10;i++)); do
6108                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6109                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6110                 SLEEPPID=$!
6111                 sleep 0.500s
6112                 kill $SLEEPPID
6113                 wait $SLEEPPID
6114         done
6115
6116         set_nodes_failloc "$(osts_nodes)" 0
6117         rm -rf $DIR/$tdir
6118 }
6119 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6120
6121 test_118l()
6122 {
6123         # LU-646
6124         mkdir -p $DIR/$tdir
6125         multiop $DIR/$tdir Dy || error "fsync dir failed"
6126         rm -rf $DIR/$tdir
6127 }
6128 run_test 118l "fsync dir ========="
6129
6130 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6131
6132 test_119a() # bug 11737
6133 {
6134         BSIZE=$((512 * 1024))
6135         directio write $DIR/$tfile 0 1 $BSIZE
6136         # We ask to read two blocks, which is more than a file size.
6137         # directio will indicate an error when requested and actual
6138         # sizes aren't equeal (a normal situation in this case) and
6139         # print actual read amount.
6140         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6141         if [ "$NOB" != "$BSIZE" ]; then
6142                 error "read $NOB bytes instead of $BSIZE"
6143         fi
6144         rm -f $DIR/$tfile
6145 }
6146 run_test 119a "Short directIO read must return actual read amount"
6147
6148 test_119b() # bug 11737
6149 {
6150         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6151
6152         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6153         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6154         sync
6155         multiop $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6156                 error "direct read failed"
6157         rm -f $DIR/$tfile
6158 }
6159 run_test 119b "Sparse directIO read must return actual read amount"
6160
6161 test_119c() # bug 13099
6162 {
6163         BSIZE=1048576
6164         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6165         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6166         rm -f $DIR/$tfile
6167 }
6168 run_test 119c "Testing for direct read hitting hole"
6169
6170 test_119d() # bug 15950
6171 {
6172         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6173         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6174         BSIZE=1048576
6175         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6176         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6177         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6178         lctl set_param fail_loc=0x40d
6179         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6180         pid_dio=$!
6181         sleep 1
6182         cat $DIR/$tfile > /dev/null &
6183         lctl set_param fail_loc=0
6184         pid_reads=$!
6185         wait $pid_dio
6186         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6187         sleep 2
6188         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6189         error "the read rpcs have not completed in 2s"
6190         rm -f $DIR/$tfile
6191         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6192 }
6193 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6194
6195 test_120a() {
6196         mkdir -p $DIR/$tdir
6197         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6198                skip "no early lock cancel on server" && return 0
6199         lru_resize_disable mdc
6200         lru_resize_disable osc
6201         cancel_lru_locks mdc
6202         stat $DIR/$tdir > /dev/null
6203         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6204         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6205         mkdir $DIR/$tdir/d1
6206         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6207         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6208         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6209         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6210         lru_resize_enable mdc
6211         lru_resize_enable osc
6212 }
6213 run_test 120a "Early Lock Cancel: mkdir test"
6214
6215 test_120b() {
6216         mkdir -p $DIR/$tdir
6217         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6218                skip "no early lock cancel on server" && return 0
6219         lru_resize_disable mdc
6220         lru_resize_disable osc
6221         cancel_lru_locks mdc
6222         stat $DIR/$tdir > /dev/null
6223         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6224         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6225         touch $DIR/$tdir/f1
6226         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6227         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6228         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6229         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6230         lru_resize_enable mdc
6231         lru_resize_enable osc
6232 }
6233 run_test 120b "Early Lock Cancel: create test"
6234
6235 test_120c() {
6236         mkdir -p $DIR/$tdir
6237         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6238                skip "no early lock cancel on server" && return 0
6239         lru_resize_disable mdc
6240         lru_resize_disable osc
6241         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6242         touch $DIR/$tdir/d1/f1
6243         cancel_lru_locks mdc
6244         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6245         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6246         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6247         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6248         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6249         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6250         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6251         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6252         lru_resize_enable mdc
6253         lru_resize_enable osc
6254 }
6255 run_test 120c "Early Lock Cancel: link test"
6256
6257 test_120d() {
6258         mkdir -p $DIR/$tdir
6259         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6260                skip "no early lock cancel on server" && return 0
6261         lru_resize_disable mdc
6262         lru_resize_disable osc
6263         touch $DIR/$tdir
6264         cancel_lru_locks mdc
6265         stat $DIR/$tdir > /dev/null
6266         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6267         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6268         chmod a+x $DIR/$tdir
6269         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6270         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6271         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6272         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6273         lru_resize_enable mdc
6274         lru_resize_enable osc
6275 }
6276 run_test 120d "Early Lock Cancel: setattr test"
6277
6278 test_120e() {
6279         mkdir -p $DIR/$tdir
6280         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6281                skip "no early lock cancel on server" && return 0
6282         lru_resize_disable mdc
6283         lru_resize_disable osc
6284         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6285         cancel_lru_locks mdc
6286         cancel_lru_locks osc
6287         dd if=$DIR/$tdir/f1 of=/dev/null
6288         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
6289         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6290               awk '/ldlm_cancel/ {print $2}'`
6291         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6292               awk '/ldlm_bl_callback/ {print $2}'`
6293         unlink $DIR/$tdir/f1
6294         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6295               awk '/ldlm_cancel/ {print $2}'`
6296         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6297               awk '/ldlm_bl_callback/ {print $2}'`
6298         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6299         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6300         lru_resize_enable mdc
6301         lru_resize_enable osc
6302 }
6303 run_test 120e "Early Lock Cancel: unlink test"
6304
6305 test_120f() {
6306         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6307                skip "no early lock cancel on server" && return 0
6308         mkdir -p $DIR/$tdir
6309         lru_resize_disable mdc
6310         lru_resize_disable osc
6311         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6312         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
6313         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
6314         cancel_lru_locks mdc
6315         cancel_lru_locks osc
6316         dd if=$DIR/$tdir/d1/f1 of=/dev/null
6317         dd if=$DIR/$tdir/d2/f2 of=/dev/null
6318         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
6319         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6320               awk '/ldlm_cancel/ {print $2}'`
6321         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6322               awk '/ldlm_bl_callback/ {print $2}'`
6323         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6324         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6325               awk '/ldlm_cancel/ {print $2}'`
6326         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6327               awk '/ldlm_bl_callback/ {print $2}'`
6328         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6329         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6330         lru_resize_enable mdc
6331         lru_resize_enable osc
6332 }
6333 run_test 120f "Early Lock Cancel: rename test"
6334
6335 test_120g() {
6336         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6337                skip "no early lock cancel on server" && return 0
6338         lru_resize_disable mdc
6339         lru_resize_disable osc
6340         count=10000
6341         echo create $count files
6342         mkdir -p $DIR/$tdir
6343         cancel_lru_locks mdc
6344         cancel_lru_locks osc
6345         t0=`date +%s`
6346
6347         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6348               awk '/ldlm_cancel/ {print $2}'`
6349         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6350               awk '/ldlm_bl_callback/ {print $2}'`
6351         createmany -o $DIR/$tdir/f $count
6352         sync
6353         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6354               awk '/ldlm_cancel/ {print $2}'`
6355         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6356               awk '/ldlm_bl_callback/ {print $2}'`
6357         t1=`date +%s`
6358         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
6359         echo rm $count files
6360         rm -r $DIR/$tdir
6361         sync
6362         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6363               awk '/ldlm_cancel/ {print $2}'`
6364         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6365               awk '/ldlm_bl_callback/ {print $2}'`
6366         t2=`date +%s`
6367         echo total: $count removes in $((t2-t1))
6368         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
6369         sleep 2
6370         # wait for commitment of removal
6371         lru_resize_enable mdc
6372         lru_resize_enable osc
6373 }
6374 run_test 120g "Early Lock Cancel: performance test"
6375
6376 test_121() { #bug #10589
6377         rm -rf $DIR/$tfile
6378         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
6379 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
6380         lctl set_param fail_loc=0x310
6381         cancel_lru_locks osc > /dev/null
6382         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
6383         lctl set_param fail_loc=0
6384         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
6385 }
6386 run_test 121 "read cancel race ========="
6387
6388 test_123a() { # was test 123, statahead(bug 11401)
6389         SLOWOK=0
6390         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
6391             log "testing on UP system. Performance may be not as good as expected."
6392                         SLOWOK=1
6393         fi
6394
6395         rm -rf $DIR/$tdir
6396         mkdir -p $DIR/$tdir
6397         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6398         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
6399         MULT=10
6400         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
6401                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
6402
6403                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6404                 lctl set_param -n llite.*.statahead_max 0
6405                 lctl get_param llite.*.statahead_max
6406                 cancel_lru_locks mdc
6407                 cancel_lru_locks osc
6408                 stime=`date +%s`
6409                 time ls -l $DIR/$tdir | wc -l
6410                 etime=`date +%s`
6411                 delta=$((etime - stime))
6412                 log "ls $i files without statahead: $delta sec"
6413                 lctl set_param llite.*.statahead_max=$max
6414
6415                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6416                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
6417                 cancel_lru_locks mdc
6418                 cancel_lru_locks osc
6419                 stime=`date +%s`
6420                 time ls -l $DIR/$tdir | wc -l
6421                 etime=`date +%s`
6422                 delta_sa=$((etime - stime))
6423                 log "ls $i files with statahead: $delta_sa sec"
6424                 lctl get_param -n llite.*.statahead_stats
6425                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6426
6427                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
6428                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
6429
6430                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6431                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6432                     lctl set_param -n llite.*.statahead_max 0
6433                     lctl get_param llite.*.statahead_max
6434                     cancel_lru_locks mdc
6435                     cancel_lru_locks osc
6436                     stime=`date +%s`
6437                     time ls -l $DIR/$tdir | wc -l
6438                     etime=`date +%s`
6439                     delta=$((etime - stime))
6440                     log "ls $i files again without statahead: $delta sec"
6441                     lctl set_param llite.*.statahead_max=$max
6442                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6443                         if [  $SLOWOK -eq 0 ]; then
6444                                 error "ls $i files is slower with statahead!"
6445                         else
6446                                 log "ls $i files is slower with statahead!"
6447                         fi
6448                         break
6449                     fi
6450                 fi
6451
6452                 [ $delta -gt 20 ] && break
6453                 [ $delta -gt 8 ] && MULT=$((50 / delta))
6454                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
6455         done
6456         log "ls done"
6457
6458         stime=`date +%s`
6459         rm -r $DIR/$tdir
6460         sync
6461         etime=`date +%s`
6462         delta=$((etime - stime))
6463         log "rm -r $DIR/$tdir/: $delta seconds"
6464         log "rm done"
6465         lctl get_param -n llite.*.statahead_stats
6466 }
6467 run_test 123a "verify statahead work"
6468
6469 test_123b () { # statahead(bug 15027)
6470         mkdir -p $DIR/$tdir
6471         createmany -o $DIR/$tdir/$tfile-%d 1000
6472
6473         cancel_lru_locks mdc
6474         cancel_lru_locks osc
6475
6476 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
6477         lctl set_param fail_loc=0x80000803
6478         ls -lR $DIR/$tdir > /dev/null
6479         log "ls done"
6480         lctl set_param fail_loc=0x0
6481         lctl get_param -n llite.*.statahead_stats
6482         rm -r $DIR/$tdir
6483         sync
6484
6485 }
6486 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
6487
6488 test_124a() {
6489         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6490                skip "no lru resize on server" && return 0
6491         local NR=2000
6492         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
6493
6494         log "create $NR files at $DIR/$tdir"
6495         createmany -o $DIR/$tdir/f $NR ||
6496                 error "failed to create $NR files in $DIR/$tdir"
6497
6498         cancel_lru_locks mdc
6499         ls -l $DIR/$tdir > /dev/null
6500
6501         local NSDIR=""
6502         local LRU_SIZE=0
6503         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
6504                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6505                 LRU_SIZE=$(lctl get_param -n $PARAM)
6506                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
6507                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
6508                                                 log "NSDIR=$NSDIR"
6509                         log "NS=$(basename $NSDIR)"
6510                         break
6511                 fi
6512         done
6513
6514         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
6515                 skip "Not enough cached locks created!"
6516                 return 0
6517         fi
6518         log "LRU=$LRU_SIZE"
6519
6520         local SLEEP=30
6521
6522         # We know that lru resize allows one client to hold $LIMIT locks
6523         # for 10h. After that locks begin to be killed by client.
6524         local MAX_HRS=10
6525         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
6526                 log "LIMIT=$LIMIT"
6527
6528         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
6529         # killing locks. Some time was spent for creating locks. This means
6530         # that up to the moment of sleep finish we must have killed some of
6531         # them (10-100 locks). This depends on how fast ther were created.
6532         # Many of them were touched in almost the same moment and thus will
6533         # be killed in groups.
6534         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
6535
6536         # Use $LRU_SIZE_B here to take into account real number of locks
6537         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
6538         local LRU_SIZE_B=$LRU_SIZE
6539         log "LVF=$LVF"
6540         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
6541                 log "OLD_LVF=$OLD_LVF"
6542         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
6543
6544         # Let's make sure that we really have some margin. Client checks
6545         # cached locks every 10 sec.
6546         SLEEP=$((SLEEP+20))
6547         log "Sleep ${SLEEP} sec"
6548         local SEC=0
6549         while ((SEC<$SLEEP)); do
6550                 echo -n "..."
6551                 sleep 5
6552                 SEC=$((SEC+5))
6553                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
6554                 echo -n "$LRU_SIZE"
6555         done
6556         echo ""
6557         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
6558         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
6559
6560         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
6561                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
6562                 unlinkmany $DIR/$tdir/f $NR
6563                 return
6564         }
6565
6566         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
6567         log "unlink $NR files at $DIR/$tdir"
6568         unlinkmany $DIR/$tdir/f $NR
6569 }
6570 run_test 124a "lru resize ======================================="
6571
6572 get_max_pool_limit()
6573 {
6574         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
6575         local max=0
6576         for l in $limit; do
6577                 if test $l -gt $max; then
6578                         max=$l
6579                 fi
6580         done
6581         echo $max
6582 }
6583
6584 test_124b() {
6585         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6586                skip "no lru resize on server" && return 0
6587
6588         LIMIT=`get_max_pool_limit`
6589
6590         NR=$(($(default_lru_size)*20))
6591         if [ $NR -gt $LIMIT ]; then
6592                 log "Limit lock number by $LIMIT locks"
6593                 NR=$LIMIT
6594         fi
6595         lru_resize_disable mdc
6596         mkdir -p $DIR/$tdir/disable_lru_resize ||
6597                 error "failed to create $DIR/$tdir/disable_lru_resize"
6598
6599         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
6600         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
6601         cancel_lru_locks mdc
6602         stime=`date +%s`
6603         PID=""
6604         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6605         PID="$PID $!"
6606         sleep 2
6607         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6608         PID="$PID $!"
6609         sleep 2
6610         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6611         PID="$PID $!"
6612         wait $PID
6613         etime=`date +%s`
6614         nolruresize_delta=$((etime-stime))
6615         log "ls -la time: $nolruresize_delta seconds"
6616         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6617         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
6618
6619         lru_resize_enable mdc
6620         mkdir -p $DIR/$tdir/enable_lru_resize ||
6621                 error "failed to create $DIR/$tdir/enable_lru_resize"
6622
6623         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
6624         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
6625         cancel_lru_locks mdc
6626         stime=`date +%s`
6627         PID=""
6628         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6629         PID="$PID $!"
6630         sleep 2
6631         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6632         PID="$PID $!"
6633         sleep 2
6634         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6635         PID="$PID $!"
6636         wait $PID
6637         etime=`date +%s`
6638         lruresize_delta=$((etime-stime))
6639         log "ls -la time: $lruresize_delta seconds"
6640         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6641
6642         if [ $lruresize_delta -gt $nolruresize_delta ]; then
6643                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
6644         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
6645                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
6646         else
6647                 log "lru resize performs the same with no lru resize"
6648         fi
6649         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
6650 }
6651 run_test 124b "lru resize (performance test) ======================="
6652
6653 test_125() { # 13358
6654         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6655         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6656         mkdir -p $DIR/d125 || error "mkdir failed"
6657         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
6658         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
6659         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
6660 }
6661 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
6662
6663 test_126() { # bug 12829/13455
6664         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6665         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
6666         $GSS && skip "must run as gss disabled" && return
6667
6668         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
6669         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
6670         rm -f $DIR/$tfile
6671         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
6672 }
6673 run_test 126 "check that the fsgid provided by the client is taken into account"
6674
6675 test_127a() { # bug 15521
6676         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
6677         $LCTL set_param osc.*.stats=0
6678         FSIZE=$((2048 * 1024))
6679         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6680         cancel_lru_locks osc
6681         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
6682
6683         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
6684         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
6685                 echo "got $COUNT $NAME"
6686                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
6687                 eval $NAME=$COUNT || error "Wrong proc format"
6688
6689                 case $NAME in
6690                         read_bytes|write_bytes)
6691                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
6692                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
6693                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
6694                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
6695                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
6696                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
6697                                 error "sumsquare is too small: $SUMSQ"
6698                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
6699                                 error "sumsquare is too big: $SUMSQ"
6700                         ;;
6701                         *) ;;
6702                 esac
6703         done < $DIR/${tfile}.tmp
6704
6705         #check that we actually got some stats
6706         [ "$read_bytes" ] || error "Missing read_bytes stats"
6707         [ "$write_bytes" ] || error "Missing write_bytes stats"
6708         [ "$read_bytes" != 0 ] || error "no read done"
6709         [ "$write_bytes" != 0 ] || error "no write done"
6710 }
6711 run_test 127a "verify the client stats are sane"
6712
6713 test_127b() { # bug LU-333
6714         $LCTL set_param llite.*.stats=0
6715         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
6716         # perform 2 reads and writes so MAX is different from SUM.
6717         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6718         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6719         cancel_lru_locks osc
6720         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
6721         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
6722
6723         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
6724         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
6725                 echo "got $COUNT $NAME"
6726                 eval $NAME=$COUNT || error "Wrong proc format"
6727
6728         case $NAME in
6729                 read_bytes)
6730                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
6731                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
6732                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
6733                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
6734                         ;;
6735                 write_bytes)
6736                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
6737                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
6738                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
6739                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
6740                         ;;
6741                         *) ;;
6742                 esac
6743         done < $TMP/${tfile}.tmp
6744
6745         #check that we actually got some stats
6746         [ "$read_bytes" ] || error "Missing read_bytes stats"
6747         [ "$write_bytes" ] || error "Missing write_bytes stats"
6748         [ "$read_bytes" != 0 ] || error "no read done"
6749         [ "$write_bytes" != 0 ] || error "no write done"
6750 }
6751 run_test 127b "verify the llite client stats are sane"
6752
6753 test_128() { # bug 15212
6754         touch $DIR/$tfile
6755         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
6756                 find $DIR/$tfile
6757                 find $DIR/$tfile
6758         EOF
6759
6760         result=$(grep error $TMP/$tfile.log)
6761         rm -f $DIR/$tfile
6762         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
6763 }
6764 run_test 128 "interactive lfs for 2 consecutive find's"
6765
6766 set_dir_limits () {
6767         local mntdev
6768         local canondev
6769         local node
6770
6771         local LDPROC=/proc/fs/ldiskfs
6772
6773         for facet in $(get_facets MDS); do
6774                 canondev=$(ldiskfs_canon *.$(convert_facet2label $facet).mntdev $facet)
6775                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" || LDPROC=/sys/fs/ldiskfs
6776                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
6777         done
6778 }
6779 test_129() {
6780         [ "$FSTYPE" != "ldiskfs" ] && skip "not needed for FSTYPE=$FSTYPE" && return 0
6781         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6782
6783         EFBIG=27
6784         MAX=16384
6785
6786         set_dir_limits $MAX
6787
6788         mkdir -p $DIR/$tdir
6789
6790         I=0
6791         J=0
6792         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
6793                 multiop $DIR/$tdir/$J Oc
6794                 rc=$?
6795                 if [ $rc -eq $EFBIG ]; then
6796                         set_dir_limits 0
6797                         echo "return code $rc received as expected"
6798                         return 0
6799                 elif [ $rc -ne 0 ]; then
6800                         set_dir_limits 0
6801                         error_exit "return code $rc received instead of expected $EFBIG"
6802                 fi
6803                 J=$((J+1))
6804                 I=$(stat -c%s "$DIR/$tdir")
6805         done
6806
6807         set_dir_limits 0
6808         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
6809 }
6810 run_test 129 "test directory size limit ========================"
6811
6812 OLDIFS="$IFS"
6813 cleanup_130() {
6814         trap 0
6815         IFS="$OLDIFS"
6816 }
6817
6818 test_130a() {
6819         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
6820         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
6821
6822         trap cleanup_130 EXIT RETURN
6823
6824         local fm_file=$DIR/$tfile
6825         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
6826         dd if=/dev/zero of=$fm_file bs=65536 count=1 || error "dd failed for $fm_file"
6827
6828         filefrag -ves $fm_file || error "filefrag $fm_file failed"
6829         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
6830
6831         lun=$($GETSTRIPE -i $fm_file)
6832
6833         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
6834         IFS=$'\n'
6835         tot_len=0
6836         for line in $filefrag_op
6837         do
6838                 frag_lun=`echo $line | cut -d: -f5`
6839                 ext_len=`echo $line | cut -d: -f4`
6840                 if (( $frag_lun != $lun )); then
6841                         cleanup_130
6842                         error "FIEMAP on 1-stripe file($fm_file) failed"
6843                         return
6844                 fi
6845                 (( tot_len += ext_len ))
6846         done
6847
6848         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
6849                 cleanup_130
6850                 error "FIEMAP on 1-stripe file($fm_file) failed;"
6851                 return
6852         fi
6853
6854         cleanup_130
6855
6856         echo "FIEMAP on single striped file succeeded"
6857 }
6858 run_test 130a "FIEMAP (1-stripe file)"
6859
6860 test_130b() {
6861         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping FIEMAP on 2-stripe file test" && return
6862
6863         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
6864         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
6865
6866         trap cleanup_130 EXIT RETURN
6867
6868         local fm_file=$DIR/$tfile
6869         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
6870         dd if=/dev/zero of=$fm_file bs=1M count=2 || error "dd failed on $fm_file"
6871
6872         filefrag -ves $fm_file || error "filefrag $fm_file failed"
6873         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
6874
6875         last_lun=`echo $filefrag_op | cut -d: -f5`
6876
6877         IFS=$'\n'
6878         tot_len=0
6879         num_luns=1
6880         for line in $filefrag_op
6881         do
6882                 frag_lun=`echo $line | cut -d: -f5`
6883                 ext_len=`echo $line | cut -d: -f4`
6884                 if (( $frag_lun != $last_lun )); then
6885                         if (( tot_len != 1024 )); then
6886                                 cleanup_130
6887                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
6888                                 return
6889                         else
6890                                 (( num_luns += 1 ))
6891                                 tot_len=0
6892                         fi
6893                 fi
6894                 (( tot_len += ext_len ))
6895                 last_lun=$frag_lun
6896         done
6897         if (( num_luns != 2 || tot_len != 1024 )); then
6898                 cleanup_130
6899                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
6900                 return
6901         fi
6902
6903         cleanup_130
6904
6905         echo "FIEMAP on 2-stripe file succeeded"
6906 }
6907 run_test 130b "FIEMAP (2-stripe file)"
6908
6909 test_130c() {
6910         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping FIEMAP on 2-stripe file with hole test" && return
6911
6912         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
6913         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
6914
6915         trap cleanup_130 EXIT RETURN
6916
6917         local fm_file=$DIR/$tfile
6918         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
6919         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
6920
6921         filefrag -ves $fm_file || error "filefrag $fm_file failed"
6922         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
6923
6924         last_lun=`echo $filefrag_op | cut -d: -f5`
6925
6926         IFS=$'\n'
6927         tot_len=0
6928         num_luns=1
6929         for line in $filefrag_op
6930         do
6931                 frag_lun=`echo $line | cut -d: -f5`
6932                 ext_len=`echo $line | cut -d: -f4`
6933                 if (( $frag_lun != $last_lun )); then
6934                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
6935                         if (( logical != 512 )); then
6936                                 cleanup_130
6937                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
6938                                 return
6939                         fi
6940                         if (( tot_len != 512 )); then
6941                                 cleanup_130
6942                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
6943                                 return
6944                         else
6945                                 (( num_luns += 1 ))
6946                                 tot_len=0
6947                         fi
6948                 fi
6949                 (( tot_len += ext_len ))
6950                 last_lun=$frag_lun
6951         done
6952         if (( num_luns != 2 || tot_len != 512 )); then
6953                 cleanup_130
6954                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
6955                 return
6956         fi
6957
6958         cleanup_130
6959
6960         echo "FIEMAP on 2-stripe file with hole succeeded"
6961 }
6962 run_test 130c "FIEMAP (2-stripe file with hole)"
6963
6964 test_130d() {
6965         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
6966
6967         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
6968         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
6969
6970         trap cleanup_130 EXIT RETURN
6971
6972         local fm_file=$DIR/$tfile
6973         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
6974         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
6975
6976         filefrag -ves $fm_file || error "filefrag $fm_file failed"
6977         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
6978
6979         last_lun=`echo $filefrag_op | cut -d: -f5`
6980
6981         IFS=$'\n'
6982         tot_len=0
6983         num_luns=1
6984         for line in $filefrag_op
6985         do
6986                 frag_lun=`echo $line | cut -d: -f5`
6987                 ext_len=`echo $line | cut -d: -f4`
6988                 if (( $frag_lun != $last_lun )); then
6989                         if (( tot_len != 1024 )); then
6990                                 cleanup_130
6991                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
6992                                 return
6993                         else
6994                                 (( num_luns += 1 ))
6995                                 tot_len=0
6996                         fi
6997                 fi
6998                 (( tot_len += ext_len ))
6999                 last_lun=$frag_lun
7000         done
7001         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7002                 cleanup_130
7003                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7004                 return
7005         fi
7006
7007         cleanup_130
7008
7009         echo "FIEMAP on N-stripe file succeeded"
7010 }
7011 run_test 130d "FIEMAP (N-stripe file)"
7012
7013 test_130e() {
7014         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7015
7016         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7017         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7018
7019         trap cleanup_130 EXIT RETURN
7020
7021         local fm_file=$DIR/$tfile
7022         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7023         NUM_BLKS=512
7024         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7025         for ((i = 0; i < $NUM_BLKS; i++))
7026         do
7027                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7028         done
7029
7030         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7031         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7032
7033         last_lun=`echo $filefrag_op | cut -d: -f5`
7034
7035         IFS=$'\n'
7036         tot_len=0
7037         num_luns=1
7038         for line in $filefrag_op
7039         do
7040                 frag_lun=`echo $line | cut -d: -f5`
7041                 ext_len=`echo $line | cut -d: -f4`
7042                 if (( $frag_lun != $last_lun )); then
7043                         if (( tot_len != $EXPECTED_LEN )); then
7044                                 cleanup_130
7045                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7046                                 return
7047                         else
7048                                 (( num_luns += 1 ))
7049                                 tot_len=0
7050                         fi
7051                 fi
7052                 (( tot_len += ext_len ))
7053                 last_lun=$frag_lun
7054         done
7055         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7056                 cleanup_130
7057                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7058                 return
7059         fi
7060
7061         cleanup_130
7062
7063         echo "FIEMAP with continuation calls succeeded"
7064 }
7065 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7066
7067 # Test for writev/readv
7068 test_131a() {
7069         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7070         error "writev test failed"
7071         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7072         error "readv failed"
7073         rm -f $DIR/$tfile
7074 }
7075 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7076
7077 test_131b() {
7078         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7079         error "append writev test failed"
7080         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7081         error "append writev test failed"
7082         rm -f $DIR/$tfile
7083 }
7084 run_test 131b "test append writev"
7085
7086 test_131c() {
7087         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7088         error "NOT PASS"
7089 }
7090 run_test 131c "test read/write on file w/o objects"
7091
7092 test_131d() {
7093         rwv -f $DIR/$tfile -w -n 1 1572864
7094         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7095         if [ "$NOB" != 1572864 ]; then
7096                 error "Short read filed: read $NOB bytes instead of 1572864"
7097         fi
7098         rm -f $DIR/$tfile
7099 }
7100 run_test 131d "test short read"
7101
7102 test_131e() {
7103         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7104         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7105         error "read hitting hole failed"
7106         rm -f $DIR/$tfile
7107 }
7108 run_test 131e "test read hitting hole"
7109
7110 get_ost_param() {
7111         local token=$1
7112         local gl_sum=0
7113         for node in $(osts_nodes); do
7114                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7115                 [ x$gl = x"" ] && gl=0
7116                 gl_sum=$((gl_sum + gl))
7117         done
7118         echo $gl
7119 }
7120
7121 som_mode_switch() {
7122         local som=$1
7123         local gl1=$2
7124         local gl2=$3
7125
7126         if [ x$som = x"enabled" ]; then
7127                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7128                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7129                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7130         else
7131                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7132                 MOUNTOPT="$MOUNTOPT,som_preview"
7133                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7134         fi
7135
7136         # do remount to make new mount-conf parameters actual
7137         echo remounting...
7138         sync
7139         stopall
7140         setupall
7141 }
7142
7143 test_132() { #1028, SOM
7144         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7145         local num=$(get_mds_dir $DIR)
7146         local mymds=mds${num}
7147         local MOUNTOPT_SAVE=$MOUNTOPT
7148
7149         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7150         cancel_lru_locks osc
7151
7152         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7153
7154         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7155         stat $DIR/$tfile >/dev/null
7156         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7157         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7158         rm $DIR/$tfile
7159         som_mode_switch $som1 $gl1 $gl2
7160
7161         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7162         cancel_lru_locks osc
7163
7164         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7165         if [ $som1 == $som2 ]; then
7166             error "som is still "$som2
7167             if [ x$som2 = x"enabled" ]; then
7168                 som2="disabled"
7169             else
7170                 som2="enabled"
7171             fi
7172         fi
7173
7174         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7175         stat $DIR/$tfile >/dev/null
7176         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7177         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7178         som_mode_switch $som2 $gl1 $gl2
7179         MOUNTOPT=$MOUNTOPT_SAVE
7180 }
7181 run_test 132 "som avoids glimpse rpc"
7182
7183 check_stats() {
7184         local res
7185         local count
7186         case $1 in
7187         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7188                  ;;
7189         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7190                  ;;
7191         *) error "Wrong argument $1" ;;
7192         esac
7193         echo $res
7194         count=`echo $res | awk '{print $2}'`
7195         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7196         # if the argument $3 is zero, it means any stat increment is ok.
7197         if [ $3 -gt 0 ] ; then
7198                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7199         fi
7200 }
7201
7202 test_133a() {
7203         remote_ost_nodsh && skip "remote OST with nodsh" && return
7204         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7205         local testdir=$DIR/${tdir}/stats_testdir
7206         mkdir -p $DIR/${tdir}
7207
7208         # clear stats.
7209         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7210         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7211
7212         # verify mdt stats first.
7213         mkdir ${testdir} || error "mkdir failed"
7214         check_stats $SINGLEMDS "mkdir" 1
7215         touch ${testdir}/${tfile} || "touch failed"
7216         check_stats $SINGLEMDS "open" 1
7217         check_stats $SINGLEMDS "close" 1
7218         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7219         check_stats $SINGLEMDS "mknod" 1
7220         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7221         check_stats $SINGLEMDS "unlink" 1
7222         rm -f ${testdir}/${tfile} || error "file remove failed"
7223         check_stats $SINGLEMDS "unlink" 2
7224
7225         # remove working dir and check mdt stats again.
7226         rmdir ${testdir} || error "rmdir failed"
7227         check_stats $SINGLEMDS "rmdir" 1
7228
7229         local testdir1=$DIR/${tdir}/stats_testdir1
7230         mkdir -p ${testdir}
7231         mkdir -p ${testdir1}
7232         touch ${testdir1}/test1
7233         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7234         check_stats $SINGLEMDS "crossdir_rename" 1
7235
7236         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
7237         check_stats $SINGLEMDS "samedir_rename" 1
7238
7239         rm -rf $DIR/${tdir}
7240 }
7241 run_test 133a "Verifying MDT stats ========================================"
7242
7243 test_133b() {
7244         remote_ost_nodsh && skip "remote OST with nodsh" && return
7245         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7246         local testdir=$DIR/${tdir}/stats_testdir
7247         mkdir -p ${testdir} || error "mkdir failed"
7248         touch ${testdir}/${tfile} || "touch failed"
7249         cancel_lru_locks mdc
7250
7251         # clear stats.
7252         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7253         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7254
7255         # extra mdt stats verification.
7256         chmod 444 ${testdir}/${tfile} || error "chmod failed"
7257         check_stats $SINGLEMDS "setattr" 1
7258         $LFS df || error "lfs failed"
7259         check_stats $SINGLEMDS "statfs" 1
7260
7261         rm -rf $DIR/${tdir}
7262 }
7263 run_test 133b "Verifying extra MDT stats =================================="
7264
7265 test_133c() {
7266         remote_ost_nodsh && skip "remote OST with nodsh" && return
7267         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7268         local testdir=$DIR/${tdir}/stats_testdir
7269         mkdir -p ${testdir} || error "mkdir failed"
7270
7271         # verify obdfilter stats.
7272         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
7273         sync
7274         cancel_lru_locks osc
7275
7276         # clear stats.
7277         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7278         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7279
7280         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
7281         sync
7282         cancel_lru_locks osc
7283         check_stats ost "write" 1
7284
7285         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
7286         check_stats ost "read" 1
7287
7288         > ${testdir}/${tfile} || error "truncate failed"
7289         check_stats ost "punch" 1
7290
7291         rm -f ${testdir}/${tfile} || error "file remove failed"
7292         check_stats ost "destroy" 1
7293
7294         rm -rf $DIR/${tdir}
7295 }
7296 run_test 133c "Verifying OST stats ========================================"
7297
7298 order_2() {
7299     local value=$1
7300     local orig=$value
7301     local order=1
7302
7303     while [ $value -ge 2 ]; do
7304         order=$((order*2))
7305         value=$((value/2))
7306     done
7307
7308     if [ $orig -gt $order ]; then
7309         order=$((order*2))
7310     fi
7311     echo $order
7312 }
7313
7314 size_in_KMGT() {
7315     local value=$1
7316     local size=('K' 'M' 'G' 'T');
7317     local i=0
7318     local size_string=$value
7319
7320     while [ $value -ge 1024 ]; do
7321         if [ $i -gt 3 ]; then
7322             #T is the biggest unit we get here, if that is bigger,
7323             #just return XXXT
7324             size_string=${value}T
7325             break
7326         fi
7327         value=$((value >> 10))
7328         if [ $value -lt 1024 ]; then
7329             size_string=${value}${size[$i]}
7330             break
7331         fi
7332         i=$((i + 1))
7333     done
7334
7335     echo $size_string
7336 }
7337
7338 get_rename_size() {
7339     local size=$1
7340     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats | \
7341                    awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
7342     echo $sample
7343 }
7344
7345 test_133d() {
7346     remote_ost_nodsh && skip "remote OST with nodsh" && return
7347     remote_mds_nodsh && skip "remote MDS with nodsh" && return
7348     local testdir1=$DIR/${tdir}/stats_testdir1
7349     local testdir2=$DIR/${tdir}/stats_testdir2
7350
7351     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7352
7353     mkdir -p ${testdir1} || error "mkdir failed"
7354     mkdir -p ${testdir2} || error "mkdir failed"
7355
7356     createmany -o $testdir1/test 512 || error "createmany failed"
7357     local testdir1_size=$(ls -l $DIR/${tdir} | \
7358                           awk '/stats_testdir1/ {print $5}')
7359     local testdir2_size=$(ls -l $DIR/${tdir} | \
7360                           awk '/stats_testdir2/ {print $5}')
7361
7362     testdir1_size=$(order_2 $testdir1_size)
7363     testdir2_size=$(order_2 $testdir2_size)
7364
7365     testdir1_size=$(size_in_KMGT $testdir1_size)
7366     testdir2_size=$(size_in_KMGT $testdir2_size)
7367
7368     # check samedir rename size
7369     mv ${testdir1}/test0 ${testdir1}/test_0
7370     local samedir=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats | \
7371                     grep 'same_dir')
7372     local same_sample=$(get_rename_size $testdir1_size)
7373     [ -z "$samedir" ] && error "samedir_rename_size count error"
7374     [ $same_sample -eq 1 ] || error "samedir_rename_size count error"
7375     echo "Check same dir rename stats success"
7376
7377     # check crossdir rename size
7378     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7379     mv ${testdir1}/test_0 ${testdir2}/test_0
7380     local crossdir=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats | \
7381                      grep 'crossdir')
7382     local src_sample=$(get_rename_size $testdir1_size)
7383     local tgt_sample=$(get_rename_size $testdir2_size)
7384     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
7385     [ $src_sample -eq 1 ] || error "crossdir_rename_size count error"
7386     [ $tgt_sample -eq 1 ] || error "crossdir_rename_size count error"
7387     echo "Check cross dir rename stats success"
7388
7389     rm -rf $DIR/${tdir}
7390 }
7391 run_test 133d "Verifying rename_stats ========================================"
7392
7393 test_140() { #bug-17379
7394         mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
7395         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
7396         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
7397
7398         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
7399         local i=0
7400         while i=`expr $i + 1`; do
7401                 mkdir -p $i || error "Creating dir $i"
7402                 cd $i || error "Changing to $i"
7403                 ln -s ../stat stat || error "Creating stat symlink"
7404                 # Read the symlink until ELOOP present,
7405                 # not LBUGing the system is considered success,
7406                 # we didn't overrun the stack.
7407                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
7408                 [ $ret -ne 0 ] && {
7409                         if [ $ret -eq 40 ]; then
7410                                 break  # -ELOOP
7411                         else
7412                                 error "Open stat symlink"
7413                                 return
7414                         fi
7415                 }
7416         done
7417         i=`expr $i - 1`
7418         echo "The symlink depth = $i"
7419         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 ] || error "Invalid symlink depth"
7420 }
7421 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
7422
7423 test_150() {
7424         local TF="$TMP/$tfile"
7425
7426         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7427         cp $TF $DIR/$tfile
7428         cancel_lru_locks osc
7429         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
7430         remount_client $MOUNT
7431         df -P $MOUNT
7432         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
7433
7434         $TRUNCATE $TF 6000
7435         $TRUNCATE $DIR/$tfile 6000
7436         cancel_lru_locks osc
7437         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
7438
7439         echo "12345" >>$TF
7440         echo "12345" >>$DIR/$tfile
7441         cancel_lru_locks osc
7442         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
7443
7444         echo "12345" >>$TF
7445         echo "12345" >>$DIR/$tfile
7446         cancel_lru_locks osc
7447         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
7448
7449         rm -f $TF
7450         true
7451 }
7452 run_test 150 "truncate/append tests"
7453
7454 function roc_hit() {
7455     local list=$(comma_list $(osts_nodes))
7456
7457     ACCNUM=$(do_nodes $list $LCTL get_param -n obdfilter.*.stats | \
7458         awk '/'cache_hit'/ {sum+=$2} END {print sum}')
7459     echo $ACCNUM
7460 }
7461
7462 function set_cache() {
7463     local on=1
7464
7465     if [ "$2" == "off" ]; then
7466         on=0;
7467     fi
7468     local list=$(comma_list $(osts_nodes))
7469     do_nodes $list lctl set_param obdfilter.*.${1}_cache_enable $on
7470
7471     cancel_lru_locks osc
7472 }
7473
7474 test_151() {
7475         remote_ost_nodsh && skip "remote OST with nodsh" && return
7476
7477         local CPAGES=3
7478         local list=$(comma_list $(osts_nodes))
7479
7480         # check whether obdfilter is cache capable at all
7481         if ! do_nodes $list $LCTL get_param -n obdfilter.*.read_cache_enable > /dev/null; then
7482                 echo "not cache-capable obdfilter"
7483                 return 0
7484         fi
7485
7486         # check cache is enabled on all obdfilters
7487         if do_nodes $list $LCTL get_param -n obdfilter.*.read_cache_enable | grep 0 >&/dev/null; then
7488                 echo "oss cache is disabled"
7489                 return 0
7490         fi
7491
7492         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable 1
7493
7494         # pages should be in the case right after write
7495         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
7496         local BEFORE=`roc_hit`
7497         cancel_lru_locks osc
7498         cat $DIR/$tfile >/dev/null
7499         local AFTER=`roc_hit`
7500         if ! let "AFTER - BEFORE == CPAGES"; then
7501                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7502         fi
7503
7504         # the following read invalidates the cache
7505         cancel_lru_locks osc
7506         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable 0
7507         cat $DIR/$tfile >/dev/null
7508
7509         # now data shouldn't be found in the cache
7510         BEFORE=`roc_hit`
7511         cancel_lru_locks osc
7512         cat $DIR/$tfile >/dev/null
7513         AFTER=`roc_hit`
7514         if let "AFTER - BEFORE != 0"; then
7515                 error "IN CACHE: before: $BEFORE, after: $AFTER"
7516         fi
7517
7518         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable 1
7519         rm -f $DIR/$tfile
7520 }
7521 run_test 151 "test cache on oss and controls ==============================="
7522
7523 test_152() {
7524         local TF="$TMP/$tfile"
7525
7526         # simulate ENOMEM during write
7527 #define OBD_FAIL_OST_NOMEM      0x226
7528         lctl set_param fail_loc=0x80000226
7529         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7530         cp $TF $DIR/$tfile
7531         sync || error "sync failed"
7532         lctl set_param fail_loc=0
7533
7534         # discard client's cache
7535         cancel_lru_locks osc
7536
7537         # simulate ENOMEM during read
7538         lctl set_param fail_loc=0x80000226
7539         cmp $TF $DIR/$tfile || error "cmp failed"
7540         lctl set_param fail_loc=0
7541
7542         rm -f $TF
7543 }
7544 run_test 152 "test read/write with enomem ============================"
7545
7546 test_153() {
7547         multiop $DIR/$tfile Ow4096Ycu || error "multiop failed"
7548 }
7549 run_test 153 "test if fdatasync does not crash ======================="
7550
7551 test_154() {
7552         cp /etc/hosts $DIR/$tfile
7553
7554         fid=$($LFS path2fid $DIR/$tfile)
7555         rc=$?
7556         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7557
7558         ffid=$DIR/.lustre/fid/$fid
7559
7560         echo "stat fid $fid"
7561         stat $ffid > /dev/null || error "stat $ffid failed."
7562         echo "touch fid $fid"
7563         touch $ffid || error "touch $ffid failed."
7564         echo "write to fid $fid"
7565         cat /etc/hosts > $ffid || error "write $ffid failed."
7566         echo "read fid $fid"
7567         diff /etc/hosts $ffid || error "read $ffid failed."
7568         echo "append write to fid $fid"
7569         cat /etc/hosts >> $ffid || error "append write $ffid failed."
7570         echo "rename fid $fid"
7571         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
7572         touch $DIR/$tfile.1
7573         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
7574         rm -f $DIR/$tfile.1
7575         echo "truncate fid $fid"
7576         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
7577         echo "link fid $fid"
7578         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
7579         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
7580                 echo "setfacl fid $fid"
7581                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
7582                 echo "getfacl fid $fid"
7583                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
7584         fi
7585         echo "unlink fid $fid"
7586         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
7587         echo "mknod fid $fid"
7588         mknod $ffid c 1 3 && error "mknod $ffid should fail."
7589
7590         fid=[0xf00000400:0x1:0x0]
7591         ffid=$DIR/.lustre/fid/$fid
7592
7593         echo "stat non-exist fid $fid"
7594         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
7595         echo "write to non-exist fid $fid"
7596         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
7597         echo "link new fid $fid"
7598         ln $DIR/$tfile $ffid && error "link $ffid should fail."
7599
7600         mkdir -p $DIR/$tdir
7601         touch $DIR/$tdir/$tfile
7602         fid=$($LFS path2fid $DIR/$tdir)
7603         rc=$?
7604         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7605
7606         ffid=$DIR/.lustre/fid/$fid
7607
7608         echo "ls $fid"
7609         ls $ffid > /dev/null || error "ls $ffid failed."
7610         echo "touch $fid/$tfile.1"
7611         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
7612
7613         echo "touch $DIR/.lustre/fid/$tfile"
7614         touch $DIR/.lustre/fid/$tfile && \
7615                 error "touch $DIR/.lustre/fid/$tfile should fail."
7616
7617         echo "Open-by-FID succeeded"
7618 }
7619 run_test 154 "Open-by-FID"
7620
7621 test_155_small_load() {
7622     local temp=$TMP/$tfile
7623     local file=$DIR/$tfile
7624
7625     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
7626         error "dd of=$temp bs=6096 count=1 failed"
7627     cp $temp $file
7628     cancel_lru_locks osc
7629     cmp $temp $file || error "$temp $file differ"
7630
7631     $TRUNCATE $temp 6000
7632     $TRUNCATE $file 6000
7633     cmp $temp $file || error "$temp $file differ (truncate1)"
7634
7635     echo "12345" >>$temp
7636     echo "12345" >>$file
7637     cmp $temp $file || error "$temp $file differ (append1)"
7638
7639     echo "12345" >>$temp
7640     echo "12345" >>$file
7641     cmp $temp $file || error "$temp $file differ (append2)"
7642
7643     rm -f $temp $file
7644     true
7645 }
7646
7647 test_155_big_load() {
7648     remote_ost_nodsh && skip "remote OST with nodsh" && return
7649     local temp=$TMP/$tfile
7650     local file=$DIR/$tfile
7651
7652     free_min_max
7653     local cache_size=$(do_facet ost$((MAXI+1)) \
7654         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
7655     local large_file_size=$((cache_size * 2))
7656
7657     echo "OSS cache size: $cache_size KB"
7658     echo "Large file size: $large_file_size KB"
7659
7660     [ $MAXV -le $large_file_size ] && \
7661         skip_env "max available OST size needs > $large_file_size KB" && \
7662         return 0
7663
7664     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
7665
7666     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
7667         error "dd of=$temp bs=$large_file_size count=1k failed"
7668     cp $temp $file
7669     ls -lh $temp $file
7670     cancel_lru_locks osc
7671     cmp $temp $file || error "$temp $file differ"
7672
7673     rm -f $temp $file
7674     true
7675 }
7676
7677 test_155a() {
7678     set_cache read on
7679     set_cache writethrough on
7680     test_155_small_load
7681 }
7682 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
7683
7684 test_155b() {
7685     set_cache read on
7686     set_cache writethrough off
7687     test_155_small_load
7688 }
7689 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
7690
7691 test_155c() {
7692     set_cache read off
7693     set_cache writethrough on
7694     test_155_small_load
7695 }
7696 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
7697
7698 test_155d() {
7699     set_cache read off
7700     set_cache writethrough off
7701     test_155_small_load
7702 }
7703 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
7704
7705 test_155e() {
7706     set_cache read on
7707     set_cache writethrough on
7708     test_155_big_load
7709 }
7710 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
7711
7712 test_155f() {
7713     set_cache read on
7714     set_cache writethrough off
7715     test_155_big_load
7716 }
7717 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
7718
7719 test_155g() {
7720     set_cache read off
7721     set_cache writethrough on
7722     test_155_big_load
7723 }
7724 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
7725
7726 test_155h() {
7727     set_cache read off
7728     set_cache writethrough off
7729     test_155_big_load
7730 }
7731 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
7732
7733 test_156() {
7734     local CPAGES=3
7735     local BEFORE
7736     local AFTER
7737     local file="$DIR/$tfile"
7738
7739     log "Turn on read and write cache"
7740     set_cache read on
7741     set_cache writethrough on
7742
7743     log "Write data and read it back."
7744     log "Read should be satisfied from the cache."
7745     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
7746     BEFORE=`roc_hit`
7747     cancel_lru_locks osc
7748     cat $file >/dev/null
7749     AFTER=`roc_hit`
7750     if ! let "AFTER - BEFORE == CPAGES"; then
7751         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7752     else
7753         log "cache hits:: before: $BEFORE, after: $AFTER"
7754     fi
7755
7756     log "Read again; it should be satisfied from the cache."
7757     BEFORE=$AFTER
7758     cancel_lru_locks osc
7759     cat $file >/dev/null
7760     AFTER=`roc_hit`
7761     if ! let "AFTER - BEFORE == CPAGES"; then
7762         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7763     else
7764         log "cache hits:: before: $BEFORE, after: $AFTER"
7765     fi
7766
7767
7768     log "Turn off the read cache and turn on the write cache"
7769     set_cache read off
7770     set_cache writethrough on
7771
7772     log "Read again; it should be satisfied from the cache."
7773     BEFORE=`roc_hit`
7774     cancel_lru_locks osc
7775     cat $file >/dev/null
7776     AFTER=`roc_hit`
7777     if ! let "AFTER - BEFORE == CPAGES"; then
7778         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7779     else
7780         log "cache hits:: before: $BEFORE, after: $AFTER"
7781     fi
7782
7783     log "Read again; it should not be satisfied from the cache."
7784     BEFORE=$AFTER
7785     cancel_lru_locks osc
7786     cat $file >/dev/null
7787     AFTER=`roc_hit`
7788     if ! let "AFTER - BEFORE == 0"; then
7789         error "IN CACHE: before: $BEFORE, after: $AFTER"
7790     else
7791         log "cache hits:: before: $BEFORE, after: $AFTER"
7792     fi
7793
7794     log "Write data and read it back."
7795     log "Read should be satisfied from the cache."
7796     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
7797     BEFORE=`roc_hit`
7798     cancel_lru_locks osc
7799     cat $file >/dev/null
7800     AFTER=`roc_hit`
7801     if ! let "AFTER - BEFORE == CPAGES"; then
7802         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7803     else
7804         log "cache hits:: before: $BEFORE, after: $AFTER"
7805     fi
7806
7807     log "Read again; it should not be satisfied from the cache."
7808     BEFORE=$AFTER
7809     cancel_lru_locks osc
7810     cat $file >/dev/null
7811     AFTER=`roc_hit`
7812     if ! let "AFTER - BEFORE == 0"; then
7813         error "IN CACHE: before: $BEFORE, after: $AFTER"
7814     else
7815         log "cache hits:: before: $BEFORE, after: $AFTER"
7816     fi
7817
7818
7819     log "Turn off read and write cache"
7820     set_cache read off
7821     set_cache writethrough off
7822
7823     log "Write data and read it back"
7824     log "It should not be satisfied from the cache."
7825     rm -f $file
7826     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
7827     cancel_lru_locks osc
7828     BEFORE=`roc_hit`
7829     cat $file >/dev/null
7830     AFTER=`roc_hit`
7831     if ! let "AFTER - BEFORE == 0"; then
7832         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
7833     else
7834         log "cache hits:: before: $BEFORE, after: $AFTER"
7835     fi
7836
7837
7838     log "Turn on the read cache and turn off the write cache"
7839     set_cache read on
7840     set_cache writethrough off
7841
7842     log "Write data and read it back"
7843     log "It should not be satisfied from the cache."
7844     rm -f $file
7845     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
7846     BEFORE=`roc_hit`
7847     cancel_lru_locks osc
7848     cat $file >/dev/null
7849     AFTER=`roc_hit`
7850     if ! let "AFTER - BEFORE == 0"; then
7851         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
7852     else
7853         log "cache hits:: before: $BEFORE, after: $AFTER"
7854     fi
7855
7856     log "Read again; it should be satisfied from the cache."
7857     BEFORE=`roc_hit`
7858     cancel_lru_locks osc
7859     cat $file >/dev/null
7860     AFTER=`roc_hit`
7861     if ! let "AFTER - BEFORE == CPAGES"; then
7862         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7863     else
7864         log "cache hits:: before: $BEFORE, after: $AFTER"
7865     fi
7866
7867     rm -f $file
7868 }
7869 run_test 156 "Verification of tunables ============================"
7870
7871 #Changelogs
7872 err17935 () {
7873     if [ $MDSCOUNT -gt 1 ]; then
7874         error_ignore 17935 $*
7875     else
7876         error $*
7877     fi
7878 }
7879
7880 changelog_chmask()
7881 {
7882     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
7883            grep -c $1)
7884
7885     if [ $MASK -eq 1 ]; then
7886         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
7887     else
7888         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
7889     fi
7890 }
7891
7892 test_160() {
7893     remote_mds_nodsh && skip "remote MDS with nodsh" && return
7894     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
7895     echo "Registered as changelog user $USER"
7896     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
7897         grep -q $USER || error "User $USER not found in changelog_users"
7898
7899     # change something
7900     mkdir -p $DIR/$tdir/pics/2008/zachy
7901     touch $DIR/$tdir/pics/2008/zachy/timestamp
7902     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
7903     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
7904     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
7905     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
7906     rm $DIR/$tdir/pics/desktop.jpg
7907
7908     $LFS changelog $MDT0 | tail -5
7909
7910     echo "verifying changelog mask"
7911     changelog_chmask "MKDIR"
7912     changelog_chmask "CLOSE"
7913
7914     mkdir -p $DIR/$tdir/pics/zach/sofia
7915     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
7916
7917     changelog_chmask "MKDIR"
7918     changelog_chmask "CLOSE"
7919
7920     mkdir -p $DIR/$tdir/pics/2008/sofia
7921     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
7922
7923     $LFS changelog $MDT0
7924     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
7925     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
7926     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
7927     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
7928
7929     # verify contents
7930     echo "verifying target fid"
7931     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
7932         tail -1 | awk '{print $6}')
7933     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
7934     [ "$fidc" == "t=$fidf" ] || \
7935         err17935 "fid in changelog $fidc != file fid $fidf"
7936     echo "verifying parent fid"
7937     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
7938         tail -1 | awk '{print $7}')
7939     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
7940     [ "$fidc" == "p=$fidf" ] || \
7941         err17935 "pfid in changelog $fidc != dir fid $fidf"
7942
7943     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
7944         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
7945     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
7946     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
7947         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
7948     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
7949     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
7950         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
7951
7952     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
7953         awk 'min == "" || $2 < min {min = $2}; END {print min}')
7954     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
7955     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
7956     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
7957         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
7958
7959     echo "verifying user deregister"
7960     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
7961     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
7962         grep -q $USER && error "User $USER still found in changelog_users"
7963
7964     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
7965         mdd.$MDT0.changelog_users | wc -l) - 2 ))
7966     if [ $USERS -eq 0 ]; then
7967         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
7968             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
7969         touch $DIR/$tdir/chloe
7970         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
7971             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
7972         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
7973         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
7974     else
7975         echo "$USERS other changelog users; can't verify off"
7976     fi
7977 }
7978 run_test 160 "changelog sanity"
7979
7980 test_161() {
7981     mkdir -p $DIR/$tdir
7982     cp /etc/hosts $DIR/$tdir/$tfile
7983     mkdir $DIR/$tdir/foo1
7984     mkdir $DIR/$tdir/foo2
7985     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
7986     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
7987     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
7988     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
7989     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
7990     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
7991         $LFS fid2path $DIR $FID
7992         err17935 "bad link ea"
7993     fi
7994     # middle
7995     rm $DIR/$tdir/foo2/zachary
7996     # last
7997     rm $DIR/$tdir/foo2/thor
7998     # first
7999     rm $DIR/$tdir/$tfile
8000     # rename
8001     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8002     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8003         then
8004         $LFS fid2path $DIR $FID
8005         err17935 "bad link rename"
8006     fi
8007     rm $DIR/$tdir/foo2/maggie
8008
8009     # overflow the EA
8010     local longname=filename_avg_len_is_thirty_two_
8011     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8012         error "failed to hardlink many files"
8013     links=$($LFS fid2path $DIR $FID | wc -l)
8014     echo -n "${links}/1000 links in link EA"
8015     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8016     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8017         error "failed to unlink many hardlinks"
8018 }
8019 run_test 161 "link ea sanity"
8020
8021 check_path() {
8022     local expected=$1
8023     shift
8024     local fid=$2
8025
8026     local path=$(${LFS} fid2path $*)
8027     RC=$?
8028
8029     if [ $RC -ne 0 ]; then
8030         err17935 "path looked up of $expected failed. Error $RC"
8031         return $RC
8032     elif [ "${path}" != "${expected}" ]; then
8033         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8034         return 2
8035     fi
8036     echo "fid $fid resolves to path $path (expected $expected)"
8037 }
8038
8039 test_162() {
8040         # Make changes to filesystem
8041         mkdir -p $DIR/$tdir/d2
8042         touch $DIR/$tdir/d2/$tfile
8043         touch $DIR/$tdir/d2/x1
8044         touch $DIR/$tdir/d2/x2
8045         mkdir -p $DIR/$tdir/d2/a/b/c
8046         mkdir -p $DIR/$tdir/d2/p/q/r
8047         # regular file
8048         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8049         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8050
8051         # softlink
8052         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8053         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8054         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8055
8056         # softlink to wrong file
8057         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8058         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8059         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8060
8061         # hardlink
8062         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8063         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8064         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8065         # fid2path dir/fsname should both work
8066         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8067         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8068
8069         # hardlink count: check that there are 2 links
8070         # Doesnt work with CMD yet: 17935
8071         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8072                 err17935 "expected 2 links"
8073
8074         # hardlink indexing: remove the first link
8075         rm $DIR/$tdir/d2/p/q/r/hlink
8076         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8077
8078         return 0
8079 }
8080 run_test 162 "path lookup sanity"
8081
8082 test_163() {
8083         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8084         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
8085         copytool $FSNAME &
8086         sleep 1
8087         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
8088         # this proc file is temporary and linux-only
8089         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
8090          error "kernel->userspace send failed"
8091         kill -INT $!
8092 }
8093 run_test 163 "kernel <-> userspace comms"
8094
8095 test_169() {
8096         # do directio so as not to populate the page cache
8097         log "creating a 10 Mb file"
8098         multiop $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8099         log "starting reads"
8100         dd if=$DIR/$tfile of=/dev/null bs=4096 &
8101         log "truncating the file"
8102         multiop $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8103         log "killing dd"
8104         kill %+ || true # reads might have finished
8105         echo "wait until dd is finished"
8106         wait
8107         log "removing the temporary file"
8108         rm -rf $DIR/$tfile || error "tmp file removal failed"
8109 }
8110 run_test 169 "parallel read and truncate should not deadlock"
8111
8112 test_170() {
8113         $LCTL clear     # bug 18514
8114         $LCTL debug_daemon start $TMP/${tfile}_log_good
8115         touch $DIR/$tfile
8116         $LCTL debug_daemon stop
8117         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
8118                error "sed failed to read log_good"
8119
8120         $LCTL debug_daemon start $TMP/${tfile}_log_good
8121         rm -rf $DIR/$tfile
8122         $LCTL debug_daemon stop
8123
8124         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
8125                error "lctl df log_bad failed"
8126
8127         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8128         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8129
8130         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
8131         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
8132
8133         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
8134                 error "bad_line good_line1 good_line2 are empty"
8135
8136         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8137         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
8138         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8139
8140         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
8141         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8142         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8143
8144         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
8145                 error "bad_line_new good_line_new are empty"
8146
8147         local expected_good=$((good_line1 + good_line2*2))
8148
8149         rm -f $TMP/${tfile}*
8150         # LU-231, short malformed line may not be counted into bad lines
8151         if [ $bad_line -ne $bad_line_new ] &&
8152                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
8153                 error "expected $bad_line bad lines, but got $bad_line_new"
8154                 return 1
8155         fi
8156
8157         if [ $expected_good -ne $good_line_new ]; then
8158                 error "expected $expected_good good lines, but got $good_line_new"
8159                 return 2
8160         fi
8161         true
8162 }
8163 run_test 170 "test lctl df to handle corrupted log ====================="
8164
8165 test_171() { # bug20592
8166 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8167         $LCTL set_param fail_loc=0x50e
8168         $LCTL set_param fail_val=3000
8169         multiop_bg_pause $DIR/$tfile O_s || true
8170         local MULTIPID=$!
8171         kill -USR1 $MULTIPID
8172         # cause log dump
8173         sleep 3
8174         wait $MULTIPID
8175         if dmesg | grep "recursive fault"; then
8176                 error "caught a recursive fault"
8177         fi
8178         $LCTL set_param fail_loc=0
8179         true
8180 }
8181 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
8182
8183 # it would be good to share it with obdfilter-survey/libecho code
8184 setup_obdecho_osc () {
8185         local rc=0
8186         local ost_nid=$1
8187         local obdfilter_name=$2
8188         echo "Creating new osc for $obdfilter_name on $ost_nid"
8189         # make sure we can find loopback nid
8190         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
8191
8192         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
8193                            ${obdfilter_name}_osc_UUID || rc=2; }
8194         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
8195                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
8196         return $rc
8197 }
8198
8199 cleanup_obdecho_osc () {
8200         local obdfilter_name=$1
8201         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
8202         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
8203         return 0
8204 }
8205
8206 obdecho_create_test() {
8207         local OBD=$1
8208         local node=$2
8209         local rc=0
8210         local id
8211         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
8212         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
8213                            rc=2; }
8214         if [ $rc -eq 0 ]; then
8215             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
8216             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
8217         fi
8218         echo "New object id is $id"
8219         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
8220                            rc=4; }
8221         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
8222                                         "cleanup" || rc=5; }
8223         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
8224                                         "detach" || rc=6; }
8225         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
8226         return $rc
8227 }
8228
8229 test_180a() {
8230         remote_ost_nodsh && skip "remote OST with nodsh" && return
8231         local rc=0
8232         local rmmod_local=0
8233
8234         if ! module_loaded obdecho; then
8235             load_module obdecho/obdecho
8236             rmmod_local=1
8237         fi
8238
8239         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
8240         local host=$(lctl get_param -n osc.$osc.import |
8241                              awk '/current_connection:/ {print $2}' )
8242         local target=$(lctl get_param -n osc.$osc.import |
8243                              awk '/target:/ {print $2}' )
8244         target=${target%_UUID}
8245
8246         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
8247         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
8248         [[ -n $target ]] && cleanup_obdecho_osc $target
8249         [ $rmmod_local -eq 1 ] && rmmod obdecho
8250         return $rc
8251 }
8252 run_test 180a "test obdecho on osc"
8253
8254 test_180b() {
8255         remote_ost_nodsh && skip "remote OST with nodsh" && return
8256         local rc=0
8257         local rmmod_remote=0
8258
8259         do_facet ost1 "lsmod | grep -q obdecho || "                      \
8260                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
8261                       "modprobe obdecho; }" && rmmod_remote=1
8262         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
8263         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
8264         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
8265         return $rc
8266 }
8267 run_test 180b "test obdecho directly on obdfilter"
8268
8269 test_181() { # bug 22177
8270         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8271         # create enough files to index the directory
8272         createmany -o $DIR/$tdir/foobar 4000
8273         # print attributes for debug purpose
8274         lsattr -d .
8275         # open dir
8276         multiop_bg_pause $DIR/$tdir D_Sc || return 1
8277         MULTIPID=$!
8278         # remove the files & current working dir
8279         unlinkmany $DIR/$tdir/foobar 4000
8280         rmdir $DIR/$tdir
8281         kill -USR1 $MULTIPID
8282         wait $MULTIPID
8283         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
8284         return 0
8285 }
8286 run_test 181 "Test open-unlinked dir ========================"
8287
8288 # OST pools tests
8289 POOL=${POOL:-cea1}
8290 TGT_COUNT=$OSTCOUNT
8291 TGTPOOL_FIRST=1
8292 TGTPOOL_MAX=$(($TGT_COUNT - 1))
8293 TGTPOOL_STEP=2
8294 TGTPOOL_LIST=`seq $TGTPOOL_FIRST $TGTPOOL_STEP $TGTPOOL_MAX`
8295 POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
8296 POOL_DIR_NAME=dir_tst
8297 POOL_DIR=$POOL_ROOT/$POOL_DIR_NAME
8298 POOL_FILE=$POOL_ROOT/file_tst
8299
8300 check_file_in_pool()
8301 {
8302         file=$1
8303         res=$($GETSTRIPE $file | grep 0x | cut -f2)
8304         for i in $res
8305         do
8306                 found=$(echo :$TGTPOOL_LIST: | tr " " ":"  | grep :$i:)
8307                 if [[ "$found" == "" ]]
8308                 then
8309                         echo "pool list: $TGTPOOL_LIST"
8310                         echo "striping: $res"
8311                         error "$file not allocated in $POOL"
8312                         return 1
8313                 fi
8314         done
8315         return 0
8316 }
8317
8318 trap "cleanup_pools $FSNAME" EXIT
8319
8320 test_200a() {
8321         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8322     create_pool $FSNAME.$POOL || return $?
8323         [ $($LFS pool_list $FSNAME | grep -c $POOL) -eq 1 ] ||
8324                 error "$POOL not in lfs pool_list"
8325 }
8326 run_test 200a "Create new pool =========================================="
8327
8328 test_200b() {
8329         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8330         TGT=$(for i in $TGTPOOL_LIST; do printf "$FSNAME-OST%04x_UUID " $i; done)
8331         do_facet mgs $LCTL pool_add $FSNAME.$POOL \
8332                 $FSNAME-OST[$TGTPOOL_FIRST-$TGTPOOL_MAX/$TGTPOOL_STEP]
8333         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL | sort -u | tr '\n' ' ' " "$TGT" ||
8334                 error "Add to pool failed"
8335         local lfscount=$($LFS pool_list $FSNAME.$POOL | grep -c "\-OST")
8336         local addcount=$((($TGTPOOL_MAX - $TGTPOOL_FIRST) / $TGTPOOL_STEP + 1))
8337         [ $lfscount -eq $addcount ] ||
8338                 error "lfs pool_list bad ost count $lfscount != $addcount"
8339 }
8340 run_test 200b "Add targets to a pool ===================================="
8341
8342 test_200c() {
8343         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8344         mkdir -p $POOL_DIR
8345         $SETSTRIPE -c 2 -p $POOL $POOL_DIR
8346         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR"
8347         # b-19919 test relative path works well
8348         mkdir -p $POOL_DIR/$POOL_DIR_NAME
8349         cd $POOL_DIR
8350         $SETSTRIPE -c 2 -p $POOL $POOL_DIR_NAME
8351         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/$POOL_DIR_NAME"
8352         $SETSTRIPE -c 2 -p $POOL ./$POOL_DIR_NAME
8353         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/./$POOL_DIR_NAME"
8354         $SETSTRIPE -c 2 -p $POOL ../$POOL_DIR_NAME
8355         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/../$POOL_DIR_NAME"
8356         $SETSTRIPE -c 2 -p $POOL ../$POOL_DIR_NAME/$POOL_DIR_NAME
8357         [ $? = 0 ] || error "Cannot set pool $POOL to $POOL_DIR/../$POOL_DIR_NAME/$POOL_DIR_NAME"
8358         rm -rf $POOL_DIR_NAME; cd -
8359 }
8360 run_test 200c "Set pool on a directory ================================="
8361
8362 test_200d() {
8363         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8364         res=$($GETSTRIPE --pool $POOL_DIR)
8365         [ $res = $POOL ] || error "Pool on $POOL_DIR is $res, not $POOL"
8366 }
8367 run_test 200d "Check pool on a directory ==============================="
8368
8369 test_200e() {
8370         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8371         failed=0
8372         for i in $(seq -w 1 $(($TGT_COUNT * 3)))
8373         do
8374                 file=$POOL_DIR/file-$i
8375                 touch $file
8376                 check_file_in_pool $file
8377                 if [[ $? != 0 ]]
8378                 then
8379                         failed=$(($failed + 1))
8380                 fi
8381         done
8382         [ "$failed" = 0 ] || error "$failed files not allocated in $POOL"
8383 }
8384 run_test 200e "Check files allocation from directory pool =============="
8385
8386 test_200f() {
8387         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8388         mkdir -p $POOL_FILE
8389         failed=0
8390         for i in $(seq -w 1 $(($TGT_COUNT * 3)))
8391         do
8392                 file=$POOL_FILE/spoo-$i
8393                 $SETSTRIPE -p $POOL $file
8394                 check_file_in_pool $file
8395                 if [[ $? != 0 ]]
8396                 then
8397                         failed=$(($failed + 1))
8398                 fi
8399         done
8400         [ "$failed" = 0 ] || error "$failed files not allocated in $POOL"
8401 }
8402 run_test 200f "Create files in a pool ==================================="
8403
8404 test_200g() {
8405         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8406         TGT=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$POOL | tr '\n' ' ')
8407         res=$($LFS df --pool $FSNAME.$POOL | awk '{print $1}' | grep "$FSNAME-OST" | tr '\n' ' ')
8408         [ "$res" = "$TGT" ] || error "Pools OSTs '$TGT' is not '$res' that lfs df reports"
8409 }
8410 run_test 200g "lfs df a pool ============================================"
8411
8412 test_200h() { # b=24039
8413         mkdir -p $POOL_DIR || error "unable to create $POOL_DIR"
8414
8415         local file="/..$POOL_DIR/$tfile-1"
8416         $SETSTRIPE -p $POOL $file || error "unable to create $file"
8417
8418         cd $POOL_DIR
8419         $SETSTRIPE -p $POOL $tfile-2 || \
8420                 error "unable to create $tfile-2 in $POOL_DIR"
8421 }
8422 run_test 200h "Create files in a pool with relative pathname ============"
8423
8424 test_201a() {
8425         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8426         TGT=$($LCTL get_param -n lov.$FSNAME-*.pools.$POOL | head -1)
8427         do_facet mgs $LCTL pool_remove $FSNAME.$POOL $TGT
8428         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL | grep $TGT" "" ||
8429                 error "$TGT not removed from $FSNAME.$POOL"
8430 }
8431 run_test 201a "Remove a target from a pool ============================="
8432
8433 test_201b() {
8434         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8435         for TGT in $($LCTL get_param -n lov.$FSNAME-*.pools.$POOL | sort -u)
8436         do
8437                 do_facet mgs $LCTL pool_remove $FSNAME.$POOL $TGT
8438         done
8439         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL" "" ||
8440                 error "Pool $FSNAME.$POOL cannot be drained"
8441         # striping on an empty/nonexistant pool should fall back to "pool of everything"
8442         touch ${POOL_DIR}/$tfile || error "failed to use fallback striping for empty pool"
8443         # setstripe on an empty pool should fail
8444         $SETSTRIPE -p $POOL ${POOL_FILE}/$tfile 2>/dev/null && \
8445                 error "expected failure when creating file with empty pool"
8446         return 0
8447 }
8448 run_test 201b "Remove all targets from a pool =========================="
8449
8450 test_201c() {
8451         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8452         do_facet mgs $LCTL pool_destroy $FSNAME.$POOL
8453
8454         sleep 2
8455     # striping on an empty/nonexistant pool should fall back to "pool of everything"
8456         touch ${POOL_DIR}/$tfile || error "failed to use fallback striping for missing pool"
8457         # setstripe on an empty pool should fail
8458         $SETSTRIPE -p $POOL ${POOL_FILE}/$tfile 2>/dev/null && \
8459                 error "expected failure when creating file with missing pool"
8460
8461         # get param should return err once pool is gone
8462         if wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$POOL 2>/dev/null ||
8463                         echo foo" "foo"; then
8464                 remove_pool_from_list $FSNAME.$POOL
8465                 return 0
8466         fi
8467         error "Pool $FSNAME.$POOL is not destroyed"
8468 }
8469 run_test 201c "Remove a pool ============================================"
8470
8471 cleanup_pools $FSNAME
8472
8473 # usage: default_attr <count | size | offset>
8474 default_attr() {
8475         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
8476 }
8477
8478 # usage: check_default_stripe_attr
8479 check_default_stripe_attr() {
8480         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
8481         case $1 in
8482         --stripe-count|--count)
8483                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
8484         --stripe-size|--size)
8485                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
8486         --stripe-index|--index)
8487                 EXPECTED=-1;;
8488         *)
8489                 error "unknown getstripe attr '$1'"
8490         esac
8491
8492         [ $ACTUAL != $EXPECTED ] &&
8493                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
8494 }
8495
8496 test_204a() {
8497         mkdir -p $DIR/$tdir
8498         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
8499
8500         check_default_stripe_attr --stripe-count
8501         check_default_stripe_attr --stripe-size
8502         check_default_stripe_attr --stripe-index
8503
8504         return 0
8505 }
8506 run_test 204a "Print default stripe attributes ================="
8507
8508 test_204b() {
8509         mkdir -p $DIR/$tdir
8510         $SETSTRIPE --stripe-count 1 $DIR/$tdir
8511
8512         check_default_stripe_attr --stripe-size
8513         check_default_stripe_attr --stripe-index
8514
8515         return 0
8516 }
8517 run_test 204b "Print default stripe size and offset  ==========="
8518
8519 test_204c() {
8520         mkdir -p $DIR/$tdir
8521         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
8522
8523         check_default_stripe_attr --stripe-count
8524         check_default_stripe_attr --stripe-index
8525
8526         return 0
8527 }
8528 run_test 204c "Print default stripe count and offset ==========="
8529
8530 test_204d() {
8531         mkdir -p $DIR/$tdir
8532         $SETSTRIPE --stripe-index 0 $DIR/$tdir
8533
8534         check_default_stripe_attr --stripe-count
8535         check_default_stripe_attr --stripe-size
8536
8537         return 0
8538 }
8539 run_test 204d "Print default stripe count and size ============="
8540
8541 test_204e() {
8542         mkdir -p $DIR/$tdir
8543         $SETSTRIPE -d $DIR/$tdir
8544
8545         check_default_stripe_attr --stripe-count --raw
8546         check_default_stripe_attr --stripe-size --raw
8547         check_default_stripe_attr --stripe-index --raw
8548
8549         return 0
8550 }
8551 run_test 204e "Print raw stripe attributes ================="
8552
8553 test_204f() {
8554         mkdir -p $DIR/$tdir
8555         $SETSTRIPE --stripe-count 1 $DIR/$tdir
8556
8557         check_default_stripe_attr --stripe-size --raw
8558         check_default_stripe_attr --stripe-index --raw
8559
8560         return 0
8561 }
8562 run_test 204f "Print raw stripe size and offset  ==========="
8563
8564 test_204g() {
8565         mkdir -p $DIR/$tdir
8566         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
8567
8568         check_default_stripe_attr --stripe-count --raw
8569         check_default_stripe_attr --stripe-index --raw
8570
8571         return 0
8572 }
8573 run_test 204g "Print raw stripe count and offset ==========="
8574
8575 test_204h() {
8576         mkdir -p $DIR/$tdir
8577         $SETSTRIPE --stripe-index 0 $DIR/$tdir
8578
8579         check_default_stripe_attr --stripe-count --raw
8580         check_default_stripe_attr --stripe-size --raw
8581
8582         return 0
8583 }
8584 run_test 204h "Print raw stripe count and size ============="
8585
8586 test_212() {
8587         size=`date +%s`
8588         size=$((size % 8192 + 1))
8589         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
8590         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
8591         rm -f $DIR/f212 $DIR/f212.xyz
8592 }
8593 run_test 212 "Sendfile test ============================================"
8594
8595 test_213() {
8596         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
8597         cancel_lru_locks osc
8598         lctl set_param fail_loc=0x8000040f
8599         # generate a read lock
8600         cat $DIR/$tfile > /dev/null
8601         # write to the file, it will try to cancel the above read lock.
8602         cat /etc/hosts >> $DIR/$tfile
8603 }
8604 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
8605
8606 test_214() { # for bug 20133
8607         mkdir -p $DIR/d214p/d214c
8608         for (( i=0; i < 340; i++ )) ; do
8609                 touch $DIR/d214p/d214c/a$i
8610         done
8611
8612         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
8613         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
8614         ls $DIR/d214c || error "ls $DIR/d214c failed"
8615         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
8616 }
8617 run_test 214 "hash-indexed directory test - bug 20133"
8618
8619 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
8620 create_lnet_proc_files() {
8621         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
8622         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
8623
8624         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
8625         rm -f "$TMP/lnet_$1.sys_tmp"
8626 }
8627
8628 # counterpart of create_lnet_proc_files
8629 remove_lnet_proc_files() {
8630         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
8631 }
8632
8633 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
8634 # 3rd arg as regexp for body
8635 check_lnet_proc_stats() {
8636         local l=$(cat "$TMP/lnet_$1" |wc -l)
8637         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
8638
8639         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
8640 }
8641
8642 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
8643 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
8644 # optional and can be regexp for 2nd line (lnet.routes case)
8645 check_lnet_proc_entry() {
8646         local blp=2            # blp stands for 'position of 1st line of body'
8647         [ "$5" = "" ] || blp=3 # lnet.routes case
8648
8649         local l=$(cat "$TMP/lnet_$1" |wc -l)
8650         # subtracting one from $blp because the body can be empty
8651         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
8652
8653         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
8654                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
8655
8656         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
8657                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
8658
8659         # bail out if any unexpected line happened
8660         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
8661         [ "$?" != 0 ] || error "$2 misformatted"
8662 }
8663
8664 test_215() { # for bugs 18102, 21079, 21517
8665         local N='(0|[1-9][0-9]*)'   # non-negative numeric
8666         local P='[1-9][0-9]*'       # positive numeric
8667         local I='(0|-?[1-9][0-9]*)' # any numeric (0 | >0 | <0)
8668         local NET='[a-z][a-z0-9]*'  # LNET net like o2ib2
8669         local ADDR='[0-9.]+'        # LNET addr like 10.0.0.1
8670         local NID="$ADDR@$NET"      # LNET nid like 10.0.0.1@o2ib2
8671
8672         local L1 # regexp for 1st line
8673         local L2 # regexp for 2nd line (optional)
8674         local BR # regexp for the rest (body)
8675
8676         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
8677         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
8678         create_lnet_proc_files "stats"
8679         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
8680         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
8681         remove_lnet_proc_files "stats"
8682
8683         # /proc/sys/lnet/routes should look like this:
8684         # Routing disabled/enabled
8685         # net hops state router
8686         # where net is a string like tcp0, hops >= 0, state is up/down,
8687         # router is a string like 192.168.1.1@tcp2
8688         L1="^Routing (disabled|enabled)$"
8689         L2="^net +hops +state +router$"
8690         BR="^$NET +$N +(up|down) +$NID$"
8691         create_lnet_proc_files "routes"
8692         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
8693         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
8694         remove_lnet_proc_files "routes"
8695
8696         # /proc/sys/lnet/routers should look like this:
8697         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
8698         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
8699         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
8700         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
8701         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
8702         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
8703         create_lnet_proc_files "routers"
8704         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
8705         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
8706         remove_lnet_proc_files "routers"
8707
8708         # /proc/sys/lnet/peers should look like this:
8709         # nid refs state last max rtr min tx min queue
8710         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
8711         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
8712         # numeric (0 or >0 or <0), queue >= 0.
8713         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
8714         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
8715         create_lnet_proc_files "peers"
8716         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
8717         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
8718         remove_lnet_proc_files "peers"
8719
8720         # /proc/sys/lnet/buffers  should look like this:
8721         # pages count credits min
8722         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
8723         L1="^pages +count +credits +min$"
8724         BR="^ +$N +$N +$I +$I$"
8725         create_lnet_proc_files "buffers"
8726         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
8727         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
8728         remove_lnet_proc_files "buffers"
8729
8730         # /proc/sys/lnet/nis should look like this:
8731         # nid status alive refs peer rtr max tx min
8732         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
8733         # alive is numeric (0 or >0 or <0), refs > 0, peer >= 0,
8734         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
8735         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
8736         BR="^$NID +(up|down) +$I +$P +$N +$N +$N +$I +$I$"
8737         create_lnet_proc_files "nis"
8738         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
8739         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
8740         remove_lnet_proc_files "nis"
8741
8742         # can we successfully write to /proc/sys/lnet/stats?
8743         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
8744         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
8745 }
8746 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
8747
8748 test_216() { # bug 20317
8749         remote_ost_nodsh && skip "remote OST with nodsh" && return
8750         local node
8751         local p="$TMP/sanityN-$TESTNAME.parameters"
8752         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
8753         for node in $(osts_nodes); do
8754                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
8755                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
8756                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
8757         done
8758         clear_osc_stats
8759
8760         # agressive lockless i/o settings
8761         for node in $(osts_nodes); do
8762                 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'
8763         done
8764         lctl set_param -n osc.*.contention_seconds 60
8765
8766         $DIRECTIO write $DIR/$tfile 0 10 4096
8767         $CHECKSTAT -s 40960 $DIR/$tfile
8768
8769         # disable lockless i/o
8770         for node in $(osts_nodes); do
8771                 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'
8772         done
8773         lctl set_param -n osc.*.contention_seconds 0
8774         clear_osc_stats
8775
8776         dd if=/dev/zero of=$DIR/$tfile count=0
8777         $CHECKSTAT -s 0 $DIR/$tfile
8778
8779         restore_lustre_params <$p
8780         rm -f $p
8781         rm $DIR/$tfile
8782 }
8783 run_test 216 "check lockless direct write works and updates file size and kms correctly"
8784
8785 test_217() { # bug 22430
8786         local node
8787         local nid
8788
8789         for node in $(nodes_list); do
8790                 nid=$(host_nids_address $node $NETTYPE)
8791                 if [[ $nid = *-* ]] ; then
8792                         echo "lctl ping $nid@$NETTYPE"
8793                         lctl ping $nid@$NETTYPE
8794                 else
8795                         echo "skipping $node (no hyphen detected)"
8796                 fi
8797         done
8798 }
8799 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
8800
8801 test_218() {
8802        # do directio so as not to populate the page cache
8803        log "creating a 10 Mb file"
8804        multiop $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8805        log "starting reads"
8806        dd if=$DIR/$tfile of=/dev/null bs=4096 &
8807        log "truncating the file"
8808        multiop $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8809        log "killing dd"
8810        kill %+ || true # reads might have finished
8811        echo "wait until dd is finished"
8812        wait
8813        log "removing the temporary file"
8814        rm -rf $DIR/$tfile || error "tmp file removal failed"
8815 }
8816 run_test 218 "parallel read and truncate should not deadlock ======================="
8817
8818 test_219() {
8819         # write one partial page
8820         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
8821         # set no grant so vvp_io_commit_write will do sync write
8822         $LCTL set_param fail_loc=0x411
8823         # write a full page at the end of file
8824         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
8825
8826         $LCTL set_param fail_loc=0
8827         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
8828         $LCTL set_param fail_loc=0x411
8829         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
8830 }
8831 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
8832
8833 test_220() { #LU-325
8834         remote_ost_nodsh && skip "remote OST with nodsh" && return
8835         local OSTIDX=0
8836
8837         mkdir -p $DIR/$tdir
8838         local OST=$(lfs osts | grep ${OSTIDX}": " | \
8839                 awk '{print $2}' | sed -e 's/_UUID$//')
8840
8841         # on the mdt's osc
8842         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
8843         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
8844                         osc.$mdtosc_proc1.prealloc_last_id)
8845         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
8846                         osc.$mdtosc_proc1.prealloc_next_id)
8847
8848         $LFS df -i
8849
8850         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
8851         #define OBD_FAIL_OST_ENOINO              0x229
8852         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
8853         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
8854         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
8855
8856         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
8857
8858         MDSOBJS=$((last_id - next_id))
8859         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
8860
8861         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
8862         echo "OST still has $count kbytes free"
8863
8864         echo "create $MDSOBJS files @next_id..."
8865         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
8866
8867         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
8868                         osc.$mdtosc_proc1.prealloc_last_id)
8869         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
8870                         osc.$mdtosc_proc1.prealloc_next_id)
8871
8872         echo "after creation, last_id=$last_id2, next_id=$next_id2"
8873         $LFS df -i
8874
8875         echo "cleanup..."
8876
8877         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
8878         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
8879
8880         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
8881         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
8882         echo "unlink $MDSOBJS files @$next_id..."
8883         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
8884 }
8885 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
8886
8887 test_221() {
8888         dd if=`which date` of=$MOUNT/date oflag=sync
8889         chmod +x $MOUNT/date
8890
8891         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
8892         $LCTL set_param fail_loc=0x80001401
8893
8894         $MOUNT/date > /dev/null
8895         rm -f $MOUNT/date
8896 }
8897 run_test 221 "make sure fault and truncate race to not cause OOM"
8898
8899 test_222a () {
8900        rm -rf $DIR/$tdir
8901        mkdir -p $DIR/$tdir
8902        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
8903        createmany -o $DIR/$tdir/$tfile 10
8904        cancel_lru_locks mdc
8905        cancel_lru_locks osc
8906        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
8907        $LCTL set_param fail_loc=0x31a
8908        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
8909        $LCTL set_param fail_loc=0
8910        rm -r $DIR/$tdir
8911 }
8912 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
8913
8914 test_222b () {
8915        rm -rf $DIR/$tdir
8916        mkdir -p $DIR/$tdir
8917        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
8918        createmany -o $DIR/$tdir/$tfile 10
8919        cancel_lru_locks mdc
8920        cancel_lru_locks osc
8921        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
8922        $LCTL set_param fail_loc=0x31a
8923        rm -r $DIR/$tdir || "AGL for rmdir failed"
8924        $LCTL set_param fail_loc=0
8925 }
8926 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
8927
8928 test_223 () {
8929        rm -rf $DIR/$tdir
8930        mkdir -p $DIR/$tdir
8931        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
8932        createmany -o $DIR/$tdir/$tfile 10
8933        cancel_lru_locks mdc
8934        cancel_lru_locks osc
8935        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
8936        $LCTL set_param fail_loc=0x31b
8937        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
8938        $LCTL set_param fail_loc=0
8939        rm -r $DIR/$tdir
8940 }
8941 run_test 223 "osc reenqueue if without AGL lock granted ======================="
8942
8943 test_224a() { # LU-1039, MRP-303
8944         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
8945         $LCTL set_param fail_loc=0x508
8946         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
8947         $LCTL set_param fail_loc=0
8948         df $DIR
8949 }
8950 run_test 224a "Don't panic on bulk IO failure"
8951
8952 test_224b() { # LU-1039, MRP-303
8953         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
8954         cancel_lru_locks osc
8955         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
8956         $LCTL set_param fail_loc=0x515
8957         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
8958         $LCTL set_param fail_loc=0
8959         df $DIR
8960 }
8961 run_test 224b "Don't panic on bulk IO failure"
8962
8963 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
8964 test_225a () {
8965        if [ -z ${MDSSURVEY} ]; then
8966               skip_env "mds-survey not found" && return
8967        fi
8968
8969        local mds=$(facet_host $SINGLEMDS)
8970        local target=$(do_nodes $mds 'lctl dl' | \
8971                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
8972
8973        local cmd1="file_count=1000 thrhi=4"
8974        local cmd2="dir_count=2 layer=mdd stripe_count=0"
8975        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
8976        local cmd="$cmd1 $cmd2 $cmd3"
8977
8978        rm -f ${TMP}/mds_survey*
8979        echo + $cmd
8980        eval $cmd || error "mds-survey with zero-stripe failed"
8981        cat ${TMP}/mds_survey*
8982        rm -f ${TMP}/mds_survey*
8983 }
8984 run_test 225a "Metadata survey sanity with zero-stripe"
8985
8986 test_225b () {
8987        if [ -z ${MDSSURVEY} ]; then
8988               skip_env "mds-survey not found" && return
8989        fi
8990
8991        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
8992               skip_env "Need to mount OST to test" && return
8993        fi
8994
8995        local mds=$(facet_host $SINGLEMDS)
8996        local target=$(do_nodes $mds 'lctl dl' | \
8997                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
8998
8999        local cmd1="file_count=1000 thrhi=4"
9000        local cmd2="dir_count=2 layer=mdd stripe_count=1"
9001        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9002        local cmd="$cmd1 $cmd2 $cmd3"
9003
9004        rm -f ${TMP}/mds_survey*
9005        echo + $cmd
9006        eval $cmd || error "mds-survey with stripe_count failed"
9007        cat ${TMP}/mds_survey*
9008        rm -f ${TMP}/mds_survey*
9009 }
9010 run_test 225b "Metadata survey sanity with stripe_count = 1"
9011
9012 #
9013 # tests that do cleanup/setup should be run at the end
9014 #
9015
9016 test_900() {
9017         local ls
9018         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
9019         $LCTL set_param fail_loc=0x903
9020         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
9021         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
9022                 echo "clear" > $ls
9023         done
9024         FAIL_ON_ERROR=true cleanup
9025         FAIL_ON_ERROR=true setup
9026 }
9027 run_test 900 "umount should not race with any mgc requeue thread"
9028
9029 complete $(basename $0) $SECONDS
9030 check_and_cleanup_lustre
9031 if [ "$I_MOUNTED" != "yes" ]; then
9032         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
9033 fi
9034 exit_status