Whamcloud - gitweb
da12150df01359ce1c5f9d5aeee52869e1505393
[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 # with LOD/OSP landing
15 # bug number for skipped tests: LU2036
16 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
17
18
19 # Tests that fail on uml
20 CPU=`awk '/model/ {print $4}' /proc/cpuinfo`
21 #                                    buffer i/o errs             sock spc runas
22 [ "$CPU" = "UML" ] && EXCEPT="$EXCEPT 27m 27n 27o 27p 27q 27r 31d 54a  64b 99a 99b 99c 99d 99e 99f 101a"
23
24 SRCDIR=$(cd $(dirname $0); echo $PWD)
25 export PATH=$PATH:/sbin
26
27 TMP=${TMP:-/tmp}
28
29 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
30 CREATETEST=${CREATETEST:-createtest}
31 LFS=${LFS:-lfs}
32 LFIND=${LFIND:-"$LFS find"}
33 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
34 LCTL=${LCTL:-lctl}
35 MCREATE=${MCREATE:-mcreate}
36 OPENFILE=${OPENFILE:-openfile}
37 OPENUNLINK=${OPENUNLINK:-openunlink}
38 export MULTIOP=${MULTIOP:-multiop}
39 READS=${READS:-"reads"}
40 MUNLINK=${MUNLINK:-munlink}
41 SOCKETSERVER=${SOCKETSERVER:-socketserver}
42 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
43 MEMHOG=${MEMHOG:-memhog}
44 DIRECTIO=${DIRECTIO:-directio}
45 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
46 UMOUNT=${UMOUNT:-"umount -d"}
47 STRIPES_PER_OBJ=-1
48 CHECK_GRANT=${CHECK_GRANT:-"yes"}
49 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
50
51 export NAME=${NAME:-local}
52
53 SAVE_PWD=$PWD
54
55 CLEANUP=${CLEANUP:-:}
56 SETUP=${SETUP:-:}
57 TRACE=${TRACE:-""}
58 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
59 . $LUSTRE/tests/test-framework.sh
60 init_test_env $@
61 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
62 init_logging
63
64 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24v 27m 36f 36g 36h 51b 60c 63 64b 68 71 73 77f 78 101a 103 115 120g 124b"
65
66 FAIL_ON_ERROR=false
67
68 cleanup() {
69         echo -n "cln.."
70         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
71         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
72 }
73 setup() {
74         echo -n "mnt.."
75         load_modules
76         setupall || exit 10
77         echo "done"
78 }
79
80 check_kernel_version() {
81         WANT_VER=$1
82         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
83         case $GOT_VER in
84         patchless|patchless_client) return 0;;
85         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
86         esac
87         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
88         return 1
89 }
90
91 if [ "$ONLY" == "cleanup" ]; then
92        sh llmountcleanup.sh
93        exit 0
94 fi
95
96 check_and_setup_lustre
97
98 DIR=${DIR:-$MOUNT}
99 assert_DIR
100
101 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid | \
102     awk '{gsub(/_UUID/,""); print $1}' | head -1)
103 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
104 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
105 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
106 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
107 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
108 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
109
110 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
111 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
112 rm -rf $DIR/[Rdfs][0-9]*
113
114 # $RUNAS_ID may get set incorrectly somewhere else
115 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
116
117 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
118
119 build_test_filter
120
121 if [ "${ONLY}" = "MOUNT" ] ; then
122         echo "Lustre is up, please go on"
123         exit
124 fi
125
126 echo "preparing for tests involving mounts"
127 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
128 touch $EXT2_DEV
129 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
130 echo # add a newline after mke2fs.
131
132 umask 077
133
134 OLDDEBUG="`lctl get_param -n debug 2> /dev/null`"
135 lctl set_param debug=-1 2> /dev/null || true
136 test_0() {
137         touch $DIR/$tfile
138         $CHECKSTAT -t file $DIR/$tfile || error
139         rm $DIR/$tfile
140         $CHECKSTAT -a $DIR/$tfile || error
141 }
142 run_test 0 "touch .../$tfile ; rm .../$tfile ====================="
143
144 test_0b() {
145         chmod 0755 $DIR || error
146         $CHECKSTAT -p 0755 $DIR || error
147 }
148 run_test 0b "chmod 0755 $DIR ============================="
149
150 test_0c() {
151     $LCTL get_param mdc.*.import | grep  "state: FULL" || error "import not FULL"
152     $LCTL get_param mdc.*.import | grep  "target: $FSNAME-MDT" || error "bad target"
153 }
154 run_test 0c "check import proc ============================="
155
156 test_1a() {
157         mkdir $DIR/d1
158         mkdir $DIR/d1/d2
159         mkdir $DIR/d1/d2 && error "we expect EEXIST, but not returned"
160         $CHECKSTAT -t dir $DIR/d1/d2 || error
161 }
162 run_test 1a "mkdir .../d1; mkdir .../d1/d2 ====================="
163
164 test_1b() {
165         rmdir $DIR/d1/d2
166         rmdir $DIR/d1
167         $CHECKSTAT -a $DIR/d1 || error
168 }
169 run_test 1b "rmdir .../d1/d2; rmdir .../d1 ====================="
170
171 test_2a() {
172         mkdir $DIR/d2
173         touch $DIR/d2/f
174         $CHECKSTAT -t file $DIR/d2/f || error
175 }
176 run_test 2a "mkdir .../d2; touch .../d2/f ======================"
177
178 test_2b() {
179         rm -r $DIR/d2
180         $CHECKSTAT -a $DIR/d2 || error
181 }
182 run_test 2b "rm -r .../d2; checkstat .../d2/f ======================"
183
184 test_3a() {
185         mkdir $DIR/d3
186         $CHECKSTAT -t dir $DIR/d3 || error
187 }
188 run_test 3a "mkdir .../d3 ======================================"
189
190 test_3b() {
191         if [ ! -d $DIR/d3 ]; then
192                 mkdir $DIR/d3
193         fi
194         touch $DIR/d3/f
195         $CHECKSTAT -t file $DIR/d3/f || error
196 }
197 run_test 3b "touch .../d3/f ===================================="
198
199 test_3c() {
200         rm -r $DIR/d3
201         $CHECKSTAT -a $DIR/d3 || error
202 }
203 run_test 3c "rm -r .../d3 ======================================"
204
205 test_4a() {
206         mkdir $DIR/d4
207         $CHECKSTAT -t dir $DIR/d4 || error
208 }
209 run_test 4a "mkdir .../d4 ======================================"
210
211 test_4b() {
212         if [ ! -d $DIR/d4 ]; then
213                 mkdir $DIR/d4
214         fi
215         mkdir $DIR/d4/d2
216         $CHECKSTAT -t dir $DIR/d4/d2 || error
217 }
218 run_test 4b "mkdir .../d4/d2 ==================================="
219
220 test_5() {
221         mkdir $DIR/d5
222         mkdir $DIR/d5/d2
223         chmod 0707 $DIR/d5/d2
224         $CHECKSTAT -t dir -p 0707 $DIR/d5/d2 || error
225 }
226 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
227
228 test_6a() {
229         touch $DIR/f6a
230         chmod 0666 $DIR/f6a || error
231         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
232 }
233 run_test 6a "touch .../f6a; chmod .../f6a ======================"
234
235 test_6b() {
236         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
237         if [ ! -f $DIR/f6a ]; then
238                 touch $DIR/f6a
239                 chmod 0666 $DIR/f6a
240         fi
241         $RUNAS chmod 0444 $DIR/f6a && error
242         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/f6a || error
243 }
244 run_test 6b "$RUNAS chmod .../f6a (should return error) =="
245
246 test_6c() {
247         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
248         touch $DIR/f6c
249         chown $RUNAS_ID $DIR/f6c || error
250         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
251 }
252 run_test 6c "touch .../f6c; chown .../f6c ======================"
253
254 test_6d() {
255         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
256         if [ ! -f $DIR/f6c ]; then
257                 touch $DIR/f6c
258                 chown $RUNAS_ID $DIR/f6c
259         fi
260         $RUNAS chown $UID $DIR/f6c && error
261         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/f6c || error
262 }
263 run_test 6d "$RUNAS chown .../f6c (should return error) =="
264
265 test_6e() {
266         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
267         touch $DIR/f6e
268         chgrp $RUNAS_ID $DIR/f6e || error
269         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
270 }
271 run_test 6e "touch .../f6e; chgrp .../f6e ======================"
272
273 test_6f() {
274         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
275         if [ ! -f $DIR/f6e ]; then
276                 touch $DIR/f6e
277                 chgrp $RUNAS_ID $DIR/f6e
278         fi
279         $RUNAS chgrp $UID $DIR/f6e && error
280         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/f6e || error
281 }
282 run_test 6f "$RUNAS chgrp .../f6e (should return error) =="
283
284 test_6g() {
285         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
286         mkdir $DIR/d6g || error
287         chmod 777 $DIR/d6g || error
288         $RUNAS mkdir $DIR/d6g/d || error
289         chmod g+s $DIR/d6g/d || error
290         mkdir $DIR/d6g/d/subdir
291         $CHECKSTAT -g \#$RUNAS_GID $DIR/d6g/d/subdir || error
292 }
293 run_test 6g "Is new dir in sgid dir inheriting group?"
294
295 test_6h() { # bug 7331
296         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
297         touch $DIR/f6h || error "touch failed"
298         chown $RUNAS_ID:$RUNAS_GID $DIR/f6h || error "initial chown failed"
299         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/f6h && error "chown worked"
300         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/f6h || error
301 }
302 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
303
304 test_7a() {
305         mkdir $DIR/d7
306         $MCREATE $DIR/d7/f
307         chmod 0666 $DIR/d7/f
308         $CHECKSTAT -t file -p 0666 $DIR/d7/f || error
309 }
310 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
311
312 test_7b() {
313         if [ ! -d $DIR/d7 ]; then
314                 mkdir $DIR/d7
315         fi
316         $MCREATE $DIR/d7/f2
317         echo -n foo > $DIR/d7/f2
318         [ "`cat $DIR/d7/f2`" = "foo" ] || error
319         $CHECKSTAT -t file -s 3 $DIR/d7/f2 || error
320 }
321 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
322
323 test_8() {
324         mkdir $DIR/d8
325         touch $DIR/d8/f
326         chmod 0666 $DIR/d8/f
327         $CHECKSTAT -t file -p 0666 $DIR/d8/f || error
328 }
329 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
330
331 test_9() {
332         mkdir $DIR/d9
333         mkdir $DIR/d9/d2
334         mkdir $DIR/d9/d2/d3
335         $CHECKSTAT -t dir $DIR/d9/d2/d3 || error
336 }
337 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
338
339 test_10() {
340         mkdir $DIR/d10
341         mkdir $DIR/d10/d2
342         touch $DIR/d10/d2/f
343         $CHECKSTAT -t file $DIR/d10/d2/f || error
344 }
345 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
346
347 test_11() {
348         mkdir $DIR/d11
349         mkdir $DIR/d11/d2
350         chmod 0666 $DIR/d11/d2
351         chmod 0705 $DIR/d11/d2
352         $CHECKSTAT -t dir -p 0705 $DIR/d11/d2 || error
353 }
354 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
355
356 test_12() {
357         mkdir $DIR/d12
358         touch $DIR/d12/f
359         chmod 0666 $DIR/d12/f
360         chmod 0654 $DIR/d12/f
361         $CHECKSTAT -t file -p 0654 $DIR/d12/f || error
362 }
363 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
364
365 test_13() {
366         mkdir $DIR/d13
367         dd if=/dev/zero of=$DIR/d13/f count=10
368         >  $DIR/d13/f
369         $CHECKSTAT -t file -s 0 $DIR/d13/f || error
370 }
371 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
372
373 test_14() {
374         mkdir $DIR/d14
375         touch $DIR/d14/f
376         rm $DIR/d14/f
377         $CHECKSTAT -a $DIR/d14/f || error
378 }
379 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
380
381 test_15() {
382         mkdir $DIR/d15
383         touch $DIR/d15/f
384         mv $DIR/d15/f $DIR/d15/f2
385         $CHECKSTAT -t file $DIR/d15/f2 || error
386 }
387 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
388
389 test_16() {
390         mkdir $DIR/d16
391         touch $DIR/d16/f
392         rm -rf $DIR/d16/f
393         $CHECKSTAT -a $DIR/d16/f || error
394 }
395 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
396
397 test_17a() {
398         mkdir -p $DIR/d17
399         touch $DIR/d17/f
400         ln -s $DIR/d17/f $DIR/d17/l-exist
401         ls -l $DIR/d17
402         $CHECKSTAT -l $DIR/d17/f $DIR/d17/l-exist || error
403         $CHECKSTAT -f -t f $DIR/d17/l-exist || error
404         rm -f $DIR/d17/l-exist
405         $CHECKSTAT -a $DIR/d17/l-exist || error
406 }
407 run_test 17a "symlinks: create, remove (real) =================="
408
409 test_17b() {
410         mkdir -p $DIR/d17
411         ln -s no-such-file $DIR/d17/l-dangle
412         ls -l $DIR/d17
413         $CHECKSTAT -l no-such-file $DIR/d17/l-dangle || error
414         $CHECKSTAT -fa $DIR/d17/l-dangle || error
415         rm -f $DIR/d17/l-dangle
416         $CHECKSTAT -a $DIR/d17/l-dangle || error
417 }
418 run_test 17b "symlinks: create, remove (dangling) =============="
419
420 test_17c() { # bug 3440 - don't save failed open RPC for replay
421         mkdir -p $DIR/d17
422         ln -s foo $DIR/d17/f17c
423         cat $DIR/d17/f17c && error "opened non-existent symlink" || true
424 }
425 run_test 17c "symlinks: open dangling (should return error) ===="
426
427 test_17d() {
428         mkdir -p $DIR/d17
429         ln -s foo $DIR/d17/f17d
430         touch $DIR/d17/f17d || error "creating to new symlink"
431 }
432 run_test 17d "symlinks: create dangling ========================"
433
434 test_17e() {
435         mkdir -p $DIR/$tdir
436         local foo=$DIR/$tdir/$tfile
437         ln -s $foo $foo || error "create symlink failed"
438         ls -l $foo || error "ls -l failed"
439         ls $foo && error "ls not failed" || true
440 }
441 run_test 17e "symlinks: create recursive symlink (should return error) ===="
442
443 test_17f() {
444         mkdir -p $DIR/d17f
445         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
446         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
447         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
448         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
449         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
450         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
451         ls -l  $DIR/d17f
452 }
453 run_test 17f "symlinks: long and very long symlink name ========================"
454
455 test_17g() {
456         mkdir -p $DIR/$tdir
457         LONGSYMLINK="$(dd if=/dev/zero bs=4095 count=1 | tr '\0' 'x')"
458         ln -s $LONGSYMLINK $DIR/$tdir/$tfile
459         ls -l $DIR/$tdir
460 }
461 run_test 17g "symlinks: really long symlink name ==============================="
462
463 test_17h() { #bug 17378
464         remote_mds_nodsh && skip "remote MDS with nodsh" && return
465         mkdir -p $DIR/$tdir
466         $SETSTRIPE -c -1 $DIR/$tdir
467 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
468         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000141
469         touch $DIR/$tdir/$tfile || true
470 }
471 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
472
473 test_17i() { #bug 20018
474         remote_mds_nodsh && skip "remote MDS with nodsh" && return
475         mkdir -p $DIR/$tdir
476         local foo=$DIR/$tdir/$tfile
477         ln -s $foo $foo || error "create symlink failed"
478 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
479         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000143
480         ls -l $foo && error "error not detected"
481         return 0
482 }
483 run_test 17i "don't panic on short symlink"
484
485 test_17k() { #bug 22301
486         rsync --help | grep -q xattr ||
487                 skip_env "$(rsync --version| head -1) does not support xattrs"
488         mkdir -p $DIR/{$tdir,$tdir.new}
489         touch $DIR/$tdir/$tfile
490         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
491         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
492                 error "rsync failed with xattrs enabled"
493 }
494 run_test 17k "symlinks: rsync with xattrs enabled ========================="
495
496 # LU-1540
497 test_17m() {
498         local short_sym="0123456789"
499         local WDIR=$DIR/${tdir}m
500         local mds_index
501         local devname
502         local cmd
503         local i
504         local rc=0
505
506         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
507         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
508                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
509         mkdir -p $WDIR
510         long_sym=$short_sym
511         # create a long symlink file
512         for ((i = 0; i < 4; ++i)); do
513                 long_sym=${long_sym}${long_sym}
514         done
515
516         echo "create 512 short and long symlink files under $WDIR"
517         for ((i = 0; i < 256; ++i)); do
518                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
519                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
520         done
521
522         echo "erase them"
523         rm -f $WDIR/*
524         sync
525         sleep 2
526
527         echo "recreate the 512 symlink files with a shorter string"
528         for ((i = 0; i < 512; ++i)); do
529                 # rewrite the symlink file with a shorter string
530                 ln -sf ${long_sym} $WDIR/long-$i
531                 ln -sf ${short_sym} $WDIR/short-$i
532         done
533
534         mds_index=$($LFS getstripe -M $WDIR)
535         mds_index=$((mds_index+1))
536         devname=$(mdsdevname $mds_index)
537         cmd="$E2FSCK -fnvd $devname"
538
539         echo "stop and checking mds${mds_index}: $cmd"
540         # e2fsck should not return error
541         stop mds${mds_index} -f
542         do_facet mds${mds_index} $cmd || rc=$?
543
544         start mds${mds_index} $devname $MDS_MOUNT_OPTS
545         df $MOUNT > /dev/null 2>&1
546         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
547                 "short/long symlink MDT: rc=$rc"
548         return $rc
549 }
550 run_test 17m "run e2fsck against MDT which contains short/long symlink"
551
552 test_18() {
553         touch $DIR/f
554         ls $DIR || error
555 }
556 run_test 18 "touch .../f ; ls ... =============================="
557
558 test_19a() {
559         touch $DIR/f19
560         ls -l $DIR
561         rm $DIR/f19
562         $CHECKSTAT -a $DIR/f19 || error
563 }
564 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
565
566 test_19b() {
567         ls -l $DIR/f19 && error || true
568 }
569 run_test 19b "ls -l .../f19 (should return error) =============="
570
571 test_19c() {
572         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
573         $RUNAS touch $DIR/f19 && error || true
574 }
575 run_test 19c "$RUNAS touch .../f19 (should return error) =="
576
577 test_19d() {
578         cat $DIR/f19 && error || true
579 }
580 run_test 19d "cat .../f19 (should return error) =============="
581
582 test_20() {
583         touch $DIR/f
584         rm $DIR/f
585         log "1 done"
586         touch $DIR/f
587         rm $DIR/f
588         log "2 done"
589         touch $DIR/f
590         rm $DIR/f
591         log "3 done"
592         $CHECKSTAT -a $DIR/f || error
593 }
594 run_test 20 "touch .../f ; ls -l ... ==========================="
595
596 test_21() {
597         mkdir $DIR/d21
598         [ -f $DIR/d21/dangle ] && rm -f $DIR/d21/dangle
599         ln -s dangle $DIR/d21/link
600         echo foo >> $DIR/d21/link
601         cat $DIR/d21/dangle
602         $CHECKSTAT -t link $DIR/d21/link || error
603         $CHECKSTAT -f -t file $DIR/d21/link || error
604 }
605 run_test 21 "write to dangling link ============================"
606
607 test_22() {
608         WDIR=$DIR/$tdir
609         mkdir -p $WDIR
610         chown $RUNAS_ID:$RUNAS_GID $WDIR
611         (cd $WDIR || error "cd $WDIR failed";
612         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
613         $RUNAS tar xf -)
614         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
615         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
616         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
617 }
618 run_test 22 "unpack tar archive as non-root user ==============="
619
620 # was test_23
621 test_23a() {
622         mkdir -p $DIR/$tdir
623         local file=$DIR/$tdir/$tfile
624
625         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
626         openfile -f O_CREAT:O_EXCL $file &&
627                 error "$file recreate succeeded" || true
628 }
629 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
630
631 test_23b() { # bug 18988
632         mkdir -p $DIR/$tdir
633         local file=$DIR/$tdir/$tfile
634
635         rm -f $file
636         echo foo > $file || error "write filed"
637         echo bar >> $file || error "append filed"
638         $CHECKSTAT -s 8 $file || error "wrong size"
639         rm $file
640 }
641 run_test 23b "O_APPEND check =========================="
642
643 test_24a() {
644         echo '== rename sanity =============================================='
645         echo '-- same directory rename'
646         mkdir $DIR/R1
647         touch $DIR/R1/f
648         mv $DIR/R1/f $DIR/R1/g
649         $CHECKSTAT -t file $DIR/R1/g || error
650 }
651 run_test 24a "touch .../R1/f; rename .../R1/f .../R1/g ========="
652
653 test_24b() {
654         mkdir $DIR/R2
655         touch $DIR/R2/{f,g}
656         mv $DIR/R2/f $DIR/R2/g
657         $CHECKSTAT -a $DIR/R2/f || error
658         $CHECKSTAT -t file $DIR/R2/g || error
659 }
660 run_test 24b "touch .../R2/{f,g}; rename .../R2/f .../R2/g ====="
661
662 test_24c() {
663         mkdir $DIR/R3
664         mkdir $DIR/R3/f
665         mv $DIR/R3/f $DIR/R3/g
666         $CHECKSTAT -a $DIR/R3/f || error
667         $CHECKSTAT -t dir $DIR/R3/g || error
668 }
669 run_test 24c "mkdir .../R3/f; rename .../R3/f .../R3/g ========="
670
671 test_24d() {
672         mkdir $DIR/R4
673         mkdir $DIR/R4/{f,g}
674         mrename $DIR/R4/f $DIR/R4/g
675         $CHECKSTAT -a $DIR/R4/f || error
676         $CHECKSTAT -t dir $DIR/R4/g || error
677 }
678 run_test 24d "mkdir .../R4/{f,g}; rename .../R4/f .../R4/g ====="
679
680 test_24e() {
681         echo '-- cross directory renames --'
682         mkdir $DIR/R5{a,b}
683         touch $DIR/R5a/f
684         mv $DIR/R5a/f $DIR/R5b/g
685         $CHECKSTAT -a $DIR/R5a/f || error
686         $CHECKSTAT -t file $DIR/R5b/g || error
687 }
688 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
689
690 test_24f() {
691         mkdir $DIR/R6{a,b}
692         touch $DIR/R6a/f $DIR/R6b/g
693         mv $DIR/R6a/f $DIR/R6b/g
694         $CHECKSTAT -a $DIR/R6a/f || error
695         $CHECKSTAT -t file $DIR/R6b/g || error
696 }
697 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
698
699 test_24g() {
700         mkdir $DIR/R7{a,b}
701         mkdir $DIR/R7a/d
702         mv $DIR/R7a/d $DIR/R7b/e
703         $CHECKSTAT -a $DIR/R7a/d || error
704         $CHECKSTAT -t dir $DIR/R7b/e || error
705 }
706 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
707
708 test_24h() {
709         mkdir $DIR/R8{a,b}
710         mkdir $DIR/R8a/d $DIR/R8b/e
711         mrename $DIR/R8a/d $DIR/R8b/e
712         $CHECKSTAT -a $DIR/R8a/d || error
713         $CHECKSTAT -t dir $DIR/R8b/e || error
714 }
715 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
716
717 test_24i() {
718         echo "-- rename error cases"
719         mkdir $DIR/R9
720         mkdir $DIR/R9/a
721         touch $DIR/R9/f
722         mrename $DIR/R9/f $DIR/R9/a
723         $CHECKSTAT -t file $DIR/R9/f || error
724         $CHECKSTAT -t dir  $DIR/R9/a || error
725         $CHECKSTAT -a $DIR/R9/a/f || error
726 }
727 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
728
729 test_24j() {
730         mkdir $DIR/R10
731         mrename $DIR/R10/f $DIR/R10/g
732         $CHECKSTAT -t dir $DIR/R10 || error
733         $CHECKSTAT -a $DIR/R10/f || error
734         $CHECKSTAT -a $DIR/R10/g || error
735 }
736 run_test 24j "source does not exist ============================"
737
738 test_24k() {
739         mkdir $DIR/R11a $DIR/R11a/d
740         touch $DIR/R11a/f
741         mv $DIR/R11a/f $DIR/R11a/d
742         $CHECKSTAT -a $DIR/R11a/f || error
743         $CHECKSTAT -t file $DIR/R11a/d/f || error
744 }
745 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
746
747 # bug 2429 - rename foo foo foo creates invalid file
748 test_24l() {
749         f="$DIR/f24l"
750         $MULTIOP $f OcNs || error
751 }
752 run_test 24l "Renaming a file to itself ========================"
753
754 test_24m() {
755         f="$DIR/f24m"
756         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
757         # on ext3 this does not remove either the source or target files
758         # though the "expected" operation would be to remove the source
759         $CHECKSTAT -t file ${f} || error "${f} missing"
760         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
761 }
762 run_test 24m "Renaming a file to a hard link to itself ========="
763
764 test_24n() {
765     f="$DIR/f24n"
766     # this stats the old file after it was renamed, so it should fail
767     touch ${f}
768     $CHECKSTAT ${f}
769     mv ${f} ${f}.rename
770     $CHECKSTAT ${f}.rename
771     $CHECKSTAT -a ${f}
772 }
773 run_test 24n "Statting the old file after renaming (Posix rename 2)"
774
775 test_24o() {
776         check_kernel_version 37 || return 0
777         mkdir -p $DIR/d24o
778         rename_many -s random -v -n 10 $DIR/d24o
779 }
780 run_test 24o "rename of files during htree split ==============="
781
782 test_24p() {
783         mkdir $DIR/R12{a,b}
784         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
785         mrename $DIR/R12a $DIR/R12b
786         $CHECKSTAT -a $DIR/R12a || error
787         $CHECKSTAT -t dir $DIR/R12b || error
788         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
789         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
790 }
791 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
792
793 test_24q() {
794         mkdir $DIR/R13{a,b}
795         DIRINO=`ls -lid $DIR/R13a | awk '{ print $1 }'`
796         multiop_bg_pause $DIR/R13b D_c || return 1
797         MULTIPID=$!
798
799         mrename $DIR/R13a $DIR/R13b
800         $CHECKSTAT -a $DIR/R13a || error
801         $CHECKSTAT -t dir $DIR/R13b || error
802         DIRINO2=`ls -lid $DIR/R13b | awk '{ print $1 }'`
803         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
804         kill -USR1 $MULTIPID
805         wait $MULTIPID || error "multiop close failed"
806 }
807 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
808
809 test_24r() { #bug 3789
810         mkdir $DIR/R14a $DIR/R14a/b
811         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
812         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
813         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
814 }
815 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
816
817 test_24s() {
818         mkdir $DIR/R15a $DIR/R15a/b $DIR/R15a/b/c
819         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
820         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
821         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
822 }
823 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
824 test_24t() {
825         mkdir $DIR/R16a $DIR/R16a/b $DIR/R16a/b/c
826         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
827         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
828         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
829 }
830 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
831
832 test_24u() { # bug12192
833         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
834         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
835 }
836 run_test 24u "create stripe file"
837
838 page_size() {
839         getconf PAGE_SIZE
840 }
841
842 test_24v() {
843         local NRFILES=100000
844         local FREE_INODES=`lfs df -i|grep "filesystem summary" | awk '{print $5}'`
845         [ $FREE_INODES -lt $NRFILES ] && \
846                 skip "not enough free inodes $FREE_INODES required $NRFILES" && \
847                 return
848
849         mkdir -p $DIR/d24v
850         createmany -m $DIR/d24v/$tfile $NRFILES
851
852         cancel_lru_locks mdc
853         lctl set_param mdc.*.stats clear
854
855         ls $DIR/d24v >/dev/null || error "error in listing large dir"
856
857         # LU-5 large readdir
858         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
859         #               8 bytes for name(filename is mostly 5 in this test) +
860         #               8 bytes for luda_type
861         # take into account of overhead in lu_dirpage header and end mark in
862         # each page, plus one in RPC_NUM calculation.
863         DIRENT_SIZE=48
864         RPC_SIZE=$(($(lctl get_param -n mdc.*.max_pages_per_rpc)*$(page_size)))
865         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
866         mds_readpage=`lctl get_param mdc.*.stats | \
867                                 awk '/^mds_readpage/ {print $2}'`
868         [ $mds_readpage -gt $RPC_NUM ] && \
869                 error "large readdir doesn't take effect"
870
871         rm $DIR/d24v -rf
872 }
873 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
874
875 test_24w() { # bug21506
876         SZ1=234852
877         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
878         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
879         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
880         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
881         [ "$SZ1" = "$SZ2" ] || \
882                 error "Error reading at the end of the file $tfile"
883 }
884 run_test 24w "Reading a file larger than 4Gb"
885
886 test_25a() {
887         echo '== symlink sanity ============================================='
888
889         mkdir $DIR/d25
890         ln -s d25 $DIR/s25
891         touch $DIR/s25/foo || error
892 }
893 run_test 25a "create file in symlinked directory ==============="
894
895 test_25b() {
896         [ ! -d $DIR/d25 ] && test_25a
897         $CHECKSTAT -t file $DIR/s25/foo || error
898 }
899 run_test 25b "lookup file in symlinked directory ==============="
900
901 test_26a() {
902         mkdir $DIR/d26
903         mkdir $DIR/d26/d26-2
904         ln -s d26/d26-2 $DIR/s26
905         touch $DIR/s26/foo || error
906 }
907 run_test 26a "multiple component symlink ======================="
908
909 test_26b() {
910         mkdir -p $DIR/d26b/d26-2
911         ln -s d26b/d26-2/foo $DIR/s26-2
912         touch $DIR/s26-2 || error
913 }
914 run_test 26b "multiple component symlink at end of lookup ======"
915
916 test_26c() {
917         mkdir $DIR/d26.2
918         touch $DIR/d26.2/foo
919         ln -s d26.2 $DIR/s26.2-1
920         ln -s s26.2-1 $DIR/s26.2-2
921         ln -s s26.2-2 $DIR/s26.2-3
922         chmod 0666 $DIR/s26.2-3/foo
923 }
924 run_test 26c "chain of symlinks ================================"
925
926 # recursive symlinks (bug 439)
927 test_26d() {
928         ln -s d26-3/foo $DIR/d26-3
929 }
930 run_test 26d "create multiple component recursive symlink ======"
931
932 test_26e() {
933         [ ! -h $DIR/d26-3 ] && test_26d
934         rm $DIR/d26-3
935 }
936 run_test 26e "unlink multiple component recursive symlink ======"
937
938 # recursive symlinks (bug 7022)
939 test_26f() {
940         mkdir -p $DIR/$tdir
941         mkdir $DIR/$tdir/$tfile        || error "mkdir $DIR/$tdir/$tfile failed"
942         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
943         mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
944         mkdir $tfile             || error "mkdir $tfile failed"
945         cd $tfile                || error "cd $tfile failed"
946         ln -s .. dotdot          || error "ln dotdot failed"
947         ln -s dotdot/lndir lndir || error "ln lndir failed"
948         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
949         output=`ls $tfile/$tfile/lndir/bar1`
950         [ "$output" = bar1 ] && error "unexpected output"
951         rm -r $tfile             || error "rm $tfile failed"
952         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
953 }
954 run_test 26f "rm -r of a directory which has recursive symlink ="
955
956 test_27a() {
957         echo '== stripe sanity =============================================='
958         mkdir -p $DIR/d27 || error "mkdir failed"
959         $GETSTRIPE $DIR/d27
960         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
961         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
962         pass
963         log "== test_27a: write to one stripe file ========================="
964         cp /etc/hosts $DIR/d27/f0 || error
965 }
966 run_test 27a "one stripe file =================================="
967
968 test_27b() {
969         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
970         mkdir -p $DIR/d27
971         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
972         $GETSTRIPE -c $DIR/d27/f01
973         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
974                 error "two-stripe file doesn't have two stripes"
975 }
976 run_test 27b "create two stripe file"
977
978 test_27c() {
979         [ -f $DIR/d27/f01 ] || skip "test_27b not run" && return
980
981         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
982 }
983 run_test 27c "write to two stripe file"
984
985 test_27d() {
986         mkdir -p $DIR/d27
987         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
988         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
989         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
990 }
991 run_test 27d "create file with default settings ================"
992
993 test_27e() {
994         mkdir -p $DIR/d27
995         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
996         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
997         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
998 }
999 run_test 27e "setstripe existing file (should return error) ======"
1000
1001 test_27f() {
1002         mkdir -p $DIR/d27
1003         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1004         dd if=/dev/zero of=$DIR/d27/f12 bs=4k count=4 || error "dd failed"
1005         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1006 }
1007 run_test 27f "setstripe with bad stripe size (should return error)"
1008
1009 test_27g() {
1010         mkdir -p $DIR/d27
1011         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1012         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1013                 error "$DIR/d27/fnone has object"
1014 }
1015 run_test 27g "$GETSTRIPE with no objects"
1016
1017 test_27i() {
1018         touch $DIR/d27/fsome || error "touch failed"
1019         [ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ] || error "missing objects"
1020 }
1021 run_test 27i "$GETSTRIPE with some objects"
1022
1023 test_27j() {
1024         mkdir -p $DIR/d27
1025         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1026 }
1027 run_test 27j "setstripe with bad stripe offset (should return error)"
1028
1029 test_27k() { # bug 2844
1030         mkdir -p $DIR/d27
1031         FILE=$DIR/d27/f27k
1032         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1033         [ ! -d $DIR/d27 ] && mkdir -p $DIR/d27
1034         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1035         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1036         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1037         dd if=/dev/zero of=$FILE bs=4k count=1
1038         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1039         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "$BLKSIZE > $LL_MAX_BLKSIZE"
1040 }
1041 run_test 27k "limit i_blksize for broken user apps ============="
1042
1043 test_27l() {
1044         mkdir -p $DIR/d27
1045         mcreate $DIR/f27l || error "creating file"
1046         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1047                 error "setstripe should have failed" || true
1048 }
1049 run_test 27l "check setstripe permissions (should return error)"
1050
1051 test_27m() {
1052         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1053         if [ $ORIGFREE -gt $MAXFREE ]; then
1054                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1055                 return
1056         fi
1057         mkdir -p $DIR/d27
1058         $SETSTRIPE -i 0 -c 1 $DIR/d27/f27m_1
1059         dd if=/dev/zero of=$DIR/d27/f27m_1 bs=1024 count=$MAXFREE &&
1060                 error "dd should fill OST0"
1061         i=2
1062         while $SETSTRIPE -i 0 -c 1 $DIR/d27/f27m_$i; do
1063                 i=`expr $i + 1`
1064                 [ $i -gt 256 ] && break
1065         done
1066         i=`expr $i + 1`
1067         touch $DIR/d27/f27m_$i
1068         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
1069                 error "OST0 was full but new created file still use it"
1070         i=`expr $i + 1`
1071         touch $DIR/d27/f27m_$i
1072         [ `$GETSTRIPE $DIR/d27/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] && \
1073                 error "OST0 was full but new created file still use it"
1074         rm -r $DIR/d27
1075         sleep 15
1076 }
1077 run_test 27m "create file while OST0 was full =================="
1078
1079 sleep_maxage() {
1080         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1081         sleep $DELAY
1082 }
1083
1084 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1085 # if the OST isn't full anymore.
1086 reset_enospc() {
1087         local OSTIDX=${1:-""}
1088
1089         local list=$(comma_list $(osts_nodes))
1090         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1091
1092         do_nodes $list lctl set_param fail_loc=0
1093         sync    # initiate all OST_DESTROYs from MDS to OST
1094         sleep_maxage
1095 }
1096
1097 exhaust_precreations() {
1098         local OSTIDX=$1
1099         local FAILLOC=$2
1100         local FAILIDX=${3:-$OSTIDX}
1101
1102         mkdir -p $DIR/$tdir
1103         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1104         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1105
1106         local OST=$(ostname_from_index $OSTIDX)
1107         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1108                           sed -e 's/_UUID$//;s/^.*-//')
1109
1110         # on the mdt's osc
1111         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1112         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1113         osc.$mdtosc_proc1.prealloc_last_id)
1114         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1115         osc.$mdtosc_proc1.prealloc_next_id)
1116
1117         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1118         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1119
1120         mkdir -p $DIR/$tdir/${OST}
1121         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1122 #define OBD_FAIL_OST_ENOSPC              0x215
1123         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1124         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1125         echo "Creating to objid $last_id on ost $OST..."
1126         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1127         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1128         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1129         sleep_maxage
1130 }
1131
1132 exhaust_all_precreations() {
1133         local i
1134         for (( i=0; i < OSTCOUNT; i++ )) ; do
1135                 exhaust_precreations $i $1 -1
1136         done
1137 }
1138
1139 test_27n() {
1140         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1141         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1142         remote_ost_nodsh && skip "remote OST with nodsh" && return
1143
1144         reset_enospc
1145         rm -f $DIR/$tdir/$tfile
1146         exhaust_precreations 0 0x80000215
1147         $SETSTRIPE -c -1 $DIR/$tdir
1148         touch $DIR/$tdir/$tfile || error
1149         $GETSTRIPE $DIR/$tdir/$tfile
1150         reset_enospc
1151 }
1152 run_test 27n "create file with some full OSTs =================="
1153
1154 test_27o() {
1155         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1156         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1157         remote_ost_nodsh && skip "remote OST with nodsh" && return
1158
1159         reset_enospc
1160         rm -f $DIR/$tdir/$tfile
1161         exhaust_all_precreations 0x215
1162
1163         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1164
1165         reset_enospc
1166         rm -rf $DIR/$tdir/*
1167 }
1168 run_test 27o "create file with all full OSTs (should error) ===="
1169
1170 test_27p() {
1171         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1172         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1173         remote_ost_nodsh && skip "remote OST with nodsh" && return
1174
1175         reset_enospc
1176         rm -f $DIR/$tdir/$tfile
1177         mkdir -p $DIR/$tdir
1178
1179         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1180         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1181         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1182
1183         exhaust_precreations 0 0x80000215
1184         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1185         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1186         $GETSTRIPE $DIR/$tdir/$tfile
1187
1188         reset_enospc
1189 }
1190 run_test 27p "append to a truncated file with some full OSTs ==="
1191
1192 test_27q() {
1193         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1194         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1195         remote_ost_nodsh && skip "remote OST with nodsh" && return
1196
1197         reset_enospc
1198         rm -f $DIR/$tdir/$tfile
1199
1200         mkdir -p $DIR/$tdir
1201         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1202         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1203         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1204
1205         exhaust_all_precreations 0x215
1206
1207         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1208         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1209
1210         reset_enospc
1211 }
1212 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1213
1214 test_27r() {
1215         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1216         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1217         remote_ost_nodsh && skip "remote OST with nodsh" && return
1218
1219         reset_enospc
1220         rm -f $DIR/$tdir/$tfile
1221         exhaust_precreations 0 0x80000215
1222
1223         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1224
1225         reset_enospc
1226 }
1227 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1228
1229 test_27s() { # bug 10725
1230         mkdir -p $DIR/$tdir
1231         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1232         local stripe_count=0
1233         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1234         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1235                 error "stripe width >= 2^32 succeeded" || true
1236
1237 }
1238 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1239
1240 test_27t() { # bug 10864
1241         WDIR=`pwd`
1242         WLFS=`which lfs`
1243         cd $DIR
1244         touch $tfile
1245         $WLFS getstripe $tfile
1246         cd $WDIR
1247 }
1248 run_test 27t "check that utils parse path correctly"
1249
1250 test_27u() { # bug 4900
1251         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1252         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1253
1254 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1255         do_facet $SINGLEMDS lctl set_param fail_loc=0x139
1256         mkdir -p $DIR/$tdir
1257         createmany -o $DIR/$tdir/t- 1000
1258         do_facet $SINGLEMDS lctl set_param fail_loc=0
1259
1260         TLOG=$DIR/$tfile.getstripe
1261         $GETSTRIPE $DIR/$tdir > $TLOG
1262         OBJS=`awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj;}' $TLOG`
1263         unlinkmany $DIR/$tdir/t- 1000
1264         [ $OBJS -gt 0 ] && \
1265                 error "$OBJS objects created on OST-0.  See $TLOG" || pass
1266 }
1267 run_test 27u "skip object creation on OSC w/o objects =========="
1268
1269 test_27v() { # bug 4900
1270         [ "$OSTCOUNT" -lt "2" ] && skip_env "too few OSTs" && return
1271         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1272         remote_ost_nodsh && skip "remote OST with nodsh" && return
1273
1274         exhaust_all_precreations 0x215
1275         reset_enospc
1276
1277         mkdir -p $DIR/$tdir
1278         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1279
1280         touch $DIR/$tdir/$tfile
1281         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1282         # all except ost1
1283         for (( i=1; i < OSTCOUNT; i++ )); do
1284                 do_facet ost$i lctl set_param fail_loc=0x705
1285         done
1286         local START=`date +%s`
1287         createmany -o $DIR/$tdir/$tfile 32
1288
1289         local FINISH=`date +%s`
1290         local TIMEOUT=`lctl get_param -n timeout`
1291         local PROCESS=$((FINISH - START))
1292         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1293                error "$FINISH - $START >= $TIMEOUT / 2"
1294         sleep $((TIMEOUT / 2 - PROCESS))
1295         reset_enospc
1296 }
1297 run_test 27v "skip object creation on slow OST ================="
1298
1299 test_27w() { # bug 10997
1300         mkdir -p $DIR/$tdir || error "mkdir failed"
1301         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1302         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1303                 error "stripe size $size != 65536" || true
1304         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1305                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1306 }
1307 run_test 27w "check $SETSTRIPE -S option"
1308
1309 test_27wa() {
1310         [ "$OSTCOUNT" -lt "2" ] &&
1311                 skip_env "skipping multiple stripe count/offset test" && return
1312
1313         mkdir -p $DIR/$tdir || error "mkdir failed"
1314         for i in $(seq 1 $OSTCOUNT); do
1315                 offset=$((i - 1))
1316                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1317                         error "setstripe -c $i -i $offset failed"
1318                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1319                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1320                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1321                 [ $index -ne $offset ] &&
1322                         error "stripe offset $index != $offset" || true
1323         done
1324 }
1325 run_test 27wa "check $SETSTRIPE -c -i options"
1326
1327 test_27x() {
1328         remote_ost_nodsh && skip "remote OST with nodsh" && return
1329         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
1330         OFFSET=$(($OSTCOUNT - 1))
1331         OSTIDX=0
1332         local OST=$(ostname_from_index $OSTIDX)
1333
1334         mkdir -p $DIR/$tdir
1335         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1336         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1337         sleep_maxage
1338         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1339         for i in `seq 0 $OFFSET`; do
1340                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1341                 error "OST0 was degraded but new created file still use it"
1342         done
1343         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1344 }
1345 run_test 27x "create files while OST0 is degraded"
1346
1347 test_27y() {
1348         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1349         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1350         remote_ost_nodsh && skip "remote OST with nodsh" && return
1351
1352         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1353         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1354             osc.$mdtosc.prealloc_last_id)
1355         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1356             osc.$mdtosc.prealloc_next_id)
1357         local fcount=$((last_id - next_id))
1358         [ $fcount -eq 0 ] && skip "not enough space on OST0" && return
1359         [ $fcount -gt $OSTCOUNT ] && fcount=$OSTCOUNT
1360
1361         MDS_OSCS=`do_facet $SINGLEMDS lctl dl | awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
1362         OFFSET=$(($OSTCOUNT-1))
1363         OST=-1
1364         for OSC in $MDS_OSCS; do
1365                 if [ $OST == -1 ]; then {
1366                         OST=`osc_to_ost $OSC`
1367                 } else {
1368                         echo $OSC "is Deactivate:"
1369                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1370                 } fi
1371         done
1372
1373         OSTIDX=$(index_from_ostuuid $OST)
1374         mkdir -p $DIR/$tdir
1375         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1376
1377         do_facet ost$((OSTIDX+1)) lctl set_param -n obdfilter.$OST.degraded 1
1378         sleep_maxage
1379         createmany -o $DIR/$tdir/$tfile $fcount
1380         do_facet ost$((OSTIDX+1)) lctl set_param -n obdfilter.$OST.degraded 0
1381
1382         for i in `seq 0 $OFFSET`; do
1383                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "$OSTIDX"` ] || \
1384                       error "files created on deactivated OSTs instead of degraded OST"
1385         done
1386         for OSC in $MDS_OSCS; do
1387                 [ `osc_to_ost $OSC` != $OST  ] && {
1388                         echo $OSC "is activate"
1389                         do_facet $SINGLEMDS lctl --device %$OSC activate
1390                 }
1391         done
1392 }
1393 run_test 27y "create files while OST0 is degraded and the rest inactive"
1394
1395 check_seq_oid()
1396 {
1397         log "check file $1"
1398
1399         lmm_count=$($GETSTRIPE -c $1)
1400         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1401         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1402
1403         local old_ifs="$IFS"
1404         IFS=$'[:]'
1405         fid=($($LFS path2fid $1))
1406         IFS="$old_ifs"
1407
1408         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1409         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1410
1411         # compare lmm_seq and lu_fid->f_seq
1412         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1413         # compare lmm_object_id and lu_fid->oid
1414         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1415
1416         # check the trusted.fid attribute of the OST objects of the file
1417         local have_obdidx=false
1418         local stripe_nr=0
1419         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1420                 # skip lines up to and including "obdidx"
1421                 [ -z "$obdidx" ] && break
1422                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1423                 $have_obdidx || continue
1424
1425                 local ost=$((obdidx + 1))
1426                 local dev=$(ostdevname $ost)
1427
1428                 if [ $(facet_fstype ost$ost) != ldiskfs ]; then
1429                         echo "Currently only works with ldiskfs-based OSTs"
1430                         continue
1431                 fi
1432
1433                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1434
1435                 #don't unmount/remount the OSTs if we don't need to do that
1436                 #local dir=$(facet_mntpt ost$ost)
1437                 #stop ost$dev
1438                 #do_facet ost$dev mount -t $FSTYPE $dev $dir $OST_MOUNT_OPTS ||
1439                 #       { error "mounting $dev as $FSTYPE failed"; return 3; }
1440                 #local obj_file=$(do_facet ost$ost find $dir/O/$seq -name $oid)
1441                 #local ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID $obj_file)
1442
1443                 local obj_file="O/$seq/d$((oid %32))/$oid"
1444                 local ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1445                            $dev 2>/dev/null" | grep "parent=")
1446
1447                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1448
1449                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1450
1451                 #do_facet ost$ost umount -d $dir
1452                 #start ost$ost $dev $OST_MOUNT_OPTS
1453
1454                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1455                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1456                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1457                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1458                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1459                 local ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1460
1461                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1462                 [ $ff_pseq = $lmm_seq ] ||
1463                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1464                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1465                 [ $ff_poid = $lmm_oid ] ||
1466                         error "FF parent OID $ff_poid != $lmm_oid"
1467                 [ $ff_pstripe = $stripe_nr ] ||
1468                         error "FF stripe $ff_pstripe != $stripe_nr"
1469
1470                 stripe_nr=$((stripe_nr + 1))
1471         done
1472 }
1473
1474 test_27z() {
1475         remote_ost_nodsh && skip "remote OST with nodsh" && return
1476         mkdir -p $DIR/$tdir
1477
1478         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1479                 { error "setstripe -c -1 failed"; return 1; }
1480         # We need to send a write to every object to get parent FID info set.
1481         # This _should_ also work for setattr, but does not currently.
1482         # touch $DIR/$tdir/$tfile-1 ||
1483         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1484                 { error "dd $tfile-1 failed"; return 2; }
1485         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1486                 { error "setstripe -c -1 failed"; return 3; }
1487         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1488                 { error "dd $tfile-2 failed"; return 4; }
1489
1490         # make sure write RPCs have been sent to OSTs
1491         sync; sleep 5; sync
1492
1493         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1494         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1495 }
1496 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1497
1498 test_27A() { # b=19102
1499         local restore_size=$($GETSTRIPE -S $MOUNT)
1500         local restore_count=$($GETSTRIPE -c $MOUNT)
1501         local restore_offset=$($GETSTRIPE -i $MOUNT)
1502         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1503         local default_size=$($GETSTRIPE -S $MOUNT)
1504         local default_count=$($GETSTRIPE -c $MOUNT)
1505         local default_offset=$($GETSTRIPE -i $MOUNT)
1506         local dsize=$((1024 * 1024))
1507         [ $default_size -eq $dsize ] ||
1508                 error "stripe size $default_size != $dsize"
1509         [ $default_count -eq 1 ] || error "stripe count $default_count != 1"
1510         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1511         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1512 }
1513 run_test 27A "check filesystem-wide default LOV EA values"
1514
1515 # createtest also checks that device nodes are created and
1516 # then visible correctly (#2091)
1517 test_28() { # bug 2091
1518         mkdir $DIR/d28
1519         $CREATETEST $DIR/d28/ct || error
1520 }
1521 run_test 28 "create/mknod/mkdir with bad file types ============"
1522
1523 test_29() {
1524         cancel_lru_locks mdc
1525         mkdir $DIR/d29
1526         touch $DIR/d29/foo
1527         log 'first d29'
1528         ls -l $DIR/d29
1529
1530         declare -i LOCKCOUNTORIG=0
1531         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1532                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1533         done
1534         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1535
1536         declare -i LOCKUNUSEDCOUNTORIG=0
1537         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1538                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1539         done
1540
1541         log 'second d29'
1542         ls -l $DIR/d29
1543         log 'done'
1544
1545         declare -i LOCKCOUNTCURRENT=0
1546         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1547                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1548         done
1549
1550         declare -i LOCKUNUSEDCOUNTCURRENT=0
1551         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1552                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1553         done
1554
1555         if [ "$LOCKCOUNTCURRENT" -gt "$LOCKCOUNTORIG" ]; then
1556                 lctl set_param -n ldlm.dump_namespaces ""
1557                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
1558                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1559                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1560                 return 2
1561         fi
1562         if [ "$LOCKUNUSEDCOUNTCURRENT" -gt "$LOCKUNUSEDCOUNTORIG" ]; then
1563                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
1564                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
1565                 log "dumped log to $TMP/test_29.dk (bug 5793)"
1566                 return 3
1567         fi
1568 }
1569 run_test 29 "IT_GETATTR regression  ============================"
1570
1571 test_30a() { # was test_30
1572         cp `which ls` $DIR || cp /bin/ls $DIR
1573         $DIR/ls / || error
1574         rm $DIR/ls
1575 }
1576 run_test 30a "execute binary from Lustre (execve) =============="
1577
1578 test_30b() {
1579         cp `which ls` $DIR || cp /bin/ls $DIR
1580         chmod go+rx $DIR/ls
1581         $RUNAS $DIR/ls / || error
1582         rm $DIR/ls
1583 }
1584 run_test 30b "execute binary from Lustre as non-root ==========="
1585
1586 test_30c() { # b=22376
1587         cp `which ls` $DIR || cp /bin/ls $DIR
1588         chmod a-rw $DIR/ls
1589         cancel_lru_locks mdc
1590         cancel_lru_locks osc
1591         $RUNAS $DIR/ls / || error
1592         rm -f $DIR/ls
1593 }
1594 run_test 30c "execute binary from Lustre without read perms ===="
1595
1596 test_31a() {
1597         $OPENUNLINK $DIR/f31 $DIR/f31 || error
1598         $CHECKSTAT -a $DIR/f31 || error
1599 }
1600 run_test 31a "open-unlink file =================================="
1601
1602 test_31b() {
1603         touch $DIR/f31 || error
1604         ln $DIR/f31 $DIR/f31b || error
1605         $MULTIOP $DIR/f31b Ouc || error
1606         $CHECKSTAT -t file $DIR/f31 || error
1607 }
1608 run_test 31b "unlink file with multiple links while open ======="
1609
1610 test_31c() {
1611         touch $DIR/f31 || error
1612         ln $DIR/f31 $DIR/f31c || error
1613         multiop_bg_pause $DIR/f31 O_uc || return 1
1614         MULTIPID=$!
1615         $MULTIOP $DIR/f31c Ouc
1616         kill -USR1 $MULTIPID
1617         wait $MULTIPID
1618 }
1619 run_test 31c "open-unlink file with multiple links ============="
1620
1621 test_31d() {
1622         opendirunlink $DIR/d31d $DIR/d31d || error
1623         $CHECKSTAT -a $DIR/d31d || error
1624 }
1625 run_test 31d "remove of open directory ========================="
1626
1627 test_31e() { # bug 2904
1628         check_kernel_version 34 || return 0
1629         openfilleddirunlink $DIR/d31e || error
1630 }
1631 run_test 31e "remove of open non-empty directory ==============="
1632
1633 test_31f() { # bug 4554
1634         set -vx
1635         mkdir $DIR/d31f
1636         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1637         cp /etc/hosts $DIR/d31f
1638         ls -l $DIR/d31f
1639         $GETSTRIPE $DIR/d31f/hosts
1640         multiop_bg_pause $DIR/d31f D_c || return 1
1641         MULTIPID=$!
1642
1643         rm -rv $DIR/d31f || error "first of $DIR/d31f"
1644         mkdir $DIR/d31f
1645         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
1646         cp /etc/hosts $DIR/d31f
1647         ls -l $DIR/d31f
1648         $GETSTRIPE $DIR/d31f/hosts
1649         multiop_bg_pause $DIR/d31f D_c || return 1
1650         MULTIPID2=$!
1651
1652         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
1653         wait $MULTIPID || error "first opendir $MULTIPID failed"
1654
1655         sleep 6
1656
1657         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
1658         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
1659         set +vx
1660 }
1661 run_test 31f "remove of open directory with open-unlink file ==="
1662
1663 test_31g() {
1664         echo "-- cross directory link --"
1665         mkdir $DIR/d31g{a,b}
1666         touch $DIR/d31ga/f
1667         ln $DIR/d31ga/f $DIR/d31gb/g
1668         $CHECKSTAT -t file $DIR/d31ga/f || error "source"
1669         [ `stat -c%h $DIR/d31ga/f` == '2' ] || error "source nlink"
1670         $CHECKSTAT -t file $DIR/d31gb/g || error "target"
1671         [ `stat -c%h $DIR/d31gb/g` == '2' ] || error "target nlink"
1672 }
1673 run_test 31g "cross directory link==============="
1674
1675 test_31h() {
1676         echo "-- cross directory link --"
1677         mkdir $DIR/d31h
1678         mkdir $DIR/d31h/dir
1679         touch $DIR/d31h/f
1680         ln $DIR/d31h/f $DIR/d31h/dir/g
1681         $CHECKSTAT -t file $DIR/d31h/f || error "source"
1682         [ `stat -c%h $DIR/d31h/f` == '2' ] || error "source nlink"
1683         $CHECKSTAT -t file $DIR/d31h/dir/g || error "target"
1684         [ `stat -c%h $DIR/d31h/dir/g` == '2' ] || error "target nlink"
1685 }
1686 run_test 31h "cross directory link under child==============="
1687
1688 test_31i() {
1689         echo "-- cross directory link --"
1690         mkdir $DIR/d31i
1691         mkdir $DIR/d31i/dir
1692         touch $DIR/d31i/dir/f
1693         ln $DIR/d31i/dir/f $DIR/d31i/g
1694         $CHECKSTAT -t file $DIR/d31i/dir/f || error "source"
1695         [ `stat -c%h $DIR/d31i/dir/f` == '2' ] || error "source nlink"
1696         $CHECKSTAT -t file $DIR/d31i/g || error "target"
1697         [ `stat -c%h $DIR/d31i/g` == '2' ] || error "target nlink"
1698 }
1699 run_test 31i "cross directory link under parent==============="
1700
1701
1702 test_31j() {
1703         mkdir $DIR/d31j
1704         mkdir $DIR/d31j/dir1
1705         ln $DIR/d31j/dir1 $DIR/d31j/dir2 && error "ln for dir"
1706         link $DIR/d31j/dir1 $DIR/d31j/dir3 && error "link for dir"
1707         mlink $DIR/d31j/dir1 $DIR/d31j/dir4 && error "mlink for dir"
1708         mlink $DIR/d31j/dir1 $DIR/d31j/dir1 && error "mlink to the same dir"
1709         return 0
1710 }
1711 run_test 31j "link for directory==============="
1712
1713
1714 test_31k() {
1715         mkdir $DIR/d31k
1716         touch $DIR/d31k/s
1717         touch $DIR/d31k/exist
1718         mlink $DIR/d31k/s $DIR/d31k/t || error "mlink"
1719         mlink $DIR/d31k/s $DIR/d31k/exist && error "mlink to exist file"
1720         mlink $DIR/d31k/s $DIR/d31k/s && error "mlink to the same file"
1721         mlink $DIR/d31k/s $DIR/d31k && error "mlink to parent dir"
1722         mlink $DIR/d31k $DIR/d31k/s && error "mlink parent dir to target"
1723         mlink $DIR/d31k/not-exist $DIR/d31k/foo && error "mlink non-existing to new"
1724         mlink $DIR/d31k/not-exist $DIR/d31k/s && error "mlink non-existing to exist"
1725         return 0
1726 }
1727 run_test 31k "link to file: the same, non-existing, dir==============="
1728
1729 test_31m() {
1730         mkdir $DIR/d31m
1731         touch $DIR/d31m/s
1732         mkdir $DIR/d31m2
1733         touch $DIR/d31m2/exist
1734         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
1735         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
1736         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
1737         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
1738         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
1739         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
1740         return 0
1741 }
1742 run_test 31m "link to file: the same, non-existing, dir==============="
1743
1744 test_32a() {
1745         echo "== more mountpoints and symlinks ================="
1746         [ -e $DIR/d32a ] && rm -fr $DIR/d32a
1747         mkdir -p $DIR/d32a/ext2-mountpoint
1748         mount -t ext2 -o loop $EXT2_DEV $DIR/d32a/ext2-mountpoint || error
1749         $CHECKSTAT -t dir $DIR/d32a/ext2-mountpoint/.. || error
1750         $UMOUNT $DIR/d32a/ext2-mountpoint || error
1751 }
1752 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
1753
1754 test_32b() {
1755         [ -e $DIR/d32b ] && rm -fr $DIR/d32b
1756         mkdir -p $DIR/d32b/ext2-mountpoint
1757         mount -t ext2 -o loop $EXT2_DEV $DIR/d32b/ext2-mountpoint || error
1758         ls -al $DIR/d32b/ext2-mountpoint/.. || error
1759         $UMOUNT $DIR/d32b/ext2-mountpoint || error
1760 }
1761 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
1762
1763 test_32c() {
1764         [ -e $DIR/d32c ] && rm -fr $DIR/d32c
1765         mkdir -p $DIR/d32c/ext2-mountpoint
1766         mount -t ext2 -o loop $EXT2_DEV $DIR/d32c/ext2-mountpoint || error
1767         mkdir -p $DIR/d32c/d2/test_dir
1768         $CHECKSTAT -t dir $DIR/d32c/ext2-mountpoint/../d2/test_dir || error
1769         $UMOUNT $DIR/d32c/ext2-mountpoint || error
1770 }
1771 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
1772
1773 test_32d() {
1774         [ -e $DIR/d32d ] && rm -fr $DIR/d32d
1775         mkdir -p $DIR/d32d/ext2-mountpoint
1776         mount -t ext2 -o loop $EXT2_DEV $DIR/d32d/ext2-mountpoint || error
1777         mkdir -p $DIR/d32d/d2/test_dir
1778         ls -al $DIR/d32d/ext2-mountpoint/../d2/test_dir || error
1779         $UMOUNT $DIR/d32d/ext2-mountpoint || error
1780 }
1781 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
1782
1783 test_32e() {
1784         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
1785         mkdir -p $DIR/d32e/tmp
1786         TMP_DIR=$DIR/d32e/tmp
1787         ln -s $DIR/d32e $TMP_DIR/symlink11
1788         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1789         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
1790         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
1791 }
1792 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
1793
1794 test_32f() {
1795         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
1796         mkdir -p $DIR/d32f/tmp
1797         TMP_DIR=$DIR/d32f/tmp
1798         ln -s $DIR/d32f $TMP_DIR/symlink11
1799         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1800         ls $DIR/d32f/tmp/symlink11  || error
1801         ls $DIR/d32f/symlink01 || error
1802 }
1803 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
1804
1805 test_32g() {
1806         TMP_DIR=$DIR/$tdir/tmp
1807         mkdir -p $TMP_DIR $DIR/${tdir}2
1808         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1809         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1810         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
1811         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
1812         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
1813         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
1814 }
1815 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1816
1817 test_32h() {
1818         rm -fr $DIR/$tdir $DIR/${tdir}2
1819         TMP_DIR=$DIR/$tdir/tmp
1820         mkdir -p $TMP_DIR $DIR/${tdir}2
1821         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
1822         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1823         ls $TMP_DIR/symlink12 || error
1824         ls $DIR/$tdir/symlink02  || error
1825 }
1826 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
1827
1828 test_32i() {
1829         [ -e $DIR/d32i ] && rm -fr $DIR/d32i
1830         mkdir -p $DIR/d32i/ext2-mountpoint
1831         mount -t ext2 -o loop $EXT2_DEV $DIR/d32i/ext2-mountpoint || error
1832         touch $DIR/d32i/test_file
1833         $CHECKSTAT -t file $DIR/d32i/ext2-mountpoint/../test_file || error
1834         $UMOUNT $DIR/d32i/ext2-mountpoint || error
1835 }
1836 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
1837
1838 test_32j() {
1839         [ -e $DIR/d32j ] && rm -fr $DIR/d32j
1840         mkdir -p $DIR/d32j/ext2-mountpoint
1841         mount -t ext2 -o loop $EXT2_DEV $DIR/d32j/ext2-mountpoint || error
1842         touch $DIR/d32j/test_file
1843         cat $DIR/d32j/ext2-mountpoint/../test_file || error
1844         $UMOUNT $DIR/d32j/ext2-mountpoint || error
1845 }
1846 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
1847
1848 test_32k() {
1849         rm -fr $DIR/d32k
1850         mkdir -p $DIR/d32k/ext2-mountpoint
1851         mount -t ext2 -o loop $EXT2_DEV $DIR/d32k/ext2-mountpoint
1852         mkdir -p $DIR/d32k/d2
1853         touch $DIR/d32k/d2/test_file || error
1854         $CHECKSTAT -t file $DIR/d32k/ext2-mountpoint/../d2/test_file || error
1855         $UMOUNT $DIR/d32k/ext2-mountpoint || error
1856 }
1857 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
1858
1859 test_32l() {
1860         rm -fr $DIR/d32l
1861         mkdir -p $DIR/d32l/ext2-mountpoint
1862         mount -t ext2 -o loop $EXT2_DEV $DIR/d32l/ext2-mountpoint || error
1863         mkdir -p $DIR/d32l/d2
1864         touch $DIR/d32l/d2/test_file
1865         cat  $DIR/d32l/ext2-mountpoint/../d2/test_file || error
1866         $UMOUNT $DIR/d32l/ext2-mountpoint || error
1867 }
1868 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
1869
1870 test_32m() {
1871         rm -fr $DIR/d32m
1872         mkdir -p $DIR/d32m/tmp
1873         TMP_DIR=$DIR/d32m/tmp
1874         ln -s $DIR $TMP_DIR/symlink11
1875         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1876         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
1877         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
1878 }
1879 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
1880
1881 test_32n() {
1882         rm -fr $DIR/d32n
1883         mkdir -p $DIR/d32n/tmp
1884         TMP_DIR=$DIR/d32n/tmp
1885         ln -s $DIR $TMP_DIR/symlink11
1886         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
1887         ls -l $DIR/d32n/tmp/symlink11  || error
1888         ls -l $DIR/d32n/symlink01 || error
1889 }
1890 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
1891
1892 test_32o() {
1893         rm -fr $DIR/d32o $DIR/$tfile
1894         touch $DIR/$tfile
1895         mkdir -p $DIR/d32o/tmp
1896         TMP_DIR=$DIR/d32o/tmp
1897         ln -s $DIR/$tfile $TMP_DIR/symlink12
1898         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1899         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
1900         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
1901         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
1902         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
1903 }
1904 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
1905
1906 test_32p() {
1907     log 32p_1
1908         rm -fr $DIR/d32p
1909     log 32p_2
1910         rm -f $DIR/$tfile
1911     log 32p_3
1912         touch $DIR/$tfile
1913     log 32p_4
1914         mkdir -p $DIR/d32p/tmp
1915     log 32p_5
1916         TMP_DIR=$DIR/d32p/tmp
1917     log 32p_6
1918         ln -s $DIR/$tfile $TMP_DIR/symlink12
1919     log 32p_7
1920         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
1921     log 32p_8
1922         cat $DIR/d32p/tmp/symlink12 || error
1923     log 32p_9
1924         cat $DIR/d32p/symlink02 || error
1925     log 32p_10
1926 }
1927 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
1928
1929 test_32q() {
1930         [ -e $DIR/d32q ] && rm -fr $DIR/d32q
1931         mkdir -p $DIR/d32q
1932         touch $DIR/d32q/under_the_mount
1933         mount -t ext2 -o loop $EXT2_DEV $DIR/d32q
1934         ls $DIR/d32q/under_the_mount && error || true
1935         $UMOUNT $DIR/d32q || error
1936 }
1937 run_test 32q "stat follows mountpoints in Lustre (should return error)"
1938
1939 test_32r() {
1940         [ -e $DIR/d32r ] && rm -fr $DIR/d32r
1941         mkdir -p $DIR/d32r
1942         touch $DIR/d32r/under_the_mount
1943         mount -t ext2 -o loop $EXT2_DEV $DIR/d32r
1944         ls $DIR/d32r | grep -q under_the_mount && error || true
1945         $UMOUNT $DIR/d32r || error
1946 }
1947 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
1948
1949 test_33() {
1950         rm -f $DIR/$tfile
1951         touch $DIR/$tfile
1952         chmod 444 $DIR/$tfile
1953         chown $RUNAS_ID $DIR/$tfile
1954         log 33_1
1955         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
1956         log 33_2
1957 }
1958 run_test 33 "write file with mode 444 (should return error) ===="
1959
1960 test_33a() {
1961         rm -fr $DIR/d33
1962         mkdir -p $DIR/d33
1963         chown $RUNAS_ID $DIR/d33
1964         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
1965         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
1966                 error "open RDWR" || true
1967 }
1968 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
1969
1970 test_33b() {
1971         rm -fr $DIR/d33
1972         mkdir -p $DIR/d33
1973         chown $RUNAS_ID $DIR/d33
1974         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
1975 }
1976 run_test 33b "test open file with malformed flags (No panic and return error)"
1977
1978 test_33c() {
1979         local ostnum
1980         local ostname
1981         local write_bytes
1982         local all_zeros
1983
1984         remote_ost_nodsh && skip "remote OST with nodsh" && return
1985         all_zeros=:
1986         rm -fr $DIR/d33
1987         mkdir -p $DIR/d33
1988         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
1989
1990         sync
1991         for ostnum in $(seq $OSTCOUNT); do
1992                 # test-framework's OST numbering is one-based, while Lustre's
1993                 # is zero-based
1994                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
1995                 # Parsing llobdstat's output sucks; we could grep the /proc
1996                 # path, but that's likely to not be as portable as using the
1997                 # llobdstat utility.  So we parse lctl output instead.
1998                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
1999                         obdfilter/$ostname/stats |
2000                         awk '/^write_bytes/ {print $7}' )
2001                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2002                 if (( ${write_bytes:-0} > 0 ))
2003                 then
2004                         all_zeros=false
2005                         break;
2006                 fi
2007         done
2008
2009         $all_zeros || return 0
2010
2011         # Write four bytes
2012         echo foo > $DIR/d33/bar
2013         # Really write them
2014         sync
2015
2016         # Total up write_bytes after writing.  We'd better find non-zeros.
2017         for ostnum in $(seq $OSTCOUNT); do
2018                 ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2019                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2020                         obdfilter/$ostname/stats |
2021                         awk '/^write_bytes/ {print $7}' )
2022                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2023                 if (( ${write_bytes:-0} > 0 ))
2024                 then
2025                         all_zeros=false
2026                         break;
2027                 fi
2028         done
2029
2030         if $all_zeros
2031         then
2032                 for ostnum in $(seq $OSTCOUNT); do
2033                         ostname=$(printf "$FSNAME-OST%.4d" $((ostnum - 1)))
2034                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2035                         do_facet ost$ostnum lctl get_param -n \
2036                                 obdfilter/$ostname/stats
2037                 done
2038                 error "OST not keeping write_bytes stats (b22312)"
2039         fi
2040 }
2041 run_test 33c "test llobdstat and write_bytes"
2042
2043 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2044 test_34a() {
2045         rm -f $DIR/f34
2046         $MCREATE $DIR/f34 || error
2047         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2048         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2049         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2050         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2051 }
2052 run_test 34a "truncate file that has not been opened ==========="
2053
2054 test_34b() {
2055         [ ! -f $DIR/f34 ] && test_34a
2056         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2057         $OPENFILE -f O_RDONLY $DIR/f34
2058         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2059         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2060 }
2061 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2062
2063 test_34c() {
2064         [ ! -f $DIR/f34 ] && test_34a
2065         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2066         $OPENFILE -f O_RDWR $DIR/f34
2067         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2068         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2069 }
2070 run_test 34c "O_RDWR opening file-with-size works =============="
2071
2072 test_34d() {
2073         [ ! -f $DIR/f34 ] && test_34a
2074         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2075         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2076         rm $DIR/f34
2077 }
2078 run_test 34d "write to sparse file ============================="
2079
2080 test_34e() {
2081         rm -f $DIR/f34e
2082         $MCREATE $DIR/f34e || error
2083         $TRUNCATE $DIR/f34e 1000 || error
2084         $CHECKSTAT -s 1000 $DIR/f34e || error
2085         $OPENFILE -f O_RDWR $DIR/f34e
2086         $CHECKSTAT -s 1000 $DIR/f34e || error
2087 }
2088 run_test 34e "create objects, some with size and some without =="
2089
2090 test_34f() { # bug 6242, 6243
2091         SIZE34F=48000
2092         rm -f $DIR/f34f
2093         $MCREATE $DIR/f34f || error
2094         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2095         dd if=$DIR/f34f of=$TMP/f34f
2096         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2097         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2098         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2099         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2100         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2101 }
2102 run_test 34f "read from a file with no objects until EOF ======="
2103
2104 test_34g() {
2105         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2106         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2107         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2108         cancel_lru_locks osc
2109         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2110                 error "wrong size after lock cancel"
2111
2112         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2113         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2114                 error "expanding truncate failed"
2115         cancel_lru_locks osc
2116         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2117                 error "wrong expanded size after lock cancel"
2118 }
2119 run_test 34g "truncate long file ==============================="
2120
2121 test_34h() {
2122         local gid=10
2123         local sz=1000
2124
2125         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2126         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2127         MULTIPID=$!
2128         sleep 2
2129
2130         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2131                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2132                 kill -9 $MULTIPID
2133         fi
2134         wait $MULTIPID
2135         local nsz=`stat -c %s $DIR/$tfile`
2136         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2137 }
2138 run_test 34h "ftruncate file under grouplock should not block"
2139
2140 test_35a() {
2141         cp /bin/sh $DIR/f35a
2142         chmod 444 $DIR/f35a
2143         chown $RUNAS_ID $DIR/f35a
2144         $RUNAS $DIR/f35a && error || true
2145         rm $DIR/f35a
2146 }
2147 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2148
2149 test_36a() {
2150         rm -f $DIR/f36
2151         utime $DIR/f36 || error
2152 }
2153 run_test 36a "MDS utime check (mknod, utime) ==================="
2154
2155 test_36b() {
2156         echo "" > $DIR/f36
2157         utime $DIR/f36 || error
2158 }
2159 run_test 36b "OST utime check (open, utime) ===================="
2160
2161 test_36c() {
2162         rm -f $DIR/d36/f36
2163         mkdir $DIR/d36
2164         chown $RUNAS_ID $DIR/d36
2165         $RUNAS utime $DIR/d36/f36 || error
2166 }
2167 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2168
2169 test_36d() {
2170         [ ! -d $DIR/d36 ] && test_36c
2171         echo "" > $DIR/d36/f36
2172         $RUNAS utime $DIR/d36/f36 || error
2173 }
2174 run_test 36d "non-root OST utime check (open, utime) ==========="
2175
2176 test_36e() {
2177         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2178         mkdir -p $DIR/$tdir
2179         touch $DIR/$tdir/$tfile
2180         $RUNAS utime $DIR/$tdir/$tfile && \
2181                 error "utime worked, expected failure" || true
2182 }
2183 run_test 36e "utime on non-owned file (should return error) ===="
2184
2185 subr_36fh() {
2186         local fl="$1"
2187         local LANG_SAVE=$LANG
2188         local LC_LANG_SAVE=$LC_LANG
2189         export LANG=C LC_LANG=C # for date language
2190
2191         DATESTR="Dec 20  2000"
2192         mkdir -p $DIR/$tdir
2193         lctl set_param fail_loc=$fl
2194         date; date +%s
2195         cp /etc/hosts $DIR/$tdir/$tfile
2196         sync & # write RPC generated with "current" inode timestamp, but delayed
2197         sleep 1
2198         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2199         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2200         cancel_lru_locks osc
2201         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2202         date; date +%s
2203         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2204                 echo "BEFORE: $LS_BEFORE" && \
2205                 echo "AFTER : $LS_AFTER" && \
2206                 echo "WANT  : $DATESTR" && \
2207                 error "$DIR/$tdir/$tfile timestamps changed" || true
2208
2209         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2210 }
2211
2212 test_36f() {
2213         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2214         subr_36fh "0x80000214"
2215 }
2216 run_test 36f "utime on file racing with OST BRW write =========="
2217
2218 test_36g() {
2219         remote_ost_nodsh && skip "remote OST with nodsh" && return
2220         local fmd_max_age
2221         local fmd_before
2222         local fmd_after
2223
2224         mkdir -p $DIR/$tdir
2225         fmd_max_age=$(do_facet ost1 \
2226                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2227                 head -n 1")
2228
2229         fmd_before=$(do_facet ost1 \
2230                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2231         touch $DIR/$tdir/$tfile
2232         sleep $((fmd_max_age + 12))
2233         fmd_after=$(do_facet ost1 \
2234                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2235
2236         echo "fmd_before: $fmd_before"
2237         echo "fmd_after: $fmd_after"
2238         [ "$fmd_after" -gt "$fmd_before" ] && \
2239                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" && \
2240                 error "fmd didn't expire after ping" || true
2241 }
2242 run_test 36g "filter mod data cache expiry ====================="
2243
2244 test_36h() {
2245         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2246         subr_36fh "0x80000227"
2247 }
2248 run_test 36h "utime on file racing with OST BRW write =========="
2249
2250 test_37() {
2251         mkdir -p $DIR/$tdir
2252         echo f > $DIR/$tdir/fbugfile
2253         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2254         ls $DIR/$tdir | grep "\<fbugfile\>" && error
2255         $UMOUNT $DIR/$tdir || error
2256         rm -f $DIR/$tdir/fbugfile || error
2257 }
2258 run_test 37 "ls a mounted file system to check old content ====="
2259
2260 test_38() {
2261         local file=$DIR/$tfile
2262         touch $file
2263         openfile -f O_DIRECTORY $file
2264         local RC=$?
2265         local ENOTDIR=20
2266         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2267         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2268 }
2269 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2270
2271 test_39() {
2272         touch $DIR/$tfile
2273         touch $DIR/${tfile}2
2274 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2275 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2276 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2277         sleep 2
2278         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2279         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2280                 echo "mtime"
2281                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2282                 echo "atime"
2283                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2284                 echo "ctime"
2285                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2286                 error "O_TRUNC didn't change timestamps"
2287         fi
2288 }
2289 run_test 39 "mtime changed on create ==========================="
2290
2291 test_39b() {
2292         mkdir -p $DIR/$tdir
2293         cp -p /etc/passwd $DIR/$tdir/fopen
2294         cp -p /etc/passwd $DIR/$tdir/flink
2295         cp -p /etc/passwd $DIR/$tdir/funlink
2296         cp -p /etc/passwd $DIR/$tdir/frename
2297         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2298
2299         sleep 1
2300         echo "aaaaaa" >> $DIR/$tdir/fopen
2301         echo "aaaaaa" >> $DIR/$tdir/flink
2302         echo "aaaaaa" >> $DIR/$tdir/funlink
2303         echo "aaaaaa" >> $DIR/$tdir/frename
2304
2305         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2306         local link_new=`stat -c %Y $DIR/$tdir/flink`
2307         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2308         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2309
2310         cat $DIR/$tdir/fopen > /dev/null
2311         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2312         rm -f $DIR/$tdir/funlink2
2313         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2314
2315         for (( i=0; i < 2; i++ )) ; do
2316                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2317                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2318                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2319                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2320
2321                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2322                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2323                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2324                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2325
2326                 cancel_lru_locks osc
2327                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2328         done
2329 }
2330 run_test 39b "mtime change on open, link, unlink, rename  ======"
2331
2332 # this should be set to past
2333 TEST_39_MTIME=`date -d "1 year ago" +%s`
2334
2335 # bug 11063
2336 test_39c() {
2337         touch $DIR1/$tfile
2338         sleep 2
2339         local mtime0=`stat -c %Y $DIR1/$tfile`
2340
2341         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2342         local mtime1=`stat -c %Y $DIR1/$tfile`
2343         [ "$mtime1" = $TEST_39_MTIME ] || \
2344                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2345
2346         local d1=`date +%s`
2347         echo hello >> $DIR1/$tfile
2348         local d2=`date +%s`
2349         local mtime2=`stat -c %Y $DIR1/$tfile`
2350         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2351                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2352
2353         mv $DIR1/$tfile $DIR1/$tfile-1
2354
2355         for (( i=0; i < 2; i++ )) ; do
2356                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2357                 [ "$mtime2" = "$mtime3" ] || \
2358                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2359
2360                 cancel_lru_locks osc
2361                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2362         done
2363 }
2364 run_test 39c "mtime change on rename ==========================="
2365
2366 # bug 21114
2367 test_39d() {
2368         touch $DIR1/$tfile
2369
2370         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2371
2372         for (( i=0; i < 2; i++ )) ; do
2373                 local mtime=`stat -c %Y $DIR1/$tfile`
2374                 [ $mtime = $TEST_39_MTIME ] || \
2375                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2376
2377                 cancel_lru_locks osc
2378                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2379         done
2380 }
2381 run_test 39d "create, utime, stat =============================="
2382
2383 # bug 21114
2384 test_39e() {
2385         touch $DIR1/$tfile
2386         local mtime1=`stat -c %Y $DIR1/$tfile`
2387
2388         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2389
2390         for (( i=0; i < 2; i++ )) ; do
2391                 local mtime2=`stat -c %Y $DIR1/$tfile`
2392                 [ $mtime2 = $TEST_39_MTIME ] || \
2393                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2394
2395                 cancel_lru_locks osc
2396                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2397         done
2398 }
2399 run_test 39e "create, stat, utime, stat ========================"
2400
2401 # bug 21114
2402 test_39f() {
2403         touch $DIR1/$tfile
2404         mtime1=`stat -c %Y $DIR1/$tfile`
2405
2406         sleep 2
2407         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2408
2409         for (( i=0; i < 2; i++ )) ; do
2410                 local mtime2=`stat -c %Y $DIR1/$tfile`
2411                 [ $mtime2 = $TEST_39_MTIME ] || \
2412                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2413
2414                 cancel_lru_locks osc
2415                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2416         done
2417 }
2418 run_test 39f "create, stat, sleep, utime, stat ================="
2419
2420 # bug 11063
2421 test_39g() {
2422         echo hello >> $DIR1/$tfile
2423         local mtime1=`stat -c %Y $DIR1/$tfile`
2424
2425         sleep 2
2426         chmod o+r $DIR1/$tfile
2427
2428         for (( i=0; i < 2; i++ )) ; do
2429                 local mtime2=`stat -c %Y $DIR1/$tfile`
2430                 [ "$mtime1" = "$mtime2" ] || \
2431                         error "lost mtime: $mtime2, should be $mtime1"
2432
2433                 cancel_lru_locks osc
2434                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2435         done
2436 }
2437 run_test 39g "write, chmod, stat ==============================="
2438
2439 # bug 11063
2440 test_39h() {
2441         touch $DIR1/$tfile
2442         sleep 1
2443
2444         local d1=`date`
2445         echo hello >> $DIR1/$tfile
2446         local mtime1=`stat -c %Y $DIR1/$tfile`
2447
2448         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2449         local d2=`date`
2450         if [ "$d1" != "$d2" ]; then
2451                 echo "write and touch not within one second"
2452         else
2453                 for (( i=0; i < 2; i++ )) ; do
2454                         local mtime2=`stat -c %Y $DIR1/$tfile`
2455                         [ "$mtime2" = $TEST_39_MTIME ] || \
2456                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
2457
2458                         cancel_lru_locks osc
2459                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2460                 done
2461         fi
2462 }
2463 run_test 39h "write, utime within one second, stat ============="
2464
2465 test_39i() {
2466         touch $DIR1/$tfile
2467         sleep 1
2468
2469         echo hello >> $DIR1/$tfile
2470         local mtime1=`stat -c %Y $DIR1/$tfile`
2471
2472         mv $DIR1/$tfile $DIR1/$tfile-1
2473
2474         for (( i=0; i < 2; i++ )) ; do
2475                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2476
2477                 [ "$mtime1" = "$mtime2" ] || \
2478                         error "lost mtime: $mtime2, should be $mtime1"
2479
2480                 cancel_lru_locks osc
2481                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2482         done
2483 }
2484 run_test 39i "write, rename, stat =============================="
2485
2486 test_39j() {
2487         start_full_debug_logging
2488         touch $DIR1/$tfile
2489         sleep 1
2490
2491         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
2492         lctl set_param fail_loc=0x80000412
2493         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
2494                 error "multiop failed"
2495         local multipid=$!
2496         local mtime1=`stat -c %Y $DIR1/$tfile`
2497
2498         mv $DIR1/$tfile $DIR1/$tfile-1
2499
2500         kill -USR1 $multipid
2501         wait $multipid || error "multiop close failed"
2502
2503         for (( i=0; i < 2; i++ )) ; do
2504                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
2505                 [ "$mtime1" = "$mtime2" ] ||
2506                         error "mtime is lost on close: $mtime2, " \
2507                               "should be $mtime1"
2508
2509                 cancel_lru_locks osc
2510                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2511         done
2512         lctl set_param fail_loc=0
2513         stop_full_debug_logging
2514 }
2515 run_test 39j "write, rename, close, stat ======================="
2516
2517 test_39k() {
2518         touch $DIR1/$tfile
2519         sleep 1
2520
2521         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
2522         local multipid=$!
2523         local mtime1=`stat -c %Y $DIR1/$tfile`
2524
2525         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2526
2527         kill -USR1 $multipid
2528         wait $multipid || error "multiop close failed"
2529
2530         for (( i=0; i < 2; i++ )) ; do
2531                 local mtime2=`stat -c %Y $DIR1/$tfile`
2532
2533                 [ "$mtime2" = $TEST_39_MTIME ] || \
2534                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
2535
2536                 cancel_lru_locks osc
2537                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2538         done
2539 }
2540 run_test 39k "write, utime, close, stat ========================"
2541
2542 # this should be set to future
2543 TEST_39_ATIME=`date -d "1 year" +%s`
2544
2545 is_sles11()                                             # LU-1783
2546 {
2547         if [ -r /etc/SuSE-release ]
2548         then
2549                 local vers=`grep VERSION /etc/SuSE-release | awk '{print $3}'`
2550                 local patchlev=`grep PATCHLEVEL /etc/SuSE-release \
2551                         | awk '{print $3}'`
2552                 if [ $vers -eq 11 ] && [ $patchlev -eq 1 ]
2553                 then
2554                         return 0
2555                 fi
2556         fi
2557         return 1
2558 }
2559
2560 test_39l() {
2561         is_sles11 && skip "SLES 11 SP1" && return       # LU-1783
2562         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2563         local atime_diff=$(do_facet $SINGLEMDS lctl get_param -n mdd.*.atime_diff)
2564
2565         mkdir -p $DIR/$tdir
2566
2567         # test setting directory atime to future
2568         touch -a -d @$TEST_39_ATIME $DIR/$tdir
2569         local atime=$(stat -c %X $DIR/$tdir)
2570         [ "$atime" = $TEST_39_ATIME ] || \
2571                 error "atime is not set to future: $atime, should be $TEST_39_ATIME"
2572
2573         # test setting directory atime from future to now
2574         local d1=$(date +%s)
2575         ls $DIR/$tdir
2576         local d2=$(date +%s)
2577
2578         cancel_lru_locks mdc
2579         atime=$(stat -c %X $DIR/$tdir)
2580         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2581                 error "atime is not updated from future: $atime, should be $d1<atime<$d2"
2582
2583         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=2
2584         sleep 3
2585
2586         # test setting directory atime when now > dir atime + atime_diff
2587         d1=$(date +%s)
2588         ls $DIR/$tdir
2589         d2=$(date +%s)
2590         cancel_lru_locks mdc
2591         atime=$(stat -c %X $DIR/$tdir)
2592         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2593                 error "atime is not updated  : $atime, should be $d2"
2594
2595         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=60
2596         sleep 3
2597
2598         # test not setting directory atime when now < dir atime + atime_diff
2599         ls $DIR/$tdir
2600         cancel_lru_locks mdc
2601         atime=$(stat -c %X $DIR/$tdir)
2602         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
2603                 error "atime is updated to $atime, should remain $d1<atime<$d2"
2604
2605         do_facet $SINGLEMDS lctl set_param -n mdd.*.atime_diff=$atime_diff
2606 }
2607 run_test 39l "directory atime update ==========================="
2608
2609 test_39m() {
2610         touch $DIR1/$tfile
2611         sleep 2
2612         local far_past_mtime=$(date -d "May 29 1953" +%s)
2613         local far_past_atime=$(date -d "Dec 17 1903" +%s)
2614
2615         touch -m -d @$far_past_mtime $DIR1/$tfile
2616         touch -a -d @$far_past_atime $DIR1/$tfile
2617
2618         for (( i=0; i < 2; i++ )) ; do
2619                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
2620                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
2621                         error "atime or mtime set incorrectly"
2622
2623                 cancel_lru_locks osc
2624                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2625         done
2626 }
2627 run_test 39m "test atime and mtime before 1970"
2628
2629 test_40() {
2630         dd if=/dev/zero of=$DIR/f40 bs=4096 count=1
2631         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/f40 && error
2632         $CHECKSTAT -t file -s 4096 $DIR/f40 || error
2633 }
2634 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
2635
2636 test_41() {
2637         # bug 1553
2638         small_write $DIR/f41 18
2639 }
2640 run_test 41 "test small file write + fstat ====================="
2641
2642 count_ost_writes() {
2643         lctl get_param -n osc.*.stats |
2644             awk -vwrites=0 '/ost_write/ { writes += $2 } END { print writes; }'
2645 }
2646
2647 # decent default
2648 WRITEBACK_SAVE=500
2649 DIRTY_RATIO_SAVE=40
2650 MAX_DIRTY_RATIO=50
2651 BG_DIRTY_RATIO_SAVE=10
2652 MAX_BG_DIRTY_RATIO=25
2653
2654 start_writeback() {
2655         trap 0
2656         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
2657         # dirty_ratio, dirty_background_ratio
2658         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2659                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
2660                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
2661                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
2662         else
2663                 # if file not here, we are a 2.4 kernel
2664                 kill -CONT `pidof kupdated`
2665         fi
2666 }
2667
2668 stop_writeback() {
2669         # setup the trap first, so someone cannot exit the test at the
2670         # exact wrong time and mess up a machine
2671         trap start_writeback EXIT
2672         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
2673         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
2674                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
2675                 sysctl -w vm.dirty_writeback_centisecs=0
2676                 sysctl -w vm.dirty_writeback_centisecs=0
2677                 # save and increase /proc/sys/vm/dirty_ratio
2678                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
2679                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
2680                 # save and increase /proc/sys/vm/dirty_background_ratio
2681                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
2682                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
2683         else
2684                 # if file not here, we are a 2.4 kernel
2685                 kill -STOP `pidof kupdated`
2686         fi
2687 }
2688
2689 # ensure that all stripes have some grant before we test client-side cache
2690 setup_test42() {
2691         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
2692                 dd if=/dev/zero of=$i bs=4k count=1
2693                 rm $i
2694         done
2695 }
2696
2697 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
2698 # file truncation, and file removal.
2699 test_42a() {
2700         setup_test42
2701         cancel_lru_locks osc
2702         stop_writeback
2703         sync; sleep 1; sync # just to be safe
2704         BEFOREWRITES=`count_ost_writes`
2705         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
2706         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
2707         AFTERWRITES=`count_ost_writes`
2708         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2709                 error "$BEFOREWRITES < $AFTERWRITES"
2710         start_writeback
2711 }
2712 run_test 42a "ensure that we don't flush on close =============="
2713
2714 test_42b() {
2715         setup_test42
2716         cancel_lru_locks osc
2717         stop_writeback
2718         sync
2719         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
2720         BEFOREWRITES=`count_ost_writes`
2721         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
2722         AFTERWRITES=`count_ost_writes`
2723         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2724                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
2725         fi
2726         BEFOREWRITES=`count_ost_writes`
2727         sync || error "sync: $?"
2728         AFTERWRITES=`count_ost_writes`
2729         if [ $BEFOREWRITES -lt $AFTERWRITES ]; then
2730                 error "$BEFOREWRITES < $AFTERWRITES on sync"
2731         fi
2732         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
2733         start_writeback
2734         return 0
2735 }
2736 run_test 42b "test destroy of file with cached dirty data ======"
2737
2738 # if these tests just want to test the effect of truncation,
2739 # they have to be very careful.  consider:
2740 # - the first open gets a {0,EOF}PR lock
2741 # - the first write conflicts and gets a {0, count-1}PW
2742 # - the rest of the writes are under {count,EOF}PW
2743 # - the open for truncate tries to match a {0,EOF}PR
2744 #   for the filesize and cancels the PWs.
2745 # any number of fixes (don't get {0,EOF} on open, match
2746 # composite locks, do smarter file size management) fix
2747 # this, but for now we want these tests to verify that
2748 # the cancellation with truncate intent works, so we
2749 # start the file with a full-file pw lock to match against
2750 # until the truncate.
2751 trunc_test() {
2752         test=$1
2753         file=$DIR/$test
2754         offset=$2
2755         cancel_lru_locks osc
2756         stop_writeback
2757         # prime the file with 0,EOF PW to match
2758         touch $file
2759         $TRUNCATE $file 0
2760         sync; sync
2761         # now the real test..
2762         dd if=/dev/zero of=$file bs=1024 count=100
2763         BEFOREWRITES=`count_ost_writes`
2764         $TRUNCATE $file $offset
2765         cancel_lru_locks osc
2766         AFTERWRITES=`count_ost_writes`
2767         start_writeback
2768 }
2769
2770 test_42c() {
2771         trunc_test 42c 1024
2772         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
2773             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
2774         rm $file
2775 }
2776 run_test 42c "test partial truncate of file with cached dirty data"
2777
2778 test_42d() {
2779         trunc_test 42d 0
2780         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
2781             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
2782         rm $file
2783 }
2784 run_test 42d "test complete truncate of file with cached dirty data"
2785
2786 test_42e() { # bug22074
2787         local TDIR=$DIR/${tdir}e
2788         local pagesz=$(page_size)
2789         local pages=16 # hardcoded 16 pages, don't change it.
2790         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
2791         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
2792         local max_dirty_mb
2793         local warmup_files
2794
2795         mkdir -p $TDIR
2796         $SETSTRIPE -c 1 $TDIR
2797         createmany -o $TDIR/f $files
2798
2799         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
2800
2801         # we assume that with $OSTCOUNT files, at least one of them will
2802         # be allocated on OST0.
2803         warmup_files=$((OSTCOUNT * max_dirty_mb))
2804         createmany -o $TDIR/w $warmup_files
2805
2806         # write a large amount of data into one file and sync, to get good
2807         # avail_grant number from OST.
2808         for ((i=0; i<$warmup_files; i++)); do
2809                 idx=$($GETSTRIPE -i $TDIR/w$i)
2810                 [ $idx -ne 0 ] && continue
2811                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
2812                 break
2813         done
2814         [ $i -gt $warmup_files ] && error "OST0 is still cold"
2815         sync
2816         $LCTL get_param $proc_osc0/cur_dirty_bytes
2817         $LCTL get_param $proc_osc0/cur_grant_bytes
2818
2819         # create as much dirty pages as we can while not to trigger the actual
2820         # RPCs directly. but depends on the env, VFS may trigger flush during this
2821         # period, hopefully we are good.
2822         for ((i=0; i<$warmup_files; i++)); do
2823                 idx=$($GETSTRIPE -i $TDIR/w$i)
2824                 [ $idx -ne 0 ] && continue
2825                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
2826         done
2827         $LCTL get_param $proc_osc0/cur_dirty_bytes
2828         $LCTL get_param $proc_osc0/cur_grant_bytes
2829
2830         # perform the real test
2831         $LCTL set_param $proc_osc0/rpc_stats 0
2832         for ((;i<$files; i++)); do
2833                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
2834                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
2835         done
2836         sync
2837         $LCTL get_param $proc_osc0/rpc_stats
2838
2839         local percent=0
2840         local have_ppr=false
2841         $LCTL get_param $proc_osc0/rpc_stats |
2842                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
2843                         # skip lines until we are at the RPC histogram data
2844                         [ "$PPR" == "pages" ] && have_ppr=true && continue
2845                         $have_ppr || continue
2846
2847                         # we only want the percent stat for < 16 pages
2848                         [ $(echo $PPR | tr -d ':') -ge $pages ] && break
2849
2850                         percent=$((percent + WPCT))
2851                         if [ $percent -gt 15 ]; then
2852                                 error "less than 16-pages write RPCs" \
2853                                       "$percent% > 15%"
2854                                 break
2855                         fi
2856                 done
2857         rm -rf $TDIR
2858 }
2859 run_test 42e "verify sub-RPC writes are not done synchronously"
2860
2861 test_43() {
2862         mkdir -p $DIR/$tdir
2863         cp -p /bin/ls $DIR/$tdir/$tfile
2864         $MULTIOP $DIR/$tdir/$tfile Ow_c &
2865         pid=$!
2866         # give multiop a chance to open
2867         sleep 1
2868
2869         $DIR/$tdir/$tfile && error || true
2870         kill -USR1 $pid
2871 }
2872 run_test 43 "execution of file opened for write should return -ETXTBSY"
2873
2874 test_43a() {
2875         mkdir -p $DIR/d43
2876         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2877         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2878         MULTIOP_PID=$!
2879         $MULTIOP $DIR/d43/multiop Oc && error "expected error, got success"
2880         kill -USR1 $MULTIOP_PID || return 2
2881         wait $MULTIOP_PID || return 3
2882         rm $TMP/test43.junk
2883 }
2884 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
2885
2886 test_43b() {
2887         mkdir -p $DIR/d43
2888         cp -p `which $MULTIOP` $DIR/d43/multiop || cp -p multiop $DIR/d43/multiop
2889         MULTIOP_PROG=$DIR/d43/multiop multiop_bg_pause $TMP/test43.junk O_c || return 1
2890         MULTIOP_PID=$!
2891         $TRUNCATE $DIR/d43/multiop 0 && error "expected error, got success"
2892         kill -USR1 $MULTIOP_PID || return 2
2893         wait $MULTIOP_PID || return 3
2894         rm $TMP/test43.junk
2895 }
2896 run_test 43b "truncate of file being executed should return -ETXTBSY"
2897
2898 test_43c() {
2899         local testdir="$DIR/d43c"
2900         mkdir -p $testdir
2901         cp $SHELL $testdir/
2902         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
2903                 ( cd $testdir && md5sum -c)
2904 }
2905 run_test 43c "md5sum of copy into lustre========================"
2906
2907 test_44() {
2908         [  "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
2909         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
2910         dd if=$DIR/f1 bs=4k count=1 > /dev/null
2911 }
2912 run_test 44 "zero length read from a sparse stripe ============="
2913
2914 test_44a() {
2915     local nstripe=`$LCTL lov_getconfig $DIR | grep default_stripe_count: | \
2916                          awk '{print $2}'`
2917     [ -z "$nstripe" ] && skip "can't get stripe info" && return
2918     [ "$nstripe" -gt "$OSTCOUNT" ] && skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" && return
2919     local stride=`$LCTL lov_getconfig $DIR | grep default_stripe_size: | \
2920                       awk '{print $2}'`
2921     if [ $nstripe -eq 0 -o $nstripe -eq -1 ] ; then
2922         nstripe=`$LCTL lov_getconfig $DIR | grep obd_count: | awk '{print $2}'`
2923     fi
2924
2925     OFFSETS="0 $((stride/2)) $((stride-1))"
2926     for offset in $OFFSETS ; do
2927       for i in `seq 0 $((nstripe-1))`; do
2928         local GLOBALOFFSETS=""
2929         local size=$((((i + 2 * $nstripe )*$stride + $offset)))  # Bytes
2930         local myfn=$DIR/d44a-$size
2931         echo "--------writing $myfn at $size"
2932         ll_sparseness_write $myfn $size  || error "ll_sparseness_write"
2933         GLOBALOFFSETS="$GLOBALOFFSETS $size"
2934         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2935                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2936
2937         for j in `seq 0 $((nstripe-1))`; do
2938             size=$((((j + $nstripe )*$stride + $offset)))  # Bytes
2939             ll_sparseness_write $myfn $size || error "ll_sparseness_write"
2940             GLOBALOFFSETS="$GLOBALOFFSETS $size"
2941         done
2942         ll_sparseness_verify $myfn $GLOBALOFFSETS \
2943                             || error "ll_sparseness_verify $GLOBALOFFSETS"
2944         rm -f $myfn
2945       done
2946     done
2947 }
2948 run_test 44a "test sparse pwrite ==============================="
2949
2950 dirty_osc_total() {
2951         tot=0
2952         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
2953                 tot=$(($tot + $d))
2954         done
2955         echo $tot
2956 }
2957 do_dirty_record() {
2958         before=`dirty_osc_total`
2959         echo executing "\"$*\""
2960         eval $*
2961         after=`dirty_osc_total`
2962         echo before $before, after $after
2963 }
2964 test_45() {
2965         f="$DIR/f45"
2966         # Obtain grants from OST if it supports it
2967         echo blah > ${f}_grant
2968         stop_writeback
2969         sync
2970         do_dirty_record "echo blah > $f"
2971         [ $before -eq $after ] && error "write wasn't cached"
2972         do_dirty_record "> $f"
2973         [ $before -gt $after ] || error "truncate didn't lower dirty count"
2974         do_dirty_record "echo blah > $f"
2975         [ $before -eq $after ] && error "write wasn't cached"
2976         do_dirty_record "sync"
2977         [ $before -gt $after ] || error "writeback didn't lower dirty count"
2978         do_dirty_record "echo blah > $f"
2979         [ $before -eq $after ] && error "write wasn't cached"
2980         do_dirty_record "cancel_lru_locks osc"
2981         [ $before -gt $after ] || error "lock cancellation didn't lower dirty count"
2982         start_writeback
2983 }
2984 run_test 45 "osc io page accounting ============================"
2985
2986 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
2987 # test tickles a bug where re-dirtying a page was failing to be mapped to the
2988 # objects offset and an assert hit when an rpc was built with 1023's mapped
2989 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
2990 test_46() {
2991         f="$DIR/f46"
2992         stop_writeback
2993         sync
2994         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2995         sync
2996         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
2997         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
2998         sync
2999         start_writeback
3000 }
3001 run_test 46 "dirtying a previously written page ================"
3002
3003 # test_47 is removed "Device nodes check" is moved to test_28
3004
3005 test_48a() { # bug 2399
3006         check_kernel_version 34 || return 0
3007         mkdir -p $DIR/d48a
3008         cd $DIR/d48a
3009         mv $DIR/d48a $DIR/d48.new || error "move directory failed"
3010         mkdir $DIR/d48a || error "recreate directory failed"
3011         touch foo || error "'touch foo' failed after recreating cwd"
3012         mkdir bar || error "'mkdir foo' failed after recreating cwd"
3013         if check_kernel_version 44; then
3014                 touch .foo || error "'touch .foo' failed after recreating cwd"
3015                 mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
3016         fi
3017         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3018         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3019         cd . || error "'cd .' failed after recreating cwd"
3020         mkdir . && error "'mkdir .' worked after recreating cwd"
3021         rmdir . && error "'rmdir .' worked after recreating cwd"
3022         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3023         cd .. || error "'cd ..' failed after recreating cwd"
3024 }
3025 run_test 48a "Access renamed working dir (should return errors)="
3026
3027 test_48b() { # bug 2399
3028         check_kernel_version 34 || return 0
3029         mkdir -p $DIR/d48b
3030         cd $DIR/d48b
3031         rmdir $DIR/d48b || error "remove cwd $DIR/d48b failed"
3032         touch foo && error "'touch foo' worked after removing cwd"
3033         mkdir foo && error "'mkdir foo' worked after removing cwd"
3034         if check_kernel_version 44; then
3035                 touch .foo && error "'touch .foo' worked after removing cwd"
3036                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3037         fi
3038         ls . > /dev/null && error "'ls .' worked after removing cwd"
3039         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3040         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3041         mkdir . && error "'mkdir .' worked after removing cwd"
3042         rmdir . && error "'rmdir .' worked after removing cwd"
3043         ln -s . foo && error "'ln -s .' worked after removing cwd"
3044         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3045 }
3046 run_test 48b "Access removed working dir (should return errors)="
3047
3048 test_48c() { # bug 2350
3049         check_kernel_version 36 || return 0
3050         #lctl set_param debug=-1
3051         #set -vx
3052         mkdir -p $DIR/d48c/dir
3053         cd $DIR/d48c/dir
3054         $TRACE rmdir $DIR/d48c/dir || error "remove cwd $DIR/d48c/dir failed"
3055         $TRACE touch foo && error "'touch foo' worked after removing cwd"
3056         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
3057         if check_kernel_version 44; then
3058                 touch .foo && error "'touch .foo' worked after removing cwd"
3059                 mkdir .foo && error "'mkdir .foo' worked after removing cwd"
3060         fi
3061         $TRACE ls . && error "'ls .' worked after removing cwd"
3062         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3063         is_patchless || ( $TRACE cd . && error "'cd .' worked after removing cwd" )
3064         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
3065         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3066         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3067         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3068 }
3069 run_test 48c "Access removed working subdir (should return errors)"
3070
3071 test_48d() { # bug 2350
3072         check_kernel_version 36 || return 0
3073         #lctl set_param debug=-1
3074         #set -vx
3075         mkdir -p $DIR/d48d/dir
3076         cd $DIR/d48d/dir
3077         $TRACE rmdir $DIR/d48d/dir || error "remove cwd $DIR/d48d/dir failed"
3078         $TRACE rmdir $DIR/d48d || error "remove parent $DIR/d48d failed"
3079         $TRACE touch foo && error "'touch foo' worked after removing parent"
3080         $TRACE mkdir foo && error "'mkdir foo' worked after removing parent"
3081         if check_kernel_version 44; then
3082                 touch .foo && error "'touch .foo' worked after removing parent"
3083                 mkdir .foo && error "'mkdir .foo' worked after removing parent"
3084         fi
3085         $TRACE ls . && error "'ls .' worked after removing parent"
3086         $TRACE ls .. && error "'ls ..' worked after removing parent"
3087         is_patchless || ( $TRACE cd . && error "'cd .' worked after recreate parent" )
3088         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
3089         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3090         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3091         is_patchless || ( $TRACE cd .. && error "'cd ..' worked after removing parent" || true )
3092 }
3093 run_test 48d "Access removed parent subdir (should return errors)"
3094
3095 test_48e() { # bug 4134
3096         check_kernel_version 41 || return 0
3097         #lctl set_param debug=-1
3098         #set -vx
3099         mkdir -p $DIR/d48e/dir
3100         cd $DIR/d48e/dir
3101         $TRACE rmdir $DIR/d48e/dir || error "remove cwd $DIR/d48e/dir failed"
3102         $TRACE rmdir $DIR/d48e || error "remove parent $DIR/d48e failed"
3103         $TRACE touch $DIR/d48e || error "'touch $DIR/d48e' failed"
3104         $TRACE chmod +x $DIR/d48e || error "'chmod +x $DIR/d48e' failed"
3105         # On a buggy kernel addition of "touch foo" after cd .. will
3106         # produce kernel oops in lookup_hash_it
3107         touch ../foo && error "'cd ..' worked after recreate parent"
3108         cd $DIR
3109         $TRACE rm $DIR/d48e || error "rm '$DIR/d48e' failed"
3110 }
3111 run_test 48e "Access to recreated parent subdir (should return errors)"
3112
3113 test_49() { # LU-1030
3114         # get ost1 size - lustre-OST0000
3115         ost1_size=$(do_facet ost1 lfs df |grep ${ost1_svc} |awk '{print $4}')
3116         # write 800M at maximum
3117         [ $ost1_size -gt 819200 ] && ost1_size=819200
3118
3119         lfs setstripe -c 1 -i 0 $DIR/$tfile
3120         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3121         local dd_pid=$!
3122
3123         # change max_pages_per_rpc while writing the file
3124         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3125         local orig_mppc=`$LCTL get_param -n $osc1_mppc`
3126         # loop until dd process exits
3127         while ps ax -opid | grep -wq $dd_pid; do
3128                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3129                 sleep $((RANDOM % 5 + 1))
3130         done
3131         # restore original max_pages_per_rpc
3132         $LCTL set_param $osc1_mppc=$orig_mppc
3133         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3134 }
3135 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3136
3137 test_50() {
3138         # bug 1485
3139         mkdir $DIR/d50
3140         cd $DIR/d50
3141         ls /proc/$$/cwd || error
3142 }
3143 run_test 50 "special situations: /proc symlinks  ==============="
3144
3145 test_51a() {    # was test_51
3146         # bug 1516 - create an empty entry right after ".." then split dir
3147         mkdir $DIR/d51
3148         touch $DIR/d51/foo
3149         $MCREATE $DIR/d51/bar
3150         rm $DIR/d51/foo
3151         createmany -m $DIR/d51/longfile 201
3152         FNUM=202
3153         while [ `ls -sd $DIR/d51 | awk '{ print $1 }'` -eq 4 ]; do
3154                 $MCREATE $DIR/d51/longfile$FNUM
3155                 FNUM=$(($FNUM + 1))
3156                 echo -n "+"
3157         done
3158         echo
3159         ls -l $DIR/d51 > /dev/null || error
3160 }
3161 run_test 51a "special situations: split htree with empty entry =="
3162
3163 export NUMTEST=70000
3164 test_51b() {
3165         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3166         [ $NUMFREE -lt 21000 ] && \
3167                 skip "not enough free inodes ($NUMFREE)" && \
3168                 return
3169
3170         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 50))
3171
3172         mkdir -p $DIR/d51b
3173         createmany -d $DIR/d51b/t- $NUMTEST
3174 }
3175 run_test 51b "mkdir .../t-0 --- .../t-$NUMTEST ===================="
3176
3177 test_51ba() { # LU-993
3178         local BASE=$DIR/d51b
3179         # unlink all but 100 subdirectories, then check it still works
3180         local LEFT=100
3181         local DELETE=$((NUMTEST - LEFT))
3182
3183         # continue on to run this test even if 51b didn't finish,
3184         # just to delete the many subdirectories created.
3185         ! [ -d "${BASE}/t-1" ] && skip "test_51b() not run" && return 0
3186
3187         # for ldiskfs the nlink count should be 1, but this is OSD specific
3188         # and so this is listed for informational purposes only
3189         log "nlink before: $(stat -c %h $BASE)"
3190         unlinkmany -d $BASE/t- $DELETE ||
3191                 error "unlink of first $DELETE subdirs failed"
3192
3193         log "nlink between: $(stat -c %h $BASE)"
3194         local FOUND=$(ls -l ${BASE} | wc -l)
3195         FOUND=$((FOUND - 1))  # trim the first line of ls output
3196         [ $FOUND -ne $LEFT ] &&
3197                 error "can't find subdirs: found only $FOUND/$LEFT"
3198
3199         unlinkmany -d $BASE/t- $DELETE $LEFT ||
3200                 error "unlink of second $LEFT subdirs failed"
3201         log "nlink after: $(stat -c %h $BASE)"
3202 }
3203 run_test 51ba "rmdir .../t-0 --- .../t-$NUMTEST"
3204
3205 test_51bb() {
3206         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3207
3208         local ndirs=${TEST51BB_NDIRS:-10}
3209         local nfiles=${TEST51BB_NFILES:-100}
3210
3211         local numfree=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
3212
3213         [ $numfree -lt $(( ndirs * nfiles)) ] && \
3214                 nfiles=$(( numfree / ndirs - 10 ))
3215
3216         local dir=$DIR/d51bb
3217         mkdir -p $dir
3218         local savePOLICY=$(lctl get_param -n lmv.*.placement)
3219         lctl set_param -n lmv.*.placement=CHAR
3220
3221         lfs df -i $dir
3222         local IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3223         OLDUSED=($IUSED)
3224
3225         declare -a dirs
3226         for ((i=0; i < $ndirs; i++)); do
3227                 dirs[i]=$dir/$RANDOM
3228                 echo Creating directory ${dirs[i]}
3229                 mkdir -p ${dirs[i]}
3230                 ls $dir
3231                 echo Creating $nfiles in dir ${dirs[i]} ...
3232                 echo "createmany -o ${dirs[i]}/$tfile- $nfiles"
3233                 createmany -o ${dirs[i]}/$tfile- $nfiles
3234         done
3235         ls $dir
3236
3237         sleep 1
3238
3239         IUSED=$(lfs df -i $dir | grep MDT | awk '{print $3}')
3240         NEWUSED=($IUSED)
3241
3242         local rc=0
3243         for ((i=0; i<${#NEWUSED[@]}; i++)); do
3244                 echo "mds $i: inodes count OLD ${OLDUSED[$i]} NEW ${NEWUSED[$i]}"
3245                 [ ${OLDUSED[$i]} -lt ${NEWUSED[$i]} ] || rc=$((rc + 1))
3246         done
3247
3248         lctl set_param -n lmv.*.placement=$savePOLICY
3249
3250         [ $rc -ne $MDSCOUNT ] || \
3251                 error "Objects/inodes are not distributed over all mds servers"
3252 }
3253 run_test 51bb "mkdir createmany CMD $MDSCOUNT  ===================="
3254
3255 test_51d() {
3256         [  "$OSTCOUNT" -lt "3" ] && skip_env "skipping test with few OSTs" && return
3257         mkdir -p $DIR/d51d
3258         createmany -o $DIR/d51d/t- 1000
3259         $GETSTRIPE $DIR/d51d > $TMP/files
3260         for N in `seq 0 $((OSTCOUNT - 1))`; do
3261             OBJS[$N]=`awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}' $TMP/files`
3262             OBJS0[$N]=`grep -A 1 idx $TMP/files | awk -vobjs=0 '($1 == '$N') { objs += 1 } END { print objs;}'`
3263             log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3264         done
3265         unlinkmany $DIR/d51d/t- 1000
3266
3267         NLAST=0
3268         for N in `seq 1 $((OSTCOUNT - 1))`; do
3269             [ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ] && \
3270                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3271             [ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ] && \
3272                 error "OST $N has less objects vs OST $NLAST (${OBJS[$N]} < ${OBJS[$NLAST]}"
3273
3274             [ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ] && \
3275                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3276             [ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ] && \
3277                 error "OST $N has less #0 objects vs OST $NLAST (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3278             NLAST=$N
3279         done
3280 }
3281 run_test 51d "check object distribution ===================="
3282
3283 test_52a() {
3284         [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
3285         mkdir -p $DIR/d52a
3286         touch $DIR/d52a/foo
3287         chattr +a $DIR/d52a/foo || error "chattr +a failed"
3288         echo bar >> $DIR/d52a/foo || error "append bar failed"
3289         cp /etc/hosts $DIR/d52a/foo && error "cp worked"
3290         rm -f $DIR/d52a/foo 2>/dev/null && error "rm worked"
3291         link $DIR/d52a/foo $DIR/d52a/foo_link 2>/dev/null && error "link worked"
3292         echo foo >> $DIR/d52a/foo || error "append foo failed"
3293         mrename $DIR/d52a/foo $DIR/d52a/foo_ren && error "rename worked"
3294         lsattr $DIR/d52a/foo | egrep -q "^-+a[-e]+ $DIR/d52a/foo" || error "lsattr"
3295         chattr -a $DIR/d52a/foo || error "chattr -a failed"
3296         cp -r $DIR/d52a /tmp/
3297         rm -fr $DIR/d52a || error "cleanup rm failed"
3298 }
3299 run_test 52a "append-only flag test (should return errors) ====="
3300
3301 test_52b() {
3302         [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
3303         mkdir -p $DIR/d52b
3304         touch $DIR/d52b/foo
3305         chattr +i $DIR/d52b/foo || error "chattr +i failed"
3306         cat test > $DIR/d52b/foo && error "cat test worked"
3307         cp /etc/hosts $DIR/d52b/foo && error "cp worked"
3308         rm -f $DIR/d52b/foo 2>/dev/null && error "rm worked"
3309         link $DIR/d52b/foo $DIR/d52b/foo_link 2>/dev/null && error  "link worked"
3310         echo foo >> $DIR/d52b/foo && error "echo worked"
3311         mrename $DIR/d52b/foo $DIR/d52b/foo_ren && error "rename worked"
3312         [ -f $DIR/d52b/foo ] || error
3313         [ -f $DIR/d52b/foo_ren ] && error
3314         lsattr $DIR/d52b/foo | egrep -q "^-+i[-e]+ $DIR/d52b/foo" || error "lsattr"
3315         chattr -i $DIR/d52b/foo || error "chattr failed"
3316
3317         rm -fr $DIR/d52b || error
3318 }
3319 run_test 52b "immutable flag test (should return errors) ======="
3320
3321 test_53() {
3322         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3323         remote_ost_nodsh && skip "remote OST with nodsh" && return
3324
3325         local param
3326         local ostname
3327         local mds_last
3328         local ost_last
3329         local ostnum
3330
3331         # only test MDT0000
3332         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
3333         for value in $(do_facet $SINGLEMDS lctl get_param osc.$mdtosc.prealloc_last_id) ; do
3334                 param=`echo ${value[0]} | cut -d "=" -f1`
3335                 ostname=`echo $param | cut -d "." -f2 | cut -d - -f 1-2`
3336                 mds_last=$(do_facet $SINGLEMDS lctl get_param -n $param)
3337                 ostnum=$(echo $ostname | sed "s/${FSNAME}-OST//g" | awk '{print ($1+1)}' )
3338                 ost_last=$(do_facet ost$ostnum lctl get_param -n obdfilter.$ostname.last_id | head -n 1)
3339                 echo "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3340                 if [ $ost_last != $mds_last ]; then
3341                     error "$ostname.last_id=$ost_last ; MDS.last_id=$mds_last"
3342                 fi
3343         done
3344 }
3345 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
3346
3347 test_54a() {
3348         [ ! -f "$SOCKETSERVER" ] && skip_env "no socketserver, skipping" && return
3349         [ ! -f "$SOCKETCLIENT" ] && skip_env "no socketclient, skipping" && return
3350         $SOCKETSERVER $DIR/socket
3351         $SOCKETCLIENT $DIR/socket || error
3352         $MUNLINK $DIR/socket
3353 }
3354 run_test 54a "unix domain socket test =========================="
3355
3356 test_54b() {
3357         f="$DIR/f54b"
3358         mknod $f c 1 3
3359         chmod 0666 $f
3360         dd if=/dev/zero of=$f bs=`page_size` count=1
3361 }
3362 run_test 54b "char device works in lustre ======================"
3363
3364 find_loop_dev() {
3365         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
3366         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
3367         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
3368
3369         for i in `seq 3 7`; do
3370                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
3371                 LOOPDEV=$LOOPBASE$i
3372                 LOOPNUM=$i
3373                 break
3374         done
3375 }
3376
3377 test_54c() {
3378         tfile="$DIR/f54c"
3379         tdir="$DIR/d54c"
3380         loopdev="$DIR/loop54c"
3381
3382         find_loop_dev
3383         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
3384         mknod $loopdev b 7 $LOOPNUM
3385         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
3386         dd if=/dev/zero of=$tfile bs=`page_size` seek=1024 count=1 > /dev/null
3387         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
3388         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
3389         mkdir -p $tdir
3390         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
3391         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
3392         df $tdir
3393         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
3394         $UMOUNT $tdir
3395         losetup -d $loopdev
3396         rm $loopdev
3397 }
3398 run_test 54c "block device works in lustre ====================="
3399
3400 test_54d() {
3401         f="$DIR/f54d"
3402         string="aaaaaa"
3403         mknod $f p
3404         [ "$string" = `echo $string > $f | cat $f` ] || error
3405 }
3406 run_test 54d "fifo device works in lustre ======================"
3407
3408 test_54e() {
3409         check_kernel_version 46 || return 0
3410         f="$DIR/f54e"
3411         string="aaaaaa"
3412         cp -aL /dev/console $f
3413         echo $string > $f || error
3414 }
3415 run_test 54e "console/tty device works in lustre ======================"
3416
3417 #The test_55 used to be iopen test and it was removed by bz#24037.
3418 #run_test 55 "check iopen_connect_dentry() ======================"
3419
3420 test_56a() {    # was test_56
3421         rm -rf $DIR/d56
3422         $SETSTRIPE -d $DIR
3423         mkdir $DIR/d56
3424         mkdir $DIR/d56/dir
3425         NUMFILES=3
3426         NUMFILESx2=$(($NUMFILES * 2))
3427         for i in `seq 1 $NUMFILES` ; do
3428                 touch $DIR/d56/file$i
3429                 touch $DIR/d56/dir/file$i
3430         done
3431
3432         # test lfs getstripe with --recursive
3433         FILENUM=`$GETSTRIPE --recursive $DIR/d56 | grep -c obdidx`
3434         [ $FILENUM -eq $NUMFILESx2 ] ||
3435                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
3436         FILENUM=`$GETSTRIPE $DIR/d56 | grep -c obdidx`
3437         [ $FILENUM -eq $NUMFILES ] ||
3438                 error "$GETSTRIPE $DIR/d56: found $FILENUM, not $NUMFILES"
3439         echo "$GETSTRIPE --recursive passed."
3440
3441         # test lfs getstripe with file instead of dir
3442         FILENUM=`$GETSTRIPE $DIR/d56/file1 | grep -c obdidx`
3443         [ $FILENUM  -eq 1 ] || error \
3444                  "$GETSTRIPE $DIR/d56/file1: found $FILENUM, not 1"
3445         echo "$GETSTRIPE file1 passed."
3446
3447         #test lfs getstripe with --verbose
3448         [ `$GETSTRIPE --verbose $DIR/d56 | grep -c lmm_magic` -eq $NUMFILES ] ||
3449                 error "$GETSTRIPE --verbose $DIR/d56: want $NUMFILES lmm_magic"
3450         [ `$GETSTRIPE $DIR/d56 | grep -c lmm_magic` -eq 0 ] ||
3451             error "$GETSTRIPE $DIR/d56: showed lmm_magic"
3452         echo "$GETSTRIPE --verbose passed."
3453
3454         #test lfs getstripe with --obd
3455         $GETSTRIPE --obd wrong_uuid $DIR/d56 2>&1 | grep -q "unknown obduuid" ||
3456                 error "$GETSTRIPE --obd wrong_uuid should return error message"
3457
3458         [  "$OSTCOUNT" -lt 2 ] &&
3459                 skip_env "skipping other $GETSTRIPE --obd test" && return
3460
3461         OSTIDX=1
3462         OBDUUID=$(ostuuid_from_index $OSTIDX)
3463         FILENUM=`$GETSTRIPE -ir $DIR/d56 | grep -x $OSTIDX | wc -l`
3464         FOUND=`$GETSTRIPE -r --obd $OBDUUID $DIR/d56 | grep obdidx | wc -l`
3465         [ $FOUND -eq $FILENUM ] ||
3466                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
3467         [ `$GETSTRIPE -r -v --obd $OBDUUID $DIR/d56 |
3468                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
3469                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l` -eq 0 ] ||
3470                 error "$GETSTRIPE --obd: should not show file on other obd"
3471         echo "$GETSTRIPE --obd passed"
3472 }
3473 run_test 56a "check $GETSTRIPE"
3474
3475 NUMFILES=3
3476 NUMDIRS=3
3477 setup_56() {
3478         local LOCAL_NUMFILES="$1"
3479         local LOCAL_NUMDIRS="$2"
3480         local MKDIR_PARAMS="$3"
3481
3482         if [ ! -d "$TDIR" ] ; then
3483                 mkdir -p $TDIR
3484                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
3485                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3486                         touch $TDIR/file$i
3487                 done
3488                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3489                         mkdir $TDIR/dir$i
3490                         for j in `seq 1 $LOCAL_NUMFILES` ; do
3491                                 touch $TDIR/dir$i/file$j
3492                         done
3493                 done
3494         fi
3495 }
3496
3497 setup_56_special() {
3498         LOCAL_NUMFILES=$1
3499         LOCAL_NUMDIRS=$2
3500         setup_56 $1 $2
3501         if [ ! -e "$TDIR/loop1b" ] ; then
3502                 for i in `seq 1 $LOCAL_NUMFILES` ; do
3503                         mknod $TDIR/loop${i}b b 7 $i
3504                         mknod $TDIR/null${i}c c 1 3
3505                         ln -s $TDIR/file1 $TDIR/link${i}l
3506                 done
3507                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
3508                         mknod $TDIR/dir$i/loop${i}b b 7 $i
3509                         mknod $TDIR/dir$i/null${i}c c 1 3
3510                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
3511                 done
3512         fi
3513 }
3514
3515 test_56g() {
3516         $SETSTRIPE -d $DIR
3517
3518         TDIR=$DIR/${tdir}g
3519         setup_56 $NUMFILES $NUMDIRS
3520
3521         EXPECTED=$(($NUMDIRS + 2))
3522         # test lfs find with -name
3523         for i in $(seq 1 $NUMFILES) ; do
3524                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
3525                 [ $NUMS -eq $EXPECTED ] ||
3526                         error "lfs find -name \"*$i\" $TDIR wrong: "\
3527                               "found $NUMS, expected $EXPECTED"
3528         done
3529 }
3530 run_test 56g "check lfs find -name ============================="
3531
3532 test_56h() {
3533         $SETSTRIPE -d $DIR
3534
3535         TDIR=$DIR/${tdir}g
3536         setup_56 $NUMFILES $NUMDIRS
3537
3538         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
3539         # test lfs find with ! -name
3540         for i in $(seq 1 $NUMFILES) ; do
3541                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
3542                 [ $NUMS -eq $EXPECTED ] ||
3543                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
3544                               "found $NUMS, expected $EXPECTED"
3545         done
3546 }
3547 run_test 56h "check lfs find ! -name ============================="
3548
3549 test_56i() {
3550        tdir=${tdir}i
3551        mkdir -p $DIR/$tdir
3552        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
3553        CMD="$LFIND -ost $UUID $DIR/$tdir"
3554        OUT=$($CMD)
3555        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
3556 }
3557 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
3558
3559 test_56j() {
3560         TDIR=$DIR/${tdir}g
3561         setup_56_special $NUMFILES $NUMDIRS
3562
3563         EXPECTED=$((NUMDIRS + 1))
3564         CMD="$LFIND -type d $TDIR"
3565         NUMS=$($CMD | wc -l)
3566         [ $NUMS -eq $EXPECTED ] ||
3567                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3568 }
3569 run_test 56j "check lfs find -type d ============================="
3570
3571 test_56k() {
3572         TDIR=$DIR/${tdir}g
3573         setup_56_special $NUMFILES $NUMDIRS
3574
3575         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3576         CMD="$LFIND -type f $TDIR"
3577         NUMS=$($CMD | wc -l)
3578         [ $NUMS -eq $EXPECTED ] ||
3579                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3580 }
3581 run_test 56k "check lfs find -type f ============================="
3582
3583 test_56l() {
3584         TDIR=$DIR/${tdir}g
3585         setup_56_special $NUMFILES $NUMDIRS
3586
3587         EXPECTED=$((NUMDIRS + NUMFILES))
3588         CMD="$LFIND -type b $TDIR"
3589         NUMS=$($CMD | wc -l)
3590         [ $NUMS -eq $EXPECTED ] ||
3591                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3592 }
3593 run_test 56l "check lfs find -type b ============================="
3594
3595 test_56m() {
3596         TDIR=$DIR/${tdir}g
3597         setup_56_special $NUMFILES $NUMDIRS
3598
3599         EXPECTED=$((NUMDIRS + NUMFILES))
3600         CMD="$LFIND -type c $TDIR"
3601         NUMS=$($CMD | wc -l)
3602         [ $NUMS -eq $EXPECTED ] ||
3603                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3604 }
3605 run_test 56m "check lfs find -type c ============================="
3606
3607 test_56n() {
3608         TDIR=$DIR/${tdir}g
3609         setup_56_special $NUMFILES $NUMDIRS
3610
3611         EXPECTED=$((NUMDIRS + NUMFILES))
3612         CMD="$LFIND -type l $TDIR"
3613         NUMS=$($CMD | wc -l)
3614         [ $NUMS -eq $EXPECTED ] ||
3615                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3616 }
3617 run_test 56n "check lfs find -type l ============================="
3618
3619 test_56o() {
3620         TDIR=$DIR/${tdir}o
3621         setup_56 $NUMFILES $NUMDIRS
3622
3623         utime $TDIR/file1 > /dev/null || error "utime (1)"
3624         utime $TDIR/file2 > /dev/null || error "utime (2)"
3625         utime $TDIR/dir1 > /dev/null || error "utime (3)"
3626         utime $TDIR/dir2 > /dev/null || error "utime (4)"
3627         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
3628         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
3629
3630         EXPECTED=4
3631         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
3632         [ $NUMS -eq $EXPECTED ] || \
3633                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
3634
3635         EXPECTED=12
3636         CMD="$LFIND -mtime 0 $TDIR"
3637         NUMS=$($CMD | wc -l)
3638         [ $NUMS -eq $EXPECTED ] ||
3639                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3640 }
3641 run_test 56o "check lfs find -mtime for old files =========================="
3642
3643 test_56p() {
3644         [ $RUNAS_ID -eq $UID ] &&
3645                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3646
3647         TDIR=$DIR/${tdir}p
3648         setup_56 $NUMFILES $NUMDIRS
3649
3650         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
3651         EXPECTED=$NUMFILES
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         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
3658         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
3659         NUMS=$($CMD | wc -l)
3660         [ $NUMS -eq $EXPECTED ] || \
3661                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3662 }
3663 run_test 56p "check lfs find -uid and ! -uid ==============================="
3664
3665 test_56q() {
3666         [ $RUNAS_ID -eq $UID ] &&
3667                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3668
3669         TDIR=$DIR/${tdir}q
3670         setup_56 $NUMFILES $NUMDIRS
3671
3672         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
3673
3674         EXPECTED=$NUMFILES
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         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
3681         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
3682         NUMS=$($CMD | wc -l)
3683         [ $NUMS -eq $EXPECTED ] ||
3684                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3685 }
3686 run_test 56q "check lfs find -gid and ! -gid ==============================="
3687
3688 test_56r() {
3689         TDIR=$DIR/${tdir}r
3690         setup_56 $NUMFILES $NUMDIRS
3691
3692         EXPECTED=12
3693         CMD="$LFIND -size 0 -type f $TDIR"
3694         NUMS=$($CMD | wc -l)
3695         [ $NUMS -eq $EXPECTED ] ||
3696                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3697         EXPECTED=0
3698         CMD="$LFIND ! -size 0 -type f $TDIR"
3699         NUMS=$($CMD | wc -l)
3700         [ $NUMS -eq $EXPECTED ] ||
3701                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3702         echo "test" > $TDIR/$tfile
3703         echo "test2" > $TDIR/$tfile.2 && sync
3704         EXPECTED=1
3705         CMD="$LFIND -size 5 -type f $TDIR"
3706         NUMS=$($CMD | wc -l)
3707         [ $NUMS -eq $EXPECTED ] ||
3708                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3709         EXPECTED=1
3710         CMD="$LFIND -size +5 -type f $TDIR"
3711         NUMS=$($CMD | wc -l)
3712         [ $NUMS -eq $EXPECTED ] ||
3713                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3714         EXPECTED=2
3715         CMD="$LFIND -size +0 -type f $TDIR"
3716         NUMS=$($CMD | wc -l)
3717         [ $NUMS -eq $EXPECTED ] ||
3718                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3719         EXPECTED=2
3720         CMD="$LFIND ! -size -5 -type f $TDIR"
3721         NUMS=$($CMD | wc -l)
3722         [ $NUMS -eq $EXPECTED ] ||
3723                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3724         EXPECTED=12
3725         CMD="$LFIND -size -5 -type f $TDIR"
3726         NUMS=$($CMD | wc -l)
3727         [ $NUMS -eq $EXPECTED ] ||
3728                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3729 }
3730 run_test 56r "check lfs find -size works =========================="
3731
3732 test_56s() { # LU-611
3733         TDIR=$DIR/${tdir}s
3734         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3735
3736         if [ $OSTCOUNT -gt 1 ]; then
3737                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
3738                 ONESTRIPE=4
3739                 EXTRA=4
3740         else
3741                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
3742                 EXTRA=0
3743         fi
3744
3745         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3746         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
3747         NUMS=$($CMD | wc -l)
3748         [ $NUMS -eq $EXPECTED ] ||
3749                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3750
3751         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
3752         CMD="$LFIND -stripe-count +0 -type f $TDIR"
3753         NUMS=$($CMD | wc -l)
3754         [ $NUMS -eq $EXPECTED ] ||
3755                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3756
3757         EXPECTED=$ONESTRIPE
3758         CMD="$LFIND -stripe-count 1 -type f $TDIR"
3759         NUMS=$($CMD | wc -l)
3760         [ $NUMS -eq $EXPECTED ] ||
3761                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3762
3763         CMD="$LFIND -stripe-count -2 -type f $TDIR"
3764         NUMS=$($CMD | wc -l)
3765         [ $NUMS -eq $EXPECTED ] ||
3766                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3767
3768         EXPECTED=0
3769         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
3770         NUMS=$($CMD | wc -l)
3771         [ $NUMS -eq $EXPECTED ] ||
3772                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3773 }
3774 run_test 56s "check lfs find -stripe-count works"
3775
3776 test_56t() { # LU-611
3777         TDIR=$DIR/${tdir}t
3778         setup_56 $NUMFILES $NUMDIRS "-s 512k"
3779
3780         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
3781
3782         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3783         CMD="$LFIND -stripe-size 512k -type f $TDIR"
3784         NUMS=$($CMD | wc -l)
3785         [ $NUMS -eq $EXPECTED ] ||
3786                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3787
3788         CMD="$LFIND -stripe-size +320k -type f $TDIR"
3789         NUMS=$($CMD | wc -l)
3790         [ $NUMS -eq $EXPECTED ] ||
3791                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3792
3793         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
3794         CMD="$LFIND -stripe-size +200k -type f $TDIR"
3795         NUMS=$($CMD | wc -l)
3796         [ $NUMS -eq $EXPECTED ] ||
3797                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3798
3799         CMD="$LFIND -stripe-size -640k -type f $TDIR"
3800         NUMS=$($CMD | wc -l)
3801         [ $NUMS -eq $EXPECTED ] ||
3802                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3803
3804         EXPECTED=4
3805         CMD="$LFIND -stripe-size 256k -type f $TDIR"
3806         NUMS=$($CMD | wc -l)
3807         [ $NUMS -eq $EXPECTED ] ||
3808                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3809
3810         CMD="$LFIND -stripe-size -320k -type f $TDIR"
3811         NUMS=$($CMD | wc -l)
3812         [ $NUMS -eq $EXPECTED ] ||
3813                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3814
3815         EXPECTED=0
3816         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
3817         NUMS=$($CMD | wc -l)
3818         [ $NUMS -eq $EXPECTED ] ||
3819                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3820 }
3821 run_test 56t "check lfs find -stripe-size works"
3822
3823 test_56u() { # LU-611
3824         TDIR=$DIR/${tdir}u
3825         setup_56 $NUMFILES $NUMDIRS "-i 0"
3826
3827         if [ $OSTCOUNT -gt 1 ]; then
3828                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
3829                 ONESTRIPE=4
3830         else
3831                 ONESTRIPE=0
3832         fi
3833
3834         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
3835         CMD="$LFIND -stripe-index 0 -type f $TDIR"
3836         NUMS=$($CMD | wc -l)
3837         [ $NUMS -eq $EXPECTED ] ||
3838                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3839
3840         EXPECTED=$ONESTRIPE
3841         CMD="$LFIND -stripe-index 1 -type f $TDIR"
3842         NUMS=$($CMD | wc -l)
3843         [ $NUMS -eq $EXPECTED ] ||
3844                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3845
3846         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
3847         NUMS=$($CMD | wc -l)
3848         [ $NUMS -eq $EXPECTED ] ||
3849                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3850
3851         EXPECTED=0
3852         # This should produce an error and not return any files
3853         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
3854         NUMS=$($CMD 2>/dev/null | wc -l)
3855         [ $NUMS -eq $EXPECTED ] ||
3856                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3857
3858         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
3859         CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
3860         NUMS=$($CMD | wc -l)
3861         [ $NUMS -eq $EXPECTED ] ||
3862                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
3863 }
3864 run_test 56u "check lfs find -stripe-index works"
3865
3866 test_56v() {
3867     local MDT_IDX=0
3868
3869     TDIR=$DIR/${tdir}v
3870     rm -rf $TDIR
3871     setup_56 $NUMFILES $NUMDIRS
3872
3873     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
3874     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
3875
3876     for file in $($LFIND -mdt $UUID $TDIR); do
3877         file_mdt_idx=$($GETSTRIPE -M $file)
3878         [ $file_mdt_idx -eq $MDT_IDX ] ||
3879             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
3880     done
3881 }
3882 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
3883
3884 # Get and check the actual stripe count of one file.
3885 # Usage: check_stripe_count <file> <expected_stripe_count>
3886 check_stripe_count() {
3887     local file=$1
3888     local expected=$2
3889     local actual
3890
3891     [[ -z "$file" || -z "$expected" ]] &&
3892         error "check_stripe_count: invalid argument!"
3893
3894     local cmd="$GETSTRIPE -c $file"
3895     actual=$($cmd) || error "$cmd failed"
3896     actual=${actual%% *}
3897
3898     if [[ $actual -ne $expected ]]; then
3899         [[ $expected -eq -1 ]] ||
3900             error "$cmd wrong: found $actual, expected $expected"
3901         [[ $actual -eq $OSTCOUNT ]] ||
3902             error "$cmd wrong: found $actual, expected $OSTCOUNT"
3903     fi
3904 }
3905
3906 test_56w() {
3907     TDIR=$DIR/${tdir}w
3908
3909     rm -rf $TDIR || error "remove $TDIR failed"
3910     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
3911
3912     local stripe_size
3913     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
3914         error "$GETSTRIPE -S -d $TDIR failed"
3915     stripe_size=${stripe_size%% *}
3916
3917     local file_size=$((stripe_size * OSTCOUNT))
3918     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
3919     local required_space=$((file_num * file_size))
3920     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
3921     [[ $free_space -le $((required_space / 1024)) ]] &&
3922         skip_env "need at least $required_space bytes free space," \
3923                  "have $free_space kbytes" && return
3924
3925     local dd_bs=65536
3926     local dd_count=$((file_size / dd_bs))
3927
3928     # write data into the files
3929     local i
3930     local j
3931     local file
3932     for i in $(seq 1 $NUMFILES); do
3933         file=$TDIR/file$i
3934         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
3935             error "write data into $file failed"
3936     done
3937     for i in $(seq 1 $NUMDIRS); do
3938         for j in $(seq 1 $NUMFILES); do
3939             file=$TDIR/dir$i/file$j
3940             yes | dd bs=$dd_bs count=$dd_count of=$file \
3941                 >/dev/null 2>&1 ||
3942                 error "write data into $file failed"
3943         done
3944     done
3945
3946     local expected=-1
3947     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
3948
3949     # lfs_migrate file
3950     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
3951     echo "$cmd"
3952     eval $cmd || error "$cmd failed"
3953
3954     check_stripe_count $TDIR/file1 $expected
3955
3956     # lfs_migrate dir
3957     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
3958     echo "$cmd"
3959     eval $cmd || error "$cmd failed"
3960
3961     for j in $(seq 1 $NUMFILES); do
3962         check_stripe_count $TDIR/dir1/file$j $expected
3963     done
3964
3965     # lfs_migrate works with lfs find
3966     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
3967          $LFS_MIGRATE -y -c $expected"
3968     echo "$cmd"
3969     eval $cmd || error "$cmd failed"
3970
3971     for i in $(seq 2 $NUMFILES); do
3972         check_stripe_count $TDIR/file$i $expected
3973     done
3974     for i in $(seq 2 $NUMDIRS); do
3975         for j in $(seq 1 $NUMFILES); do
3976             check_stripe_count $TDIR/dir$i/file$j $expected
3977         done
3978     done
3979 }
3980 run_test 56w "check lfs_migrate -c stripe_count works"
3981
3982 test_57a() {
3983         # note test will not do anything if MDS is not local
3984         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3985         local MNTDEV="osd*.*MDT*.mntdev"
3986         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
3987         [ -z "$DEV" ] && error "can't access $MNTDEV"
3988         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
3989                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
3990                         error "can't access $DEV"
3991                 DEVISIZE=`awk '/Inode size:/ { print $3 }' $TMP/t57a.dump`
3992                 [ "$DEVISIZE" -gt 128 ] || error "inode size $DEVISIZE"
3993                 rm $TMP/t57a.dump
3994         done
3995 }
3996 run_test 57a "verify MDS filesystem created with large inodes =="
3997
3998 test_57b() {
3999         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4000         local dir=$DIR/d57b
4001
4002         local FILECOUNT=100
4003         local FILE1=$dir/f1
4004         local FILEN=$dir/f$FILECOUNT
4005
4006         rm -rf $dir || error "removing $dir"
4007         mkdir -p $dir || error "creating $dir"
4008         local num=$(get_mds_dir $dir)
4009         local mymds=mds$num
4010
4011         echo "mcreating $FILECOUNT files"
4012         createmany -m $dir/f 1 $FILECOUNT || \
4013                 error "creating files in $dir"
4014
4015         # verify that files do not have EAs yet
4016         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4017         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4018
4019         sync
4020         sleep 1
4021         df $dir  #make sure we get new statfs data
4022         local MDSFREE=$(do_facet $mymds \
4023                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4024         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4025         echo "opening files to create objects/EAs"
4026         local FILE
4027         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4028                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4029         done
4030
4031         # verify that files have EAs now
4032         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4033         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4034
4035         sleep 1  #make sure we get new statfs data
4036         df $dir
4037         local MDSFREE2=$(do_facet $mymds \
4038                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4039         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4040         if [ "$MDCFREE2" -lt "$((MDCFREE - 8))" ]; then
4041                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4042                         error "MDC before $MDCFREE != after $MDCFREE2"
4043                 else
4044                         echo "MDC before $MDCFREE != after $MDCFREE2"
4045                         echo "unable to confirm if MDS has large inodes"
4046                 fi
4047         fi
4048         rm -rf $dir
4049 }
4050 run_test 57b "default LOV EAs are stored inside large inodes ==="
4051
4052 test_58() {
4053     [ -z "$(which wiretest 2>/dev/null)" ] && skip_env "could not find wiretest" && return
4054     wiretest
4055 }
4056 run_test 58 "verify cross-platform wire constants =============="
4057
4058 test_59() {
4059         echo "touch 130 files"
4060         createmany -o $DIR/f59- 130
4061         echo "rm 130 files"
4062         unlinkmany $DIR/f59- 130
4063         sync
4064         sleep 2
4065         # wait for commitment of removal
4066 }
4067 run_test 59 "verify cancellation of llog records async ========="
4068
4069 TEST60_HEAD="test_60 run $RANDOM"
4070 test_60a() {
4071         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4072         [ ! -f run-llog.sh ] && skip_env "missing subtest run-llog.sh" && return
4073         log "$TEST60_HEAD - from kernel mode"
4074         do_facet mgs sh run-llog.sh
4075 }
4076 run_test 60a "llog sanity tests run from kernel module =========="
4077
4078 test_60b() { # bug 6411
4079         dmesg > $DIR/$tfile
4080         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4081                                  /llog.test/ {
4082                                          if (marker)
4083                                                  from_marker++
4084                                          from_begin++
4085                                  }
4086                                  END {
4087                                          if (marker)
4088                                                  print from_marker
4089                                          else
4090                                                  print from_begin
4091                                  }"`
4092         [ $LLOG_COUNT -gt 50 ] && error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)"|| true
4093 }
4094 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4095
4096 test_60c() {
4097         echo "create 5000 files"
4098         createmany -o $DIR/f60c- 5000
4099 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4100         lctl set_param fail_loc=0x80000137
4101         unlinkmany $DIR/f60c- 5000
4102         lctl set_param fail_loc=0
4103 }
4104 run_test 60c "unlink file when mds full"
4105
4106 test_60d() {
4107         SAVEPRINTK=$(lctl get_param -n printk)
4108
4109         # verify "lctl mark" is even working"
4110         MESSAGE="test message ID $RANDOM $$"
4111         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4112         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4113
4114         lctl set_param printk=0 || error "set lnet.printk failed"
4115         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4116         MESSAGE="new test message ID $RANDOM $$"
4117         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4118         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4119         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4120
4121         lctl set_param -n printk="$SAVEPRINTK"
4122 }
4123 run_test 60d "test printk console message masking"
4124
4125 test_61() {
4126         f="$DIR/f61"
4127         dd if=/dev/zero of=$f bs=`page_size` count=1
4128         cancel_lru_locks osc
4129         $MULTIOP $f OSMWUc || error
4130         sync
4131 }
4132 run_test 61 "mmap() writes don't make sync hang ================"
4133
4134 # bug 2330 - insufficient obd_match error checking causes LBUG
4135 test_62() {
4136         f="$DIR/f62"
4137         echo foo > $f
4138         cancel_lru_locks osc
4139         lctl set_param fail_loc=0x405
4140         cat $f && error "cat succeeded, expect -EIO"
4141         lctl set_param fail_loc=0
4142 }
4143 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4144 # match every page all of the time.
4145 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4146
4147 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4148 test_63a() {    # was test_63
4149         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4150         lctl set_param -n osc.*.max_dirty_mb 0
4151         for i in `seq 10` ; do
4152                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4153                 sleep 5
4154                 kill $!
4155                 sleep 1
4156         done
4157
4158         lctl set_param -n osc.*.max_dirty_mb $MAX_DIRTY_MB
4159         rm -f $DIR/f63 || true
4160 }
4161 run_test 63a "Verify oig_wait interruption does not crash ======="
4162
4163 # bug 2248 - async write errors didn't return to application on sync
4164 # bug 3677 - async write errors left page locked
4165 test_63b() {
4166         debugsave
4167         lctl set_param debug=-1
4168
4169         # ensure we have a grant to do async writes
4170         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4171         rm $DIR/$tfile
4172
4173         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
4174         lctl set_param fail_loc=0x80000406
4175         $MULTIOP $DIR/$tfile Owy && \
4176                 error "sync didn't return ENOMEM"
4177         sync; sleep 2; sync     # do a real sync this time to flush page
4178         lctl get_param -n llite.*.dump_page_cache | grep locked && \
4179                 error "locked page left in cache after async error" || true
4180         debugrestore
4181 }
4182 run_test 63b "async write errors should be returned to fsync ==="
4183
4184 test_64a () {
4185         df $DIR
4186         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
4187 }
4188 run_test 64a "verify filter grant calculations (in kernel) ====="
4189
4190 test_64b () {
4191         [ ! -f oos.sh ] && skip_env "missing subtest oos.sh" && return
4192         sh oos.sh $MOUNT
4193 }
4194 run_test 64b "check out-of-space detection on client ==========="
4195
4196 # bug 1414 - set/get directories' stripe info
4197 test_65a() {
4198         mkdir -p $DIR/d65
4199         touch $DIR/d65/f1
4200         $LVERIFY $DIR/d65 $DIR/d65/f1 || error "lverify failed"
4201 }
4202 run_test 65a "directory with no stripe info ===================="
4203
4204 test_65b() {
4205         mkdir -p $DIR/d65
4206         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65||error "setstripe"
4207         touch $DIR/d65/f2
4208         $LVERIFY $DIR/d65 $DIR/d65/f2 || error "lverify failed"
4209 }
4210 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
4211
4212 test_65c() {
4213         if [ $OSTCOUNT -gt 1 ]; then
4214                 mkdir -p $DIR/d65
4215                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
4216                         -c $(($OSTCOUNT - 1)) $DIR/d65 || error "setstripe"
4217                 touch $DIR/d65/f3
4218                 $LVERIFY $DIR/d65 $DIR/d65/f3 || error "lverify failed"
4219         fi
4220 }
4221 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
4222
4223 test_65d() {
4224         mkdir -p $DIR/d65
4225         if [ $STRIPECOUNT -le 0 ]; then
4226                 sc=1
4227         elif [ $STRIPECOUNT -gt 2000 ]; then
4228 #LOV_MAX_STRIPE_COUNT is 2000
4229                 [ $OSTCOUNT -gt 2000 ] && sc=2000 || sc=$(($OSTCOUNT - 1))
4230         else
4231                 sc=$(($STRIPECOUNT - 1))
4232         fi
4233         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/d65 || error "setstripe"
4234         touch $DIR/d65/f4 $DIR/d65/f5
4235         $LVERIFY $DIR/d65 $DIR/d65/f4 $DIR/d65/f5 || error "lverify failed"
4236 }
4237 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
4238
4239 test_65e() {
4240         mkdir -p $DIR/d65
4241
4242         $SETSTRIPE $DIR/d65 || error "setstripe"
4243         $GETSTRIPE -v $DIR/d65 | grep "Default" || error "no stripe info failed"
4244         touch $DIR/d65/f6
4245         $LVERIFY $DIR/d65 $DIR/d65/f6 || error "lverify failed"
4246 }
4247 run_test 65e "directory setstripe defaults ======================="
4248
4249 test_65f() {
4250         mkdir -p $DIR/d65f
4251         $RUNAS $SETSTRIPE $DIR/d65f && error "setstripe succeeded" || true
4252 }
4253 run_test 65f "dir setstripe permission (should return error) ==="
4254
4255 test_65g() {
4256         mkdir -p $DIR/d65
4257         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4258         $SETSTRIPE -d $DIR/d65 || error "setstripe"
4259         $GETSTRIPE -v $DIR/d65 | grep "Default" || \
4260                 error "delete default stripe failed"
4261 }
4262 run_test 65g "directory setstripe -d ==========================="
4263
4264 test_65h() {
4265         mkdir -p $DIR/d65
4266         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/d65 ||error "setstripe"
4267         mkdir -p $DIR/d65/dd1
4268         [ $($GETSTRIPE -c $DIR/d65) == $($GETSTRIPE -c $DIR/d65/dd1) ] ||
4269                 error "stripe info inherit failed"
4270 }
4271 run_test 65h "directory stripe info inherit ===================="
4272
4273 test_65i() { # bug6367
4274         $SETSTRIPE -S 65536 -c -1 $MOUNT
4275 }
4276 run_test 65i "set non-default striping on root directory (bug 6367)="
4277
4278 test_65ia() { # bug12836
4279         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
4280 }
4281 run_test 65ia "getstripe on -1 default directory striping"
4282
4283 test_65ib() { # bug12836
4284         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
4285 }
4286 run_test 65ib "getstripe -v on -1 default directory striping"
4287
4288 test_65ic() { # bug12836
4289         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
4290 }
4291 run_test 65ic "new find on -1 default directory striping"
4292
4293 test_65j() { # bug6367
4294         sync; sleep 1
4295         # if we aren't already remounting for each test, do so for this test
4296         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
4297                 cleanup || error "failed to unmount"
4298                 setup
4299         fi
4300         $SETSTRIPE -d $MOUNT || error "setstripe failed"
4301 }
4302 run_test 65j "set default striping on root directory (bug 6367)="
4303
4304 test_65k() { # bug11679
4305     [ "$OSTCOUNT" -lt 2 ] && skip_env "too few OSTs" && return
4306     remote_mds_nodsh && skip "remote MDS with nodsh" && return
4307
4308     echo "Check OST status: "
4309     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
4310               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
4311
4312     for OSC in $MDS_OSCS; do
4313         echo $OSC "is activate"
4314         do_facet $SINGLEMDS lctl --device %$OSC activate
4315     done
4316
4317     do_facet client mkdir -p $DIR/$tdir
4318     for INACTIVE_OSC in $MDS_OSCS; do
4319         echo "Deactivate: " $INACTIVE_OSC
4320         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
4321         for STRIPE_OSC in $MDS_OSCS; do
4322             OST=`osc_to_ost $STRIPE_OSC`
4323             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
4324                  awk -F: /$OST/'{ print $1 }' | head -n 1`
4325
4326             [ -f $DIR/$tdir/$IDX ] && continue
4327             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
4328             do_facet client $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
4329             RC=$?
4330             [ $RC -ne 0 ] && error "setstripe should have succeeded"
4331         done
4332         do_facet client rm -f $DIR/$tdir/*
4333         echo $INACTIVE_OSC "is Activate."
4334         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
4335     done
4336 }
4337 run_test 65k "validate manual striping works properly with deactivated OSCs"
4338
4339 test_65l() { # bug 12836
4340         mkdir -p $DIR/$tdir/test_dir
4341         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
4342         $LFS find -mtime -1 $DIR/$tdir >/dev/null
4343 }
4344 run_test 65l "lfs find on -1 stripe dir ========================"
4345
4346 # bug 2543 - update blocks count on client
4347 test_66() {
4348         COUNT=${COUNT:-8}
4349         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
4350         sync; sync_all_data; sync; sync_all_data
4351         cancel_lru_locks osc
4352         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
4353         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
4354 }
4355 run_test 66 "update inode blocks count on client ==============="
4356
4357 LLOOP=
4358 LLITELOOPLOAD=
4359 cleanup_68() {
4360         trap 0
4361         if [ ! -z "$LLOOP" ]; then
4362                 if swapon -s | grep -q $LLOOP; then
4363                         swapoff $LLOOP || error "swapoff failed"
4364                 fi
4365
4366                 $LCTL blockdev_detach $LLOOP || error "detach failed"
4367                 rm -f $LLOOP
4368                 unset LLOOP
4369         fi
4370         if [ ! -z "$LLITELOOPLOAD" ]; then
4371                 rmmod llite_lloop
4372                 unset LLITELOOPLOAD
4373         fi
4374         rm -f $DIR/f68*
4375 }
4376
4377 meminfo() {
4378         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
4379 }
4380
4381 swap_used() {
4382         swapon -s | awk '($1 == "'$1'") { print $4 }'
4383 }
4384
4385 # test case for lloop driver, basic function
4386 test_68a() {
4387         [ "$UID" != 0 ] && skip_env "must run as root" && return
4388         llite_lloop_enabled || \
4389                 { skip_env "llite_lloop module disabled" && return; }
4390
4391         trap cleanup_68 EXIT
4392
4393         if ! module_loaded llite_lloop; then
4394                 if load_module llite/llite_lloop; then
4395                         LLITELOOPLOAD=yes
4396                 else
4397                         skip_env "can't find module llite_lloop"
4398                         return
4399                 fi
4400         fi
4401
4402         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4403         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
4404         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
4405
4406         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
4407         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
4408
4409         cleanup_68
4410 }
4411 run_test 68a "lloop driver - basic test ========================"
4412
4413 # excercise swapping to lustre by adding a high priority swapfile entry
4414 # and then consuming memory until it is used.
4415 test_68b() {  # was test_68
4416         [ "$UID" != 0 ] && skip_env "must run as root" && return
4417         lctl get_param -n devices | grep -q obdfilter && \
4418                 skip "local OST" && return
4419
4420         grep -q llite_lloop /proc/modules
4421         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
4422
4423         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
4424                 skip "can't reliably test swap with TCP" && return
4425
4426         MEMTOTAL=`meminfo MemTotal`
4427         NR_BLOCKS=$((MEMTOTAL>>8))
4428         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
4429
4430         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
4431         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
4432         mkswap $DIR/f68b
4433
4434         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
4435
4436         trap cleanup_68 EXIT
4437
4438         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
4439
4440         echo "before: `swapon -s | grep $LLOOP`"
4441         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
4442         echo "after: `swapon -s | grep $LLOOP`"
4443         SWAPUSED=`swap_used $LLOOP`
4444
4445         cleanup_68
4446
4447         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
4448 }
4449 run_test 68b "support swapping to Lustre ========================"
4450
4451 # bug5265, obdfilter oa2dentry return -ENOENT
4452 # #define OBD_FAIL_OST_ENOENT 0x217
4453 test_69() {
4454         remote_ost_nodsh && skip "remote OST with nodsh" && return
4455
4456         f="$DIR/$tfile"
4457         $SETSTRIPE -c 1 -i 0 $f
4458
4459         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
4460
4461         do_facet ost1 lctl set_param fail_loc=0x217
4462         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
4463         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
4464
4465         do_facet ost1 lctl set_param fail_loc=0
4466         $DIRECTIO write $f 0 2 || error "write error"
4467
4468         cancel_lru_locks osc
4469         $DIRECTIO read $f 0 1 || error "read error"
4470
4471         do_facet ost1 lctl set_param fail_loc=0x217
4472         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
4473
4474         do_facet ost1 lctl set_param fail_loc=0
4475         rm -f $f
4476 }
4477 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
4478
4479 test_71() {
4480     mkdir -p $DIR/$tdir
4481     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
4482 }
4483 run_test 71 "Running dbench on lustre (don't segment fault) ===="
4484
4485 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
4486         check_kernel_version 43 || return 0
4487         [ "$RUNAS_ID" = "$UID" ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4488
4489         # Check that testing environment is properly set up. Skip if not
4490         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
4491                 skip_env "User $RUNAS_ID does not exist - skipping"
4492                 return 0
4493         }
4494         # We had better clear the $DIR to get enough space for dd
4495         rm -rf $DIR/*
4496         touch $DIR/f72
4497         chmod 777 $DIR/f72
4498         chmod ug+s $DIR/f72
4499         $RUNAS dd if=/dev/zero of=$DIR/f72 bs=512 count=1 || error
4500         # See if we are still setuid/sgid
4501         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on write"
4502         # Now test that MDS is updated too
4503         cancel_lru_locks mdc
4504         test -u $DIR/f72 -o -g $DIR/f72 && error "S/gid is not dropped on MDS"
4505         true
4506         rm -f $DIR/f72
4507 }
4508 run_test 72a "Test that remove suid works properly (bug5695) ===="
4509
4510 test_72b() { # bug 24226 -- keep mode setting when size is not changing
4511         local perm
4512
4513         [ "$RUNAS_ID" = "$UID" ] && \
4514                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4515         [ "$RUNAS_ID" -eq 0 ] && \
4516                 skip_env "RUNAS_ID = 0 -- skipping" && return
4517
4518         # Check that testing environment is properly set up. Skip if not
4519         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
4520                 skip_env "User $RUNAS_ID does not exist - skipping"
4521                 return 0
4522         }
4523         touch $DIR/${tfile}-f{g,u}
4524         mkdir $DIR/${tfile}-d{g,u}
4525         chmod 770 $DIR/${tfile}-{f,d}{g,u}
4526         chmod g+s $DIR/${tfile}-{f,d}g
4527         chmod u+s $DIR/${tfile}-{f,d}u
4528         for perm in 777 2777 4777; do
4529                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
4530                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
4531                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
4532                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
4533         done
4534         true
4535 }
4536 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
4537
4538 # bug 3462 - multiple simultaneous MDC requests
4539 test_73() {
4540         mkdir $DIR/d73-1
4541         mkdir $DIR/d73-2
4542         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
4543         pid1=$!
4544
4545         lctl set_param fail_loc=0x80000129
4546         $MULTIOP $DIR/d73-1/f73-2 Oc &
4547         sleep 1
4548         lctl set_param fail_loc=0
4549
4550         $MULTIOP $DIR/d73-2/f73-3 Oc &
4551         pid3=$!
4552
4553         kill -USR1 $pid1
4554         wait $pid1 || return 1
4555
4556         sleep 25
4557
4558         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
4559         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
4560         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
4561
4562         rm -rf $DIR/d73-*
4563 }
4564 run_test 73 "multiple MDC requests (should not deadlock)"
4565
4566 test_74a() { # bug 6149, 6184
4567         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4568         #
4569         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4570         # will spin in a tight reconnection loop
4571         touch $DIR/f74a
4572         lctl set_param fail_loc=0x8000030e
4573         # get any lock that won't be difficult - lookup works.
4574         ls $DIR/f74a
4575         lctl set_param fail_loc=0
4576         true
4577         rm -f $DIR/f74a
4578 }
4579 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
4580
4581 test_74b() { # bug 13310
4582         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
4583         #
4584         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
4585         # will spin in a tight reconnection loop
4586         lctl set_param fail_loc=0x8000030e
4587         # get a "difficult" lock
4588         touch $DIR/f74b
4589         lctl set_param fail_loc=0
4590         true
4591         rm -f $DIR/f74b
4592 }
4593 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
4594
4595 test_74c() {
4596 #define OBD_FAIL_LDLM_NEW_LOCK
4597         lctl set_param fail_loc=0x80000319
4598         touch $DIR/$tfile && error "Touch successful"
4599         true
4600 }
4601 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
4602
4603 num_inodes() {
4604         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
4605 }
4606
4607 get_inode_slab_tunables() {
4608         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
4609 }
4610
4611 set_inode_slab_tunables() {
4612         echo "lustre_inode_cache $1" > /proc/slabinfo
4613 }
4614
4615 test_76() { # Now for bug 20433, added originally in bug 1443
4616         local SLAB_SETTINGS=`get_inode_slab_tunables`
4617         local CPUS=`getconf _NPROCESSORS_ONLN`
4618         # we cannot set limit below 1 which means 1 inode in each
4619         # per-cpu cache is still allowed
4620         set_inode_slab_tunables "1 1 0"
4621         cancel_lru_locks osc
4622         BEFORE_INODES=`num_inodes`
4623         echo "before inodes: $BEFORE_INODES"
4624         local COUNT=1000
4625         [ "$SLOW" = "no" ] && COUNT=100
4626         for i in `seq $COUNT`; do
4627                 touch $DIR/$tfile
4628                 rm -f $DIR/$tfile
4629         done
4630         cancel_lru_locks osc
4631         AFTER_INODES=`num_inodes`
4632         echo "after inodes: $AFTER_INODES"
4633         local wait=0
4634         while [ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]; do
4635                 sleep 2
4636                 AFTER_INODES=`num_inodes`
4637                 wait=$((wait+2))
4638                 echo "wait $wait seconds inodes: $AFTER_INODES"
4639                 if [ $wait -gt 30 ]; then
4640                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
4641                 fi
4642         done
4643         set_inode_slab_tunables "$SLAB_SETTINGS"
4644 }
4645 run_test 76 "confirm clients recycle inodes properly ===="
4646
4647
4648 export ORIG_CSUM=""
4649 set_checksums()
4650 {
4651         # Note: in sptlrpc modes which enable its own bulk checksum, the
4652         # original crc32_le bulk checksum will be automatically disabled,
4653         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
4654         # will be checked by sptlrpc code against sptlrpc bulk checksum.
4655         # In this case set_checksums() will not be no-op, because sptlrpc
4656         # bulk checksum will be enabled all through the test.
4657
4658         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
4659         lctl set_param -n osc.*.checksums $1
4660         return 0
4661 }
4662
4663 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
4664                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
4665 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
4666 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
4667 set_checksum_type()
4668 {
4669         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
4670         log "set checksum type to $1"
4671         return 0
4672 }
4673 F77_TMP=$TMP/f77-temp
4674 F77SZ=8
4675 setup_f77() {
4676         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
4677                 error "error writing to $F77_TMP"
4678 }
4679
4680 test_77a() { # bug 10889
4681         $GSS && skip "could not run with gss" && return
4682         [ ! -f $F77_TMP ] && setup_f77
4683         set_checksums 1
4684         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
4685         set_checksums 0
4686         rm -f $DIR/$tfile
4687 }
4688 run_test 77a "normal checksum read/write operation ============="
4689
4690 test_77b() { # bug 10889
4691         $GSS && skip "could not run with gss" && return
4692         [ ! -f $F77_TMP ] && setup_f77
4693         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4694         lctl set_param fail_loc=0x80000409
4695         set_checksums 1
4696         dd if=$F77_TMP of=$DIR/f77b bs=1M count=$F77SZ conv=sync || \
4697                 error "dd error: $?"
4698         lctl set_param fail_loc=0
4699         set_checksums 0
4700 }
4701 run_test 77b "checksum error on client write ===================="
4702
4703 test_77c() { # bug 10889
4704         $GSS && skip "could not run with gss" && return
4705         [ ! -f $DIR/f77b ] && skip "requires 77b - skipping" && return
4706         set_checksums 1
4707         for algo in $CKSUM_TYPES; do
4708                 cancel_lru_locks osc
4709                 set_checksum_type $algo
4710                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4711                 lctl set_param fail_loc=0x80000408
4712                 cmp $F77_TMP $DIR/f77b || error "file compare failed"
4713                 lctl set_param fail_loc=0
4714         done
4715         set_checksums 0
4716         set_checksum_type $ORIG_CSUM_TYPE
4717         rm -f $DIR/f77b
4718 }
4719 run_test 77c "checksum error on client read ==================="
4720
4721 test_77d() { # bug 10889
4722         $GSS && skip "could not run with gss" && return
4723         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4724         lctl set_param fail_loc=0x80000409
4725         set_checksums 1
4726         directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4727                 error "direct write: rc=$?"
4728         lctl set_param fail_loc=0
4729         set_checksums 0
4730 }
4731 run_test 77d "checksum error on OST direct write ==============="
4732
4733 test_77e() { # bug 10889
4734         $GSS && skip "could not run with gss" && return
4735         [ ! -f $DIR/f77 ] && skip "requires 77d - skipping" && return
4736         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
4737         lctl set_param fail_loc=0x80000408
4738         set_checksums 1
4739         cancel_lru_locks osc
4740         directio read $DIR/f77 0 $F77SZ $((1024 * 1024)) || \
4741                 error "direct read: rc=$?"
4742         lctl set_param fail_loc=0
4743         set_checksums 0
4744 }
4745 run_test 77e "checksum error on OST direct read ================"
4746
4747 test_77f() { # bug 10889
4748         $GSS && skip "could not run with gss" && return
4749         set_checksums 1
4750         for algo in $CKSUM_TYPES; do
4751                 cancel_lru_locks osc
4752                 set_checksum_type $algo
4753                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
4754                 lctl set_param fail_loc=0x409
4755                 directio write $DIR/f77 0 $F77SZ $((1024 * 1024)) && \
4756                         error "direct write succeeded"
4757                 lctl set_param fail_loc=0
4758         done
4759         set_checksum_type $ORIG_CSUM_TYPE
4760         set_checksums 0
4761 }
4762 run_test 77f "repeat checksum error on write (expect error) ===="
4763
4764 test_77g() { # bug 10889
4765         $GSS && skip "could not run with gss" && return
4766         remote_ost_nodsh && skip "remote OST with nodsh" && return
4767
4768         [ ! -f $F77_TMP ] && setup_f77
4769
4770         $SETSTRIPE -c 1 -i 0 $DIR/f77g
4771         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
4772         do_facet ost1 lctl set_param fail_loc=0x8000021a
4773         set_checksums 1
4774         dd if=$F77_TMP of=$DIR/f77g bs=1M count=$F77SZ || \
4775                 error "write error: rc=$?"
4776         do_facet ost1 lctl set_param fail_loc=0
4777         set_checksums 0
4778 }
4779 run_test 77g "checksum error on OST write ======================"
4780
4781 test_77h() { # bug 10889
4782         $GSS && skip "could not run with gss" && return
4783         remote_ost_nodsh && skip "remote OST with nodsh" && return
4784
4785         [ ! -f $DIR/f77g ] && skip "requires 77g - skipping" && return
4786         cancel_lru_locks osc
4787         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
4788         do_facet ost1 lctl set_param fail_loc=0x8000021b
4789         set_checksums 1
4790         cmp $F77_TMP $DIR/f77g || error "file compare failed"
4791         do_facet ost1 lctl set_param fail_loc=0
4792         set_checksums 0
4793 }
4794 run_test 77h "checksum error on OST read ======================="
4795
4796 test_77i() { # bug 13805
4797         $GSS && skip "could not run with gss" && return
4798         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
4799         lctl set_param fail_loc=0x40b
4800         remount_client $MOUNT
4801         lctl set_param fail_loc=0
4802         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4803                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4804                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4805                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4806         done
4807         remount_client $MOUNT
4808 }
4809 run_test 77i "client not supporting OSD_CONNECT_CKSUM =========="
4810
4811 test_77j() { # bug 13805
4812         $GSS && skip "could not run with gss" && return
4813         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
4814         lctl set_param fail_loc=0x40c
4815         remount_client $MOUNT
4816         lctl set_param fail_loc=0
4817         sleep 2 # wait async osc connect to finish
4818         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
4819                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
4820                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
4821                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
4822         done
4823         remount_client $MOUNT
4824 }
4825 run_test 77j "client only supporting ADLER32 ===================="
4826
4827 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
4828 rm -f $F77_TMP
4829 unset F77_TMP
4830
4831 test_78() { # bug 10901
4832         remote_ost || { skip_env "local OST" && return; }
4833
4834         NSEQ=5
4835         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
4836         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
4837         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
4838         echo "MemTotal: $MEMTOTAL"
4839 # reserve 256MB of memory for the kernel and other running processes,
4840 # and then take 1/2 of the remaining memory for the read/write buffers.
4841     if [ $MEMTOTAL -gt 512 ] ;then
4842         MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
4843     else
4844         # for those poor memory-starved high-end clusters...
4845         MEMTOTAL=$((MEMTOTAL / 2))
4846     fi
4847         echo "Mem to use for directio: $MEMTOTAL"
4848         [ $F78SIZE -gt $MEMTOTAL ] && F78SIZE=$MEMTOTAL
4849         [ $F78SIZE -gt 512 ] && F78SIZE=512
4850         [ $F78SIZE -gt $((MAXFREE / 1024)) ] && F78SIZE=$((MAXFREE / 1024))
4851         SMALLESTOST=`lfs df $DIR |grep OST | awk '{print $4}' |sort -n |head -1`
4852         echo "Smallest OST: $SMALLESTOST"
4853         [ $SMALLESTOST -lt 10240 ] && \
4854                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
4855
4856         [ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ] && \
4857                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
4858
4859         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
4860         echo "File size: $F78SIZE"
4861         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
4862         for i in `seq 1 $NSEQ`
4863         do
4864                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
4865                 echo directIO rdwr round $i of $NSEQ
4866                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
4867         done
4868
4869         rm -f $DIR/$tfile
4870 }
4871 run_test 78 "handle large O_DIRECT writes correctly ============"
4872
4873 test_79() { # bug 12743
4874         wait_delete_completed
4875
4876         BKTOTAL=$(calc_osc_kbytes kbytestotal)
4877         BKFREE=$(calc_osc_kbytes kbytesfree)
4878         BKAVAIL=$(calc_osc_kbytes kbytesavail)
4879
4880         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
4881         DFTOTAL=`echo $STRING | cut -d, -f1`
4882         DFUSED=`echo $STRING  | cut -d, -f2`
4883         DFAVAIL=`echo $STRING | cut -d, -f3`
4884         DFFREE=$(($DFTOTAL - $DFUSED))
4885
4886         ALLOWANCE=$((64 * $OSTCOUNT))
4887
4888         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
4889            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
4890                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
4891         fi
4892         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
4893            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
4894                 error "df free($DFFREE) mismatch OST free($BKFREE)"
4895         fi
4896         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
4897            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
4898                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
4899         fi
4900 }
4901 run_test 79 "df report consistency check ======================="
4902
4903 test_80() { # bug 10718
4904         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
4905         sync; sleep 1; sync
4906         local BEFORE=`date +%s`
4907         cancel_lru_locks osc
4908         local AFTER=`date +%s`
4909         local DIFF=$((AFTER-BEFORE))
4910         if [ $DIFF -gt 1 ] ; then
4911                 error "elapsed for 1M@1T = $DIFF"
4912         fi
4913         true
4914         rm -f $DIR/$tfile
4915 }
4916 run_test 80 "Page eviction is equally fast at high offsets too  ===="
4917
4918 test_81a() { # LU-456
4919         remote_ost_nodsh && skip "remote OST with nodsh" && return
4920         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4921         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
4922         do_facet ost0 lctl set_param fail_loc=0x80000228
4923
4924         # write should trigger a retry and success
4925         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4926         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4927         RC=$?
4928         if [ $RC -ne 0 ] ; then
4929                 error "write should success, but failed for $RC"
4930         fi
4931 }
4932 run_test 81a "OST should retry write when get -ENOSPC ==============="
4933
4934 test_81b() { # LU-456
4935         remote_ost_nodsh && skip "remote OST with nodsh" && return
4936         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
4937         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
4938         do_facet ost0 lctl set_param fail_loc=0x228
4939
4940         # write should retry several times and return -ENOSPC finally
4941         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
4942         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
4943         RC=$?
4944         ENOSPC=28
4945         if [ $RC -ne $ENOSPC ] ; then
4946                 error "dd should fail for -ENOSPC, but succeed."
4947         fi
4948 }
4949 run_test 81b "OST should return -ENOSPC when retry still fails ======="
4950
4951 test_82() { # LU-1031
4952         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
4953         local gid1=14091995
4954         local gid2=16022000
4955
4956         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
4957         local MULTIPID1=$!
4958         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
4959         local MULTIPID2=$!
4960         kill -USR1 $MULTIPID2
4961         sleep 2
4962         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
4963                 error "First grouplock does not block second one"
4964         else
4965                 echo "Second grouplock blocks first one"
4966         fi
4967         kill -USR1 $MULTIPID1
4968         wait $MULTIPID1
4969         wait $MULTIPID2
4970 }
4971 run_test 82 "Basic grouplock test ==============================="
4972
4973 test_99a() {
4974         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && \
4975             return
4976         mkdir -p $DIR/d99cvsroot
4977         chown $RUNAS_ID $DIR/d99cvsroot
4978         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
4979         cd $TMP
4980
4981         $RUNAS cvs -d $DIR/d99cvsroot init || error
4982         cd $oldPWD
4983 }
4984 run_test 99a "cvs init ========================================="
4985
4986 test_99b() {
4987         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
4988         [ ! -d $DIR/d99cvsroot ] && test_99a
4989         cd /etc/init.d
4990         # some versions of cvs import exit(1) when asked to import links or
4991         # files they can't read.  ignore those files.
4992         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
4993                         ! -perm +4 -printf '-I %f\n')
4994         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
4995                 d99reposname vtag rtag
4996 }
4997 run_test 99b "cvs import ======================================="
4998
4999 test_99c() {
5000         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5001         [ ! -d $DIR/d99cvsroot ] && test_99b
5002         cd $DIR
5003         mkdir -p $DIR/d99reposname
5004         chown $RUNAS_ID $DIR/d99reposname
5005         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5006 }
5007 run_test 99c "cvs checkout ====================================="
5008
5009 test_99d() {
5010         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5011         [ ! -d $DIR/d99cvsroot ] && test_99c
5012         cd $DIR/d99reposname
5013         $RUNAS touch foo99
5014         $RUNAS cvs add -m 'addmsg' foo99
5015 }
5016 run_test 99d "cvs add =========================================="
5017
5018 test_99e() {
5019         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5020         [ ! -d $DIR/d99cvsroot ] && test_99c
5021         cd $DIR/d99reposname
5022         $RUNAS cvs update
5023 }
5024 run_test 99e "cvs update ======================================="
5025
5026 test_99f() {
5027         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5028         [ ! -d $DIR/d99cvsroot ] && test_99d
5029         cd $DIR/d99reposname
5030         $RUNAS cvs commit -m 'nomsg' foo99
5031     rm -fr $DIR/d99cvsroot
5032 }
5033 run_test 99f "cvs commit ======================================="
5034
5035 test_100() {
5036         [ "$NETTYPE" = tcp ] || \
5037                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5038                         return ; }
5039
5040         remote_ost_nodsh && skip "remote OST with nodsh" && return
5041         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5042         remote_servers || \
5043                 { skip "useless for local single node setup" && return; }
5044
5045         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5046                 [ "$PROT" != "tcp" ] && continue
5047                 RPORT=$(echo $REMOTE | cut -d: -f2)
5048                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5049
5050                 rc=0
5051                 LPORT=`echo $LOCAL | cut -d: -f2`
5052                 if [ $LPORT -ge 1024 ]; then
5053                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5054                         netstat -tna
5055                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5056                 fi
5057         done
5058         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5059 }
5060 run_test 100 "check local port using privileged port ==========="
5061
5062 function get_named_value()
5063 {
5064     local tag
5065
5066     tag=$1
5067     while read ;do
5068         line=$REPLY
5069         case $line in
5070         $tag*)
5071             echo $line | sed "s/^$tag[ ]*//"
5072             break
5073             ;;
5074         esac
5075     done
5076 }
5077
5078 export CACHE_MAX=`$LCTL get_param -n llite.*.max_cached_mb | head -n 1`
5079 cleanup_101a() {
5080         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5081         trap 0
5082 }
5083
5084 test_101a() {
5085         local s
5086         local discard
5087         local nreads=10000
5088         [ "$CPU" = "UML" ] && nreads=1000
5089         local cache_limit=32
5090
5091         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5092         trap cleanup_101a EXIT
5093         $LCTL set_param -n llite.*.read_ahead_stats 0
5094         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5095
5096         #
5097         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5098         #
5099         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5100         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5101
5102         discard=0
5103         for s in `$LCTL get_param -n llite.*.read_ahead_stats | \
5104                 get_named_value 'read but discarded' | cut -d" " -f1`; do
5105                         discard=$(($discard + $s))
5106         done
5107         cleanup_101a
5108
5109         if [ $(($discard * 10)) -gt $nreads ] ;then
5110                 $LCTL get_param osc.*-osc*.rpc_stats
5111                 $LCTL get_param llite.*.read_ahead_stats
5112                 error "too many ($discard) discarded pages"
5113         fi
5114         rm -f $DIR/$tfile || true
5115 }
5116 run_test 101a "check read-ahead for random reads ================"
5117
5118 setup_test101bc() {
5119         mkdir -p $DIR/$tdir
5120         STRIPE_SIZE=1048576
5121         STRIPE_COUNT=$OSTCOUNT
5122         STRIPE_OFFSET=0
5123
5124         local list=$(comma_list $(osts_nodes))
5125         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=0
5126         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=0
5127
5128         trap cleanup_test101bc EXIT
5129         # prepare the read-ahead file
5130         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
5131
5132         dd if=/dev/zero of=$DIR/$tfile bs=1024k count=100 2> /dev/null
5133 }
5134
5135 cleanup_test101bc() {
5136         trap 0
5137         rm -rf $DIR/$tdir
5138         rm -f $DIR/$tfile
5139
5140         local list=$(comma_list $(osts_nodes))
5141         do_nodes $list $LCTL set_param -n obdfilter.*.read_cache_enable=1
5142         do_nodes $list $LCTL set_param -n obdfilter.*.writethrough_cache_enable=1
5143 }
5144
5145 calc_total() {
5146         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
5147 }
5148
5149 ra_check_101() {
5150         local READ_SIZE=$1
5151         local STRIPE_SIZE=1048576
5152         local RA_INC=1048576
5153         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
5154         local FILE_LENGTH=$((64*100))
5155         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
5156                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
5157         DISCARD=`$LCTL get_param -n llite.*.read_ahead_stats | \
5158                         get_named_value 'read but discarded' | \
5159                         cut -d" " -f1 | calc_total`
5160         if [ $DISCARD -gt $discard_limit ]; then
5161                 $LCTL get_param llite.*.read_ahead_stats
5162                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
5163         else
5164                 echo "Read-ahead success for size ${READ_SIZE}"
5165         fi
5166 }
5167
5168 test_101b() {
5169         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping stride IO stride-ahead test" && return
5170         local STRIPE_SIZE=1048576
5171         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
5172         local FILE_LENGTH=$((STRIPE_SIZE*100))
5173         local ITERATION=$((FILE_LENGTH/STRIDE_SIZE))
5174         # prepare the read-ahead file
5175         setup_test101bc
5176         cancel_lru_locks osc
5177         for BIDX in 2 4 8 16 32 64 128 256
5178         do
5179                 local BSIZE=$((BIDX*4096))
5180                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
5181                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
5182                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
5183                 $LCTL set_param -n llite.*.read_ahead_stats 0
5184                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
5185                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
5186                 cancel_lru_locks osc
5187                 ra_check_101 $BSIZE
5188         done
5189         cleanup_test101bc
5190         true
5191 }
5192 run_test 101b "check stride-io mode read-ahead ================="
5193
5194 test_101c() {
5195     local STRIPE_SIZE=1048576
5196     local FILE_LENGTH=$((STRIPE_SIZE*100))
5197     local nreads=10000
5198     local osc
5199
5200     setup_test101bc
5201
5202     cancel_lru_locks osc
5203     $LCTL set_param osc.*.rpc_stats 0
5204     $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
5205     for osc in $($LCTL get_param -N osc.*); do
5206         if [ "$osc" == "osc.num_refs" ]; then
5207             continue
5208         fi
5209
5210         local lines=$($LCTL get_param -n ${osc}.rpc_stats | wc | awk '{print $1}')
5211         if [ $lines -le 20 ]; then
5212             continue
5213         fi
5214
5215         local rpc4k=$($LCTL get_param -n ${osc}.rpc_stats |
5216                                      awk '$1 == "1:" { print $2; exit; }')
5217         local rpc8k=$($LCTL get_param -n ${osc}.rpc_stats |
5218                                      awk '$1 == "2:" { print $2; exit; }')
5219         local rpc16k=$($LCTL get_param -n ${osc}.rpc_stats |
5220                                      awk '$1 == "4:" { print $2; exit; }')
5221         local rpc32k=$($LCTL get_param -n ${osc}.rpc_stats |
5222                                      awk '$1 == "8:" { print $2; exit; }')
5223
5224         [ $rpc4k != 0 ]  && error "Small 4k read IO ${rpc4k}!"
5225         [ $rpc8k != 0 ]  && error "Small 8k read IO ${rpc8k}!"
5226         [ $rpc16k != 0 ] && error "Small 16k read IO ${rpc16k}!"
5227         [ $rpc32k != 0 ] && error "Small 32k read IO ${rpc32k}!"
5228         echo "${osc} rpc check passed!"
5229     done
5230     cleanup_test101bc
5231     true
5232 }
5233 run_test 101c "check stripe_size aligned read-ahead ================="
5234
5235 set_read_ahead() {
5236    $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
5237    $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
5238 }
5239
5240 test_101d() {
5241     local file=$DIR/$tfile
5242     local size=${FILESIZE_101c:-500}
5243     local ra_MB=${READAHEAD_MB:-40}
5244
5245     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5246     [ $space -gt $((size / 1024)) ] ||
5247         { skip "Need free space ${size}M, have $space" && return; }
5248
5249     echo Creating ${size}M test file $file
5250     dd if=/dev/zero of=$file bs=1M count=$size
5251     echo Cancel LRU locks on lustre client to flush the client cache
5252     cancel_lru_locks osc
5253
5254     echo Disable read-ahead
5255     local old_READAHEAD=$(set_read_ahead 0)
5256
5257     echo Reading the test file $file with read-ahead disabled
5258     time_ra_OFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5259
5260     echo Cancel LRU locks on lustre client to flush the client cache
5261     cancel_lru_locks osc
5262     echo Enable read-ahead with ${ra_MB}MB
5263     set_read_ahead $ra_MB
5264
5265     echo Reading the test file $file with read-ahead enabled
5266     time_ra_ON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$size")
5267
5268     echo read-ahead disabled time read $time_ra_OFF
5269     echo read-ahead enabled  time read $time_ra_ON
5270
5271         set_read_ahead $old_READAHEAD
5272         rm -f $file
5273         wait_delete_completed
5274
5275     [ $time_ra_ON -lt $time_ra_OFF ] ||
5276         error "read-ahead enabled  time read (${time_ra_ON}s) is more than
5277                read-ahead disabled time read (${time_ra_OFF}s) filesize ${size}M"
5278 }
5279 run_test 101d "file read with and without read-ahead enabled  ================="
5280
5281 test_101e() {
5282     local file=$DIR/$tfile
5283     local size=500  #KB
5284     local count=100
5285     local blksize=1024
5286
5287     local space=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
5288     local need_space=$((count * size))
5289     [ $space -gt $need_space ] ||
5290         { skip_env "Need free space $need_space, have $space" && return; }
5291
5292     echo Creating $count ${size}K test files
5293     for ((i = 0; i < $count; i++)); do
5294         dd if=/dev/zero of=${file}_${i} bs=$blksize count=$size 2>/dev/null
5295     done
5296
5297     echo Cancel LRU locks on lustre client to flush the client cache
5298     cancel_lru_locks osc
5299
5300     echo Reset readahead stats
5301     $LCTL set_param -n llite.*.read_ahead_stats 0
5302
5303     for ((i = 0; i < $count; i++)); do
5304         dd if=${file}_${i} of=/dev/null bs=$blksize count=$size 2>/dev/null
5305     done
5306
5307     local miss=$($LCTL get_param -n llite.*.read_ahead_stats | \
5308           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5309
5310     for ((i = 0; i < $count; i++)); do
5311         rm -rf ${file}_${i} 2>/dev/null
5312     done
5313
5314     #10000 means 20% reads are missing in readahead
5315     [ $miss -lt 10000 ] ||  error "misses too much for small reads"
5316 }
5317 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
5318
5319 cleanup_test101f() {
5320     trap 0
5321     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
5322     rm -rf $DIR/$tfile 2>/dev/null
5323 }
5324
5325 test_101f() {
5326     local file=$DIR/$tfile
5327     local nreads=1000
5328
5329     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
5330     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
5331     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
5332     trap cleanup_test101f EXIT
5333
5334     echo Cancel LRU locks on lustre client to flush the client cache
5335     cancel_lru_locks osc
5336
5337     echo Reset readahead stats
5338     $LCTL set_param -n llite.*.read_ahead_stats 0
5339     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
5340     # readahead should read in 2M file on second read, so only miss
5341     # 2 pages.
5342     echo Random 4K reads on 2M file for 1000 times
5343     $READS -f $file -s 2097152 -b 4096 -n $nreads
5344
5345     echo checking missing pages
5346     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
5347           get_named_value 'misses' | cut -d" " -f1 | calc_total)
5348
5349     [ $miss -lt 3 ] || error "misses too much pages!"
5350     cleanup_test101f
5351 }
5352 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
5353
5354 setup_test102() {
5355         mkdir -p $DIR/$tdir
5356         chown $RUNAS_ID $DIR/$tdir
5357         STRIPE_SIZE=65536
5358         STRIPE_OFFSET=1
5359         STRIPE_COUNT=$OSTCOUNT
5360         [ $OSTCOUNT -gt 4 ] && STRIPE_COUNT=4
5361
5362         trap cleanup_test102 EXIT
5363         cd $DIR
5364         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
5365         cd $DIR/$tdir
5366         for num in 1 2 3 4; do
5367                 for count in $(seq 1 $STRIPE_COUNT); do
5368                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
5369                                 local size=`expr $STRIPE_SIZE \* $num`
5370                                 local file=file"$num-$idx-$count"
5371                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
5372                         done
5373                 done
5374         done
5375
5376         cd $DIR
5377         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
5378 }
5379
5380 cleanup_test102() {
5381         trap 0
5382         rm -f $TMP/f102.tar
5383         rm -rf $DIR/d0.sanity/d102
5384 }
5385
5386 test_102a() {
5387         local testfile=$DIR/xattr_testfile
5388
5389         touch $testfile
5390
5391         [ "$UID" != 0 ] && skip_env "must run as root" && return
5392         [ -z "`lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr`" ] &&
5393                 skip_env "must have user_xattr" && return
5394
5395         [ -z "$(which setfattr 2>/dev/null)" ] &&
5396                 skip_env "could not find setfattr" && return
5397
5398         echo "set/get xattr..."
5399         setfattr -n trusted.name1 -v value1 $testfile || error
5400         getfattr -n trusted.name1 $testfile 2> /dev/null |
5401           grep "trusted.name1=.value1" ||
5402                 error "$testfile missing trusted.name1=value1"
5403
5404         setfattr -n user.author1 -v author1 $testfile || error
5405         getfattr -n user.author1 $testfile 2> /dev/null |
5406           grep "user.author1=.author1" ||
5407                 error "$testfile missing trusted.author1=author1"
5408
5409         echo "listxattr..."
5410         setfattr -n trusted.name2 -v value2 $testfile ||
5411                 error "$testfile unable to set trusted.name2"
5412         setfattr -n trusted.name3 -v value3 $testfile ||
5413                 error "$testfile unable to set trusted.name3"
5414         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
5415             grep "trusted.name" | wc -l) -eq 3 ] ||
5416                 error "$testfile missing 3 trusted.name xattrs"
5417
5418         setfattr -n user.author2 -v author2 $testfile ||
5419                 error "$testfile unable to set user.author2"
5420         setfattr -n user.author3 -v author3 $testfile ||
5421                 error "$testfile unable to set user.author3"
5422         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
5423             grep "user.author" | wc -l) -eq 3 ] ||
5424                 error "$testfile missing 3 user.author xattrs"
5425
5426         echo "remove xattr..."
5427         setfattr -x trusted.name1 $testfile ||
5428                 error "$testfile error deleting trusted.name1"
5429         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
5430                 error "$testfile did not delete trusted.name1 xattr"
5431
5432         setfattr -x user.author1 $testfile ||
5433                 error "$testfile error deleting user.author1"
5434         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
5435                 error "$testfile did not delete trusted.name1 xattr"
5436
5437         # b10667: setting lustre special xattr be silently discarded
5438         echo "set lustre special xattr ..."
5439         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
5440                 error "$testfile allowed setting trusted.lov"
5441 }
5442 run_test 102a "user xattr test =================================="
5443
5444 test_102b() {
5445         # b10930: get/set/list trusted.lov xattr
5446         echo "get/set/list trusted.lov xattr ..."
5447         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5448         local testfile=$DIR/$tfile
5449         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5450                 error "setstripe failed"
5451         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
5452                 error "getstripe failed"
5453         getfattr -d -m "^trusted" $testfile 2> /dev/null | \
5454         grep "trusted.lov" || error "can't get trusted.lov from $testfile"
5455
5456         local testfile2=${testfile}2
5457         local value=`getfattr -n trusted.lov $testfile 2> /dev/null | \
5458                      grep "trusted.lov" |sed -e 's/[^=]\+=//'`
5459
5460         $MCREATE $testfile2
5461         setfattr -n trusted.lov -v $value $testfile2
5462         local stripe_size=$($GETSTRIPE -S $testfile2)
5463         local stripe_count=$($GETSTRIPE -c $testfile2)
5464         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5465         [ $stripe_count -eq $STRIPECOUNT ] ||
5466                 error "stripe count $stripe_count != $STRIPECOUNT"
5467         rm -f $DIR/$tfile
5468 }
5469 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
5470
5471 test_102c() {
5472         # b10930: get/set/list lustre.lov xattr
5473         echo "get/set/list lustre.lov xattr ..."
5474         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
5475         mkdir -p $DIR/$tdir
5476         chown $RUNAS_ID $DIR/$tdir
5477         local testfile=$DIR/$tdir/$tfile
5478         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
5479                 error "setstripe failed"
5480         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
5481                 error "getstripe failed"
5482         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
5483         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
5484
5485         local testfile2=${testfile}2
5486         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
5487                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
5488
5489         $RUNAS $MCREATE $testfile2
5490         $RUNAS setfattr -n lustre.lov -v $value $testfile2
5491         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
5492         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
5493         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
5494         [ $stripe_count -eq $STRIPECOUNT ] ||
5495                 error "stripe count $stripe_count != $STRIPECOUNT"
5496 }
5497 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
5498
5499 compare_stripe_info1() {
5500         local stripe_index_all_zero=true
5501
5502         for num in 1 2 3 4; do
5503                 for count in $(seq 1 $STRIPE_COUNT); do
5504                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
5505                                 local size=$((STRIPE_SIZE * num))
5506                                 local file=file"$num-$offset-$count"
5507                                 stripe_size=$(lfs getstripe -S $PWD/$file)
5508                                 [ $stripe_size -ne $size ] &&
5509                                     error "$file: size $stripe_size != $size"
5510                                 stripe_count=$(lfs getstripe -c $PWD/$file)
5511                                 # allow fewer stripes to be created, ORI-601
5512                                 [ $stripe_count -lt $(((3 * count + 3) / 4)) ]&&
5513                                     error "$file: count $stripe_count != $count"
5514                                 stripe_index=$(lfs getstripe -i $PWD/$file)
5515                                 [ $stripe_index -ne 0 ] &&
5516                                         stripe_index_all_zero=false
5517                         done
5518                 done
5519         done
5520         $stripe_index_all_zero &&
5521                 error "all files are being extracted starting from OST index 0"
5522         return 0
5523 }
5524
5525 find_lustre_tar() {
5526         [ -n "$(which tar 2>/dev/null)" ] &&
5527                 strings $(which tar) | grep -q "lustre" && echo tar
5528 }
5529
5530 test_102d() {
5531         # b10930: tar test for trusted.lov xattr
5532         TAR=$(find_lustre_tar)
5533         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5534         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5535         setup_test102
5536         mkdir -p $DIR/d102d
5537         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
5538         cd $DIR/d102d/$tdir
5539         compare_stripe_info1
5540 }
5541 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
5542
5543 test_102f() {
5544         # b10930: tar test for trusted.lov xattr
5545         TAR=$(find_lustre_tar)
5546         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5547         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5548         setup_test102
5549         mkdir -p $DIR/d102f
5550         cd $DIR
5551         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
5552         cd $DIR/d102f/$tdir
5553         compare_stripe_info1
5554 }
5555 run_test 102f "tar copy files, not keep osts ==========="
5556
5557 grow_xattr() {
5558         local xsize=${1:-1024}  # in bytes
5559         local file=$DIR/$tfile
5560
5561         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
5562                 skip "must have user_xattr" && return 0
5563         [ -z "$(which setfattr 2>/dev/null)" ] &&
5564                 skip_env "could not find setfattr" && return 0
5565         [ -z "$(which getfattr 2>/dev/null)" ] &&
5566                 skip_env "could not find getfattr" && return 0
5567
5568         touch $file
5569
5570         local value="$(generate_string $xsize)"
5571
5572         local xbig=trusted.big
5573         log "save $xbig on $file"
5574         setfattr -n $xbig -v $value $file ||
5575                 error "saving $xbig on $file failed"
5576
5577         local orig=$(get_xattr_value $xbig $file)
5578         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
5579
5580         local xsml=trusted.sml
5581         log "save $xsml on $file"
5582         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
5583
5584         local new=$(get_xattr_value $xbig $file)
5585         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
5586
5587         log "grow $xsml on $file"
5588         setfattr -n $xsml -v "$value" $file ||
5589                 error "growing $xsml on $file failed"
5590
5591         new=$(get_xattr_value $xbig $file)
5592         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
5593         log "$xbig still valid after growing $xsml"
5594
5595         rm -f $file
5596 }
5597
5598 test_102h() { # bug 15777
5599         grow_xattr 1024
5600 }
5601 run_test 102h "grow xattr from inside inode to external block"
5602
5603 test_102ha() {
5604         large_xattr_enabled || { skip "large_xattr disabled" && return; }
5605         grow_xattr $(max_xattr_size)
5606 }
5607 run_test 102ha "grow xattr from inside inode to external inode"
5608
5609 test_102i() { # bug 17038
5610         touch $DIR/$tfile
5611         ln -s $DIR/$tfile $DIR/${tfile}link
5612         getfattr -n trusted.lov $DIR/$tfile || error "lgetxattr on $DIR/$tfile failed"
5613         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"
5614         rm -f $DIR/$tfile $DIR/${tfile}link
5615 }
5616 run_test 102i "lgetxattr test on symbolic link ============"
5617
5618 test_102j() {
5619         TAR=$(find_lustre_tar)
5620         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
5621         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping N-stripe test" && return
5622         setup_test102 "$RUNAS"
5623         mkdir -p $DIR/d102j
5624         chown $RUNAS_ID $DIR/d102j
5625         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
5626         cd $DIR/d102j/$tdir
5627         compare_stripe_info1 "$RUNAS"
5628 }
5629 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
5630
5631 test_102k() {
5632         touch $DIR/$tfile
5633         # b22187 just check that does not crash for regular file.
5634         setfattr -n trusted.lov $DIR/$tfile
5635         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
5636         local test_kdir=$DIR/d102k
5637         mkdir $test_kdir
5638         local default_size=`$GETSTRIPE -S $test_kdir`
5639         local default_count=`$GETSTRIPE -c $test_kdir`
5640         local default_offset=`$GETSTRIPE -i $test_kdir`
5641         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $test_kdir ||
5642                 error 'dir setstripe failed'
5643         setfattr -n trusted.lov $test_kdir
5644         local stripe_size=`$GETSTRIPE -S $test_kdir`
5645         local stripe_count=`$GETSTRIPE -c $test_kdir`
5646         local stripe_offset=`$GETSTRIPE -i $test_kdir`
5647         [ $stripe_size -eq $default_size ] ||
5648                 error "stripe size $stripe_size != $default_size"
5649         [ $stripe_count -eq $default_count ] ||
5650                 error "stripe count $stripe_count != $default_count"
5651         [ $stripe_offset -eq $default_offset ] ||
5652                 error "stripe offset $stripe_offset != $default_offset"
5653         rm -rf $DIR/$tfile $test_kdir
5654 }
5655 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
5656
5657 test_102l() {
5658         # LU-532 trusted. xattr is invisible to non-root
5659         local testfile=$DIR/$tfile
5660
5661         touch $testfile
5662
5663         echo "listxattr as user..."
5664         chown $RUNAS_ID $testfile
5665         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
5666             grep -q "trusted" &&
5667                 error "$testfile trusted xattrs are user visible"
5668
5669         return 0;
5670 }
5671 run_test 102l "listxattr filter test =================================="
5672
5673 cleanup_test102
5674
5675 run_acl_subtest()
5676 {
5677     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
5678     return $?
5679 }
5680
5681 test_103 () {
5682     [ "$UID" != 0 ] && skip_env "must run as root" && return
5683     [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
5684     [ -z "$(which setfacl 2>/dev/null)" ] && skip_env "could not find setfacl" && return
5685     $GSS && skip "could not run under gss" && return
5686
5687     declare -a identity_old
5688
5689     for num in `seq $MDSCOUNT`; do
5690         switch_identity $num true || identity_old[$num]=$?
5691     done
5692
5693     SAVE_UMASK=`umask`
5694     umask 0022
5695     cd $DIR
5696
5697     echo "performing cp ..."
5698     run_acl_subtest cp || error
5699     echo "performing getfacl-noacl..."
5700     run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
5701     echo "performing misc..."
5702     run_acl_subtest misc || error  "misc test failed"
5703     echo "performing permissions..."
5704     run_acl_subtest permissions || error "permissions failed"
5705     echo "performing setfacl..."
5706     run_acl_subtest setfacl || error  "setfacl test failed"
5707
5708     # inheritance test got from HP
5709     echo "performing inheritance..."
5710     cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
5711     chmod +x make-tree || error "chmod +x failed"
5712     run_acl_subtest inheritance || error "inheritance test failed"
5713     rm -f make-tree
5714
5715     cd $SAVE_PWD
5716     umask $SAVE_UMASK
5717
5718     for num in `seq $MDSCOUNT`; do
5719         if [ "${identity_old[$num]}" = 1 ]; then
5720             switch_identity $num false || identity_old[$num]=$?
5721         fi
5722     done
5723 }
5724 run_test 103 "acl test ========================================="
5725
5726 test_104a() {
5727         touch $DIR/$tfile
5728         lfs df || error "lfs df failed"
5729         lfs df -ih || error "lfs df -ih failed"
5730         lfs df -h $DIR || error "lfs df -h $DIR failed"
5731         lfs df -i $DIR || error "lfs df -i $DIR failed"
5732         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
5733         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
5734
5735         OSC=`lctl dl |grep OST0000-osc-[^M] |awk '{print $4}'`
5736         lctl --device %$OSC deactivate
5737         lfs df || error "lfs df with deactivated OSC failed"
5738         lctl --device %$OSC activate
5739         # wait the osc back to normal
5740         wait_osc_import_state client ost FULL
5741
5742         lfs df || error "lfs df with reactivated OSC failed"
5743         rm -f $DIR/$tfile
5744 }
5745 run_test 104a "lfs df [-ih] [path] test ========================="
5746
5747 test_104b() {
5748         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5749         chmod 666 /dev/obd
5750         denied_cnt=$((`$RUNAS $LFS check servers 2>&1 | grep "Permission denied" | wc -l`))
5751         if [ $denied_cnt -ne 0 ];
5752         then
5753                     error "lfs check servers test failed"
5754         fi
5755 }
5756 run_test 104b "$RUNAS lfs check servers test ===================="
5757
5758 test_105a() {
5759         # doesn't work on 2.4 kernels
5760         touch $DIR/$tfile
5761         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5762         then
5763                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
5764         else
5765                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
5766         fi
5767         rm -f $DIR/$tfile
5768 }
5769 run_test 105a "flock when mounted without -o flock test ========"
5770
5771 test_105b() {
5772         touch $DIR/$tfile
5773         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5774         then
5775                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
5776         else
5777                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
5778         fi
5779         rm -f $DIR/$tfile
5780 }
5781 run_test 105b "fcntl when mounted without -o flock test ========"
5782
5783 test_105c() {
5784         touch $DIR/$tfile
5785         if [ -n "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ];
5786         then
5787                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
5788         else
5789                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
5790         fi
5791         rm -f $DIR/$tfile
5792 }
5793 run_test 105c "lockf when mounted without -o flock test ========"
5794
5795 test_105d() { # bug 15924
5796         mkdir -p $DIR/$tdir
5797         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5798                 skip "mount w/o flock enabled" && return
5799         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
5800         $LCTL set_param fail_loc=0x80000315
5801         flocks_test 2 $DIR/$tdir
5802 }
5803 run_test 105d "flock race (should not freeze) ========"
5804
5805 test_105e() { # bug 22660 && 22040
5806         [ -z "`mount | grep \"$DIR.*flock\" | grep -v noflock`" ] && \
5807                 skip "mount w/o flock enabled" && return
5808         touch $DIR/$tfile
5809         flocks_test 3 $DIR/$tfile
5810 }
5811 run_test 105e "Two conflicting flocks from same process ======="
5812
5813 test_106() { #bug 10921
5814         mkdir -p $DIR/$tdir
5815         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
5816         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
5817 }
5818 run_test 106 "attempt exec of dir followed by chown of that dir"
5819
5820 test_107() {
5821         CDIR=`pwd`
5822         cd $DIR
5823
5824         local file=core
5825         rm -f $file
5826
5827         local save_pattern=$(sysctl -n kernel.core_pattern)
5828         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
5829         sysctl -w kernel.core_pattern=$file
5830         sysctl -w kernel.core_uses_pid=0
5831
5832         ulimit -c unlimited
5833         sleep 60 &
5834         SLEEPPID=$!
5835
5836         sleep 1
5837
5838         kill -s 11 $SLEEPPID
5839         wait $SLEEPPID
5840         if [ -e $file ]; then
5841                 size=`stat -c%s $file`
5842                 [ $size -eq 0 ] && error "Fail to create core file $file"
5843         else
5844                 error "Fail to create core file $file"
5845         fi
5846         rm -f $file
5847         sysctl -w kernel.core_pattern=$save_pattern
5848         sysctl -w kernel.core_uses_pid=$save_uses_pid
5849         cd $CDIR
5850 }
5851 run_test 107 "Coredump on SIG"
5852
5853 test_110() {
5854         mkdir -p $DIR/d110
5855         mkdir $DIR/d110/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || error "mkdir with 255 char fail"
5856         mkdir $DIR/d110/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && error "mkdir with 256 char should fail, but not"
5857         touch $DIR/d110/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx || error "create with 255 char fail"
5858         touch $DIR/d110/yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy && error ""create with 256 char should fail, but not
5859
5860         ls -l $DIR/d110
5861     rm -fr $DIR/d110
5862 }
5863 run_test 110 "filename length checking"
5864
5865 test_115() {
5866         OSTIO_pre=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5867             cut -c11-20)
5868         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && \
5869             return
5870         echo "Starting with $OSTIO_pre threads"
5871
5872         NUMTEST=20000
5873         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
5874         [ $NUMFREE -lt $NUMTEST ] && NUMTEST=$(($NUMFREE - 1000))
5875         echo "$NUMTEST creates/unlinks"
5876         mkdir -p $DIR/$tdir
5877         createmany -o $DIR/$tdir/$tfile $NUMTEST
5878         unlinkmany $DIR/$tdir/$tfile $NUMTEST
5879
5880         OSTIO_post=$(ps -e|grep ll_ost_io|awk '{print $4}'|sort -n|tail -1|\
5881             cut -c11-20)
5882
5883         # don't return an error
5884         [ $OSTIO_post == $OSTIO_pre ] && echo \
5885             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
5886             echo "This may be fine, depending on what ran before this test" &&
5887             echo "and how fast this system is." && return
5888
5889         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
5890 }
5891 run_test 115 "verify dynamic thread creation===================="
5892
5893 free_min_max () {
5894         wait_delete_completed
5895         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
5896         echo OST kbytes available: ${AVAIL[@]}
5897         MAXI=0; MAXV=${AVAIL[0]}
5898         MINI=0; MINV=${AVAIL[0]}
5899         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
5900             #echo OST $i: ${AVAIL[i]}kb
5901             if [ ${AVAIL[i]} -gt $MAXV ]; then
5902                 MAXV=${AVAIL[i]}; MAXI=$i
5903             fi
5904             if [ ${AVAIL[i]} -lt $MINV ]; then
5905                 MINV=${AVAIL[i]}; MINI=$i
5906             fi
5907         done
5908         echo Min free space: OST $MINI: $MINV
5909         echo Max free space: OST $MAXI: $MAXV
5910 }
5911
5912 test_116() {
5913         [ "$OSTCOUNT" -lt "2" ] && skip_env "$OSTCOUNT < 2 OSTs" && return
5914
5915         echo -n "Free space priority "
5916         lctl get_param -n lov.*-clilov-*.qos_prio_free
5917         declare -a AVAIL
5918         free_min_max
5919         [ $MINV -gt 960000 ] && skip "too much free space in OST$MINI, skip" &&\
5920                 return
5921
5922         # generate uneven OSTs
5923         mkdir -p $DIR/$tdir/OST${MINI}
5924         declare -i FILL
5925         FILL=$(($MINV / 4))
5926         echo "Filling 25% remaining space in OST${MINI} with ${FILL}Kb"
5927         $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI}||error "setstripe failed"
5928         i=0
5929         while [ $FILL -gt 0 ]; do
5930             i=$(($i + 1))
5931             dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i bs=2M count=1 2>/dev/null
5932             FILL=$(($FILL - 2048))
5933             echo -n .
5934         done
5935         FILL=$(($MINV / 4))
5936         sync
5937         sleep_maxage
5938
5939         free_min_max
5940         DIFF=$(($MAXV - $MINV))
5941         DIFF2=$(($DIFF * 100 / $MINV))
5942         echo -n "diff=${DIFF}=${DIFF2}% must be > 20% for QOS mode..."
5943         if [ $DIFF2 -gt 20 ]; then
5944             echo "ok"
5945         else
5946             echo "failed - QOS mode won't be used"
5947             error_ignore "QOS imbalance criteria not met"
5948             return
5949         fi
5950
5951         MINI1=$MINI; MINV1=$MINV
5952         MAXI1=$MAXI; MAXV1=$MAXV
5953
5954         # now fill using QOS
5955         echo writing a bunch of files to QOS-assigned OSTs
5956         $SETSTRIPE -c 1 $DIR/$tdir
5957         i=0
5958         while [ $FILL -gt 0 ]; do
5959             i=$(($i + 1))
5960             dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1024 count=200 2>/dev/null
5961             FILL=$(($FILL - 200))
5962             echo -n .
5963         done
5964         echo "wrote $i 200k files"
5965         sync
5966         sleep_maxage
5967
5968         echo "Note: free space may not be updated, so measurements might be off"
5969         free_min_max
5970         DIFF2=$(($MAXV - $MINV))
5971         echo "free space delta: orig $DIFF final $DIFF2"
5972         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
5973         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
5974         echo "Wrote $DIFF to smaller OST $MINI1"
5975         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
5976         echo "Wrote $DIFF2 to larger OST $MAXI1"
5977         [ $DIFF -gt 0 ] && echo "Wrote $(($DIFF2 * 100 / $DIFF - 100))% more data to larger OST $MAXI1"
5978
5979         # Figure out which files were written where
5980         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
5981                awk '/'$MINI1': / {print $2; exit}')
5982         echo $UUID
5983         MINC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
5984         echo "$MINC files created on smaller OST $MINI1"
5985         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
5986                awk '/'$MAXI1': / {print $2; exit}')
5987         echo $UUID
5988         MAXC=$($GETSTRIPE --obd $UUID $DIR/$tdir | wc -l)
5989         echo "$MAXC files created on larger OST $MAXI1"
5990         [ $MINC -gt 0 ] && echo "Wrote $(($MAXC * 100 / $MINC - 100))% more files to larger OST $MAXI1"
5991         [ $MAXC -gt $MINC ] || error_ignore "stripe QOS didn't balance free space"
5992
5993         rm -rf $DIR/$tdir
5994 }
5995 run_test 116 "stripe QOS: free space balance ==================="
5996
5997 test_117() # bug 10891
5998 {
5999         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
6000         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
6001         lctl set_param fail_loc=0x21e
6002         > $DIR/$tfile || error "truncate failed"
6003         lctl set_param fail_loc=0
6004         echo "Truncate succeeded."
6005         rm -f $DIR/$tfile
6006 }
6007 run_test 117 "verify fsfilt_extend =========="
6008
6009 export OLD_RESENDCOUNT=""
6010 set_resend_count () {
6011         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
6012         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -1)
6013         lctl set_param -n $PROC_RESENDCOUNT $1
6014         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
6015 }
6016
6017 [ "$SLOW" = "no" ] && set_resend_count 4 # for reduce test_118* time (bug 14842)
6018
6019 # Reset async IO behavior after error case
6020 reset_async() {
6021         FILE=$DIR/reset_async
6022
6023         # Ensure all OSCs are cleared
6024         $SETSTRIPE -c -1 $FILE
6025         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
6026         sync
6027         rm $FILE
6028 }
6029
6030 test_118a() #bug 11710
6031 {
6032         reset_async
6033
6034         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6035         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6036         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6037
6038         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6039                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6040                 return 1;
6041         fi
6042         rm -f $DIR/$tfile
6043 }
6044 run_test 118a "verify O_SYNC works =========="
6045
6046 test_118b()
6047 {
6048         remote_ost_nodsh && skip "remote OST with nodsh" && return
6049
6050         reset_async
6051
6052         #define OBD_FAIL_OST_ENOENT 0x217
6053         set_nodes_failloc "$(osts_nodes)" 0x217
6054         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6055         RC=$?
6056         set_nodes_failloc "$(osts_nodes)" 0
6057         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6058         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6059                     grep -c writeback)
6060
6061         if [[ $RC -eq 0 ]]; then
6062                 error "Must return error due to dropped pages, rc=$RC"
6063                 return 1;
6064         fi
6065
6066         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6067                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6068                 return 1;
6069         fi
6070
6071         echo "Dirty pages not leaked on ENOENT"
6072
6073         # Due to the above error the OSC will issue all RPCs syncronously
6074         # until a subsequent RPC completes successfully without error.
6075         $MULTIOP $DIR/$tfile Ow4096yc
6076         rm -f $DIR/$tfile
6077
6078         return 0
6079 }
6080 run_test 118b "Reclaim dirty pages on fatal error =========="
6081
6082 test_118c()
6083 {
6084         remote_ost_nodsh && skip "remote OST with nodsh" && return
6085
6086         reset_async
6087
6088         #define OBD_FAIL_OST_EROFS               0x216
6089         set_nodes_failloc "$(osts_nodes)" 0x216
6090
6091         # multiop should block due to fsync until pages are written
6092         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6093         MULTIPID=$!
6094         sleep 1
6095
6096         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6097                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6098         fi
6099
6100         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6101                     grep -c writeback)
6102         if [[ $WRITEBACK -eq 0 ]]; then
6103                 error "No page in writeback, writeback=$WRITEBACK"
6104         fi
6105
6106         set_nodes_failloc "$(osts_nodes)" 0
6107         wait $MULTIPID
6108         RC=$?
6109         if [[ $RC -ne 0 ]]; then
6110                 error "Multiop fsync failed, rc=$RC"
6111         fi
6112
6113         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6114         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6115                     grep -c writeback)
6116         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6117                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6118         fi
6119
6120         rm -f $DIR/$tfile
6121         echo "Dirty pages flushed via fsync on EROFS"
6122         return 0
6123 }
6124 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
6125
6126 test_118d()
6127 {
6128         remote_ost_nodsh && skip "remote OST with nodsh" && return
6129
6130         reset_async
6131
6132         #define OBD_FAIL_OST_BRW_PAUSE_BULK
6133         set_nodes_failloc "$(osts_nodes)" 0x214
6134         # multiop should block due to fsync until pages are written
6135         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6136         MULTIPID=$!
6137         sleep 1
6138
6139         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
6140                 error "Multiop failed to block on fsync, pid=$MULTIPID"
6141         fi
6142
6143         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6144                     grep -c writeback)
6145         if [[ $WRITEBACK -eq 0 ]]; then
6146                 error "No page in writeback, writeback=$WRITEBACK"
6147         fi
6148
6149         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
6150         set_nodes_failloc "$(osts_nodes)" 0
6151
6152         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6153         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6154                     grep -c writeback)
6155         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6156                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6157         fi
6158
6159         rm -f $DIR/$tfile
6160         echo "Dirty pages gaurenteed flushed via fsync"
6161         return 0
6162 }
6163 run_test 118d "Fsync validation inject a delay of the bulk =========="
6164
6165 test_118f() {
6166         reset_async
6167
6168         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
6169         lctl set_param fail_loc=0x8000040a
6170
6171         # Should simulate EINVAL error which is fatal
6172         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6173         RC=$?
6174         if [[ $RC -eq 0 ]]; then
6175                 error "Must return error due to dropped pages, rc=$RC"
6176         fi
6177
6178         lctl set_param fail_loc=0x0
6179
6180         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6181         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6182         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6183                     grep -c writeback)
6184         if [[ $LOCKED -ne 0 ]]; then
6185                 error "Locked pages remain in cache, locked=$LOCKED"
6186         fi
6187
6188         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6189                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6190         fi
6191
6192         rm -f $DIR/$tfile
6193         echo "No pages locked after fsync"
6194
6195         reset_async
6196         return 0
6197 }
6198 run_test 118f "Simulate unrecoverable OSC side error =========="
6199
6200 test_118g() {
6201         reset_async
6202
6203         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6204         lctl set_param fail_loc=0x406
6205
6206         # simulate local -ENOMEM
6207         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6208         RC=$?
6209
6210         lctl set_param fail_loc=0
6211         if [[ $RC -eq 0 ]]; then
6212                 error "Must return error due to dropped pages, rc=$RC"
6213         fi
6214
6215         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6216         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6217         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6218                         grep -c writeback)
6219         if [[ $LOCKED -ne 0 ]]; then
6220                 error "Locked pages remain in cache, locked=$LOCKED"
6221         fi
6222
6223         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6224                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6225         fi
6226
6227         rm -f $DIR/$tfile
6228         echo "No pages locked after fsync"
6229
6230         reset_async
6231         return 0
6232 }
6233 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
6234
6235 test_118h() {
6236         remote_ost_nodsh && skip "remote OST with nodsh" && return
6237
6238         reset_async
6239
6240         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6241         set_nodes_failloc "$(osts_nodes)" 0x20e
6242         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6243         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6244         RC=$?
6245
6246         set_nodes_failloc "$(osts_nodes)" 0
6247         if [[ $RC -eq 0 ]]; then
6248                 error "Must return error due to dropped pages, rc=$RC"
6249         fi
6250
6251         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6252         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6253         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
6254                     grep -c writeback)
6255         if [[ $LOCKED -ne 0 ]]; then
6256                 error "Locked pages remain in cache, locked=$LOCKED"
6257         fi
6258
6259         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6260                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6261         fi
6262
6263         rm -f $DIR/$tfile
6264         echo "No pages locked after fsync"
6265
6266         return 0
6267 }
6268 run_test 118h "Verify timeout in handling recoverables errors  =========="
6269
6270 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6271
6272 test_118i() {
6273         remote_ost_nodsh && skip "remote OST with nodsh" && return
6274
6275         reset_async
6276
6277         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6278         set_nodes_failloc "$(osts_nodes)" 0x20e
6279
6280         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
6281         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
6282         PID=$!
6283         sleep 5
6284         set_nodes_failloc "$(osts_nodes)" 0
6285
6286         wait $PID
6287         RC=$?
6288         if [[ $RC -ne 0 ]]; then
6289                 error "got error, but should be not, rc=$RC"
6290         fi
6291
6292         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6293         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6294         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6295         if [[ $LOCKED -ne 0 ]]; then
6296                 error "Locked pages remain in cache, locked=$LOCKED"
6297         fi
6298
6299         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6300                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6301         fi
6302
6303         rm -f $DIR/$tfile
6304         echo "No pages locked after fsync"
6305
6306         return 0
6307 }
6308 run_test 118i "Fix error before timeout in recoverable error  =========="
6309
6310 [ "$SLOW" = "no" ] && set_resend_count 4
6311
6312 test_118j() {
6313         remote_ost_nodsh && skip "remote OST with nodsh" && return
6314
6315         reset_async
6316
6317         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
6318         set_nodes_failloc "$(osts_nodes)" 0x220
6319
6320         # return -EIO from OST
6321         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6322         RC=$?
6323         set_nodes_failloc "$(osts_nodes)" 0x0
6324         if [[ $RC -eq 0 ]]; then
6325                 error "Must return error due to dropped pages, rc=$RC"
6326         fi
6327
6328         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
6329         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
6330         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
6331         if [[ $LOCKED -ne 0 ]]; then
6332                 error "Locked pages remain in cache, locked=$LOCKED"
6333         fi
6334
6335         # in recoverable error on OST we want resend and stay until it finished
6336         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
6337                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
6338         fi
6339
6340         rm -f $DIR/$tfile
6341         echo "No pages locked after fsync"
6342
6343         return 0
6344 }
6345 run_test 118j "Simulate unrecoverable OST side error =========="
6346
6347 test_118k()
6348 {
6349         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
6350
6351         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
6352         set_nodes_failloc "$(osts_nodes)" 0x20e
6353         mkdir -p $DIR/$tdir
6354
6355         for ((i=0;i<10;i++)); do
6356                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
6357                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
6358                 SLEEPPID=$!
6359                 sleep 0.500s
6360                 kill $SLEEPPID
6361                 wait $SLEEPPID
6362         done
6363
6364         set_nodes_failloc "$(osts_nodes)" 0
6365         rm -rf $DIR/$tdir
6366 }
6367 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
6368
6369 test_118l()
6370 {
6371         # LU-646
6372         mkdir -p $DIR/$tdir
6373         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
6374         rm -rf $DIR/$tdir
6375 }
6376 run_test 118l "fsync dir ========="
6377
6378 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
6379
6380 test_119a() # bug 11737
6381 {
6382         BSIZE=$((512 * 1024))
6383         directio write $DIR/$tfile 0 1 $BSIZE
6384         # We ask to read two blocks, which is more than a file size.
6385         # directio will indicate an error when requested and actual
6386         # sizes aren't equeal (a normal situation in this case) and
6387         # print actual read amount.
6388         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
6389         if [ "$NOB" != "$BSIZE" ]; then
6390                 error "read $NOB bytes instead of $BSIZE"
6391         fi
6392         rm -f $DIR/$tfile
6393 }
6394 run_test 119a "Short directIO read must return actual read amount"
6395
6396 test_119b() # bug 11737
6397 {
6398         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
6399
6400         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
6401         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
6402         sync
6403         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
6404                 error "direct read failed"
6405         rm -f $DIR/$tfile
6406 }
6407 run_test 119b "Sparse directIO read must return actual read amount"
6408
6409 test_119c() # bug 13099
6410 {
6411         BSIZE=1048576
6412         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
6413         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
6414         rm -f $DIR/$tfile
6415 }
6416 run_test 119c "Testing for direct read hitting hole"
6417
6418 test_119d() # bug 15950
6419 {
6420         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
6421         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
6422         BSIZE=1048576
6423         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
6424         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
6425         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
6426         lctl set_param fail_loc=0x40d
6427         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
6428         pid_dio=$!
6429         sleep 1
6430         cat $DIR/$tfile > /dev/null &
6431         lctl set_param fail_loc=0
6432         pid_reads=$!
6433         wait $pid_dio
6434         log "the DIO writes have completed, now wait for the reads (should not block very long)"
6435         sleep 2
6436         [ -n "`ps h -p $pid_reads -o comm`" ] && \
6437         error "the read rpcs have not completed in 2s"
6438         rm -f $DIR/$tfile
6439         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
6440 }
6441 run_test 119d "The DIO path should try to send a new rpc once one is completed"
6442
6443 test_120a() {
6444         mkdir -p $DIR/$tdir
6445         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6446                skip "no early lock cancel on server" && return 0
6447         lru_resize_disable mdc
6448         lru_resize_disable osc
6449         cancel_lru_locks mdc
6450         stat $DIR/$tdir > /dev/null
6451         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6452         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6453         mkdir $DIR/$tdir/d1
6454         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6455         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6456         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6457         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6458         lru_resize_enable mdc
6459         lru_resize_enable osc
6460 }
6461 run_test 120a "Early Lock Cancel: mkdir test"
6462
6463 test_120b() {
6464         mkdir -p $DIR/$tdir
6465         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6466                skip "no early lock cancel on server" && return 0
6467         lru_resize_disable mdc
6468         lru_resize_disable osc
6469         cancel_lru_locks mdc
6470         stat $DIR/$tdir > /dev/null
6471         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6472         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6473         touch $DIR/$tdir/f1
6474         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6475         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6476         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6477         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6478         lru_resize_enable mdc
6479         lru_resize_enable osc
6480 }
6481 run_test 120b "Early Lock Cancel: create test"
6482
6483 test_120c() {
6484         mkdir -p $DIR/$tdir
6485         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6486                skip "no early lock cancel on server" && return 0
6487         lru_resize_disable mdc
6488         lru_resize_disable osc
6489         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6490         touch $DIR/$tdir/d1/f1
6491         cancel_lru_locks mdc
6492         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
6493         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6494         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6495         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6496         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6497         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6498         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6499         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6500         lru_resize_enable mdc
6501         lru_resize_enable osc
6502 }
6503 run_test 120c "Early Lock Cancel: link test"
6504
6505 test_120d() {
6506         mkdir -p $DIR/$tdir
6507         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6508                skip "no early lock cancel on server" && return 0
6509         lru_resize_disable mdc
6510         lru_resize_disable osc
6511         touch $DIR/$tdir
6512         cancel_lru_locks mdc
6513         stat $DIR/$tdir > /dev/null
6514         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6515         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6516         chmod a+x $DIR/$tdir
6517         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats | awk '/ldlm_cancel/ {print $2}'`
6518         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats | awk '/ldlm_bl_callback/ {print $2}'`
6519         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6520         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6521         lru_resize_enable mdc
6522         lru_resize_enable osc
6523 }
6524 run_test 120d "Early Lock Cancel: setattr test"
6525
6526 test_120e() {
6527         mkdir -p $DIR/$tdir
6528         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6529                skip "no early lock cancel on server" && return 0
6530         lru_resize_disable mdc
6531         lru_resize_disable osc
6532         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
6533         cancel_lru_locks mdc
6534         cancel_lru_locks osc
6535         dd if=$DIR/$tdir/f1 of=/dev/null
6536         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
6537         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6538               awk '/ldlm_cancel/ {print $2}'`
6539         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6540               awk '/ldlm_bl_callback/ {print $2}'`
6541         unlink $DIR/$tdir/f1
6542         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6543               awk '/ldlm_cancel/ {print $2}'`
6544         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6545               awk '/ldlm_bl_callback/ {print $2}'`
6546         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6547         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6548         lru_resize_enable mdc
6549         lru_resize_enable osc
6550 }
6551 run_test 120e "Early Lock Cancel: unlink test"
6552
6553 test_120f() {
6554         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6555                skip "no early lock cancel on server" && return 0
6556         mkdir -p $DIR/$tdir
6557         lru_resize_disable mdc
6558         lru_resize_disable osc
6559         mkdir -p $DIR/$tdir/d1 $DIR/$tdir/d2
6560         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
6561         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
6562         cancel_lru_locks mdc
6563         cancel_lru_locks osc
6564         dd if=$DIR/$tdir/d1/f1 of=/dev/null
6565         dd if=$DIR/$tdir/d2/f2 of=/dev/null
6566         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
6567         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6568               awk '/ldlm_cancel/ {print $2}'`
6569         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6570               awk '/ldlm_bl_callback/ {print $2}'`
6571         mv $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
6572         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6573               awk '/ldlm_cancel/ {print $2}'`
6574         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6575               awk '/ldlm_bl_callback/ {print $2}'`
6576         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
6577         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
6578         lru_resize_enable mdc
6579         lru_resize_enable osc
6580 }
6581 run_test 120f "Early Lock Cancel: rename test"
6582
6583 test_120g() {
6584         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
6585                skip "no early lock cancel on server" && return 0
6586         lru_resize_disable mdc
6587         lru_resize_disable osc
6588         count=10000
6589         echo create $count files
6590         mkdir -p $DIR/$tdir
6591         cancel_lru_locks mdc
6592         cancel_lru_locks osc
6593         t0=`date +%s`
6594
6595         can0=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6596               awk '/ldlm_cancel/ {print $2}'`
6597         blk0=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6598               awk '/ldlm_bl_callback/ {print $2}'`
6599         createmany -o $DIR/$tdir/f $count
6600         sync
6601         can1=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6602               awk '/ldlm_cancel/ {print $2}'`
6603         blk1=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6604               awk '/ldlm_bl_callback/ {print $2}'`
6605         t1=`date +%s`
6606         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
6607         echo rm $count files
6608         rm -r $DIR/$tdir
6609         sync
6610         can2=`lctl get_param -n ldlm.services.ldlm_canceld.stats |
6611               awk '/ldlm_cancel/ {print $2}'`
6612         blk2=`lctl get_param -n ldlm.services.ldlm_cbd.stats |
6613               awk '/ldlm_bl_callback/ {print $2}'`
6614         t2=`date +%s`
6615         echo total: $count removes in $((t2-t1))
6616         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
6617         sleep 2
6618         # wait for commitment of removal
6619         lru_resize_enable mdc
6620         lru_resize_enable osc
6621 }
6622 run_test 120g "Early Lock Cancel: performance test"
6623
6624 test_121() { #bug #10589
6625         rm -rf $DIR/$tfile
6626         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
6627 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
6628         lctl set_param fail_loc=0x310
6629         cancel_lru_locks osc > /dev/null
6630         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
6631         lctl set_param fail_loc=0
6632         [ "$reads" -eq "$writes" ] || error "read" $reads "blocks, must be" $writes
6633 }
6634 run_test 121 "read cancel race ========="
6635
6636 test_123a() { # was test 123, statahead(bug 11401)
6637         SLOWOK=0
6638         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
6639             log "testing on UP system. Performance may be not as good as expected."
6640                         SLOWOK=1
6641         fi
6642
6643         rm -rf $DIR/$tdir
6644         mkdir -p $DIR/$tdir
6645         NUMFREE=`df -i -P $DIR | tail -n 1 | awk '{ print $4 }'`
6646         [ $NUMFREE -gt 100000 ] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
6647         MULT=10
6648         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
6649                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
6650
6651                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6652                 lctl set_param -n llite.*.statahead_max 0
6653                 lctl get_param llite.*.statahead_max
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=$((etime - stime))
6660                 log "ls $i files without statahead: $delta sec"
6661                 lctl set_param llite.*.statahead_max=$max
6662
6663                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6664                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
6665                 cancel_lru_locks mdc
6666                 cancel_lru_locks osc
6667                 stime=`date +%s`
6668                 time ls -l $DIR/$tdir | wc -l
6669                 etime=`date +%s`
6670                 delta_sa=$((etime - stime))
6671                 log "ls $i files with statahead: $delta_sa sec"
6672                 lctl get_param -n llite.*.statahead_stats
6673                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
6674
6675                 [ $swrong -lt $ewrong ] && log "statahead was stopped, maybe too many locks held!"
6676                 [ $delta -eq 0 -o $delta_sa -eq 0 ] && continue
6677
6678                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6679                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
6680                     lctl set_param -n llite.*.statahead_max 0
6681                     lctl get_param llite.*.statahead_max
6682                     cancel_lru_locks mdc
6683                     cancel_lru_locks osc
6684                     stime=`date +%s`
6685                     time ls -l $DIR/$tdir | wc -l
6686                     etime=`date +%s`
6687                     delta=$((etime - stime))
6688                     log "ls $i files again without statahead: $delta sec"
6689                     lctl set_param llite.*.statahead_max=$max
6690                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
6691                         if [  $SLOWOK -eq 0 ]; then
6692                                 error "ls $i files is slower with statahead!"
6693                         else
6694                                 log "ls $i files is slower with statahead!"
6695                         fi
6696                         break
6697                     fi
6698                 fi
6699
6700                 [ $delta -gt 20 ] && break
6701                 [ $delta -gt 8 ] && MULT=$((50 / delta))
6702                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
6703         done
6704         log "ls done"
6705
6706         stime=`date +%s`
6707         rm -r $DIR/$tdir
6708         sync
6709         etime=`date +%s`
6710         delta=$((etime - stime))
6711         log "rm -r $DIR/$tdir/: $delta seconds"
6712         log "rm done"
6713         lctl get_param -n llite.*.statahead_stats
6714 }
6715 run_test 123a "verify statahead work"
6716
6717 test_123b () { # statahead(bug 15027)
6718         mkdir -p $DIR/$tdir
6719         createmany -o $DIR/$tdir/$tfile-%d 1000
6720
6721         cancel_lru_locks mdc
6722         cancel_lru_locks osc
6723
6724 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
6725         lctl set_param fail_loc=0x80000803
6726         ls -lR $DIR/$tdir > /dev/null
6727         log "ls done"
6728         lctl set_param fail_loc=0x0
6729         lctl get_param -n llite.*.statahead_stats
6730         rm -r $DIR/$tdir
6731         sync
6732
6733 }
6734 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
6735
6736 test_124a() {
6737         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6738                skip "no lru resize on server" && return 0
6739         local NR=2000
6740         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
6741
6742         log "create $NR files at $DIR/$tdir"
6743         createmany -o $DIR/$tdir/f $NR ||
6744                 error "failed to create $NR files in $DIR/$tdir"
6745
6746         cancel_lru_locks mdc
6747         ls -l $DIR/$tdir > /dev/null
6748
6749         local NSDIR=""
6750         local LRU_SIZE=0
6751         for VALUE in `lctl get_param ldlm.namespaces.*mdc-*.lru_size`; do
6752                 local PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
6753                 LRU_SIZE=$(lctl get_param -n $PARAM)
6754                 if [ $LRU_SIZE -gt $(default_lru_size) ]; then
6755                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
6756                                                 log "NSDIR=$NSDIR"
6757                         log "NS=$(basename $NSDIR)"
6758                         break
6759                 fi
6760         done
6761
6762         if [ -z "$NSDIR" -o $LRU_SIZE -lt $(default_lru_size) ]; then
6763                 skip "Not enough cached locks created!"
6764                 return 0
6765         fi
6766         log "LRU=$LRU_SIZE"
6767
6768         local SLEEP=30
6769
6770         # We know that lru resize allows one client to hold $LIMIT locks
6771         # for 10h. After that locks begin to be killed by client.
6772         local MAX_HRS=10
6773         local LIMIT=`lctl get_param -n $NSDIR.pool.limit`
6774                 log "LIMIT=$LIMIT"
6775
6776         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
6777         # killing locks. Some time was spent for creating locks. This means
6778         # that up to the moment of sleep finish we must have killed some of
6779         # them (10-100 locks). This depends on how fast ther were created.
6780         # Many of them were touched in almost the same moment and thus will
6781         # be killed in groups.
6782         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
6783
6784         # Use $LRU_SIZE_B here to take into account real number of locks
6785         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
6786         local LRU_SIZE_B=$LRU_SIZE
6787         log "LVF=$LVF"
6788         local OLD_LVF=`lctl get_param -n $NSDIR.pool.lock_volume_factor`
6789                 log "OLD_LVF=$OLD_LVF"
6790         lctl set_param -n $NSDIR.pool.lock_volume_factor $LVF
6791
6792         # Let's make sure that we really have some margin. Client checks
6793         # cached locks every 10 sec.
6794         SLEEP=$((SLEEP+20))
6795         log "Sleep ${SLEEP} sec"
6796         local SEC=0
6797         while ((SEC<$SLEEP)); do
6798                 echo -n "..."
6799                 sleep 5
6800                 SEC=$((SEC+5))
6801                 LRU_SIZE=`lctl get_param -n $NSDIR/lru_size`
6802                 echo -n "$LRU_SIZE"
6803         done
6804         echo ""
6805         lctl set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
6806         local LRU_SIZE_A=`lctl get_param -n $NSDIR.lru_size`
6807
6808         [ $LRU_SIZE_B -gt $LRU_SIZE_A ] || {
6809                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
6810                 unlinkmany $DIR/$tdir/f $NR
6811                 return
6812         }
6813
6814         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
6815         log "unlink $NR files at $DIR/$tdir"
6816         unlinkmany $DIR/$tdir/f $NR
6817 }
6818 run_test 124a "lru resize ======================================="
6819
6820 get_max_pool_limit()
6821 {
6822         local limit=`lctl get_param -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit`
6823         local max=0
6824         for l in $limit; do
6825                 if test $l -gt $max; then
6826                         max=$l
6827                 fi
6828         done
6829         echo $max
6830 }
6831
6832 test_124b() {
6833         [ -z "`lctl get_param -n mdc.*.connect_flags | grep lru_resize`" ] && \
6834                skip "no lru resize on server" && return 0
6835
6836         LIMIT=`get_max_pool_limit`
6837
6838         NR=$(($(default_lru_size)*20))
6839         if [ $NR -gt $LIMIT ]; then
6840                 log "Limit lock number by $LIMIT locks"
6841                 NR=$LIMIT
6842         fi
6843         lru_resize_disable mdc
6844         mkdir -p $DIR/$tdir/disable_lru_resize ||
6845                 error "failed to create $DIR/$tdir/disable_lru_resize"
6846
6847         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
6848         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
6849         cancel_lru_locks mdc
6850         stime=`date +%s`
6851         PID=""
6852         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6853         PID="$PID $!"
6854         sleep 2
6855         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6856         PID="$PID $!"
6857         sleep 2
6858         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
6859         PID="$PID $!"
6860         wait $PID
6861         etime=`date +%s`
6862         nolruresize_delta=$((etime-stime))
6863         log "ls -la time: $nolruresize_delta seconds"
6864         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6865         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
6866
6867         lru_resize_enable mdc
6868         mkdir -p $DIR/$tdir/enable_lru_resize ||
6869                 error "failed to create $DIR/$tdir/enable_lru_resize"
6870
6871         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
6872         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
6873         cancel_lru_locks mdc
6874         stime=`date +%s`
6875         PID=""
6876         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6877         PID="$PID $!"
6878         sleep 2
6879         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6880         PID="$PID $!"
6881         sleep 2
6882         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
6883         PID="$PID $!"
6884         wait $PID
6885         etime=`date +%s`
6886         lruresize_delta=$((etime-stime))
6887         log "ls -la time: $lruresize_delta seconds"
6888         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
6889
6890         if [ $lruresize_delta -gt $nolruresize_delta ]; then
6891                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
6892         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
6893                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
6894         else
6895                 log "lru resize performs the same with no lru resize"
6896         fi
6897         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
6898 }
6899 run_test 124b "lru resize (performance test) ======================="
6900
6901 test_125() { # 13358
6902         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6903         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
6904         mkdir -p $DIR/d125 || error "mkdir failed"
6905         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
6906         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
6907         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
6908 }
6909 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
6910
6911 test_126() { # bug 12829/13455
6912         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
6913         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
6914         $GSS && skip "must run as gss disabled" && return
6915
6916         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
6917         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
6918         rm -f $DIR/$tfile
6919         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
6920 }
6921 run_test 126 "check that the fsgid provided by the client is taken into account"
6922
6923 test_127a() { # bug 15521
6924         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
6925         $LCTL set_param osc.*.stats=0
6926         FSIZE=$((2048 * 1024))
6927         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6928         cancel_lru_locks osc
6929         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
6930
6931         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
6932         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
6933                 echo "got $COUNT $NAME"
6934                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
6935                 eval $NAME=$COUNT || error "Wrong proc format"
6936
6937                 case $NAME in
6938                         read_bytes|write_bytes)
6939                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
6940                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
6941                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
6942                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
6943                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
6944                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
6945                                 error "sumsquare is too small: $SUMSQ"
6946                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
6947                                 error "sumsquare is too big: $SUMSQ"
6948                         ;;
6949                         *) ;;
6950                 esac
6951         done < $DIR/${tfile}.tmp
6952
6953         #check that we actually got some stats
6954         [ "$read_bytes" ] || error "Missing read_bytes stats"
6955         [ "$write_bytes" ] || error "Missing write_bytes stats"
6956         [ "$read_bytes" != 0 ] || error "no read done"
6957         [ "$write_bytes" != 0 ] || error "no write done"
6958 }
6959 run_test 127a "verify the client stats are sane"
6960
6961 test_127b() { # bug LU-333
6962         $LCTL set_param llite.*.stats=0
6963         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
6964         # perform 2 reads and writes so MAX is different from SUM.
6965         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6966         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
6967         cancel_lru_locks osc
6968         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
6969         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
6970
6971         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
6972         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
6973                 echo "got $COUNT $NAME"
6974                 eval $NAME=$COUNT || error "Wrong proc format"
6975
6976         case $NAME in
6977                 read_bytes)
6978                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
6979                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
6980                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
6981                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
6982                         ;;
6983                 write_bytes)
6984                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
6985                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
6986                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
6987                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
6988                         ;;
6989                         *) ;;
6990                 esac
6991         done < $TMP/${tfile}.tmp
6992
6993         #check that we actually got some stats
6994         [ "$read_bytes" ] || error "Missing read_bytes stats"
6995         [ "$write_bytes" ] || error "Missing write_bytes stats"
6996         [ "$read_bytes" != 0 ] || error "no read done"
6997         [ "$write_bytes" != 0 ] || error "no write done"
6998 }
6999 run_test 127b "verify the llite client stats are sane"
7000
7001 test_128() { # bug 15212
7002         touch $DIR/$tfile
7003         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
7004                 find $DIR/$tfile
7005                 find $DIR/$tfile
7006         EOF
7007
7008         result=$(grep error $TMP/$tfile.log)
7009         rm -f $DIR/$tfile
7010         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
7011 }
7012 run_test 128 "interactive lfs for 2 consecutive find's"
7013
7014 set_dir_limits () {
7015         local mntdev
7016         local canondev
7017         local node
7018
7019         local LDPROC=/proc/fs/ldiskfs
7020
7021         for facet in $(get_facets MDS); do
7022                 canondev=$(ldiskfs_canon *.$(convert_facet2label $facet).mntdev $facet)
7023                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" || LDPROC=/sys/fs/ldiskfs
7024                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
7025         done
7026 }
7027 test_129() {
7028         if [ "$(facet_type_fstype MDS)" != ldiskfs ]; then
7029                 skip "Only applicable to ldiskfs-based MDTs"
7030                 return
7031         fi
7032         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7033
7034         EFBIG=27
7035         MAX=16384
7036
7037         set_dir_limits $MAX
7038
7039         mkdir -p $DIR/$tdir
7040
7041         I=0
7042         J=0
7043         while [ ! $I -gt $((MAX * MDSCOUNT)) ]; do
7044                 $MULTIOP $DIR/$tdir/$J Oc
7045                 rc=$?
7046                 if [ $rc -eq $EFBIG ]; then
7047                         set_dir_limits 0
7048                         echo "return code $rc received as expected"
7049                         return 0
7050                 elif [ $rc -ne 0 ]; then
7051                         set_dir_limits 0
7052                         error_exit "return code $rc received instead of expected $EFBIG"
7053                 fi
7054                 J=$((J+1))
7055                 I=$(stat -c%s "$DIR/$tdir")
7056         done
7057
7058         set_dir_limits 0
7059         error "exceeded dir size limit $MAX x $MDSCOUNT $((MAX * MDSCOUNT)) : $I bytes"
7060 }
7061 run_test 129 "test directory size limit ========================"
7062
7063 OLDIFS="$IFS"
7064 cleanup_130() {
7065         trap 0
7066         IFS="$OLDIFS"
7067 }
7068
7069 test_130a() {
7070         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7071         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7072                 return
7073
7074         trap cleanup_130 EXIT RETURN
7075
7076         local fm_file=$DIR/$tfile
7077         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
7078         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
7079                 error "dd failed for $fm_file"
7080
7081         # LU-1795: test filefrag/FIEMAP once, even if unsupported
7082         filefrag -ves $fm_file
7083         RC=$?
7084         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7085                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7086         [ $RC != 0 ] && error "filefrag $fm_file failed"
7087
7088         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7089                       grep -v "ext:" | grep -v "found")
7090         lun=$($GETSTRIPE -i $fm_file)
7091
7092         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
7093         IFS=$'\n'
7094         tot_len=0
7095         for line in $filefrag_op
7096         do
7097                 frag_lun=`echo $line | cut -d: -f5`
7098                 ext_len=`echo $line | cut -d: -f4`
7099                 if (( $frag_lun != $lun )); then
7100                         cleanup_130
7101                         error "FIEMAP on 1-stripe file($fm_file) failed"
7102                         return
7103                 fi
7104                 (( tot_len += ext_len ))
7105         done
7106
7107         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
7108                 cleanup_130
7109                 error "FIEMAP on 1-stripe file($fm_file) failed;"
7110                 return
7111         fi
7112
7113         cleanup_130
7114
7115         echo "FIEMAP on single striped file succeeded"
7116 }
7117 run_test 130a "FIEMAP (1-stripe file)"
7118
7119 test_130b() {
7120         [ "$OSTCOUNT" -lt "2" ] &&
7121                 skip_env "skipping FIEMAP on 2-stripe file test" && return
7122
7123         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7124         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
7125                 return
7126
7127         trap cleanup_130 EXIT RETURN
7128
7129         local fm_file=$DIR/$tfile
7130         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7131         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7132                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7133
7134         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
7135                 error "dd failed on $fm_file"
7136
7137         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7138         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
7139                       grep -v "ext:" | grep -v "found")
7140
7141         last_lun=$(echo $filefrag_op | cut -d: -f5)
7142
7143         IFS=$'\n'
7144         tot_len=0
7145         num_luns=1
7146         for line in $filefrag_op
7147         do
7148                 frag_lun=`echo $line | cut -d: -f5`
7149                 ext_len=`echo $line | cut -d: -f4`
7150                 if (( $frag_lun != $last_lun )); then
7151                         if (( tot_len != 1024 )); then
7152                                 cleanup_130
7153                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
7154                                 return
7155                         else
7156                                 (( num_luns += 1 ))
7157                                 tot_len=0
7158                         fi
7159                 fi
7160                 (( tot_len += ext_len ))
7161                 last_lun=$frag_lun
7162         done
7163         if (( num_luns != 2 || tot_len != 1024 )); then
7164                 cleanup_130
7165                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7166                 return
7167         fi
7168
7169         cleanup_130
7170
7171         echo "FIEMAP on 2-stripe file succeeded"
7172 }
7173 run_test 130b "FIEMAP (2-stripe file)"
7174
7175 test_130c() {
7176         [ "$OSTCOUNT" -lt "2" ] &&
7177                 skip_env "skipping FIEMAP on 2-stripe file" && return
7178
7179         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7180         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
7181                 return
7182
7183         trap cleanup_130 EXIT RETURN
7184
7185         local fm_file=$DIR/$tfile
7186         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
7187         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7188                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7189
7190         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
7191
7192         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7193         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7194
7195         last_lun=`echo $filefrag_op | cut -d: -f5`
7196
7197         IFS=$'\n'
7198         tot_len=0
7199         num_luns=1
7200         for line in $filefrag_op
7201         do
7202                 frag_lun=`echo $line | cut -d: -f5`
7203                 ext_len=`echo $line | cut -d: -f4`
7204                 if (( $frag_lun != $last_lun )); then
7205                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
7206                         if (( logical != 512 )); then
7207                                 cleanup_130
7208                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
7209                                 return
7210                         fi
7211                         if (( tot_len != 512 )); then
7212                                 cleanup_130
7213                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7214                                 return
7215                         else
7216                                 (( num_luns += 1 ))
7217                                 tot_len=0
7218                         fi
7219                 fi
7220                 (( tot_len += ext_len ))
7221                 last_lun=$frag_lun
7222         done
7223         if (( num_luns != 2 || tot_len != 512 )); then
7224                 cleanup_130
7225                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7226                 return
7227         fi
7228
7229         cleanup_130
7230
7231         echo "FIEMAP on 2-stripe file with hole succeeded"
7232 }
7233 run_test 130c "FIEMAP (2-stripe file with hole)"
7234
7235 test_130d() {
7236         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
7237
7238         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7239         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7240
7241         trap cleanup_130 EXIT RETURN
7242
7243         local fm_file=$DIR/$tfile
7244         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
7245         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7246                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7247         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT || error "dd failed on $fm_file"
7248
7249         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7250         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
7251
7252         last_lun=`echo $filefrag_op | cut -d: -f5`
7253
7254         IFS=$'\n'
7255         tot_len=0
7256         num_luns=1
7257         for line in $filefrag_op
7258         do
7259                 frag_lun=`echo $line | cut -d: -f5`
7260                 ext_len=`echo $line | cut -d: -f4`
7261                 if (( $frag_lun != $last_lun )); then
7262                         if (( tot_len != 1024 )); then
7263                                 cleanup_130
7264                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
7265                                 return
7266                         else
7267                                 (( num_luns += 1 ))
7268                                 tot_len=0
7269                         fi
7270                 fi
7271                 (( tot_len += ext_len ))
7272                 last_lun=$frag_lun
7273         done
7274         if (( num_luns != OSTCOUNT || tot_len != 1024 )); then
7275                 cleanup_130
7276                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7277                 return
7278         fi
7279
7280         cleanup_130
7281
7282         echo "FIEMAP on N-stripe file succeeded"
7283 }
7284 run_test 130d "FIEMAP (N-stripe file)"
7285
7286 test_130e() {
7287         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
7288
7289         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
7290         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
7291
7292         trap cleanup_130 EXIT RETURN
7293
7294         local fm_file=$DIR/$tfile
7295         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
7296         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
7297                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
7298
7299         NUM_BLKS=512
7300         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
7301         for ((i = 0; i < $NUM_BLKS; i++))
7302         do
7303                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
7304         done
7305
7306         filefrag -ves $fm_file || error "filefrag $fm_file failed"
7307         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
7308
7309         last_lun=`echo $filefrag_op | cut -d: -f5`
7310
7311         IFS=$'\n'
7312         tot_len=0
7313         num_luns=1
7314         for line in $filefrag_op
7315         do
7316                 frag_lun=`echo $line | cut -d: -f5`
7317                 ext_len=`echo $line | cut -d: -f4`
7318                 if (( $frag_lun != $last_lun )); then
7319                         if (( tot_len != $EXPECTED_LEN )); then
7320                                 cleanup_130
7321                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
7322                                 return
7323                         else
7324                                 (( num_luns += 1 ))
7325                                 tot_len=0
7326                         fi
7327                 fi
7328                 (( tot_len += ext_len ))
7329                 last_lun=$frag_lun
7330         done
7331         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
7332                 cleanup_130
7333                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
7334                 return
7335         fi
7336
7337         cleanup_130
7338
7339         echo "FIEMAP with continuation calls succeeded"
7340 }
7341 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
7342
7343 # Test for writev/readv
7344 test_131a() {
7345         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
7346         error "writev test failed"
7347         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
7348         error "readv failed"
7349         rm -f $DIR/$tfile
7350 }
7351 run_test 131a "test iov's crossing stripe boundary for writev/readv"
7352
7353 test_131b() {
7354         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
7355         error "append writev test failed"
7356         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
7357         error "append writev test failed"
7358         rm -f $DIR/$tfile
7359 }
7360 run_test 131b "test append writev"
7361
7362 test_131c() {
7363         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
7364         error "NOT PASS"
7365 }
7366 run_test 131c "test read/write on file w/o objects"
7367
7368 test_131d() {
7369         rwv -f $DIR/$tfile -w -n 1 1572864
7370         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
7371         if [ "$NOB" != 1572864 ]; then
7372                 error "Short read filed: read $NOB bytes instead of 1572864"
7373         fi
7374         rm -f $DIR/$tfile
7375 }
7376 run_test 131d "test short read"
7377
7378 test_131e() {
7379         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
7380         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
7381         error "read hitting hole failed"
7382         rm -f $DIR/$tfile
7383 }
7384 run_test 131e "test read hitting hole"
7385
7386 get_ost_param() {
7387         local token=$1
7388         local gl_sum=0
7389         for node in $(osts_nodes); do
7390                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
7391                 [ x$gl = x"" ] && gl=0
7392                 gl_sum=$((gl_sum + gl))
7393         done
7394         echo $gl
7395 }
7396
7397 som_mode_switch() {
7398         local som=$1
7399         local gl1=$2
7400         local gl2=$3
7401
7402         if [ x$som = x"enabled" ]; then
7403                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
7404                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
7405                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
7406         else
7407                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
7408                 MOUNTOPT="$MOUNTOPT,som_preview"
7409                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
7410         fi
7411
7412         # do remount to make new mount-conf parameters actual
7413         echo remounting...
7414         sync
7415         stopall
7416         setupall
7417 }
7418
7419 test_132() { #1028, SOM
7420         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7421         local num=$(get_mds_dir $DIR)
7422         local mymds=mds${num}
7423         local MOUNTOPT_SAVE=$MOUNTOPT
7424
7425         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7426         cancel_lru_locks osc
7427
7428         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7429
7430         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7431         stat $DIR/$tfile >/dev/null
7432         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7433         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
7434         rm $DIR/$tfile
7435         som_mode_switch $som1 $gl1 $gl2
7436
7437         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
7438         cancel_lru_locks osc
7439
7440         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
7441         if [ $som1 == $som2 ]; then
7442             error "som is still "$som2
7443             if [ x$som2 = x"enabled" ]; then
7444                 som2="disabled"
7445             else
7446                 som2="enabled"
7447             fi
7448         fi
7449
7450         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
7451         stat $DIR/$tfile >/dev/null
7452         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
7453         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
7454         som_mode_switch $som2 $gl1 $gl2
7455         MOUNTOPT=$MOUNTOPT_SAVE
7456 }
7457 run_test 132 "som avoids glimpse rpc"
7458
7459 check_stats() {
7460         local res
7461         local count
7462         case $1 in
7463         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
7464                  ;;
7465         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
7466                  ;;
7467         *) error "Wrong argument $1" ;;
7468         esac
7469         echo $res
7470         count=`echo $res | awk '{print $2}'`
7471         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
7472         # if the argument $3 is zero, it means any stat increment is ok.
7473         if [ $3 -gt 0 ] ; then
7474                 [ $count -ne $3 ] && error "The $2 counter on $1 is wrong - expected $3"
7475         fi
7476 }
7477
7478 test_133a() {
7479         remote_ost_nodsh && skip "remote OST with nodsh" && return
7480         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7481
7482         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7483                 { skip "MDS doesn't support rename stats"; return; }
7484         local testdir=$DIR/${tdir}/stats_testdir
7485         mkdir -p $DIR/${tdir}
7486
7487         # clear stats.
7488         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7489         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7490
7491         # verify mdt stats first.
7492         mkdir ${testdir} || error "mkdir failed"
7493         check_stats $SINGLEMDS "mkdir" 1
7494         touch ${testdir}/${tfile} || "touch failed"
7495         check_stats $SINGLEMDS "open" 1
7496         check_stats $SINGLEMDS "close" 1
7497         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
7498         check_stats $SINGLEMDS "mknod" 1
7499         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
7500         check_stats $SINGLEMDS "unlink" 1
7501         rm -f ${testdir}/${tfile} || error "file remove failed"
7502         check_stats $SINGLEMDS "unlink" 2
7503
7504         # remove working dir and check mdt stats again.
7505         rmdir ${testdir} || error "rmdir failed"
7506         check_stats $SINGLEMDS "rmdir" 1
7507
7508         local testdir1=$DIR/${tdir}/stats_testdir1
7509         mkdir -p ${testdir}
7510         mkdir -p ${testdir1}
7511         touch ${testdir1}/test1
7512         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
7513         check_stats $SINGLEMDS "crossdir_rename" 1
7514
7515         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
7516         check_stats $SINGLEMDS "samedir_rename" 1
7517
7518         rm -rf $DIR/${tdir}
7519 }
7520 run_test 133a "Verifying MDT stats ========================================"
7521
7522 test_133b() {
7523         remote_ost_nodsh && skip "remote OST with nodsh" && return
7524         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7525         local testdir=$DIR/${tdir}/stats_testdir
7526         mkdir -p ${testdir} || error "mkdir failed"
7527         touch ${testdir}/${tfile} || "touch failed"
7528         cancel_lru_locks mdc
7529
7530         # clear stats.
7531         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7532         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7533
7534         # extra mdt stats verification.
7535         chmod 444 ${testdir}/${tfile} || error "chmod failed"
7536         check_stats $SINGLEMDS "setattr" 1
7537         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7538         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
7539         then            # LU-1740
7540                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
7541                 check_stats $SINGLEMDS "getattr" 1
7542         fi
7543         $LFS df || error "lfs failed"
7544         check_stats $SINGLEMDS "statfs" 1
7545
7546         rm -rf $DIR/${tdir}
7547 }
7548 run_test 133b "Verifying extra MDT stats =================================="
7549
7550 test_133c() {
7551         remote_ost_nodsh && skip "remote OST with nodsh" && return
7552         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7553         local testdir=$DIR/${tdir}/stats_testdir
7554         mkdir -p ${testdir} || error "mkdir failed"
7555
7556         # verify obdfilter stats.
7557         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
7558         sync
7559         cancel_lru_locks osc
7560         wait_delete_completed
7561
7562         # clear stats.
7563         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
7564         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
7565
7566         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
7567         sync
7568         cancel_lru_locks osc
7569         check_stats ost "write" 1
7570
7571         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
7572         check_stats ost "read" 1
7573
7574         > ${testdir}/${tfile} || error "truncate failed"
7575         check_stats ost "punch" 1
7576
7577         rm -f ${testdir}/${tfile} || error "file remove failed"
7578         wait_delete_completed
7579         check_stats ost "destroy" 1
7580
7581         rm -rf $DIR/${tdir}
7582 }
7583 run_test 133c "Verifying OST stats ========================================"
7584
7585 order_2() {
7586     local value=$1
7587     local orig=$value
7588     local order=1
7589
7590     while [ $value -ge 2 ]; do
7591         order=$((order*2))
7592         value=$((value/2))
7593     done
7594
7595     if [ $orig -gt $order ]; then
7596         order=$((order*2))
7597     fi
7598     echo $order
7599 }
7600
7601 size_in_KMGT() {
7602     local value=$1
7603     local size=('K' 'M' 'G' 'T');
7604     local i=0
7605     local size_string=$value
7606
7607     while [ $value -ge 1024 ]; do
7608         if [ $i -gt 3 ]; then
7609             #T is the biggest unit we get here, if that is bigger,
7610             #just return XXXT
7611             size_string=${value}T
7612             break
7613         fi
7614         value=$((value >> 10))
7615         if [ $value -lt 1024 ]; then
7616             size_string=${value}${size[$i]}
7617             break
7618         fi
7619         i=$((i + 1))
7620     done
7621
7622     echo $size_string
7623 }
7624
7625 get_rename_size() {
7626     local size=$1
7627     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
7628                    awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
7629     echo $sample
7630 }
7631
7632 test_133d() {
7633     remote_ost_nodsh && skip "remote OST with nodsh" && return
7634     remote_mds_nodsh && skip "remote MDS with nodsh" && return
7635     do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
7636         { skip "MDS doesn't support rename stats"; return; }
7637
7638     local testdir1=$DIR/${tdir}/stats_testdir1
7639     local testdir2=$DIR/${tdir}/stats_testdir2
7640
7641     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7642
7643     mkdir -p ${testdir1} || error "mkdir failed"
7644     mkdir -p ${testdir2} || error "mkdir failed"
7645
7646     createmany -o $testdir1/test 512 || error "createmany failed"
7647
7648         # check samedir rename size
7649         mv ${testdir1}/test0 ${testdir1}/test_0
7650
7651         local testdir1_size=$(ls -l $DIR/${tdir} |
7652                 awk '/stats_testdir1/ {print $5}')
7653         local testdir2_size=$(ls -l $DIR/${tdir} |
7654                 awk '/stats_testdir2/ {print $5}')
7655
7656         testdir1_size=$(order_2 $testdir1_size)
7657         testdir2_size=$(order_2 $testdir2_size)
7658
7659         testdir1_size=$(size_in_KMGT $testdir1_size)
7660         testdir2_size=$(size_in_KMGT $testdir2_size)
7661
7662         echo "source rename dir size: ${testdir1_size}"
7663         echo "target rename dir size: ${testdir2_size}"
7664
7665     local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
7666     eval $cmd || error "$cmd failed"
7667     local samedir=$($cmd | grep 'same_dir')
7668     local same_sample=$(get_rename_size $testdir1_size)
7669     [ -z "$samedir" ] && error "samedir_rename_size count error"
7670     [ "$same_sample" -eq 1 ] || error "samedir_rename_size error $same_sample"
7671     echo "Check same dir rename stats success"
7672
7673     do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
7674
7675     # check crossdir rename size
7676     mv ${testdir1}/test_0 ${testdir2}/test_0
7677
7678         testdir1_size=$(ls -l $DIR/${tdir} |
7679                 awk '/stats_testdir1/ {print $5}')
7680         testdir2_size=$(ls -l $DIR/${tdir} |
7681                 awk '/stats_testdir2/ {print $5}')
7682
7683         testdir1_size=$(order_2 $testdir1_size)
7684         testdir2_size=$(order_2 $testdir2_size)
7685
7686         testdir1_size=$(size_in_KMGT $testdir1_size)
7687         testdir2_size=$(size_in_KMGT $testdir2_size)
7688
7689         echo "source rename dir size: ${testdir1_size}"
7690         echo "target rename dir size: ${testdir2_size}"
7691
7692     eval $cmd || error "$cmd failed"
7693     local crossdir=$($cmd | grep 'crossdir')
7694     local src_sample=$(get_rename_size $testdir1_size)
7695     local tgt_sample=$(get_rename_size $testdir2_size)
7696     [ -z "$crossdir" ] && error "crossdir_rename_size count error"
7697     [ "$src_sample" -eq 1 ] || error "crossdir_rename_size error $src_sample"
7698     [ "$tgt_sample" -eq 1 ] || error "crossdir_rename_size error $tgt_sample"
7699     echo "Check cross dir rename stats success"
7700     rm -rf $DIR/${tdir}
7701 }
7702 run_test 133d "Verifying rename_stats ========================================"
7703
7704 test_140() { #bug-17379
7705         mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
7706         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
7707         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
7708
7709         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
7710         local i=0
7711         while i=`expr $i + 1`; do
7712                 mkdir -p $i || error "Creating dir $i"
7713                 cd $i || error "Changing to $i"
7714                 ln -s ../stat stat || error "Creating stat symlink"
7715                 # Read the symlink until ELOOP present,
7716                 # not LBUGing the system is considered success,
7717                 # we didn't overrun the stack.
7718                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
7719                 [ $ret -ne 0 ] && {
7720                         if [ $ret -eq 40 ]; then
7721                                 break  # -ELOOP
7722                         else
7723                                 error "Open stat symlink"
7724                                 return
7725                         fi
7726                 }
7727         done
7728         i=`expr $i - 1`
7729         echo "The symlink depth = $i"
7730         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 ] || error "Invalid symlink depth"
7731 }
7732 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
7733
7734 test_150() {
7735         local TF="$TMP/$tfile"
7736
7737         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7738         cp $TF $DIR/$tfile
7739         cancel_lru_locks osc
7740         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
7741         remount_client $MOUNT
7742         df -P $MOUNT
7743         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
7744
7745         $TRUNCATE $TF 6000
7746         $TRUNCATE $DIR/$tfile 6000
7747         cancel_lru_locks osc
7748         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
7749
7750         echo "12345" >>$TF
7751         echo "12345" >>$DIR/$tfile
7752         cancel_lru_locks osc
7753         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
7754
7755         echo "12345" >>$TF
7756         echo "12345" >>$DIR/$tfile
7757         cancel_lru_locks osc
7758         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
7759
7760         rm -f $TF
7761         true
7762 }
7763 run_test 150 "truncate/append tests"
7764
7765 function roc_hit() {
7766         local list=$(comma_list $(osts_nodes))
7767
7768         echo $(get_obdfilter_param $list '' stats |
7769                awk '/'cache_hit'/ {sum+=$2} END {print sum}')
7770 }
7771
7772 function set_cache() {
7773         local on=1
7774
7775         if [ "$2" == "off" ]; then
7776                 on=0;
7777         fi
7778         local list=$(comma_list $(osts_nodes))
7779         set_obdfilter_param $list '' $1_cache_enable $on
7780
7781         cancel_lru_locks osc
7782 }
7783
7784 test_151() {
7785         remote_ost_nodsh && skip "remote OST with nodsh" && return
7786
7787         local CPAGES=3
7788         local list=$(comma_list $(osts_nodes))
7789
7790         # check whether obdfilter is cache capable at all
7791         if ! get_obdfilter_param $list '' read_cache_enable >/dev/null; then
7792                 echo "not cache-capable obdfilter"
7793                 return 0
7794         fi
7795
7796         # check cache is enabled on all obdfilters
7797         if get_obdfilter_param $list '' read_cache_enable | grep 0; then
7798                 echo "oss cache is disabled"
7799                 return 0
7800         fi
7801
7802         set_obdfilter_param $list '' writethrough_cache_enable 1
7803
7804         # pages should be in the case right after write
7805         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES || error "dd failed"
7806         local BEFORE=`roc_hit`
7807         cancel_lru_locks osc
7808         cat $DIR/$tfile >/dev/null
7809         local AFTER=`roc_hit`
7810         if ! let "AFTER - BEFORE == CPAGES"; then
7811                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
7812         fi
7813
7814         # the following read invalidates the cache
7815         cancel_lru_locks osc
7816         set_obdfilter_param $list '' read_cache_enable 0
7817         cat $DIR/$tfile >/dev/null
7818
7819         # now data shouldn't be found in the cache
7820         BEFORE=`roc_hit`
7821         cancel_lru_locks osc
7822         cat $DIR/$tfile >/dev/null
7823         AFTER=`roc_hit`
7824         if let "AFTER - BEFORE != 0"; then
7825                 error "IN CACHE: before: $BEFORE, after: $AFTER"
7826         fi
7827
7828         set_obdfilter_param $list '' read_cache_enable 1
7829         rm -f $DIR/$tfile
7830 }
7831 run_test 151 "test cache on oss and controls ==============================="
7832
7833 test_152() {
7834         local TF="$TMP/$tfile"
7835
7836         # simulate ENOMEM during write
7837 #define OBD_FAIL_OST_NOMEM      0x226
7838         lctl set_param fail_loc=0x80000226
7839         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
7840         cp $TF $DIR/$tfile
7841         sync || error "sync failed"
7842         lctl set_param fail_loc=0
7843
7844         # discard client's cache
7845         cancel_lru_locks osc
7846
7847         # simulate ENOMEM during read
7848         lctl set_param fail_loc=0x80000226
7849         cmp $TF $DIR/$tfile || error "cmp failed"
7850         lctl set_param fail_loc=0
7851
7852         rm -f $TF
7853 }
7854 run_test 152 "test read/write with enomem ============================"
7855
7856 test_153() {
7857         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
7858 }
7859 run_test 153 "test if fdatasync does not crash ======================="
7860
7861 test_154() {
7862         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
7863                 { skip "Need MDS version at least 2.2.51"; return 0; }
7864
7865         cp /etc/hosts $DIR/$tfile
7866
7867         fid=$($LFS path2fid $DIR/$tfile)
7868         rc=$?
7869         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7870
7871         ffid=$DIR/.lustre/fid/$fid
7872
7873         echo "stat fid $fid"
7874         stat $ffid > /dev/null || error "stat $ffid failed."
7875         echo "touch fid $fid"
7876         touch $ffid || error "touch $ffid failed."
7877         echo "write to fid $fid"
7878         cat /etc/hosts > $ffid || error "write $ffid failed."
7879         echo "read fid $fid"
7880         diff /etc/hosts $ffid || error "read $ffid failed."
7881         echo "append write to fid $fid"
7882         cat /etc/hosts >> $ffid || error "append write $ffid failed."
7883         echo "rename fid $fid"
7884         mv $ffid $DIR/$tfile.1 && error "rename $ffid to $tfile.1 should fail."
7885         touch $DIR/$tfile.1
7886         mv $DIR/$tfile.1 $ffid && error "rename $tfile.1 to $ffid should fail."
7887         rm -f $DIR/$tfile.1
7888         echo "truncate fid $fid"
7889         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
7890         echo "link fid $fid"
7891         ln -f $ffid $DIR/tfile.lnk || error "link $ffid failed."
7892         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
7893                 echo "setfacl fid $fid"
7894                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
7895                 echo "getfacl fid $fid"
7896                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
7897         fi
7898         echo "unlink fid $fid"
7899         unlink $DIR/.lustre/fid/$fid && error "unlink $ffid should fail."
7900         echo "mknod fid $fid"
7901         mknod $ffid c 1 3 && error "mknod $ffid should fail."
7902
7903         fid=[0xf00000400:0x1:0x0]
7904         ffid=$DIR/.lustre/fid/$fid
7905
7906         echo "stat non-exist fid $fid"
7907         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
7908         echo "write to non-exist fid $fid"
7909         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
7910         echo "link new fid $fid"
7911         ln $DIR/$tfile $ffid && error "link $ffid should fail."
7912
7913         mkdir -p $DIR/$tdir
7914         touch $DIR/$tdir/$tfile
7915         fid=$($LFS path2fid $DIR/$tdir)
7916         rc=$?
7917         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
7918
7919         ffid=$DIR/.lustre/fid/$fid
7920
7921         echo "ls $fid"
7922         ls $ffid > /dev/null || error "ls $ffid failed."
7923         echo "touch $fid/$tfile.1"
7924         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
7925
7926         echo "touch $DIR/.lustre/fid/$tfile"
7927         touch $DIR/.lustre/fid/$tfile && \
7928                 error "touch $DIR/.lustre/fid/$tfile should fail."
7929
7930         echo "setxattr to $DIR/.lustre/fid"
7931         setfattr -n trusted.name1 -v value1 $DIR/.lustre/fid &&
7932                 error "setxattr should fail."
7933
7934         echo "listxattr for $DIR/.lustre/fid"
7935         getfattr -d -m "^trusted" $DIR/.lustre/fid &&
7936                 error "listxattr should fail."
7937
7938         echo "delxattr from $DIR/.lustre/fid"
7939         setfattr -x trusted.name1 $DIR/.lustre/fid &&
7940                 error "delxattr should fail."
7941
7942         echo "touch invalid fid: $DIR/.lustre/fid/[0x200000400:0x2:0x3]"
7943         touch $DIR/.lustre/fid/[0x200000400:0x2:0x3] &&
7944                 error "touch invalid fid should fail."
7945
7946         echo "touch non-normal fid: $DIR/.lustre/fid/[0x1:0x2:0x0]"
7947         touch $DIR/.lustre/fid/[0x1:0x2:0x0] &&
7948                 error "touch non-normal fid should fail."
7949
7950         echo "rename $tdir to $DIR/.lustre/fid"
7951         mrename $DIR/$tdir $DIR/.lustre/fid &&
7952                 error "rename to $DIR/.lustre/fid should fail."
7953
7954         echo "rename .lustre to itself"
7955         fid=$($LFS path2fid $DIR)
7956         mrename $DIR/.lustre $DIR/.lustre/fid/$fid/.lustre &&
7957                 error "rename .lustre to itself should fail."
7958
7959         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $DIR/$tfile-2
7960         fid=$($LFS path2fid $DIR/$tfile-2)
7961         echo "cp /etc/passwd $DIR/.lustre/fid/$fid"
7962         cp /etc/passwd $DIR/.lustre/fid/$fid &&
7963                 error "create lov data thru .lustre should fail."
7964         echo "cp /etc/passwd $DIR/$tfile-2"
7965         cp /etc/passwd $DIR/$tfile-2 || error "copy to $DIR/$tfile-2 failed."
7966         echo "diff /etc/passwd $DIR/.lustre/fid/$fid"
7967         diff /etc/passwd $DIR/.lustre/fid/$fid ||
7968                 error "diff /etc/passwd $DIR/.lustre/fid/$fid failed."
7969
7970         echo "Open-by-FID succeeded"
7971 }
7972 run_test 154 "Open-by-FID"
7973
7974 test_155_small_load() {
7975     local temp=$TMP/$tfile
7976     local file=$DIR/$tfile
7977
7978     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
7979         error "dd of=$temp bs=6096 count=1 failed"
7980     cp $temp $file
7981     cancel_lru_locks osc
7982     cmp $temp $file || error "$temp $file differ"
7983
7984     $TRUNCATE $temp 6000
7985     $TRUNCATE $file 6000
7986     cmp $temp $file || error "$temp $file differ (truncate1)"
7987
7988     echo "12345" >>$temp
7989     echo "12345" >>$file
7990     cmp $temp $file || error "$temp $file differ (append1)"
7991
7992     echo "12345" >>$temp
7993     echo "12345" >>$file
7994     cmp $temp $file || error "$temp $file differ (append2)"
7995
7996     rm -f $temp $file
7997     true
7998 }
7999
8000 test_155_big_load() {
8001     remote_ost_nodsh && skip "remote OST with nodsh" && return
8002     local temp=$TMP/$tfile
8003     local file=$DIR/$tfile
8004
8005     free_min_max
8006     local cache_size=$(do_facet ost$((MAXI+1)) \
8007         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
8008     local large_file_size=$((cache_size * 2))
8009
8010     echo "OSS cache size: $cache_size KB"
8011     echo "Large file size: $large_file_size KB"
8012
8013     [ $MAXV -le $large_file_size ] && \
8014         skip_env "max available OST size needs > $large_file_size KB" && \
8015         return 0
8016
8017     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
8018
8019     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
8020         error "dd of=$temp bs=$large_file_size count=1k failed"
8021     cp $temp $file
8022     ls -lh $temp $file
8023     cancel_lru_locks osc
8024     cmp $temp $file || error "$temp $file differ"
8025
8026     rm -f $temp $file
8027     true
8028 }
8029
8030 test_155a() {
8031     set_cache read on
8032     set_cache writethrough on
8033     test_155_small_load
8034 }
8035 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
8036
8037 test_155b() {
8038     set_cache read on
8039     set_cache writethrough off
8040     test_155_small_load
8041 }
8042 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
8043
8044 test_155c() {
8045     set_cache read off
8046     set_cache writethrough on
8047     test_155_small_load
8048 }
8049 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
8050
8051 test_155d() {
8052     set_cache read off
8053     set_cache writethrough off
8054     test_155_small_load
8055 }
8056 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
8057
8058 test_155e() {
8059     set_cache read on
8060     set_cache writethrough on
8061     test_155_big_load
8062 }
8063 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
8064
8065 test_155f() {
8066     set_cache read on
8067     set_cache writethrough off
8068     test_155_big_load
8069 }
8070 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
8071
8072 test_155g() {
8073     set_cache read off
8074     set_cache writethrough on
8075     test_155_big_load
8076 }
8077 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
8078
8079 test_155h() {
8080     set_cache read off
8081     set_cache writethrough off
8082     test_155_big_load
8083 }
8084 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
8085
8086 test_156() {
8087     local CPAGES=3
8088     local BEFORE
8089     local AFTER
8090     local file="$DIR/$tfile"
8091
8092     log "Turn on read and write cache"
8093     set_cache read on
8094     set_cache writethrough on
8095
8096     log "Write data and read it back."
8097     log "Read should be satisfied from the cache."
8098     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8099     BEFORE=`roc_hit`
8100     cancel_lru_locks osc
8101     cat $file >/dev/null
8102     AFTER=`roc_hit`
8103     if ! let "AFTER - BEFORE == CPAGES"; then
8104         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8105     else
8106         log "cache hits:: before: $BEFORE, after: $AFTER"
8107     fi
8108
8109     log "Read again; it should be satisfied from the cache."
8110     BEFORE=$AFTER
8111     cancel_lru_locks osc
8112     cat $file >/dev/null
8113     AFTER=`roc_hit`
8114     if ! let "AFTER - BEFORE == CPAGES"; then
8115         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8116     else
8117         log "cache hits:: before: $BEFORE, after: $AFTER"
8118     fi
8119
8120
8121     log "Turn off the read cache and turn on the write cache"
8122     set_cache read off
8123     set_cache writethrough on
8124
8125     log "Read again; it should be satisfied from the cache."
8126     BEFORE=`roc_hit`
8127     cancel_lru_locks osc
8128     cat $file >/dev/null
8129     AFTER=`roc_hit`
8130     if ! let "AFTER - BEFORE == CPAGES"; then
8131         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8132     else
8133         log "cache hits:: before: $BEFORE, after: $AFTER"
8134     fi
8135
8136     log "Read again; it should not be satisfied from the cache."
8137     BEFORE=$AFTER
8138     cancel_lru_locks osc
8139     cat $file >/dev/null
8140     AFTER=`roc_hit`
8141     if ! let "AFTER - BEFORE == 0"; then
8142         error "IN CACHE: before: $BEFORE, after: $AFTER"
8143     else
8144         log "cache hits:: before: $BEFORE, after: $AFTER"
8145     fi
8146
8147     log "Write data and read it back."
8148     log "Read should be satisfied from the cache."
8149     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8150     BEFORE=`roc_hit`
8151     cancel_lru_locks osc
8152     cat $file >/dev/null
8153     AFTER=`roc_hit`
8154     if ! let "AFTER - BEFORE == CPAGES"; then
8155         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8156     else
8157         log "cache hits:: before: $BEFORE, after: $AFTER"
8158     fi
8159
8160     log "Read again; it should not be satisfied from the cache."
8161     BEFORE=$AFTER
8162     cancel_lru_locks osc
8163     cat $file >/dev/null
8164     AFTER=`roc_hit`
8165     if ! let "AFTER - BEFORE == 0"; then
8166         error "IN CACHE: before: $BEFORE, after: $AFTER"
8167     else
8168         log "cache hits:: before: $BEFORE, after: $AFTER"
8169     fi
8170
8171
8172     log "Turn off read and write cache"
8173     set_cache read off
8174     set_cache writethrough off
8175
8176     log "Write data and read it back"
8177     log "It should not be satisfied from the cache."
8178     rm -f $file
8179     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8180     cancel_lru_locks osc
8181     BEFORE=`roc_hit`
8182     cat $file >/dev/null
8183     AFTER=`roc_hit`
8184     if ! let "AFTER - BEFORE == 0"; then
8185         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8186     else
8187         log "cache hits:: before: $BEFORE, after: $AFTER"
8188     fi
8189
8190
8191     log "Turn on the read cache and turn off the write cache"
8192     set_cache read on
8193     set_cache writethrough off
8194
8195     log "Write data and read it back"
8196     log "It should not be satisfied from the cache."
8197     rm -f $file
8198     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
8199     BEFORE=`roc_hit`
8200     cancel_lru_locks osc
8201     cat $file >/dev/null
8202     AFTER=`roc_hit`
8203     if ! let "AFTER - BEFORE == 0"; then
8204         error_ignore 20762 "IN CACHE: before: $BEFORE, after: $AFTER"
8205     else
8206         log "cache hits:: before: $BEFORE, after: $AFTER"
8207     fi
8208
8209     log "Read again; it should be satisfied from the cache."
8210     BEFORE=`roc_hit`
8211     cancel_lru_locks osc
8212     cat $file >/dev/null
8213     AFTER=`roc_hit`
8214     if ! let "AFTER - BEFORE == CPAGES"; then
8215         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
8216     else
8217         log "cache hits:: before: $BEFORE, after: $AFTER"
8218     fi
8219
8220     rm -f $file
8221 }
8222 run_test 156 "Verification of tunables ============================"
8223
8224 #Changelogs
8225 err17935 () {
8226     if [ $MDSCOUNT -gt 1 ]; then
8227         error_ignore 17935 $*
8228     else
8229         error $*
8230     fi
8231 }
8232
8233 changelog_chmask()
8234 {
8235     MASK=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_mask |\
8236            grep -c $1)
8237
8238     if [ $MASK -eq 1 ]; then
8239         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="-$1"
8240     else
8241         do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_mask="+$1"
8242     fi
8243 }
8244
8245 test_160() {
8246     remote_mds_nodsh && skip "remote MDS with nodsh" && return
8247     [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
8248         { skip "Need MDS version at least 2.2.0"; return; }
8249     USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
8250     echo "Registered as changelog user $USER"
8251     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8252         grep -q $USER || error "User $USER not found in changelog_users"
8253
8254     # change something
8255     mkdir -p $DIR/$tdir/pics/2008/zachy
8256     touch $DIR/$tdir/pics/2008/zachy/timestamp
8257     cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
8258     mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
8259     ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
8260     ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
8261     rm $DIR/$tdir/pics/desktop.jpg
8262
8263     $LFS changelog $MDT0 | tail -5
8264
8265     echo "verifying changelog mask"
8266     changelog_chmask "MKDIR"
8267     changelog_chmask "CLOSE"
8268
8269     mkdir -p $DIR/$tdir/pics/zach/sofia
8270     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8271
8272     changelog_chmask "MKDIR"
8273     changelog_chmask "CLOSE"
8274
8275     mkdir -p $DIR/$tdir/pics/2008/sofia
8276     echo "zzzzzz" > $DIR/$tdir/pics/zach/file
8277
8278     $LFS changelog $MDT0
8279     MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
8280     CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
8281     [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
8282     [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
8283
8284     # verify contents
8285     echo "verifying target fid"
8286     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8287         tail -1 | awk '{print $6}')
8288     fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
8289     [ "$fidc" == "t=$fidf" ] || \
8290         err17935 "fid in changelog $fidc != file fid $fidf"
8291     echo "verifying parent fid"
8292     fidc=$($LFS changelog $MDT0 | grep timestamp | grep "CREAT" | \
8293         tail -1 | awk '{print $7}')
8294     fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
8295     [ "$fidc" == "p=$fidf" ] || \
8296         err17935 "pfid in changelog $fidc != dir fid $fidf"
8297
8298     USER_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8299         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8300     $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
8301     USER_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8302         mdd.$MDT0.changelog_users | grep $USER | awk '{print $2}')
8303     echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
8304     [ $USER_REC2 == $(($USER_REC1 + 5)) ] || \
8305         err17935 "user index should be $(($USER_REC1 + 5)); is $USER_REC2"
8306
8307     MIN_REC=$(do_facet $SINGLEMDS $LCTL get_param mdd.$MDT0.changelog_users | \
8308         awk 'min == "" || $2 < min {min = $2}; END {print min}')
8309     FIRST_REC=$($LFS changelog $MDT0 | head -1 | awk '{print $1}')
8310     echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
8311     [ $FIRST_REC == $(($MIN_REC + 1)) ] || \
8312         err17935 "first index should be $(($MIN_REC + 1)); is $FIRST_REC"
8313
8314     echo "verifying user deregister"
8315     do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
8316     do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_users | \
8317         grep -q $USER && error "User $USER still found in changelog_users"
8318
8319     USERS=$(( $(do_facet $SINGLEMDS $LCTL get_param -n \
8320         mdd.$MDT0.changelog_users | wc -l) - 2 ))
8321     if [ $USERS -eq 0 ]; then
8322         LAST_REC1=$(do_facet $SINGLEMDS $LCTL get_param -n \
8323             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8324         touch $DIR/$tdir/chloe
8325         LAST_REC2=$(do_facet $SINGLEMDS $LCTL get_param -n \
8326             mdd.$MDT0.changelog_users | head -1 | awk '{print $3}')
8327         echo "verify changelogs are off if we were the only user: $LAST_REC1 == $LAST_REC2"
8328         [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
8329     else
8330         echo "$USERS other changelog users; can't verify off"
8331     fi
8332 }
8333 run_test 160 "changelog sanity"
8334
8335 test_161() {
8336     mkdir -p $DIR/$tdir
8337     cp /etc/hosts $DIR/$tdir/$tfile
8338     mkdir $DIR/$tdir/foo1
8339     mkdir $DIR/$tdir/foo2
8340     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
8341     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
8342     ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
8343     ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
8344     local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[')
8345     if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
8346         $LFS fid2path $DIR $FID
8347         err17935 "bad link ea"
8348     fi
8349     # middle
8350     rm $DIR/$tdir/foo2/zachary
8351     # last
8352     rm $DIR/$tdir/foo2/thor
8353     # first
8354     rm $DIR/$tdir/$tfile
8355     # rename
8356     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
8357     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
8358         then
8359         $LFS fid2path $DIR $FID
8360         err17935 "bad link rename"
8361     fi
8362     rm $DIR/$tdir/foo2/maggie
8363
8364     # overflow the EA
8365     local longname=filename_avg_len_is_thirty_two_
8366     createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 || \
8367         error "failed to hardlink many files"
8368     links=$($LFS fid2path $DIR $FID | wc -l)
8369     echo -n "${links}/1000 links in link EA"
8370     [ ${links} -gt 60 ] || err17935 "expected at least 60 links in link EA"
8371     unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
8372         error "failed to unlink many hardlinks"
8373 }
8374 run_test 161 "link ea sanity"
8375
8376 check_path() {
8377     local expected=$1
8378     shift
8379     local fid=$2
8380
8381     local path=$(${LFS} fid2path $*)
8382     RC=$?
8383
8384     if [ $RC -ne 0 ]; then
8385         err17935 "path looked up of $expected failed. Error $RC"
8386         return $RC
8387     elif [ "${path}" != "${expected}" ]; then
8388         err17935 "path looked up \"${path}\" instead of \"${expected}\""
8389         return 2
8390     fi
8391     echo "fid $fid resolves to path $path (expected $expected)"
8392 }
8393
8394 test_162() {
8395         # Make changes to filesystem
8396         mkdir -p $DIR/$tdir/d2
8397         touch $DIR/$tdir/d2/$tfile
8398         touch $DIR/$tdir/d2/x1
8399         touch $DIR/$tdir/d2/x2
8400         mkdir -p $DIR/$tdir/d2/a/b/c
8401         mkdir -p $DIR/$tdir/d2/p/q/r
8402         # regular file
8403         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
8404         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
8405
8406         # softlink
8407         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
8408         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
8409         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
8410
8411         # softlink to wrong file
8412         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
8413         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
8414         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
8415
8416         # hardlink
8417         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
8418         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
8419         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
8420         # fid2path dir/fsname should both work
8421         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
8422         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
8423
8424         # hardlink count: check that there are 2 links
8425         # Doesnt work with CMD yet: 17935
8426         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
8427                 err17935 "expected 2 links"
8428
8429         # hardlink indexing: remove the first link
8430         rm $DIR/$tdir/d2/p/q/r/hlink
8431         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
8432
8433         return 0
8434 }
8435 run_test 162 "path lookup sanity"
8436
8437 test_163() {
8438         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8439         copytool --test $FSNAME || { skip "copytool not runnable: $?" && return; }
8440         copytool $FSNAME &
8441         sleep 1
8442         local uuid=$($LCTL get_param -n mdc.${FSNAME}-MDT0000-mdc-*.uuid)
8443         # this proc file is temporary and linux-only
8444         do_facet $SINGLEMDS lctl set_param mdt.${FSNAME}-MDT0000.mdccomm=$uuid ||\
8445          error "kernel->userspace send failed"
8446         kill -INT $!
8447 }
8448 run_test 163 "kernel <-> userspace comms"
8449
8450 test_169() {
8451         # do directio so as not to populate the page cache
8452         log "creating a 10 Mb file"
8453         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
8454         log "starting reads"
8455         dd if=$DIR/$tfile of=/dev/null bs=4096 &
8456         log "truncating the file"
8457         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
8458         log "killing dd"
8459         kill %+ || true # reads might have finished
8460         echo "wait until dd is finished"
8461         wait
8462         log "removing the temporary file"
8463         rm -rf $DIR/$tfile || error "tmp file removal failed"
8464 }
8465 run_test 169 "parallel read and truncate should not deadlock"
8466
8467 test_170() {
8468         $LCTL clear     # bug 18514
8469         $LCTL debug_daemon start $TMP/${tfile}_log_good
8470         touch $DIR/$tfile
8471         $LCTL debug_daemon stop
8472         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
8473                error "sed failed to read log_good"
8474
8475         $LCTL debug_daemon start $TMP/${tfile}_log_good
8476         rm -rf $DIR/$tfile
8477         $LCTL debug_daemon stop
8478
8479         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
8480                error "lctl df log_bad failed"
8481
8482         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8483         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8484
8485         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
8486         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
8487
8488         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
8489                 error "bad_line good_line1 good_line2 are empty"
8490
8491         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8492         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
8493         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
8494
8495         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
8496         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
8497         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
8498
8499         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
8500                 error "bad_line_new good_line_new are empty"
8501
8502         local expected_good=$((good_line1 + good_line2*2))
8503
8504         rm -f $TMP/${tfile}*
8505         # LU-231, short malformed line may not be counted into bad lines
8506         if [ $bad_line -ne $bad_line_new ] &&
8507                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
8508                 error "expected $bad_line bad lines, but got $bad_line_new"
8509                 return 1
8510         fi
8511
8512         if [ $expected_good -ne $good_line_new ]; then
8513                 error "expected $expected_good good lines, but got $good_line_new"
8514                 return 2
8515         fi
8516         true
8517 }
8518 run_test 170 "test lctl df to handle corrupted log ====================="
8519
8520 test_171() { # bug20592
8521 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8522         $LCTL set_param fail_loc=0x50e
8523         $LCTL set_param fail_val=3000
8524         multiop_bg_pause $DIR/$tfile O_s || true
8525         local MULTIPID=$!
8526         kill -USR1 $MULTIPID
8527         # cause log dump
8528         sleep 3
8529         wait $MULTIPID
8530         if dmesg | grep "recursive fault"; then
8531                 error "caught a recursive fault"
8532         fi
8533         $LCTL set_param fail_loc=0
8534         true
8535 }
8536 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
8537
8538 # it would be good to share it with obdfilter-survey/libecho code
8539 setup_obdecho_osc () {
8540         local rc=0
8541         local ost_nid=$1
8542         local obdfilter_name=$2
8543         echo "Creating new osc for $obdfilter_name on $ost_nid"
8544         # make sure we can find loopback nid
8545         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
8546
8547         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
8548                            ${obdfilter_name}_osc_UUID || rc=2; }
8549         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
8550                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
8551         return $rc
8552 }
8553
8554 cleanup_obdecho_osc () {
8555         local obdfilter_name=$1
8556         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
8557         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
8558         return 0
8559 }
8560
8561 obdecho_create_test() {
8562         local OBD=$1
8563         local node=$2
8564         local rc=0
8565         local id
8566         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
8567         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
8568                            rc=2; }
8569         if [ $rc -eq 0 ]; then
8570             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
8571             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
8572         fi
8573         echo "New object id is $id"
8574         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec test_brw 10 w v 64 $id" ||
8575                            rc=4; }
8576         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
8577                                         "cleanup" || rc=5; }
8578         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
8579                                         "detach" || rc=6; }
8580         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
8581         return $rc
8582 }
8583
8584 test_180a() {
8585         remote_ost_nodsh && skip "remote OST with nodsh" && return
8586         local rc=0
8587         local rmmod_local=0
8588
8589         if ! module_loaded obdecho; then
8590             load_module obdecho/obdecho
8591             rmmod_local=1
8592         fi
8593
8594         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
8595         local host=$(lctl get_param -n osc.$osc.import |
8596                              awk '/current_connection:/ {print $2}' )
8597         local target=$(lctl get_param -n osc.$osc.import |
8598                              awk '/target:/ {print $2}' )
8599         target=${target%_UUID}
8600
8601         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
8602         [ $rc -eq 0 ] && { obdecho_create_test ${target}_osc client || rc=2; }
8603         [[ -n $target ]] && cleanup_obdecho_osc $target
8604         [ $rmmod_local -eq 1 ] && rmmod obdecho
8605         return $rc
8606 }
8607 run_test 180a "test obdecho on osc"
8608
8609 test_180b() {
8610         remote_ost_nodsh && skip "remote OST with nodsh" && return
8611         local rc=0
8612         local rmmod_remote=0
8613
8614         do_facet ost1 "lsmod | grep -q obdecho || "                      \
8615                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
8616                       "modprobe obdecho; }" && rmmod_remote=1
8617         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
8618         [[ -n $target ]] && { obdecho_create_test $target ost1 || rc=1; }
8619         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
8620         return $rc
8621 }
8622 run_test 180b "test obdecho directly on obdfilter"
8623
8624 test_181() { # bug 22177
8625         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8626         # create enough files to index the directory
8627         createmany -o $DIR/$tdir/foobar 4000
8628         # print attributes for debug purpose
8629         lsattr -d .
8630         # open dir
8631         multiop_bg_pause $DIR/$tdir D_Sc || return 1
8632         MULTIPID=$!
8633         # remove the files & current working dir
8634         unlinkmany $DIR/$tdir/foobar 4000
8635         rmdir $DIR/$tdir
8636         kill -USR1 $MULTIPID
8637         wait $MULTIPID
8638         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
8639         return 0
8640 }
8641 run_test 181 "Test open-unlinked dir ========================"
8642
8643 test_182() {
8644         # disable MDC RPC lock wouldn't crash client
8645         local fcount=1000
8646         local tcount=4
8647
8648         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
8649 #define OBD_FAIL_MDC_RPCS_SEM           0x804
8650         $LCTL set_param fail_loc=0x804
8651
8652         for (( i=0; i < $tcount; i++ )) ; do
8653                 mkdir $DIR/$tdir/$i
8654                 createmany -o $DIR/$tdir/$i/f- $fcount &
8655         done
8656         wait
8657
8658         for (( i=0; i < $tcount; i++ )) ; do
8659                 unlinkmany $DIR/$tdir/$i/f- $fcount &
8660         done
8661         wait
8662
8663         rm -rf $DIR/$tdir
8664
8665         $LCTL set_param fail_loc=0
8666 }
8667 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
8668
8669 # OST pools tests
8670 check_file_in_pool()
8671 {
8672         local file=$1
8673         local pool=$2
8674         local tlist="$3"
8675         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
8676         for i in $res
8677         do
8678                 for t in $tlist ; do
8679                         [ "$i" -eq "$t" ] && continue 2
8680                 done
8681
8682                 echo "pool list: $tlist"
8683                 echo "striping: $res"
8684                 error_noexit "$file not allocated in $pool"
8685                 return 1
8686         done
8687         return 0
8688 }
8689
8690 pool_add() {
8691         echo "Creating new pool"
8692         local pool=$1
8693
8694         create_pool $FSNAME.$pool ||
8695                 { error_noexit "No pool created, result code $?"; return 1; }
8696         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
8697                 { error_noexit "$pool not in lfs pool_list"; return 2; }
8698 }
8699
8700 pool_add_targets() {
8701         echo "Adding targets to pool"
8702         local pool=$1
8703         local first=$2
8704         local last=$3
8705         local step=${4:-1}
8706
8707         local list=$(seq $first $step $last)
8708
8709         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
8710         do_facet mgs $LCTL pool_add \
8711                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
8712         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
8713                         | sort -u | tr '\n' ' ' " "$t" || { 
8714                 error_noexit "Add to pool failed"
8715                 return 1
8716         }
8717         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
8718         local addcount=$(((last - first) / step + 1))
8719         [ $lfscount -eq $addcount ] || {
8720                 error_noexit "lfs pool_list bad ost count" \
8721                                                 "$lfscount != $addcount"
8722                 return 2
8723         }
8724 }
8725
8726 pool_set_dir() {
8727         local pool=$1
8728         local tdir=$2
8729         echo "Setting pool on directory $tdir"
8730
8731         $SETSTRIPE -c 2 -p $pool $tdir && return 0
8732
8733         error_noexit "Cannot set pool $pool to $tdir"
8734         return 1
8735 }
8736
8737 pool_check_dir() {
8738         local pool=$1
8739         local tdir=$2
8740         echo "Checking pool on directory $tdir"
8741
8742         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
8743         [ "$res" = "$pool" ] && return 0
8744
8745         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
8746         return 1
8747 }
8748
8749 pool_dir_rel_path() {
8750         echo "Testing relative path works well"
8751         local pool=$1
8752         local tdir=$2
8753         local root=$3
8754
8755         mkdir -p $root/$tdir/$tdir
8756         cd $root/$tdir
8757         pool_set_dir $pool $tdir          || return 1
8758         pool_set_dir $pool ./$tdir        || return 2
8759         pool_set_dir $pool ../$tdir       || return 3
8760         pool_set_dir $pool ../$tdir/$tdir || return 4
8761         rm -rf $tdir; cd - > /dev/null
8762 }
8763
8764 pool_alloc_files() {
8765         echo "Checking files allocation from directory pool"
8766         local pool=$1
8767         local tdir=$2
8768         local count=$3
8769         local tlist="$4"
8770
8771         local failed=0
8772         for i in $(seq -w 1 $count)
8773         do
8774                 local file=$tdir/file-$i
8775                 touch $file
8776                 check_file_in_pool $file $pool "$tlist" || \
8777                         failed=$((failed + 1))
8778         done
8779         [ "$failed" = 0 ] && return 0
8780
8781         error_noexit "$failed files not allocated in $pool"
8782         return 1
8783 }
8784
8785 pool_create_files() {
8786         echo "Creating files in pool"
8787         local pool=$1
8788         local tdir=$2
8789         local count=$3
8790         local tlist="$4"
8791
8792         mkdir -p $tdir
8793         local failed=0
8794         for i in $(seq -w 1 $count)
8795         do
8796                 local file=$tdir/spoo-$i
8797                 $SETSTRIPE -p $pool $file
8798                 check_file_in_pool $file $pool "$tlist" || \
8799                         failed=$((failed + 1))
8800         done
8801         [ "$failed" = 0 ] && return 0
8802
8803         error_noexit "$failed files not allocated in $pool"
8804         return 1
8805 }
8806
8807 pool_lfs_df() {
8808         echo "Checking 'lfs df' output"
8809         local pool=$1
8810
8811         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
8812                         tr '\n' ' ')
8813         local res=$($LFS df --pool $FSNAME.$pool |
8814                         awk '{print $1}' |
8815                         grep "$FSNAME-OST" |
8816                         tr '\n' ' ')
8817         [ "$res" = "$t" ] && return 0
8818
8819         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
8820         return 1
8821 }
8822
8823 pool_file_rel_path() {
8824         echo "Creating files in a pool with relative pathname"
8825         local pool=$1
8826         local tdir=$2
8827
8828         mkdir -p $tdir ||
8829                 { error_noexit "unable to create $tdir"; return 1 ; }
8830         local file="/..$tdir/$tfile-1"
8831         $SETSTRIPE -p $pool $file ||
8832                 { error_noexit "unable to create $file" ; return 2 ; }
8833
8834         cd $tdir
8835         $SETSTRIPE -p $pool $tfile-2 || {
8836                 error_noexit "unable to create $tfile-2 in $tdir"
8837                 return 3
8838         }
8839 }
8840
8841 pool_remove_first_target() {
8842         echo "Removing first target from a pool"
8843         local pool=$1
8844
8845         local pname="lov.$FSNAME-*.pools.$pool"
8846         local t=$($LCTL get_param -n $pname | head -1)
8847         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
8848         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
8849                 error_noexit "$t not removed from $FSNAME.$pool"
8850                 return 1
8851         }
8852 }
8853
8854 pool_remove_all_targets() {
8855         echo "Removing all targets from pool"
8856         local pool=$1
8857         local file=$2
8858         local pname="lov.$FSNAME-*.pools.$pool"
8859         for t in $($LCTL get_param -n $pname | sort -u)
8860         do
8861                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
8862         done
8863         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
8864                 error_noexit "Pool $FSNAME.$pool cannot be drained"
8865                 return 1
8866         }
8867         # striping on an empty/nonexistant pool should fall back 
8868         # to "pool of everything"
8869         touch $file || {
8870                 error_noexit "failed to use fallback striping for empty pool"
8871                 return 2
8872         }
8873         # setstripe on an empty pool should fail
8874         $SETSTRIPE -p $pool $file 2>/dev/null && {
8875                 error_noexit "expected failure when creating file" \
8876                                                         "with empty pool"
8877                 return 3
8878         }
8879         return 0
8880 }
8881
8882 pool_remove() {
8883         echo "Destroying pool"
8884         local pool=$1
8885         local file=$2
8886
8887         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
8888
8889         sleep 2
8890         # striping on an empty/nonexistant pool should fall back 
8891         # to "pool of everything"
8892         touch $file || {
8893                 error_noexit "failed to use fallback striping for missing pool"
8894                 return 1
8895         }
8896         # setstripe on an empty pool should fail
8897         $SETSTRIPE -p $pool $file 2>/dev/null && {
8898                 error_noexit "expected failure when creating file" \
8899                                                         "with missing pool"
8900                 return 2
8901         }
8902
8903         # get param should return err once pool is gone
8904         if wait_update $HOSTNAME "lctl get_param -n \
8905                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
8906         then
8907                 remove_pool_from_list $FSNAME.$pool
8908                 return 0
8909         fi
8910         error_noexit "Pool $FSNAME.$pool is not destroyed"
8911         return 3
8912 }
8913
8914 test_200() {
8915         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
8916
8917         local POOL=${POOL:-cea1}
8918         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
8919         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
8920         # Pool OST targets
8921         local first_ost=0
8922         local last_ost=$(($OSTCOUNT - 1))
8923         local ost_step=2
8924         local ost_list=$(seq $first_ost $ost_step $last_ost)
8925         local ost_range="$first_ost $last_ost $ost_step"
8926         local test_path=$POOL_ROOT/$POOL_DIR_NAME
8927         local file_dir=$POOL_ROOT/file_tst
8928
8929         local rc=0
8930         while : ; do
8931                 # former test_200a test_200b
8932                 pool_add $POOL                          || { rc=$? ; break; }
8933                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
8934                 # former test_200c test_200d
8935                 mkdir -p $test_path
8936                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
8937                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
8938                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
8939                                                         || { rc=$? ; break; }
8940                 # former test_200e test_200f
8941                 local files=$((OSTCOUNT*3))
8942                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
8943                                                         || { rc=$? ; break; }
8944                 pool_create_files $POOL $file_dir $files "$ost_list" \
8945                                                         || { rc=$? ; break; }
8946                 # former test_200g test_200h
8947                 pool_lfs_df $POOL                       || { rc=$? ; break; }
8948                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
8949
8950                 # former test_201a test_201b test_201c
8951                 pool_remove_first_target $POOL          || { rc=$? ; break; }
8952
8953                 local f=$test_path/$tfile
8954                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
8955                 pool_remove $POOL $f                    || { rc=$? ; break; }
8956                 break
8957         done
8958
8959         cleanup_pools
8960         return $rc
8961 }
8962 run_test 200 "OST pools"
8963
8964 # usage: default_attr <count | size | offset>
8965 default_attr() {
8966         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
8967 }
8968
8969 # usage: check_default_stripe_attr
8970 check_default_stripe_attr() {
8971         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
8972         case $1 in
8973         --stripe-count|--count)
8974                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
8975         --stripe-size|--size)
8976                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
8977         --stripe-index|--index)
8978                 EXPECTED=-1;;
8979         *)
8980                 error "unknown getstripe attr '$1'"
8981         esac
8982
8983         [ $ACTUAL != $EXPECTED ] &&
8984                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
8985 }
8986
8987 test_204a() {
8988         mkdir -p $DIR/$tdir
8989         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
8990
8991         check_default_stripe_attr --stripe-count
8992         check_default_stripe_attr --stripe-size
8993         check_default_stripe_attr --stripe-index
8994
8995         return 0
8996 }
8997 run_test 204a "Print default stripe attributes ================="
8998
8999 test_204b() {
9000         mkdir -p $DIR/$tdir
9001         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9002
9003         check_default_stripe_attr --stripe-size
9004         check_default_stripe_attr --stripe-index
9005
9006         return 0
9007 }
9008 run_test 204b "Print default stripe size and offset  ==========="
9009
9010 test_204c() {
9011         mkdir -p $DIR/$tdir
9012         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9013
9014         check_default_stripe_attr --stripe-count
9015         check_default_stripe_attr --stripe-index
9016
9017         return 0
9018 }
9019 run_test 204c "Print default stripe count and offset ==========="
9020
9021 test_204d() {
9022         mkdir -p $DIR/$tdir
9023         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9024
9025         check_default_stripe_attr --stripe-count
9026         check_default_stripe_attr --stripe-size
9027
9028         return 0
9029 }
9030 run_test 204d "Print default stripe count and size ============="
9031
9032 test_204e() {
9033         mkdir -p $DIR/$tdir
9034         $SETSTRIPE -d $DIR/$tdir
9035
9036         check_default_stripe_attr --stripe-count --raw
9037         check_default_stripe_attr --stripe-size --raw
9038         check_default_stripe_attr --stripe-index --raw
9039
9040         return 0
9041 }
9042 run_test 204e "Print raw stripe attributes ================="
9043
9044 test_204f() {
9045         mkdir -p $DIR/$tdir
9046         $SETSTRIPE --stripe-count 1 $DIR/$tdir
9047
9048         check_default_stripe_attr --stripe-size --raw
9049         check_default_stripe_attr --stripe-index --raw
9050
9051         return 0
9052 }
9053 run_test 204f "Print raw stripe size and offset  ==========="
9054
9055 test_204g() {
9056         mkdir -p $DIR/$tdir
9057         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
9058
9059         check_default_stripe_attr --stripe-count --raw
9060         check_default_stripe_attr --stripe-index --raw
9061
9062         return 0
9063 }
9064 run_test 204g "Print raw stripe count and offset ==========="
9065
9066 test_204h() {
9067         mkdir -p $DIR/$tdir
9068         $SETSTRIPE --stripe-index 0 $DIR/$tdir
9069
9070         check_default_stripe_attr --stripe-count --raw
9071         check_default_stripe_attr --stripe-size --raw
9072
9073         return 0
9074 }
9075 run_test 204h "Print raw stripe count and size ============="
9076
9077 # Figure out which job scheduler is being used, if any,
9078 # or use a fake one
9079 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
9080         JOBENV=SLURM_JOB_ID
9081 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
9082         JOBENV=LSB_JOBID
9083 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
9084         JOBENV=PBS_JOBID
9085 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
9086         JOBENV=LOADL_STEP_ID
9087 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
9088         JOBENV=JOB_ID
9089 else
9090         JOBENV=FAKE_JOBID
9091 fi
9092
9093 verify_jobstats() {
9094         local cmd=$1
9095         local target=$2
9096
9097         # clear old jobstats
9098         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
9099         do_facet ost0 lctl set_param obdfilter.*.job_stats="clear"
9100
9101         # use a new JobID for this test, or we might see an old one
9102         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
9103
9104         JOBVAL=${!JOBENV}
9105         log "Test: $cmd"
9106         log "Using JobID environment variable $JOBENV=$JOBVAL"
9107
9108         if [ $JOBENV = "FAKE_JOBID" ]; then
9109                 FAKE_JOBID=$JOBVAL $cmd
9110         else
9111                 $cmd
9112         fi
9113
9114         if [ "$target" = "mdt" -o "$target" = "both" ]; then
9115                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
9116                 do_facet $FACET lctl get_param mdt.*.job_stats |
9117                         grep $JOBVAL || error "No job stats found on MDT $FACET"
9118         fi
9119         if [ "$target" = "ost" -o "$target" = "both" ]; then
9120                 FACET=ost0
9121                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
9122                         grep $JOBVAL || error "No job stats found on OST $FACET"
9123         fi
9124 }
9125
9126 test_205() { # Job stats
9127         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
9128                 skip "Server doesn't support jobstats" && return 0
9129
9130         local cmd
9131         OLD_JOBENV=`$LCTL get_param -n jobid_var`
9132         if [ $OLD_JOBENV != $JOBENV ]; then
9133                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$JOBENV
9134                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9135                         $JOBENV || return 1
9136         fi
9137
9138         # mkdir
9139         cmd="mkdir $DIR/$tfile"
9140         verify_jobstats "$cmd" "mdt"
9141         # rmdir
9142         cmd="rm -fr $DIR/$tfile"
9143         verify_jobstats "$cmd" "mdt"
9144         # mknod
9145         cmd="mknod $DIR/$tfile c 1 3"
9146         verify_jobstats "$cmd" "mdt"
9147         # unlink
9148         cmd="rm -f $DIR/$tfile"
9149         verify_jobstats "$cmd" "mdt"
9150         # open & close
9151         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
9152         verify_jobstats "$cmd" "mdt"
9153         # setattr
9154         cmd="touch $DIR/$tfile"
9155         verify_jobstats "$cmd" "both"
9156         # write
9157         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
9158         verify_jobstats "$cmd" "ost"
9159         # read
9160         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
9161         verify_jobstats "$cmd" "ost"
9162         # truncate
9163         cmd="$TRUNCATE $DIR/$tfile 0"
9164         verify_jobstats "$cmd" "both"
9165         # rename
9166         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
9167         verify_jobstats "$cmd" "mdt"
9168
9169         # cleanup
9170         rm -f $DIR/jobstats_test_rename
9171
9172         if [ $OLD_JOBENV != $JOBENV ]; then
9173                 do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$OLD_JOBENV
9174                 wait_update $HOSTNAME "$LCTL get_param -n jobid_var" \
9175                         $OLD_JOBENV || return 1
9176         fi
9177 }
9178 run_test 205 "Verify job stats"
9179
9180 # LU-1480, LU-1773 and LU-1657
9181 test_206() {
9182         mkdir -p $DIR/$tdir
9183         lfs setstripe -c -1 $DIR/$tdir
9184 #define OBD_FAIL_LOV_INIT 0x1403
9185         $LCTL set_param fail_loc=0xa0001403
9186         $LCTL set_param fail_val=1
9187         touch $DIR/$tdir/$tfile || true
9188 }
9189 run_test 206 "fail lov_init_raid0() doesn't lbug"
9190
9191 test_212() {
9192         size=`date +%s`
9193         size=$((size % 8192 + 1))
9194         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
9195         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
9196         rm -f $DIR/f212 $DIR/f212.xyz
9197 }
9198 run_test 212 "Sendfile test ============================================"
9199
9200 test_213() {
9201         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
9202         cancel_lru_locks osc
9203         lctl set_param fail_loc=0x8000040f
9204         # generate a read lock
9205         cat $DIR/$tfile > /dev/null
9206         # write to the file, it will try to cancel the above read lock.
9207         cat /etc/hosts >> $DIR/$tfile
9208 }
9209 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
9210
9211 test_214() { # for bug 20133
9212         mkdir -p $DIR/d214p/d214c
9213         for (( i=0; i < 340; i++ )) ; do
9214                 touch $DIR/d214p/d214c/a$i
9215         done
9216
9217         ls -l $DIR/d214p || error "ls -l $DIR/d214p failed"
9218         mv $DIR/d214p/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
9219         ls $DIR/d214c || error "ls $DIR/d214c failed"
9220         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
9221 }
9222 run_test 214 "hash-indexed directory test - bug 20133"
9223
9224 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
9225 create_lnet_proc_files() {
9226         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
9227         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
9228
9229         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
9230         rm -f "$TMP/lnet_$1.sys_tmp"
9231 }
9232
9233 # counterpart of create_lnet_proc_files
9234 remove_lnet_proc_files() {
9235         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
9236 }
9237
9238 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9239 # 3rd arg as regexp for body
9240 check_lnet_proc_stats() {
9241         local l=$(cat "$TMP/lnet_$1" |wc -l)
9242         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
9243
9244         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
9245 }
9246
9247 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
9248 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
9249 # optional and can be regexp for 2nd line (lnet.routes case)
9250 check_lnet_proc_entry() {
9251         local blp=2            # blp stands for 'position of 1st line of body'
9252         [ "$5" = "" ] || blp=3 # lnet.routes case
9253
9254         local l=$(cat "$TMP/lnet_$1" |wc -l)
9255         # subtracting one from $blp because the body can be empty
9256         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
9257
9258         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
9259                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
9260
9261         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
9262                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
9263
9264         # bail out if any unexpected line happened
9265         sed -n "$blp~1 p" "$TMP/lnet_$1" |grep -Ev "$3"
9266         [ "$?" != 0 ] || error "$2 misformatted"
9267 }
9268
9269 test_215() { # for bugs 18102, 21079, 21517
9270         local N='(0|[1-9][0-9]*)'       # non-negative numeric
9271         local P='[1-9][0-9]*'           # positive numeric
9272         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
9273         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
9274         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
9275         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
9276
9277         local L1 # regexp for 1st line
9278         local L2 # regexp for 2nd line (optional)
9279         local BR # regexp for the rest (body)
9280
9281         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
9282         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
9283         create_lnet_proc_files "stats"
9284         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
9285         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
9286         remove_lnet_proc_files "stats"
9287
9288         # /proc/sys/lnet/routes should look like this:
9289         # Routing disabled/enabled
9290         # net hops state router
9291         # where net is a string like tcp0, hops >= 0, state is up/down,
9292         # router is a string like 192.168.1.1@tcp2
9293         L1="^Routing (disabled|enabled)$"
9294         L2="^net +hops +state +router$"
9295         BR="^$NET +$N +(up|down) +$NID$"
9296         create_lnet_proc_files "routes"
9297         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
9298         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
9299         remove_lnet_proc_files "routes"
9300
9301         # /proc/sys/lnet/routers should look like this:
9302         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
9303         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
9304         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
9305         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
9306         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
9307         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
9308         create_lnet_proc_files "routers"
9309         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
9310         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
9311         remove_lnet_proc_files "routers"
9312
9313         # /proc/sys/lnet/peers should look like this:
9314         # nid refs state last max rtr min tx min queue
9315         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
9316         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
9317         # numeric (0 or >0 or <0), queue >= 0.
9318         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
9319         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
9320         create_lnet_proc_files "peers"
9321         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
9322         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
9323         remove_lnet_proc_files "peers"
9324
9325         # /proc/sys/lnet/buffers  should look like this:
9326         # pages count credits min
9327         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
9328         L1="^pages +count +credits +min$"
9329         BR="^ +$N +$N +$I +$I$"
9330         create_lnet_proc_files "buffers"
9331         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
9332         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
9333         remove_lnet_proc_files "buffers"
9334
9335         # /proc/sys/lnet/nis should look like this:
9336         # nid status alive refs peer rtr max tx min
9337         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
9338         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
9339         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
9340         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
9341         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
9342         create_lnet_proc_files "nis"
9343         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
9344         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
9345         remove_lnet_proc_files "nis"
9346
9347         # can we successfully write to /proc/sys/lnet/stats?
9348         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
9349         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
9350 }
9351 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
9352
9353 test_216() { # bug 20317
9354         remote_ost_nodsh && skip "remote OST with nodsh" && return
9355         local node
9356         local p="$TMP/sanityN-$TESTNAME.parameters"
9357         save_lustre_params $HOSTNAME "osc.*.contention_seconds" > $p
9358         for node in $(osts_nodes); do
9359                 save_lustre_params $node "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
9360                 save_lustre_params $node "ldlm.namespaces.filter-*.contended_locks" >> $p
9361                 save_lustre_params $node "ldlm.namespaces.filter-*.contention_seconds" >> $p
9362         done
9363         clear_osc_stats
9364
9365         # agressive lockless i/o settings
9366         for node in $(osts_nodes); do
9367                 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'
9368         done
9369         lctl set_param -n osc.*.contention_seconds 60
9370
9371         $DIRECTIO write $DIR/$tfile 0 10 4096
9372         $CHECKSTAT -s 40960 $DIR/$tfile
9373
9374         # disable lockless i/o
9375         for node in $(osts_nodes); do
9376                 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'
9377         done
9378         lctl set_param -n osc.*.contention_seconds 0
9379         clear_osc_stats
9380
9381         dd if=/dev/zero of=$DIR/$tfile count=0
9382         $CHECKSTAT -s 0 $DIR/$tfile
9383
9384         restore_lustre_params <$p
9385         rm -f $p
9386         rm $DIR/$tfile
9387 }
9388 run_test 216 "check lockless direct write works and updates file size and kms correctly"
9389
9390 test_217() { # bug 22430
9391         local node
9392         local nid
9393
9394         for node in $(nodes_list); do
9395                 nid=$(host_nids_address $node $NETTYPE)
9396                 if [[ $nid = *-* ]] ; then
9397                         echo "lctl ping $nid@$NETTYPE"
9398                         lctl ping $nid@$NETTYPE
9399                 else
9400                         echo "skipping $node (no hyphen detected)"
9401                 fi
9402         done
9403 }
9404 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
9405
9406 test_218() {
9407        # do directio so as not to populate the page cache
9408        log "creating a 10 Mb file"
9409        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
9410        log "starting reads"
9411        dd if=$DIR/$tfile of=/dev/null bs=4096 &
9412        log "truncating the file"
9413        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
9414        log "killing dd"
9415        kill %+ || true # reads might have finished
9416        echo "wait until dd is finished"
9417        wait
9418        log "removing the temporary file"
9419        rm -rf $DIR/$tfile || error "tmp file removal failed"
9420 }
9421 run_test 218 "parallel read and truncate should not deadlock ======================="
9422
9423 test_219() {
9424         # write one partial page
9425         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
9426         # set no grant so vvp_io_commit_write will do sync write
9427         $LCTL set_param fail_loc=0x411
9428         # write a full page at the end of file
9429         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
9430
9431         $LCTL set_param fail_loc=0
9432         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
9433         $LCTL set_param fail_loc=0x411
9434         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
9435 }
9436 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
9437
9438 test_220() { #LU-325
9439         remote_ost_nodsh && skip "remote OST with nodsh" && return
9440         local OSTIDX=0
9441
9442         mkdir -p $DIR/$tdir
9443         local OST=$(lfs osts | grep ${OSTIDX}": " | \
9444                 awk '{print $2}' | sed -e 's/_UUID$//')
9445
9446         # on the mdt's osc
9447         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
9448         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
9449                         osc.$mdtosc_proc1.prealloc_last_id)
9450         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
9451                         osc.$mdtosc_proc1.prealloc_next_id)
9452
9453         $LFS df -i
9454
9455         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
9456         #define OBD_FAIL_OST_ENOINO              0x229
9457         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
9458         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
9459         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
9460
9461         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
9462
9463         MDSOBJS=$((last_id - next_id))
9464         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
9465
9466         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
9467         echo "OST still has $count kbytes free"
9468
9469         echo "create $MDSOBJS files @next_id..."
9470         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
9471
9472         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9473                         osc.$mdtosc_proc1.prealloc_last_id)
9474         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
9475                         osc.$mdtosc_proc1.prealloc_next_id)
9476
9477         echo "after creation, last_id=$last_id2, next_id=$next_id2"
9478         $LFS df -i
9479
9480         echo "cleanup..."
9481
9482         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
9483         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
9484
9485         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
9486         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
9487         echo "unlink $MDSOBJS files @$next_id..."
9488         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
9489 }
9490 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
9491
9492 test_221() {
9493         dd if=`which date` of=$MOUNT/date oflag=sync
9494         chmod +x $MOUNT/date
9495
9496         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
9497         $LCTL set_param fail_loc=0x80001401
9498
9499         $MOUNT/date > /dev/null
9500         rm -f $MOUNT/date
9501 }
9502 run_test 221 "make sure fault and truncate race to not cause OOM"
9503
9504 test_222a () {
9505        rm -rf $DIR/$tdir
9506        mkdir -p $DIR/$tdir
9507        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9508        createmany -o $DIR/$tdir/$tfile 10
9509        cancel_lru_locks mdc
9510        cancel_lru_locks osc
9511        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9512        $LCTL set_param fail_loc=0x31a
9513        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
9514        $LCTL set_param fail_loc=0
9515        rm -r $DIR/$tdir
9516 }
9517 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
9518
9519 test_222b () {
9520        rm -rf $DIR/$tdir
9521        mkdir -p $DIR/$tdir
9522        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9523        createmany -o $DIR/$tdir/$tfile 10
9524        cancel_lru_locks mdc
9525        cancel_lru_locks osc
9526        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
9527        $LCTL set_param fail_loc=0x31a
9528        rm -r $DIR/$tdir || "AGL for rmdir failed"
9529        $LCTL set_param fail_loc=0
9530 }
9531 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
9532
9533 test_223 () {
9534        rm -rf $DIR/$tdir
9535        mkdir -p $DIR/$tdir
9536        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
9537        createmany -o $DIR/$tdir/$tfile 10
9538        cancel_lru_locks mdc
9539        cancel_lru_locks osc
9540        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
9541        $LCTL set_param fail_loc=0x31b
9542        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
9543        $LCTL set_param fail_loc=0
9544        rm -r $DIR/$tdir
9545 }
9546 run_test 223 "osc reenqueue if without AGL lock granted ======================="
9547
9548 test_224a() { # LU-1039, MRP-303
9549         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
9550         $LCTL set_param fail_loc=0x508
9551         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
9552         $LCTL set_param fail_loc=0
9553         df $DIR
9554 }
9555 run_test 224a "Don't panic on bulk IO failure"
9556
9557 test_224b() { # LU-1039, MRP-303
9558         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
9559         cancel_lru_locks osc
9560         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
9561         $LCTL set_param fail_loc=0x515
9562         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
9563         $LCTL set_param fail_loc=0
9564         df $DIR
9565 }
9566 run_test 224b "Don't panic on bulk IO failure"
9567
9568 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
9569 test_225a () {
9570        if [ -z ${MDSSURVEY} ]; then
9571               skip_env "mds-survey not found" && return
9572        fi
9573        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9574             { skip "Need MDS version at least 2.2.51"; return; }
9575
9576        local mds=$(facet_host $SINGLEMDS)
9577        local target=$(do_nodes $mds 'lctl dl' | \
9578                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9579
9580        local cmd1="file_count=1000 thrhi=4"
9581        local cmd2="dir_count=2 layer=mdd stripe_count=0"
9582        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9583        local cmd="$cmd1 $cmd2 $cmd3"
9584
9585        rm -f ${TMP}/mds_survey*
9586        echo + $cmd
9587        eval $cmd || error "mds-survey with zero-stripe failed"
9588        cat ${TMP}/mds_survey*
9589        rm -f ${TMP}/mds_survey*
9590 }
9591 run_test 225a "Metadata survey sanity with zero-stripe"
9592
9593 test_225b () {
9594        if [ -z ${MDSSURVEY} ]; then
9595               skip_env "mds-survey not found" && return
9596        fi
9597        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
9598             { skip "Need MDS version at least 2.2.51"; return; }
9599
9600        if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
9601               skip_env "Need to mount OST to test" && return
9602        fi
9603
9604        local mds=$(facet_host $SINGLEMDS)
9605        local target=$(do_nodes $mds 'lctl dl' | \
9606                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
9607
9608        local cmd1="file_count=1000 thrhi=4"
9609        local cmd2="dir_count=2 layer=mdd stripe_count=1"
9610        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
9611        local cmd="$cmd1 $cmd2 $cmd3"
9612
9613        rm -f ${TMP}/mds_survey*
9614        echo + $cmd
9615        eval $cmd || error "mds-survey with stripe_count failed"
9616        cat ${TMP}/mds_survey*
9617        rm -f ${TMP}/mds_survey*
9618 }
9619 run_test 225b "Metadata survey sanity with stripe_count = 1"
9620
9621 mcreate_path2fid () {
9622         local mode=$1
9623         local major=$2
9624         local minor=$3
9625         local name=$4
9626         local desc=$5
9627         local path=$DIR/$tdir/$name
9628         local fid
9629         local rc
9630         local fid_path
9631
9632         $MCREATE --mode=$1 --major=$2 --minor=$3 $path || \
9633                 error "error: cannot create $desc"
9634
9635         fid=$($LFS path2fid $path)
9636         rc=$?
9637         [ $rc -ne 0 ] && error "error: cannot get fid of a $desc"
9638
9639         fid_path=$($LFS fid2path $DIR $fid)
9640         rc=$?
9641         [ $rc -ne 0 ] && error "error: cannot get path of a $desc by fid"
9642
9643         [ "$path" == "$fid_path" ] || \
9644                 error "error: fid2path returned \`$fid_path', expected \`$path'"
9645 }
9646
9647 test_226 () {
9648         rm -rf $DIR/$tdir
9649         mkdir -p $DIR/$tdir
9650
9651         mcreate_path2fid 0010666 0 0 fifo "FIFO"
9652         mcreate_path2fid 0020666 1 3 null "character special file (null)"
9653         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
9654         mcreate_path2fid 0040666 0 0 dir "directory"
9655         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
9656         mcreate_path2fid 0100666 0 0 file "regular file"
9657         mcreate_path2fid 0120666 0 0 link "symbolic link"
9658         mcreate_path2fid 0140666 0 0 sock "socket"
9659 }
9660 run_test 226 "call path2fid and fid2path on files of all type"
9661
9662 # LU-1299 Executing or running ldd on a truncated executable does not
9663 # cause an out-of-memory condition.
9664 test_227() {
9665         dd if=`which date` of=$MOUNT/date bs=1k count=1
9666         chmod +x $MOUNT/date
9667
9668         $MOUNT/date > /dev/null
9669         ldd $MOUNT/date > /dev/null
9670         rm -f $MOUNT/date
9671 }
9672 run_test 227 "running truncated executable does not cause OOM"
9673
9674 # LU-1512 try to reuse idle OI blocks
9675 test_228a() {
9676         [ "$FSTYPE" != "ldiskfs" ] && skip "non-ldiskfs backend" && return
9677
9678         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9679         local myDIR=$DIR/$tdir
9680
9681         mkdir -p $myDIR
9682         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9683         $LCTL set_param fail_loc=0x80001002
9684         createmany -o $myDIR/t- 10000
9685         $LCTL set_param fail_loc=0
9686         # The guard is current the largest FID holder
9687         touch $myDIR/guard
9688         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9689                     tr -d '[')
9690         local IDX=$(($SEQ % 64))
9691
9692         do_facet $SINGLEMDS sync
9693         # Make sure journal flushed.
9694         sleep 6
9695         local blk1=$(do_facet $SINGLEMDS \
9696                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9697                      grep Blockcount | awk '{print $4}')
9698
9699         # Remove old files, some OI blocks will become idle.
9700         unlinkmany $myDIR/t- 10000
9701         # Create new files, idle OI blocks should be reused.
9702         createmany -o $myDIR/t- 2000
9703         do_facet $SINGLEMDS sync
9704         # Make sure journal flushed.
9705         sleep 6
9706         local blk2=$(do_facet $SINGLEMDS \
9707                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9708                      grep Blockcount | awk '{print $4}')
9709
9710         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9711 }
9712 run_test 228a "try to reuse idle OI blocks"
9713
9714 test_228b() {
9715         [ "$FSTYPE" != "ldiskfs" ] && skip "non-ldiskfs backend" && return
9716
9717         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9718         local myDIR=$DIR/$tdir
9719
9720         mkdir -p $myDIR
9721         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9722         $LCTL set_param fail_loc=0x80001002
9723         createmany -o $myDIR/t- 10000
9724         $LCTL set_param fail_loc=0
9725         # The guard is current the largest FID holder
9726         touch $myDIR/guard
9727         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9728                     tr -d '[')
9729         local IDX=$(($SEQ % 64))
9730
9731         do_facet $SINGLEMDS sync
9732         # Make sure journal flushed.
9733         sleep 6
9734         local blk1=$(do_facet $SINGLEMDS \
9735                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9736                      grep Blockcount | awk '{print $4}')
9737
9738         # Remove old files, some OI blocks will become idle.
9739         unlinkmany $myDIR/t- 10000
9740
9741         # stop the MDT
9742         stop $SINGLEMDS || error "Fail to stop MDT."
9743         # remount the MDT
9744         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9745
9746         df $MOUNT || error "Fail to df."
9747         # Create new files, idle OI blocks should be reused.
9748         createmany -o $myDIR/t- 2000
9749         do_facet $SINGLEMDS sync
9750         # Make sure journal flushed.
9751         sleep 6
9752         local blk2=$(do_facet $SINGLEMDS \
9753                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9754                      grep Blockcount | awk '{print $4}')
9755
9756         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9757 }
9758 run_test 228b "idle OI blocks can be reused after MDT restart"
9759
9760 #LU-1881
9761 test_228c() {
9762         [ "$FSTYPE" != "ldiskfs" ] && skip "non-ldiskfs backend" && return
9763
9764         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9765         local myDIR=$DIR/$tdir
9766
9767         mkdir -p $myDIR
9768         #define OBD_FAIL_SEQ_EXHAUST             0x1002
9769         $LCTL set_param fail_loc=0x80001002
9770         # 20000 files can guarantee there are index nodes in the OI file
9771         createmany -o $myDIR/t- 20000
9772         $LCTL set_param fail_loc=0
9773         # The guard is current the largest FID holder
9774         touch $myDIR/guard
9775         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
9776                     tr -d '[')
9777         local IDX=$(($SEQ % 64))
9778
9779         do_facet $SINGLEMDS sync
9780         # Make sure journal flushed.
9781         sleep 6
9782         local blk1=$(do_facet $SINGLEMDS \
9783                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9784                      grep Blockcount | awk '{print $4}')
9785
9786         # Remove old files, some OI blocks will become idle.
9787         unlinkmany $myDIR/t- 20000
9788         rm -f $myDIR/guard
9789         # The OI file should become empty now
9790
9791         # Create new files, idle OI blocks should be reused.
9792         createmany -o $myDIR/t- 2000
9793         do_facet $SINGLEMDS sync
9794         # Make sure journal flushed.
9795         sleep 6
9796         local blk2=$(do_facet $SINGLEMDS \
9797                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
9798                      grep Blockcount | awk '{print $4}')
9799
9800         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
9801 }
9802 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
9803
9804 #
9805 # tests that do cleanup/setup should be run at the end
9806 #
9807
9808 test_900() {
9809         local ls
9810         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
9811         $LCTL set_param fail_loc=0x903
9812         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
9813         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
9814                 echo "clear" > $ls
9815         done
9816         FAIL_ON_ERROR=true cleanup
9817         FAIL_ON_ERROR=true setup
9818 }
9819 run_test 900 "umount should not race with any mgc requeue thread"
9820
9821 complete $(basename $0) $SECONDS
9822 check_and_cleanup_lustre
9823 if [ "$I_MOUNTED" != "yes" ]; then
9824         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
9825 fi
9826 exit_status