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