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