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