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