Whamcloud - gitweb
b2c6cf8e356b2b4b272164ce0e8cc546878e8293
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
11 ALWAYS_EXCEPT="                27u   42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
12 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
13
14 # Tests that fail on uml
15 CPU=`awk '/model/ {print $4}' /proc/cpuinfo`
16 #                                    buffer i/o errs             sock spc runas
17 [ "$CPU" = "UML" ] && EXCEPT="$EXCEPT 27m 27n 27o 27p 27q 27r 31d 54a  64b 99a 99b 99c 99d 99e 99f 101a"
18
19 SRCDIR=$(cd $(dirname $0); echo $PWD)
20 export PATH=$PATH:/sbin
21
22 TMP=${TMP:-/tmp}
23
24 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
25 CREATETEST=${CREATETEST:-createtest}
26 LFS=${LFS:-lfs}
27 LFIND=${LFIND:-"$LFS find"}
28 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
29 LCTL=${LCTL:-lctl}
30 MCREATE=${MCREATE:-mcreate}
31 OPENFILE=${OPENFILE:-openfile}
32 OPENUNLINK=${OPENUNLINK:-openunlink}
33 export MULTIOP=${MULTIOP:-multiop}
34 READS=${READS:-"reads"}
35 MUNLINK=${MUNLINK:-munlink}
36 SOCKETSERVER=${SOCKETSERVER:-socketserver}
37 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
38 MEMHOG=${MEMHOG:-memhog}
39 DIRECTIO=${DIRECTIO:-directio}
40 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
41 UMOUNT=${UMOUNT:-"umount -d"}
42 STRIPES_PER_OBJ=-1
43 CHECK_GRANT=${CHECK_GRANT:-"yes"}
44 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
45
46 export NAME=${NAME:-local}
47
48 SAVE_PWD=$PWD
49
50 CLEANUP=${CLEANUP:-:}
51 SETUP=${SETUP:-:}
52 TRACE=${TRACE:-""}
53 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
54 . $LUSTRE/tests/test-framework.sh
55 init_test_env $@
56 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
57 init_logging
58
59 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 60c 63 64b 68 71 73 77f 78 101a 103 115 120g 124b"
60
61 FAIL_ON_ERROR=false
62
63 cleanup() {
64         echo -n "cln.."
65         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
66         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
67 }
68 setup() {
69         echo -n "mnt.."
70         load_modules
71         setupall || exit 10
72         echo "done"
73 }
74
75 check_kernel_version() {
76         WANT_VER=$1
77         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
78         case $GOT_VER in
79         patchless|patchless_client) return 0;;
80         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
81         esac
82         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
83         return 1
84 }
85
86 if [ "$ONLY" == "cleanup" ]; then
87        sh llmountcleanup.sh
88        exit 0
89 fi
90
91 check_and_setup_lustre
92
93 DIR=${DIR:-$MOUNT}
94 assert_DIR
95
96 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
97     awk '{gsub(/_UUID/,""); print $1}' | head -1)
98 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
99 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
100 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
101 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
102 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
103 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
104
105 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
106 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
107 rm -rf $DIR/[Rdfs][0-9]*
108
109 # $RUNAS_ID may get set incorrectly somewhere else
110 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
111
112 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
113
114 build_test_filter
115
116 if [ "${ONLY}" = "MOUNT" ] ; then
117         echo "Lustre is up, please go on"
118         exit
119 fi
120
121 echo "preparing for tests involving mounts"
122 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
123 touch $EXT2_DEV
124 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
125 echo # add a newline after mke2fs.
126
127 umask 077
128
129 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
130 lctl set_param debug=-1 2> /dev/null || true
131 test_0() {
132         touch $DIR/$tfile
133         $CHECKSTAT -t file $DIR/$tfile || error
134         rm $DIR/$tfile
135         $CHECKSTAT -a $DIR/$tfile || error
136 }
137 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
138
139 test_0b() {
140         chmod 0755 $DIR || error
141         $CHECKSTAT -p 0755 $DIR || error
142 }
143 run_test 0b "chmod 0755 $DIR ============================="
144
145 test_0c() {
146     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
147     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
148 }
149 run_test 0c "check import proc ============================="
150
151 test_1a() {
152         mkdir $DIR/d1
153         mkdir $DIR/d1/d2
154         mkdir $DIR/d1/d2 && error "we expect EEXIST, but not returned"
155         $CHECKSTAT -t dir $DIR/d1/d2 || error
156 }
157 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
158
159 test_1b() {
160         rmdir $DIR/d1/d2
161         rmdir $DIR/d1
162         $CHECKSTAT -a $DIR/d1 || error
163 }
164 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
165
166 test_2a() {
167         mkdir $DIR/d2
168         touch $DIR/d2/f
169         $CHECKSTAT -t file $DIR/d2/f || error
170 }
171 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
172
173 test_2b() {
174         rm -r $DIR/d2
175         $CHECKSTAT -a $DIR/d2 || error
176 }
177 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
178
179 test_3a() {
180         mkdir $DIR/d3
181         $CHECKSTAT -t dir $DIR/d3 || error
182 }
183 run_test 3a "mkdir .../d3 ======================================"
184
185 test_3b() {
186         if [ ! -d $DIR/d3 ]; then
187                 mkdir $DIR/d3
188         fi
189         touch $DIR/d3/f
190         $CHECKSTAT -t file $DIR/d3/f || error
191 }
192 run_test 3b "touch .../d3/f ===================================="
193
194 test_3c() {
195         rm -r $DIR/d3
196         $CHECKSTAT -a $DIR/d3 || error
197 }
198 run_test 3c "rm -r .../d3 ======================================"
199
200 test_4a() {
201         mkdir $DIR/d4
202         $CHECKSTAT -t dir $DIR/d4 || error
203 }
204 run_test 4a "mkdir .../d4 ======================================"
205
206 test_4b() {
207         if [ ! -d $DIR/d4 ]; then
208                 mkdir $DIR/d4
209         fi
210         mkdir $DIR/d4/d2
211         $CHECKSTAT -t dir $DIR/d4/d2 || error
212 }
213 run_test 4b "mkdir .../d4/d2 ==================================="
214
215 test_5() {
216         mkdir $DIR/d5
217         mkdir $DIR/d5/d2
218         chmod 0707 $DIR/d5/d2
219         $CHECKSTAT -t dir -p 0707 $DIR/d5/d2 || error
220 }
221 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
222
223 test_6a() {
224         touch $DIR/f6a
225         chmod 0666 $DIR/f6a || error
226         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
227 }
228 run_test 6a "touch .../f6a; chmod .../f6a ======================"
229
230 test_6b() {
231         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
232         if [ ! -f $DIR/f6a ]; then
233                 touch $DIR/f6a
234                 chmod 0666 $DIR/f6a
235         fi
236         $RUNAS chmod 0444 $DIR/f6a && error
237         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
238 }
239 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
240
241 test_6c() {
242         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
243         touch $DIR/f6c
244         chown $RUNAS_ID $DIR/f6c || error
245         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
246 }
247 run_test 6c "touch .../f6c; chown .../f6c ======================"
248
249 test_6d() {
250         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
251         if [ ! -f $DIR/f6c ]; then
252                 touch $DIR/f6c
253                 chown $RUNAS_ID $DIR/f6c
254         fi
255         $RUNAS chown $UID $DIR/f6c && error
256         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
257 }
258 run_test 6d "$RUNAS chown .../f6c (should return error) =="
259
260 test_6e() {
261         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
262         touch $DIR/f6e
263         chgrp $RUNAS_ID $DIR/f6e || error
264         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
265 }
266 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
267
268 test_6f() {
269         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
270         if [ ! -f $DIR/f6e ]; then
271                 touch $DIR/f6e
272                 chgrp $RUNAS_ID $DIR/f6e
273         fi
274         $RUNAS chgrp $UID $DIR/f6e && error
275         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
276 }
277 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
278
279 test_6g() {
280         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
281         mkdir $DIR/d6g || error
282         chmod 777 $DIR/d6g || error
283         $RUNAS mkdir $DIR/d6g/d || error
284         chmod g+s $DIR/d6g/d || error
285         mkdir $DIR/d6g/d/subdir
286         $CHECKSTAT -g \#$RUNAS_GID $DIR/d6g/d/subdir || error
287 }
288 run_test 6g "Is new dir in sgid dir inheriting group?"
289
290 test_6h() { # bug 7331
291         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
292         touch $DIR/f6h || error "touch failed"
293         chown $RUNAS_ID:$RUNAS_GID $DIR/f6h || error "initial chown failed"
294         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/f6h && error "chown worked"
295         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/f6h || error
296 }
297 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
298
299 test_7a() {
300         mkdir $DIR/d7
301         $MCREATE $DIR/d7/f
302         chmod 0666 $DIR/d7/f
303         $CHECKSTAT -t file -p 0666 $DIR/d7/f || error
304 }
305 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
306
307 test_7b() {
308         if [ ! -d $DIR/d7 ]; then
309                 mkdir $DIR/d7
310         fi
311         $MCREATE $DIR/d7/f2
312         echo -n foo > $DIR/d7/f2
313         [ "`cat $DIR/d7/f2`" = "foo" ] || error
314         $CHECKSTAT -t file -s 3 $DIR/d7/f2 || error
315 }
316 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
317
318 test_8() {
319         mkdir $DIR/d8
320         touch $DIR/d8/f
321         chmod 0666 $DIR/d8/f
322         $CHECKSTAT -t file -p 0666 $DIR/d8/f || error
323 }
324 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
325
326 test_9() {
327         mkdir $DIR/d9
328         mkdir $DIR/d9/d2
329         mkdir $DIR/d9/d2/d3
330         $CHECKSTAT -t dir $DIR/d9/d2/d3 || error
331 }
332 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
333
334 test_10() {
335         mkdir $DIR/d10
336         mkdir $DIR/d10/d2
337         touch $DIR/d10/d2/f
338         $CHECKSTAT -t file $DIR/d10/d2/f || error
339 }
340 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
341
342 test_11() {
343         mkdir $DIR/d11
344         mkdir $DIR/d11/d2
345         chmod 0666 $DIR/d11/d2
346         chmod 0705 $DIR/d11/d2
347         $CHECKSTAT -t dir -p 0705 $DIR/d11/d2 || error
348 }
349 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
350
351 test_12() {
352         mkdir $DIR/d12
353         touch $DIR/d12/f
354         chmod 0666 $DIR/d12/f
355         chmod 0654 $DIR/d12/f
356         $CHECKSTAT -t file -p 0654 $DIR/d12/f || error
357 }
358 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
359
360 test_13() {
361         mkdir $DIR/d13
362         dd if=/dev/zero of=$DIR/d13/f count=10
363         >  $DIR/d13/f
364         $CHECKSTAT -t file -s 0 $DIR/d13/f || error
365 }
366 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
367
368 test_14() {
369         mkdir $DIR/d14
370         touch $DIR/d14/f
371         rm $DIR/d14/f
372         $CHECKSTAT -a $DIR/d14/f || error
373 }
374 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
375
376 test_15() {
377         mkdir $DIR/d15
378         touch $DIR/d15/f
379         mv $DIR/d15/f $DIR/d15/f2
380         $CHECKSTAT -t file $DIR/d15/f2 || error
381 }
382 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
383
384 test_16() {
385         mkdir $DIR/d16
386         touch $DIR/d16/f
387         rm -rf $DIR/d16/f
388         $CHECKSTAT -a $DIR/d16/f || error
389 }
390 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
391
392 test_17a() {
393         mkdir -p $DIR/d17
394         touch $DIR/d17/f
395         ln -s $DIR/d17/f $DIR/d17/l-exist
396         ls -l $DIR/d17
397         $CHECKSTAT -l $DIR/d17/f $DIR/d17/l-exist || error
398         $CHECKSTAT -f -t f $DIR/d17/l-exist || error
399         rm -f $DIR/d17/l-exist
400         $CHECKSTAT -a $DIR/d17/l-exist || error
401 }
402 run_test 17a "symlinks: create, remove (real) =================="
403
404 test_17b() {
405         mkdir -p $DIR/d17
406         ln -s no-such-file $DIR/d17/l-dangle
407         ls -l $DIR/d17
408         $CHECKSTAT -l no-such-file $DIR/d17/l-dangle || error
409         $CHECKSTAT -fa $DIR/d17/l-dangle || error
410         rm -f $DIR/d17/l-dangle
411         $CHECKSTAT -a $DIR/d17/l-dangle || error
412 }
413 run_test 17b "symlinks: create, remove (dangling) =============="
414
415 test_17c() { # bug 3440 - don't save failed open RPC for replay
416         mkdir -p $DIR/d17
417         ln -s foo $DIR/d17/f17c
418         cat $DIR/d17/f17c && error "opened non-existent symlink" || true
419 }
420 run_test 17c "symlinks: open dangling (should return error) ===="
421
422 test_17d() {
423         mkdir -p $DIR/d17
424         ln -s foo $DIR/d17/f17d
425         touch $DIR/d17/f17d || error "creating to new symlink"
426 }
427 run_test 17d "symlinks: create dangling ========================"
428
429 test_17e() {
430         mkdir -p $DIR/$tdir
431         local foo=$DIR/$tdir/$tfile
432         ln -s $foo $foo || error "create symlink failed"
433         ls -l $foo || error "ls -l failed"
434         ls $foo && error "ls not failed" || true
435 }
436 run_test 17e "symlinks: create recursive symlink (should return error) ===="
437
438 test_17f() {
439         mkdir -p $DIR/d17f
440         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
441         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
442         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
443         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
444         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
445         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
446         ls -l  $DIR/d17f
447 }
448 run_test 17f "symlinks: long and very long symlink name ========================"
449
450 test_17g() {
451         mkdir -p $DIR/$tdir
452         LONGSYMLINK="$(dd if=/dev/zero bs=4095 count=1 | tr '\0' 'x')"
453         ln -s $LONGSYMLINK $DIR/$tdir/$tfile
454         ls -l $DIR/$tdir
455 }
456 run_test 17g "symlinks: really long symlink name ==============================="
457
458 test_17h() { #bug 17378
459         remote_mds_nodsh && skip "remote MDS with nodsh" && return
460         mkdir -p $DIR/$tdir
461         $SETSTRIPE -c -1 $DIR/$tdir
462 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
463         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000141
464         touch $DIR/$tdir/$tfile || true
465 }
466 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
467
468 test_17i() { #bug 20018
469         remote_mds_nodsh && skip "remote MDS with nodsh" && return
470         mkdir -p $DIR/$tdir
471         local foo=$DIR/$tdir/$tfile
472         ln -s $foo $foo || error "create symlink failed"
473 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
474         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000143
475         ls -l $foo && error "error not detected"
476         return 0
477 }
478 run_test 17i "don't panic on short symlink"
479
480 test_17k() { #bug 22301
481         rsync --help | grep -q xattr ||
482                 skip_env "$(rsync --version| head -1) does not support xattrs"
483         mkdir -p $DIR/{$tdir,$tdir.new}
484         touch $DIR/$tdir/$tfile
485         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
486         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
487                 error "rsync failed with xattrs enabled"
488 }
489 run_test 17k "symlinks: rsync with xattrs enabled ========================="
490
491 # LU-1540
492 test_17m() {
493         local short_sym="0123456789"
494         local WDIR=$DIR/${tdir}m
495         local mds_index
496         local devname
497         local cmd
498         local i
499         local rc=0
500
501         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
502         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
503                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
504         mkdir -p $WDIR
505         long_sym=$short_sym
506         # create a long symlink file
507         for ((i = 0; i < 4; ++i)); do
508                 long_sym=${long_sym}${long_sym}
509         done
510
511         echo "create 512 short and long symlink files under $WDIR"
512         for ((i = 0; i < 256; ++i)); do
513                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
514                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
515         done
516
517         echo "erase them"
518         rm -f $WDIR/*
519         sync
520         sleep 2
521
522         echo "recreate the 512 symlink files with a shorter string"
523         for ((i = 0; i < 512; ++i)); do
524                 # rewrite the symlink file with a shorter string
525                 ln -sf ${long_sym} $WDIR/long-$i
526                 ln -sf ${short_sym} $WDIR/short-$i
527         done
528
529         mds_index=$($LFS getstripe -M $WDIR)
530         mds_index=$((mds_index+1))
531         devname=$(mdsdevname $mds_index)
532         cmd="$E2FSCK -fnvd $devname"
533
534         echo "stop and checking mds${mds_index}: $cmd"
535         # e2fsck should not return error
536         stop mds${mds_index} -f
537         do_facet mds${mds_index} $cmd || rc=$?
538
539         start mds${mds_index} $devname $MDS_MOUNT_OPTS
540         df $MOUNT > /dev/null 2>&1
541         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
542                 "short/long symlink MDT: rc=$rc"
543         return $rc
544 }
545 run_test 17m "run e2fsck against MDT which contains short/long symlink"
546
547 test_18() {
548         touch $DIR/f
549         ls $DIR || error
550 }
551 run_test 18 "touch .../f ; ls ... =============================="
552
553 test_19a() {
554         touch $DIR/f19
555         ls -l $DIR
556         rm $DIR/f19
557         $CHECKSTAT -a $DIR/f19 || error
558 }
559 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
560
561 test_19b() {
562         ls -l $DIR/f19 && error || true
563 }
564 run_test 19b "ls -l .../f19 (should return error) =============="
565
566 test_19c() {
567         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
568         $RUNAS touch $DIR/f19 && error || true
569 }
570 run_test 19c "$RUNAS touch .../f19 (should return error) =="
571
572 test_19d() {
573         cat $DIR/f19 && error || true
574 }
575 run_test 19d "cat .../f19 (should return error) =============="
576
577 test_20() {
578         touch $DIR/f
579         rm $DIR/f
580         log "1 done"
581         touch $DIR/f
582         rm $DIR/f
583         log "2 done"
584         touch $DIR/f
585         rm $DIR/f
586         log "3 done"
587         $CHECKSTAT -a $DIR/f || error
588 }
589 run_test 20 "touch .../f ; ls -l ... ==========================="
590
591 test_21() {
592         mkdir $DIR/d21
593         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
594         ln -s dangle $DIR/d21/link
595         echo foo >> $DIR/d21/link
596         cat $DIR/d21/dangle
597         $CHECKSTAT -t link $DIR/d21/link || error
598         $CHECKSTAT -f -t file $DIR/d21/link || error
599 }
600 run_test 21 "write to dangling link ============================"
601
602 test_22() {
603         WDIR=$DIR/$tdir
604         mkdir -p $WDIR
605         chown $RUNAS_ID:$RUNAS_GID $WDIR
606         (cd $WDIR || error "cd $WDIR failed";
607         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
608         $RUNAS tar xf -)
609         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
610         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
611         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
612 }
613 run_test 22 "unpack tar archive as non-root user ==============="
614
615 # was test_23
616 test_23a() {
617         mkdir -p $DIR/$tdir
618         local file=$DIR/$tdir/$tfile
619
620         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
621         openfile -f O_CREAT:O_EXCL $file &&
622                 error "$file recreate succeeded" || true
623 }
624 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
625
626 test_23b() { # bug 18988
627         mkdir -p $DIR/$tdir
628         local file=$DIR/$tdir/$tfile
629
630         rm -f $file
631         echo foo > $file || error "write filed"
632         echo bar >> $file || error "append filed"
633         $CHECKSTAT -s 8 $file || error "wrong size"
634         rm $file
635 }
636 run_test 23b "O_APPEND check =========================="
637
638 test_24a() {
639         echo '== rename sanity =============================================='
640         echo '-- same directory rename'
641         mkdir $DIR/R1
642         touch $DIR/R1/f
643         mv $DIR/R1/f $DIR/R1/g
644         $CHECKSTAT -t file $DIR/R1/g || error
645 }
646 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
647
648 test_24b() {
649         mkdir $DIR/R2
650         touch $DIR/R2/{f,g}
651         mv $DIR/R2/f $DIR/R2/g
652         $CHECKSTAT -a $DIR/R2/f || error
653         $CHECKSTAT -t file $DIR/R2/g || error
654 }
655 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
656
657 test_24c() {
658         mkdir $DIR/R3
659         mkdir $DIR/R3/f
660         mv $DIR/R3/f $DIR/R3/g
661         $CHECKSTAT -a $DIR/R3/f || error
662         $CHECKSTAT -t dir $DIR/R3/g || error
663 }
664 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
665
666 test_24d() {
667         mkdir $DIR/R4
668         mkdir $DIR/R4/{f,g}
669         mrename $DIR/R4/f $DIR/R4/g
670         $CHECKSTAT -a $DIR/R4/f || error
671         $CHECKSTAT -t dir $DIR/R4/g || error
672 }
673 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
674
675 test_24e() {
676         echo '-- cross directory renames --'
677         mkdir $DIR/R5{a,b}
678         touch $DIR/R5a/f
679         mv $DIR/R5a/f $DIR/R5b/g
680         $CHECKSTAT -a $DIR/R5a/f || error
681         $CHECKSTAT -t file $DIR/R5b/g || error
682 }
683 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
684
685 test_24f() {
686         mkdir $DIR/R6{a,b}
687         touch $DIR/R6a/f $DIR/R6b/g
688         mv $DIR/R6a/f $DIR/R6b/g
689         $CHECKSTAT -a $DIR/R6a/f || error
690         $CHECKSTAT -t file $DIR/R6b/g || error
691 }
692 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
693
694 test_24g() {
695         mkdir $DIR/R7{a,b}
696         mkdir $DIR/R7a/d
697         mv $DIR/R7a/d $DIR/R7b/e
698         $CHECKSTAT -a $DIR/R7a/d || error
699         $CHECKSTAT -t dir $DIR/R7b/e || error
700 }
701 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
702
703 test_24h() {
704         mkdir $DIR/R8{a,b}
705         mkdir $DIR/R8a/d $DIR/R8b/e
706         mrename $DIR/R8a/d $DIR/R8b/e
707         $CHECKSTAT -a $DIR/R8a/d || error
708         $CHECKSTAT -t dir $DIR/R8b/e || error
709 }
710 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
711
712 test_24i() {
713         echo "-- rename error cases"
714         mkdir $DIR/R9
715         mkdir $DIR/R9/a
716         touch $DIR/R9/f
717         mrename $DIR/R9/f $DIR/R9/a
718         $CHECKSTAT -t file $DIR/R9/f || error
719         $CHECKSTAT -t dir  $DIR/R9/a || error
720         $CHECKSTAT -a $DIR/R9/a/f || error
721 }
722 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
723
724 test_24j() {
725         mkdir $DIR/R10
726         mrename $DIR/R10/f $DIR/R10/g
727         $CHECKSTAT -t dir $DIR/R10 || error
728         $CHECKSTAT -a $DIR/R10/f || error
729         $CHECKSTAT -a $DIR/R10/g || error
730 }
731 run_test 24j "source does not exist ============================"
732
733 test_24k() {
734         mkdir $DIR/R11a $DIR/R11a/d
735         touch $DIR/R11a/f
736         mv $DIR/R11a/f $DIR/R11a/d
737         $CHECKSTAT -a $DIR/R11a/f || error
738         $CHECKSTAT -t file $DIR/R11a/d/f || error
739 }
740 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
741
742 # bug 2429 - rename foo foo foo creates invalid file
743 test_24l() {
744         f="$DIR/f24l"
745         $MULTIOP $f OcNs || error
746 }
747 run_test 24l "Renaming a file to itself ========================"
748
749 test_24m() {
750         f="$DIR/f24m"
751         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
752         # on ext3 this does not remove either the source or target files
753         # though the "expected" operation would be to remove the source
754         $CHECKSTAT -t file ${f} || error "${f} missing"
755         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
756 }
757 run_test 24m "Renaming a file to a hard link to itself ========="
758
759 test_24n() {
760     f="$DIR/f24n"
761     # this stats the old file after it was renamed, so it should fail
762     touch ${f}
763     $CHECKSTAT ${f}
764     mv ${f} ${f}.rename
765     $CHECKSTAT ${f}.rename
766     $CHECKSTAT -a ${f}
767 }
768 run_test 24n "Statting the old file after renaming (Posix rename 2)"
769
770 test_24o() {
771         check_kernel_version 37 || return 0
772         mkdir -p $DIR/d24o
773         rename_many -s random -v -n 10 $DIR/d24o
774 }
775 run_test 24o "rename of files during htree split ==============="
776
777 test_24p() {
778         mkdir $DIR/R12{a,b}
779         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
780         mrename $DIR/R12a $DIR/R12b
781         $CHECKSTAT -a $DIR/R12a || error
782         $CHECKSTAT -t dir $DIR/R12b || error
783         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
784         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
785 }
786 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
787
788 test_24q() {
789         mkdir $DIR/R13{a,b}
790         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
791         multiop_bg_pause $DIR/R13b D_c || return 1
792         MULTIPID=$!
793
794         mrename $DIR/R13a $DIR/R13b
795         $CHECKSTAT -a $DIR/R13a || error
796         $CHECKSTAT -t dir $DIR/R13b || error
797         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
798         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
799         kill -USR1 $MULTIPID
800         wait $MULTIPID || error "multiop close failed"
801 }
802 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
803
804 test_24r() { #bug 3789
805         mkdir $DIR/R14a $DIR/R14a/b
806         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
807         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
808         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
809 }
810 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
811
812 test_24s() {
813         mkdir $DIR/R15a $DIR/R15a/b $DIR/R15a/b/c
814         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
815         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
816         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
817 }
818 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
819 test_24t() {
820         mkdir $DIR/R16a $DIR/R16a/b $DIR/R16a/b/c
821         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
822         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
823         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
824 }
825 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
826
827 test_24u() { # bug12192
828         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
829         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
830 }
831 run_test 24u "create stripe file"
832
833 page_size() {
834         getconf PAGE_SIZE
835 }
836
837 test_24v() {
838         local NRFILES=100000
839         local FREE_INODES=`lfs df -i|grep "filesystem summary" | awk '{print $5}'`
840         [ $FREE_INODES -lt $NRFILES ] && \
841                 skip "not enough free inodes $FREE_INODES required $NRFILES" && \
842                 return
843
844         mkdir -p $DIR/d24v
845         createmany -m $DIR/d24v/$tfile $NRFILES
846
847         cancel_lru_locks mdc
848         lctl set_param mdc.*.stats clear
849
850         ls $DIR/d24v >/dev/null || error "error in listing large dir"
851
852         # LU-5 large readdir
853         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
854         #               8 bytes for name(filename is mostly 5 in this test) +
855         #               8 bytes for luda_type
856         # take into account of overhead in lu_dirpage header and end mark in
857         # each page, plus one in RPC_NUM calculation.
858         DIRENT_SIZE=48
859         RPC_SIZE=$(($(lctl get_param -n mdc.*.max_pages_per_rpc)*$(page_size)))
860         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
861         mds_readpage=`lctl get_param mdc.*.stats | \
862                                 awk '/^mds_readpage/ {print $2}'`
863         [ $mds_readpage -gt $RPC_NUM ] && \
864                 error "large readdir doesn't take effect"
865
866         rm $DIR/d24v -rf
867 }
868 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
869
870 test_24w() { # bug21506
871         SZ1=234852
872         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
873         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
874         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
875         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
876         [ "$SZ1" = "$SZ2" ] || \
877                 error "Error reading at the end of the file $tfile"
878 }
879 run_test 24w "Reading a file larger than 4Gb"
880
881 test_25a() {
882         echo '== symlink sanity ============================================='
883
884         mkdir $DIR/d25
885         ln -s d25 $DIR/s25
886         touch $DIR/s25/foo || error
887 }
888 run_test 25a "create file in symlinked directory ==============="
889
890 test_25b() {
891         [ ! -d $DIR/d25 ] && test_25a
892         $CHECKSTAT -t file $DIR/s25/foo || error
893 }
894 run_test 25b "lookup file in symlinked directory ==============="
895
896 test_26a() {
897         mkdir $DIR/d26
898         mkdir $DIR/d26/d26-2
899         ln -s d26/d26-2 $DIR/s26
900         touch $DIR/s26/foo || error
901 }
902 run_test 26a "multiple component symlink ======================="
903
904 test_26b() {
905         mkdir -p $DIR/d26b/d26-2
906         ln -s d26b/d26-2/foo $DIR/s26-2
907         touch $DIR/s26-2 || error
908 }
909 run_test 26b "multiple component symlink at end of lookup ======"
910
911 test_26c() {
912         mkdir $DIR/d26.2
913         touch $DIR/d26.2/foo
914         ln -s d26.2 $DIR/s26.2-1
915         ln -s s26.2-1 $DIR/s26.2-2
916         ln -s s26.2-2 $DIR/s26.2-3
917         chmod 0666 $DIR/s26.2-3/foo
918 }
919 run_test 26c "chain of symlinks ================================"
920
921 # recursive symlinks (bug 439)
922 test_26d() {
923         ln -s d26-3/foo $DIR/d26-3
924 }
925 run_test 26d "create multiple component recursive symlink ======"
926
927 test_26e() {
928         [ ! -h $DIR/d26-3 ] && test_26d
929         rm $DIR/d26-3
930 }
931 run_test 26e "unlink multiple component recursive symlink ======"
932
933 # recursive symlinks (bug 7022)
934 test_26f() {
935         mkdir -p $DIR/$tdir
936         mkdir $DIR/$tdir/$tfile        || error "mkdir $DIR/$tdir/$tfile failed"
937         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
938         mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
939         mkdir $tfile             || error "mkdir $tfile failed"
940         cd $tfile                || error "cd $tfile failed"
941         ln -s .. dotdot          || error "ln dotdot failed"
942         ln -s dotdot/lndir lndir || error "ln lndir failed"
943         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
944         output=`ls $tfile/$tfile/lndir/bar1`
945         [ "$output" = bar1 ] && error "unexpected output"
946         rm -r $tfile             || error "rm $tfile failed"
947         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
948 }
949 run_test 26f "rm -r of a directory which has recursive symlink ="
950
951 test_27a() {
952         echo '== stripe sanity =============================================='
953         mkdir -p $DIR/d27 || error "mkdir failed"
954         $GETSTRIPE $DIR/d27
955         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
956         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
957         pass
958         log "== test_27a: write to one stripe file ========================="
959         cp /etc/hosts $DIR/d27/f0 || error
960 }
961 run_test 27a "one stripe file =================================="
962
963 test_27b() {
964         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
965         mkdir -p $DIR/d27
966         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
967         $GETSTRIPE -c $DIR/d27/f01
968         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
969                 error "two-stripe file doesn't have two stripes"
970 }
971 run_test 27b "create two stripe file"
972
973 test_27c() {
974         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
975
976         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
977 }
978 run_test 27c "write to two stripe file"
979
980 test_27d() {
981         mkdir -p $DIR/d27
982         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
983         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
984         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
985 }
986 run_test 27d "create file with default settings ================"
987
988 test_27e() {
989         mkdir -p $DIR/d27
990         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
991         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
992         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
993 }
994 run_test 27e "setstripe existing file (should return error) ======"
995
996 test_27f() {
997         mkdir -p $DIR/d27
998         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
999         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1000         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1001 }
1002 run_test 27f "setstripe with bad stripe size (should return error)"
1003
1004 test_27g() {
1005         mkdir -p $DIR/d27
1006         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1007         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1008                 error "$DIR/d27/fnone has object"
1009 }
1010 run_test 27g "$GETSTRIPE with no objects"
1011
1012 test_27i() {
1013         touch $DIR/d27/fsome || error "touch failed"
1014         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1015 }
1016 run_test 27i "$GETSTRIPE with some objects"
1017
1018 test_27j() {
1019         mkdir -p $DIR/d27
1020         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1021 }
1022 run_test 27j "setstripe with bad stripe offset (should return error)"
1023
1024 test_27k() { # bug 2844
1025         mkdir -p $DIR/d27
1026         FILE=$DIR/d27/f27k
1027         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1028         [ ! -d $DIR/d27 ] && mkdir -p $DIR/d27
1029         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1030         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1031         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1032         dd if=/dev/zero of=$FILE bs=4k count=1
1033         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1034         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1035 }
1036 run_test 27k "limit i_blksize for broken user apps ============="
1037
1038 test_27l() {
1039         mkdir -p $DIR/d27
1040         mcreate $DIR/f27l || error "creating file"
1041         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1042                 error "setstripe should have failed" || true
1043 }
1044 run_test 27l "check setstripe permissions (should return error)"
1045
1046 test_27m() {
1047         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1048         if [ $ORIGFREE -gt $MAXFREE ]; then
1049                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1050                 return
1051         fi
1052         mkdir -p $DIR/d27
1053         $SETSTRIPE -i 0 -c 1 $DIR/d27/f27m_1
1054         dd if=/dev/zero of=$DIR/d27/f27m_1 bs=1024 count=$MAXFREE &&
1055                 error "dd should fill OST0"
1056         i=2
1057         while $SETSTRIPE -i 0 -c 1 $DIR/d27/f27m_$i; do
1058                 i=`expr $i + 1`
1059                 [ $i -gt 256 ] && break
1060         done
1061         i=`expr $i + 1`
1062         touch $DIR/d27/f27m_$i
1063         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
1064                 error "OST0 was full but new created file still use it"
1065         i=`expr $i + 1`
1066         touch $DIR/d27/f27m_$i
1067         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
1068                 error "OST0 was full but new created file still use it"
1069         rm -r $DIR/d27
1070         sleep 15
1071 }
1072 run_test 27m "create file while OST0 was full =================="
1073
1074 sleep_maxage() {
1075         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1076         sleep $DELAY
1077 }
1078
1079 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1080 # if the OST isn't full anymore.
1081 reset_enospc() {
1082         local OSTIDX=${1:-""}
1083
1084         local list=$(comma_list $(osts_nodes))
1085         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1086
1087         do_nodes $list lctl set_param fail_loc=0
1088         sync    # initiate all OST_DESTROYs from MDS to OST
1089         sleep_maxage
1090 }
1091
1092 exhaust_precreations() {
1093         local OSTIDX=$1
1094         local FAILLOC=$2
1095         local FAILIDX=${3:-$OSTIDX}
1096
1097         mkdir -p $DIR/$tdir
1098         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1099         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1100
1101         local OST=$(ostname_from_index $OSTIDX)
1102         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1103                           sed -e 's/_UUID$//;s/^.*-//')
1104
1105         # on the mdt's osc
1106         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1107         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1108         osc.$mdtosc_proc1.prealloc_last_id)
1109         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1110         osc.$mdtosc_proc1.prealloc_next_id)
1111
1112         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1113         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1114
1115         mkdir -p $DIR/$tdir/${OST}
1116         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1117 #define OBD_FAIL_OST_ENOSPC              0x215
1118         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1119         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1120         echo "Creating to objid $last_id on ost $OST..."
1121         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1122         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1123         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1124         sleep_maxage
1125 }
1126
1127 exhaust_all_precreations() {
1128         local i
1129         for (( i=0; i < OSTCOUNT; i++ )) ; do
1130                 exhaust_precreations $i $1 -1
1131         done
1132 }
1133
1134 test_27n() {
1135         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1136         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1137         remote_ost_nodsh && skip "remote OST with nodsh" && return
1138
1139         reset_enospc
1140         rm -f $DIR/$tdir/$tfile
1141         exhaust_precreations 0 0x80000215
1142         $SETSTRIPE -c -1 $DIR/$tdir
1143         touch $DIR/$tdir/$tfile || error
1144         $GETSTRIPE $DIR/$tdir/$tfile
1145         reset_enospc
1146 }
1147 run_test 27n "create file with some full OSTs =================="
1148
1149 test_27o() {
1150         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1151         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1152         remote_ost_nodsh && skip "remote OST with nodsh" && return
1153
1154         reset_enospc
1155         rm -f $DIR/$tdir/$tfile
1156         exhaust_all_precreations 0x215
1157
1158         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1159
1160         reset_enospc
1161         rm -rf $DIR/$tdir/*
1162 }
1163 run_test 27o "create file with all full OSTs (should error) ===="
1164
1165 test_27p() {
1166         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1167         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1168         remote_ost_nodsh && skip "remote OST with nodsh" && return
1169
1170         reset_enospc
1171         rm -f $DIR/$tdir/$tfile
1172         mkdir -p $DIR/$tdir
1173
1174         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1175         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1176         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1177
1178         exhaust_precreations 0 0x80000215
1179         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1180         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1181         $GETSTRIPE $DIR/$tdir/$tfile
1182
1183         reset_enospc
1184 }
1185 run_test 27p "append to a truncated file with some full OSTs ==="
1186
1187 test_27q() {
1188         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1189         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1190         remote_ost_nodsh && skip "remote OST with nodsh" && return
1191
1192         reset_enospc
1193         rm -f $DIR/$tdir/$tfile
1194
1195         mkdir -p $DIR/$tdir
1196         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1197         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1198         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1199
1200         exhaust_all_precreations 0x215
1201
1202         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1203         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1204
1205         reset_enospc
1206 }
1207 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1208
1209 test_27r() {
1210         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1211         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1212         remote_ost_nodsh && skip "remote OST with nodsh" && return
1213
1214         reset_enospc
1215         rm -f $DIR/$tdir/$tfile
1216         exhaust_precreations 0 0x80000215
1217
1218         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1219
1220         reset_enospc
1221 }
1222 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1223
1224 test_27s() { # bug 10725
1225         mkdir -p $DIR/$tdir
1226         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1227         local stripe_count=0
1228         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1229         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1230                 error "stripe width >= 2^32 succeeded" || true
1231
1232 }
1233 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1234
1235 test_27t() { # bug 10864
1236         WDIR=`pwd`
1237         WLFS=`which lfs`
1238         cd $DIR
1239         touch $tfile
1240         $WLFS getstripe $tfile
1241         cd $WDIR
1242 }
1243 run_test 27t "check that utils parse path correctly"
1244
1245 test_27u() { # bug 4900
1246         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1247         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1248
1249 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1250         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1251         mkdir -p $DIR/$tdir
1252         createmany -o $DIR/$tdir/t- 1000
1253         do_facet $SINGLEMDS lctl set_param fail_loc=0
1254
1255         TLOG=$DIR/$tfile.getstripe
1256         $GETSTRIPE $DIR/$tdir > $TLOG
1257         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1258         unlinkmany $DIR/$tdir/t- 1000
1259         [ $OBJS -gt 0 ] && \
1260                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1261 }
1262 run_test 27u "skip object creation on OSC w/o objects =========="
1263
1264 test_27v() { # bug 4900
1265         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1266         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1267         remote_ost_nodsh && skip "remote OST with nodsh" && return
1268
1269         exhaust_all_precreations 0x215
1270         reset_enospc
1271
1272         mkdir -p $DIR/$tdir
1273         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1274
1275         touch $DIR/$tdir/$tfile
1276         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1277         # all except ost1
1278         for (( i=1; i < OSTCOUNT; i++ )); do
1279                 do_facet ost$i lctl set_param fail_loc=0x705
1280         done
1281         local START=`date +%s`
1282         createmany -o $DIR/$tdir/$tfile 32
1283
1284         local FINISH=`date +%s`
1285         local TIMEOUT=`lctl get_param -n timeout`
1286         local PROCESS=$((FINISH - START))
1287         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1288                error "$FINISH - $START >= $TIMEOUT / 2"
1289         sleep $((TIMEOUT / 2 - PROCESS))
1290         reset_enospc
1291 }
1292 run_test 27v "skip object creation on slow OST ================="
1293
1294 test_27w() { # bug 10997
1295         mkdir -p $DIR/$tdir || error "mkdir failed"
1296         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1297         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1298                 error "stripe size $size != 65536" || true
1299         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1300                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1301 }
1302 run_test 27w "check $SETSTRIPE -S option"
1303
1304 test_27wa() {
1305         [ "$OSTCOUNT" -lt "2" ] &&
1306                 skip_env "skipping multiple stripe count/offset test" && return
1307
1308         mkdir -p $DIR/$tdir || error "mkdir failed"
1309         for i in $(seq 1 $OSTCOUNT); do
1310                 offset=$((i - 1))
1311                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1312                         error "setstripe -c $i -i $offset failed"
1313                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1314                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1315                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1316                 [ $index -ne $offset ] &&
1317                         error "stripe offset $index != $offset" || true
1318         done
1319 }
1320 run_test 27wa "check $SETSTRIPE -c -i options"
1321
1322 test_27x() {
1323         remote_ost_nodsh && skip "remote OST with nodsh" && return
1324         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1325         OFFSET=$(($OSTCOUNT - 1))
1326         OSTIDX=0
1327         local OST=$(ostname_from_index $OSTIDX)
1328
1329         mkdir -p $DIR/$tdir
1330         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1331         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1332         sleep_maxage
1333         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1334         for i in `seq 0 $OFFSET`; do
1335                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1336                 error "OST0 was degraded but new created file still use it"
1337         done
1338         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1339 }
1340 run_test 27x "create files while OST0 is degraded"
1341
1342 test_27y() {
1343         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1344         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1345         remote_ost_nodsh && skip "remote OST with nodsh" && return
1346
1347         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1348         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1349             osc.$mdtosc.prealloc_last_id)
1350         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1351             osc.$mdtosc.prealloc_next_id)
1352         local fcount=$((last_id - next_id))
1353         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1354         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1355
1356         MDS_OSCS=`do_facet $SINGLEMDS lctl dl | awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
1357         OFFSET=$(($OSTCOUNT-1))
1358         OST=-1
1359         for OSC in $MDS_OSCS; do
1360                 if [ $OST == -1 ]; then {
1361                         OST=`osc_to_ost $OSC`
1362                 } else {
1363                         echo $OSC "is Deactivate:"
1364                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1365                 } fi
1366         done
1367
1368         OSTIDX=$(index_from_ostuuid $OST)
1369         mkdir -p $DIR/$tdir
1370         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1371
1372         do_facet ost$((OSTIDX+1)) lctl set_param -n obdfilter.$OST.degraded 1
1373         sleep_maxage
1374         createmany -o $DIR/$tdir/$tfile $fcount
1375         do_facet ost$((OSTIDX+1)) lctl set_param -n obdfilter.$OST.degraded 0
1376
1377         for i in `seq 0 $OFFSET`; do
1378                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "$OSTIDX"` ] || \
1379                       error "files created on deactivated OSTs instead of degraded OST"
1380         done
1381         for OSC in $MDS_OSCS; do
1382                 [ `osc_to_ost $OSC` != $OST  ] && {
1383                         echo $OSC "is activate"
1384                         do_facet $SINGLEMDS lctl --device %$OSC activate
1385                 }
1386         done
1387 }
1388 run_test 27y "create files while OST0 is degraded and the rest inactive"
1389
1390 check_seq_oid()
1391 {
1392         log "check file $1"
1393
1394         lmm_count=$($GETSTRIPE -c $1)
1395         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1396         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1397
1398         local old_ifs="$IFS"
1399         IFS=$'[:]'
1400         fid=($($LFS path2fid $1))
1401         IFS="$old_ifs"
1402
1403         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1404         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1405
1406         # compare lmm_seq and lu_fid->f_seq
1407         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1408         # compare lmm_object_id and lu_fid->oid
1409         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1410
1411         # check the trusted.fid attribute of the OST objects of the file
1412         local have_obdidx=false
1413         local stripe_nr=0
1414         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1415                 # skip lines up to and including "obdidx"
1416                 [ -z "$obdidx" ] && break
1417                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1418                 $have_obdidx || continue
1419
1420                 local ost=$((obdidx + 1))
1421                 local dev=$(ostdevname $ost)
1422
1423                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1424                         echo "Currently only works with ldiskfs-based OSTs"
1425                         continue
1426                 fi
1427
1428                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1429
1430                 #don't unmount/remount the OSTs if we don't need to do that
1431                 #local dir=$(facet_mntpt ost$ost)
1432                 #stop ost$dev
1433                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1434                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1435                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1436                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1437
1438                 local obj_file="O/$seq/d$((oid %32))/$oid"
1439                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1440                            $dev 2>/dev/null" | grep "parent=")
1441
1442                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1443
1444                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1445
1446                 #do_facet ost$ost umount -d $dir
1447                 #start ost$ost $dev $OST_MOUNT_OPTS
1448
1449                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1450                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1451                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1452                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1453                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1454                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1455
1456                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1457                 [ $ff_pseq = $lmm_seq ] ||
1458                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1459                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1460                 [ $ff_poid = $lmm_oid ] ||
1461                         error "FF parent OID $ff_poid != $lmm_oid"
1462                 [ $ff_pstripe = $stripe_nr ] ||
1463                         error "FF stripe $ff_pstripe != $stripe_nr"
1464
1465                 stripe_nr=$((stripe_nr + 1))
1466         done
1467 }
1468
1469 test_27z() {
1470         remote_ost_nodsh && skip "remote OST with nodsh" && return
1471         mkdir -p $DIR/$tdir
1472
1473         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1474                 { error "setstripe -c -1 failed"; return 1; }
1475         # We need to send a write to every object to get parent FID info set.
1476         # This _should_ also work for setattr, but does not currently.
1477         # touch $DIR/$tdir/$tfile-1 ||
1478         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1479                 { error "dd $tfile-1 failed"; return 2; }
1480         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1481                 { error "setstripe -c -1 failed"; return 3; }
1482         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1483                 { error "dd $tfile-2 failed"; return 4; }
1484
1485         # make sure write RPCs have been sent to OSTs
1486         sync; sleep 5; sync
1487
1488         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1489         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1490 }
1491 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1492
1493 test_27A() { # b=19102
1494         local restore_size=$($GETSTRIPE -S $MOUNT)
1495         local restore_count=$($GETSTRIPE -c $MOUNT)
1496         local restore_offset=$($GETSTRIPE -i $MOUNT)
1497         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1498         local default_size=$($GETSTRIPE -S $MOUNT)
1499         local default_count=$($GETSTRIPE -c $MOUNT)
1500         local default_offset=$($GETSTRIPE -i $MOUNT)
1501         local dsize=$((1024 * 1024))
1502         [ $default_size -eq $dsize ] ||
1503                 error "stripe size $default_size != $dsize"
1504         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1505         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1506         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1507 }
1508 run_test 27A "check filesystem-wide default LOV EA values"
1509
1510 # createtest also checks that device nodes are created and
1511 # then visible correctly (#2091)
1512 test_28() { # bug 2091
1513         mkdir $DIR/d28
1514         $CREATETEST $DIR/d28/ct || error
1515 }
1516 run_test 28 "create/mknod/mkdir with bad file types ============"
1517
1518 test_29() {
1519         cancel_lru_locks mdc
1520         mkdir $DIR/d29
1521         touch $DIR/d29/foo
1522         log 'first d29'
1523         ls -l $DIR/d29
1524
1525         declare -i LOCKCOUNTORIG=0
1526         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1527                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1528         done
1529         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1530
1531         declare -i LOCKUNUSEDCOUNTORIG=0
1532         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1533                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1534         done
1535
1536         log 'second d29'
1537         ls -l $DIR/d29
1538         log 'done'
1539
1540         declare -i LOCKCOUNTCURRENT=0
1541         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1542                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1543         done
1544
1545         declare -i LOCKUNUSEDCOUNTCURRENT=0
1546         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1547                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1548         done
1549
1550         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1551                 lctl set_param -n ldlm.dump_namespaces ""
1552                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1553                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1554                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1555                 return 2
1556         fi
1557         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1558                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1559                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1560                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1561                 return 3
1562         fi
1563 }
1564 run_test 29 "IT_GETATTR regression  ============================"
1565
1566 test_30a() { # was test_30
1567         cp `which ls` $DIR || cp /bin/ls $DIR
1568         $DIR/ls / || error
1569         rm $DIR/ls
1570 }
1571 run_test 30a "execute binary from Lustre (execve) =============="
1572
1573 test_30b() {
1574         cp `which ls` $DIR || cp /bin/ls $DIR
1575         chmod go+rx $DIR/ls
1576         $RUNAS $DIR/ls / || error
1577         rm $DIR/ls
1578 }
1579 run_test 30b "execute binary from Lustre as non-root ==========="
1580
1581 test_30c() { # b=22376
1582         cp `which ls` $DIR || cp /bin/ls $DIR
1583         chmod a-rw $DIR/ls
1584         cancel_lru_locks mdc
1585         cancel_lru_locks osc
1586         $RUNAS $DIR/ls / || error
1587         rm -f $DIR/ls
1588 }
1589 run_test 30c "execute binary from Lustre without read perms ===="
1590
1591 test_31a() {
1592         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1593         $CHECKSTAT -a $DIR/f31 || error
1594 }
1595 run_test 31a "open-unlink file =================================="
1596
1597 test_31b() {
1598         touch $DIR/f31 || error
1599         ln $DIR/f31 $DIR/f31b || error
1600         $MULTIOP $DIR/f31b Ouc || error
1601         $CHECKSTAT -t file $DIR/f31 || error
1602 }
1603 run_test 31b "unlink file with multiple links while open ======="
1604
1605 test_31c() {
1606         touch $DIR/f31 || error
1607         ln $DIR/f31 $DIR/f31c || error
1608         multiop_bg_pause $DIR/f31 O_uc || return 1
1609         MULTIPID=$!
1610         $MULTIOP $DIR/f31c Ouc
1611         kill -USR1 $MULTIPID
1612         wait $MULTIPID
1613 }
1614 run_test 31c "open-unlink file with multiple links ============="
1615
1616 test_31d() {
1617         opendirunlink $DIR/d31d $DIR/d31d || error
1618         $CHECKSTAT -a $DIR/d31d || error
1619 }
1620 run_test 31d "remove of open directory ========================="
1621
1622 test_31e() { # bug 2904
1623         check_kernel_version 34 || return 0
1624         openfilleddirunlink $DIR/d31e || error
1625 }
1626 run_test 31e "remove of open non-empty directory ==============="
1627
1628 test_31f() { # bug 4554
1629         set -vx
1630         mkdir $DIR/d31f
1631         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1632         cp /etc/hosts $DIR/d31f
1633         ls -l $DIR/d31f
1634         $GETSTRIPE $DIR/d31f/hosts
1635         multiop_bg_pause $DIR/d31f D_c || return 1
1636         MULTIPID=$!
1637
1638         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1639         mkdir $DIR/d31f
1640         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1641         cp /etc/hosts $DIR/d31f
1642         ls -l $DIR/d31f
1643         $GETSTRIPE $DIR/d31f/hosts
1644         multiop_bg_pause $DIR/d31f D_c || return 1
1645         MULTIPID2=$!
1646
1647         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1648         wait $MULTIPID || error "first opendir $MULTIPID failed"
1649
1650         sleep 6
1651
1652         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1653         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1654         set +vx
1655 }
1656 run_test 31f "remove of open directory with open-unlink file ==="
1657
1658 test_31g() {
1659         echo "-- cross directory link --"
1660         mkdir $DIR/d31g{a,b}
1661         touch $DIR/d31ga/f
1662         ln $DIR/d31ga/f $DIR/d31gb/g
1663         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1664         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1665         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1666         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1667 }
1668 run_test 31g "cross directory link==============="
1669
1670 test_31h() {
1671         echo "-- cross directory link --"
1672         mkdir $DIR/d31h
1673         mkdir $DIR/d31h/dir
1674         touch $DIR/d31h/f
1675         ln $DIR/d31h/f $DIR/d31h/dir/g
1676         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1677         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1678         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1679         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1680 }
1681 run_test 31h "cross directory link under child==============="
1682
1683 test_31i() {
1684         echo "-- cross directory link --"
1685         mkdir $DIR/d31i
1686         mkdir $DIR/d31i/dir
1687         touch $DIR/d31i/dir/f
1688         ln $DIR/d31i/dir/f $DIR/d31i/g
1689         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1690         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1691         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1692         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1693 }
1694 run_test 31i "cross directory link under parent==============="
1695
1696
1697 test_31j() {
1698         mkdir $DIR/d31j
1699         mkdir $DIR/d31j/dir1
1700         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1701         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1702         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1703         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1704         return 0
1705 }
1706 run_test 31j "link for directory==============="
1707
1708
1709 test_31k() {
1710         mkdir $DIR/d31k
1711         touch $DIR/d31k/s
1712         touch $DIR/d31k/exist
1713         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1714         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1715         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1716         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1717         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1718         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1719         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1720         return 0
1721 }
1722 run_test 31k "link to file: the same, non-existing, dir==============="
1723
1724 test_31m() {
1725         mkdir $DIR/d31m
1726         touch $DIR/d31m/s
1727         mkdir $DIR/d31m2
1728         touch $DIR/d31m2/exist
1729         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1730         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1731         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1732         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1733         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1734         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1735         return 0
1736 }
1737 run_test 31m "link to file: the same, non-existing, dir==============="
1738
1739 test_32a() {
1740         echo "== more mountpoints and symlinks ================="
1741         [ -e $DIR/d32a ] && rm -fr $DIR/d32a
1742         mkdir -p $DIR/d32a/ext2-mountpoint
1743         mount -t ext2 -o loop $EXT2_DEV $DIR/d32a/ext2-mountpoint || error
1744         $CHECKSTAT -t dir $DIR/d32a/ext2-mountpoint/.. || error
1745         $UMOUNT $DIR/d32a/ext2-mountpoint || error
1746 }
1747 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
1748
1749 test_32b() {
1750         [ -e $DIR/d32b ] && rm -fr $DIR/d32b
1751         mkdir -p $DIR/d32b/ext2-mountpoint
1752         mount -t ext2 -o loop $EXT2_DEV $DIR/d32b/ext2-mountpoint || error
1753         ls -al $DIR/d32b/ext2-mountpoint/.. || error
1754         $UMOUNT $DIR/d32b/ext2-mountpoint || error
1755 }
1756 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
1757
1758 test_32c() {
1759         [ -e $DIR/d32c ] && rm -fr $DIR/d32c
1760         mkdir -p $DIR/d32c/ext2-mountpoint
1761         mount -t ext2 -o loop $EXT2_DEV $DIR/d32c/ext2-mountpoint || error
1762         mkdir -p $DIR/d32c/d2/test_dir
1763         $CHECKSTAT -t dir $DIR/d32c/ext2-mountpoint/../d2/test_dir || error
1764         $UMOUNT $DIR/d32c/ext2-mountpoint || error
1765 }
1766 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
1767
1768 test_32d() {
1769         [ -e $DIR/d32d ] && rm -fr $DIR/d32d
1770         mkdir -p $DIR/d32d/ext2-mountpoint
1771         mount -t ext2 -o loop $EXT2_DEV $DIR/d32d/ext2-mountpoint || error
1772         mkdir -p $DIR/d32d/d2/test_dir
1773         ls -al $DIR/d32d/ext2-mountpoint/../d2/test_dir || error
1774         $UMOUNT $DIR/d32d/ext2-mountpoint || error
1775 }
1776 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
1777
1778 test_32e() {
1779         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
1780         mkdir -p $DIR/d32e/tmp
1781         TMP_DIR=$DIR/d32e/tmp
1782         ln -s $DIR/d32e $TMP_DIR/symlink11
1783         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1784         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
1785         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
1786 }
1787 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
1788
1789 test_32f() {
1790         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
1791         mkdir -p $DIR/d32f/tmp
1792         TMP_DIR=$DIR/d32f/tmp
1793         ln -s $DIR/d32f $TMP_DIR/symlink11
1794         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1795         ls $DIR/d32f/tmp/symlink11  || error
1796         ls $DIR/d32f/symlink01 || error
1797 }
1798 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
1799
1800 test_32g() {
1801         TMP_DIR=$DIR/$tdir/tmp
1802         mkdir -p $TMP_DIR $DIR/${tdir}2
1803         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1804         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1805         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
1806         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
1807         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
1808         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
1809 }
1810 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1811
1812 test_32h() {
1813         rm -fr $DIR/$tdir $DIR/${tdir}2
1814         TMP_DIR=$DIR/$tdir/tmp
1815         mkdir -p $TMP_DIR $DIR/${tdir}2
1816         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1817         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1818         ls $TMP_DIR/symlink12 || error
1819         ls $DIR/$tdir/symlink02  || error
1820 }
1821 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1822
1823 test_32i() {
1824         [ -e $DIR/d32i ] && rm -fr $DIR/d32i
1825         mkdir -p $DIR/d32i/ext2-mountpoint
1826         mount -t ext2 -o loop $EXT2_DEV $DIR/d32i/ext2-mountpoint || error
1827         touch $DIR/d32i/test_file
1828         $CHECKSTAT -t file $DIR/d32i/ext2-mountpoint/../test_file || error
1829         $UMOUNT $DIR/d32i/ext2-mountpoint || error
1830 }
1831 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
1832
1833 test_32j() {
1834         [ -e $DIR/d32j ] && rm -fr $DIR/d32j
1835         mkdir -p $DIR/d32j/ext2-mountpoint
1836         mount -t ext2 -o loop $EXT2_DEV $DIR/d32j/ext2-mountpoint || error
1837         touch $DIR/d32j/test_file
1838         cat $DIR/d32j/ext2-mountpoint/../test_file || error
1839         $UMOUNT $DIR/d32j/ext2-mountpoint || error
1840 }
1841 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
1842
1843 test_32k() {
1844         rm -fr $DIR/d32k
1845         mkdir -p $DIR/d32k/ext2-mountpoint
1846         mount -t ext2 -o loop $EXT2_DEV $DIR/d32k/ext2-mountpoint
1847         mkdir -p $DIR/d32k/d2
1848         touch $DIR/d32k/d2/test_file || error
1849         $CHECKSTAT -t file $DIR/d32k/ext2-mountpoint/../d2/test_file || error
1850         $UMOUNT $DIR/d32k/ext2-mountpoint || error
1851 }
1852 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
1853
1854 test_32l() {
1855         rm -fr $DIR/d32l
1856         mkdir -p $DIR/d32l/ext2-mountpoint
1857         mount -t ext2 -o loop $EXT2_DEV $DIR/d32l/ext2-mountpoint || error
1858         mkdir -p $DIR/d32l/d2
1859         touch $DIR/d32l/d2/test_file
1860         cat  $DIR/d32l/ext2-mountpoint/../d2/test_file || error
1861         $UMOUNT $DIR/d32l/ext2-mountpoint || error
1862 }
1863 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
1864
1865 test_32m() {
1866         rm -fr $DIR/d32m
1867         mkdir -p $DIR/d32m/tmp
1868         TMP_DIR=$DIR/d32m/tmp
1869         ln -s $DIR $TMP_DIR/symlink11
1870         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1871         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
1872         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
1873 }
1874 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
1875
1876 test_32n() {
1877         rm -fr $DIR/d32n
1878         mkdir -p $DIR/d32n/tmp
1879         TMP_DIR=$DIR/d32n/tmp
1880         ln -s $DIR $TMP_DIR/symlink11
1881         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1882         ls -l $DIR/d32n/tmp/symlink11  || error
1883         ls -l $DIR/d32n/symlink01 || error
1884 }
1885 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
1886
1887 test_32o() {
1888         rm -fr $DIR/d32o $DIR/$tfile
1889         touch $DIR/$tfile
1890         mkdir -p $DIR/d32o/tmp
1891         TMP_DIR=$DIR/d32o/tmp
1892         ln -s $DIR/$tfile $TMP_DIR/symlink12
1893         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1894         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
1895         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
1896         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
1897         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
1898 }
1899 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
1900
1901 test_32p() {
1902     log 32p_1
1903         rm -fr $DIR/d32p
1904     log 32p_2
1905         rm -f $DIR/$tfile
1906     log 32p_3
1907         touch $DIR/$tfile
1908     log 32p_4
1909         mkdir -p $DIR/d32p/tmp
1910     log 32p_5
1911         TMP_DIR=$DIR/d32p/tmp
1912     log 32p_6
1913         ln -s $DIR/$tfile $TMP_DIR/symlink12
1914     log 32p_7
1915         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1916     log 32p_8
1917         cat $DIR/d32p/tmp/symlink12 || error
1918     log 32p_9
1919         cat $DIR/d32p/symlink02 || error
1920     log 32p_10
1921 }
1922 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
1923
1924 test_32q() {
1925         [ -e $DIR/d32q ] && rm -fr $DIR/d32q
1926         mkdir -p $DIR/d32q
1927         touch $DIR/d32q/under_the_mount
1928         mount -t ext2 -o loop $EXT2_DEV $DIR/d32q
1929         ls $DIR/d32q/under_the_mount && error || true
1930         $UMOUNT $DIR/d32q || error
1931 }
1932 run_test 32q "stat follows mountpoints in Lustre (should return error)"
1933
1934 test_32r() {
1935         [ -e $DIR/d32r ] && rm -fr $DIR/d32r
1936         mkdir -p $DIR/d32r
1937         touch $DIR/d32r/under_the_mount
1938         mount -t ext2 -o loop $EXT2_DEV $DIR/d32r
1939         ls $DIR/d32r | grep -q under_the_mount && error || true
1940         $UMOUNT $DIR/d32r || error
1941 }
1942 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
1943
1944 test_33() {
1945         rm -f $DIR/$tfile
1946         touch $DIR/$tfile
1947         chmod 444 $DIR/$tfile
1948         chown $RUNAS_ID $DIR/$tfile
1949         log 33_1
1950         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
1951         log 33_2
1952 }
1953 run_test 33 "write file with mode 444 (should return error) ===="
1954
1955 test_33a() {
1956         rm -fr $DIR/d33
1957         mkdir -p $DIR/d33
1958         chown $RUNAS_ID $DIR/d33
1959         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
1960         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
1961                 error "open RDWR" || true
1962 }
1963 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
1964
1965 test_33b() {
1966         rm -fr $DIR/d33
1967         mkdir -p $DIR/d33
1968         chown $RUNAS_ID $DIR/d33
1969         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
1970 }
1971 run_test 33b "test open file with malformed flags (No panic and return error)"
1972
1973 test_33c() {
1974         local ostnum
1975         local ostname
1976         local write_bytes
1977         local all_zeros
1978
1979         remote_ost_nodsh && skip "remote OST with nodsh" && return
1980         all_zeros=:
1981         rm -fr $DIR/d33
1982         mkdir -p $DIR/d33
1983         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
1984
1985         sync
1986         for ostnum in $(seq $OSTCOUNT); do
1987                 # test-framework's OST numbering is one-based, while Lustre's
1988                 # is zero-based
1989                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
1990                 # Parsing llobdstat's output sucks; we could grep the /proc
1991                 # path, but that's likely to not be as portable as using the
1992                 # llobdstat utility.  So we parse lctl output instead.
1993                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
1994                         obdfilter/$ostname/stats |
1995                         awk '/^write_bytes/ {print $7}' )
1996                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
1997                 if (( ${write_bytes:-0} > 0 ))
1998                 then
1999                         all_zeros=false
2000                         break;
2001                 fi
2002         done
2003
2004         $all_zeros || return 0
2005
2006         # Write four bytes
2007         echo foo > $DIR/d33/bar
2008         # Really write them
2009         sync
2010
2011         # Total up write_bytes after writing.  We'd better find non-zeros.
2012         for ostnum in $(seq $OSTCOUNT); do
2013                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2014                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2015                         obdfilter/$ostname/stats |
2016                         awk '/^write_bytes/ {print $7}' )
2017                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2018                 if (( ${write_bytes:-0} > 0 ))
2019                 then
2020                         all_zeros=false
2021                         break;
2022                 fi
2023         done
2024
2025         if $all_zeros
2026         then
2027                 for ostnum in $(seq $OSTCOUNT); do
2028                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2029                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2030                         do_facet ost$ostnum lctl get_param -n \
2031                                 obdfilter/$ostname/stats
2032                 done
2033                 error "OST not keeping write_bytes stats (b22312)"
2034         fi
2035 }
2036 run_test 33c "test llobdstat and write_bytes"
2037
2038 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2039 test_34a() {
2040         rm -f $DIR/f34
2041         $MCREATE $DIR/f34 || error
2042         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2043         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2044         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2045         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2046 }
2047 run_test 34a "truncate file that has not been opened ==========="
2048
2049 test_34b() {
2050         [ ! -f $DIR/f34 ] && test_34a
2051         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2052         $OPENFILE -f O_RDONLY $DIR/f34
2053         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2054         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2055 }
2056 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2057
2058 test_34c() {
2059         [ ! -f $DIR/f34 ] && test_34a
2060         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2061         $OPENFILE -f O_RDWR $DIR/f34
2062         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2063         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2064 }
2065 run_test 34c "O_RDWR opening file-with-size works =============="
2066
2067 test_34d() {
2068         [ ! -f $DIR/f34 ] && test_34a
2069         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2070         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2071         rm $DIR/f34
2072 }
2073 run_test 34d "write to sparse file ============================="
2074
2075 test_34e() {
2076         rm -f $DIR/f34e
2077         $MCREATE $DIR/f34e || error
2078         $TRUNCATE $DIR/f34e 1000 || error
2079         $CHECKSTAT -s 1000 $DIR/f34e || error
2080         $OPENFILE -f O_RDWR $DIR/f34e
2081         $CHECKSTAT -s 1000 $DIR/f34e || error
2082 }
2083 run_test 34e "create objects, some with size and some without =="
2084
2085 test_34f() { # bug 6242, 6243
2086         SIZE34F=48000
2087         rm -f $DIR/f34f
2088         $MCREATE $DIR/f34f || error
2089         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2090         dd if=$DIR/f34f of=$TMP/f34f
2091         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2092         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2093         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2094         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2095         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2096 }
2097 run_test 34f "read from a file with no objects until EOF ======="
2098
2099 test_34g() {
2100         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2101         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2102         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2103         cancel_lru_locks osc
2104         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2105                 error "wrong size after lock cancel"
2106
2107         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2108         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2109                 error "expanding truncate failed"
2110         cancel_lru_locks osc
2111         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2112                 error "wrong expanded size after lock cancel"
2113 }
2114 run_test 34g "truncate long file ==============================="
2115
2116 test_34h() {
2117         local gid=10
2118         local sz=1000
2119
2120         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2121         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2122         MULTIPID=$!
2123         sleep 2
2124
2125         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2126                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2127                 kill -9 $MULTIPID
2128         fi
2129         wait $MULTIPID
2130         local nsz=`stat -c %s $DIR/$tfile`
2131         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2132 }
2133 run_test 34h "ftruncate file under grouplock should not block"
2134
2135 test_35a() {
2136         cp /bin/sh $DIR/f35a
2137         chmod 444 $DIR/f35a
2138         chown $RUNAS_ID $DIR/f35a
2139         $RUNAS $DIR/f35a && error || true
2140         rm $DIR/f35a
2141 }
2142 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2143
2144 test_36a() {
2145         rm -f $DIR/f36
2146         utime $DIR/f36 || error
2147 }
2148 run_test 36a "MDS utime check (mknod, utime) ==================="
2149
2150 test_36b() {
2151         echo "" > $DIR/f36
2152         utime $DIR/f36 || error
2153 }
2154 run_test 36b "OST utime check (open, utime) ===================="
2155
2156 test_36c() {
2157         rm -f $DIR/d36/f36
2158         mkdir $DIR/d36
2159         chown $RUNAS_ID $DIR/d36
2160         $RUNAS utime $DIR/d36/f36 || error
2161 }
2162 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2163
2164 test_36d() {
2165         [ ! -d $DIR/d36 ] && test_36c
2166         echo "" > $DIR/d36/f36
2167         $RUNAS utime $DIR/d36/f36 || error
2168 }
2169 run_test 36d "non-root OST utime check (open, utime) ==========="
2170
2171 test_36e() {
2172         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2173         mkdir -p $DIR/$tdir
2174         touch $DIR/$tdir/$tfile
2175         $RUNAS utime $DIR/$tdir/$tfile && \
2176                 error "utime worked, expected failure" || true
2177 }
2178 run_test 36e "utime on non-owned file (should return error) ===="
2179
2180 subr_36fh() {
2181         local fl="$1"
2182         local LANG_SAVE=$LANG
2183         local LC_LANG_SAVE=$LC_LANG
2184         export LANG=C LC_LANG=C # for date language
2185
2186         DATESTR="Dec 20  2000"
2187         mkdir -p $DIR/$tdir
2188         lctl set_param fail_loc=$fl
2189         date; date +%s
2190         cp /etc/hosts $DIR/$tdir/$tfile
2191         sync & # write RPC generated with "current" inode timestamp, but delayed
2192         sleep 1
2193         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2194         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2195         cancel_lru_locks osc
2196         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2197         date; date +%s
2198         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2199                 echo "BEFORE: $LS_BEFORE" && \
2200                 echo "AFTER : $LS_AFTER" && \
2201                 echo "WANT  : $DATESTR" && \
2202                 error "$DIR/$tdir/$tfile timestamps changed" || true
2203
2204         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2205 }
2206
2207 test_36f() {
2208         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2209         subr_36fh "0x80000214"
2210 }
2211 run_test 36f "utime on file racing with OST BRW write =========="
2212
2213 test_36g() {
2214         remote_ost_nodsh && skip "remote OST with nodsh" && return
2215         local fmd_max_age
2216         local fmd_before
2217         local fmd_after
2218
2219         mkdir -p $DIR/$tdir
2220         fmd_max_age=$(do_facet ost1 \
2221                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2222                 head -n 1")
2223
2224         fmd_before=$(do_facet ost1 \
2225                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2226         touch $DIR/$tdir/$tfile
2227         sleep $((fmd_max_age + 12))
2228         fmd_after=$(do_facet ost1 \
2229                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2230
2231         echo "fmd_before: $fmd_before"
2232         echo "fmd_after: $fmd_after"
2233         [ "$fmd_after" -gt "$fmd_before" ] && \
2234                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2235                 error "fmd didn't expire after ping" || true
2236 }
2237 run_test 36g "filter mod data cache expiry ====================="
2238
2239 test_36h() {
2240         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2241         subr_36fh "0x80000227"
2242 }
2243 run_test 36h "utime on file racing with OST BRW write =========="
2244
2245 test_37() {
2246         mkdir -p $DIR/$tdir
2247         echo f > $DIR/$tdir/fbugfile
2248         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2249         ls $DIR/$tdir | grep "\<fbugfile\>" && error
2250         $UMOUNT $DIR/$tdir || error
2251         rm -f $DIR/$tdir/fbugfile || error
2252 }
2253 run_test 37 "ls a mounted file system to check old content ====="
2254
2255 test_38() {
2256         local file=$DIR/$tfile
2257         touch $file
2258         openfile -f O_DIRECTORY $file
2259         local RC=$?
2260         local ENOTDIR=20
2261         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2262         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2263 }
2264 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2265
2266 test_39() {
2267         touch $DIR/$tfile
2268         touch $DIR/${tfile}2
2269 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2270 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2271 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2272         sleep 2
2273         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2274         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2275                 echo "mtime"
2276                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2277                 echo "atime"
2278                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2279                 echo "ctime"
2280                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2281                 error "O_TRUNC didn't change timestamps"
2282         fi
2283 }
2284 run_test 39 "mtime changed on create ==========================="
2285
2286 test_39b() {
2287         mkdir -p $DIR/$tdir
2288         cp -p /etc/passwd $DIR/$tdir/fopen
2289         cp -p /etc/passwd $DIR/$tdir/flink
2290         cp -p /etc/passwd $DIR/$tdir/funlink
2291         cp -p /etc/passwd $DIR/$tdir/frename
2292         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2293
2294         sleep 1
2295         echo "aaaaaa" >> $DIR/$tdir/fopen
2296         echo "aaaaaa" >> $DIR/$tdir/flink
2297         echo "aaaaaa" >> $DIR/$tdir/funlink
2298         echo "aaaaaa" >> $DIR/$tdir/frename
2299
2300         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2301         local link_new=`stat -c %Y $DIR/$tdir/flink`
2302         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2303         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2304
2305         cat $DIR/$tdir/fopen > /dev/null
2306         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2307         rm -f $DIR/$tdir/funlink2
2308         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2309
2310         for (( i=0; i < 2; i++ )) ; do
2311                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2312                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2313                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2314                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2315
2316                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2317                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2318                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2319                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2320
2321                 cancel_lru_locks osc
2322                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2323         done
2324 }
2325 run_test 39b "mtime change on open, link, unlink, rename  ======"
2326
2327 # this should be set to past
2328 TEST_39_MTIME=`date -d "1 year ago" +%s`
2329
2330 # bug 11063
2331 test_39c() {
2332         touch $DIR1/$tfile
2333         sleep 2
2334         local mtime0=`stat -c %Y $DIR1/$tfile`
2335
2336         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2337         local mtime1=`stat -c %Y $DIR1/$tfile`
2338         [ "$mtime1" = $TEST_39_MTIME ] || \
2339                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2340
2341         local d1=`date +%s`
2342         echo hello >> $DIR1/$tfile
2343         local d2=`date +%s`
2344         local mtime2=`stat -c %Y $DIR1/$tfile`
2345         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2346                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2347
2348         mv $DIR1/$tfile $DIR1/$tfile-1
2349
2350         for (( i=0; i < 2; i++ )) ; do
2351                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2352                 [ "$mtime2" = "$mtime3" ] || \
2353                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2354
2355                 cancel_lru_locks osc
2356                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2357         done
2358 }
2359 run_test 39c "mtime change on rename ==========================="
2360
2361 # bug 21114
2362 test_39d() {
2363         touch $DIR1/$tfile
2364
2365         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2366
2367         for (( i=0; i < 2; i++ )) ; do
2368                 local mtime=`stat -c %Y $DIR1/$tfile`
2369                 [ $mtime = $TEST_39_MTIME ] || \
2370                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2371
2372                 cancel_lru_locks osc
2373                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2374         done
2375 }
2376 run_test 39d "create, utime, stat =============================="
2377
2378 # bug 21114
2379 test_39e() {
2380         touch $DIR1/$tfile
2381         local mtime1=`stat -c %Y $DIR1/$tfile`
2382
2383         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2384
2385         for (( i=0; i < 2; i++ )) ; do
2386                 local mtime2=`stat -c %Y $DIR1/$tfile`
2387                 [ $mtime2 = $TEST_39_MTIME ] || \
2388                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2389
2390                 cancel_lru_locks osc
2391                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2392         done
2393 }
2394 run_test 39e "create, stat, utime, stat ========================"
2395
2396 # bug 21114
2397 test_39f() {
2398         touch $DIR1/$tfile
2399         mtime1=`stat -c %Y $DIR1/$tfile`
2400
2401         sleep 2
2402         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2403
2404         for (( i=0; i < 2; i++ )) ; do
2405                 local mtime2=`stat -c %Y $DIR1/$tfile`
2406                 [ $mtime2 = $TEST_39_MTIME ] || \
2407                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2408
2409                 cancel_lru_locks osc
2410                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2411         done
2412 }
2413 run_test 39f "create, stat, sleep, utime, stat ================="
2414
2415 # bug 11063
2416 test_39g() {
2417         echo hello >> $DIR1/$tfile
2418         local mtime1=`stat -c %Y $DIR1/$tfile`
2419
2420         sleep 2
2421         chmod o+r $DIR1/$tfile
2422
2423         for (( i=0; i < 2; i++ )) ; do
2424                 local mtime2=`stat -c %Y $DIR1/$tfile`
2425                 [ "$mtime1" = "$mtime2" ] || \
2426                         error "lost mtime: $mtime2, should be $mtime1"
2427
2428                 cancel_lru_locks osc
2429                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2430         done
2431 }
2432 run_test 39g "write, chmod, stat ==============================="
2433
2434 # bug 11063
2435 test_39h() {
2436         touch $DIR1/$tfile
2437         sleep 1
2438
2439         local d1=`date`
2440         echo hello >> $DIR1/$tfile
2441         local mtime1=`stat -c %Y $DIR1/$tfile`
2442
2443         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2444         local d2=`date`
2445         if [ "$d1" != "$d2" ]; then
2446                 echo "write and touch not within one second"
2447         else
2448                 for (( i=0; i < 2; i++ )) ; do
2449                         local mtime2=`stat -c %Y $DIR1/$tfile`
2450                         [ "$mtime2" = $TEST_39_MTIME ] || \
2451                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2452
2453                         cancel_lru_locks osc
2454                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2455                 done
2456         fi
2457 }
2458 run_test 39h "write, utime within one second, stat ============="
2459
2460 test_39i() {
2461         touch $DIR1/$tfile
2462         sleep 1
2463
2464         echo hello >> $DIR1/$tfile
2465         local mtime1=`stat -c %Y $DIR1/$tfile`
2466
2467         mv $DIR1/$tfile $DIR1/$tfile-1
2468
2469         for (( i=0; i < 2; i++ )) ; do
2470                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2471
2472                 [ "$mtime1" = "$mtime2" ] || \
2473                         error "lost mtime: $mtime2, should be $mtime1"
2474
2475                 cancel_lru_locks osc
2476                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2477         done
2478 }
2479 run_test 39i "write, rename, stat =============================="
2480
2481 test_39j() {
2482         start_full_debug_logging
2483         touch $DIR1/$tfile
2484         sleep 1
2485
2486         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2487         lctl set_param fail_loc=0x80000412
2488         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2489                 error "multiop failed"
2490         local multipid=$!
2491         local mtime1=`stat -c %Y $DIR1/$tfile`
2492
2493         mv $DIR1/$tfile $DIR1/$tfile-1
2494
2495         kill -USR1 $multipid
2496         wait $multipid || error "multiop close failed"
2497
2498         for (( i=0; i < 2; i++ )) ; do
2499                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2500                 [ "$mtime1" = "$mtime2" ] ||
2501                         error "mtime is lost on close: $mtime2, " \
2502                               "should be $mtime1"
2503
2504                 cancel_lru_locks osc
2505                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2506         done
2507         lctl set_param fail_loc=0
2508         stop_full_debug_logging
2509 }
2510 run_test 39j "write, rename, close, stat ======================="
2511
2512 test_39k() {
2513         touch $DIR1/$tfile
2514         sleep 1
2515
2516         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2517         local multipid=$!
2518         local mtime1=`stat -c %Y $DIR1/$tfile`
2519
2520         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2521
2522         kill -USR1 $multipid
2523         wait $multipid || error "multiop close failed"
2524
2525         for (( i=0; i < 2; i++ )) ; do
2526                 local mtime2=`stat -c %Y $DIR1/$tfile`
2527
2528                 [ "$mtime2" = $TEST_39_MTIME ] || \
2529                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2530
2531                 cancel_lru_locks osc
2532                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2533         done
2534 }
2535 run_test 39k "write, utime, close, stat ========================"
2536
2537 # this should be set to future
2538 TEST_39_ATIME=`date -d "1 year" +%s`
2539
2540 is_sles11()                                             # LU-1783
2541 {
2542         if [ -r /etc/SuSE-release ]
2543         then
2544                 local vers=`grep VERSION /etc/SuSE-release | awk '{print $3}'`
2545                 local patchlev=`grep PATCHLEVEL /etc/SuSE-release \
2546                         | awk '{print $3}'`
2547                 if [ $vers -eq 11 ] && [ $patchlev -eq 1 ]
2548                 then
2549                         return 0
2550                 fi
2551         fi
2552         return 1
2553 }
2554
2555 test_39l() {
2556         is_sles11 && skip "SLES 11 SP1" && return       # LU-1783
2557         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2558         local atime_diff=$(do_facet $SINGLEMDS lctl get_param -n mdd.*.atime_diff)
2559
2560         mkdir -p $DIR/$tdir
2561
2562         # test setting directory atime to future
2563         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2564         local atime=$(stat -c %X $DIR/$tdir)
2565         [ "$atime" = $TEST_39_ATIME ] || \
2566                 error "atime is not set to future: $atime, should be $TEST_39_ATIME"
2567
2568         # test setting directory atime from future to now
2569         local d1=$(date +%s)
2570         ls $DIR/$tdir
2571         local d2=$(date +%s)
2572
2573         cancel_lru_locks mdc
2574         atime=$(stat -c %X $DIR/$tdir)
2575         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2576                 error "atime is not updated from future: $atime, should be $d1<atime<$d2"
2577
2578         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=2
2579         sleep 3
2580
2581         # test setting directory atime when now > dir atime + atime_diff
2582         d1=$(date +%s)
2583         ls $DIR/$tdir
2584         d2=$(date +%s)
2585         cancel_lru_locks mdc
2586         atime=$(stat -c %X $DIR/$tdir)
2587         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2588                 error "atime is not updated  : $atime, should be $d2"
2589
2590         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=60
2591         sleep 3
2592
2593         # test not setting directory atime when now < dir atime + atime_diff
2594         ls $DIR/$tdir
2595         cancel_lru_locks mdc
2596         atime=$(stat -c %X $DIR/$tdir)
2597         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2598                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2599
2600         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=$atime_diff
2601 }
2602 run_test 39l "directory atime update ==========================="
2603
2604 test_39m() {
2605         touch $DIR1/$tfile
2606         sleep 2
2607         local far_past_mtime=$(date -d "May 29 1953" +%s)
2608         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2609
2610         touch -m -d @$far_past_mtime $DIR1/$tfile
2611         touch -a -d @$far_past_atime $DIR1/$tfile
2612
2613         for (( i=0; i < 2; i++ )) ; do
2614                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2615                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2616                         error "atime or mtime set incorrectly"
2617
2618                 cancel_lru_locks osc
2619                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2620         done
2621 }
2622 run_test 39m "test atime and mtime before 1970"
2623
2624 test_40() {
2625         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2626         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2627         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2628 }
2629 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2630
2631 test_41() {
2632         # bug 1553
2633         small_write $DIR/f41 18
2634 }
2635 run_test 41 "test small file write + fstat ====================="
2636
2637 count_ost_writes() {
2638         lctl get_param -n osc.*.stats |
2639             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2640 }
2641
2642 # decent default
2643 WRITEBACK_SAVE=500
2644 DIRTY_RATIO_SAVE=40
2645 MAX_DIRTY_RATIO=50
2646 BG_DIRTY_RATIO_SAVE=10
2647 MAX_BG_DIRTY_RATIO=25
2648
2649 start_writeback() {
2650         trap 0
2651         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2652         # dirty_ratio, dirty_background_ratio
2653         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2654                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2655                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2656                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2657         else
2658                 # if file not here, we are a 2.4 kernel
2659                 kill -CONT `pidof kupdated`
2660         fi
2661 }
2662
2663 stop_writeback() {
2664         # setup the trap first, so someone cannot exit the test at the
2665         # exact wrong time and mess up a machine
2666         trap start_writeback EXIT
2667         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2668         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2669                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2670                 sysctl -w vm.dirty_writeback_centisecs=0
2671                 sysctl -w vm.dirty_writeback_centisecs=0
2672                 # save and increase /proc/sys/vm/dirty_ratio
2673                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2674                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2675                 # save and increase /proc/sys/vm/dirty_background_ratio
2676                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2677                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2678         else
2679                 # if file not here, we are a 2.4 kernel
2680                 kill -STOP `pidof kupdated`
2681         fi
2682 }
2683
2684 # ensure that all stripes have some grant before we test client-side cache
2685 setup_test42() {
2686         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2687                 dd if=/dev/zero of=$i bs=4k count=1
2688                 rm $i
2689         done
2690 }
2691
2692 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
2693 # file truncation, and file removal.
2694 test_42a() {
2695         setup_test42
2696         cancel_lru_locks osc
2697         stop_writeback
2698         sync; sleep 1; sync # just to be safe
2699         BEFOREWRITES=`count_ost_writes`
2700         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
2701         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
2702         AFTERWRITES=`count_ost_writes`
2703         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2704                 error "$BEFOREWRITES < $AFTERWRITES"
2705         start_writeback
2706 }
2707 run_test 42a "ensure that we don't flush on close =============="
2708
2709 test_42b() {
2710         setup_test42
2711         cancel_lru_locks osc
2712         stop_writeback
2713         sync
2714         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
2715         BEFOREWRITES=`count_ost_writes`
2716         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
2717         AFTERWRITES=`count_ost_writes`
2718         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2719                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
2720         fi
2721         BEFOREWRITES=`count_ost_writes`
2722         sync || error "sync: $?"
2723         AFTERWRITES=`count_ost_writes`
2724         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2725                 error "$BEFOREWRITES < $AFTERWRITES on sync"
2726         fi
2727         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
2728         start_writeback
2729         return 0
2730 }
2731 run_test 42b "test destroy of file with cached dirty data ======"
2732
2733 # if these tests just want to test the effect of truncation,
2734 # they have to be very careful.  consider:
2735 # - the first open gets a {0,EOF}PR lock
2736 # - the first write conflicts and gets a {0, count-1}PW
2737 # - the rest of the writes are under {count,EOF}PW
2738 # - the open for truncate tries to match a {0,EOF}PR
2739 #   for the filesize and cancels the PWs.
2740 # any number of fixes (don't get {0,EOF} on open, match
2741 # composite locks, do smarter file size management) fix
2742 # this, but for now we want these tests to verify that
2743 # the cancellation with truncate intent works, so we
2744 # start the file with a full-file pw lock to match against
2745 # until the truncate.
2746 trunc_test() {
2747         test=$1
2748         file=$DIR/$test
2749         offset=$2
2750         cancel_lru_locks osc
2751         stop_writeback
2752         # prime the file with 0,EOF PW to match
2753         touch $file
2754         $TRUNCATE $file 0
2755         sync; sync
2756         # now the real test..
2757         dd if=/dev/zero of=$file bs=1024 count=100
2758         BEFOREWRITES=`count_ost_writes`
2759         $TRUNCATE $file $offset
2760         cancel_lru_locks osc
2761         AFTERWRITES=`count_ost_writes`
2762         start_writeback
2763 }
2764
2765 test_42c() {
2766         trunc_test 42c 1024
2767         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
2768             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
2769         rm $file
2770 }
2771 run_test 42c "test partial truncate of file with cached dirty data"
2772
2773 test_42d() {
2774         trunc_test 42d 0
2775         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2776             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
2777         rm $file
2778 }
2779 run_test 42d "test complete truncate of file with cached dirty data"
2780
2781 test_42e() { # bug22074
2782         local TDIR=$DIR/${tdir}e
2783         local pagesz=$(page_size)
2784         local pages=16 # hardcoded 16 pages, don't change it.
2785         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
2786         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
2787         local max_dirty_mb
2788         local warmup_files
2789
2790         mkdir -p $TDIR
2791         $SETSTRIPE -c 1 $TDIR
2792         createmany -o $TDIR/f $files
2793
2794         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
2795
2796         # we assume that with $OSTCOUNT files, at least one of them will
2797         # be allocated on OST0.
2798         warmup_files=$((OSTCOUNT * max_dirty_mb))
2799         createmany -o $TDIR/w $warmup_files
2800
2801         # write a large amount of data into one file and sync, to get good
2802         # avail_grant number from OST.
2803         for ((i=0; i<$warmup_files; i++)); do
2804                 idx=$($GETSTRIPE -i $TDIR/w$i)
2805                 [ $idx -ne 0 ] && continue
2806                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
2807                 break
2808         done
2809         [ $i -gt $warmup_files ] && error "OST0 is still cold"
2810         sync
2811         $LCTL get_param $proc_osc0/cur_dirty_bytes
2812         $LCTL get_param $proc_osc0/cur_grant_bytes
2813
2814         # create as much dirty pages as we can while not to trigger the actual
2815         # RPCs directly. but depends on the env, VFS may trigger flush during this
2816         # period, hopefully we are good.
2817         for ((i=0; i<$warmup_files; i++)); do
2818                 idx=$($GETSTRIPE -i $TDIR/w$i)
2819                 [ $idx -ne 0 ] && continue
2820                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
2821         done
2822         $LCTL get_param $proc_osc0/cur_dirty_bytes
2823         $LCTL get_param $proc_osc0/cur_grant_bytes
2824
2825         # perform the real test
2826         $LCTL set_param $proc_osc0/rpc_stats 0
2827         for ((;i<$files; i++)); do
2828                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
2829                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
2830         done
2831         sync
2832         $LCTL get_param $proc_osc0/rpc_stats
2833
2834         local percent=0
2835         local have_ppr=false
2836         $LCTL get_param $proc_osc0/rpc_stats |
2837                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
2838                         # skip lines until we are at the RPC histogram data
2839                         [ "$PPR" == "pages" ] && have_ppr=true && continue
2840                         $have_ppr || continue
2841
2842                         # we only want the percent stat for < 16 pages
2843                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
2844
2845                         percent=$((percent + WPCT))
2846                         if [ $percent -gt 15 ]; then
2847                                 error "less than 16-pages write RPCs" \
2848                                       "$percent% > 15%"
2849                                 break
2850                         fi
2851                 done
2852         rm -rf $TDIR
2853 }
2854 run_test 42e "verify sub-RPC writes are not done synchronously"
2855
2856 test_43() {
2857         mkdir -p $DIR/$tdir
2858         cp -p /bin/ls $DIR/$tdir/$tfile
2859         $MULTIOP $DIR/$tdir/$tfile Ow_c &
2860         pid=$!
2861         # give multiop a chance to open
2862         sleep 1
2863
2864         $DIR/$tdir/$tfile && error || true
2865         kill -USR1 $pid
2866 }
2867 run_test 43 "execution of file opened for write should return -ETXTBSY"
2868
2869 test_43a() {
2870         mkdir -p $DIR/d43
2871         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2872         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2873         MULTIOP_PID=$!
2874         $MULTIOP $DIR/d43/multiop Oc && error "expected error, got success"
2875         kill -USR1 $MULTIOP_PID || return 2
2876         wait $MULTIOP_PID || return 3
2877         rm $TMP/test43.junk
2878 }
2879 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
2880
2881 test_43b() {
2882         mkdir -p $DIR/d43
2883         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2884         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2885         MULTIOP_PID=$!
2886         $TRUNCATE $DIR/d43/multiop 0 && error "expected error, got success"
2887         kill -USR1 $MULTIOP_PID || return 2
2888         wait $MULTIOP_PID || return 3
2889         rm $TMP/test43.junk
2890 }
2891 run_test 43b "truncate of file being executed should return -ETXTBSY"
2892
2893 test_43c() {
2894         local testdir="$DIR/d43c"
2895         mkdir -p $testdir
2896         cp $SHELL $testdir/
2897         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
2898                 ( cd $testdir && md5sum -c)
2899 }
2900 run_test 43c "md5sum of copy into lustre========================"
2901
2902 test_44() {
2903         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
2904         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
2905         dd if=$DIR/f1 bs=4k count=1 > /dev/null
2906 }
2907 run_test 44 "zero length read from a sparse stripe ============="
2908
2909 test_44a() {
2910     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
2911                          awk '{print $2}'`
2912     [ -z "$nstripe" ] && skip "can't get stripe info" && return
2913     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
2914     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
2915                       awk '{print $2}'`
2916     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
2917         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
2918     fi
2919
2920     OFFSETS="0 $((stride/2)) $((stride-1))"
2921     for offset in $OFFSETS ; do
2922       for i in `seq 0 $((nstripe-1))`; do
2923         local GLOBALOFFSETS=""
2924         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
2925         local myfn=$DIR/d44a-$size
2926         echo "--------writing $myfn at $size"
2927         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
2928         GLOBALOFFSETS="$GLOBALOFFSETS $size"
2929         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2930                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2931
2932         for j in `seq 0 $((nstripe-1))`; do
2933             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
2934             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
2935             GLOBALOFFSETS="$GLOBALOFFSETS $size"
2936         done
2937         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2938                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2939         rm -f $myfn
2940       done
2941     done
2942 }
2943 run_test 44a "test sparse pwrite ==============================="
2944
2945 dirty_osc_total() {
2946         tot=0
2947         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
2948                 tot=$(($tot + $d))
2949         done
2950         echo $tot
2951 }
2952 do_dirty_record() {
2953         before=`dirty_osc_total`
2954         echo executing "\"$*\""
2955         eval $*
2956         after=`dirty_osc_total`
2957         echo before $before, after $after
2958 }
2959 test_45() {
2960         f="$DIR/f45"
2961         # Obtain grants from OST if it supports it
2962         echo blah > ${f}_grant
2963         stop_writeback
2964         sync
2965         do_dirty_record "echo blah > $f"
2966         [ $before -eq $after ] && error "write wasn't cached"
2967         do_dirty_record "> $f"
2968         [ $before -gt $after ] || error "truncate didn't lower dirty count"
2969         do_dirty_record "echo blah > $f"
2970         [ $before -eq $after ] && error "write wasn't cached"
2971         do_dirty_record "sync"
2972         [ $before -gt $after ] || error "writeback didn't lower dirty count"
2973         do_dirty_record "echo blah > $f"
2974         [ $before -eq $after ] && error "write wasn't cached"
2975         do_dirty_record "cancel_lru_locks osc"
2976         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
2977         start_writeback
2978 }
2979 run_test 45 "osc io page accounting ============================"
2980
2981 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
2982 # test tickles a bug where re-dirtying a page was failing to be mapped to the
2983 # objects offset and an assert hit when an rpc was built with 1023's mapped
2984 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
2985 test_46() {
2986         f="$DIR/f46"
2987         stop_writeback
2988         sync
2989         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2990         sync
2991         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
2992         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2993         sync
2994         start_writeback
2995 }
2996 run_test 46 "dirtying a previously written page ================"
2997
2998 # test_47 is removed "Device nodes check" is moved to test_28
2999
3000 test_48a() { # bug 2399
3001         check_kernel_version 34 || return 0
3002         mkdir -p $DIR/d48a
3003         cd $DIR/d48a
3004         mv $DIR/d48a $DIR/d48.new || error "move directory failed"
3005         mkdir $DIR/d48a || error "recreate directory failed"
3006         touch foo || error "'touch foo' failed after recreating cwd"
3007         mkdir bar || error "'mkdir foo' failed after recreating cwd"
3008         if check_kernel_version 44; then
3009                 touch .foo || error "'touch .foo' failed after recreating cwd"
3010                 mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3011         fi
3012         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3013         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3014         cd . || error "'cd .' failed after recreating cwd"
3015         mkdir . && error "'mkdir .' worked after recreating cwd"
3016         rmdir . && error "'rmdir .' worked after recreating cwd"
3017         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3018         cd .. || error "'cd ..' failed after recreating cwd"
3019 }
3020 run_test 48a "Access renamed working dir (should return errors)="
3021
3022 test_48b() { # bug 2399
3023         check_kernel_version 34 || return 0
3024         mkdir -p $DIR/d48b
3025         cd $DIR/d48b
3026         rmdir $DIR/d48b || error "remove cwd $DIR/d48b failed"
3027         touch foo && error "'touch foo' worked after removing cwd"
3028         mkdir foo && error "'mkdir foo' worked after removing cwd"
3029         if check_kernel_version 44; then
3030                 touch .foo && error "'touch .foo' worked after removing cwd"
3031                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3032         fi
3033         ls . > /dev/null && error "'ls .' worked after removing cwd"
3034         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3035         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3036         mkdir . && error "'mkdir .' worked after removing cwd"
3037         rmdir . && error "'rmdir .' worked after removing cwd"
3038         ln -s . foo && error "'ln -s .' worked after removing cwd"
3039         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3040 }
3041 run_test 48b "Access removed working dir (should return errors)="
3042
3043 test_48c() { # bug 2350
3044         check_kernel_version 36 || return 0
3045         #lctl set_param debug=-1
3046         #set -vx
3047         mkdir -p $DIR/d48c/dir
3048         cd $DIR/d48c/dir
3049         $TRACE rmdir $DIR/d48c/dir || error "remove cwd $DIR/d48c/dir failed"
3050         $TRACE touch foo && error "'touch foo' worked after removing cwd"
3051         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
3052         if check_kernel_version 44; then
3053                 touch .foo && error "'touch .foo' worked after removing cwd"
3054                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3055         fi
3056         $TRACE ls . && error "'ls .' worked after removing cwd"
3057         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3058         is_patchless || ( $TRACE cd . && error "'cd .' worked after removing cwd" )
3059         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
3060         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3061         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3062         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3063 }
3064 run_test 48c "Access removed working subdir (should return errors)"
3065
3066 test_48d() { # bug 2350
3067         check_kernel_version 36 || return 0
3068         #lctl set_param debug=-1
3069         #set -vx
3070         mkdir -p $DIR/d48d/dir
3071         cd $DIR/d48d/dir
3072         $TRACE rmdir $DIR/d48d/dir || error "remove cwd $DIR/d48d/dir failed"
3073         $TRACE rmdir $DIR/d48d || error "remove parent $DIR/d48d failed"
3074         $TRACE touch foo && error "'touch foo' worked after removing parent"
3075         $TRACE mkdir foo && error "'mkdir foo' worked after removing parent"
3076         if check_kernel_version 44; then
3077                 touch .foo && error "'touch .foo' worked after removing parent"
3078                 mkdir .foo && error "'mkdir .foo' worked after removing parent"
3079         fi
3080         $TRACE ls . && error "'ls .' worked after removing parent"
3081         $TRACE ls .. && error "'ls ..' worked after removing parent"
3082         is_patchless || ( $TRACE cd . && error "'cd .' worked after recreate parent" )
3083         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
3084         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3085         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3086         is_patchless || ( $TRACE cd .. && error "'cd ..' worked after removing parent" || true )
3087 }
3088 run_test 48d "Access removed parent subdir (should return errors)"
3089
3090 test_48e() { # bug 4134
3091         check_kernel_version 41 || return 0
3092         #lctl set_param debug=-1
3093         #set -vx
3094         mkdir -p $DIR/d48e/dir
3095         cd $DIR/d48e/dir
3096         $TRACE rmdir $DIR/d48e/dir || error "remove cwd $DIR/d48e/dir failed"
3097         $TRACE rmdir $DIR/d48e || error "remove parent $DIR/d48e failed"
3098         $TRACE touch $DIR/d48e || error "'touch $DIR/d48e' failed"
3099         $TRACE chmod +x $DIR/d48e || error "'chmod +x $DIR/d48e' failed"
3100         # On a buggy kernel addition of "touch foo" after cd .. will
3101         # produce kernel oops in lookup_hash_it
3102         touch ../foo && error "'cd ..' worked after recreate parent"
3103         cd $DIR
3104         $TRACE rm $DIR/d48e || error "rm '$DIR/d48e' failed"
3105 }
3106 run_test 48e "Access to recreated parent subdir (should return errors)"
3107
3108 test_49() { # LU-1030
3109         # get ost1 size - lustre-OST0000
3110         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3111         # write 800M at maximum
3112         [ $ost1_size -gt 819200 ] && ost1_size=819200
3113
3114         lfs setstripe -c 1 -i 0 $DIR/$tfile
3115         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3116         local dd_pid=$!
3117
3118         # change max_pages_per_rpc while writing the file
3119         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3120         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3121         # loop until dd process exits
3122         while ps ax -opid | grep -wq $dd_pid; do
3123                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3124                 sleep $((RANDOM % 5 + 1))
3125         done
3126         # restore original max_pages_per_rpc
3127         $LCTL set_param $osc1_mppc=$orig_mppc
3128         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3129 }
3130 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3131
3132 test_50() {
3133         # bug 1485
3134         mkdir $DIR/d50
3135         cd $DIR/d50
3136         ls /proc/$$/cwd || error
3137 }
3138 run_test 50 "special situations: /proc symlinks  ==============="
3139
3140 test_51a() {    # was test_51
3141         # bug 1516 - create an empty entry right after ".." then split dir
3142         mkdir $DIR/d51
3143         touch $DIR/d51/foo
3144         $MCREATE $DIR/d51/bar
3145         rm $DIR/d51/foo
3146         createmany -m $DIR/d51/longfile 201
3147         FNUM=202
3148         while [ `ls -sd $DIR/d51 | awk '{ print $1 }'` -eq 4 ]; do
3149                 $MCREATE $DIR/d51/longfile$FNUM
3150                 FNUM=$(($FNUM + 1))
3151                 echo -n "+"
3152         done
3153         echo
3154         ls -l $DIR/d51 > /dev/null || error
3155 }
3156 run_test 51a "special situations: split htree with empty entry =="
3157
3158 export NUMTEST=70000
3159 test_51b() {
3160         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3161         [ $NUMFREE -lt 21000 ] && \
3162                 skip "not enough free inodes ($NUMFREE)" && \
3163                 return
3164
3165         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 50))
3166
3167         mkdir -p $DIR/d51b
3168         createmany -d $DIR/d51b/t- $NUMTEST
3169 }
3170 run_test 51b "mkdir .../t-0 --- .../t-$NUMTEST ===================="
3171
3172 test_51ba() { # LU-993
3173         local BASE=$DIR/d51b
3174         # unlink all but 100 subdirectories, then check it still works
3175         local LEFT=100
3176         local DELETE=$((NUMTEST - LEFT))
3177
3178         # continue on to run this test even if 51b didn't finish,
3179         # just to delete the many subdirectories created.
3180         ! [ -d "${BASE}/t-1" ] && skip "test_51b() not run" && return 0
3181
3182         # for ldiskfs the nlink count should be 1, but this is OSD specific
3183         # and so this is listed for informational purposes only
3184         log "nlink before: $(stat -c %h $BASE)"
3185         unlinkmany -d $BASE/t- $DELETE ||
3186                 error "unlink of first $DELETE subdirs failed"
3187
3188         log "nlink between: $(stat -c %h $BASE)"
3189         local FOUND=$(ls -l ${BASE} | wc -l)
3190         FOUND=$((FOUND - 1))  # trim the first line of ls output
3191         [ $FOUND -ne $LEFT ] &&
3192                 error "can't find subdirs: found only $FOUND/$LEFT"
3193
3194         unlinkmany -d $BASE/t- $DELETE $LEFT ||
3195                 error "unlink of second $LEFT subdirs failed"
3196         log "nlink after: $(stat -c %h $BASE)"
3197 }
3198 run_test 51ba "rmdir .../t-0 --- .../t-$NUMTEST"
3199
3200 test_51bb() {
3201         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3202
3203         local ndirs=${TEST51BB_NDIRS:-10}
3204         local nfiles=${TEST51BB_NFILES:-100}
3205
3206         local numfree=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3207
3208         [ $numfree -lt $(( ndirs * nfiles)) ] && \
3209                 nfiles=$(( numfree / ndirs - 10 ))
3210
3211         local dir=$DIR/d51bb
3212         mkdir -p $dir
3213         local savePOLICY=$(lctl get_param -n lmv.*.placement)
3214         lctl set_param -n lmv.*.placement=CHAR
3215
3216         lfs df -i $dir
3217         local IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3218         OLDUSED=($IUSED)
3219
3220         declare -a dirs
3221         for ((i=0; i < $ndirs; i++)); do
3222                 dirs[i]=$dir/$RANDOM
3223                 echo Creating directory ${dirs[i]}
3224                 mkdir -p ${dirs[i]}
3225                 ls $dir
3226                 echo Creating $nfiles in dir ${dirs[i]} ...
3227                 echo "createmany -o ${dirs[i]}/$tfile- $nfiles"
3228                 createmany -o ${dirs[i]}/$tfile- $nfiles
3229         done
3230         ls $dir
3231
3232         sleep 1
3233
3234         IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3235         NEWUSED=($IUSED)
3236
3237         local rc=0
3238         for ((i=0; i<${#NEWUSED[@]}; i++)); do
3239                 echo "mds $i: inodes count OLD ${OLDUSED[$i]} NEW ${NEWUSED[$i]}"
3240                 [ ${OLDUSED[$i]} -lt ${NEWUSED[$i]} ] || rc=$((rc + 1))
3241         done
3242
3243         lctl set_param -n lmv.*.placement=$savePOLICY
3244
3245         [ $rc -ne $MDSCOUNT ] || \
3246                 error "Objects/inodes are not distributed over all mds servers"
3247 }
3248 run_test 51bb "mkdir createmany CMD $MDSCOUNT  ===================="
3249
3250 test_51d() {
3251         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3252         mkdir -p $DIR/d51d
3253         createmany -o $DIR/d51d/t- 1000
3254         $GETSTRIPE $DIR/d51d > $TMP/files
3255         for N in `seq 0 $((OSTCOUNT - 1))`; do
3256             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3257             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3258             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3259         done
3260         unlinkmany $DIR/d51d/t- 1000
3261
3262         NLAST=0
3263         for N in `seq 1 $((OSTCOUNT - 1))`; do
3264             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3265                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3266             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3267                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3268
3269             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3270                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3271             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3272                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3273             NLAST=$N
3274         done
3275 }
3276 run_test 51d "check object distribution ===================="
3277
3278 test_52a() {
3279         [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
3280         mkdir -p $DIR/d52a
3281         touch $DIR/d52a/foo
3282         chattr +a $DIR/d52a/foo || error "chattr +a failed"
3283         echo bar >> $DIR/d52a/foo || error "append bar failed"
3284         cp /etc/hosts $DIR/d52a/foo && error "cp worked"
3285         rm -f $DIR/d52a/foo 2>/dev/null && error "rm worked"
3286         link $DIR/d52a/foo $DIR/d52a/foo_link 2>/dev/null && error "link worked"
3287         echo foo >> $DIR/d52a/foo || error "append foo failed"
3288         mrename $DIR/d52a/foo $DIR/d52a/foo_ren && error "rename worked"
3289         lsattr $DIR/d52a/foo | egrep -q "^-+a[-e]+ $DIR/d52a/foo" || error "lsattr"
3290         chattr -a $DIR/d52a/foo || error "chattr -a failed"
3291         cp -r $DIR/d52a /tmp/
3292         rm -fr $DIR/d52a || error "cleanup rm failed"
3293 }
3294 run_test 52a "append-only flag test (should return errors) ====="
3295
3296 test_52b() {
3297         [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
3298         mkdir -p $DIR/d52b
3299         touch $DIR/d52b/foo
3300         chattr +i $DIR/d52b/foo || error "chattr +i failed"
3301         cat test > $DIR/d52b/foo && error "cat test worked"
3302         cp /etc/hosts $DIR/d52b/foo && error "cp worked"
3303         rm -f $DIR/d52b/foo 2>/dev/null && error "rm worked"
3304         link $DIR/d52b/foo $DIR/d52b/foo_link 2>/dev/null && error  "link worked"
3305         echo foo >> $DIR/d52b/foo && error "echo worked"
3306         mrename $DIR/d52b/foo $DIR/d52b/foo_ren && error "rename worked"
3307         [ -f $DIR/d52b/foo ] || error
3308         [ -f $DIR/d52b/foo_ren ] && error
3309         lsattr $DIR/d52b/foo | egrep -q "^-+i[-e]+ $DIR/d52b/foo" || error "lsattr"
3310         chattr -i $DIR/d52b/foo || error "chattr failed"
3311
3312         rm -fr $DIR/d52b || error
3313 }
3314 run_test 52b "immutable flag test (should return errors) ======="
3315
3316 test_53() {
3317         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3318         remote_ost_nodsh && skip "remote OST with nodsh" && return
3319
3320         local param
3321         local ostname
3322         local mds_last
3323         local ost_last
3324         local ostnum
3325
3326         # only test MDT0000
3327         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3328         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3329                 param=`echo ${value[0]} | cut -d "=" -f1`
3330                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3331                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3332                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
3333                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id | head -n 1)
3334                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3335                 if [ $ost_last != $mds_last ]; then
3336                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3337                 fi
3338         done
3339 }
3340 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3341
3342 test_54a() {
3343         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3344         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3345         $SOCKETSERVER $DIR/socket
3346         $SOCKETCLIENT $DIR/socket || error
3347         $MUNLINK $DIR/socket
3348 }
3349 run_test 54a "unix domain socket test =========================="
3350
3351 test_54b() {
3352         f="$DIR/f54b"
3353         mknod $f c 1 3
3354         chmod 0666 $f
3355         dd if=/dev/zero of=$f bs=`page_size` count=1
3356 }
3357 run_test 54b "char device works in lustre ======================"
3358
3359 find_loop_dev() {
3360         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3361         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3362         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3363
3364         for i in `seq 3 7`; do
3365                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3366                 LOOPDEV=$LOOPBASE$i
3367                 LOOPNUM=$i
3368                 break
3369         done
3370 }
3371
3372 test_54c() {
3373         tfile="$DIR/f54c"
3374         tdir="$DIR/d54c"
3375         loopdev="$DIR/loop54c"
3376
3377         find_loop_dev
3378         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3379         mknod $loopdev b 7 $LOOPNUM
3380         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3381         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3382         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3383         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3384         mkdir -p $tdir
3385         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3386         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3387         df $tdir
3388         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3389         $UMOUNT $tdir
3390         losetup -d $loopdev
3391         rm $loopdev
3392 }
3393 run_test 54c "block device works in lustre ====================="
3394
3395 test_54d() {
3396         f="$DIR/f54d"
3397         string="aaaaaa"
3398         mknod $f p
3399         [ "$string" = `echo $string > $f | cat $f` ] || error
3400 }
3401 run_test 54d "fifo device works in lustre ======================"
3402
3403 test_54e() {
3404         check_kernel_version 46 || return 0
3405         f="$DIR/f54e"
3406         string="aaaaaa"
3407         cp -aL /dev/console $f
3408         echo $string > $f || error
3409 }
3410 run_test 54e "console/tty device works in lustre ======================"
3411
3412 #The test_55 used to be iopen test and it was removed by bz#24037.
3413 #run_test 55 "check iopen_connect_dentry() ======================"
3414
3415 test_56a() {    # was test_56
3416         rm -rf $DIR/d56
3417         $SETSTRIPE -d $DIR
3418         mkdir $DIR/d56
3419         mkdir $DIR/d56/dir
3420         NUMFILES=3
3421         NUMFILESx2=$(($NUMFILES * 2))
3422         for i in `seq 1 $NUMFILES` ; do
3423                 touch $DIR/d56/file$i
3424                 touch $DIR/d56/dir/file$i
3425         done
3426
3427         # test lfs getstripe with --recursive
3428         FILENUM=`$GETSTRIPE --recursive $DIR/d56 | grep -c obdidx`
3429         [ $FILENUM -eq $NUMFILESx2 ] ||
3430                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3431         FILENUM=`$GETSTRIPE $DIR/d56 | grep -c obdidx`
3432         [ $FILENUM -eq $NUMFILES ] ||
3433                 error "$GETSTRIPE $DIR/d56: found $FILENUM, not $NUMFILES"
3434         echo "$GETSTRIPE --recursive passed."
3435
3436         # test lfs getstripe with file instead of dir
3437         FILENUM=`$GETSTRIPE $DIR/d56/file1 | grep -c obdidx`
3438         [ $FILENUM  -eq 1 ] || error \
3439                  "$GETSTRIPE $DIR/d56/file1: found $FILENUM, not 1"
3440         echo "$GETSTRIPE file1 passed."
3441
3442         #test lfs getstripe with --verbose
3443         [ `$GETSTRIPE --verbose $DIR/d56 | grep -c lmm_magic` -eq $NUMFILES ] ||
3444                 error "$GETSTRIPE --verbose $DIR/d56: want $NUMFILES lmm_magic"
3445         [ `$GETSTRIPE $DIR/d56 | grep -c lmm_magic` -eq 0 ] ||
3446             error "$GETSTRIPE $DIR/d56: showed lmm_magic"
3447         echo "$GETSTRIPE --verbose passed."
3448
3449         #test lfs getstripe with --obd
3450         $GETSTRIPE --obd wrong_uuid $DIR/d56 2>&1 | grep -q "unknown obduuid" ||
3451                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3452
3453         [  "$OSTCOUNT" -lt 2 ] &&
3454                 skip_env "skipping other $GETSTRIPE --obd test" && return
3455
3456         OSTIDX=1
3457         OBDUUID=$(ostuuid_from_index $OSTIDX)
3458         FILENUM=`$GETSTRIPE -ir $DIR/d56 | grep -x $OSTIDX | wc -l`
3459         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/d56 | grep obdidx | wc -l`
3460         [ $FOUND -eq $FILENUM ] ||
3461                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3462         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/d56 |
3463                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3464                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3465                 error "$GETSTRIPE --obd: should not show file on other obd"
3466         echo "$GETSTRIPE --obd passed"
3467 }
3468 run_test 56a "check $GETSTRIPE"
3469
3470 NUMFILES=3
3471 NUMDIRS=3
3472 setup_56() {
3473         local LOCAL_NUMFILES="$1"
3474         local LOCAL_NUMDIRS="$2"
3475         local MKDIR_PARAMS="$3"
3476
3477         if [ ! -d "$TDIR" ] ; then
3478                 mkdir -p $TDIR
3479                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3480                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3481                         touch $TDIR/file$i
3482                 done
3483                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3484                         mkdir $TDIR/dir$i
3485                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3486                                 touch $TDIR/dir$i/file$j
3487                         done
3488                 done
3489         fi
3490 }
3491
3492 setup_56_special() {
3493         LOCAL_NUMFILES=$1
3494         LOCAL_NUMDIRS=$2
3495         setup_56 $1 $2
3496         if [ ! -e "$TDIR/loop1b" ] ; then
3497                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3498                         mknod $TDIR/loop${i}b b 7 $i
3499                         mknod $TDIR/null${i}c c 1 3
3500                         ln -s $TDIR/file1 $TDIR/link${i}l
3501                 done
3502                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3503                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3504                         mknod $TDIR/dir$i/null${i}c c 1 3
3505                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3506                 done
3507         fi
3508 }
3509
3510 test_56g() {
3511         $SETSTRIPE -d $DIR
3512
3513         TDIR=$DIR/${tdir}g
3514         setup_56 $NUMFILES $NUMDIRS
3515
3516         EXPECTED=$(($NUMDIRS + 2))
3517         # test lfs find with -name
3518         for i in $(seq 1 $NUMFILES) ; do
3519                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3520                 [ $NUMS -eq $EXPECTED ] ||
3521                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3522                               "found $NUMS, expected $EXPECTED"
3523         done
3524 }
3525 run_test 56g "check lfs find -name ============================="
3526
3527 test_56h() {
3528         $SETSTRIPE -d $DIR
3529
3530         TDIR=$DIR/${tdir}g
3531         setup_56 $NUMFILES $NUMDIRS
3532
3533         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3534         # test lfs find with ! -name
3535         for i in $(seq 1 $NUMFILES) ; do
3536                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3537                 [ $NUMS -eq $EXPECTED ] ||
3538                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3539                               "found $NUMS, expected $EXPECTED"
3540         done
3541 }
3542 run_test 56h "check lfs find ! -name ============================="
3543
3544 test_56i() {
3545        tdir=${tdir}i
3546        mkdir -p $DIR/$tdir
3547        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3548        CMD="$LFIND -ost $UUID $DIR/$tdir"
3549        OUT=$($CMD)
3550        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3551 }
3552 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3553
3554 test_56j() {
3555         TDIR=$DIR/${tdir}g
3556         setup_56_special $NUMFILES $NUMDIRS
3557
3558         EXPECTED=$((NUMDIRS + 1))
3559         CMD="$LFIND -type d $TDIR"
3560         NUMS=$($CMD | wc -l)
3561         [ $NUMS -eq $EXPECTED ] ||
3562                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3563 }
3564 run_test 56j "check lfs find -type d ============================="
3565
3566 test_56k() {
3567         TDIR=$DIR/${tdir}g
3568         setup_56_special $NUMFILES $NUMDIRS
3569
3570         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3571         CMD="$LFIND -type f $TDIR"
3572         NUMS=$($CMD | wc -l)
3573         [ $NUMS -eq $EXPECTED ] ||
3574                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3575 }
3576 run_test 56k "check lfs find -type f ============================="
3577
3578 test_56l() {
3579         TDIR=$DIR/${tdir}g
3580         setup_56_special $NUMFILES $NUMDIRS
3581
3582         EXPECTED=$((NUMDIRS + NUMFILES))
3583         CMD="$LFIND -type b $TDIR"
3584         NUMS=$($CMD | wc -l)
3585         [ $NUMS -eq $EXPECTED ] ||
3586                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3587 }
3588 run_test 56l "check lfs find -type b ============================="
3589
3590 test_56m() {
3591         TDIR=$DIR/${tdir}g
3592         setup_56_special $NUMFILES $NUMDIRS
3593
3594         EXPECTED=$((NUMDIRS + NUMFILES))
3595         CMD="$LFIND -type c $TDIR"
3596         NUMS=$($CMD | wc -l)
3597         [ $NUMS -eq $EXPECTED ] ||
3598                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3599 }
3600 run_test 56m "check lfs find -type c ============================="
3601
3602 test_56n() {
3603         TDIR=$DIR/${tdir}g
3604         setup_56_special $NUMFILES $NUMDIRS
3605
3606         EXPECTED=$((NUMDIRS + NUMFILES))
3607         CMD="$LFIND -type l $TDIR"
3608         NUMS=$($CMD | wc -l)
3609         [ $NUMS -eq $EXPECTED ] ||
3610                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3611 }
3612 run_test 56n "check lfs find -type l ============================="
3613
3614 test_56o() {
3615         TDIR=$DIR/${tdir}o
3616         setup_56 $NUMFILES $NUMDIRS
3617
3618         utime $TDIR/file1 > /dev/null || error "utime (1)"
3619         utime $TDIR/file2 > /dev/null || error "utime (2)"
3620         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3621         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3622         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3623         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3624
3625         EXPECTED=4
3626         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3627         [ $NUMS -eq $EXPECTED ] || \
3628                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3629
3630         EXPECTED=12
3631         CMD="$LFIND -mtime 0 $TDIR"
3632         NUMS=$($CMD | wc -l)
3633         [ $NUMS -eq $EXPECTED ] ||
3634                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3635 }
3636 run_test 56o "check lfs find -mtime for old files =========================="
3637
3638 test_56p() {
3639         [ $RUNAS_ID -eq $UID ] &&
3640                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3641
3642         TDIR=$DIR/${tdir}p
3643         setup_56 $NUMFILES $NUMDIRS
3644
3645         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3646         EXPECTED=$NUMFILES
3647         CMD="$LFIND -uid $RUNAS_ID $TDIR"
3648         NUMS=$($CMD | wc -l)
3649         [ $NUMS -eq $EXPECTED ] || \
3650                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3651
3652         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3653         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3654         NUMS=$($CMD | wc -l)
3655         [ $NUMS -eq $EXPECTED ] || \
3656                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3657 }
3658 run_test 56p "check lfs find -uid and ! -uid ==============================="
3659
3660 test_56q() {
3661         [ $RUNAS_ID -eq $UID ] &&
3662                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3663
3664         TDIR=$DIR/${tdir}q
3665         setup_56 $NUMFILES $NUMDIRS
3666
3667         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3668
3669         EXPECTED=$NUMFILES
3670         CMD="$LFIND -gid $RUNAS_GID $TDIR"
3671         NUMS=$($CMD | wc -l)
3672         [ $NUMS -eq $EXPECTED ] ||
3673                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3674
3675         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
3676         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
3677         NUMS=$($CMD | wc -l)
3678         [ $NUMS -eq $EXPECTED ] ||
3679                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3680 }
3681 run_test 56q "check lfs find -gid and ! -gid ==============================="
3682
3683 test_56r() {
3684         TDIR=$DIR/${tdir}r
3685         setup_56 $NUMFILES $NUMDIRS
3686
3687         EXPECTED=12
3688         CMD="$LFIND -size 0 -type f $TDIR"
3689         NUMS=$($CMD | wc -l)
3690         [ $NUMS -eq $EXPECTED ] ||
3691                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3692         EXPECTED=0
3693         CMD="$LFIND ! -size 0 -type f $TDIR"
3694         NUMS=$($CMD | wc -l)
3695         [ $NUMS -eq $EXPECTED ] ||
3696                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3697         echo "test" > $TDIR/$tfile
3698         echo "test2" > $TDIR/$tfile.2 && sync
3699         EXPECTED=1
3700         CMD="$LFIND -size 5 -type f $TDIR"
3701         NUMS=$($CMD | wc -l)
3702         [ $NUMS -eq $EXPECTED ] ||
3703                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3704         EXPECTED=1
3705         CMD="$LFIND -size +5 -type f $TDIR"
3706         NUMS=$($CMD | wc -l)
3707         [ $NUMS -eq $EXPECTED ] ||
3708                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3709         EXPECTED=2
3710         CMD="$LFIND -size +0 -type f $TDIR"
3711         NUMS=$($CMD | wc -l)
3712         [ $NUMS -eq $EXPECTED ] ||
3713                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3714         EXPECTED=2
3715         CMD="$LFIND ! -size -5 -type f $TDIR"
3716         NUMS=$($CMD | wc -l)
3717         [ $NUMS -eq $EXPECTED ] ||
3718                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3719         EXPECTED=12
3720         CMD="$LFIND -size -5 -type f $TDIR"
3721         NUMS=$($CMD | wc -l)
3722         [ $NUMS -eq $EXPECTED ] ||
3723                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3724 }
3725 run_test 56r "check lfs find -size works =========================="
3726
3727 test_56s() { # LU-611
3728         TDIR=$DIR/${tdir}s
3729         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3730
3731         if [ $OSTCOUNT -gt 1 ]; then
3732                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
3733                 ONESTRIPE=4
3734                 EXTRA=4
3735         else
3736                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
3737                 EXTRA=0
3738         fi
3739
3740         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3741         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
3742         NUMS=$($CMD | wc -l)
3743         [ $NUMS -eq $EXPECTED ] ||
3744                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3745
3746         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
3747         CMD="$LFIND -stripe-count +0 -type f $TDIR"
3748         NUMS=$($CMD | wc -l)
3749         [ $NUMS -eq $EXPECTED ] ||
3750                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3751
3752         EXPECTED=$ONESTRIPE
3753         CMD="$LFIND -stripe-count 1 -type f $TDIR"
3754         NUMS=$($CMD | wc -l)
3755         [ $NUMS -eq $EXPECTED ] ||
3756                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3757
3758         CMD="$LFIND -stripe-count -2 -type f $TDIR"
3759         NUMS=$($CMD | wc -l)
3760         [ $NUMS -eq $EXPECTED ] ||
3761                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3762
3763         EXPECTED=0
3764         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
3765         NUMS=$($CMD | wc -l)
3766         [ $NUMS -eq $EXPECTED ] ||
3767                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3768 }
3769 run_test 56s "check lfs find -stripe-count works"
3770
3771 test_56t() { # LU-611
3772         TDIR=$DIR/${tdir}t
3773         setup_56 $NUMFILES $NUMDIRS "-s 512k"
3774
3775         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
3776
3777         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3778         CMD="$LFIND -stripe-size 512k -type f $TDIR"
3779         NUMS=$($CMD | wc -l)
3780         [ $NUMS -eq $EXPECTED ] ||
3781                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3782
3783         CMD="$LFIND -stripe-size +320k -type f $TDIR"
3784         NUMS=$($CMD | wc -l)
3785         [ $NUMS -eq $EXPECTED ] ||
3786                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3787
3788         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
3789         CMD="$LFIND -stripe-size +200k -type f $TDIR"
3790         NUMS=$($CMD | wc -l)
3791         [ $NUMS -eq $EXPECTED ] ||
3792                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3793
3794         CMD="$LFIND -stripe-size -640k -type f $TDIR"
3795         NUMS=$($CMD | wc -l)
3796         [ $NUMS -eq $EXPECTED ] ||
3797                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3798
3799         EXPECTED=4
3800         CMD="$LFIND -stripe-size 256k -type f $TDIR"
3801         NUMS=$($CMD | wc -l)
3802         [ $NUMS -eq $EXPECTED ] ||
3803                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3804
3805         CMD="$LFIND -stripe-size -320k -type f $TDIR"
3806         NUMS=$($CMD | wc -l)
3807         [ $NUMS -eq $EXPECTED ] ||
3808                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3809
3810         EXPECTED=0
3811         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
3812         NUMS=$($CMD | wc -l)
3813         [ $NUMS -eq $EXPECTED ] ||
3814                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3815 }
3816 run_test 56t "check lfs find -stripe-size works"
3817
3818 test_56u() { # LU-611
3819         TDIR=$DIR/${tdir}u
3820         setup_56 $NUMFILES $NUMDIRS "-i 0"
3821
3822         if [ $OSTCOUNT -gt 1 ]; then
3823                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
3824                 ONESTRIPE=4
3825         else
3826                 ONESTRIPE=0
3827         fi
3828
3829         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3830         CMD="$LFIND -stripe-index 0 -type f $TDIR"
3831         NUMS=$($CMD | wc -l)
3832         [ $NUMS -eq $EXPECTED ] ||
3833                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3834
3835         EXPECTED=$ONESTRIPE
3836         CMD="$LFIND -stripe-index 1 -type f $TDIR"
3837         NUMS=$($CMD | wc -l)
3838         [ $NUMS -eq $EXPECTED ] ||
3839                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3840
3841         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
3842         NUMS=$($CMD | wc -l)
3843         [ $NUMS -eq $EXPECTED ] ||
3844                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3845
3846         EXPECTED=0
3847         # This should produce an error and not return any files
3848         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
3849         NUMS=$($CMD 2>/dev/null | wc -l)
3850         [ $NUMS -eq $EXPECTED ] ||
3851                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3852
3853         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
3854         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
3855         NUMS=$($CMD | wc -l)
3856         [ $NUMS -eq $EXPECTED ] ||
3857                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3858 }
3859 run_test 56u "check lfs find -stripe-index works"
3860
3861 test_56v() {
3862     local MDT_IDX=0
3863
3864     TDIR=$DIR/${tdir}v
3865     rm -rf $TDIR
3866     setup_56 $NUMFILES $NUMDIRS
3867
3868     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
3869     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
3870
3871     for file in $($LFIND -mdt $UUID $TDIR); do
3872         file_mdt_idx=$($GETSTRIPE -M $file)
3873         [ $file_mdt_idx -eq $MDT_IDX ] ||
3874             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
3875     done
3876 }
3877 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
3878
3879 # Get and check the actual stripe count of one file.
3880 # Usage: check_stripe_count <file> <expected_stripe_count>
3881 check_stripe_count() {
3882     local file=$1
3883     local expected=$2
3884     local actual
3885
3886     [[ -z "$file" || -z "$expected" ]] &&
3887         error "check_stripe_count: invalid argument!"
3888
3889     local cmd="$GETSTRIPE -c $file"
3890     actual=$($cmd) || error "$cmd failed"
3891     actual=${actual%% *}
3892
3893     if [[ $actual -ne $expected ]]; then
3894         [[ $expected -eq -1 ]] ||
3895             error "$cmd wrong: found $actual, expected $expected"
3896         [[ $actual -eq $OSTCOUNT ]] ||
3897             error "$cmd wrong: found $actual, expected $OSTCOUNT"
3898     fi
3899 }
3900
3901 test_56w() {
3902     TDIR=$DIR/${tdir}w
3903
3904     rm -rf $TDIR || error "remove $TDIR failed"
3905     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3906
3907     local stripe_size
3908     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
3909         error "$GETSTRIPE -S -d $TDIR failed"
3910     stripe_size=${stripe_size%% *}
3911
3912     local file_size=$((stripe_size * OSTCOUNT))
3913     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
3914     local required_space=$((file_num * file_size))
3915     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
3916     [[ $free_space -le $((required_space / 1024)) ]] &&
3917         skip_env "need at least $required_space bytes free space," \
3918                  "have $free_space kbytes" && return
3919
3920     local dd_bs=65536
3921     local dd_count=$((file_size / dd_bs))
3922
3923     # write data into the files
3924     local i
3925     local j
3926     local file
3927     for i in $(seq 1 $NUMFILES); do
3928         file=$TDIR/file$i
3929         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
3930             error "write data into $file failed"
3931     done
3932     for i in $(seq 1 $NUMDIRS); do
3933         for j in $(seq 1 $NUMFILES); do
3934             file=$TDIR/dir$i/file$j
3935             yes | dd bs=$dd_bs count=$dd_count of=$file \
3936                 >/dev/null 2>&1 ||
3937                 error "write data into $file failed"
3938         done
3939     done
3940
3941     local expected=-1
3942     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
3943
3944     # lfs_migrate file
3945     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
3946     echo "$cmd"
3947     eval $cmd || error "$cmd failed"
3948
3949     check_stripe_count $TDIR/file1 $expected
3950
3951     # lfs_migrate dir
3952     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
3953     echo "$cmd"
3954     eval $cmd || error "$cmd failed"
3955
3956     for j in $(seq 1 $NUMFILES); do
3957         check_stripe_count $TDIR/dir1/file$j $expected
3958     done
3959
3960     # lfs_migrate works with lfs find
3961     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
3962          $LFS_MIGRATE -y -c $expected"
3963     echo "$cmd"
3964     eval $cmd || error "$cmd failed"
3965
3966     for i in $(seq 2 $NUMFILES); do
3967         check_stripe_count $TDIR/file$i $expected
3968     done
3969     for i in $(seq 2 $NUMDIRS); do
3970         for j in $(seq 1 $NUMFILES); do
3971             check_stripe_count $TDIR/dir$i/file$j $expected
3972         done
3973     done
3974 }
3975 run_test 56w "check lfs_migrate -c stripe_count works"
3976
3977 test_57a() {
3978         # note test will not do anything if MDS is not local
3979         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3980         local MNTDEV="osd*.*MDT*.mntdev"
3981         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
3982         [ -z "$DEV" ] && error "can't access $MNTDEV"
3983         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
3984                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
3985                         error "can't access $DEV"
3986                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
3987                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
3988                 rm $TMP/t57a.dump
3989         done
3990 }
3991 run_test 57a "verify MDS filesystem created with large inodes =="
3992
3993 test_57b() {
3994         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3995         local dir=$DIR/d57b
3996
3997         local FILECOUNT=100
3998         local FILE1=$dir/f1
3999         local FILEN=$dir/f$FILECOUNT
4000
4001         rm -rf $dir || error "removing $dir"
4002         mkdir -p $dir || error "creating $dir"
4003         local num=$(get_mds_dir $dir)
4004         local mymds=mds$num
4005
4006         echo "mcreating $FILECOUNT files"
4007         createmany -m $dir/f 1 $FILECOUNT || \
4008                 error "creating files in $dir"
4009
4010         # verify that files do not have EAs yet
4011         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4012         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4013
4014         sync
4015         sleep 1
4016         df $dir  #make sure we get new statfs data
4017         local MDSFREE=$(do_facet $mymds \
4018                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4019         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4020         echo "opening files to create objects/EAs"
4021         local FILE
4022         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4023                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4024         done
4025
4026         # verify that files have EAs now
4027         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4028         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4029
4030         sleep 1  #make sure we get new statfs data
4031         df $dir
4032         local MDSFREE2=$(do_facet $mymds \
4033                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4034         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4035         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4036                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4037                         error "MDC before $MDCFREE != after $MDCFREE2"
4038                 else
4039                         echo "MDC before $MDCFREE != after $MDCFREE2"
4040                         echo "unable to confirm if MDS has large inodes"
4041                 fi
4042         fi
4043         rm -rf $dir
4044 }
4045 run_test 57b "default LOV EAs are stored inside large inodes ==="
4046
4047 test_58() {
4048     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
4049     wiretest
4050 }
4051 run_test 58 "verify cross-platform wire constants =============="
4052
4053 test_59() {
4054         echo "touch 130 files"
4055         createmany -o $DIR/f59- 130
4056         echo "rm 130 files"
4057         unlinkmany $DIR/f59- 130
4058         sync
4059         sleep 2
4060         # wait for commitment of removal
4061 }
4062 run_test 59 "verify cancellation of llog records async ========="
4063
4064 TEST60_HEAD="test_60 run $RANDOM"
4065 test_60a() {
4066         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4067         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4068         log "$TEST60_HEAD - from kernel mode"
4069         do_facet mgs sh run-llog.sh
4070 }
4071 run_test 60a "llog sanity tests run from kernel module =========="
4072
4073 test_60b() { # bug 6411
4074         dmesg > $DIR/$tfile
4075         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4076                                  /llog.test/ {
4077                                          if (marker)
4078                                                  from_marker++
4079                                          from_begin++
4080                                  }
4081                                  END {
4082                                          if (marker)
4083                                                  print from_marker
4084                                          else
4085                                                  print from_begin
4086                                  }"`
4087         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4088 }
4089 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4090
4091 test_60c() {
4092         echo "create 5000 files"
4093         createmany -o $DIR/f60c- 5000
4094 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4095         lctl set_param fail_loc=0x80000137
4096         unlinkmany $DIR/f60c- 5000
4097         lctl set_param fail_loc=0
4098 }
4099 run_test 60c "unlink file when mds full"
4100
4101 test_60d() {
4102         SAVEPRINTK=$(lctl get_param -n printk)
4103
4104         # verify "lctl mark" is even working"
4105         MESSAGE="test message ID $RANDOM $$"
4106         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4107         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4108
4109         lctl set_param printk=0 || error "set lnet.printk failed"
4110         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4111         MESSAGE="new test message ID $RANDOM $$"
4112         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4113         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4114         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4115
4116         lctl set_param -n printk="$SAVEPRINTK"
4117 }
4118 run_test 60d "test printk console message masking"
4119
4120 test_61() {
4121         f="$DIR/f61"
4122         dd if=/dev/zero of=$f bs=`page_size` count=1
4123         cancel_lru_locks osc
4124         $MULTIOP $f OSMWUc || error
4125         sync
4126 }
4127 run_test 61 "mmap() writes don't make sync hang ================"
4128
4129 # bug 2330 - insufficient obd_match error checking causes LBUG
4130 test_62() {
4131         f="$DIR/f62"
4132         echo foo > $f
4133         cancel_lru_locks osc
4134         lctl set_param fail_loc=0x405
4135         cat $f && error "cat succeeded, expect -EIO"
4136         lctl set_param fail_loc=0
4137 }
4138 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4139 # match every page all of the time.
4140 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4141
4142 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4143 test_63a() {    # was test_63
4144         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4145         lctl set_param -n osc.*.max_dirty_mb 0
4146         for i in `seq 10` ; do
4147                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4148                 sleep 5
4149                 kill $!
4150                 sleep 1
4151         done
4152
4153         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4154         rm -f $DIR/f63 || true
4155 }
4156 run_test 63a "Verify oig_wait interruption does not crash ======="
4157
4158 # bug 2248 - async write errors didn't return to application on sync
4159 # bug 3677 - async write errors left page locked
4160 test_63b() {
4161         debugsave
4162         lctl set_param debug=-1
4163
4164         # ensure we have a grant to do async writes
4165         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4166         rm $DIR/$tfile
4167
4168         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4169         lctl set_param fail_loc=0x80000406
4170         $MULTIOP $DIR/$tfile Owy && \
4171                 error "sync didn't return ENOMEM"
4172         sync; sleep 2; sync     # do a real sync this time to flush page
4173         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4174                 error "locked page left in cache after async error" || true
4175         debugrestore
4176 }
4177 run_test 63b "async write errors should be returned to fsync ==="
4178
4179 test_64a () {
4180         df $DIR
4181         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4182 }
4183 run_test 64a "verify filter grant calculations (in kernel) ====="
4184
4185 test_64b () {
4186         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4187         sh oos.sh $MOUNT
4188 }
4189 run_test 64b "check out-of-space detection on client ==========="
4190
4191 # bug 1414 - set/get directories' stripe info
4192 test_65a() {
4193         mkdir -p $DIR/d65
4194         touch $DIR/d65/f1
4195         $LVERIFY $DIR/d65 $DIR/d65/f1 || error "lverify failed"
4196 }
4197 run_test 65a "directory with no stripe info ===================="
4198
4199 test_65b() {
4200         mkdir -p $DIR/d65
4201         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65||error "setstripe"
4202         touch $DIR/d65/f2
4203         $LVERIFY $DIR/d65 $DIR/d65/f2 || error "lverify failed"
4204 }
4205 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4206
4207 test_65c() {
4208         if [ $OSTCOUNT -gt 1 ]; then
4209                 mkdir -p $DIR/d65
4210                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4211                         -c $(($OSTCOUNT - 1)) $DIR/d65 || error "setstripe"
4212                 touch $DIR/d65/f3
4213                 $LVERIFY $DIR/d65 $DIR/d65/f3 || error "lverify failed"
4214         fi
4215 }
4216 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4217
4218 test_65d() {
4219         mkdir -p $DIR/d65
4220         if [ $STRIPECOUNT -le 0 ]; then
4221                 sc=1
4222         elif [ $STRIPECOUNT -gt 2000 ]; then
4223 #LOV_MAX_STRIPE_COUNT is 2000
4224                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4225         else
4226                 sc=$(($STRIPECOUNT - 1))
4227         fi
4228         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/d65 || error "setstripe"
4229         touch $DIR/d65/f4 $DIR/d65/f5
4230         $LVERIFY $DIR/d65 $DIR/d65/f4 $DIR/d65/f5 || error "lverify failed"
4231 }
4232 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4233
4234 test_65e() {
4235         mkdir -p $DIR/d65
4236
4237         $SETSTRIPE $DIR/d65 || error "setstripe"
4238         $GETSTRIPE -v $DIR/d65 | grep "Default" || error "no stripe info failed"
4239         touch $DIR/d65/f6
4240         $LVERIFY $DIR/d65 $DIR/d65/f6 || error "lverify failed"
4241 }
4242 run_test 65e "directory setstripe defaults ======================="
4243
4244 test_65f() {
4245         mkdir -p $DIR/d65f
4246         $RUNAS $SETSTRIPE $DIR/d65f && error "setstripe succeeded" || true
4247 }
4248 run_test 65f "dir setstripe permission (should return error) ==="
4249
4250 test_65g() {
4251         mkdir -p $DIR/d65
4252         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4253         $SETSTRIPE -d $DIR/d65 || error "setstripe"
4254         $GETSTRIPE -v $DIR/d65 | grep "Default" || \
4255                 error "delete default stripe failed"
4256 }
4257 run_test 65g "directory setstripe -d ==========================="
4258
4259 test_65h() {
4260         mkdir -p $DIR/d65
4261         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4262         mkdir -p $DIR/d65/dd1
4263         [ $($GETSTRIPE -c $DIR/d65) == $($GETSTRIPE -c $DIR/d65/dd1) ] ||
4264                 error "stripe info inherit failed"
4265 }
4266 run_test 65h "directory stripe info inherit ===================="
4267
4268 test_65i() { # bug6367
4269         $SETSTRIPE -S 65536 -c -1 $MOUNT
4270 }
4271 run_test 65i "set non-default striping on root directory (bug 6367)="
4272
4273 test_65ia() { # bug12836
4274         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4275 }
4276 run_test 65ia "getstripe on -1 default directory striping"
4277
4278 test_65ib() { # bug12836
4279         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4280 }
4281 run_test 65ib "getstripe -v on -1 default directory striping"
4282
4283 test_65ic() { # bug12836
4284         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4285 }
4286 run_test 65ic "new find on -1 default directory striping"
4287
4288 test_65j() { # bug6367
4289         sync; sleep 1
4290         # if we aren't already remounting for each test, do so for this test
4291         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4292                 cleanup || error "failed to unmount"
4293                 setup
4294         fi
4295         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4296 }
4297 run_test 65j "set default striping on root directory (bug 6367)="
4298
4299 test_65k() { # bug11679
4300     [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4301     remote_mds_nodsh && skip "remote MDS with nodsh" && return
4302
4303     echo "Check OST status: "
4304     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4305               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4306
4307     for OSC in $MDS_OSCS; do
4308         echo $OSC "is activate"
4309         do_facet $SINGLEMDS lctl --device %$OSC activate
4310     done
4311
4312     do_facet client mkdir -p $DIR/$tdir
4313     for INACTIVE_OSC in $MDS_OSCS; do
4314         echo "Deactivate: " $INACTIVE_OSC
4315         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4316         for STRIPE_OSC in $MDS_OSCS; do
4317             OST=`osc_to_ost $STRIPE_OSC`
4318             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4319                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4320
4321             [ -f $DIR/$tdir/$IDX ] && continue
4322             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4323             do_facet client $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4324             RC=$?
4325             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4326         done
4327         do_facet client rm -f $DIR/$tdir/*
4328         echo $INACTIVE_OSC "is Activate."
4329         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4330     done
4331 }
4332 run_test 65k "validate manual striping works properly with deactivated OSCs"
4333
4334 test_65l() { # bug 12836
4335         mkdir -p $DIR/$tdir/test_dir
4336         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4337         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4338 }
4339 run_test 65l "lfs find on -1 stripe dir ========================"
4340
4341 # bug 2543 - update blocks count on client
4342 test_66() {
4343         COUNT=${COUNT:-8}
4344         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4345         sync; sync_all_data; sync; sync_all_data
4346         cancel_lru_locks osc
4347         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4348         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4349 }
4350 run_test 66 "update inode blocks count on client ==============="
4351
4352 LLOOP=
4353 LLITELOOPLOAD=
4354 cleanup_68() {
4355         trap 0
4356         if [ ! -z "$LLOOP" ]; then
4357                 if swapon -s | grep -q $LLOOP; then
4358                         swapoff $LLOOP || error "swapoff failed"
4359                 fi
4360
4361                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4362                 rm -f $LLOOP
4363                 unset LLOOP
4364         fi
4365         if [ ! -z "$LLITELOOPLOAD" ]; then
4366                 rmmod llite_lloop
4367                 unset LLITELOOPLOAD
4368         fi
4369         rm -f $DIR/f68*
4370 }
4371
4372 meminfo() {
4373         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4374 }
4375
4376 swap_used() {
4377         swapon -s | awk '($1 == "'$1'") { print $4 }'
4378 }
4379
4380 # test case for lloop driver, basic function
4381 test_68a() {
4382         [ "$UID" != 0 ] && skip_env "must run as root" && return
4383         llite_lloop_enabled || \
4384                 { skip_env "llite_lloop module disabled" && return; }
4385
4386         trap cleanup_68 EXIT
4387
4388         if ! module_loaded llite_lloop; then
4389                 if load_module llite/llite_lloop; then
4390                         LLITELOOPLOAD=yes
4391                 else
4392                         skip_env "can't find module llite_lloop"
4393                         return
4394                 fi
4395         fi
4396
4397         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4398         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4399         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4400
4401         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4402         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4403
4404         cleanup_68
4405 }
4406 run_test 68a "lloop driver - basic test ========================"
4407
4408 # excercise swapping to lustre by adding a high priority swapfile entry
4409 # and then consuming memory until it is used.
4410 test_68b() {  # was test_68
4411         [ "$UID" != 0 ] && skip_env "must run as root" && return
4412         lctl get_param -n devices | grep -q obdfilter && \
4413                 skip "local OST" && return
4414
4415         grep -q llite_lloop /proc/modules
4416         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4417
4418         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4419                 skip "can't reliably test swap with TCP" && return
4420
4421         MEMTOTAL=`meminfo MemTotal`
4422         NR_BLOCKS=$((MEMTOTAL>>8))
4423         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4424
4425         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4426         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4427         mkswap $DIR/f68b
4428
4429         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4430
4431         trap cleanup_68 EXIT
4432
4433         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4434
4435         echo "before: `swapon -s | grep $LLOOP`"
4436         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4437         echo "after: `swapon -s | grep $LLOOP`"
4438         SWAPUSED=`swap_used $LLOOP`
4439
4440         cleanup_68
4441
4442         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4443 }
4444 run_test 68b "support swapping to Lustre ========================"
4445
4446 # bug5265, obdfilter oa2dentry return -ENOENT
4447 # #define OBD_FAIL_OST_ENOENT 0x217
4448 test_69() {
4449         remote_ost_nodsh && skip "remote OST with nodsh" && return
4450
4451         f="$DIR/$tfile"
4452         $SETSTRIPE -c 1 -i 0 $f
4453
4454         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4455
4456         do_facet ost1 lctl set_param fail_loc=0x217
4457         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4458         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4459
4460         do_facet ost1 lctl set_param fail_loc=0
4461         $DIRECTIO write $f 0 2 || error "write error"
4462
4463         cancel_lru_locks osc
4464         $DIRECTIO read $f 0 1 || error "read error"
4465
4466         do_facet ost1 lctl set_param fail_loc=0x217
4467         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4468
4469         do_facet ost1 lctl set_param fail_loc=0
4470         rm -f $f
4471 }
4472 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4473
4474 test_71() {
4475     mkdir -p $DIR/$tdir
4476     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4477 }
4478 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4479
4480 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4481         check_kernel_version 43 || return 0
4482         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4483
4484         # Check that testing environment is properly set up. Skip if not
4485         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4486                 skip_env "User $RUNAS_ID does not exist - skipping"
4487                 return 0
4488         }
4489         # We had better clear the $DIR to get enough space for dd
4490         rm -rf $DIR/*
4491         touch $DIR/f72
4492         chmod 777 $DIR/f72
4493         chmod ug+s $DIR/f72
4494         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4495         # See if we are still setuid/sgid
4496         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4497         # Now test that MDS is updated too
4498         cancel_lru_locks mdc
4499         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4500         true
4501         rm -f $DIR/f72
4502 }
4503 run_test 72a "Test that remove suid works properly (bug5695) ===="
4504
4505 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4506         local perm
4507
4508         [ "$RUNAS_ID" = "$UID" ] && \
4509                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4510         [ "$RUNAS_ID" -eq 0 ] && \
4511                 skip_env "RUNAS_ID = 0 -- skipping" && return
4512
4513         # Check that testing environment is properly set up. Skip if not
4514         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4515                 skip_env "User $RUNAS_ID does not exist - skipping"
4516                 return 0
4517         }
4518         touch $DIR/${tfile}-f{g,u}
4519         mkdir $DIR/${tfile}-d{g,u}
4520         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4521         chmod g+s $DIR/${tfile}-{f,d}g
4522         chmod u+s $DIR/${tfile}-{f,d}u
4523         for perm in 777 2777 4777; do
4524                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4525                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4526                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4527                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4528         done
4529         true
4530 }
4531 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4532
4533 # bug 3462 - multiple simultaneous MDC requests
4534 test_73() {
4535         mkdir $DIR/d73-1
4536         mkdir $DIR/d73-2
4537         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4538         pid1=$!
4539
4540         lctl set_param fail_loc=0x80000129
4541         $MULTIOP $DIR/d73-1/f73-2 Oc &
4542         sleep 1
4543         lctl set_param fail_loc=0
4544
4545         $MULTIOP $DIR/d73-2/f73-3 Oc &
4546         pid3=$!
4547
4548         kill -USR1 $pid1
4549         wait $pid1 || return 1
4550
4551         sleep 25
4552
4553         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4554         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4555         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4556
4557         rm -rf $DIR/d73-*
4558 }
4559 run_test 73 "multiple MDC requests (should not deadlock)"
4560
4561 test_74a() { # bug 6149, 6184
4562         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4563         #
4564         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4565         # will spin in a tight reconnection loop
4566         touch $DIR/f74a
4567         lctl set_param fail_loc=0x8000030e
4568         # get any lock that won't be difficult - lookup works.
4569         ls $DIR/f74a
4570         lctl set_param fail_loc=0
4571         true
4572         rm -f $DIR/f74a
4573 }
4574 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4575
4576 test_74b() { # bug 13310
4577         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4578         #
4579         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4580         # will spin in a tight reconnection loop
4581         lctl set_param fail_loc=0x8000030e
4582         # get a "difficult" lock
4583         touch $DIR/f74b
4584         lctl set_param fail_loc=0
4585         true
4586         rm -f $DIR/f74b
4587 }
4588 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4589
4590 test_74c() {
4591 #define OBD_FAIL_LDLM_NEW_LOCK
4592         lctl set_param fail_loc=0x80000319
4593         touch $DIR/$tfile && error "Touch successful"
4594         true
4595 }
4596 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4597
4598 num_inodes() {
4599         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4600 }
4601
4602 get_inode_slab_tunables() {
4603         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4604 }
4605
4606 set_inode_slab_tunables() {
4607         echo "lustre_inode_cache $1" > /proc/slabinfo
4608 }
4609
4610 test_76() { # Now for bug 20433, added originally in bug 1443
4611         local SLAB_SETTINGS=`get_inode_slab_tunables`
4612         local CPUS=`getconf _NPROCESSORS_ONLN`
4613         # we cannot set limit below 1 which means 1 inode in each
4614         # per-cpu cache is still allowed
4615         set_inode_slab_tunables "1 1 0"
4616         cancel_lru_locks osc
4617         BEFORE_INODES=`num_inodes`
4618         echo "before inodes: $BEFORE_INODES"
4619         local COUNT=1000
4620         [ "$SLOW" = "no" ] && COUNT=100
4621         for i in `seq $COUNT`; do
4622                 touch $DIR/$tfile
4623                 rm -f $DIR/$tfile
4624         done
4625         cancel_lru_locks osc
4626         AFTER_INODES=`num_inodes`
4627         echo "after inodes: $AFTER_INODES"
4628         local wait=0
4629         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
4630                 sleep 2
4631                 AFTER_INODES=`num_inodes`
4632                 wait=$((wait+2))
4633                 echo "wait $wait seconds inodes: $AFTER_INODES"
4634                 if [ $wait -gt 30 ]; then
4635                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
4636                 fi
4637         done
4638         set_inode_slab_tunables "$SLAB_SETTINGS"
4639 }
4640 run_test 76 "confirm clients recycle inodes properly ===="
4641
4642
4643 export ORIG_CSUM=""
4644 set_checksums()
4645 {
4646         # Note: in sptlrpc modes which enable its own bulk checksum, the
4647         # original crc32_le bulk checksum will be automatically disabled,
4648         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
4649         # will be checked by sptlrpc code against sptlrpc bulk checksum.
4650         # In this case set_checksums() will not be no-op, because sptlrpc
4651         # bulk checksum will be enabled all through the test.
4652
4653         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
4654         lctl set_param -n osc.*.checksums $1
4655         return 0
4656 }
4657
4658 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
4659                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
4660 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
4661 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
4662 set_checksum_type()
4663 {
4664         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
4665         log "set checksum type to $1"
4666         return 0
4667 }
4668 F77_TMP=$TMP/f77-temp
4669 F77SZ=8
4670 setup_f77() {
4671         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
4672                 error "error writing to $F77_TMP"
4673 }
4674
4675 test_77a() { # bug 10889
4676         $GSS && skip "could not run with gss" && return
4677         [ ! -f $F77_TMP ] && setup_f77
4678         set_checksums 1
4679         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
4680         set_checksums 0
4681         rm -f $DIR/$tfile
4682 }
4683 run_test 77a "normal checksum read/write operation ============="
4684
4685 test_77b() { # bug 10889
4686         $GSS && skip "could not run with gss" && return
4687         [ ! -f $F77_TMP ] && setup_f77
4688         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4689         lctl set_param fail_loc=0x80000409
4690         set_checksums 1
4691         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
4692                 error "dd error: $?"
4693         lctl set_param fail_loc=0
4694         set_checksums 0
4695 }
4696 run_test 77b "checksum error on client write ===================="
4697
4698 test_77c() { # bug 10889
4699         $GSS && skip "could not run with gss" && return
4700         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
4701         set_checksums 1
4702         for algo in $CKSUM_TYPES; do
4703                 cancel_lru_locks osc
4704                 set_checksum_type $algo
4705                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4706                 lctl set_param fail_loc=0x80000408
4707                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
4708                 lctl set_param fail_loc=0
4709         done
4710         set_checksums 0
4711         set_checksum_type $ORIG_CSUM_TYPE
4712         rm -f $DIR/f77b
4713 }
4714 run_test 77c "checksum error on client read ==================="
4715
4716 test_77d() { # bug 10889
4717         $GSS && skip "could not run with gss" && return
4718         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4719         lctl set_param fail_loc=0x80000409
4720         set_checksums 1
4721         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4722                 error "direct write: rc=$?"
4723         lctl set_param fail_loc=0
4724         set_checksums 0
4725 }
4726 run_test 77d "checksum error on OST direct write ==============="
4727
4728 test_77e() { # bug 10889
4729         $GSS && skip "could not run with gss" && return
4730         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
4731         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4732         lctl set_param fail_loc=0x80000408
4733         set_checksums 1
4734         cancel_lru_locks osc
4735         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4736                 error "direct read: rc=$?"
4737         lctl set_param fail_loc=0
4738         set_checksums 0
4739 }
4740 run_test 77e "checksum error on OST direct read ================"
4741
4742 test_77f() { # bug 10889
4743         $GSS && skip "could not run with gss" && return
4744         set_checksums 1
4745         for algo in $CKSUM_TYPES; do
4746                 cancel_lru_locks osc
4747                 set_checksum_type $algo
4748                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4749                 lctl set_param fail_loc=0x409
4750                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
4751                         error "direct write succeeded"
4752                 lctl set_param fail_loc=0
4753         done
4754         set_checksum_type $ORIG_CSUM_TYPE
4755         set_checksums 0
4756 }
4757 run_test 77f "repeat checksum error on write (expect error) ===="
4758
4759 test_77g() { # bug 10889
4760         $GSS && skip "could not run with gss" && return
4761         remote_ost_nodsh && skip "remote OST with nodsh" && return
4762
4763         [ ! -f $F77_TMP ] && setup_f77
4764
4765         $SETSTRIPE -c 1 -i 0 $DIR/f77g
4766         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
4767         do_facet ost1 lctl set_param fail_loc=0x8000021a
4768         set_checksums 1
4769         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
4770                 error "write error: rc=$?"
4771         do_facet ost1 lctl set_param fail_loc=0
4772         set_checksums 0
4773 }
4774 run_test 77g "checksum error on OST write ======================"
4775
4776 test_77h() { # bug 10889
4777         $GSS && skip "could not run with gss" && return
4778         remote_ost_nodsh && skip "remote OST with nodsh" && return
4779
4780         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
4781         cancel_lru_locks osc
4782         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
4783         do_facet ost1 lctl set_param fail_loc=0x8000021b
4784         set_checksums 1
4785         cmp $F77_TMP $DIR/f77g || error "file compare failed"
4786         do_facet ost1 lctl set_param fail_loc=0
4787         set_checksums 0
4788 }
4789 run_test 77h "checksum error on OST read ======================="
4790
4791 test_77i() { # bug 13805
4792         $GSS && skip "could not run with gss" && return
4793         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
4794         lctl set_param fail_loc=0x40b
4795         remount_client $MOUNT
4796         lctl set_param fail_loc=0
4797         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4798                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4799                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4800                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4801         done
4802         remount_client $MOUNT
4803 }
4804 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
4805
4806 test_77j() { # bug 13805
4807         $GSS && skip "could not run with gss" && return
4808         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
4809         lctl set_param fail_loc=0x40c
4810         remount_client $MOUNT
4811         lctl set_param fail_loc=0
4812         sleep 2 # wait async osc connect to finish
4813         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4814                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4815                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4816                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4817         done
4818         remount_client $MOUNT
4819 }
4820 run_test 77j "client only supporting ADLER32 ===================="
4821
4822 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
4823 rm -f $F77_TMP
4824 unset F77_TMP
4825
4826 test_78() { # bug 10901
4827         remote_ost || { skip_env "local OST" && return; }
4828
4829         NSEQ=5
4830         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
4831         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
4832         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
4833         echo "MemTotal: $MEMTOTAL"
4834 # reserve 256MB of memory for the kernel and other running processes,
4835 # and then take 1/2 of the remaining memory for the read/write buffers.
4836     if [ $MEMTOTAL -gt 512 ] ;then
4837         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
4838     else
4839         # for those poor memory-starved high-end clusters...
4840         MEMTOTAL=$((MEMTOTAL / 2))
4841     fi
4842         echo "Mem to use for directio: $MEMTOTAL"
4843         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
4844         [ $F78SIZE -gt 512 ] && F78SIZE=512
4845         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
4846         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
4847         echo "Smallest OST: $SMALLESTOST"
4848         [ $SMALLESTOST -lt 10240 ] && \
4849                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
4850
4851         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
4852                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
4853
4854         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
4855         echo "File size: $F78SIZE"
4856         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
4857         for i in `seq 1 $NSEQ`
4858         do
4859                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
4860                 echo directIO rdwr round $i of $NSEQ
4861                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
4862         done
4863
4864         rm -f $DIR/$tfile
4865 }
4866 run_test 78 "handle large O_DIRECT writes correctly ============"
4867
4868 test_79() { # bug 12743
4869         wait_delete_completed
4870
4871         BKTOTAL=$(calc_osc_kbytes kbytestotal)
4872         BKFREE=$(calc_osc_kbytes kbytesfree)
4873         BKAVAIL=$(calc_osc_kbytes kbytesavail)
4874
4875         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
4876         DFTOTAL=`echo $STRING | cut -d, -f1`
4877         DFUSED=`echo $STRING  | cut -d, -f2`
4878         DFAVAIL=`echo $STRING | cut -d, -f3`
4879         DFFREE=$(($DFTOTAL - $DFUSED))
4880
4881         ALLOWANCE=$((64 * $OSTCOUNT))
4882
4883         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
4884            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
4885                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
4886         fi
4887         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
4888            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
4889                 error "df free($DFFREE) mismatch OST free($BKFREE)"
4890         fi
4891         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
4892            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
4893                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
4894         fi
4895 }
4896 run_test 79 "df report consistency check ======================="
4897
4898 test_80() { # bug 10718
4899         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
4900         sync; sleep 1; sync
4901         local BEFORE=`date +%s`
4902         cancel_lru_locks osc
4903         local AFTER=`date +%s`
4904         local DIFF=$((AFTER-BEFORE))
4905         if [ $DIFF -gt 1 ] ; then
4906                 error "elapsed for 1M@1T = $DIFF"
4907         fi
4908         true
4909         rm -f $DIR/$tfile
4910 }
4911 run_test 80 "Page eviction is equally fast at high offsets too  ===="
4912
4913 test_81a() { # LU-456
4914         remote_ost_nodsh && skip "remote OST with nodsh" && return
4915         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4916         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
4917         do_facet ost0 lctl set_param fail_loc=0x80000228
4918
4919         # write should trigger a retry and success
4920         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4921         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4922         RC=$?
4923         if [ $RC -ne 0 ] ; then
4924                 error "write should success, but failed for $RC"
4925         fi
4926 }
4927 run_test 81a "OST should retry write when get -ENOSPC ==============="
4928
4929 test_81b() { # LU-456
4930         remote_ost_nodsh && skip "remote OST with nodsh" && return
4931         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4932         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
4933         do_facet ost0 lctl set_param fail_loc=0x228
4934
4935         # write should retry several times and return -ENOSPC finally
4936         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4937         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4938         RC=$?
4939         ENOSPC=28
4940         if [ $RC -ne $ENOSPC ] ; then
4941                 error "dd should fail for -ENOSPC, but succeed."
4942         fi
4943 }
4944 run_test 81b "OST should return -ENOSPC when retry still fails ======="
4945
4946 test_82() { # LU-1031
4947         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
4948         local gid1=14091995
4949         local gid2=16022000
4950
4951         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
4952         local MULTIPID1=$!
4953         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
4954         local MULTIPID2=$!
4955         kill -USR1 $MULTIPID2
4956         sleep 2
4957         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
4958                 error "First grouplock does not block second one"
4959         else
4960                 echo "Second grouplock blocks first one"
4961         fi
4962         kill -USR1 $MULTIPID1
4963         wait $MULTIPID1
4964         wait $MULTIPID2
4965 }
4966 run_test 82 "Basic grouplock test ==============================="
4967
4968 test_99a() {
4969         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
4970             return
4971         mkdir -p $DIR/d99cvsroot
4972         chown $RUNAS_ID $DIR/d99cvsroot
4973         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
4974         cd $TMP
4975
4976         $RUNAS cvs -d $DIR/d99cvsroot init || error
4977         cd $oldPWD
4978 }
4979 run_test 99a "cvs init ========================================="
4980
4981 test_99b() {
4982         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4983         [ ! -d $DIR/d99cvsroot ] && test_99a
4984         cd /etc/init.d
4985         # some versions of cvs import exit(1) when asked to import links or
4986         # files they can't read.  ignore those files.
4987         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
4988                         ! -perm +4 -printf '-I %f\n')
4989         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
4990                 d99reposname vtag rtag
4991 }
4992 run_test 99b "cvs import ======================================="
4993
4994 test_99c() {
4995         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4996         [ ! -d $DIR/d99cvsroot ] && test_99b
4997         cd $DIR
4998         mkdir -p $DIR/d99reposname
4999         chown $RUNAS_ID $DIR/d99reposname
5000         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5001 }
5002 run_test 99c "cvs checkout ====================================="
5003
5004 test_99d() {
5005         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5006         [ ! -d $DIR/d99cvsroot ] && test_99c
5007         cd $DIR/d99reposname
5008         $RUNAS touch foo99
5009         $RUNAS cvs add -m 'addmsg' foo99
5010 }
5011 run_test 99d "cvs add =========================================="
5012
5013 test_99e() {
5014         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5015         [ ! -d $DIR/d99cvsroot ] && test_99c
5016         cd $DIR/d99reposname
5017         $RUNAS cvs update
5018 }
5019 run_test 99e "cvs update ======================================="
5020
5021 test_99f() {
5022         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5023         [ ! -d $DIR/d99cvsroot ] && test_99d
5024         cd $DIR/d99reposname
5025         $RUNAS cvs commit -m 'nomsg' foo99
5026     rm -fr $DIR/d99cvsroot
5027 }
5028 run_test 99f "cvs commit ======================================="
5029
5030 test_100() {
5031         [ "$NETTYPE" = tcp ] || \
5032                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5033                         return ; }
5034
5035         remote_ost_nodsh && skip "remote OST with nodsh" && return
5036         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5037         remote_servers || \
5038                 { skip "useless for local single node setup" && return; }
5039
5040         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5041                 [ "$PROT" != "tcp" ] && continue
5042                 RPORT=$(echo $REMOTE | cut -d: -f2)
5043                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5044
5045                 rc=0
5046                 LPORT=`echo $LOCAL | cut -d: -f2`
5047                 if [ $LPORT -ge 1024 ]; then
5048                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5049                         netstat -tna
5050                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5051                 fi
5052         done
5053         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5054 }
5055 run_test 100 "check local port using privileged port ==========="
5056
5057 function get_named_value()
5058 {
5059     local tag
5060
5061     tag=$1
5062     while read ;do
5063         line=$REPLY
5064         case $line in
5065         $tag*)
5066             echo $line | sed "s/^$tag[ ]*//"
5067             break
5068             ;;
5069         esac
5070     done
5071 }
5072
5073 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
5074 cleanup_101a() {
5075         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5076         trap 0
5077 }
5078
5079 test_101a() {
5080         local s
5081         local discard
5082         local nreads=10000
5083         [ "$CPU" = "UML" ] && nreads=1000
5084         local cache_limit=32
5085
5086         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5087         trap cleanup_101a EXIT
5088         $LCTL set_param -n llite.*.read_ahead_stats 0
5089         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5090
5091         #
5092         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5093         #
5094         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5095         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5096
5097         discard=0
5098         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5099                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5100                         discard=$(($discard + $s))
5101         done
5102         cleanup_101a
5103
5104         if [ $(($discard * 10)) -gt $nreads ] ;then
5105                 $LCTL get_param osc.*-osc*.rpc_stats
5106                 $LCTL get_param llite.*.read_ahead_stats
5107                 error "too many ($discard) discarded pages"
5108         fi
5109         rm -f $DIR/$tfile || true
5110 }
5111 run_test 101a "check read-ahead for random reads ================"
5112
5113 setup_test101bc() {
5114         mkdir -p $DIR/$tdir
5115         STRIPE_SIZE=1048576
5116         STRIPE_COUNT=$OSTCOUNT
5117         STRIPE_OFFSET=0
5118
5119         local list=$(comma_list $(osts_nodes))
5120         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=0
5121         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=0
5122
5123         trap cleanup_test101bc EXIT
5124         # prepare the read-ahead file
5125         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5126
5127         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5128 }
5129
5130 cleanup_test101bc() {
5131         trap 0
5132         rm -rf $DIR/$tdir
5133         rm -f $DIR/$tfile
5134
5135         local list=$(comma_list $(osts_nodes))
5136         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
5137         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=1
5138 }
5139
5140 calc_total() {
5141         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5142 }
5143
5144 ra_check_101() {
5145         local READ_SIZE=$1
5146         local STRIPE_SIZE=1048576
5147         local RA_INC=1048576
5148         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5149         local FILE_LENGTH=$((64*100))
5150         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5151                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5152         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5153                         get_named_value 'read but discarded' | \
5154                         cut -d" " -f1 | calc_total`
5155         if [ $DISCARD -gt $discard_limit ]; then
5156                 $LCTL get_param llite.*.read_ahead_stats
5157                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5158         else
5159                 echo "Read-ahead success for size ${READ_SIZE}"
5160         fi
5161 }
5162
5163 test_101b() {
5164         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5165         local STRIPE_SIZE=1048576
5166         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5167         local FILE_LENGTH=$((STRIPE_SIZE*100))
5168         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5169         # prepare the read-ahead file
5170         setup_test101bc
5171         cancel_lru_locks osc
5172         for BIDX in 2 4 8 16 32 64 128 256
5173         do
5174                 local BSIZE=$((BIDX*4096))
5175                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5176                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5177                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5178                 $LCTL set_param -n llite.*.read_ahead_stats 0
5179                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5180                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5181                 cancel_lru_locks osc
5182                 ra_check_101 $BSIZE
5183         done
5184         cleanup_test101bc
5185         true
5186 }
5187 run_test 101b "check stride-io mode read-ahead ================="
5188
5189 test_101c() {
5190     local STRIPE_SIZE=1048576
5191     local FILE_LENGTH=$((STRIPE_SIZE*100))
5192     local nreads=10000
5193     local osc
5194
5195     setup_test101bc
5196
5197     cancel_lru_locks osc
5198     $LCTL set_param osc.*.rpc_stats 0
5199     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5200     for osc in $($LCTL get_param -N osc.*); do
5201         if [ "$osc" == "osc.num_refs" ]; then
5202             continue
5203         fi
5204
5205         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5206         if [ $lines -le 20 ]; then
5207             continue
5208         fi
5209
5210         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5211                                      awk '$1 == "1:" { print $2; exit; }')
5212         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5213                                      awk '$1 == "2:" { print $2; exit; }')
5214         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5215                                      awk '$1 == "4:" { print $2; exit; }')
5216         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5217                                      awk '$1 == "8:" { print $2; exit; }')
5218
5219         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5220         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5221         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5222         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5223         echo "${osc} rpc check passed!"
5224     done
5225     cleanup_test101bc
5226     true
5227 }
5228 run_test 101c "check stripe_size aligned read-ahead ================="
5229
5230 set_read_ahead() {
5231    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5232    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5233 }
5234
5235 test_101d() {
5236     local file=$DIR/$tfile
5237     local size=${FILESIZE_101c:-500}
5238     local ra_MB=${READAHEAD_MB:-40}
5239
5240     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5241     [ $space -gt $((size / 1024)) ] ||
5242         { skip "Need free space ${size}M, have $space" && return; }
5243
5244     echo Creating ${size}M test file $file
5245     dd if=/dev/zero of=$file bs=1M count=$size
5246     echo Cancel LRU locks on lustre client to flush the client cache
5247     cancel_lru_locks osc
5248
5249     echo Disable read-ahead
5250     local old_READAHEAD=$(set_read_ahead 0)
5251
5252     echo Reading the test file $file with read-ahead disabled
5253     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5254
5255     echo Cancel LRU locks on lustre client to flush the client cache
5256     cancel_lru_locks osc
5257     echo Enable read-ahead with ${ra_MB}MB
5258     set_read_ahead $ra_MB
5259
5260     echo Reading the test file $file with read-ahead enabled
5261     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5262
5263     echo read-ahead disabled time read $time_ra_OFF
5264     echo read-ahead enabled  time read $time_ra_ON
5265
5266         set_read_ahead $old_READAHEAD
5267         rm -f $file
5268         wait_delete_completed
5269
5270     [ $time_ra_ON -lt $time_ra_OFF ] ||
5271         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5272                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5273 }
5274 run_test 101d "file read with and without read-ahead enabled  ================="
5275
5276 test_101e() {
5277     local file=$DIR/$tfile
5278     local size=500  #KB
5279     local count=100
5280     local blksize=1024
5281
5282     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5283     local need_space=$((count * size))
5284     [ $space -gt $need_space ] ||
5285         { skip_env "Need free space $need_space, have $space" && return; }
5286
5287     echo Creating $count ${size}K test files
5288     for ((i = 0; i < $count; i++)); do
5289         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5290     done
5291
5292     echo Cancel LRU locks on lustre client to flush the client cache
5293     cancel_lru_locks osc
5294
5295     echo Reset readahead stats
5296     $LCTL set_param -n llite.*.read_ahead_stats 0
5297
5298     for ((i = 0; i < $count; i++)); do
5299         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5300     done
5301
5302     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5303           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5304
5305     for ((i = 0; i < $count; i++)); do
5306         rm -rf ${file}_${i} 2>/dev/null
5307     done
5308
5309     #10000 means 20% reads are missing in readahead
5310     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5311 }
5312 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5313
5314 cleanup_test101f() {
5315     trap 0
5316     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5317     rm -rf $DIR/$tfile 2>/dev/null
5318 }
5319
5320 test_101f() {
5321     local file=$DIR/$tfile
5322     local nreads=1000
5323
5324     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5325     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5326     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5327     trap cleanup_test101f EXIT
5328
5329     echo Cancel LRU locks on lustre client to flush the client cache
5330     cancel_lru_locks osc
5331
5332     echo Reset readahead stats
5333     $LCTL set_param -n llite.*.read_ahead_stats 0
5334     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5335     # readahead should read in 2M file on second read, so only miss
5336     # 2 pages.
5337     echo Random 4K reads on 2M file for 1000 times
5338     $READS -f $file -s 2097152 -b 4096 -n $nreads
5339
5340     echo checking missing pages
5341     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5342           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5343
5344     [ $miss -lt 3 ] || error "misses too much pages!"
5345     cleanup_test101f
5346 }
5347 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5348
5349 setup_test102() {
5350         mkdir -p $DIR/$tdir
5351         chown $RUNAS_ID $DIR/$tdir
5352         STRIPE_SIZE=65536
5353         STRIPE_OFFSET=1
5354         STRIPE_COUNT=$OSTCOUNT
5355         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5356
5357         trap cleanup_test102 EXIT
5358         cd $DIR
5359         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5360         cd $DIR/$tdir
5361         for num in 1 2 3 4; do
5362                 for count in $(seq 1 $STRIPE_COUNT); do
5363                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5364                                 local size=`expr $STRIPE_SIZE \* $num`
5365                                 local file=file"$num-$idx-$count"
5366                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5367                         done
5368                 done
5369         done
5370
5371         cd $DIR
5372         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5373 }
5374
5375 cleanup_test102() {
5376         trap 0
5377         rm -f $TMP/f102.tar
5378         rm -rf $DIR/d0.sanity/d102
5379 }
5380
5381 test_102a() {
5382         local testfile=$DIR/xattr_testfile
5383
5384         touch $testfile
5385
5386         [ "$UID" != 0 ] && skip_env "must run as root" && return
5387         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5388                 skip_env "must have user_xattr" && return
5389
5390         [ -z "$(which setfattr 2>/dev/null)" ] &&
5391                 skip_env "could not find setfattr" && return
5392
5393         echo "set/get xattr..."
5394         setfattr -n trusted.name1 -v value1 $testfile || error
5395         getfattr -n trusted.name1 $testfile 2> /dev/null |
5396           grep "trusted.name1=.value1" ||
5397                 error "$testfile missing trusted.name1=value1"
5398
5399         setfattr -n user.author1 -v author1 $testfile || error
5400         getfattr -n user.author1 $testfile 2> /dev/null |
5401           grep "user.author1=.author1" ||
5402                 error "$testfile missing trusted.author1=author1"
5403
5404         echo "listxattr..."
5405         setfattr -n trusted.name2 -v value2 $testfile ||
5406                 error "$testfile unable to set trusted.name2"
5407         setfattr -n trusted.name3 -v value3 $testfile ||
5408                 error "$testfile unable to set trusted.name3"
5409         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5410             grep "trusted.name" | wc -l) -eq 3 ] ||
5411                 error "$testfile missing 3 trusted.name xattrs"
5412
5413         setfattr -n user.author2 -v author2 $testfile ||
5414                 error "$testfile unable to set user.author2"
5415         setfattr -n user.author3 -v author3 $testfile ||
5416                 error "$testfile unable to set user.author3"
5417         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5418             grep "user.author" | wc -l) -eq 3 ] ||
5419                 error "$testfile missing 3 user.author xattrs"
5420
5421         echo "remove xattr..."
5422         setfattr -x trusted.name1 $testfile ||
5423                 error "$testfile error deleting trusted.name1"
5424         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5425                 error "$testfile did not delete trusted.name1 xattr"
5426
5427         setfattr -x user.author1 $testfile ||
5428                 error "$testfile error deleting user.author1"
5429         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5430                 error "$testfile did not delete trusted.name1 xattr"
5431
5432         # b10667: setting lustre special xattr be silently discarded
5433         echo "set lustre special xattr ..."
5434         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5435                 error "$testfile allowed setting trusted.lov"
5436 }
5437 run_test 102a "user xattr test =================================="
5438
5439 test_102b() {
5440         # b10930: get/set/list trusted.lov xattr
5441         echo "get/set/list trusted.lov xattr ..."
5442         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5443         local testfile=$DIR/$tfile
5444         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5445                 error "setstripe failed"
5446         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5447                 error "getstripe failed"
5448         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5449         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5450
5451         local testfile2=${testfile}2
5452         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5453                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5454
5455         $MCREATE $testfile2
5456         setfattr -n trusted.lov -v $value $testfile2
5457         local stripe_size=$($GETSTRIPE -S $testfile2)
5458         local stripe_count=$($GETSTRIPE -c $testfile2)
5459         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5460         [ $stripe_count -eq $STRIPECOUNT ] ||
5461                 error "stripe count $stripe_count != $STRIPECOUNT"
5462         rm -f $DIR/$tfile
5463 }
5464 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5465
5466 test_102c() {
5467         # b10930: get/set/list lustre.lov xattr
5468         echo "get/set/list lustre.lov xattr ..."
5469         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5470         mkdir -p $DIR/$tdir
5471         chown $RUNAS_ID $DIR/$tdir
5472         local testfile=$DIR/$tdir/$tfile
5473         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5474                 error "setstripe failed"
5475         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5476                 error "getstripe failed"
5477         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5478         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5479
5480         local testfile2=${testfile}2
5481         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5482                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5483
5484         $RUNAS $MCREATE $testfile2
5485         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5486         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5487         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5488         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5489         [ $stripe_count -eq $STRIPECOUNT ] ||
5490                 error "stripe count $stripe_count != $STRIPECOUNT"
5491 }
5492 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5493
5494 compare_stripe_info1() {
5495         local stripe_index_all_zero=true
5496
5497         for num in 1 2 3 4; do
5498                 for count in $(seq 1 $STRIPE_COUNT); do
5499                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5500                                 local size=$((STRIPE_SIZE * num))
5501                                 local file=file"$num-$offset-$count"
5502                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5503                                 [ $stripe_size -ne $size ] &&
5504                                     error "$file: size $stripe_size != $size"
5505                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5506                                 # allow fewer stripes to be created, ORI-601
5507                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5508                                     error "$file: count $stripe_count != $count"
5509                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5510                                 [ $stripe_index -ne 0 ] &&
5511                                         stripe_index_all_zero=false
5512                         done
5513                 done
5514         done
5515         $stripe_index_all_zero &&
5516                 error "all files are being extracted starting from OST index 0"
5517         return 0
5518 }
5519
5520 find_lustre_tar() {
5521         [ -n "$(which tar 2>/dev/null)" ] &&
5522                 strings $(which tar) | grep -q "lustre" && echo tar
5523 }
5524
5525 test_102d() {
5526         # b10930: tar test for trusted.lov xattr
5527         TAR=$(find_lustre_tar)
5528         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5529         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5530         setup_test102
5531         mkdir -p $DIR/d102d
5532         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5533         cd $DIR/d102d/$tdir
5534         compare_stripe_info1
5535 }
5536 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5537
5538 test_102f() {
5539         # b10930: tar test for trusted.lov xattr
5540         TAR=$(find_lustre_tar)
5541         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5542         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5543         setup_test102
5544         mkdir -p $DIR/d102f
5545         cd $DIR
5546         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5547         cd $DIR/d102f/$tdir
5548         compare_stripe_info1
5549 }
5550 run_test 102f "tar copy files, not keep osts ==========="
5551
5552 grow_xattr() {
5553         local xsize=${1:-1024}  # in bytes
5554         local file=$DIR/$tfile
5555
5556         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5557                 skip "must have user_xattr" && return 0
5558         [ -z "$(which setfattr 2>/dev/null)" ] &&
5559                 skip_env "could not find setfattr" && return 0
5560         [ -z "$(which getfattr 2>/dev/null)" ] &&
5561                 skip_env "could not find getfattr" && return 0
5562
5563         touch $file
5564
5565         local value="$(generate_string $xsize)"
5566
5567         local xbig=trusted.big
5568         log "save $xbig on $file"
5569         setfattr -n $xbig -v $value $file ||
5570                 error "saving $xbig on $file failed"
5571
5572         local orig=$(get_xattr_value $xbig $file)
5573         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
5574
5575         local xsml=trusted.sml
5576         log "save $xsml on $file"
5577         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
5578
5579         local new=$(get_xattr_value $xbig $file)
5580         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
5581
5582         log "grow $xsml on $file"
5583         setfattr -n $xsml -v "$value" $file ||
5584                 error "growing $xsml on $file failed"
5585
5586         new=$(get_xattr_value $xbig $file)
5587         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
5588         log "$xbig still valid after growing $xsml"
5589
5590         rm -f $file
5591 }
5592
5593 test_102h() { # bug 15777
5594         grow_xattr 1024
5595 }
5596 run_test 102h "grow xattr from inside inode to external block"
5597
5598 test_102ha() {
5599         large_xattr_enabled || { skip "large_xattr disabled" && return; }
5600         grow_xattr $(max_xattr_size)
5601 }
5602 run_test 102ha "grow xattr from inside inode to external inode"
5603
5604 test_102i() { # bug 17038
5605         touch $DIR/$tfile
5606         ln -s $DIR/$tfile $DIR/${tfile}link
5607         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
5608         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"
5609         rm -f $DIR/$tfile $DIR/${tfile}link
5610 }
5611 run_test 102i "lgetxattr test on symbolic link ============"
5612
5613 test_102j() {
5614         TAR=$(find_lustre_tar)
5615         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5616         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5617         setup_test102 "$RUNAS"
5618         mkdir -p $DIR/d102j
5619         chown $RUNAS_ID $DIR/d102j
5620         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
5621         cd $DIR/d102j/$tdir
5622         compare_stripe_info1 "$RUNAS"
5623 }
5624 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
5625
5626 test_102k() {
5627         touch $DIR/$tfile
5628         # b22187 just check that does not crash for regular file.
5629         setfattr -n trusted.lov $DIR/$tfile
5630         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
5631         local test_kdir=$DIR/d102k
5632         mkdir $test_kdir
5633         local default_size=`$GETSTRIPE -S $test_kdir`
5634         local default_count=`$GETSTRIPE -c $test_kdir`
5635         local default_offset=`$GETSTRIPE -i $test_kdir`
5636         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
5637                 error 'dir setstripe failed'
5638         setfattr -n trusted.lov $test_kdir
5639         local stripe_size=`$GETSTRIPE -S $test_kdir`
5640         local stripe_count=`$GETSTRIPE -c $test_kdir`
5641         local stripe_offset=`$GETSTRIPE -i $test_kdir`
5642         [ $stripe_size -eq $default_size ] ||
5643                 error "stripe size $stripe_size != $default_size"
5644         [ $stripe_count -eq $default_count ] ||
5645                 error "stripe count $stripe_count != $default_count"
5646         [ $stripe_offset -eq $default_offset ] ||
5647                 error "stripe offset $stripe_offset != $default_offset"
5648         rm -rf $DIR/$tfile $test_kdir
5649 }
5650 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
5651
5652 test_102l() {
5653         # LU-532 trusted. xattr is invisible to non-root
5654         local testfile=$DIR/$tfile
5655
5656         touch $testfile
5657
5658         echo "listxattr as user..."
5659         chown $RUNAS_ID $testfile
5660         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
5661             grep -q "trusted" &&
5662                 error "$testfile trusted xattrs are user visible"
5663
5664         return 0;
5665 }
5666 run_test 102l "listxattr filter test =================================="
5667
5668 cleanup_test102
5669
5670 run_acl_subtest()
5671 {
5672     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
5673     return $?
5674 }
5675
5676 test_103 () {
5677     [ "$UID" != 0 ] && skip_env "must run as root" && return
5678     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5679     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
5680     $GSS && skip "could not run under gss" && return
5681
5682     declare -a identity_old
5683
5684     for num in `seq $MDSCOUNT`; do
5685         switch_identity $num true || identity_old[$num]=$?
5686     done
5687
5688     SAVE_UMASK=`umask`
5689     umask 0022
5690     cd $DIR
5691
5692     echo "performing cp ..."
5693     run_acl_subtest cp || error
5694     echo "performing getfacl-noacl..."
5695     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
5696     echo "performing misc..."
5697     run_acl_subtest misc || error  "misc test failed"
5698     echo "performing permissions..."
5699     run_acl_subtest permissions || error "permissions failed"
5700     echo "performing setfacl..."
5701     run_acl_subtest setfacl || error  "setfacl test failed"
5702
5703     # inheritance test got from HP
5704     echo "performing inheritance..."
5705     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
5706     chmod +x make-tree || error "chmod +x failed"
5707     run_acl_subtest inheritance || error "inheritance test failed"
5708     rm -f make-tree
5709
5710     cd $SAVE_PWD
5711     umask $SAVE_UMASK
5712
5713     for num in `seq $MDSCOUNT`; do
5714         if [ "${identity_old[$num]}" = 1 ]; then
5715             switch_identity $num false || identity_old[$num]=$?
5716         fi
5717     done
5718 }
5719 run_test 103 "acl test ========================================="
5720
5721 test_104a() {
5722         touch $DIR/$tfile
5723         lfs df || error "lfs df failed"
5724         lfs df -ih || error "lfs df -ih failed"
5725         lfs df -h $DIR || error "lfs df -h $DIR failed"
5726         lfs df -i $DIR || error "lfs df -i $DIR failed"
5727         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
5728         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
5729
5730         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
5731         lctl --device %$OSC deactivate
5732         lfs df || error "lfs df with deactivated OSC failed"
5733         lctl --device %$OSC activate
5734         # wait the osc back to normal
5735         wait_osc_import_state client ost FULL
5736
5737         lfs df || error "lfs df with reactivated OSC failed"
5738         rm -f $DIR/$tfile
5739 }
5740 run_test 104a "lfs df [-ih] [path] test ========================="
5741
5742 test_104b() {
5743         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5744         chmod 666 /dev/obd
5745         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
5746         if [ $denied_cnt -ne 0 ];
5747         then
5748                     error "lfs check servers test failed"
5749         fi
5750 }
5751 run_test 104b "$RUNAS lfs check servers test ===================="
5752
5753 test_105a() {
5754         # doesn't work on 2.4 kernels
5755         touch $DIR/$tfile
5756         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5757         then
5758                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
5759         else
5760                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
5761         fi
5762         rm -f $DIR/$tfile
5763 }
5764 run_test 105a "flock when mounted without -o flock test ========"
5765
5766 test_105b() {
5767         touch $DIR/$tfile
5768         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5769         then
5770                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
5771         else
5772                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
5773         fi
5774         rm -f $DIR/$tfile
5775 }
5776 run_test 105b "fcntl when mounted without -o flock test ========"
5777
5778 test_105c() {
5779         touch $DIR/$tfile
5780         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5781         then
5782                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
5783         else
5784                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
5785         fi
5786         rm -f $DIR/$tfile
5787 }
5788 run_test 105c "lockf when mounted without -o flock test ========"
5789
5790 test_105d() { # bug 15924
5791         mkdir -p $DIR/$tdir
5792         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5793                 skip "mount w/o flock enabled" && return
5794         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
5795         $LCTL set_param fail_loc=0x80000315
5796         flocks_test 2 $DIR/$tdir
5797 }
5798 run_test 105d "flock race (should not freeze) ========"
5799
5800 test_105e() { # bug 22660 && 22040
5801         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5802                 skip "mount w/o flock enabled" && return
5803         touch $DIR/$tfile
5804         flocks_test 3 $DIR/$tfile
5805 }
5806 run_test 105e "Two conflicting flocks from same process ======="
5807
5808 test_106() { #bug 10921
5809         mkdir -p $DIR/$tdir
5810         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
5811         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
5812 }
5813 run_test 106 "attempt exec of dir followed by chown of that dir"
5814
5815 test_107() {
5816         CDIR=`pwd`
5817         cd $DIR
5818
5819         local file=core
5820         rm -f $file
5821
5822         local save_pattern=$(sysctl -n kernel.core_pattern)
5823         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
5824         sysctl -w kernel.core_pattern=$file
5825         sysctl -w kernel.core_uses_pid=0
5826
5827         ulimit -c unlimited
5828         sleep 60 &
5829         SLEEPPID=$!
5830
5831         sleep 1
5832
5833         kill -s 11 $SLEEPPID
5834         wait $SLEEPPID
5835         if [ -e $file ]; then
5836                 size=`stat -c%s $file`
5837                 [ $size -eq 0 ] && error "Fail to create core file $file"
5838         else
5839                 error "Fail to create core file $file"
5840         fi
5841         rm -f $file
5842         sysctl -w kernel.core_pattern=$save_pattern
5843         sysctl -w kernel.core_uses_pid=$save_uses_pid
5844         cd $CDIR
5845 }
5846 run_test 107 "Coredump on SIG"
5847
5848 test_110() {
5849         mkdir -p $DIR/d110
5850         mkdir $DIR/d110/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || error "mkdir with 255 char fail"
5851         mkdir $DIR/d110/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && error "mkdir with 256 char should fail, but not"
5852         touch $DIR/d110/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx || error "create with 255 char fail"
5853         touch $DIR/d110/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy && error ""create with 256 char should fail, but not
5854
5855         ls -l $DIR/d110
5856     rm -fr $DIR/d110
5857 }
5858 run_test 110 "filename length checking"
5859
5860 test_115() {
5861         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5862             cut -c11-20)
5863         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
5864             return
5865         echo "Starting with $OSTIO_pre threads"
5866
5867         NUMTEST=20000
5868         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
5869         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
5870         echo "$NUMTEST creates/unlinks"
5871         mkdir -p $DIR/$tdir
5872         createmany -o $DIR/$tdir/$tfile $NUMTEST
5873         unlinkmany $DIR/$tdir/$tfile $NUMTEST
5874
5875         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5876             cut -c11-20)
5877
5878         # don't return an error
5879         [ $OSTIO_post == $OSTIO_pre ] && echo \
5880             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
5881             echo "This may be fine, depending on what ran before this test" &&
5882             echo "and how fast this system is." && return
5883
5884         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
5885 }
5886 run_test 115 "verify dynamic thread creation===================="
5887
5888 free_min_max () {
5889         wait_delete_completed
5890         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
5891         echo OST kbytes available: ${AVAIL[@]}
5892         MAXI=0; MAXV=${AVAIL[0]}
5893         MINI=0; MINV=${AVAIL[0]}
5894         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
5895             #echo OST $i: ${AVAIL[i]}kb
5896             if [ ${AVAIL[i]} -gt $MAXV ]; then
5897                 MAXV=${AVAIL[i]}; MAXI=$i
5898             fi
5899             if [ ${AVAIL[i]} -lt $MINV ]; then
5900                 MINV=${AVAIL[i]}; MINI=$i
5901             fi
5902         done
5903         echo Min free space: OST $MINI: $MINV
5904         echo Max free space: OST $MAXI: $MAXV
5905 }
5906
5907 test_116() {
5908         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
5909
5910         echo -n "Free space priority "
5911         lctl get_param -n lov.*-clilov-*.qos_prio_free
5912         declare -a AVAIL
5913         free_min_max
5914         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
5915                 return
5916
5917         # generate uneven OSTs
5918         mkdir -p $DIR/$tdir/OST${MINI}
5919         declare -i FILL
5920         FILL=$(($MINV / 4))
5921         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
5922         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
5923         i=0
5924         while [ $FILL -gt 0 ]; do
5925             i=$(($i + 1))
5926             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
5927             FILL=$(($FILL - 2048))
5928             echo -n .
5929         done
5930         FILL=$(($MINV / 4))
5931         sync
5932         sleep_maxage
5933
5934         free_min_max
5935         DIFF=$(($MAXV - $MINV))
5936         DIFF2=$(($DIFF * 100 / $MINV))
5937         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
5938         if [ $DIFF2 -gt 20 ]; then
5939             echo "ok"
5940         else
5941             echo "failed - QOS mode won't be used"
5942             error_ignore "QOS imbalance criteria not met"
5943             return
5944         fi
5945
5946         MINI1=$MINI; MINV1=$MINV
5947         MAXI1=$MAXI; MAXV1=$MAXV
5948
5949         # now fill using QOS
5950         echo writing a bunch of files to QOS-assigned OSTs
5951         $SETSTRIPE -c 1 $DIR/$tdir
5952         i=0
5953         while [ $FILL -gt 0 ]; do
5954             i=$(($i + 1))
5955             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
5956             FILL=$(($FILL - 200))
5957             echo -n .
5958         done
5959         echo "wrote $i 200k files"
5960         sync
5961         sleep_maxage
5962
5963         echo "Note: free space may not be updated, so measurements might be off"
5964         free_min_max
5965         DIFF2=$(($MAXV - $MINV))
5966         echo "free space delta: orig $DIFF final $DIFF2"
5967         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
5968         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
5969         echo "Wrote $DIFF to smaller OST $MINI1"
5970         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
5971         echo "Wrote $DIFF2 to larger OST $MAXI1"
5972         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
5973
5974         # Figure out which files were written where
5975         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
5976                awk '/'$MINI1': / {print $2; exit}')
5977         echo $UUID
5978         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
5979         echo "$MINC files created on smaller OST $MINI1"
5980         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
5981                awk '/'$MAXI1': / {print $2; exit}')
5982         echo $UUID
5983         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
5984         echo "$MAXC files created on larger OST $MAXI1"
5985         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
5986         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
5987
5988         rm -rf $DIR/$tdir
5989 }
5990 run_test 116 "stripe QOS: free space balance ==================="
5991
5992 test_117() # bug 10891
5993 {
5994         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
5995         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
5996         lctl set_param fail_loc=0x21e
5997         > $DIR/$tfile || error "truncate failed"
5998         lctl set_param fail_loc=0
5999         echo "Truncate succeeded."
6000         rm -f $DIR/$tfile
6001 }
6002 run_test 117 "verify fsfilt_extend =========="
6003
6004 export OLD_RESENDCOUNT=""
6005 set_resend_count () {
6006         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6007         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6008         lctl set_param -n $PROC_RESENDCOUNT $1
6009         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6010 }
6011
6012 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6013
6014 # Reset async IO behavior after error case
6015 reset_async() {
6016         FILE=$DIR/reset_async
6017
6018         # Ensure all OSCs are cleared
6019         $SETSTRIPE -c -1 $FILE
6020         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6021         sync
6022         rm $FILE
6023 }
6024
6025 test_118a() #bug 11710
6026 {
6027         reset_async
6028
6029         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6030         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6031         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6032
6033         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6034                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6035                 return 1;
6036         fi
6037         rm -f $DIR/$tfile
6038 }
6039 run_test 118a "verify O_SYNC works =========="
6040
6041 test_118b()
6042 {
6043         remote_ost_nodsh && skip "remote OST with nodsh" && return
6044
6045         reset_async
6046
6047         #define OBD_FAIL_OST_ENOENT 0x217
6048         set_nodes_failloc "$(osts_nodes)" 0x217
6049         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6050         RC=$?
6051         set_nodes_failloc "$(osts_nodes)" 0
6052         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6053         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6054                     grep -c writeback)
6055
6056         if [[ $RC -eq 0 ]]; then
6057                 error "Must return error due to dropped pages, rc=$RC"
6058                 return 1;
6059         fi
6060
6061         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6062                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6063                 return 1;
6064         fi
6065
6066         echo "Dirty pages not leaked on ENOENT"
6067
6068         # Due to the above error the OSC will issue all RPCs syncronously
6069         # until a subsequent RPC completes successfully without error.
6070         $MULTIOP $DIR/$tfile Ow4096yc
6071         rm -f $DIR/$tfile
6072
6073         return 0
6074 }
6075 run_test 118b "Reclaim dirty pages on fatal error =========="
6076
6077 test_118c()
6078 {
6079         remote_ost_nodsh && skip "remote OST with nodsh" && return
6080
6081         reset_async
6082
6083         #define OBD_FAIL_OST_EROFS               0x216
6084         set_nodes_failloc "$(osts_nodes)" 0x216
6085
6086         # multiop should block due to fsync until pages are written
6087         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6088         MULTIPID=$!
6089         sleep 1
6090
6091         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6092                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6093         fi
6094
6095         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6096                     grep -c writeback)
6097         if [[ $WRITEBACK -eq 0 ]]; then
6098                 error "No page in writeback, writeback=$WRITEBACK"
6099         fi
6100
6101         set_nodes_failloc "$(osts_nodes)" 0
6102         wait $MULTIPID
6103         RC=$?
6104         if [[ $RC -ne 0 ]]; then
6105                 error "Multiop fsync failed, rc=$RC"
6106         fi
6107
6108         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6109         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6110                     grep -c writeback)
6111         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6112                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6113         fi
6114
6115         rm -f $DIR/$tfile
6116         echo "Dirty pages flushed via fsync on EROFS"
6117         return 0
6118 }
6119 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6120
6121 test_118d()
6122 {
6123         remote_ost_nodsh && skip "remote OST with nodsh" && return
6124
6125         reset_async
6126
6127         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6128         set_nodes_failloc "$(osts_nodes)" 0x214
6129         # multiop should block due to fsync until pages are written
6130         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6131         MULTIPID=$!
6132         sleep 1
6133
6134         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6135                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6136         fi
6137
6138         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6139                     grep -c writeback)
6140         if [[ $WRITEBACK -eq 0 ]]; then
6141                 error "No page in writeback, writeback=$WRITEBACK"
6142         fi
6143
6144         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6145         set_nodes_failloc "$(osts_nodes)" 0
6146
6147         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6148         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6149                     grep -c writeback)
6150         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6151                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6152         fi
6153
6154         rm -f $DIR/$tfile
6155         echo "Dirty pages gaurenteed flushed via fsync"
6156         return 0
6157 }
6158 run_test 118d "Fsync validation inject a delay of the bulk =========="
6159
6160 test_118f() {
6161         reset_async
6162
6163         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6164         lctl set_param fail_loc=0x8000040a
6165
6166         # Should simulate EINVAL error which is fatal
6167         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6168         RC=$?
6169         if [[ $RC -eq 0 ]]; then
6170                 error "Must return error due to dropped pages, rc=$RC"
6171         fi
6172
6173         lctl set_param fail_loc=0x0
6174
6175         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6176         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6177         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6178                     grep -c writeback)
6179         if [[ $LOCKED -ne 0 ]]; then
6180                 error "Locked pages remain in cache, locked=$LOCKED"
6181         fi
6182
6183         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6184                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6185         fi
6186
6187         rm -f $DIR/$tfile
6188         echo "No pages locked after fsync"
6189
6190         reset_async
6191         return 0
6192 }
6193 run_test 118f "Simulate unrecoverable OSC side error =========="
6194
6195 test_118g() {
6196         reset_async
6197
6198         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6199         lctl set_param fail_loc=0x406
6200
6201         # simulate local -ENOMEM
6202         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6203         RC=$?
6204
6205         lctl set_param fail_loc=0
6206         if [[ $RC -eq 0 ]]; then
6207                 error "Must return error due to dropped pages, rc=$RC"
6208         fi
6209
6210         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6211         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6212         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6213                         grep -c writeback)
6214         if [[ $LOCKED -ne 0 ]]; then
6215                 error "Locked pages remain in cache, locked=$LOCKED"
6216         fi
6217
6218         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6219                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6220         fi
6221
6222         rm -f $DIR/$tfile
6223         echo "No pages locked after fsync"
6224
6225         reset_async
6226         return 0
6227 }
6228 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6229
6230 test_118h() {
6231         remote_ost_nodsh && skip "remote OST with nodsh" && return
6232
6233         reset_async
6234
6235         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6236         set_nodes_failloc "$(osts_nodes)" 0x20e
6237         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6238         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6239         RC=$?
6240
6241         set_nodes_failloc "$(osts_nodes)" 0
6242         if [[ $RC -eq 0 ]]; then
6243                 error "Must return error due to dropped pages, rc=$RC"
6244         fi
6245
6246         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6247         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6248         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6249                     grep -c writeback)
6250         if [[ $LOCKED -ne 0 ]]; then
6251                 error "Locked pages remain in cache, locked=$LOCKED"
6252         fi
6253
6254         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6255                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6256         fi
6257
6258         rm -f $DIR/$tfile
6259         echo "No pages locked after fsync"
6260
6261         return 0
6262 }
6263 run_test 118h "Verify timeout in handling recoverables errors  =========="
6264
6265 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6266
6267 test_118i() {
6268         remote_ost_nodsh && skip "remote OST with nodsh" && return
6269
6270         reset_async
6271
6272         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6273         set_nodes_failloc "$(osts_nodes)" 0x20e
6274
6275         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6276         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6277         PID=$!
6278         sleep 5
6279         set_nodes_failloc "$(osts_nodes)" 0
6280
6281         wait $PID
6282         RC=$?
6283         if [[ $RC -ne 0 ]]; then
6284                 error "got error, but should be not, rc=$RC"
6285         fi
6286
6287         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6288         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6289         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6290         if [[ $LOCKED -ne 0 ]]; then
6291                 error "Locked pages remain in cache, locked=$LOCKED"
6292         fi
6293
6294         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6295                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6296         fi
6297
6298         rm -f $DIR/$tfile
6299         echo "No pages locked after fsync"
6300
6301         return 0
6302 }
6303 run_test 118i "Fix error before timeout in recoverable error  =========="
6304
6305 [ "$SLOW" = "no" ] && set_resend_count 4
6306
6307 test_118j() {
6308         remote_ost_nodsh && skip "remote OST with nodsh" && return
6309
6310         reset_async
6311
6312         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6313         set_nodes_failloc "$(osts_nodes)" 0x220
6314
6315         # return -EIO from OST
6316         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6317         RC=$?
6318         set_nodes_failloc "$(osts_nodes)" 0x0
6319         if [[ $RC -eq 0 ]]; then
6320                 error "Must return error due to dropped pages, rc=$RC"
6321         fi
6322
6323         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6324         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6325         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6326         if [[ $LOCKED -ne 0 ]]; then
6327                 error "Locked pages remain in cache, locked=$LOCKED"
6328         fi
6329
6330         # in recoverable error on OST we want resend and stay until it finished
6331         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6332                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6333         fi
6334
6335         rm -f $DIR/$tfile
6336         echo "No pages locked after fsync"
6337
6338         return 0
6339 }
6340 run_test 118j "Simulate unrecoverable OST side error =========="
6341
6342 test_118k()
6343 {
6344         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6345
6346         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6347         set_nodes_failloc "$(osts_nodes)" 0x20e
6348         mkdir -p $DIR/$tdir
6349
6350         for ((i=0;i<10;i++)); do
6351                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6352                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6353                 SLEEPPID=$!
6354                 sleep 0.500s
6355                 kill $SLEEPPID
6356                 wait $SLEEPPID
6357         done
6358
6359         set_nodes_failloc "$(osts_nodes)" 0
6360         rm -rf $DIR/$tdir
6361 }
6362 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6363
6364 test_118l()
6365 {
6366         # LU-646
6367         mkdir -p $DIR/$tdir
6368         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6369         rm -rf $DIR/$tdir
6370 }
6371 run_test 118l "fsync dir ========="
6372
6373 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6374
6375 test_119a() # bug 11737
6376 {
6377         BSIZE=$((512 * 1024))
6378         directio write $DIR/$tfile 0 1 $BSIZE
6379         # We ask to read two blocks, which is more than a file size.
6380         # directio will indicate an error when requested and actual
6381         # sizes aren't equeal (a normal situation in this case) and
6382         # print actual read amount.
6383         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6384         if [ "$NOB" != "$BSIZE" ]; then
6385                 error "read $NOB bytes instead of $BSIZE"
6386         fi
6387         rm -f $DIR/$tfile
6388 }
6389 run_test 119a "Short directIO read must return actual read amount"
6390
6391 test_119b() # bug 11737
6392 {
6393         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6394
6395         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6396         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6397         sync
6398         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6399                 error "direct read failed"
6400         rm -f $DIR/$tfile
6401 }
6402 run_test 119b "Sparse directIO read must return actual read amount"
6403
6404 test_119c() # bug 13099
6405 {
6406         BSIZE=1048576
6407         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6408         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6409         rm -f $DIR/$tfile
6410 }
6411 run_test 119c "Testing for direct read hitting hole"
6412
6413 test_119d() # bug 15950
6414 {
6415         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6416         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6417         BSIZE=1048576
6418         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6419         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6420         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6421         lctl set_param fail_loc=0x40d
6422         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6423         pid_dio=$!
6424         sleep 1
6425         cat $DIR/$tfile > /dev/null &
6426         lctl set_param fail_loc=0
6427         pid_reads=$!
6428         wait $pid_dio
6429         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6430         sleep 2
6431         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6432         error "the read rpcs have not completed in 2s"
6433         rm -f $DIR/$tfile
6434         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6435 }
6436 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6437
6438 test_120a() {
6439         mkdir -p $DIR/$tdir
6440         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6441                skip "no early lock cancel on server" && return 0
6442         lru_resize_disable mdc
6443         lru_resize_disable osc
6444         cancel_lru_locks mdc
6445         stat $DIR/$tdir > /dev/null
6446         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6447         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6448         mkdir $DIR/$tdir/d1
6449         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6450         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6451         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6452         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6453         lru_resize_enable mdc
6454         lru_resize_enable osc
6455 }
6456 run_test 120a "Early Lock Cancel: mkdir test"
6457
6458 test_120b() {
6459         mkdir -p $DIR/$tdir
6460         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6461                skip "no early lock cancel on server" && return 0
6462         lru_resize_disable mdc
6463         lru_resize_disable osc
6464         cancel_lru_locks mdc
6465         stat $DIR/$tdir > /dev/null
6466         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6467         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6468         touch $DIR/$tdir/f1
6469         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6470         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6471         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6472         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6473         lru_resize_enable mdc
6474         lru_resize_enable osc
6475 }
6476 run_test 120b "Early Lock Cancel: create test"
6477
6478 test_120c() {
6479         mkdir -p $DIR/$tdir
6480         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6481                skip "no early lock cancel on server" && return 0
6482         lru_resize_disable mdc
6483         lru_resize_disable osc
6484         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6485         touch $DIR/$tdir/d1/f1
6486         cancel_lru_locks mdc
6487         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6488         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6489         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6490         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6491         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6492         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6493         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6494         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6495         lru_resize_enable mdc
6496         lru_resize_enable osc
6497 }
6498 run_test 120c "Early Lock Cancel: link test"
6499
6500 test_120d() {
6501         mkdir -p $DIR/$tdir
6502         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6503                skip "no early lock cancel on server" && return 0
6504         lru_resize_disable mdc
6505         lru_resize_disable osc
6506         touch $DIR/$tdir
6507         cancel_lru_locks mdc
6508         stat $DIR/$tdir > /dev/null
6509         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6510         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6511         chmod a+x $DIR/$tdir
6512         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6513         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6514         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6515         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6516         lru_resize_enable mdc
6517         lru_resize_enable osc
6518 }
6519 run_test 120d "Early Lock Cancel: setattr test"
6520
6521 test_120e() {
6522         mkdir -p $DIR/$tdir
6523         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6524                skip "no early lock cancel on server" && return 0
6525         lru_resize_disable mdc
6526         lru_resize_disable osc
6527         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6528         cancel_lru_locks mdc
6529         cancel_lru_locks osc
6530         dd if=$DIR/$tdir/f1 of=/dev/null
6531         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
6532         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6533               awk '/ldlm_cancel/ {print $2}'`
6534         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6535               awk '/ldlm_bl_callback/ {print $2}'`
6536         unlink $DIR/$tdir/f1
6537         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6538               awk '/ldlm_cancel/ {print $2}'`
6539         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6540               awk '/ldlm_bl_callback/ {print $2}'`
6541         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6542         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6543         lru_resize_enable mdc
6544         lru_resize_enable osc
6545 }
6546 run_test 120e "Early Lock Cancel: unlink test"
6547
6548 test_120f() {
6549         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6550                skip "no early lock cancel on server" && return 0
6551         mkdir -p $DIR/$tdir
6552         lru_resize_disable mdc
6553         lru_resize_disable osc
6554         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6555         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
6556         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
6557         cancel_lru_locks mdc
6558         cancel_lru_locks osc
6559         dd if=$DIR/$tdir/d1/f1 of=/dev/null
6560         dd if=$DIR/$tdir/d2/f2 of=/dev/null
6561         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
6562         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6563               awk '/ldlm_cancel/ {print $2}'`
6564         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6565               awk '/ldlm_bl_callback/ {print $2}'`
6566         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6567         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6568               awk '/ldlm_cancel/ {print $2}'`
6569         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6570               awk '/ldlm_bl_callback/ {print $2}'`
6571         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6572         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6573         lru_resize_enable mdc
6574         lru_resize_enable osc
6575 }
6576 run_test 120f "Early Lock Cancel: rename test"
6577
6578 test_120g() {
6579         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6580                skip "no early lock cancel on server" && return 0
6581         lru_resize_disable mdc
6582         lru_resize_disable osc
6583         count=10000
6584         echo create $count files
6585         mkdir -p $DIR/$tdir
6586         cancel_lru_locks mdc
6587         cancel_lru_locks osc
6588         t0=`date +%s`
6589
6590         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6591               awk '/ldlm_cancel/ {print $2}'`
6592         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6593               awk '/ldlm_bl_callback/ {print $2}'`
6594         createmany -o $DIR/$tdir/f $count
6595         sync
6596         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6597               awk '/ldlm_cancel/ {print $2}'`
6598         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6599               awk '/ldlm_bl_callback/ {print $2}'`
6600         t1=`date +%s`
6601         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
6602         echo rm $count files
6603         rm -r $DIR/$tdir
6604         sync
6605         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6606               awk '/ldlm_cancel/ {print $2}'`
6607         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6608               awk '/ldlm_bl_callback/ {print $2}'`
6609         t2=`date +%s`
6610         echo total: $count removes in $((t2-t1))
6611         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
6612         sleep 2
6613         # wait for commitment of removal
6614         lru_resize_enable mdc
6615         lru_resize_enable osc
6616 }
6617 run_test 120g "Early Lock Cancel: performance test"
6618
6619 test_121() { #bug #10589
6620         rm -rf $DIR/$tfile
6621         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
6622 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
6623         lctl set_param fail_loc=0x310
6624         cancel_lru_locks osc > /dev/null
6625         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
6626         lctl set_param fail_loc=0
6627         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
6628 }
6629 run_test 121 "read cancel race ========="
6630
6631 test_123a() { # was test 123, statahead(bug 11401)
6632         SLOWOK=0
6633         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
6634             log "testing on UP system. Performance may be not as good as expected."
6635                         SLOWOK=1
6636         fi
6637
6638         rm -rf $DIR/$tdir
6639         mkdir -p $DIR/$tdir
6640         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6641         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
6642         MULT=10
6643         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
6644                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
6645
6646                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6647                 lctl set_param -n llite.*.statahead_max 0
6648                 lctl get_param llite.*.statahead_max
6649                 cancel_lru_locks mdc
6650                 cancel_lru_locks osc
6651                 stime=`date +%s`
6652                 time ls -l $DIR/$tdir | wc -l
6653                 etime=`date +%s`
6654                 delta=$((etime - stime))
6655                 log "ls $i files without statahead: $delta sec"
6656                 lctl set_param llite.*.statahead_max=$max
6657
6658                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6659                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
6660                 cancel_lru_locks mdc
6661                 cancel_lru_locks osc
6662                 stime=`date +%s`
6663                 time ls -l $DIR/$tdir | wc -l
6664                 etime=`date +%s`
6665                 delta_sa=$((etime - stime))
6666                 log "ls $i files with statahead: $delta_sa sec"
6667                 lctl get_param -n llite.*.statahead_stats
6668                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6669
6670                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
6671                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
6672
6673                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6674                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6675                     lctl set_param -n llite.*.statahead_max 0
6676                     lctl get_param llite.*.statahead_max
6677                     cancel_lru_locks mdc
6678                     cancel_lru_locks osc
6679                     stime=`date +%s`
6680                     time ls -l $DIR/$tdir | wc -l
6681                     etime=`date +%s`
6682                     delta=$((etime - stime))
6683                     log "ls $i files again without statahead: $delta sec"
6684                     lctl set_param llite.*.statahead_max=$max
6685                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6686                         if [  $SLOWOK -eq 0 ]; then
6687                                 error "ls $i files is slower with statahead!"
6688                         else
6689                                 log "ls $i files is slower with statahead!"
6690                         fi
6691                         break
6692                     fi
6693                 fi
6694
6695                 [ $delta -gt 20 ] && break
6696                 [ $delta -gt 8 ] && MULT=$((50 / delta))
6697                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
6698         done
6699         log "ls done"
6700
6701         stime=`date +%s`
6702         rm -r $DIR/$tdir
6703         sync
6704         etime=`date +%s`
6705         delta=$((etime - stime))
6706         log "rm -r $DIR/$tdir/: $delta seconds"
6707         log "rm done"
6708         lctl get_param -n llite.*.statahead_stats
6709 }
6710 run_test 123a "verify statahead work"
6711
6712 test_123b () { # statahead(bug 15027)
6713         mkdir -p $DIR/$tdir
6714         createmany -o $DIR/$tdir/$tfile-%d 1000
6715
6716         cancel_lru_locks mdc
6717         cancel_lru_locks osc
6718
6719 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
6720         lctl set_param fail_loc=0x80000803
6721         ls -lR $DIR/$tdir > /dev/null
6722         log "ls done"
6723         lctl set_param fail_loc=0x0
6724         lctl get_param -n llite.*.statahead_stats
6725         rm -r $DIR/$tdir
6726         sync
6727
6728 }
6729 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
6730
6731 test_124a() {
6732         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6733                skip "no lru resize on server" && return 0
6734         local NR=2000
6735         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
6736
6737         log "create $NR files at $DIR/$tdir"
6738         createmany -o $DIR/$tdir/f $NR ||
6739                 error "failed to create $NR files in $DIR/$tdir"
6740
6741         cancel_lru_locks mdc
6742         ls -l $DIR/$tdir > /dev/null
6743
6744         local NSDIR=""
6745         local LRU_SIZE=0
6746         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
6747                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6748                 LRU_SIZE=$(lctl get_param -n $PARAM)
6749                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
6750                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
6751                                                 log "NSDIR=$NSDIR"
6752                         log "NS=$(basename $NSDIR)"
6753                         break
6754                 fi
6755         done
6756
6757         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
6758                 skip "Not enough cached locks created!"
6759                 return 0
6760         fi
6761         log "LRU=$LRU_SIZE"
6762
6763         local SLEEP=30
6764
6765         # We know that lru resize allows one client to hold $LIMIT locks
6766         # for 10h. After that locks begin to be killed by client.
6767         local MAX_HRS=10
6768         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
6769                 log "LIMIT=$LIMIT"
6770
6771         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
6772         # killing locks. Some time was spent for creating locks. This means
6773         # that up to the moment of sleep finish we must have killed some of
6774         # them (10-100 locks). This depends on how fast ther were created.
6775         # Many of them were touched in almost the same moment and thus will
6776         # be killed in groups.
6777         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
6778
6779         # Use $LRU_SIZE_B here to take into account real number of locks
6780         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
6781         local LRU_SIZE_B=$LRU_SIZE
6782         log "LVF=$LVF"
6783         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
6784                 log "OLD_LVF=$OLD_LVF"
6785         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
6786
6787         # Let's make sure that we really have some margin. Client checks
6788         # cached locks every 10 sec.
6789         SLEEP=$((SLEEP+20))
6790         log "Sleep ${SLEEP} sec"
6791         local SEC=0
6792         while ((SEC<$SLEEP)); do
6793                 echo -n "..."
6794                 sleep 5
6795                 SEC=$((SEC+5))
6796                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
6797                 echo -n "$LRU_SIZE"
6798         done
6799         echo ""
6800         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
6801         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
6802
6803         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
6804                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
6805                 unlinkmany $DIR/$tdir/f $NR
6806                 return
6807         }
6808
6809         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
6810         log "unlink $NR files at $DIR/$tdir"
6811         unlinkmany $DIR/$tdir/f $NR
6812 }
6813 run_test 124a "lru resize ======================================="
6814
6815 get_max_pool_limit()
6816 {
6817         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
6818         local max=0
6819         for l in $limit; do
6820                 if test $l -gt $max; then
6821                         max=$l
6822                 fi
6823         done
6824         echo $max
6825 }
6826
6827 test_124b() {
6828         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6829                skip "no lru resize on server" && return 0
6830
6831         LIMIT=`get_max_pool_limit`
6832
6833         NR=$(($(default_lru_size)*20))
6834         if [ $NR -gt $LIMIT ]; then
6835                 log "Limit lock number by $LIMIT locks"
6836                 NR=$LIMIT
6837         fi
6838         lru_resize_disable mdc
6839         mkdir -p $DIR/$tdir/disable_lru_resize ||
6840                 error "failed to create $DIR/$tdir/disable_lru_resize"
6841
6842         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
6843         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
6844         cancel_lru_locks mdc
6845         stime=`date +%s`
6846         PID=""
6847         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6848         PID="$PID $!"
6849         sleep 2
6850         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6851         PID="$PID $!"
6852         sleep 2
6853         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6854         PID="$PID $!"
6855         wait $PID
6856         etime=`date +%s`
6857         nolruresize_delta=$((etime-stime))
6858         log "ls -la time: $nolruresize_delta seconds"
6859         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6860         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
6861
6862         lru_resize_enable mdc
6863         mkdir -p $DIR/$tdir/enable_lru_resize ||
6864                 error "failed to create $DIR/$tdir/enable_lru_resize"
6865
6866         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
6867         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
6868         cancel_lru_locks mdc
6869         stime=`date +%s`
6870         PID=""
6871         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6872         PID="$PID $!"
6873         sleep 2
6874         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6875         PID="$PID $!"
6876         sleep 2
6877         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6878         PID="$PID $!"
6879         wait $PID
6880         etime=`date +%s`
6881         lruresize_delta=$((etime-stime))
6882         log "ls -la time: $lruresize_delta seconds"
6883         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6884
6885         if [ $lruresize_delta -gt $nolruresize_delta ]; then
6886                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
6887         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
6888                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
6889         else
6890                 log "lru resize performs the same with no lru resize"
6891         fi
6892         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
6893 }
6894 run_test 124b "lru resize (performance test) ======================="
6895
6896 test_125() { # 13358
6897         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6898         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6899         mkdir -p $DIR/d125 || error "mkdir failed"
6900         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
6901         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
6902         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
6903 }
6904 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
6905
6906 test_126() { # bug 12829/13455
6907         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6908         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
6909         $GSS && skip "must run as gss disabled" && return
6910
6911         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
6912         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
6913         rm -f $DIR/$tfile
6914         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
6915 }
6916 run_test 126 "check that the fsgid provided by the client is taken into account"
6917
6918 test_127a() { # bug 15521
6919         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
6920         $LCTL set_param osc.*.stats=0
6921         FSIZE=$((2048 * 1024))
6922         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6923         cancel_lru_locks osc
6924         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
6925
6926         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
6927         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
6928                 echo "got $COUNT $NAME"
6929                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
6930                 eval $NAME=$COUNT || error "Wrong proc format"
6931
6932                 case $NAME in
6933                         read_bytes|write_bytes)
6934                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
6935                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
6936                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
6937                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
6938                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
6939                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
6940                                 error "sumsquare is too small: $SUMSQ"
6941                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
6942                                 error "sumsquare is too big: $SUMSQ"
6943                         ;;
6944                         *) ;;
6945                 esac
6946         done < $DIR/${tfile}.tmp
6947
6948         #check that we actually got some stats
6949         [ "$read_bytes" ] || error "Missing read_bytes stats"
6950         [ "$write_bytes" ] || error "Missing write_bytes stats"
6951         [ "$read_bytes" != 0 ] || error "no read done"
6952         [ "$write_bytes" != 0 ] || error "no write done"
6953 }
6954 run_test 127a "verify the client stats are sane"
6955
6956 test_127b() { # bug LU-333
6957         $LCTL set_param llite.*.stats=0
6958         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
6959         # perform 2 reads and writes so MAX is different from SUM.
6960         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6961         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6962         cancel_lru_locks osc
6963         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
6964         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
6965
6966         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
6967         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
6968                 echo "got $COUNT $NAME"
6969                 eval $NAME=$COUNT || error "Wrong proc format"
6970
6971         case $NAME in
6972                 read_bytes)
6973                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
6974                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
6975                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
6976                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
6977                         ;;
6978                 write_bytes)
6979                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
6980                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
6981                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
6982                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
6983                         ;;
6984                         *) ;;
6985                 esac
6986         done < $TMP/${tfile}.tmp
6987
6988         #check that we actually got some stats
6989         [ "$read_bytes" ] || error "Missing read_bytes stats"
6990         [ "$write_bytes" ] || error "Missing write_bytes stats"
6991         [ "$read_bytes" != 0 ] || error "no read done"
6992         [ "$write_bytes" != 0 ] || error "no write done"
6993 }
6994 run_test 127b "verify the llite client stats are sane"
6995
6996 test_128() { # bug 15212
6997         touch $DIR/$tfile
6998         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
6999                 find $DIR/$tfile
7000                 find $DIR/$tfile
7001         EOF
7002
7003         result=$(grep error $TMP/$tfile.log)
7004         rm -f $DIR/$tfile
7005         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7006 }
7007 run_test 128 "interactive lfs for 2 consecutive find's"
7008
7009 set_dir_limits () {
7010         local mntdev
7011         local canondev
7012         local node
7013
7014         local LDPROC=/proc/fs/ldiskfs
7015
7016         for facet in $(get_facets MDS); do
7017                 canondev=$(ldiskfs_canon *.$(convert_facet2label $facet).mntdev $facet)
7018                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" || LDPROC=/sys/fs/ldiskfs
7019                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7020         done
7021 }
7022 test_129() {
7023         if [ "$(facet_type_fstype MDS)" != ldiskfs ]; then
7024                 skip "Only applicable to ldiskfs-based MDTs"
7025                 return
7026         fi
7027         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7028
7029         EFBIG=27
7030         MAX=16384
7031
7032         set_dir_limits $MAX
7033
7034         mkdir -p $DIR/$tdir
7035
7036         I=0
7037         J=0
7038         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7039                 $MULTIOP $DIR/$tdir/$J Oc
7040                 rc=$?
7041                 if [ $rc -eq $EFBIG ]; then
7042                         set_dir_limits 0
7043                         echo "return code $rc received as expected"
7044                         return 0
7045                 elif [ $rc -ne 0 ]; then
7046                         set_dir_limits 0
7047                         error_exit "return code $rc received instead of expected $EFBIG"
7048                 fi
7049                 J=$((J+1))
7050                 I=$(stat -c%s "$DIR/$tdir")
7051         done
7052
7053         set_dir_limits 0
7054         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7055 }
7056 run_test 129 "test directory size limit ========================"
7057
7058 OLDIFS="$IFS"
7059 cleanup_130() {
7060         trap 0
7061         IFS="$OLDIFS"
7062 }
7063
7064 test_130a() {
7065         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7066         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7067                 return
7068
7069         trap cleanup_130 EXIT RETURN
7070
7071         local fm_file=$DIR/$tfile
7072         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7073         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7074                 error "dd failed for $fm_file"
7075
7076         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7077         filefrag -ves $fm_file
7078         RC=$?
7079         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7080                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7081         [ $RC != 0 ] && error "filefrag $fm_file failed"
7082
7083         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7084                       grep -v "ext:" | grep -v "found")
7085         lun=$($GETSTRIPE -i $fm_file)
7086
7087         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7088         IFS=$'\n'
7089         tot_len=0
7090         for line in $filefrag_op
7091         do
7092                 frag_lun=`echo $line | cut -d: -f5`
7093                 ext_len=`echo $line | cut -d: -f4`
7094                 if (( $frag_lun != $lun )); then
7095                         cleanup_130
7096                         error "FIEMAP on 1-stripe file($fm_file) failed"
7097                         return
7098                 fi
7099                 (( tot_len += ext_len ))
7100         done
7101
7102         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7103                 cleanup_130
7104                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7105                 return
7106         fi
7107
7108         cleanup_130
7109
7110         echo "FIEMAP on single striped file succeeded"
7111 }
7112 run_test 130a "FIEMAP (1-stripe file)"
7113
7114 test_130b() {
7115         [ "$OSTCOUNT" -lt "2" ] &&
7116                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7117
7118         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7119         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7120                 return
7121
7122         trap cleanup_130 EXIT RETURN
7123
7124         local fm_file=$DIR/$tfile
7125         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7126         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7127                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7128
7129         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7130                 error "dd failed on $fm_file"
7131
7132         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7133         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7134                       grep -v "ext:" | grep -v "found")
7135
7136         last_lun=$(echo $filefrag_op | cut -d: -f5)
7137
7138         IFS=$'\n'
7139         tot_len=0
7140         num_luns=1
7141         for line in $filefrag_op
7142         do
7143                 frag_lun=`echo $line | cut -d: -f5`
7144                 ext_len=`echo $line | cut -d: -f4`
7145                 if (( $frag_lun != $last_lun )); then
7146                         if (( tot_len != 1024 )); then
7147                                 cleanup_130
7148                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7149                                 return
7150                         else
7151                                 (( num_luns += 1 ))
7152                                 tot_len=0
7153                         fi
7154                 fi
7155                 (( tot_len += ext_len ))
7156                 last_lun=$frag_lun
7157         done
7158         if (( num_luns != 2 || tot_len != 1024 )); then
7159                 cleanup_130
7160                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7161                 return
7162         fi
7163
7164         cleanup_130
7165
7166         echo "FIEMAP on 2-stripe file succeeded"
7167 }
7168 run_test 130b "FIEMAP (2-stripe file)"
7169
7170 test_130c() {
7171         [ "$OSTCOUNT" -lt "2" ] &&
7172                 skip_env "skipping FIEMAP on 2-stripe file" && return
7173
7174         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7175         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7176                 return
7177
7178         trap cleanup_130 EXIT RETURN
7179
7180         local fm_file=$DIR/$tfile
7181         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7182         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7183                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7184
7185         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7186
7187         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7188         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7189
7190         last_lun=`echo $filefrag_op | cut -d: -f5`
7191
7192         IFS=$'\n'
7193         tot_len=0
7194         num_luns=1
7195         for line in $filefrag_op
7196         do
7197                 frag_lun=`echo $line | cut -d: -f5`
7198                 ext_len=`echo $line | cut -d: -f4`
7199                 if (( $frag_lun != $last_lun )); then
7200                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7201                         if (( logical != 512 )); then
7202                                 cleanup_130
7203                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7204                                 return
7205                         fi
7206                         if (( tot_len != 512 )); then
7207                                 cleanup_130
7208                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7209                                 return
7210                         else
7211                                 (( num_luns += 1 ))
7212                                 tot_len=0
7213                         fi
7214                 fi
7215                 (( tot_len += ext_len ))
7216                 last_lun=$frag_lun
7217         done
7218         if (( num_luns != 2 || tot_len != 512 )); then
7219                 cleanup_130
7220                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7221                 return
7222         fi
7223
7224         cleanup_130
7225
7226         echo "FIEMAP on 2-stripe file with hole succeeded"
7227 }
7228 run_test 130c "FIEMAP (2-stripe file with hole)"
7229
7230 test_130d() {
7231         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7232
7233         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7234         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7235
7236         trap cleanup_130 EXIT RETURN
7237
7238         local fm_file=$DIR/$tfile
7239         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7240         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7241                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7242         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7243
7244         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7245         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7246
7247         last_lun=`echo $filefrag_op | cut -d: -f5`
7248
7249         IFS=$'\n'
7250         tot_len=0
7251         num_luns=1
7252         for line in $filefrag_op
7253         do
7254                 frag_lun=`echo $line | cut -d: -f5`
7255                 ext_len=`echo $line | cut -d: -f4`
7256                 if (( $frag_lun != $last_lun )); then
7257                         if (( tot_len != 1024 )); then
7258                                 cleanup_130
7259                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7260                                 return
7261                         else
7262                                 (( num_luns += 1 ))
7263                                 tot_len=0
7264                         fi
7265                 fi
7266                 (( tot_len += ext_len ))
7267                 last_lun=$frag_lun
7268         done
7269         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7270                 cleanup_130
7271                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7272                 return
7273         fi
7274
7275         cleanup_130
7276
7277         echo "FIEMAP on N-stripe file succeeded"
7278 }
7279 run_test 130d "FIEMAP (N-stripe file)"
7280
7281 test_130e() {
7282         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7283
7284         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7285         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7286
7287         trap cleanup_130 EXIT RETURN
7288
7289         local fm_file=$DIR/$tfile
7290         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7291         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7292                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7293
7294         NUM_BLKS=512
7295         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7296         for ((i = 0; i < $NUM_BLKS; i++))
7297         do
7298                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7299         done
7300
7301         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7302         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7303
7304         last_lun=`echo $filefrag_op | cut -d: -f5`
7305
7306         IFS=$'\n'
7307         tot_len=0
7308         num_luns=1
7309         for line in $filefrag_op
7310         do
7311                 frag_lun=`echo $line | cut -d: -f5`
7312                 ext_len=`echo $line | cut -d: -f4`
7313                 if (( $frag_lun != $last_lun )); then
7314                         if (( tot_len != $EXPECTED_LEN )); then
7315                                 cleanup_130
7316                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7317                                 return
7318                         else
7319                                 (( num_luns += 1 ))
7320                                 tot_len=0
7321                         fi
7322                 fi
7323                 (( tot_len += ext_len ))
7324                 last_lun=$frag_lun
7325         done
7326         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7327                 cleanup_130
7328                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7329                 return
7330         fi
7331
7332         cleanup_130
7333
7334         echo "FIEMAP with continuation calls succeeded"
7335 }
7336 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7337
7338 # Test for writev/readv
7339 test_131a() {
7340         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7341         error "writev test failed"
7342         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7343         error "readv failed"
7344         rm -f $DIR/$tfile
7345 }
7346 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7347
7348 test_131b() {
7349         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7350         error "append writev test failed"
7351         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7352         error "append writev test failed"
7353         rm -f $DIR/$tfile
7354 }
7355 run_test 131b "test append writev"
7356
7357 test_131c() {
7358         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7359         error "NOT PASS"
7360 }
7361 run_test 131c "test read/write on file w/o objects"
7362
7363 test_131d() {
7364         rwv -f $DIR/$tfile -w -n 1 1572864
7365         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7366         if [ "$NOB" != 1572864 ]; then
7367                 error "Short read filed: read $NOB bytes instead of 1572864"
7368         fi
7369         rm -f $DIR/$tfile
7370 }
7371 run_test 131d "test short read"
7372
7373 test_131e() {
7374         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7375         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7376         error "read hitting hole failed"
7377         rm -f $DIR/$tfile
7378 }
7379 run_test 131e "test read hitting hole"
7380
7381 get_ost_param() {
7382         local token=$1
7383         local gl_sum=0
7384         for node in $(osts_nodes); do
7385                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7386                 [ x$gl = x"" ] && gl=0
7387                 gl_sum=$((gl_sum + gl))
7388         done
7389         echo $gl
7390 }
7391
7392 som_mode_switch() {
7393         local som=$1
7394         local gl1=$2
7395         local gl2=$3
7396
7397         if [ x$som = x"enabled" ]; then
7398                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7399                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7400                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7401         else
7402                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7403                 MOUNTOPT="$MOUNTOPT,som_preview"
7404                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7405         fi
7406
7407         # do remount to make new mount-conf parameters actual
7408         echo remounting...
7409         sync
7410         stopall
7411         setupall
7412 }
7413
7414 test_132() { #1028, SOM
7415         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7416         local num=$(get_mds_dir $DIR)
7417         local mymds=mds${num}
7418         local MOUNTOPT_SAVE=$MOUNTOPT
7419
7420         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7421         cancel_lru_locks osc
7422
7423         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7424
7425         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7426         stat $DIR/$tfile >/dev/null
7427         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7428         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7429         rm $DIR/$tfile
7430         som_mode_switch $som1 $gl1 $gl2
7431
7432         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7433         cancel_lru_locks osc
7434
7435         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7436         if [ $som1 == $som2 ]; then
7437             error "som is still "$som2
7438             if [ x$som2 = x"enabled" ]; then
7439                 som2="disabled"
7440             else
7441                 som2="enabled"
7442             fi
7443         fi
7444
7445         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7446         stat $DIR/$tfile >/dev/null
7447         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7448         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7449         som_mode_switch $som2 $gl1 $gl2
7450         MOUNTOPT=$MOUNTOPT_SAVE
7451 }
7452 run_test 132 "som avoids glimpse rpc"
7453
7454 check_stats() {
7455         local res
7456         local count
7457         case $1 in
7458         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7459                  ;;
7460         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7461                  ;;
7462         *) error "Wrong argument $1" ;;
7463         esac
7464         echo $res
7465         count=`echo $res | awk '{print $2}'`
7466         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7467         # if the argument $3 is zero, it means any stat increment is ok.
7468         if [ $3 -gt 0 ] ; then
7469                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7470         fi
7471 }
7472
7473 test_133a() {
7474         remote_ost_nodsh && skip "remote OST with nodsh" && return
7475         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7476
7477         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7478                 { skip "MDS doesn't support rename stats"; return; }
7479         local testdir=$DIR/${tdir}/stats_testdir
7480         mkdir -p $DIR/${tdir}
7481
7482         # clear stats.
7483         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7484         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7485
7486         # verify mdt stats first.
7487         mkdir ${testdir} || error "mkdir failed"
7488         check_stats $SINGLEMDS "mkdir" 1
7489         touch ${testdir}/${tfile} || "touch failed"
7490         check_stats $SINGLEMDS "open" 1
7491         check_stats $SINGLEMDS "close" 1
7492         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7493         check_stats $SINGLEMDS "mknod" 1
7494         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7495         check_stats $SINGLEMDS "unlink" 1
7496         rm -f ${testdir}/${tfile} || error "file remove failed"
7497         check_stats $SINGLEMDS "unlink" 2
7498
7499         # remove working dir and check mdt stats again.
7500         rmdir ${testdir} || error "rmdir failed"
7501         check_stats $SINGLEMDS "rmdir" 1
7502
7503         local testdir1=$DIR/${tdir}/stats_testdir1
7504         mkdir -p ${testdir}
7505         mkdir -p ${testdir1}
7506         touch ${testdir1}/test1
7507         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7508         check_stats $SINGLEMDS "crossdir_rename" 1
7509
7510         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
7511         check_stats $SINGLEMDS "samedir_rename" 1
7512
7513         rm -rf $DIR/${tdir}
7514 }
7515 run_test 133a "Verifying MDT stats ========================================"
7516
7517 test_133b() {
7518         remote_ost_nodsh && skip "remote OST with nodsh" && return
7519         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7520         local testdir=$DIR/${tdir}/stats_testdir
7521         mkdir -p ${testdir} || error "mkdir failed"
7522         touch ${testdir}/${tfile} || "touch failed"
7523         cancel_lru_locks mdc
7524
7525         # clear stats.
7526         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7527         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7528
7529         # extra mdt stats verification.
7530         chmod 444 ${testdir}/${tfile} || error "chmod failed"
7531         check_stats $SINGLEMDS "setattr" 1
7532         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7533         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
7534         then            # LU-1740
7535                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
7536                 check_stats $SINGLEMDS "getattr" 1
7537         fi
7538         $LFS df || error "lfs failed"
7539         check_stats $SINGLEMDS "statfs" 1
7540
7541         rm -rf $DIR/${tdir}
7542 }
7543 run_test 133b "Verifying extra MDT stats =================================="
7544
7545 test_133c() {
7546         remote_ost_nodsh && skip "remote OST with nodsh" && return
7547         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7548         local testdir=$DIR/${tdir}/stats_testdir
7549         mkdir -p ${testdir} || error "mkdir failed"
7550
7551         # verify obdfilter stats.
7552         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
7553         sync
7554         cancel_lru_locks osc
7555         wait_delete_completed
7556
7557         # clear stats.
7558         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7559         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7560
7561         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
7562         sync
7563         cancel_lru_locks osc
7564         check_stats ost "write" 1
7565
7566         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
7567         check_stats ost "read" 1
7568
7569         > ${testdir}/${tfile} || error "truncate failed"
7570         check_stats ost "punch" 1
7571
7572         rm -f ${testdir}/${tfile} || error "file remove failed"
7573         wait_delete_completed
7574         check_stats ost "destroy" 1
7575
7576         rm -rf $DIR/${tdir}
7577 }
7578 run_test 133c "Verifying OST stats ========================================"
7579
7580 order_2() {
7581     local value=$1
7582     local orig=$value
7583     local order=1
7584
7585     while [ $value -ge 2 ]; do
7586         order=$((order*2))
7587         value=$((value/2))
7588     done
7589
7590     if [ $orig -gt $order ]; then
7591         order=$((order*2))
7592     fi
7593     echo $order
7594 }
7595
7596 size_in_KMGT() {
7597     local value=$1
7598     local size=('K' 'M' 'G' 'T');
7599     local i=0
7600     local size_string=$value
7601
7602     while [ $value -ge 1024 ]; do
7603         if [ $i -gt 3 ]; then
7604             #T is the biggest unit we get here, if that is bigger,
7605             #just return XXXT
7606             size_string=${value}T
7607             break
7608         fi
7609         value=$((value >> 10))
7610         if [ $value -lt 1024 ]; then
7611             size_string=${value}${size[$i]}
7612             break
7613         fi
7614         i=$((i + 1))
7615     done
7616
7617     echo $size_string
7618 }
7619
7620 get_rename_size() {
7621     local size=$1
7622     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
7623                    awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
7624     echo $sample
7625 }
7626
7627 test_133d() {
7628     remote_ost_nodsh && skip "remote OST with nodsh" && return
7629     remote_mds_nodsh && skip "remote MDS with nodsh" && return
7630     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7631         { skip "MDS doesn't support rename stats"; return; }
7632
7633     local testdir1=$DIR/${tdir}/stats_testdir1
7634     local testdir2=$DIR/${tdir}/stats_testdir2
7635
7636     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7637
7638     mkdir -p ${testdir1} || error "mkdir failed"
7639     mkdir -p ${testdir2} || error "mkdir failed"
7640
7641     createmany -o $testdir1/test 512 || error "createmany failed"
7642
7643         # check samedir rename size
7644         mv ${testdir1}/test0 ${testdir1}/test_0
7645
7646         local testdir1_size=$(ls -l $DIR/${tdir} |
7647                 awk '/stats_testdir1/ {print $5}')
7648         local testdir2_size=$(ls -l $DIR/${tdir} |
7649                 awk '/stats_testdir2/ {print $5}')
7650
7651         testdir1_size=$(order_2 $testdir1_size)
7652         testdir2_size=$(order_2 $testdir2_size)
7653
7654         testdir1_size=$(size_in_KMGT $testdir1_size)
7655         testdir2_size=$(size_in_KMGT $testdir2_size)
7656
7657         echo "source rename dir size: ${testdir1_size}"
7658         echo "target rename dir size: ${testdir2_size}"
7659
7660     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
7661     eval $cmd || error "$cmd failed"
7662     local samedir=$($cmd | grep 'same_dir')
7663     local same_sample=$(get_rename_size $testdir1_size)
7664     [ -z "$samedir" ] && error "samedir_rename_size count error"
7665     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
7666     echo "Check same dir rename stats success"
7667
7668     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7669
7670     # check crossdir rename size
7671     mv ${testdir1}/test_0 ${testdir2}/test_0
7672
7673         testdir1_size=$(ls -l $DIR/${tdir} |
7674                 awk '/stats_testdir1/ {print $5}')
7675         testdir2_size=$(ls -l $DIR/${tdir} |
7676                 awk '/stats_testdir2/ {print $5}')
7677
7678         testdir1_size=$(order_2 $testdir1_size)
7679         testdir2_size=$(order_2 $testdir2_size)
7680
7681         testdir1_size=$(size_in_KMGT $testdir1_size)
7682         testdir2_size=$(size_in_KMGT $testdir2_size)
7683
7684         echo "source rename dir size: ${testdir1_size}"
7685         echo "target rename dir size: ${testdir2_size}"
7686
7687     eval $cmd || error "$cmd failed"
7688     local crossdir=$($cmd | grep 'crossdir')
7689     local src_sample=$(get_rename_size $testdir1_size)
7690     local tgt_sample=$(get_rename_size $testdir2_size)
7691     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
7692     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
7693     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
7694     echo "Check cross dir rename stats success"
7695     rm -rf $DIR/${tdir}
7696 }
7697 run_test 133d "Verifying rename_stats ========================================"
7698
7699 test_140() { #bug-17379
7700         mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
7701         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
7702         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
7703
7704         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
7705         local i=0
7706         while i=`expr $i + 1`; do
7707                 mkdir -p $i || error "Creating dir $i"
7708                 cd $i || error "Changing to $i"
7709                 ln -s ../stat stat || error "Creating stat symlink"
7710                 # Read the symlink until ELOOP present,
7711                 # not LBUGing the system is considered success,
7712                 # we didn't overrun the stack.
7713                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
7714                 [ $ret -ne 0 ] && {
7715                         if [ $ret -eq 40 ]; then
7716                                 break  # -ELOOP
7717                         else
7718                                 error "Open stat symlink"
7719                                 return
7720                         fi
7721                 }
7722         done
7723         i=`expr $i - 1`
7724         echo "The symlink depth = $i"
7725         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 ] || error "Invalid symlink depth"
7726 }
7727 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
7728
7729 test_150() {
7730         local TF="$TMP/$tfile"
7731
7732         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7733         cp $TF $DIR/$tfile
7734         cancel_lru_locks osc
7735         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
7736         remount_client $MOUNT
7737         df -P $MOUNT
7738         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
7739
7740         $TRUNCATE $TF 6000
7741         $TRUNCATE $DIR/$tfile 6000
7742         cancel_lru_locks osc
7743         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
7744
7745         echo "12345" >>$TF
7746         echo "12345" >>$DIR/$tfile
7747         cancel_lru_locks osc
7748         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
7749
7750         echo "12345" >>$TF
7751         echo "12345" >>$DIR/$tfile
7752         cancel_lru_locks osc
7753         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
7754
7755         rm -f $TF
7756         true
7757 }
7758 run_test 150 "truncate/append tests"
7759
7760 function roc_hit() {
7761         local list=$(comma_list $(osts_nodes))
7762
7763         echo $(get_obdfilter_param $list '' stats |
7764                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
7765 }
7766
7767 function set_cache() {
7768         local on=1
7769
7770         if [ "$2" == "off" ]; then
7771                 on=0;
7772         fi
7773         local list=$(comma_list $(osts_nodes))
7774         set_obdfilter_param $list '' $1_cache_enable $on
7775
7776         cancel_lru_locks osc
7777 }
7778
7779 test_151() {
7780         remote_ost_nodsh && skip "remote OST with nodsh" && return
7781
7782         local CPAGES=3
7783         local list=$(comma_list $(osts_nodes))
7784
7785         # check whether obdfilter is cache capable at all
7786         if ! get_obdfilter_param $list '' read_cache_enable >/dev/null; then
7787                 echo "not cache-capable obdfilter"
7788                 return 0
7789         fi
7790
7791         # check cache is enabled on all obdfilters
7792         if get_obdfilter_param $list '' read_cache_enable | grep 0; then
7793                 echo "oss cache is disabled"
7794                 return 0
7795         fi
7796
7797         set_obdfilter_param $list '' writethrough_cache_enable 1
7798
7799         # pages should be in the case right after write
7800         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
7801         local BEFORE=`roc_hit`
7802         cancel_lru_locks osc
7803         cat $DIR/$tfile >/dev/null
7804         local AFTER=`roc_hit`
7805         if ! let "AFTER - BEFORE == CPAGES"; then
7806                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7807         fi
7808
7809         # the following read invalidates the cache
7810         cancel_lru_locks osc
7811         set_obdfilter_param $list '' read_cache_enable 0
7812         cat $DIR/$tfile >/dev/null
7813
7814         # now data shouldn't be found in the cache
7815         BEFORE=`roc_hit`
7816         cancel_lru_locks osc
7817         cat $DIR/$tfile >/dev/null
7818         AFTER=`roc_hit`
7819         if let "AFTER - BEFORE != 0"; then
7820                 error "IN CACHE: before: $BEFORE, after: $AFTER"
7821         fi
7822
7823         set_obdfilter_param $list '' read_cache_enable 1
7824         rm -f $DIR/$tfile
7825 }
7826 run_test 151 "test cache on oss and controls ==============================="
7827
7828 test_152() {
7829         local TF="$TMP/$tfile"
7830
7831         # simulate ENOMEM during write
7832 #define OBD_FAIL_OST_NOMEM      0x226
7833         lctl set_param fail_loc=0x80000226
7834         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7835         cp $TF $DIR/$tfile
7836         sync || error "sync failed"
7837         lctl set_param fail_loc=0
7838
7839         # discard client's cache
7840         cancel_lru_locks osc
7841
7842         # simulate ENOMEM during read
7843         lctl set_param fail_loc=0x80000226
7844         cmp $TF $DIR/$tfile || error "cmp failed"
7845         lctl set_param fail_loc=0
7846
7847         rm -f $TF
7848 }
7849 run_test 152 "test read/write with enomem ============================"
7850
7851 test_153() {
7852         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
7853 }
7854 run_test 153 "test if fdatasync does not crash ======================="
7855
7856 test_154() {
7857         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
7858                 { skip "Need MDS version at least 2.2.51"; return 0; }
7859
7860         cp /etc/hosts $DIR/$tfile
7861
7862         fid=$($LFS path2fid $DIR/$tfile)
7863         rc=$?
7864         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7865
7866         ffid=$DIR/.lustre/fid/$fid
7867
7868         echo "stat fid $fid"
7869         stat $ffid > /dev/null || error "stat $ffid failed."
7870         echo "touch fid $fid"
7871         touch $ffid || error "touch $ffid failed."
7872         echo "write to fid $fid"
7873         cat /etc/hosts > $ffid || error "write $ffid failed."
7874         echo "read fid $fid"
7875         diff /etc/hosts $ffid || error "read $ffid failed."
7876         echo "append write to fid $fid"
7877         cat /etc/hosts >> $ffid || error "append write $ffid failed."
7878         echo "rename fid $fid"
7879         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
7880         touch $DIR/$tfile.1
7881         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
7882         rm -f $DIR/$tfile.1
7883         echo "truncate fid $fid"
7884         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
7885         echo "link fid $fid"
7886         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
7887         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
7888                 echo "setfacl fid $fid"
7889                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
7890                 echo "getfacl fid $fid"
7891                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
7892         fi
7893         echo "unlink fid $fid"
7894         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
7895         echo "mknod fid $fid"
7896         mknod $ffid c 1 3 && error "mknod $ffid should fail."
7897
7898         fid=[0xf00000400:0x1:0x0]
7899         ffid=$DIR/.lustre/fid/$fid
7900
7901         echo "stat non-exist fid $fid"
7902         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
7903         echo "write to non-exist fid $fid"
7904         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
7905         echo "link new fid $fid"
7906         ln $DIR/$tfile $ffid && error "link $ffid should fail."
7907
7908         mkdir -p $DIR/$tdir
7909         touch $DIR/$tdir/$tfile
7910         fid=$($LFS path2fid $DIR/$tdir)
7911         rc=$?
7912         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7913
7914         ffid=$DIR/.lustre/fid/$fid
7915
7916         echo "ls $fid"
7917         ls $ffid > /dev/null || error "ls $ffid failed."
7918         echo "touch $fid/$tfile.1"
7919         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
7920
7921         echo "touch $DIR/.lustre/fid/$tfile"
7922         touch $DIR/.lustre/fid/$tfile && \
7923                 error "touch $DIR/.lustre/fid/$tfile should fail."
7924
7925         echo "setxattr to $DIR/.lustre/fid"
7926         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
7927                 error "setxattr should fail."
7928
7929         echo "listxattr for $DIR/.lustre/fid"
7930         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
7931                 error "listxattr should fail."
7932
7933         echo "delxattr from $DIR/.lustre/fid"
7934         setfattr -x trusted.name1 $DIR/.lustre/fid &&
7935                 error "delxattr should fail."
7936
7937         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
7938         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
7939                 error "touch invalid fid should fail."
7940
7941         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
7942         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
7943                 error "touch non-normal fid should fail."
7944
7945         echo "rename $tdir to $DIR/.lustre/fid"
7946         mrename $DIR/$tdir $DIR/.lustre/fid &&
7947                 error "rename to $DIR/.lustre/fid should fail."
7948
7949         echo "rename .lustre to itself"
7950         fid=$($LFS path2fid $DIR)
7951         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
7952                 error "rename .lustre to itself should fail."
7953
7954         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
7955         fid=$($LFS path2fid $DIR/$tfile-2)
7956         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
7957         cp /etc/passwd $DIR/.lustre/fid/$fid &&
7958                 error "create lov data thru .lustre should fail."
7959         echo "cp /etc/passwd $DIR/$tfile-2"
7960         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
7961         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
7962         diff /etc/passwd $DIR/.lustre/fid/$fid ||
7963                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
7964
7965         echo "Open-by-FID succeeded"
7966 }
7967 run_test 154 "Open-by-FID"
7968
7969 test_155_small_load() {
7970     local temp=$TMP/$tfile
7971     local file=$DIR/$tfile
7972
7973     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
7974         error "dd of=$temp bs=6096 count=1 failed"
7975     cp $temp $file
7976     cancel_lru_locks osc
7977     cmp $temp $file || error "$temp $file differ"
7978
7979     $TRUNCATE $temp 6000
7980     $TRUNCATE $file 6000
7981     cmp $temp $file || error "$temp $file differ (truncate1)"
7982
7983     echo "12345" >>$temp
7984     echo "12345" >>$file
7985     cmp $temp $file || error "$temp $file differ (append1)"
7986
7987     echo "12345" >>$temp
7988     echo "12345" >>$file
7989     cmp $temp $file || error "$temp $file differ (append2)"
7990
7991     rm -f $temp $file
7992     true
7993 }
7994
7995 test_155_big_load() {
7996     remote_ost_nodsh && skip "remote OST with nodsh" && return
7997     local temp=$TMP/$tfile
7998     local file=$DIR/$tfile
7999
8000     free_min_max
8001     local cache_size=$(do_facet ost$((MAXI+1)) \
8002         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8003     local large_file_size=$((cache_size * 2))
8004
8005     echo "OSS cache size: $cache_size KB"
8006     echo "Large file size: $large_file_size KB"
8007
8008     [ $MAXV -le $large_file_size ] && \
8009         skip_env "max available OST size needs > $large_file_size KB" && \
8010         return 0
8011
8012     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8013
8014     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8015         error "dd of=$temp bs=$large_file_size count=1k failed"
8016     cp $temp $file
8017     ls -lh $temp $file
8018     cancel_lru_locks osc
8019     cmp $temp $file || error "$temp $file differ"
8020
8021     rm -f $temp $file
8022     true
8023 }
8024
8025 test_155a() {
8026     set_cache read on
8027     set_cache writethrough on
8028     test_155_small_load
8029 }
8030 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8031
8032 test_155b() {
8033     set_cache read on
8034     set_cache writethrough off
8035     test_155_small_load
8036 }
8037 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8038
8039 test_155c() {
8040     set_cache read off
8041     set_cache writethrough on
8042     test_155_small_load
8043 }
8044 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8045
8046 test_155d() {
8047     set_cache read off
8048     set_cache writethrough off
8049     test_155_small_load
8050 }
8051 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8052
8053 test_155e() {
8054     set_cache read on
8055     set_cache writethrough on
8056     test_155_big_load
8057 }
8058 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8059
8060 test_155f() {
8061     set_cache read on
8062     set_cache writethrough off
8063     test_155_big_load
8064 }
8065 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8066
8067 test_155g() {
8068     set_cache read off
8069     set_cache writethrough on
8070     test_155_big_load
8071 }
8072 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8073
8074 test_155h() {
8075     set_cache read off
8076     set_cache writethrough off
8077     test_155_big_load
8078 }
8079 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8080
8081 test_156() {
8082     local CPAGES=3
8083     local BEFORE
8084     local AFTER
8085     local file="$DIR/$tfile"
8086
8087     log "Turn on read and write cache"
8088     set_cache read on
8089     set_cache writethrough on
8090
8091     log "Write data and read it back."
8092     log "Read should be satisfied from the cache."
8093     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8094     BEFORE=`roc_hit`
8095     cancel_lru_locks osc
8096     cat $file >/dev/null
8097     AFTER=`roc_hit`
8098     if ! let "AFTER - BEFORE == CPAGES"; then
8099         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8100     else
8101         log "cache hits:: before: $BEFORE, after: $AFTER"
8102     fi
8103
8104     log "Read again; it should be satisfied from the cache."
8105     BEFORE=$AFTER
8106     cancel_lru_locks osc
8107     cat $file >/dev/null
8108     AFTER=`roc_hit`
8109     if ! let "AFTER - BEFORE == CPAGES"; then
8110         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8111     else
8112         log "cache hits:: before: $BEFORE, after: $AFTER"
8113     fi
8114
8115
8116     log "Turn off the read cache and turn on the write cache"
8117     set_cache read off
8118     set_cache writethrough on
8119
8120     log "Read again; it should be satisfied from the cache."
8121     BEFORE=`roc_hit`
8122     cancel_lru_locks osc
8123     cat $file >/dev/null
8124     AFTER=`roc_hit`
8125     if ! let "AFTER - BEFORE == CPAGES"; then
8126         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8127     else
8128         log "cache hits:: before: $BEFORE, after: $AFTER"
8129     fi
8130
8131     log "Read again; it should not be satisfied from the cache."
8132     BEFORE=$AFTER
8133     cancel_lru_locks osc
8134     cat $file >/dev/null
8135     AFTER=`roc_hit`
8136     if ! let "AFTER - BEFORE == 0"; then
8137         error "IN CACHE: before: $BEFORE, after: $AFTER"
8138     else
8139         log "cache hits:: before: $BEFORE, after: $AFTER"
8140     fi
8141
8142     log "Write data and read it back."
8143     log "Read should be satisfied from the cache."
8144     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8145     BEFORE=`roc_hit`
8146     cancel_lru_locks osc
8147     cat $file >/dev/null
8148     AFTER=`roc_hit`
8149     if ! let "AFTER - BEFORE == CPAGES"; then
8150         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8151     else
8152         log "cache hits:: before: $BEFORE, after: $AFTER"
8153     fi
8154
8155     log "Read again; it should not be satisfied from the cache."
8156     BEFORE=$AFTER
8157     cancel_lru_locks osc
8158     cat $file >/dev/null
8159     AFTER=`roc_hit`
8160     if ! let "AFTER - BEFORE == 0"; then
8161         error "IN CACHE: before: $BEFORE, after: $AFTER"
8162     else
8163         log "cache hits:: before: $BEFORE, after: $AFTER"
8164     fi
8165
8166
8167     log "Turn off read and write cache"
8168     set_cache read off
8169     set_cache writethrough off
8170
8171     log "Write data and read it back"
8172     log "It should not be satisfied from the cache."
8173     rm -f $file
8174     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8175     cancel_lru_locks osc
8176     BEFORE=`roc_hit`
8177     cat $file >/dev/null
8178     AFTER=`roc_hit`
8179     if ! let "AFTER - BEFORE == 0"; then
8180         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8181     else
8182         log "cache hits:: before: $BEFORE, after: $AFTER"
8183     fi
8184
8185
8186     log "Turn on the read cache and turn off the write cache"
8187     set_cache read on
8188     set_cache writethrough off
8189
8190     log "Write data and read it back"
8191     log "It should not be satisfied from the cache."
8192     rm -f $file
8193     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8194     BEFORE=`roc_hit`
8195     cancel_lru_locks osc
8196     cat $file >/dev/null
8197     AFTER=`roc_hit`
8198     if ! let "AFTER - BEFORE == 0"; then
8199         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8200     else
8201         log "cache hits:: before: $BEFORE, after: $AFTER"
8202     fi
8203
8204     log "Read again; it should be satisfied from the cache."
8205     BEFORE=`roc_hit`
8206     cancel_lru_locks osc
8207     cat $file >/dev/null
8208     AFTER=`roc_hit`
8209     if ! let "AFTER - BEFORE == CPAGES"; then
8210         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8211     else
8212         log "cache hits:: before: $BEFORE, after: $AFTER"
8213     fi
8214
8215     rm -f $file
8216 }
8217 run_test 156 "Verification of tunables ============================"
8218
8219 #Changelogs
8220 err17935 () {
8221     if [ $MDSCOUNT -gt 1 ]; then
8222         error_ignore 17935 $*
8223     else
8224         error $*
8225     fi
8226 }
8227
8228 changelog_chmask()
8229 {
8230     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8231            grep -c $1)
8232
8233     if [ $MASK -eq 1 ]; then
8234         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8235     else
8236         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8237     fi
8238 }
8239
8240 test_160() {
8241     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8242     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8243         { skip "Need MDS version at least 2.2.0"; return; }
8244     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8245     echo "Registered as changelog user $USER"
8246     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8247         grep -q $USER || error "User $USER not found in changelog_users"
8248
8249     # change something
8250     mkdir -p $DIR/$tdir/pics/2008/zachy
8251     touch $DIR/$tdir/pics/2008/zachy/timestamp
8252     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8253     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8254     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8255     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8256     rm $DIR/$tdir/pics/desktop.jpg
8257
8258     $LFS changelog $MDT0 | tail -5
8259
8260     echo "verifying changelog mask"
8261     changelog_chmask "MKDIR"
8262     changelog_chmask "CLOSE"
8263
8264     mkdir -p $DIR/$tdir/pics/zach/sofia
8265     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8266
8267     changelog_chmask "MKDIR"
8268     changelog_chmask "CLOSE"
8269
8270     mkdir -p $DIR/$tdir/pics/2008/sofia
8271     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8272
8273     $LFS changelog $MDT0
8274     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8275     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8276     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8277     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8278
8279     # verify contents
8280     echo "verifying target fid"
8281     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8282         tail -1 | awk '{print $6}')
8283     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8284     [ "$fidc" == "t=$fidf" ] || \
8285         err17935 "fid in changelog $fidc != file fid $fidf"
8286     echo "verifying parent fid"
8287     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8288         tail -1 | awk '{print $7}')
8289     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8290     [ "$fidc" == "p=$fidf" ] || \
8291         err17935 "pfid in changelog $fidc != dir fid $fidf"
8292
8293     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8294         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8295     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8296     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8297         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8298     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8299     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8300         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8301
8302     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8303         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8304     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8305     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8306     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8307         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8308
8309     echo "verifying user deregister"
8310     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8311     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8312         grep -q $USER && error "User $USER still found in changelog_users"
8313
8314     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8315         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8316     if [ $USERS -eq 0 ]; then
8317         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8318             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8319         touch $DIR/$tdir/chloe
8320         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8321             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8322         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8323         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8324     else
8325         echo "$USERS other changelog users; can't verify off"
8326     fi
8327 }
8328 run_test 160 "changelog sanity"
8329
8330 test_161() {
8331     mkdir -p $DIR/$tdir
8332     cp /etc/hosts $DIR/$tdir/$tfile
8333     mkdir $DIR/$tdir/foo1
8334     mkdir $DIR/$tdir/foo2
8335     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8336     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8337     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8338     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8339     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8340     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8341         $LFS fid2path $DIR $FID
8342         err17935 "bad link ea"
8343     fi
8344     # middle
8345     rm $DIR/$tdir/foo2/zachary
8346     # last
8347     rm $DIR/$tdir/foo2/thor
8348     # first
8349     rm $DIR/$tdir/$tfile
8350     # rename
8351     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8352     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8353         then
8354         $LFS fid2path $DIR $FID
8355         err17935 "bad link rename"
8356     fi
8357     rm $DIR/$tdir/foo2/maggie
8358
8359     # overflow the EA
8360     local longname=filename_avg_len_is_thirty_two_
8361     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8362         error "failed to hardlink many files"
8363     links=$($LFS fid2path $DIR $FID | wc -l)
8364     echo -n "${links}/1000 links in link EA"
8365     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8366     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8367         error "failed to unlink many hardlinks"
8368 }
8369 run_test 161 "link ea sanity"
8370
8371 check_path() {
8372     local expected=$1
8373     shift
8374     local fid=$2
8375
8376     local path=$(${LFS} fid2path $*)
8377     RC=$?
8378
8379     if [ $RC -ne 0 ]; then
8380         err17935 "path looked up of $expected failed. Error $RC"
8381         return $RC
8382     elif [ "${path}" != "${expected}" ]; then
8383         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8384         return 2
8385     fi
8386     echo "fid $fid resolves to path $path (expected $expected)"
8387 }
8388
8389 test_162() {
8390         # Make changes to filesystem
8391         mkdir -p $DIR/$tdir/d2
8392         touch $DIR/$tdir/d2/$tfile
8393         touch $DIR/$tdir/d2/x1
8394         touch $DIR/$tdir/d2/x2
8395         mkdir -p $DIR/$tdir/d2/a/b/c
8396         mkdir -p $DIR/$tdir/d2/p/q/r
8397         # regular file
8398         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8399         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8400
8401         # softlink
8402         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8403         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8404         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8405
8406         # softlink to wrong file
8407         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8408         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8409         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8410
8411         # hardlink
8412         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8413         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8414         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8415         # fid2path dir/fsname should both work
8416         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8417         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8418
8419         # hardlink count: check that there are 2 links
8420         # Doesnt work with CMD yet: 17935
8421         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8422                 err17935 "expected 2 links"
8423
8424         # hardlink indexing: remove the first link
8425         rm $DIR/$tdir/d2/p/q/r/hlink
8426         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8427
8428         return 0
8429 }
8430 run_test 162 "path lookup sanity"
8431
8432 test_163() {
8433         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8434         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
8435         copytool $FSNAME &
8436         sleep 1
8437         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
8438         # this proc file is temporary and linux-only
8439         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
8440          error "kernel->userspace send failed"
8441         kill -INT $!
8442 }
8443 run_test 163 "kernel <-> userspace comms"
8444
8445 test_169() {
8446         # do directio so as not to populate the page cache
8447         log "creating a 10 Mb file"
8448         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8449         log "starting reads"
8450         dd if=$DIR/$tfile of=/dev/null bs=4096 &
8451         log "truncating the file"
8452         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8453         log "killing dd"
8454         kill %+ || true # reads might have finished
8455         echo "wait until dd is finished"
8456         wait
8457         log "removing the temporary file"
8458         rm -rf $DIR/$tfile || error "tmp file removal failed"
8459 }
8460 run_test 169 "parallel read and truncate should not deadlock"
8461
8462 test_170() {
8463         $LCTL clear     # bug 18514
8464         $LCTL debug_daemon start $TMP/${tfile}_log_good
8465         touch $DIR/$tfile
8466         $LCTL debug_daemon stop
8467         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
8468                error "sed failed to read log_good"
8469
8470         $LCTL debug_daemon start $TMP/${tfile}_log_good
8471         rm -rf $DIR/$tfile
8472         $LCTL debug_daemon stop
8473
8474         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
8475                error "lctl df log_bad failed"
8476
8477         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8478         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8479
8480         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
8481         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
8482
8483         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
8484                 error "bad_line good_line1 good_line2 are empty"
8485
8486         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8487         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
8488         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8489
8490         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
8491         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8492         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8493
8494         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
8495                 error "bad_line_new good_line_new are empty"
8496
8497         local expected_good=$((good_line1 + good_line2*2))
8498
8499         rm -f $TMP/${tfile}*
8500         # LU-231, short malformed line may not be counted into bad lines
8501         if [ $bad_line -ne $bad_line_new ] &&
8502                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
8503                 error "expected $bad_line bad lines, but got $bad_line_new"
8504                 return 1
8505         fi
8506
8507         if [ $expected_good -ne $good_line_new ]; then
8508                 error "expected $expected_good good lines, but got $good_line_new"
8509                 return 2
8510         fi
8511         true
8512 }
8513 run_test 170 "test lctl df to handle corrupted log ====================="
8514
8515 test_171() { # bug20592
8516 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8517         $LCTL set_param fail_loc=0x50e
8518         $LCTL set_param fail_val=3000
8519         multiop_bg_pause $DIR/$tfile O_s || true
8520         local MULTIPID=$!
8521         kill -USR1 $MULTIPID
8522         # cause log dump
8523         sleep 3
8524         wait $MULTIPID
8525         if dmesg | grep "recursive fault"; then
8526                 error "caught a recursive fault"
8527         fi
8528         $LCTL set_param fail_loc=0
8529         true
8530 }
8531 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
8532
8533 # it would be good to share it with obdfilter-survey/libecho code
8534 setup_obdecho_osc () {
8535         local rc=0
8536         local ost_nid=$1
8537         local obdfilter_name=$2
8538         echo "Creating new osc for $obdfilter_name on $ost_nid"
8539         # make sure we can find loopback nid
8540         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
8541
8542         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
8543                            ${obdfilter_name}_osc_UUID || rc=2; }
8544         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
8545                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
8546         return $rc
8547 }
8548
8549 cleanup_obdecho_osc () {
8550         local obdfilter_name=$1
8551         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
8552         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
8553         return 0
8554 }
8555
8556 obdecho_create_test() {
8557         local OBD=$1
8558         local node=$2
8559         local rc=0
8560         local id
8561         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
8562         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
8563                            rc=2; }
8564         if [ $rc -eq 0 ]; then
8565             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
8566             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
8567         fi
8568         echo "New object id is $id"
8569         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
8570                            rc=4; }
8571         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
8572                                         "cleanup" || rc=5; }
8573         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
8574                                         "detach" || rc=6; }
8575         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
8576         return $rc
8577 }
8578
8579 test_180a() {
8580         remote_ost_nodsh && skip "remote OST with nodsh" && return
8581         local rc=0
8582         local rmmod_local=0
8583
8584         if ! module_loaded obdecho; then
8585             load_module obdecho/obdecho
8586             rmmod_local=1
8587         fi
8588
8589         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
8590         local host=$(lctl get_param -n osc.$osc.import |
8591                              awk '/current_connection:/ {print $2}' )
8592         local target=$(lctl get_param -n osc.$osc.import |
8593                              awk '/target:/ {print $2}' )
8594         target=${target%_UUID}
8595
8596         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
8597         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
8598         [[ -n $target ]] && cleanup_obdecho_osc $target
8599         [ $rmmod_local -eq 1 ] && rmmod obdecho
8600         return $rc
8601 }
8602 run_test 180a "test obdecho on osc"
8603
8604 test_180b() {
8605         remote_ost_nodsh && skip "remote OST with nodsh" && return
8606         local rc=0
8607         local rmmod_remote=0
8608
8609         do_facet ost1 "lsmod | grep -q obdecho || "                      \
8610                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
8611                       "modprobe obdecho; }" && rmmod_remote=1
8612         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
8613         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
8614         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
8615         return $rc
8616 }
8617 run_test 180b "test obdecho directly on obdfilter"
8618
8619 test_181() { # bug 22177
8620         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8621         # create enough files to index the directory
8622         createmany -o $DIR/$tdir/foobar 4000
8623         # print attributes for debug purpose
8624         lsattr -d .
8625         # open dir
8626         multiop_bg_pause $DIR/$tdir D_Sc || return 1
8627         MULTIPID=$!
8628         # remove the files & current working dir
8629         unlinkmany $DIR/$tdir/foobar 4000
8630         rmdir $DIR/$tdir
8631         kill -USR1 $MULTIPID
8632         wait $MULTIPID
8633         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
8634         return 0
8635 }
8636 run_test 181 "Test open-unlinked dir ========================"
8637
8638 test_182() {
8639         # disable MDC RPC lock wouldn't crash client
8640         local fcount=1000
8641         local tcount=4
8642
8643         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8644 #define OBD_FAIL_MDC_RPCS_SEM           0x804
8645         $LCTL set_param fail_loc=0x804
8646
8647         for (( i=0; i < $tcount; i++ )) ; do
8648                 mkdir $DIR/$tdir/$i
8649                 createmany -o $DIR/$tdir/$i/f- $fcount &
8650         done
8651         wait
8652
8653         for (( i=0; i < $tcount; i++ )) ; do
8654                 unlinkmany $DIR/$tdir/$i/f- $fcount &
8655         done
8656         wait
8657
8658         rm -rf $DIR/$tdir
8659
8660         $LCTL set_param fail_loc=0
8661 }
8662 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
8663
8664 # OST pools tests
8665 check_file_in_pool()
8666 {
8667         local file=$1
8668         local pool=$2
8669         local tlist="$3"
8670         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
8671         for i in $res
8672         do
8673                 for t in $tlist ; do
8674                         [ "$i" -eq "$t" ] && continue 2
8675                 done
8676
8677                 echo "pool list: $tlist"
8678                 echo "striping: $res"
8679                 error_noexit "$file not allocated in $pool"
8680                 return 1
8681         done
8682         return 0
8683 }
8684
8685 pool_add() {
8686         echo "Creating new pool"
8687         local pool=$1
8688
8689         create_pool $FSNAME.$pool ||
8690                 { error_noexit "No pool created, result code $?"; return 1; }
8691         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
8692                 { error_noexit "$pool not in lfs pool_list"; return 2; }
8693 }
8694
8695 pool_add_targets() {
8696         echo "Adding targets to pool"
8697         local pool=$1
8698         local first=$2
8699         local last=$3
8700         local step=${4:-1}
8701
8702         local list=$(seq $first $step $last)
8703
8704         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
8705         do_facet mgs $LCTL pool_add \
8706                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
8707         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
8708                         | sort -u | tr '\n' ' ' " "$t" || { 
8709                 error_noexit "Add to pool failed"
8710                 return 1
8711         }
8712         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
8713         local addcount=$(((last - first) / step + 1))
8714         [ $lfscount -eq $addcount ] || {
8715                 error_noexit "lfs pool_list bad ost count" \
8716                                                 "$lfscount != $addcount"
8717                 return 2
8718         }
8719 }
8720
8721 pool_set_dir() {
8722         local pool=$1
8723         local tdir=$2
8724         echo "Setting pool on directory $tdir"
8725
8726         $SETSTRIPE -c 2 -p $pool $tdir && return 0
8727
8728         error_noexit "Cannot set pool $pool to $tdir"
8729         return 1
8730 }
8731
8732 pool_check_dir() {
8733         local pool=$1
8734         local tdir=$2
8735         echo "Checking pool on directory $tdir"
8736
8737         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
8738         [ "$res" = "$pool" ] && return 0
8739
8740         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
8741         return 1
8742 }
8743
8744 pool_dir_rel_path() {
8745         echo "Testing relative path works well"
8746         local pool=$1
8747         local tdir=$2
8748         local root=$3
8749
8750         mkdir -p $root/$tdir/$tdir
8751         cd $root/$tdir
8752         pool_set_dir $pool $tdir          || return 1
8753         pool_set_dir $pool ./$tdir        || return 2
8754         pool_set_dir $pool ../$tdir       || return 3
8755         pool_set_dir $pool ../$tdir/$tdir || return 4
8756         rm -rf $tdir; cd - > /dev/null
8757 }
8758
8759 pool_alloc_files() {
8760         echo "Checking files allocation from directory pool"
8761         local pool=$1
8762         local tdir=$2
8763         local count=$3
8764         local tlist="$4"
8765
8766         local failed=0
8767         for i in $(seq -w 1 $count)
8768         do
8769                 local file=$tdir/file-$i
8770                 touch $file
8771                 check_file_in_pool $file $pool "$tlist" || \
8772                         failed=$((failed + 1))
8773         done
8774         [ "$failed" = 0 ] && return 0
8775
8776         error_noexit "$failed files not allocated in $pool"
8777         return 1
8778 }
8779
8780 pool_create_files() {
8781         echo "Creating files in pool"
8782         local pool=$1
8783         local tdir=$2
8784         local count=$3
8785         local tlist="$4"
8786
8787         mkdir -p $tdir
8788         local failed=0
8789         for i in $(seq -w 1 $count)
8790         do
8791                 local file=$tdir/spoo-$i
8792                 $SETSTRIPE -p $pool $file
8793                 check_file_in_pool $file $pool "$tlist" || \
8794                         failed=$((failed + 1))
8795         done
8796         [ "$failed" = 0 ] && return 0
8797
8798         error_noexit "$failed files not allocated in $pool"
8799         return 1
8800 }
8801
8802 pool_lfs_df() {
8803         echo "Checking 'lfs df' output"
8804         local pool=$1
8805
8806         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
8807                         tr '\n' ' ')
8808         local res=$($LFS df --pool $FSNAME.$pool |
8809                         awk '{print $1}' |
8810                         grep "$FSNAME-OST" |
8811                         tr '\n' ' ')
8812         [ "$res" = "$t" ] && return 0
8813
8814         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
8815         return 1
8816 }
8817
8818 pool_file_rel_path() {
8819         echo "Creating files in a pool with relative pathname"
8820         local pool=$1
8821         local tdir=$2
8822
8823         mkdir -p $tdir ||
8824                 { error_noexit "unable to create $tdir"; return 1 ; }
8825         local file="/..$tdir/$tfile-1"
8826         $SETSTRIPE -p $pool $file ||
8827                 { error_noexit "unable to create $file" ; return 2 ; }
8828
8829         cd $tdir
8830         $SETSTRIPE -p $pool $tfile-2 || {
8831                 error_noexit "unable to create $tfile-2 in $tdir"
8832                 return 3
8833         }
8834 }
8835
8836 pool_remove_first_target() {
8837         echo "Removing first target from a pool"
8838         local pool=$1
8839
8840         local pname="lov.$FSNAME-*.pools.$pool"
8841         local t=$($LCTL get_param -n $pname | head -1)
8842         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
8843         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
8844                 error_noexit "$t not removed from $FSNAME.$pool"
8845                 return 1
8846         }
8847 }
8848
8849 pool_remove_all_targets() {
8850         echo "Removing all targets from pool"
8851         local pool=$1
8852         local file=$2
8853         local pname="lov.$FSNAME-*.pools.$pool"
8854         for t in $($LCTL get_param -n $pname | sort -u)
8855         do
8856                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
8857         done
8858         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
8859                 error_noexit "Pool $FSNAME.$pool cannot be drained"
8860                 return 1
8861         }
8862         # striping on an empty/nonexistant pool should fall back 
8863         # to "pool of everything"
8864         touch $file || {
8865                 error_noexit "failed to use fallback striping for empty pool"
8866                 return 2
8867         }
8868         # setstripe on an empty pool should fail
8869         $SETSTRIPE -p $pool $file 2>/dev/null && {
8870                 error_noexit "expected failure when creating file" \
8871                                                         "with empty pool"
8872                 return 3
8873         }
8874         return 0
8875 }
8876
8877 pool_remove() {
8878         echo "Destroying pool"
8879         local pool=$1
8880         local file=$2
8881
8882         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
8883
8884         sleep 2
8885         # striping on an empty/nonexistant pool should fall back 
8886         # to "pool of everything"
8887         touch $file || {
8888                 error_noexit "failed to use fallback striping for missing pool"
8889                 return 1
8890         }
8891         # setstripe on an empty pool should fail
8892         $SETSTRIPE -p $pool $file 2>/dev/null && {
8893                 error_noexit "expected failure when creating file" \
8894                                                         "with missing pool"
8895                 return 2
8896         }
8897
8898         # get param should return err once pool is gone
8899         if wait_update $HOSTNAME "lctl get_param -n \
8900                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
8901         then
8902                 remove_pool_from_list $FSNAME.$pool
8903                 return 0
8904         fi
8905         error_noexit "Pool $FSNAME.$pool is not destroyed"
8906         return 3
8907 }
8908
8909 test_200() {
8910         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8911
8912         local POOL=${POOL:-cea1}
8913         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
8914         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
8915         # Pool OST targets
8916         local first_ost=0
8917         local last_ost=$(($OSTCOUNT - 1))
8918         local ost_step=2
8919         local ost_list=$(seq $first_ost $ost_step $last_ost)
8920         local ost_range="$first_ost $last_ost $ost_step"
8921         local test_path=$POOL_ROOT/$POOL_DIR_NAME
8922         local file_dir=$POOL_ROOT/file_tst
8923
8924         local rc=0
8925         while : ; do
8926                 # former test_200a test_200b
8927                 pool_add $POOL                          || { rc=$? ; break; }
8928                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
8929                 # former test_200c test_200d
8930                 mkdir -p $test_path
8931                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
8932                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
8933                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
8934                                                         || { rc=$? ; break; }
8935                 # former test_200e test_200f
8936                 local files=$((OSTCOUNT*3))
8937                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
8938                                                         || { rc=$? ; break; }
8939                 pool_create_files $POOL $file_dir $files "$ost_list" \
8940                                                         || { rc=$? ; break; }
8941                 # former test_200g test_200h
8942                 pool_lfs_df $POOL                       || { rc=$? ; break; }
8943                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
8944
8945                 # former test_201a test_201b test_201c
8946                 pool_remove_first_target $POOL          || { rc=$? ; break; }
8947
8948                 local f=$test_path/$tfile
8949                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
8950                 pool_remove $POOL $f                    || { rc=$? ; break; }
8951                 break
8952         done
8953
8954         cleanup_pools
8955         return $rc
8956 }
8957 run_test 200 "OST pools"
8958
8959 # usage: default_attr <count | size | offset>
8960 default_attr() {
8961         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
8962 }
8963
8964 # usage: check_default_stripe_attr
8965 check_default_stripe_attr() {
8966         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
8967         case $1 in
8968         --stripe-count|--count)
8969                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
8970         --stripe-size|--size)
8971                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
8972         --stripe-index|--index)
8973                 EXPECTED=-1;;
8974         *)
8975                 error "unknown getstripe attr '$1'"
8976         esac
8977
8978         [ $ACTUAL != $EXPECTED ] &&
8979                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
8980 }
8981
8982 test_204a() {
8983         mkdir -p $DIR/$tdir
8984         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
8985
8986         check_default_stripe_attr --stripe-count
8987         check_default_stripe_attr --stripe-size
8988         check_default_stripe_attr --stripe-index
8989
8990         return 0
8991 }
8992 run_test 204a "Print default stripe attributes ================="
8993
8994 test_204b() {
8995         mkdir -p $DIR/$tdir
8996         $SETSTRIPE --stripe-count 1 $DIR/$tdir
8997
8998         check_default_stripe_attr --stripe-size
8999         check_default_stripe_attr --stripe-index
9000
9001         return 0
9002 }
9003 run_test 204b "Print default stripe size and offset  ==========="
9004
9005 test_204c() {
9006         mkdir -p $DIR/$tdir
9007         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9008
9009         check_default_stripe_attr --stripe-count
9010         check_default_stripe_attr --stripe-index
9011
9012         return 0
9013 }
9014 run_test 204c "Print default stripe count and offset ==========="
9015
9016 test_204d() {
9017         mkdir -p $DIR/$tdir
9018         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9019
9020         check_default_stripe_attr --stripe-count
9021         check_default_stripe_attr --stripe-size
9022
9023         return 0
9024 }
9025 run_test 204d "Print default stripe count and size ============="
9026
9027 test_204e() {
9028         mkdir -p $DIR/$tdir
9029         $SETSTRIPE -d $DIR/$tdir
9030
9031         check_default_stripe_attr --stripe-count --raw
9032         check_default_stripe_attr --stripe-size --raw
9033         check_default_stripe_attr --stripe-index --raw
9034
9035         return 0
9036 }
9037 run_test 204e "Print raw stripe attributes ================="
9038
9039 test_204f() {
9040         mkdir -p $DIR/$tdir
9041         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9042
9043         check_default_stripe_attr --stripe-size --raw
9044         check_default_stripe_attr --stripe-index --raw
9045
9046         return 0
9047 }
9048 run_test 204f "Print raw stripe size and offset  ==========="
9049
9050 test_204g() {
9051         mkdir -p $DIR/$tdir
9052         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9053
9054         check_default_stripe_attr --stripe-count --raw
9055         check_default_stripe_attr --stripe-index --raw
9056
9057         return 0
9058 }
9059 run_test 204g "Print raw stripe count and offset ==========="
9060
9061 test_204h() {
9062         mkdir -p $DIR/$tdir
9063         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9064
9065         check_default_stripe_attr --stripe-count --raw
9066         check_default_stripe_attr --stripe-size --raw
9067
9068         return 0
9069 }
9070 run_test 204h "Print raw stripe count and size ============="
9071
9072 # Figure out which job scheduler is being used, if any,
9073 # or use a fake one
9074 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9075         JOBENV=SLURM_JOB_ID
9076 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9077         JOBENV=LSB_JOBID
9078 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9079         JOBENV=PBS_JOBID
9080 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9081         JOBENV=LOADL_STEP_ID
9082 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9083         JOBENV=JOB_ID
9084 else
9085         JOBENV=FAKE_JOBID
9086 fi
9087
9088 verify_jobstats() {
9089         local cmd=$1
9090         local target=$2
9091
9092         # clear old jobstats
9093         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9094         do_facet ost0 lctl set_param obdfilter.*.job_stats="clear"
9095
9096         # use a new JobID for this test, or we might see an old one
9097         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9098
9099         JOBVAL=${!JOBENV}
9100         log "Test: $cmd"
9101         log "Using JobID environment variable $JOBENV=$JOBVAL"
9102
9103         if [ $JOBENV = "FAKE_JOBID" ]; then
9104                 FAKE_JOBID=$JOBVAL $cmd
9105         else
9106                 $cmd
9107         fi
9108
9109         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9110                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9111                 do_facet $FACET lctl get_param mdt.*.job_stats |
9112                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9113         fi
9114         if [ "$target" = "ost" -o "$target" = "both" ]; then
9115                 FACET=ost0
9116                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9117                         grep $JOBVAL || error "No job stats found on OST $FACET"
9118         fi
9119 }
9120
9121 test_205() { # Job stats
9122         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9123                 skip "Server doesn't support jobstats" && return 0
9124
9125         local cmd
9126         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9127         if [ $OLD_JOBENV != $JOBENV ]; then
9128                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9129                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9130                         $JOBENV || return 1
9131         fi
9132
9133         # mkdir
9134         cmd="mkdir $DIR/$tfile"
9135         verify_jobstats "$cmd" "mdt"
9136         # rmdir
9137         cmd="rm -fr $DIR/$tfile"
9138         verify_jobstats "$cmd" "mdt"
9139         # mknod
9140         cmd="mknod $DIR/$tfile c 1 3"
9141         verify_jobstats "$cmd" "mdt"
9142         # unlink
9143         cmd="rm -f $DIR/$tfile"
9144         verify_jobstats "$cmd" "mdt"
9145         # open & close
9146         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9147         verify_jobstats "$cmd" "mdt"
9148         # setattr
9149         cmd="touch $DIR/$tfile"
9150         verify_jobstats "$cmd" "both"
9151         # write
9152         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9153         verify_jobstats "$cmd" "ost"
9154         # read
9155         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9156         verify_jobstats "$cmd" "ost"
9157         # truncate
9158         cmd="$TRUNCATE $DIR/$tfile 0"
9159         verify_jobstats "$cmd" "both"
9160         # rename
9161         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9162         verify_jobstats "$cmd" "mdt"
9163
9164         # cleanup
9165         rm -f $DIR/jobstats_test_rename
9166
9167         if [ $OLD_JOBENV != $JOBENV ]; then
9168                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9169                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9170                         $OLD_JOBENV || return 1
9171         fi
9172 }
9173 run_test 205 "Verify job stats"
9174
9175 # LU-1480, LU-1773 and LU-1657
9176 test_206() {
9177         mkdir -p $DIR/$tdir
9178         lfs setstripe -c -1 $DIR/$tdir
9179 #define OBD_FAIL_LOV_INIT 0x1403
9180         $LCTL set_param fail_loc=0xa0001403
9181         $LCTL set_param fail_val=1
9182         touch $DIR/$tdir/$tfile || true
9183 }
9184 run_test 206 "fail lov_init_raid0() doesn't lbug"
9185
9186 test_212() {
9187         size=`date +%s`
9188         size=$((size % 8192 + 1))
9189         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9190         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9191         rm -f $DIR/f212 $DIR/f212.xyz
9192 }
9193 run_test 212 "Sendfile test ============================================"
9194
9195 test_213() {
9196         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9197         cancel_lru_locks osc
9198         lctl set_param fail_loc=0x8000040f
9199         # generate a read lock
9200         cat $DIR/$tfile > /dev/null
9201         # write to the file, it will try to cancel the above read lock.
9202         cat /etc/hosts >> $DIR/$tfile
9203 }
9204 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9205
9206 test_214() { # for bug 20133
9207         mkdir -p $DIR/d214p/d214c
9208         for (( i=0; i < 340; i++ )) ; do
9209                 touch $DIR/d214p/d214c/a$i
9210         done
9211
9212         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9213         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
9214         ls $DIR/d214c || error "ls $DIR/d214c failed"
9215         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
9216 }
9217 run_test 214 "hash-indexed directory test - bug 20133"
9218
9219 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
9220 create_lnet_proc_files() {
9221         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
9222         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
9223
9224         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
9225         rm -f "$TMP/lnet_$1.sys_tmp"
9226 }
9227
9228 # counterpart of create_lnet_proc_files
9229 remove_lnet_proc_files() {
9230         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
9231 }
9232
9233 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9234 # 3rd arg as regexp for body
9235 check_lnet_proc_stats() {
9236         local l=$(cat "$TMP/lnet_$1" |wc -l)
9237         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
9238
9239         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
9240 }
9241
9242 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9243 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
9244 # optional and can be regexp for 2nd line (lnet.routes case)
9245 check_lnet_proc_entry() {
9246         local blp=2            # blp stands for 'position of 1st line of body'
9247         [ "$5" = "" ] || blp=3 # lnet.routes case
9248
9249         local l=$(cat "$TMP/lnet_$1" |wc -l)
9250         # subtracting one from $blp because the body can be empty
9251         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
9252
9253         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
9254                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
9255
9256         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
9257                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
9258
9259         # bail out if any unexpected line happened
9260         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
9261         [ "$?" != 0 ] || error "$2 misformatted"
9262 }
9263
9264 test_215() { # for bugs 18102, 21079, 21517
9265         local N='(0|[1-9][0-9]*)'       # non-negative numeric
9266         local P='[1-9][0-9]*'           # positive numeric
9267         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
9268         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
9269         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
9270         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
9271
9272         local L1 # regexp for 1st line
9273         local L2 # regexp for 2nd line (optional)
9274         local BR # regexp for the rest (body)
9275
9276         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
9277         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
9278         create_lnet_proc_files "stats"
9279         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
9280         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
9281         remove_lnet_proc_files "stats"
9282
9283         # /proc/sys/lnet/routes should look like this:
9284         # Routing disabled/enabled
9285         # net hops state router
9286         # where net is a string like tcp0, hops >= 0, state is up/down,
9287         # router is a string like 192.168.1.1@tcp2
9288         L1="^Routing (disabled|enabled)$"
9289         L2="^net +hops +state +router$"
9290         BR="^$NET +$N +(up|down) +$NID$"
9291         create_lnet_proc_files "routes"
9292         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
9293         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
9294         remove_lnet_proc_files "routes"
9295
9296         # /proc/sys/lnet/routers should look like this:
9297         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
9298         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
9299         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
9300         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
9301         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
9302         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
9303         create_lnet_proc_files "routers"
9304         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
9305         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
9306         remove_lnet_proc_files "routers"
9307
9308         # /proc/sys/lnet/peers should look like this:
9309         # nid refs state last max rtr min tx min queue
9310         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
9311         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
9312         # numeric (0 or >0 or <0), queue >= 0.
9313         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
9314         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
9315         create_lnet_proc_files "peers"
9316         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
9317         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
9318         remove_lnet_proc_files "peers"
9319
9320         # /proc/sys/lnet/buffers  should look like this:
9321         # pages count credits min
9322         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
9323         L1="^pages +count +credits +min$"
9324         BR="^ +$N +$N +$I +$I$"
9325         create_lnet_proc_files "buffers"
9326         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
9327         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
9328         remove_lnet_proc_files "buffers"
9329
9330         # /proc/sys/lnet/nis should look like this:
9331         # nid status alive refs peer rtr max tx min
9332         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
9333         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
9334         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
9335         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
9336         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
9337         create_lnet_proc_files "nis"
9338         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
9339         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
9340         remove_lnet_proc_files "nis"
9341
9342         # can we successfully write to /proc/sys/lnet/stats?
9343         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
9344         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
9345 }
9346 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
9347
9348 test_216() { # bug 20317
9349         remote_ost_nodsh && skip "remote OST with nodsh" && return
9350         local node
9351         local p="$TMP/sanityN-$TESTNAME.parameters"
9352         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
9353         for node in $(osts_nodes); do
9354                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
9355                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
9356                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
9357         done
9358         clear_osc_stats
9359
9360         # agressive lockless i/o settings
9361         for node in $(osts_nodes); do
9362                 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'
9363         done
9364         lctl set_param -n osc.*.contention_seconds 60
9365
9366         $DIRECTIO write $DIR/$tfile 0 10 4096
9367         $CHECKSTAT -s 40960 $DIR/$tfile
9368
9369         # disable lockless i/o
9370         for node in $(osts_nodes); do
9371                 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'
9372         done
9373         lctl set_param -n osc.*.contention_seconds 0
9374         clear_osc_stats
9375
9376         dd if=/dev/zero of=$DIR/$tfile count=0
9377         $CHECKSTAT -s 0 $DIR/$tfile
9378
9379         restore_lustre_params <$p
9380         rm -f $p
9381         rm $DIR/$tfile
9382 }
9383 run_test 216 "check lockless direct write works and updates file size and kms correctly"
9384
9385 test_217() { # bug 22430
9386         local node
9387         local nid
9388
9389         for node in $(nodes_list); do
9390                 nid=$(host_nids_address $node $NETTYPE)
9391                 if [[ $nid = *-* ]] ; then
9392                         echo "lctl ping $nid@$NETTYPE"
9393                         lctl ping $nid@$NETTYPE
9394                 else
9395                         echo "skipping $node (no hyphen detected)"
9396                 fi
9397         done
9398 }
9399 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
9400
9401 test_218() {
9402        # do directio so as not to populate the page cache
9403        log "creating a 10 Mb file"
9404        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9405        log "starting reads"
9406        dd if=$DIR/$tfile of=/dev/null bs=4096 &
9407        log "truncating the file"
9408        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9409        log "killing dd"
9410        kill %+ || true # reads might have finished
9411        echo "wait until dd is finished"
9412        wait
9413        log "removing the temporary file"
9414        rm -rf $DIR/$tfile || error "tmp file removal failed"
9415 }
9416 run_test 218 "parallel read and truncate should not deadlock ======================="
9417
9418 test_219() {
9419         # write one partial page
9420         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
9421         # set no grant so vvp_io_commit_write will do sync write
9422         $LCTL set_param fail_loc=0x411
9423         # write a full page at the end of file
9424         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
9425
9426         $LCTL set_param fail_loc=0
9427         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
9428         $LCTL set_param fail_loc=0x411
9429         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
9430 }
9431 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
9432
9433 test_220() { #LU-325
9434         remote_ost_nodsh && skip "remote OST with nodsh" && return
9435         local OSTIDX=0
9436
9437         mkdir -p $DIR/$tdir
9438         local OST=$(lfs osts | grep ${OSTIDX}": " | \
9439                 awk '{print $2}' | sed -e 's/_UUID$//')
9440
9441         # on the mdt's osc
9442         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
9443         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
9444                         osc.$mdtosc_proc1.prealloc_last_id)
9445         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
9446                         osc.$mdtosc_proc1.prealloc_next_id)
9447
9448         $LFS df -i
9449
9450         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
9451         #define OBD_FAIL_OST_ENOINO              0x229
9452         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
9453         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
9454         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
9455
9456         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
9457
9458         MDSOBJS=$((last_id - next_id))
9459         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
9460
9461         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
9462         echo "OST still has $count kbytes free"
9463
9464         echo "create $MDSOBJS files @next_id..."
9465         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
9466
9467         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9468                         osc.$mdtosc_proc1.prealloc_last_id)
9469         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9470                         osc.$mdtosc_proc1.prealloc_next_id)
9471
9472         echo "after creation, last_id=$last_id2, next_id=$next_id2"
9473         $LFS df -i
9474
9475         echo "cleanup..."
9476
9477         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
9478         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
9479
9480         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
9481         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
9482         echo "unlink $MDSOBJS files @$next_id..."
9483         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
9484 }
9485 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
9486
9487 test_221() {
9488         dd if=`which date` of=$MOUNT/date oflag=sync
9489         chmod +x $MOUNT/date
9490
9491         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
9492         $LCTL set_param fail_loc=0x80001401
9493
9494         $MOUNT/date > /dev/null
9495         rm -f $MOUNT/date
9496 }
9497 run_test 221 "make sure fault and truncate race to not cause OOM"
9498
9499 test_222a () {
9500        rm -rf $DIR/$tdir
9501        mkdir -p $DIR/$tdir
9502        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9503        createmany -o $DIR/$tdir/$tfile 10
9504        cancel_lru_locks mdc
9505        cancel_lru_locks osc
9506        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9507        $LCTL set_param fail_loc=0x31a
9508        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
9509        $LCTL set_param fail_loc=0
9510        rm -r $DIR/$tdir
9511 }
9512 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
9513
9514 test_222b () {
9515        rm -rf $DIR/$tdir
9516        mkdir -p $DIR/$tdir
9517        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9518        createmany -o $DIR/$tdir/$tfile 10
9519        cancel_lru_locks mdc
9520        cancel_lru_locks osc
9521        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9522        $LCTL set_param fail_loc=0x31a
9523        rm -r $DIR/$tdir || "AGL for rmdir failed"
9524        $LCTL set_param fail_loc=0
9525 }
9526 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
9527
9528 test_223 () {
9529        rm -rf $DIR/$tdir
9530        mkdir -p $DIR/$tdir
9531        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9532        createmany -o $DIR/$tdir/$tfile 10
9533        cancel_lru_locks mdc
9534        cancel_lru_locks osc
9535        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
9536        $LCTL set_param fail_loc=0x31b
9537        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
9538        $LCTL set_param fail_loc=0
9539        rm -r $DIR/$tdir
9540 }
9541 run_test 223 "osc reenqueue if without AGL lock granted ======================="
9542
9543 test_224a() { # LU-1039, MRP-303
9544         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
9545         $LCTL set_param fail_loc=0x508
9546         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
9547         $LCTL set_param fail_loc=0
9548         df $DIR
9549 }
9550 run_test 224a "Don't panic on bulk IO failure"
9551
9552 test_224b() { # LU-1039, MRP-303
9553         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
9554         cancel_lru_locks osc
9555         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
9556         $LCTL set_param fail_loc=0x515
9557         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
9558         $LCTL set_param fail_loc=0
9559         df $DIR
9560 }
9561 run_test 224b "Don't panic on bulk IO failure"
9562
9563 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
9564 test_225a () {
9565        if [ -z ${MDSSURVEY} ]; then
9566               skip_env "mds-survey not found" && return
9567        fi
9568        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9569             { skip "Need MDS version at least 2.2.51"; return; }
9570
9571        local mds=$(facet_host $SINGLEMDS)
9572        local target=$(do_nodes $mds 'lctl dl' | \
9573                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9574
9575        local cmd1="file_count=1000 thrhi=4"
9576        local cmd2="dir_count=2 layer=mdd stripe_count=0"
9577        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9578        local cmd="$cmd1 $cmd2 $cmd3"
9579
9580        rm -f ${TMP}/mds_survey*
9581        echo + $cmd
9582        eval $cmd || error "mds-survey with zero-stripe failed"
9583        cat ${TMP}/mds_survey*
9584        rm -f ${TMP}/mds_survey*
9585 }
9586 run_test 225a "Metadata survey sanity with zero-stripe"
9587
9588 test_225b () {
9589        if [ -z ${MDSSURVEY} ]; then
9590               skip_env "mds-survey not found" && return
9591        fi
9592        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9593             { skip "Need MDS version at least 2.2.51"; return; }
9594
9595        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
9596               skip_env "Need to mount OST to test" && return
9597        fi
9598
9599        local mds=$(facet_host $SINGLEMDS)
9600        local target=$(do_nodes $mds 'lctl dl' | \
9601                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9602
9603        local cmd1="file_count=1000 thrhi=4"
9604        local cmd2="dir_count=2 layer=mdd stripe_count=1"
9605        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9606        local cmd="$cmd1 $cmd2 $cmd3"
9607
9608        rm -f ${TMP}/mds_survey*
9609        echo + $cmd
9610        eval $cmd || error "mds-survey with stripe_count failed"
9611        cat ${TMP}/mds_survey*
9612        rm -f ${TMP}/mds_survey*
9613 }
9614 run_test 225b "Metadata survey sanity with stripe_count = 1"
9615
9616 mcreate_path2fid () {
9617         local mode=$1
9618         local major=$2
9619         local minor=$3
9620         local name=$4
9621         local desc=$5
9622         local path=$DIR/$tdir/$name
9623         local fid
9624         local rc
9625         local fid_path
9626
9627         $MCREATE --mode=$1 --major=$2 --minor=$3 $path || \
9628                 error "error: cannot create $desc"
9629
9630         fid=$($LFS path2fid $path)
9631         rc=$?
9632         [ $rc -ne 0 ] && error "error: cannot get fid of a $desc"
9633
9634         fid_path=$($LFS fid2path $DIR $fid)
9635         rc=$?
9636         [ $rc -ne 0 ] && error "error: cannot get path of a $desc by fid"
9637
9638         [ "$path" == "$fid_path" ] || \
9639                 error "error: fid2path returned \`$fid_path', expected \`$path'"
9640 }
9641
9642 test_226 () {
9643         rm -rf $DIR/$tdir
9644         mkdir -p $DIR/$tdir
9645
9646         mcreate_path2fid 0010666 0 0 fifo "FIFO"
9647         mcreate_path2fid 0020666 1 3 null "character special file (null)"
9648         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
9649         mcreate_path2fid 0040666 0 0 dir "directory"
9650         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
9651         mcreate_path2fid 0100666 0 0 file "regular file"
9652         mcreate_path2fid 0120666 0 0 link "symbolic link"
9653         mcreate_path2fid 0140666 0 0 sock "socket"
9654 }
9655 run_test 226 "call path2fid and fid2path on files of all type"
9656
9657 # LU-1299 Executing or running ldd on a truncated executable does not
9658 # cause an out-of-memory condition.
9659 test_227() {
9660         dd if=`which date` of=$MOUNT/date bs=1k count=1
9661         chmod +x $MOUNT/date
9662
9663         $MOUNT/date > /dev/null
9664         ldd $MOUNT/date > /dev/null
9665         rm -f $MOUNT/date
9666 }
9667 run_test 227 "running truncated executable does not cause OOM"
9668
9669 # LU-1512 try to reuse idle OI blocks
9670 test_228a() {
9671         [ "$FSTYPE" != "ldiskfs" ] && skip "non-ldiskfs backend" && return
9672
9673         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9674         local myDIR=$DIR/$tdir
9675
9676         mkdir -p $myDIR
9677         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9678         $LCTL set_param fail_loc=0x80001002
9679         createmany -o $myDIR/t- 10000
9680         $LCTL set_param fail_loc=0
9681         # The guard is current the largest FID holder
9682         touch $myDIR/guard
9683         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9684                     tr -d '[')
9685         local IDX=$(($SEQ % 64))
9686
9687         do_facet $SINGLEMDS sync
9688         # Make sure journal flushed.
9689         sleep 6
9690         local blk1=$(do_facet $SINGLEMDS \
9691                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9692                      grep Blockcount | awk '{print $4}')
9693
9694         # Remove old files, some OI blocks will become idle.
9695         unlinkmany $myDIR/t- 10000
9696         # Create new files, idle OI blocks should be reused.
9697         createmany -o $myDIR/t- 2000
9698         do_facet $SINGLEMDS sync
9699         # Make sure journal flushed.
9700         sleep 6
9701         local blk2=$(do_facet $SINGLEMDS \
9702                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9703                      grep Blockcount | awk '{print $4}')
9704
9705         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9706 }
9707 run_test 228a "try to reuse idle OI blocks"
9708
9709 test_228b() {
9710         [ "$FSTYPE" != "ldiskfs" ] && skip "non-ldiskfs backend" && return
9711
9712         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9713         local myDIR=$DIR/$tdir
9714
9715         mkdir -p $myDIR
9716         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9717         $LCTL set_param fail_loc=0x80001002
9718         createmany -o $myDIR/t- 10000
9719         $LCTL set_param fail_loc=0
9720         # The guard is current the largest FID holder
9721         touch $myDIR/guard
9722         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9723                     tr -d '[')
9724         local IDX=$(($SEQ % 64))
9725
9726         do_facet $SINGLEMDS sync
9727         # Make sure journal flushed.
9728         sleep 6
9729         local blk1=$(do_facet $SINGLEMDS \
9730                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9731                      grep Blockcount | awk '{print $4}')
9732
9733         # Remove old files, some OI blocks will become idle.
9734         unlinkmany $myDIR/t- 10000
9735
9736         # stop the MDT
9737         stop $SINGLEMDS || error "Fail to stop MDT."
9738         # remount the MDT
9739         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9740
9741         df $MOUNT || error "Fail to df."
9742         # Create new files, idle OI blocks should be reused.
9743         createmany -o $myDIR/t- 2000
9744         do_facet $SINGLEMDS sync
9745         # Make sure journal flushed.
9746         sleep 6
9747         local blk2=$(do_facet $SINGLEMDS \
9748                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9749                      grep Blockcount | awk '{print $4}')
9750
9751         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9752 }
9753 run_test 228b "idle OI blocks can be reused after MDT restart"
9754
9755 #LU-1881
9756 test_228c() {
9757         [ "$FSTYPE" != "ldiskfs" ] && skip "non-ldiskfs backend" && return
9758
9759         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9760         local myDIR=$DIR/$tdir
9761
9762         mkdir -p $myDIR
9763         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9764         $LCTL set_param fail_loc=0x80001002
9765         # 20000 files can guarantee there are index nodes in the OI file
9766         createmany -o $myDIR/t- 20000
9767         $LCTL set_param fail_loc=0
9768         # The guard is current the largest FID holder
9769         touch $myDIR/guard
9770         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9771                     tr -d '[')
9772         local IDX=$(($SEQ % 64))
9773
9774         do_facet $SINGLEMDS sync
9775         # Make sure journal flushed.
9776         sleep 6
9777         local blk1=$(do_facet $SINGLEMDS \
9778                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9779                      grep Blockcount | awk '{print $4}')
9780
9781         # Remove old files, some OI blocks will become idle.
9782         unlinkmany $myDIR/t- 20000
9783         rm -f $myDIR/guard
9784         # The OI file should become empty now
9785
9786         # Create new files, idle OI blocks should be reused.
9787         createmany -o $myDIR/t- 2000
9788         do_facet $SINGLEMDS sync
9789         # Make sure journal flushed.
9790         sleep 6
9791         local blk2=$(do_facet $SINGLEMDS \
9792                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9793                      grep Blockcount | awk '{print $4}')
9794
9795         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9796 }
9797 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
9798
9799 #
9800 # tests that do cleanup/setup should be run at the end
9801 #
9802
9803 test_900() {
9804         local ls
9805         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
9806         $LCTL set_param fail_loc=0x903
9807         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
9808         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
9809                 echo "clear" > $ls
9810         done
9811         FAIL_ON_ERROR=true cleanup
9812         FAIL_ON_ERROR=true setup
9813 }
9814 run_test 900 "umount should not race with any mgc requeue thread"
9815
9816 complete $(basename $0) $SECONDS
9817 check_and_cleanup_lustre
9818 if [ "$I_MOUNTED" != "yes" ]; then
9819         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
9820 fi
9821 exit_status