Whamcloud - gitweb
LU-4991 test: check files in sanity 56s
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: 13297 2108 9789 3637 9789 3561 12622 5188
12 ALWAYS_EXCEPT="                42a  42b  42c  42d  45   51d   68b   $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # with LOD/OSP landing
16 # bug number for skipped tests: LU-2036
17 ALWAYS_EXCEPT="                 76     $ALWAYS_EXCEPT"
18
19 is_sles11()                                             # LU-4351
20 {
21         if [ -r /etc/SuSE-release ]
22         then
23                 local vers=`grep VERSION /etc/SuSE-release | awk '{print $3}'`
24                 if [ $vers -eq 11 ]
25                 then
26                         return 0
27                 fi
28         fi
29         return 1
30 }
31
32 if is_sles11; then                                      # LU-4351
33         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 54c"
34 fi
35
36 SRCDIR=$(cd $(dirname $0); echo $PWD)
37 export PATH=$PATH:/sbin
38
39 TMP=${TMP:-/tmp}
40
41 CC=${CC:-cc}
42 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
43 CREATETEST=${CREATETEST:-createtest}
44 LFS=${LFS:-lfs}
45 LFIND=${LFIND:-"$LFS find"}
46 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
47 LCTL=${LCTL:-lctl}
48 MCREATE=${MCREATE:-mcreate}
49 OPENFILE=${OPENFILE:-openfile}
50 OPENUNLINK=${OPENUNLINK:-openunlink}
51 export MULTIOP=${MULTIOP:-multiop}
52 READS=${READS:-"reads"}
53 MUNLINK=${MUNLINK:-munlink}
54 SOCKETSERVER=${SOCKETSERVER:-socketserver}
55 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
56 MEMHOG=${MEMHOG:-memhog}
57 DIRECTIO=${DIRECTIO:-directio}
58 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
59 UMOUNT=${UMOUNT:-"umount -d"}
60 STRIPES_PER_OBJ=-1
61 CHECK_GRANT=${CHECK_GRANT:-"yes"}
62 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
63 export PARALLEL=${PARALLEL:-"no"}
64
65 export NAME=${NAME:-local}
66
67 SAVE_PWD=$PWD
68
69 CLEANUP=${CLEANUP:-:}
70 SETUP=${SETUP:-:}
71 TRACE=${TRACE:-""}
72 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
73 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
74 . $LUSTRE/tests/test-framework.sh
75 init_test_env $@
76 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
77 init_logging
78
79 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 27m 64b 68 71 77f 78 115 124b 230d"
80
81 [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
82 # bug number for skipped test:        LU-1593 LU-2610 LU-2833 LU-1957 LU-2805
83         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 34h     40      48a     180     184c"
84
85 FAIL_ON_ERROR=false
86
87 cleanup() {
88         echo -n "cln.."
89         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
90         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
91 }
92 setup() {
93         echo -n "mnt.."
94         load_modules
95         setupall || exit 10
96         echo "done"
97 }
98
99 check_kernel_version() {
100         WANT_VER=$1
101         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
102         case $GOT_VER in
103         patchless|patchless_client) return 0;;
104         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
105         esac
106         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
107         return 1
108 }
109
110 check_swap_layouts_support()
111 {
112         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
113                 { skip "Does not support layout lock."; return 0; }
114         return 1
115 }
116
117 if [ "$ONLY" == "cleanup" ]; then
118        sh llmountcleanup.sh
119        exit 0
120 fi
121
122 check_and_setup_lustre
123
124 DIR=${DIR:-$MOUNT}
125 assert_DIR
126
127 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
128         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
129 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
130 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
131 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
132 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
133 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
134 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
135
136 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
137 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
138 rm -rf $DIR/[Rdfs][0-9]*
139
140 # $RUNAS_ID may get set incorrectly somewhere else
141 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
142
143 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
144
145 build_test_filter
146
147 if [ "${ONLY}" = "MOUNT" ] ; then
148         echo "Lustre is up, please go on"
149         exit
150 fi
151
152 echo "preparing for tests involving mounts"
153 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
154 touch $EXT2_DEV
155 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
156 echo # add a newline after mke2fs.
157
158 umask 077
159
160 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
161 lctl set_param debug=-1 2> /dev/null || true
162 test_0a() {
163         touch $DIR/$tfile
164         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
165         rm $DIR/$tfile
166         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
167 }
168 run_test 0a "touch; rm ====================="
169
170 test_0b() {
171         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
172         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
173 }
174 run_test 0b "chmod 0755 $DIR ============================="
175
176 test_0c() {
177         $LCTL get_param mdc.*.import | grep "state: FULL" ||
178                 error "import not FULL"
179         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
180                 error "bad target"
181 }
182 run_test 0c "check import proc ============================="
183
184 test_1() {
185         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
186         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
187         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
188         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
189         rmdir $DIR/$tdir/d2
190         rmdir $DIR/$tdir
191         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
192 }
193 run_test 1 "mkdir; remkdir; rmdir =============================="
194
195 test_2() {
196         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
197         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
198         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
199         rm -r $DIR/$tdir
200         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
201 }
202 run_test 2 "mkdir; touch; rmdir; check file ===================="
203
204 test_3() {
205         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
206         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
207         touch $DIR/$tdir/$tfile
208         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
209         rm -r $DIR/$tdir
210         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
211 }
212 run_test 3 "mkdir; touch; rmdir; check dir ====================="
213
214 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
215 test_4() {
216         local MDTIDX=1
217         local remote_dir=remote_dir
218
219         test_mkdir $DIR/$remote_dir ||
220                 error "Create remote directory failed"
221
222         touch $DIR/$remote_dir/$tfile ||
223                 error "Create file under remote directory failed"
224
225         rmdir $DIR/$remote_dir &&
226                 error "Expect error removing in-use dir $DIR/$remote_dir"
227
228         test -d $DIR/$remote_dir || error "Remote directory disappeared"
229
230         rm -rf $DIR/$remote_dir || error "remove remote dir error"
231 }
232 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
233
234 test_5() {
235         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
236         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
237         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
238         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
239         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
240 }
241 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
242
243 test_6a() {
244         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
245         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
246         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
247                 error "$tfile does not have perm 0666 or UID $UID"
248         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
249         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
250                 error "$tfile should be 0666 and owned by UID $UID"
251 }
252 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
253
254 test_6c() {
255         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
256         touch $DIR/$tfile
257         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
258         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
259                 error "$tfile should be owned by UID $RUNAS_ID"
260         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
261         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
262                 error "$tfile should be owned by UID $RUNAS_ID"
263 }
264 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
265
266 test_6e() {
267         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
268         touch $DIR/$tfile
269         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
270         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
271                 error "$tfile should be owned by GID $UID"
272         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
273         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
274                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
275 }
276 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
277
278 test_6g() {
279         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
280         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
281         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
282         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
283         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
284         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
285         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
286                 error "$tdir/d/subdir should be GID $RUNAS_GID"
287 }
288 run_test 6g "Is new dir in sgid dir inheriting group?"
289
290 test_6h() { # bug 7331
291         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
292         touch $DIR/$tfile || error "touch failed"
293         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
294         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
295                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
296         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
297                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
298 }
299 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
300
301 test_7a() {
302         test_mkdir $DIR/$tdir
303         $MCREATE $DIR/$tdir/$tfile
304         chmod 0666 $DIR/$tdir/$tfile
305         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
306                 error "$tdir/$tfile should be mode 0666"
307 }
308 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
309
310 test_7b() {
311         if [ ! -d $DIR/$tdir ]; then
312                 mkdir $DIR/$tdir
313         fi
314         $MCREATE $DIR/$tdir/$tfile
315         echo -n foo > $DIR/$tdir/$tfile
316         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
317         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
318 }
319 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
320
321 test_8() {
322         test_mkdir $DIR/$tdir
323         touch $DIR/$tdir/$tfile
324         chmod 0666 $DIR/$tdir/$tfile
325         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
326                 error "$tfile mode not 0666"
327 }
328 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
329
330 test_9() {
331         test_mkdir $DIR/$tdir
332         test_mkdir $DIR/$tdir/d2
333         test_mkdir $DIR/$tdir/d2/d3
334         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
335 }
336 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
337
338 test_10() {
339         test_mkdir $DIR/$tdir
340         test_mkdir $DIR/$tdir/d2
341         touch $DIR/$tdir/d2/$tfile
342         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
343                 error "$tdir/d2/$tfile not a file"
344 }
345 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
346
347 test_11() {
348         test_mkdir $DIR/$tdir
349         test_mkdir $DIR/$tdir/d2
350         chmod 0666 $DIR/$tdir/d2
351         chmod 0705 $DIR/$tdir/d2
352         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
353                 error "$tdir/d2 mode not 0705"
354 }
355 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
356
357 test_12() {
358         test_mkdir $DIR/$tdir
359         touch $DIR/$tdir/$tfile
360         chmod 0666 $DIR/$tdir/$tfile
361         chmod 0654 $DIR/$tdir/$tfile
362         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
363                 error "$tdir/d2 mode not 0654"
364 }
365 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
366
367 test_13() {
368         test_mkdir $DIR/$tdir
369         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
370         >  $DIR/$tdir/$tfile
371         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
372                 error "$tdir/$tfile size not 0 after truncate"
373 }
374 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
375
376 test_14() {
377         test_mkdir $DIR/$tdir
378         touch $DIR/$tdir/$tfile
379         rm $DIR/$tdir/$tfile
380         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
381 }
382 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
383
384 test_15() {
385         test_mkdir $DIR/$tdir
386         touch $DIR/$tdir/$tfile
387         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
388         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
389                 error "$tdir/${tfile_2} not a file after rename"
390 }
391 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
392
393 test_16() {
394         test_mkdir $DIR/$tdir
395         touch $DIR/$tdir/$tfile
396         rm -rf $DIR/$tdir/$tfile
397         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
398 }
399 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
400
401 test_17a() {
402         test_mkdir -p $DIR/$tdir
403         touch $DIR/$tdir/$tfile
404         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
405         ls -l $DIR/$tdir
406         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
407                 error "$tdir/l-exist not a symlink"
408         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
409                 error "$tdir/l-exist not referencing a file"
410         rm -f $DIR/$tdir/l-exist
411         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
412 }
413 run_test 17a "symlinks: create, remove (real) =================="
414
415 test_17b() {
416         test_mkdir -p $DIR/$tdir
417         ln -s no-such-file $DIR/$tdir/l-dangle
418         ls -l $DIR/$tdir
419         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
420                 error "$tdir/l-dangle not referencing no-such-file"
421         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
422                 error "$tdir/l-dangle not referencing non-existent file"
423         rm -f $DIR/$tdir/l-dangle
424         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
425 }
426 run_test 17b "symlinks: create, remove (dangling) =============="
427
428 test_17c() { # bug 3440 - don't save failed open RPC for replay
429         test_mkdir -p $DIR/$tdir
430         ln -s foo $DIR/$tdir/$tfile
431         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
432 }
433 run_test 17c "symlinks: open dangling (should return error) ===="
434
435 test_17d() {
436         test_mkdir -p $DIR/$tdir
437         ln -s foo $DIR/$tdir/$tfile
438         touch $DIR/$tdir/$tfile || error "creating to new symlink"
439 }
440 run_test 17d "symlinks: create dangling ========================"
441
442 test_17e() {
443         test_mkdir -p $DIR/$tdir
444         local foo=$DIR/$tdir/$tfile
445         ln -s $foo $foo || error "create symlink failed"
446         ls -l $foo || error "ls -l failed"
447         ls $foo && error "ls not failed" || true
448 }
449 run_test 17e "symlinks: create recursive symlink (should return error) ===="
450
451 test_17f() {
452         test_mkdir -p $DIR/d17f
453         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/d17f/111
454         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/d17f/222
455         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/d17f/333
456         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/d17f/444
457         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/d17f/555
458         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
459         ls -l  $DIR/d17f
460 }
461 run_test 17f "symlinks: long and very long symlink name ========================"
462
463 # str_repeat(S, N) generate a string that is string S repeated N times
464 str_repeat() {
465         local s=$1
466         local n=$2
467         local ret=''
468         while [ $((n -= 1)) -ge 0 ]; do
469                 ret=$ret$s
470         done
471         echo $ret
472 }
473
474 # Long symlinks and LU-2241
475 test_17g() {
476         test_mkdir -p $DIR/$tdir
477         local TESTS="59 60 61 4094 4095"
478
479         # Fix for inode size boundary in 2.1.4
480         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
481                 TESTS="4094 4095"
482
483         # Patch not applied to 2.2 or 2.3 branches
484         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
485         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
486                 TESTS="4094 4095"
487
488         # skip long symlink name for rhel6.5.
489         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
490         grep -q '6.5' /etc/redhat-release &>/dev/null &&
491                 TESTS="59 60 61 4062 4063"
492
493         for i in $TESTS; do
494                 local SYMNAME=$(str_repeat 'x' $i)
495                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
496                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
497         done
498 }
499 run_test 17g "symlinks: really long symlink name and inode boundaries"
500
501 test_17h() { #bug 17378
502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
504         local mdt_idx
505         test_mkdir -p $DIR/$tdir
506         if [[ $MDSCOUNT -gt 1 ]]; then
507                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
508         else
509                 mdt_idx=0
510         fi
511         $SETSTRIPE -c -1 $DIR/$tdir
512 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
513         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
514         touch $DIR/$tdir/$tfile || true
515 }
516 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
517
518 test_17i() { #bug 20018
519         remote_mds_nodsh && skip "remote MDS with nodsh" && return
520         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
521         test_mkdir -c1 $DIR/$tdir
522         local foo=$DIR/$tdir/$tfile
523         local mdt_idx
524         if [[ $MDSCOUNT -gt 1 ]]; then
525                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
526         else
527                 mdt_idx=0
528         fi
529         ln -s $foo $foo || error "create symlink failed"
530 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
531         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
532         ls -l $foo && error "error not detected"
533         return 0
534 }
535 run_test 17i "don't panic on short symlink"
536
537 test_17k() { #bug 22301
538         [[ -z "$(which rsync 2>/dev/null)" ]] &&
539                 skip "no rsync command" && return 0
540         rsync --help | grep -q xattr ||
541                 skip_env "$(rsync --version | head -n1) does not support xattrs"
542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
543         test_mkdir -p $DIR/$tdir
544         test_mkdir -p $DIR/$tdir.new
545         touch $DIR/$tdir/$tfile
546         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
547         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
548                 error "rsync failed with xattrs enabled"
549 }
550 run_test 17k "symlinks: rsync with xattrs enabled ========================="
551
552 test_17l() { # LU-279
553         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
554                 skip "no getfattr command" && return 0
555         mkdir -p $DIR/$tdir
556         touch $DIR/$tdir/$tfile
557         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
558         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
559                 # -h to not follow symlinks. -m '' to list all the xattrs.
560                 # grep to remove first line: '# file: $path'.
561                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
562                 do
563                         lgetxattr_size_check $path $xattr ||
564                                 error "lgetxattr_size_check $path $xattr failed"
565                 done
566         done
567 }
568 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
569
570 # LU-1540
571 test_17m() {
572         local short_sym="0123456789"
573         local WDIR=$DIR/${tdir}m
574         local mds_index
575         local devname
576         local cmd
577         local i
578         local rc=0
579
580         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
581         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
582                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
583
584         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
585                 skip "only for ldiskfs MDT" && return 0
586
587         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
588
589         mkdir -p $WDIR
590         long_sym=$short_sym
591         # create a long symlink file
592         for ((i = 0; i < 4; ++i)); do
593                 long_sym=${long_sym}${long_sym}
594         done
595
596         echo "create 512 short and long symlink files under $WDIR"
597         for ((i = 0; i < 256; ++i)); do
598                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
599                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
600         done
601
602         echo "erase them"
603         rm -f $WDIR/*
604         sync
605         wait_delete_completed
606
607         echo "recreate the 512 symlink files with a shorter string"
608         for ((i = 0; i < 512; ++i)); do
609                 # rewrite the symlink file with a shorter string
610                 ln -sf ${long_sym} $WDIR/long-$i
611                 ln -sf ${short_sym} $WDIR/short-$i
612         done
613
614         mds_index=$($LFS getstripe -M $WDIR)
615         mds_index=$((mds_index+1))
616         devname=$(mdsdevname $mds_index)
617         cmd="$E2FSCK -fnvd $devname"
618
619         echo "stop and checking mds${mds_index}: $cmd"
620         # e2fsck should not return error
621         stop mds${mds_index}
622         do_facet mds${mds_index} $cmd || rc=$?
623
624         start mds${mds_index} $devname $MDS_MOUNT_OPTS
625         df $MOUNT > /dev/null 2>&1
626         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
627                 "short/long symlink MDT: rc=$rc"
628         return $rc
629 }
630 run_test 17m "run e2fsck against MDT which contains short/long symlink"
631
632 check_fs_consistency_17n() {
633         local mdt_index
634         local devname
635         local cmd
636         local rc=0
637
638         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
639         # so it only check MDT1/MDT2 instead of all of MDTs.
640         for mdt_index in $(seq 1 2); do
641                 devname=$(mdsdevname $mdt_index)
642                 cmd="$E2FSCK -fnvd $devname"
643
644                 echo "stop and checking mds${mdt_index}: $cmd"
645                 # e2fsck should not return error
646                 stop mds${mdt_index}
647                 do_facet mds${mdt_index} $cmd || rc=$?
648
649                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS
650                 df $MOUNT > /dev/null 2>&1
651                 [ $rc -ne 0 ] && break
652         done
653         return $rc
654 }
655
656 test_17n() {
657         local i
658
659         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
660         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
661                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
662
663         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
664                 skip "only for ldiskfs MDT" && return 0
665
666         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
667
668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
669
670         mkdir $DIR/$tdir
671         for ((i=0; i<10; i++)); do
672                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
673                         error "create remote dir error $i"
674                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
675                         error "create files under remote dir failed $i"
676         done
677
678         check_fs_consistency_17n ||
679                 error "e2fsck report error after create files under remote dir"
680
681         for ((i=0;i<10;i++)); do
682                 rm -rf $DIR/$tdir/remote_dir_${i} ||
683                         error "destroy remote dir error $i"
684         done
685
686         check_fs_consistency_17n ||
687                 error "e2fsck report error after unlink files under remote dir"
688
689         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
690                 skip "lustre < 2.4.50 does not support migrate mv " && return
691
692         for ((i=0; i<10; i++)); do
693                 mkdir -p $DIR/$tdir/remote_dir_${i}
694                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
695                         error "create files under remote dir failed $i"
696                 $LFS mv -M 1 $DIR/$tdir/remote_dir_${i} ||
697                         error "migrate remote dir error $i"
698         done
699         check_fs_consistency_17n || error "e2fsck report error after migration"
700
701         for ((i=0;i<10;i++)); do
702                 rm -rf $DIR/$tdir/remote_dir_${i} ||
703                         error "destroy remote dir error $i"
704         done
705
706         check_fs_consistency_17n || error "e2fsck report error after unlink"
707 }
708 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
709
710 test_17o() {
711         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
712                 skip "Need MDS version at least 2.3.64" && return
713
714         local WDIR=$DIR/${tdir}o
715         local mdt_index
716         local mdtdevname
717         local rc=0
718
719         mkdir -p $WDIR
720         mdt_index=$($LFS getstripe -M $WDIR)
721         mdt_index=$((mdt_index+1))
722         mdtdevname=$(mdsdevname $mdt_index)
723
724         touch $WDIR/$tfile
725         stop mds${mdt_index}
726         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS
727
728         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
729         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
730         ls -l $WDIR/$tfile && rc=1
731         do_facet mds${mdt_index} lctl set_param fail_loc=0
732         [[ $rc -ne 0 ]] && error "stat file should fail"
733         true
734 }
735 run_test 17o "stat file with incompat LMA feature"
736
737 test_18() {
738         touch $DIR/f || error "Failed to touch $DIR/f: $?"
739         ls $DIR || error "Failed to ls $DIR: $?"
740 }
741 run_test 18 "touch .../f ; ls ... =============================="
742
743 test_19a() {
744         touch $DIR/$tfile
745         ls -l $DIR
746         rm $DIR/$tfile
747         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
748 }
749 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
750
751 test_19b() {
752         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
753 }
754 run_test 19b "ls -l .../f19 (should return error) =============="
755
756 test_19c() {
757         [ $RUNAS_ID -eq $UID ] &&
758                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
759         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
760 }
761 run_test 19c "$RUNAS touch .../f19 (should return error) =="
762
763 test_19d() {
764         cat $DIR/f19 && error || true
765 }
766 run_test 19d "cat .../f19 (should return error) =============="
767
768 test_20() {
769         touch $DIR/$tfile
770         rm $DIR/$tfile
771         log "1 done"
772         touch $DIR/$tfile
773         rm $DIR/$tfile
774         log "2 done"
775         touch $DIR/$tfile
776         rm $DIR/$tfile
777         log "3 done"
778         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
779 }
780 run_test 20 "touch .../f ; ls -l ... ==========================="
781
782 test_21() {
783         test_mkdir -p $DIR/$tdir
784         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
785         ln -s dangle $DIR/$tdir/link
786         echo foo >> $DIR/$tdir/link
787         cat $DIR/$tdir/dangle
788         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
789         $CHECKSTAT -f -t file $DIR/$tdir/link ||
790                 error "$tdir/link not linked to a file"
791 }
792 run_test 21 "write to dangling link ============================"
793
794 test_22() {
795         WDIR=$DIR/$tdir
796         test_mkdir -p $DIR/$tdir
797         chown $RUNAS_ID:$RUNAS_GID $WDIR
798         (cd $WDIR || error "cd $WDIR failed";
799         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
800         $RUNAS tar xf -)
801         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
802         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
803         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
804 }
805 run_test 22 "unpack tar archive as non-root user ==============="
806
807 # was test_23
808 test_23a() {
809         test_mkdir -p $DIR/$tdir
810         local file=$DIR/$tdir/$tfile
811
812         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
813         openfile -f O_CREAT:O_EXCL $file &&
814                 error "$file recreate succeeded" || true
815 }
816 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
817
818 test_23b() { # bug 18988
819         test_mkdir -p $DIR/$tdir
820         local file=$DIR/$tdir/$tfile
821
822         rm -f $file
823         echo foo > $file || error "write filed"
824         echo bar >> $file || error "append filed"
825         $CHECKSTAT -s 8 $file || error "wrong size"
826         rm $file
827 }
828 run_test 23b "O_APPEND check =========================="
829
830 # rename sanity
831 test_24a() {
832         echo '-- same directory rename'
833         test_mkdir $DIR/$tdir
834         touch $DIR/$tdir/$tfile.1
835         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
836         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
837 }
838 run_test 24a "rename file to non-existent target"
839
840 test_24b() {
841         test_mkdir $DIR/$tdir
842         touch $DIR/$tdir/$tfile.{1,2}
843         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
844         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
845         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
846 }
847 run_test 24b "rename file to existing target"
848
849 test_24c() {
850         test_mkdir $DIR/$tdir
851         test_mkdir $DIR/$tdir/d$testnum.1
852         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
853         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
854         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
855 }
856 run_test 24c "rename directory to non-existent target"
857
858 test_24d() {
859         test_mkdir -c1 $DIR/$tdir
860         test_mkdir -c1 $DIR/$tdir/d$testnum.1
861         test_mkdir -c1 $DIR/$tdir/d$testnum.2
862         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
863         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
864         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
865 }
866 run_test 24d "rename directory to existing target"
867
868 test_24e() {
869         echo '-- cross directory renames --'
870         test_mkdir $DIR/R5a
871         test_mkdir $DIR/R5b
872         touch $DIR/R5a/f
873         mv $DIR/R5a/f $DIR/R5b/g
874         $CHECKSTAT -a $DIR/R5a/f || error
875         $CHECKSTAT -t file $DIR/R5b/g || error
876 }
877 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
878
879 test_24f() {
880         test_mkdir $DIR/R6a
881         test_mkdir $DIR/R6b
882         touch $DIR/R6a/f $DIR/R6b/g
883         mv $DIR/R6a/f $DIR/R6b/g
884         $CHECKSTAT -a $DIR/R6a/f || error
885         $CHECKSTAT -t file $DIR/R6b/g || error
886 }
887 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
888
889 test_24g() {
890         test_mkdir $DIR/R7a
891         test_mkdir $DIR/R7b
892         test_mkdir $DIR/R7a/d
893         mv $DIR/R7a/d $DIR/R7b/e
894         $CHECKSTAT -a $DIR/R7a/d || error
895         $CHECKSTAT -t dir $DIR/R7b/e || error
896 }
897 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
898
899 test_24h() {
900         test_mkdir -c1 $DIR/R8a
901         test_mkdir -c1 $DIR/R8b
902         test_mkdir -c1 $DIR/R8a/d
903         test_mkdir -c1 $DIR/R8b/e
904         mrename $DIR/R8a/d $DIR/R8b/e
905         $CHECKSTAT -a $DIR/R8a/d || error
906         $CHECKSTAT -t dir $DIR/R8b/e || error
907 }
908 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
909
910 test_24i() {
911         echo "-- rename error cases"
912         test_mkdir $DIR/R9
913         test_mkdir $DIR/R9/a
914         touch $DIR/R9/f
915         mrename $DIR/R9/f $DIR/R9/a
916         $CHECKSTAT -t file $DIR/R9/f || error
917         $CHECKSTAT -t dir  $DIR/R9/a || error
918         $CHECKSTAT -a $DIR/R9/a/f || error
919 }
920 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
921
922 test_24j() {
923         test_mkdir $DIR/R10
924         mrename $DIR/R10/f $DIR/R10/g
925         $CHECKSTAT -t dir $DIR/R10 || error
926         $CHECKSTAT -a $DIR/R10/f || error
927         $CHECKSTAT -a $DIR/R10/g || error
928 }
929 run_test 24j "source does not exist ============================"
930
931 test_24k() {
932         test_mkdir $DIR/R11a
933         test_mkdir $DIR/R11a/d
934         touch $DIR/R11a/f
935         mv $DIR/R11a/f $DIR/R11a/d
936         $CHECKSTAT -a $DIR/R11a/f || error
937         $CHECKSTAT -t file $DIR/R11a/d/f || error
938 }
939 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
940
941 # bug 2429 - rename foo foo foo creates invalid file
942 test_24l() {
943         f="$DIR/f24l"
944         $MULTIOP $f OcNs || error
945 }
946 run_test 24l "Renaming a file to itself ========================"
947
948 test_24m() {
949         f="$DIR/f24m"
950         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
951         # on ext3 this does not remove either the source or target files
952         # though the "expected" operation would be to remove the source
953         $CHECKSTAT -t file ${f} || error "${f} missing"
954         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
955 }
956 run_test 24m "Renaming a file to a hard link to itself ========="
957
958 test_24n() {
959     f="$DIR/f24n"
960     # this stats the old file after it was renamed, so it should fail
961     touch ${f}
962     $CHECKSTAT ${f}
963     mv ${f} ${f}.rename
964     $CHECKSTAT ${f}.rename
965     $CHECKSTAT -a ${f}
966 }
967 run_test 24n "Statting the old file after renaming (Posix rename 2)"
968
969 test_24o() {
970         check_kernel_version 37 || return 0
971         test_mkdir -p $DIR/d24o
972         rename_many -s random -v -n 10 $DIR/d24o
973 }
974 run_test 24o "rename of files during htree split ==============="
975
976 test_24p() {
977         test_mkdir $DIR/R12a
978         test_mkdir $DIR/R12b
979         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
980         mrename $DIR/R12a $DIR/R12b
981         $CHECKSTAT -a $DIR/R12a || error
982         $CHECKSTAT -t dir $DIR/R12b || error
983         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
984         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
985 }
986 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
987
988 cleanup_multiop_pause() {
989         trap 0
990         kill -USR1 $MULTIPID
991 }
992
993 test_24q() {
994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
995         test_mkdir $DIR/R13a
996         test_mkdir $DIR/R13b
997         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
998         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
999         MULTIPID=$!
1000
1001         trap cleanup_multiop_pause EXIT
1002         mrename $DIR/R13a $DIR/R13b
1003         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1004         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1005         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1006         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1007         cleanup_multiop_pause
1008         wait $MULTIPID || error "multiop close failed"
1009 }
1010 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1011
1012 test_24r() { #bug 3789
1013         test_mkdir $DIR/R14a
1014         test_mkdir $DIR/R14a/b
1015         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1016         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1017         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1018 }
1019 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1020
1021 test_24s() {
1022         test_mkdir $DIR/R15a
1023         test_mkdir $DIR/R15a/b
1024         test_mkdir $DIR/R15a/b/c
1025         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1026         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1027         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1028 }
1029 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1030 test_24t() {
1031         test_mkdir $DIR/R16a
1032         test_mkdir $DIR/R16a/b
1033         test_mkdir $DIR/R16a/b/c
1034         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1035         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1036         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1037 }
1038 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1039
1040 test_24u() { # bug12192
1041         rm -rf $DIR/$tfile
1042         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1043         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1044 }
1045 run_test 24u "create stripe file"
1046
1047 page_size() {
1048         getconf PAGE_SIZE
1049 }
1050
1051 simple_cleanup_common() {
1052         trap 0
1053         rm -rf $DIR/$tdir
1054         wait_delete_completed
1055 }
1056
1057 max_pages_per_rpc() {
1058         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1059 }
1060
1061 test_24v() {
1062         local NRFILES=100000
1063         local FREE_INODES=$(mdt_free_inodes 0)
1064         [[ $FREE_INODES -lt $NRFILES ]] &&
1065                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1066                 return
1067
1068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1069         trap simple_cleanup_common EXIT
1070
1071         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1072         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1073
1074         mkdir -p $DIR/$tdir
1075         createmany -m $DIR/$tdir/$tfile $NRFILES
1076
1077         cancel_lru_locks mdc
1078         lctl set_param mdc.*.stats clear
1079
1080         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1081
1082         # LU-5 large readdir
1083         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1084         #               8 bytes for name(filename is mostly 5 in this test) +
1085         #               8 bytes for luda_type
1086         # take into account of overhead in lu_dirpage header and end mark in
1087         # each page, plus one in RPC_NUM calculation.
1088         DIRENT_SIZE=48
1089         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1090         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1091         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1092                                 awk '/^mds_readpage/ {print $2}')
1093         [[ $mds_readpage -gt $RPC_NUM ]] &&
1094                 error "large readdir doesn't take effect"
1095
1096         simple_cleanup_common
1097 }
1098 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1099
1100 test_24w() { # bug21506
1101         SZ1=234852
1102         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1103         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1104         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1105         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1106         [ "$SZ1" = "$SZ2" ] || \
1107                 error "Error reading at the end of the file $tfile"
1108 }
1109 run_test 24w "Reading a file larger than 4Gb"
1110
1111 test_24x() {
1112         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1113         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1114         local MDTIDX=1
1115         local remote_dir=$DIR/$tdir/remote_dir
1116
1117         mkdir -p $DIR/$tdir
1118         $LFS mkdir -i $MDTIDX $remote_dir ||
1119                 error "create remote directory failed"
1120
1121         mkdir -p $DIR/$tdir/src_dir
1122         touch $DIR/$tdir/src_file
1123         mkdir -p $remote_dir/tgt_dir
1124         touch $remote_dir/tgt_file
1125
1126         mrename $remote_dir $DIR/ &&
1127                 error "rename dir cross MDT works!"
1128
1129         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1130                 error "rename dir cross MDT works!"
1131
1132         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1133                 error "rename file cross MDT works!"
1134
1135         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1136                 error "ln file cross MDT should not work!"
1137
1138         rm -rf $DIR/$tdir || error "Can not delete directories"
1139 }
1140 run_test 24x "cross rename/link should be failed"
1141
1142 test_24y() {
1143         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1145         local MDTIDX=1
1146         local remote_dir=$DIR/$tdir/remote_dir
1147
1148         mkdir -p $DIR/$tdir
1149         $LFS mkdir -i $MDTIDX $remote_dir ||
1150                    error "create remote directory failed"
1151
1152         mkdir -p $remote_dir/src_dir
1153         touch $remote_dir/src_file
1154         mkdir -p $remote_dir/tgt_dir
1155         touch $remote_dir/tgt_file
1156
1157         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1158                 error "rename subdir in the same remote dir failed!"
1159
1160         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1161                 error "rename files in the same remote dir failed!"
1162
1163         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1164                 error "link files in the same remote dir failed!"
1165
1166         rm -rf $DIR/$tdir || error "Can not delete directories"
1167 }
1168 run_test 24y "rename/link on the same dir should succeed"
1169
1170 test_24z() {
1171         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1173         local MDTIDX=1
1174         local remote_src=$DIR/$tdir/remote_dir
1175         local remote_tgt=$DIR/$tdir/remote_tgt
1176
1177         mkdir -p $DIR/$tdir
1178         $LFS mkdir -i $MDTIDX $remote_src ||
1179                    error "create remote directory failed"
1180
1181         $LFS mkdir -i $MDTIDX $remote_tgt ||
1182                    error "create remote directory failed"
1183
1184         mrename $remote_src $remote_tgt &&
1185                 error "rename remote dirs should not work!"
1186
1187         # If target dir does not exists, it should succeed
1188         rm -rf $remote_tgt
1189         mrename $remote_src $remote_tgt ||
1190                 error "rename remote dirs(tgt dir does not exists) failed!"
1191
1192         rm -rf $DIR/$tdir || error "Can not delete directories"
1193 }
1194 run_test 24z "rename one remote dir to another remote dir should fail"
1195
1196 test_24A() { # LU-3182
1197         local NFILES=5000
1198
1199         rm -rf $DIR/$tdir
1200         mkdir -p $DIR/$tdir
1201         createmany -m $DIR/$tdir/$tfile $NFILES
1202         local t=`ls $DIR/$tdir | wc -l`
1203         local u=`ls $DIR/$tdir | sort -u | wc -l`
1204         if [ $t -ne $NFILES -o $u -ne $NFILES ] ; then
1205                 error "Expected $NFILES files, got $t ($u unique)"
1206         fi
1207
1208         rm -rf $DIR/$tdir || error "Can not delete directories"
1209 }
1210 run_test 24A "readdir() returns correct number of entries."
1211
1212 test_25a() {
1213         echo '== symlink sanity ============================================='
1214
1215         test_mkdir $DIR/d25
1216         ln -s d25 $DIR/s25
1217         touch $DIR/s25/foo || error
1218 }
1219 run_test 25a "create file in symlinked directory ==============="
1220
1221 test_25b() {
1222         [ ! -d $DIR/d25 ] && test_25a
1223         $CHECKSTAT -t file $DIR/s25/foo || error
1224 }
1225 run_test 25b "lookup file in symlinked directory ==============="
1226
1227 test_26a() {
1228         test_mkdir $DIR/d26
1229         test_mkdir $DIR/d26/d26-2
1230         ln -s d26/d26-2 $DIR/s26
1231         touch $DIR/s26/foo || error
1232 }
1233 run_test 26a "multiple component symlink ======================="
1234
1235 test_26b() {
1236         test_mkdir -p $DIR/d26b/d26-2
1237         ln -s d26b/d26-2/foo $DIR/s26-2
1238         touch $DIR/s26-2 || error
1239 }
1240 run_test 26b "multiple component symlink at end of lookup ======"
1241
1242 test_26c() {
1243         test_mkdir $DIR/d26.2
1244         touch $DIR/d26.2/foo
1245         ln -s d26.2 $DIR/s26.2-1
1246         ln -s s26.2-1 $DIR/s26.2-2
1247         ln -s s26.2-2 $DIR/s26.2-3
1248         chmod 0666 $DIR/s26.2-3/foo
1249 }
1250 run_test 26c "chain of symlinks ================================"
1251
1252 # recursive symlinks (bug 439)
1253 test_26d() {
1254         ln -s d26-3/foo $DIR/d26-3
1255 }
1256 run_test 26d "create multiple component recursive symlink ======"
1257
1258 test_26e() {
1259         [ ! -h $DIR/d26-3 ] && test_26d
1260         rm $DIR/d26-3
1261 }
1262 run_test 26e "unlink multiple component recursive symlink ======"
1263
1264 # recursive symlinks (bug 7022)
1265 test_26f() {
1266         test_mkdir -p $DIR/$tdir
1267         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1268         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1269         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1270         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1271         cd $tfile                || error "cd $tfile failed"
1272         ln -s .. dotdot          || error "ln dotdot failed"
1273         ln -s dotdot/lndir lndir || error "ln lndir failed"
1274         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1275         output=`ls $tfile/$tfile/lndir/bar1`
1276         [ "$output" = bar1 ] && error "unexpected output"
1277         rm -r $tfile             || error "rm $tfile failed"
1278         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1279 }
1280 run_test 26f "rm -r of a directory which has recursive symlink ="
1281
1282 test_27a() {
1283         echo '== stripe sanity =============================================='
1284         test_mkdir -p $DIR/d27 || error "mkdir failed"
1285         $GETSTRIPE $DIR/d27
1286         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1287         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1288         log "== test_27a: write to one stripe file ========================="
1289         cp /etc/hosts $DIR/d27/f0 || error
1290 }
1291 run_test 27a "one stripe file =================================="
1292
1293 test_27b() {
1294         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1295         test_mkdir -p $DIR/d27
1296         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1297         $GETSTRIPE -c $DIR/d27/f01
1298         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1299                 error "two-stripe file doesn't have two stripes"
1300
1301         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1302 }
1303 run_test 27b "create and write to two stripe file"
1304
1305 test_27d() {
1306         test_mkdir -p $DIR/d27
1307         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1308         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1309         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1310 }
1311 run_test 27d "create file with default settings ================"
1312
1313 test_27e() {
1314         test_mkdir -p $DIR/d27
1315         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1316         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1317         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1318 }
1319 run_test 27e "setstripe existing file (should return error) ======"
1320
1321 test_27f() {
1322         test_mkdir -p $DIR/d27
1323         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/d27/fbad && error "setstripe failed"
1324         dd if=/dev/zero of=$DIR/d27/fbad bs=4k count=4 || error "dd failed"
1325         $GETSTRIPE $DIR/d27/fbad || error "$GETSTRIPE failed"
1326 }
1327 run_test 27f "setstripe with bad stripe size (should return error)"
1328
1329 test_27g() {
1330         test_mkdir -p $DIR/d27
1331         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1332         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1333                 error "$DIR/d27/fnone has object"
1334 }
1335 run_test 27g "$GETSTRIPE with no objects"
1336
1337 test_27i() {
1338         touch $DIR/d27/fsome || error "touch failed"
1339         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1340 }
1341 run_test 27i "$GETSTRIPE with some objects"
1342
1343 test_27j() {
1344         test_mkdir -p $DIR/d27
1345         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1346 }
1347 run_test 27j "setstripe with bad stripe offset (should return error)"
1348
1349 test_27k() { # bug 2844
1350         test_mkdir -p $DIR/d27
1351         FILE=$DIR/d27/f27k
1352         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1353         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1354         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1355         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1356         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1357         dd if=/dev/zero of=$FILE bs=4k count=1
1358         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1359         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1360 }
1361 run_test 27k "limit i_blksize for broken user apps ============="
1362
1363 test_27l() {
1364         test_mkdir -p $DIR/d27
1365         mcreate $DIR/f27l || error "creating file"
1366         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1367                 error "setstripe should have failed" || true
1368 }
1369 run_test 27l "check setstripe permissions (should return error)"
1370
1371 test_27m() {
1372         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1373                 return
1374         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1375                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1376                 return
1377         fi
1378         trap simple_cleanup_common EXIT
1379         test_mkdir -p $DIR/$tdir
1380         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1381         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1382                 error "dd should fill OST0"
1383         i=2
1384         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1385                 i=$((i + 1))
1386                 [ $i -gt 256 ] && break
1387         done
1388         i=$((i + 1))
1389         touch $DIR/$tdir/f27m_$i
1390         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1391                 error "OST0 was full but new created file still use it"
1392         i=$((i + 1))
1393         touch $DIR/$tdir/f27m_$i
1394         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1395                 error "OST0 was full but new created file still use it"
1396         simple_cleanup_common
1397 }
1398 run_test 27m "create file while OST0 was full =================="
1399
1400 sleep_maxage() {
1401         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1402         sleep $DELAY
1403 }
1404
1405 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1406 # if the OST isn't full anymore.
1407 reset_enospc() {
1408         local OSTIDX=${1:-""}
1409
1410         local list=$(comma_list $(osts_nodes))
1411         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1412
1413         do_nodes $list lctl set_param fail_loc=0
1414         sync    # initiate all OST_DESTROYs from MDS to OST
1415         sleep_maxage
1416 }
1417
1418 exhaust_precreations() {
1419         local OSTIDX=$1
1420         local FAILLOC=$2
1421         local FAILIDX=${3:-$OSTIDX}
1422
1423         test_mkdir -p $DIR/$tdir
1424         local MDSIDX=$(get_mds_dir "$DIR/$tdir")
1425         echo OSTIDX=$OSTIDX MDSIDX=$MDSIDX
1426
1427         local OST=$(ostname_from_index $OSTIDX)
1428         local MDT_INDEX=$(lfs df | grep "\[MDT:$((MDSIDX - 1))\]" | awk '{print $1}' | \
1429                           sed -e 's/_UUID$//;s/^.*-//')
1430
1431         # on the mdt's osc
1432         local mdtosc_proc1=$(get_mdtosc_proc_path mds${MDSIDX} $OST)
1433         local last_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1434                         osc.$mdtosc_proc1.prealloc_last_id)
1435         local next_id=$(do_facet mds${MDSIDX} lctl get_param -n \
1436                         osc.$mdtosc_proc1.prealloc_next_id)
1437
1438         local mdtosc_proc2=$(get_mdtosc_proc_path mds${MDSIDX})
1439         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1440
1441         test_mkdir -p $DIR/$tdir/${OST}
1442         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1443 #define OBD_FAIL_OST_ENOSPC              0x215
1444         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=$FAILIDX
1445         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x215
1446         echo "Creating to objid $last_id on ost $OST..."
1447         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1448         do_facet mds${MDSIDX} lctl get_param osc.$mdtosc_proc2.prealloc*
1449         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=$FAILLOC
1450         sleep_maxage
1451 }
1452
1453 exhaust_all_precreations() {
1454         local i
1455         for (( i=0; i < OSTCOUNT; i++ )) ; do
1456                 exhaust_precreations $i $1 -1
1457         done
1458 }
1459
1460 test_27n() {
1461         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1463         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1464         remote_ost_nodsh && skip "remote OST with nodsh" && return
1465
1466         reset_enospc
1467         rm -f $DIR/$tdir/$tfile
1468         exhaust_precreations 0 0x80000215
1469         $SETSTRIPE -c -1 $DIR/$tdir
1470         touch $DIR/$tdir/$tfile || error
1471         $GETSTRIPE $DIR/$tdir/$tfile
1472         reset_enospc
1473 }
1474 run_test 27n "create file with some full OSTs =================="
1475
1476 test_27o() {
1477         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1478         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1479         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1480         remote_ost_nodsh && skip "remote OST with nodsh" && return
1481
1482         reset_enospc
1483         rm -f $DIR/$tdir/$tfile
1484         exhaust_all_precreations 0x215
1485
1486         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1487
1488         reset_enospc
1489         rm -rf $DIR/$tdir/*
1490 }
1491 run_test 27o "create file with all full OSTs (should error) ===="
1492
1493 test_27p() {
1494         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1496         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1497         remote_ost_nodsh && skip "remote OST with nodsh" && return
1498
1499         reset_enospc
1500         rm -f $DIR/$tdir/$tfile
1501         test_mkdir -p $DIR/$tdir
1502
1503         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1504         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1505         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1506
1507         exhaust_precreations 0 0x80000215
1508         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1509         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1510         $GETSTRIPE $DIR/$tdir/$tfile
1511
1512         reset_enospc
1513 }
1514 run_test 27p "append to a truncated file with some full OSTs ==="
1515
1516 test_27q() {
1517         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1518         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1519         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1520         remote_ost_nodsh && skip "remote OST with nodsh" && return
1521
1522         reset_enospc
1523         rm -f $DIR/$tdir/$tfile
1524
1525         test_mkdir -p $DIR/$tdir
1526         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1527         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1528         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1529
1530         exhaust_all_precreations 0x215
1531
1532         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1533         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1534
1535         reset_enospc
1536 }
1537 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1538
1539 test_27r() {
1540         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1541         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1542         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1543         remote_ost_nodsh && skip "remote OST with nodsh" && return
1544
1545         reset_enospc
1546         rm -f $DIR/$tdir/$tfile
1547         exhaust_precreations 0 0x80000215
1548
1549         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1550
1551         reset_enospc
1552 }
1553 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1554
1555 test_27s() { # bug 10725
1556         test_mkdir -p $DIR/$tdir
1557         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1558         local stripe_count=0
1559         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1560         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1561                 error "stripe width >= 2^32 succeeded" || true
1562
1563 }
1564 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1565
1566 test_27t() { # bug 10864
1567         WDIR=$(pwd)
1568         WLFS=$(which lfs)
1569         cd $DIR
1570         touch $tfile
1571         $WLFS getstripe $tfile
1572         cd $WDIR
1573 }
1574 run_test 27t "check that utils parse path correctly"
1575
1576 test_27u() { # bug 4900
1577         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1578         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1579         local index
1580         local list=$(comma_list $(mdts_nodes))
1581
1582 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1583         do_nodes $list $LCTL set_param fail_loc=0x139
1584         test_mkdir -p $DIR/$tdir
1585         rm -rf $DIR/$tdir/*
1586         createmany -o $DIR/$tdir/t- 1000
1587         do_nodes $list $LCTL set_param fail_loc=0
1588
1589         TLOG=$DIR/$tfile.getstripe
1590         $GETSTRIPE $DIR/$tdir > $TLOG
1591         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1592         unlinkmany $DIR/$tdir/t- 1000
1593         [[ $OBJS -gt 0 ]] &&
1594                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1595 }
1596 run_test 27u "skip object creation on OSC w/o objects =========="
1597
1598 test_27v() { # bug 4900
1599         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1601         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1602         remote_ost_nodsh && skip "remote OST with nodsh" && return
1603
1604         exhaust_all_precreations 0x215
1605         reset_enospc
1606
1607         test_mkdir -p $DIR/$tdir
1608         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1609
1610         touch $DIR/$tdir/$tfile
1611         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1612         # all except ost1
1613         for (( i=1; i < OSTCOUNT; i++ )); do
1614                 do_facet ost$i lctl set_param fail_loc=0x705
1615         done
1616         local START=`date +%s`
1617         createmany -o $DIR/$tdir/$tfile 32
1618
1619         local FINISH=`date +%s`
1620         local TIMEOUT=`lctl get_param -n timeout`
1621         local PROCESS=$((FINISH - START))
1622         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1623                error "$FINISH - $START >= $TIMEOUT / 2"
1624         sleep $((TIMEOUT / 2 - PROCESS))
1625         reset_enospc
1626 }
1627 run_test 27v "skip object creation on slow OST ================="
1628
1629 test_27w() { # bug 10997
1630         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1631         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1632         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1633                 error "stripe size $size != 65536" || true
1634         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1635                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1636 }
1637 run_test 27w "check $SETSTRIPE -S option"
1638
1639 test_27wa() {
1640         [[ $OSTCOUNT -lt 2 ]] &&
1641                 skip_env "skipping multiple stripe count/offset test" && return
1642
1643         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1644         for i in $(seq 1 $OSTCOUNT); do
1645                 offset=$((i - 1))
1646                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1647                         error "setstripe -c $i -i $offset failed"
1648                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1649                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1650                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1651                 [ $index -ne $offset ] &&
1652                         error "stripe offset $index != $offset" || true
1653         done
1654 }
1655 run_test 27wa "check $SETSTRIPE -c -i options"
1656
1657 test_27x() {
1658         remote_ost_nodsh && skip "remote OST with nodsh" && return
1659         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1660         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1661         OFFSET=$(($OSTCOUNT - 1))
1662         OSTIDX=0
1663         local OST=$(ostname_from_index $OSTIDX)
1664
1665         test_mkdir -p $DIR/$tdir
1666         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1667         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1668         sleep_maxage
1669         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1670         for i in `seq 0 $OFFSET`; do
1671                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1672                 error "OST0 was degraded but new created file still use it"
1673         done
1674         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1675 }
1676 run_test 27x "create files while OST0 is degraded"
1677
1678 test_27y() {
1679         [[ $OSTCOUNT -lt 2 ]] &&
1680                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1681         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1682         remote_ost_nodsh && skip "remote OST with nodsh" && return
1683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1684
1685         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1686         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1687             osc.$mdtosc.prealloc_last_id)
1688         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1689             osc.$mdtosc.prealloc_next_id)
1690         local fcount=$((last_id - next_id))
1691         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1692         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1693
1694         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1695                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1696         local OST_DEACTIVE_IDX=-1
1697         local OSC
1698         local OSTIDX
1699         local OST
1700
1701         for OSC in $MDS_OSCS; do
1702                 OST=$(osc_to_ost $OSC)
1703                 OSTIDX=$(index_from_ostuuid $OST)
1704                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1705                         OST_DEACTIVE_IDX=$OSTIDX
1706                 fi
1707                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1708                         echo $OSC "is Deactivated:"
1709                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1710                 fi
1711         done
1712
1713         OSTIDX=$(index_from_ostuuid $OST)
1714         mkdir -p $DIR/$tdir
1715         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1716
1717         for OSC in $MDS_OSCS; do
1718                 OST=$(osc_to_ost $OSC)
1719                 OSTIDX=$(index_from_ostuuid $OST)
1720                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1721                         echo $OST "is degraded:"
1722                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1723                                                 obdfilter.$OST.degraded=1
1724                 fi
1725         done
1726
1727         sleep_maxage
1728         createmany -o $DIR/$tdir/$tfile $fcount
1729
1730         for OSC in $MDS_OSCS; do
1731                 OST=$(osc_to_ost $OSC)
1732                 OSTIDX=$(index_from_ostuuid $OST)
1733                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1734                         echo $OST "is recovered from degraded:"
1735                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1736                                                 obdfilter.$OST.degraded=0
1737                 else
1738                         do_facet $SINGLEMDS lctl --device %$OSC activate
1739                 fi
1740         done
1741
1742         # all osp devices get activated, hence -1 stripe count restored
1743         local stripecnt=0
1744
1745         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1746         # devices get activated.
1747         sleep_maxage
1748         $SETSTRIPE -c -1 $DIR/$tfile
1749         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1750         rm -f $DIR/$tfile
1751         [ $stripecnt -ne $OSTCOUNT ] &&
1752                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1753         return 0
1754 }
1755 run_test 27y "create files while OST0 is degraded and the rest inactive"
1756
1757 check_seq_oid()
1758 {
1759         log "check file $1"
1760
1761         lmm_count=$($GETSTRIPE -c $1)
1762         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1763         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1764
1765         local old_ifs="$IFS"
1766         IFS=$'[:]'
1767         fid=($($LFS path2fid $1))
1768         IFS="$old_ifs"
1769
1770         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1771         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1772
1773         # compare lmm_seq and lu_fid->f_seq
1774         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1775         # compare lmm_object_id and lu_fid->oid
1776         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1777
1778         # check the trusted.fid attribute of the OST objects of the file
1779         local have_obdidx=false
1780         local stripe_nr=0
1781         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1782                 # skip lines up to and including "obdidx"
1783                 [ -z "$obdidx" ] && break
1784                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1785                 $have_obdidx || continue
1786
1787                 local ost=$((obdidx + 1))
1788                 local dev=$(ostdevname $ost)
1789                 local oid_hex
1790
1791                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1792
1793                 seq=$(echo $seq | sed -e "s/^0x//g")
1794                 if [ $seq == 0 ]; then
1795                         oid_hex=$(echo $oid)
1796                 else
1797                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1798                 fi
1799                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1800
1801                 local ff
1802                 #
1803                 # Don't unmount/remount the OSTs if we don't need to do that.
1804                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1805                 # update too, until that use mount/ll_decode_filter_fid/mount.
1806                 # Re-enable when debugfs will understand new filter_fid.
1807                 #
1808                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1809                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1810                                 $dev 2>/dev/null" | grep "parent=")
1811                 else
1812                         stop ost$ost
1813                         mount_fstype ost$ost
1814                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1815                                 $(facet_mntpt ost$ost)/$obj_file)
1816                         unmount_fstype ost$ost
1817                         start ost$ost $dev $OST_MOUNT_OPTS
1818                 fi
1819
1820                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1821
1822                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1823
1824                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1825                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1826                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1827                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1828                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1829                 local ff_pstripe
1830                 if echo $ff_parent | grep -q 'stripe='; then
1831                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1832                 else
1833                         #
1834                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1835                         # into f_ver in this case.  See the comment on
1836                         # ff_parent.
1837                         #
1838                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1839                                 sed -e 's/\]//')
1840                 fi
1841
1842                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1843                 [ $ff_pseq = $lmm_seq ] ||
1844                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1845                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1846                 [ $ff_poid = $lmm_oid ] ||
1847                         error "FF parent OID $ff_poid != $lmm_oid"
1848                 (($ff_pstripe == $stripe_nr)) ||
1849                         error "FF stripe $ff_pstripe != $stripe_nr"
1850
1851                 stripe_nr=$((stripe_nr + 1))
1852         done
1853 }
1854
1855 test_27z() {
1856         remote_ost_nodsh && skip "remote OST with nodsh" && return
1857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1858         test_mkdir -p $DIR/$tdir
1859
1860         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1861                 { error "setstripe -c -1 failed"; return 1; }
1862         # We need to send a write to every object to get parent FID info set.
1863         # This _should_ also work for setattr, but does not currently.
1864         # touch $DIR/$tdir/$tfile-1 ||
1865         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1866                 { error "dd $tfile-1 failed"; return 2; }
1867         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1868                 { error "setstripe -c -1 failed"; return 3; }
1869         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1870                 { error "dd $tfile-2 failed"; return 4; }
1871
1872         # make sure write RPCs have been sent to OSTs
1873         sync; sleep 5; sync
1874
1875         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1876         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1877 }
1878 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1879
1880 test_27A() { # b=19102
1881         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1882         local restore_size=$($GETSTRIPE -S $MOUNT)
1883         local restore_count=$($GETSTRIPE -c $MOUNT)
1884         local restore_offset=$($GETSTRIPE -i $MOUNT)
1885         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1886         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1887                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1888         local default_size=$($GETSTRIPE -S $MOUNT)
1889         local default_offset=$($GETSTRIPE -i $MOUNT)
1890         local dsize=$((1024 * 1024))
1891         [ $default_size -eq $dsize ] ||
1892                 error "stripe size $default_size != $dsize"
1893         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1894         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1895 }
1896 run_test 27A "check filesystem-wide default LOV EA values"
1897
1898 test_27B() { # LU-2523
1899         test_mkdir -p $DIR/$tdir
1900         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1901         touch $DIR/$tdir/f0
1902         # open f1 with O_LOV_DELAY_CREATE
1903         # rename f0 onto f1
1904         # call setstripe ioctl on open file descriptor for f1
1905         # close
1906         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1907                 $DIR/$tdir/f0
1908
1909         rm -f $DIR/$tdir/f1
1910         # open f1 with O_LOV_DELAY_CREATE
1911         # unlink f1
1912         # call setstripe ioctl on open file descriptor for f1
1913         # close
1914         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1915
1916         # Allow multiop to fail in imitation of NFS's busted semantics.
1917         true
1918 }
1919 run_test 27B "call setstripe on open unlinked file/rename victim"
1920
1921 test_27C() { #LU-2871
1922         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1923
1924         declare -a ost_idx
1925         local index
1926         local found
1927         local i
1928         local j
1929
1930         test_mkdir -p $DIR/$tdir
1931         cd $DIR/$tdir
1932         for i in $(seq 0 $((OSTCOUNT - 1))); do
1933                 # set stripe across all OSTs starting from OST$i
1934                 $SETSTRIPE -i $i -c -1 $tfile$i
1935                 # get striping information
1936                 ost_idx=($($GETSTRIPE $tfile$i |
1937                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1938                 echo ${ost_idx[@]}
1939
1940                 # check the layout
1941                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
1942                         error "${#ost_idx[@]} != $OSTCOUNT"
1943
1944                 for index in $(seq 0 $((OSTCOUNT - 1))); do
1945                         found=0
1946                         for j in $(echo ${ost_idx[@]}); do
1947                                 if [ $index -eq $j ]; then
1948                                         found=1
1949                                         break
1950                                 fi
1951                         done
1952                         [ $found = 1 ] ||
1953                                 error "Can not find $index in ${ost_idx[@]}"
1954                 done
1955         done
1956 }
1957 run_test 27C "check full striping across all OSTs"
1958
1959 # createtest also checks that device nodes are created and
1960 # then visible correctly (#2091)
1961 test_28() { # bug 2091
1962         test_mkdir $DIR/d28
1963         $CREATETEST $DIR/d28/ct || error
1964 }
1965 run_test 28 "create/mknod/mkdir with bad file types ============"
1966
1967 test_29() {
1968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1969         cancel_lru_locks mdc
1970         test_mkdir $DIR/d29
1971         touch $DIR/d29/foo
1972         log 'first d29'
1973         ls -l $DIR/d29
1974
1975         declare -i LOCKCOUNTORIG=0
1976         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1977                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
1978         done
1979         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
1980
1981         declare -i LOCKUNUSEDCOUNTORIG=0
1982         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1983                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
1984         done
1985
1986         log 'second d29'
1987         ls -l $DIR/d29
1988         log 'done'
1989
1990         declare -i LOCKCOUNTCURRENT=0
1991         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
1992                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
1993         done
1994
1995         declare -i LOCKUNUSEDCOUNTCURRENT=0
1996         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
1997                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
1998         done
1999
2000         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2001                 $LCTL set_param -n ldlm.dump_namespaces ""
2002                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2003                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2004                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2005                 return 2
2006         fi
2007         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2008                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2009                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2010                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2011                 return 3
2012         fi
2013 }
2014 run_test 29 "IT_GETATTR regression  ============================"
2015
2016 test_30a() { # was test_30
2017         cp $(which ls) $DIR || cp /bin/ls $DIR
2018         $DIR/ls / || error
2019         rm $DIR/ls
2020 }
2021 run_test 30a "execute binary from Lustre (execve) =============="
2022
2023 test_30b() {
2024         cp `which ls` $DIR || cp /bin/ls $DIR
2025         chmod go+rx $DIR/ls
2026         $RUNAS $DIR/ls / || error
2027         rm $DIR/ls
2028 }
2029 run_test 30b "execute binary from Lustre as non-root ==========="
2030
2031 test_30c() { # b=22376
2032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2033         cp `which ls` $DIR || cp /bin/ls $DIR
2034         chmod a-rw $DIR/ls
2035         cancel_lru_locks mdc
2036         cancel_lru_locks osc
2037         $RUNAS $DIR/ls / || error
2038         rm -f $DIR/ls
2039 }
2040 run_test 30c "execute binary from Lustre without read perms ===="
2041
2042 test_31a() {
2043         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2044         $CHECKSTAT -a $DIR/f31 || error
2045 }
2046 run_test 31a "open-unlink file =================================="
2047
2048 test_31b() {
2049         touch $DIR/f31 || error
2050         ln $DIR/f31 $DIR/f31b || error
2051         $MULTIOP $DIR/f31b Ouc || error
2052         $CHECKSTAT -t file $DIR/f31 || error
2053 }
2054 run_test 31b "unlink file with multiple links while open ======="
2055
2056 test_31c() {
2057         touch $DIR/f31 || error
2058         ln $DIR/f31 $DIR/f31c || error
2059         multiop_bg_pause $DIR/f31 O_uc || return 1
2060         MULTIPID=$!
2061         $MULTIOP $DIR/f31c Ouc
2062         kill -USR1 $MULTIPID
2063         wait $MULTIPID
2064 }
2065 run_test 31c "open-unlink file with multiple links ============="
2066
2067 test_31d() {
2068         opendirunlink $DIR/d31d $DIR/d31d || error
2069         $CHECKSTAT -a $DIR/d31d || error
2070 }
2071 run_test 31d "remove of open directory ========================="
2072
2073 test_31e() { # bug 2904
2074         check_kernel_version 34 || return 0
2075         openfilleddirunlink $DIR/d31e || error
2076 }
2077 run_test 31e "remove of open non-empty directory ==============="
2078
2079 test_31f() { # bug 4554
2080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2081         set -vx
2082         test_mkdir $DIR/d31f
2083         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2084         cp /etc/hosts $DIR/d31f
2085         ls -l $DIR/d31f
2086         $GETSTRIPE $DIR/d31f/hosts
2087         multiop_bg_pause $DIR/d31f D_c || return 1
2088         MULTIPID=$!
2089
2090         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2091         test_mkdir $DIR/d31f
2092         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2093         cp /etc/hosts $DIR/d31f
2094         ls -l $DIR/d31f
2095         $GETSTRIPE $DIR/d31f/hosts
2096         multiop_bg_pause $DIR/d31f D_c || return 1
2097         MULTIPID2=$!
2098
2099         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2100         wait $MULTIPID || error "first opendir $MULTIPID failed"
2101
2102         sleep 6
2103
2104         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2105         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2106         set +vx
2107 }
2108 run_test 31f "remove of open directory with open-unlink file ==="
2109
2110 test_31g() {
2111         echo "-- cross directory link --"
2112         test_mkdir -c1 $DIR/${tdir}ga
2113         test_mkdir -c1 $DIR/${tdir}gb
2114         touch $DIR/${tdir}ga/f
2115         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2116         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2117         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2118         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2119         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2120 }
2121 run_test 31g "cross directory link==============="
2122
2123 test_31h() {
2124         echo "-- cross directory link --"
2125         test_mkdir -c1 $DIR/${tdir}
2126         test_mkdir -c1 $DIR/${tdir}/dir
2127         touch $DIR/${tdir}/f
2128         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2129         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2130         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2131         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2132         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2133 }
2134 run_test 31h "cross directory link under child==============="
2135
2136 test_31i() {
2137         echo "-- cross directory link --"
2138         test_mkdir -c1 $DIR/$tdir
2139         test_mkdir -c1 $DIR/$tdir/dir
2140         touch $DIR/$tdir/dir/f
2141         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2142         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2143         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2144         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2145         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2146 }
2147 run_test 31i "cross directory link under parent==============="
2148
2149 test_31j() {
2150         test_mkdir -c1 -p $DIR/$tdir
2151         test_mkdir -c1 -p $DIR/$tdir/dir1
2152         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2153         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2154         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2155         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2156         return 0
2157 }
2158 run_test 31j "link for directory==============="
2159
2160 test_31k() {
2161         test_mkdir -c1 -p $DIR/$tdir
2162         touch $DIR/$tdir/s
2163         touch $DIR/$tdir/exist
2164         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2165         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2166         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2167         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2168         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2169         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2170         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2171         return 0
2172 }
2173 run_test 31k "link to file: the same, non-existing, dir==============="
2174
2175 test_31m() {
2176         mkdir $DIR/d31m
2177         touch $DIR/d31m/s
2178         mkdir $DIR/d31m2
2179         touch $DIR/d31m2/exist
2180         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2181         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2182         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2183         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2184         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2185         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2186         return 0
2187 }
2188 run_test 31m "link to file: the same, non-existing, dir==============="
2189
2190 test_31n() {
2191         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2192         nlink=$(stat --format=%h $DIR/$tfile)
2193         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2194         local fd=$(free_fd)
2195         local cmd="exec $fd<$DIR/$tfile"
2196         eval $cmd
2197         cmd="exec $fd<&-"
2198         trap "eval $cmd" EXIT
2199         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2200         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2201         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2202         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2203         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2204         eval $cmd
2205 }
2206 run_test 31n "check link count of unlinked file"
2207
2208 link_one() {
2209         local TEMPNAME=$(mktemp $1_XXXXXX)
2210         mlink $TEMPNAME $1 2> /dev/null &&
2211                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2212         munlink $TEMPNAME
2213 }
2214
2215 test_31o() { # LU-2901
2216         mkdir -p $DIR/$tdir
2217         for LOOP in $(seq 100); do
2218                 rm -f $DIR/$tdir/$tfile*
2219                 for THREAD in $(seq 8); do
2220                         link_one $DIR/$tdir/$tfile.$LOOP &
2221                 done
2222                 wait
2223                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2224                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2225                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2226                         break || true
2227         done
2228 }
2229 run_test 31o "duplicate hard links with same filename"
2230
2231 test_31p() {
2232         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2233
2234         mkdir $DIR/$tdir
2235         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2236         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2237
2238         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2239                 error "open unlink test1 failed"
2240         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2241                 error "open unlink test2 failed"
2242
2243         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2244                 error "test1 still exists"
2245         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2246                 error "test2 still exists"
2247 }
2248 run_test 31p "remove of open striped directory"
2249
2250 cleanup_test32_mount() {
2251         trap 0
2252         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2253 }
2254
2255 test_32a() {
2256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2257         echo "== more mountpoints and symlinks ================="
2258         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2259         trap cleanup_test32_mount EXIT
2260         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2261         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2262         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2263         cleanup_test32_mount
2264 }
2265 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2266
2267 test_32b() {
2268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2269         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2270         trap cleanup_test32_mount EXIT
2271         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2272         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2273         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2274         cleanup_test32_mount
2275 }
2276 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2277
2278 test_32c() {
2279         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2280         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2281         trap cleanup_test32_mount EXIT
2282         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2283         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2284         test_mkdir -p $DIR/$tdir/d2/test_dir
2285         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2286         cleanup_test32_mount
2287 }
2288 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2289
2290 test_32d() {
2291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2292         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2293         trap cleanup_test32_mount EXIT
2294         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2295         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2296         test_mkdir -p $DIR/$tdir/d2/test_dir
2297         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2298         cleanup_test32_mount
2299 }
2300 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2301
2302 test_32e() {
2303         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2304         test_mkdir -p $DIR/d32e/tmp
2305         TMP_DIR=$DIR/d32e/tmp
2306         ln -s $DIR/d32e $TMP_DIR/symlink11
2307         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2308         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2309         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2310 }
2311 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2312
2313 test_32f() {
2314         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2315         test_mkdir -p $DIR/d32f/tmp
2316         TMP_DIR=$DIR/d32f/tmp
2317         ln -s $DIR/d32f $TMP_DIR/symlink11
2318         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2319         ls $DIR/d32f/tmp/symlink11  || error
2320         ls $DIR/d32f/symlink01 || error
2321 }
2322 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2323
2324 test_32g() {
2325         TMP_DIR=$DIR/$tdir/tmp
2326         test_mkdir -p $DIR/$tdir/tmp
2327         test_mkdir $DIR/${tdir}2
2328         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2329         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2330         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2331         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2332         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2333         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2334 }
2335 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2336
2337 test_32h() {
2338         rm -fr $DIR/$tdir $DIR/${tdir}2
2339         TMP_DIR=$DIR/$tdir/tmp
2340         test_mkdir -p $DIR/$tdir/tmp
2341         test_mkdir $DIR/${tdir}2
2342         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2343         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2344         ls $TMP_DIR/symlink12 || error
2345         ls $DIR/$tdir/symlink02  || error
2346 }
2347 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2348
2349 test_32i() {
2350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2351         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2352         trap cleanup_test32_mount EXIT
2353         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2354         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2355         touch $DIR/$tdir/test_file
2356         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2357         cleanup_test32_mount
2358 }
2359 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2360
2361 test_32j() {
2362         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2363         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2364         trap cleanup_test32_mount EXIT
2365         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2366         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2367         touch $DIR/$tdir/test_file
2368         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2369         cleanup_test32_mount
2370 }
2371 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2372
2373 test_32k() {
2374         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2375         rm -fr $DIR/$tdir
2376         trap cleanup_test32_mount EXIT
2377         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2378         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2379         test_mkdir -p $DIR/$tdir/d2
2380         touch $DIR/$tdir/d2/test_file || error
2381         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2382         cleanup_test32_mount
2383 }
2384 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2385
2386 test_32l() {
2387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2388         rm -fr $DIR/$tdir
2389         trap cleanup_test32_mount EXIT
2390         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2391         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2392         test_mkdir -p $DIR/$tdir/d2
2393         touch $DIR/$tdir/d2/test_file
2394         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2395         cleanup_test32_mount
2396 }
2397 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2398
2399 test_32m() {
2400         rm -fr $DIR/d32m
2401         test_mkdir -p $DIR/d32m/tmp
2402         TMP_DIR=$DIR/d32m/tmp
2403         ln -s $DIR $TMP_DIR/symlink11
2404         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2405         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2406         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2407 }
2408 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2409
2410 test_32n() {
2411         rm -fr $DIR/d32n
2412         test_mkdir -p $DIR/d32n/tmp
2413         TMP_DIR=$DIR/d32n/tmp
2414         ln -s $DIR $TMP_DIR/symlink11
2415         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2416         ls -l $DIR/d32n/tmp/symlink11  || error
2417         ls -l $DIR/d32n/symlink01 || error
2418 }
2419 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2420
2421 test_32o() {
2422         rm -fr $DIR/d32o $DIR/$tfile
2423         touch $DIR/$tfile
2424         test_mkdir -p $DIR/d32o/tmp
2425         TMP_DIR=$DIR/d32o/tmp
2426         ln -s $DIR/$tfile $TMP_DIR/symlink12
2427         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2428         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2429         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2430         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2431         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2432 }
2433 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2434
2435 test_32p() {
2436     log 32p_1
2437         rm -fr $DIR/d32p
2438     log 32p_2
2439         rm -f $DIR/$tfile
2440     log 32p_3
2441         touch $DIR/$tfile
2442     log 32p_4
2443         test_mkdir -p $DIR/d32p/tmp
2444     log 32p_5
2445         TMP_DIR=$DIR/d32p/tmp
2446     log 32p_6
2447         ln -s $DIR/$tfile $TMP_DIR/symlink12
2448     log 32p_7
2449         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2450     log 32p_8
2451         cat $DIR/d32p/tmp/symlink12 || error
2452     log 32p_9
2453         cat $DIR/d32p/symlink02 || error
2454     log 32p_10
2455 }
2456 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2457
2458 cleanup_testdir_mount() {
2459         trap 0
2460         $UMOUNT -d $DIR/$tdir
2461 }
2462
2463 test_32q() {
2464         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2465         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2466         trap cleanup_testdir_mount EXIT
2467         test_mkdir -p $DIR/$tdir
2468         touch $DIR/$tdir/under_the_mount
2469         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2470         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2471         cleanup_testdir_mount
2472 }
2473 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2474
2475 test_32r() {
2476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2477         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2478         trap cleanup_testdir_mount EXIT
2479         test_mkdir -p $DIR/$tdir
2480         touch $DIR/$tdir/under_the_mount
2481         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2482         ls $DIR/$tdir | grep -q under_the_mount && error || true
2483         cleanup_testdir_mount
2484 }
2485 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2486
2487 test_33aa() {
2488         rm -f $DIR/$tfile
2489         touch $DIR/$tfile
2490         chmod 444 $DIR/$tfile
2491         chown $RUNAS_ID $DIR/$tfile
2492         log 33_1
2493         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2494         log 33_2
2495 }
2496 run_test 33aa "write file with mode 444 (should return error) ===="
2497
2498 test_33a() {
2499         rm -fr $DIR/d33
2500         test_mkdir -p $DIR/d33
2501         chown $RUNAS_ID $DIR/d33
2502         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2503         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2504                 error "open RDWR" || true
2505 }
2506 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2507
2508 test_33b() {
2509         rm -fr $DIR/d33
2510         test_mkdir -p $DIR/d33
2511         chown $RUNAS_ID $DIR/d33
2512         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 && error "create" || true
2513 }
2514 run_test 33b "test open file with malformed flags (No panic and return error)"
2515
2516 test_33c() {
2517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2518         local ostnum
2519         local ostname
2520         local write_bytes
2521         local all_zeros
2522
2523         remote_ost_nodsh && skip "remote OST with nodsh" && return
2524         all_zeros=:
2525         rm -fr $DIR/d33
2526         test_mkdir -p $DIR/d33
2527         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2528
2529         sync
2530         for ostnum in $(seq $OSTCOUNT); do
2531                 # test-framework's OST numbering is one-based, while Lustre's
2532                 # is zero-based
2533                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2534                 # Parsing llobdstat's output sucks; we could grep the /proc
2535                 # path, but that's likely to not be as portable as using the
2536                 # llobdstat utility.  So we parse lctl output instead.
2537                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2538                         obdfilter/$ostname/stats |
2539                         awk '/^write_bytes/ {print $7}' )
2540                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2541                 if (( ${write_bytes:-0} > 0 ))
2542                 then
2543                         all_zeros=false
2544                         break;
2545                 fi
2546         done
2547
2548         $all_zeros || return 0
2549
2550         # Write four bytes
2551         echo foo > $DIR/d33/bar
2552         # Really write them
2553         sync
2554
2555         # Total up write_bytes after writing.  We'd better find non-zeros.
2556         for ostnum in $(seq $OSTCOUNT); do
2557                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2558                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2559                         obdfilter/$ostname/stats |
2560                         awk '/^write_bytes/ {print $7}' )
2561                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2562                 if (( ${write_bytes:-0} > 0 ))
2563                 then
2564                         all_zeros=false
2565                         break;
2566                 fi
2567         done
2568
2569         if $all_zeros
2570         then
2571                 for ostnum in $(seq $OSTCOUNT); do
2572                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2573                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2574                         do_facet ost$ostnum lctl get_param -n \
2575                                 obdfilter/$ostname/stats
2576                 done
2577                 error "OST not keeping write_bytes stats (b22312)"
2578         fi
2579 }
2580 run_test 33c "test llobdstat and write_bytes"
2581
2582 test_33d() {
2583         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2585         local MDTIDX=1
2586         local remote_dir=$DIR/$tdir/remote_dir
2587
2588         mkdir -p $DIR/$tdir
2589         $LFS mkdir -i $MDTIDX $remote_dir ||
2590                 error "create remote directory failed"
2591
2592         touch $remote_dir/$tfile
2593         chmod 444 $remote_dir/$tfile
2594         chown $RUNAS_ID $remote_dir/$tfile
2595
2596         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2597
2598         chown $RUNAS_ID $remote_dir
2599         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2600                                         error "create" || true
2601         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2602                                     error "open RDWR" || true
2603         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2604                                     error "create" || true
2605 }
2606 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2607
2608 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2609 test_34a() {
2610         rm -f $DIR/f34
2611         $MCREATE $DIR/f34 || error
2612         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2613         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2614         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2615         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2616 }
2617 run_test 34a "truncate file that has not been opened ==========="
2618
2619 test_34b() {
2620         [ ! -f $DIR/f34 ] && test_34a
2621         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2622         $OPENFILE -f O_RDONLY $DIR/f34
2623         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2624         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2625 }
2626 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2627
2628 test_34c() {
2629         [ ! -f $DIR/f34 ] && test_34a
2630         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2631         $OPENFILE -f O_RDWR $DIR/f34
2632         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2633         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2634 }
2635 run_test 34c "O_RDWR opening file-with-size works =============="
2636
2637 test_34d() {
2638         [ ! -f $DIR/f34 ] && test_34a
2639         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2640         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2641         rm $DIR/f34
2642 }
2643 run_test 34d "write to sparse file ============================="
2644
2645 test_34e() {
2646         rm -f $DIR/f34e
2647         $MCREATE $DIR/f34e || error
2648         $TRUNCATE $DIR/f34e 1000 || error
2649         $CHECKSTAT -s 1000 $DIR/f34e || error
2650         $OPENFILE -f O_RDWR $DIR/f34e
2651         $CHECKSTAT -s 1000 $DIR/f34e || error
2652 }
2653 run_test 34e "create objects, some with size and some without =="
2654
2655 test_34f() { # bug 6242, 6243
2656         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2657         SIZE34F=48000
2658         rm -f $DIR/f34f
2659         $MCREATE $DIR/f34f || error
2660         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2661         dd if=$DIR/f34f of=$TMP/f34f
2662         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2663         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2664         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2665         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2666         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2667 }
2668 run_test 34f "read from a file with no objects until EOF ======="
2669
2670 test_34g() {
2671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2672         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2673         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2674         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2675         cancel_lru_locks osc
2676         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2677                 error "wrong size after lock cancel"
2678
2679         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2680         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2681                 error "expanding truncate failed"
2682         cancel_lru_locks osc
2683         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2684                 error "wrong expanded size after lock cancel"
2685 }
2686 run_test 34g "truncate long file ==============================="
2687
2688 test_34h() {
2689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2690         local gid=10
2691         local sz=1000
2692
2693         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2694         sync # Flush the cache so that multiop below does not block on cache
2695              # flush when getting the group lock
2696         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2697         MULTIPID=$!
2698
2699         # Since just timed wait is not good enough, let's do a sync write
2700         # that way we are sure enough time for a roundtrip + processing
2701         # passed + 2 seconds of extra margin.
2702         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2703         rm $DIR/${tfile}-1
2704         sleep 2
2705
2706         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2707                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2708                 kill -9 $MULTIPID
2709         fi
2710         wait $MULTIPID
2711         local nsz=`stat -c %s $DIR/$tfile`
2712         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2713 }
2714 run_test 34h "ftruncate file under grouplock should not block"
2715
2716 test_35a() {
2717         cp /bin/sh $DIR/f35a
2718         chmod 444 $DIR/f35a
2719         chown $RUNAS_ID $DIR/f35a
2720         $RUNAS $DIR/f35a && error || true
2721         rm $DIR/f35a
2722 }
2723 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2724
2725 test_36a() {
2726         rm -f $DIR/f36
2727         utime $DIR/f36 || error
2728 }
2729 run_test 36a "MDS utime check (mknod, utime) ==================="
2730
2731 test_36b() {
2732         echo "" > $DIR/f36
2733         utime $DIR/f36 || error
2734 }
2735 run_test 36b "OST utime check (open, utime) ===================="
2736
2737 test_36c() {
2738         rm -f $DIR/d36/f36
2739         test_mkdir $DIR/d36
2740         chown $RUNAS_ID $DIR/d36
2741         $RUNAS utime $DIR/d36/f36 || error
2742 }
2743 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2744
2745 test_36d() {
2746         [ ! -d $DIR/d36 ] && test_36c
2747         echo "" > $DIR/d36/f36
2748         $RUNAS utime $DIR/d36/f36 || error
2749 }
2750 run_test 36d "non-root OST utime check (open, utime) ==========="
2751
2752 test_36e() {
2753         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2754         test_mkdir -p $DIR/$tdir
2755         touch $DIR/$tdir/$tfile
2756         $RUNAS utime $DIR/$tdir/$tfile && \
2757                 error "utime worked, expected failure" || true
2758 }
2759 run_test 36e "utime on non-owned file (should return error) ===="
2760
2761 subr_36fh() {
2762         local fl="$1"
2763         local LANG_SAVE=$LANG
2764         local LC_LANG_SAVE=$LC_LANG
2765         export LANG=C LC_LANG=C # for date language
2766
2767         DATESTR="Dec 20  2000"
2768         test_mkdir -p $DIR/$tdir
2769         lctl set_param fail_loc=$fl
2770         date; date +%s
2771         cp /etc/hosts $DIR/$tdir/$tfile
2772         sync & # write RPC generated with "current" inode timestamp, but delayed
2773         sleep 1
2774         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2775         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2776         cancel_lru_locks osc
2777         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2778         date; date +%s
2779         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2780                 echo "BEFORE: $LS_BEFORE" && \
2781                 echo "AFTER : $LS_AFTER" && \
2782                 echo "WANT  : $DATESTR" && \
2783                 error "$DIR/$tdir/$tfile timestamps changed" || true
2784
2785         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2786 }
2787
2788 test_36f() {
2789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2790         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2791         subr_36fh "0x80000214"
2792 }
2793 run_test 36f "utime on file racing with OST BRW write =========="
2794
2795 test_36g() {
2796         remote_ost_nodsh && skip "remote OST with nodsh" && return
2797         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2798         local fmd_max_age
2799         local fmd_before
2800         local fmd_after
2801
2802         test_mkdir -p $DIR/$tdir
2803         fmd_max_age=$(do_facet ost1 \
2804                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2805                 head -n 1")
2806
2807         fmd_before=$(do_facet ost1 \
2808                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2809         touch $DIR/$tdir/$tfile
2810         sleep $((fmd_max_age + 12))
2811         fmd_after=$(do_facet ost1 \
2812                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2813
2814         echo "fmd_before: $fmd_before"
2815         echo "fmd_after: $fmd_after"
2816         [[ $fmd_after -gt $fmd_before ]] &&
2817                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2818                 error "fmd didn't expire after ping" || true
2819 }
2820 run_test 36g "filter mod data cache expiry ====================="
2821
2822 test_36h() {
2823         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2824         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2825         subr_36fh "0x80000227"
2826 }
2827 run_test 36h "utime on file racing with OST BRW write =========="
2828
2829 test_36i() {
2830         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2831
2832         mkdir $DIR/$tdir
2833         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2834
2835         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2836         local new_mtime=$((mtime + 200))
2837
2838         #change Modify time of striped dir
2839         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2840                         error "change mtime failed"
2841
2842         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2843
2844         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2845 }
2846 run_test 36i "change mtime on striped directory"
2847
2848 # test_37 - duplicate with tests 32q 32r
2849
2850 test_38() {
2851         local file=$DIR/$tfile
2852         touch $file
2853         openfile -f O_DIRECTORY $file
2854         local RC=$?
2855         local ENOTDIR=20
2856         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
2857         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
2858 }
2859 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
2860
2861 test_39() {
2862         touch $DIR/$tfile
2863         touch $DIR/${tfile}2
2864 #       ls -l  $DIR/$tfile $DIR/${tfile}2
2865 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
2866 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
2867         sleep 2
2868         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
2869         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
2870                 echo "mtime"
2871                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
2872                 echo "atime"
2873                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
2874                 echo "ctime"
2875                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
2876                 error "O_TRUNC didn't change timestamps"
2877         fi
2878 }
2879 run_test 39 "mtime changed on create ==========================="
2880
2881 test_39b() {
2882         test_mkdir -p -c1 $DIR/$tdir
2883         cp -p /etc/passwd $DIR/$tdir/fopen
2884         cp -p /etc/passwd $DIR/$tdir/flink
2885         cp -p /etc/passwd $DIR/$tdir/funlink
2886         cp -p /etc/passwd $DIR/$tdir/frename
2887         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
2888
2889         sleep 1
2890         echo "aaaaaa" >> $DIR/$tdir/fopen
2891         echo "aaaaaa" >> $DIR/$tdir/flink
2892         echo "aaaaaa" >> $DIR/$tdir/funlink
2893         echo "aaaaaa" >> $DIR/$tdir/frename
2894
2895         local open_new=`stat -c %Y $DIR/$tdir/fopen`
2896         local link_new=`stat -c %Y $DIR/$tdir/flink`
2897         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
2898         local rename_new=`stat -c %Y $DIR/$tdir/frename`
2899
2900         cat $DIR/$tdir/fopen > /dev/null
2901         ln $DIR/$tdir/flink $DIR/$tdir/flink2
2902         rm -f $DIR/$tdir/funlink2
2903         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
2904
2905         for (( i=0; i < 2; i++ )) ; do
2906                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
2907                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
2908                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
2909                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
2910
2911                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
2912                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
2913                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
2914                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
2915
2916                 cancel_lru_locks osc
2917                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2918         done
2919 }
2920 run_test 39b "mtime change on open, link, unlink, rename  ======"
2921
2922 # this should be set to past
2923 TEST_39_MTIME=`date -d "1 year ago" +%s`
2924
2925 # bug 11063
2926 test_39c() {
2927         touch $DIR1/$tfile
2928         sleep 2
2929         local mtime0=`stat -c %Y $DIR1/$tfile`
2930
2931         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2932         local mtime1=`stat -c %Y $DIR1/$tfile`
2933         [ "$mtime1" = $TEST_39_MTIME ] || \
2934                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
2935
2936         local d1=`date +%s`
2937         echo hello >> $DIR1/$tfile
2938         local d2=`date +%s`
2939         local mtime2=`stat -c %Y $DIR1/$tfile`
2940         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
2941                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
2942
2943         mv $DIR1/$tfile $DIR1/$tfile-1
2944
2945         for (( i=0; i < 2; i++ )) ; do
2946                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
2947                 [ "$mtime2" = "$mtime3" ] || \
2948                         error "mtime ($mtime2) changed (to $mtime3) on rename"
2949
2950                 cancel_lru_locks osc
2951                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2952         done
2953 }
2954 run_test 39c "mtime change on rename ==========================="
2955
2956 # bug 21114
2957 test_39d() {
2958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2959         touch $DIR1/$tfile
2960
2961         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2962
2963         for (( i=0; i < 2; i++ )) ; do
2964                 local mtime=`stat -c %Y $DIR1/$tfile`
2965                 [ $mtime = $TEST_39_MTIME ] || \
2966                         error "mtime($mtime) is not set to $TEST_39_MTIME"
2967
2968                 cancel_lru_locks osc
2969                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2970         done
2971 }
2972 run_test 39d "create, utime, stat =============================="
2973
2974 # bug 21114
2975 test_39e() {
2976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2977         touch $DIR1/$tfile
2978         local mtime1=`stat -c %Y $DIR1/$tfile`
2979
2980         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
2981
2982         for (( i=0; i < 2; i++ )) ; do
2983                 local mtime2=`stat -c %Y $DIR1/$tfile`
2984                 [ $mtime2 = $TEST_39_MTIME ] || \
2985                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
2986
2987                 cancel_lru_locks osc
2988                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
2989         done
2990 }
2991 run_test 39e "create, stat, utime, stat ========================"
2992
2993 # bug 21114
2994 test_39f() {
2995         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2996         touch $DIR1/$tfile
2997         mtime1=`stat -c %Y $DIR1/$tfile`
2998
2999         sleep 2
3000         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3001
3002         for (( i=0; i < 2; i++ )) ; do
3003                 local mtime2=`stat -c %Y $DIR1/$tfile`
3004                 [ $mtime2 = $TEST_39_MTIME ] || \
3005                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3006
3007                 cancel_lru_locks osc
3008                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3009         done
3010 }
3011 run_test 39f "create, stat, sleep, utime, stat ================="
3012
3013 # bug 11063
3014 test_39g() {
3015         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3016         echo hello >> $DIR1/$tfile
3017         local mtime1=`stat -c %Y $DIR1/$tfile`
3018
3019         sleep 2
3020         chmod o+r $DIR1/$tfile
3021
3022         for (( i=0; i < 2; i++ )) ; do
3023                 local mtime2=`stat -c %Y $DIR1/$tfile`
3024                 [ "$mtime1" = "$mtime2" ] || \
3025                         error "lost mtime: $mtime2, should be $mtime1"
3026
3027                 cancel_lru_locks osc
3028                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3029         done
3030 }
3031 run_test 39g "write, chmod, stat ==============================="
3032
3033 # bug 11063
3034 test_39h() {
3035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3036         touch $DIR1/$tfile
3037         sleep 1
3038
3039         local d1=`date`
3040         echo hello >> $DIR1/$tfile
3041         local mtime1=`stat -c %Y $DIR1/$tfile`
3042
3043         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3044         local d2=`date`
3045         if [ "$d1" != "$d2" ]; then
3046                 echo "write and touch not within one second"
3047         else
3048                 for (( i=0; i < 2; i++ )) ; do
3049                         local mtime2=`stat -c %Y $DIR1/$tfile`
3050                         [ "$mtime2" = $TEST_39_MTIME ] || \
3051                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3052
3053                         cancel_lru_locks osc
3054                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3055                 done
3056         fi
3057 }
3058 run_test 39h "write, utime within one second, stat ============="
3059
3060 test_39i() {
3061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3062         touch $DIR1/$tfile
3063         sleep 1
3064
3065         echo hello >> $DIR1/$tfile
3066         local mtime1=`stat -c %Y $DIR1/$tfile`
3067
3068         mv $DIR1/$tfile $DIR1/$tfile-1
3069
3070         for (( i=0; i < 2; i++ )) ; do
3071                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3072
3073                 [ "$mtime1" = "$mtime2" ] || \
3074                         error "lost mtime: $mtime2, should be $mtime1"
3075
3076                 cancel_lru_locks osc
3077                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3078         done
3079 }
3080 run_test 39i "write, rename, stat =============================="
3081
3082 test_39j() {
3083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3084         start_full_debug_logging
3085         touch $DIR1/$tfile
3086         sleep 1
3087
3088         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3089         lctl set_param fail_loc=0x80000412
3090         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3091                 error "multiop failed"
3092         local multipid=$!
3093         local mtime1=`stat -c %Y $DIR1/$tfile`
3094
3095         mv $DIR1/$tfile $DIR1/$tfile-1
3096
3097         kill -USR1 $multipid
3098         wait $multipid || error "multiop close failed"
3099
3100         for (( i=0; i < 2; i++ )) ; do
3101                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3102                 [ "$mtime1" = "$mtime2" ] ||
3103                         error "mtime is lost on close: $mtime2, " \
3104                               "should be $mtime1"
3105
3106                 cancel_lru_locks osc
3107                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3108         done
3109         lctl set_param fail_loc=0
3110         stop_full_debug_logging
3111 }
3112 run_test 39j "write, rename, close, stat ======================="
3113
3114 test_39k() {
3115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3116         touch $DIR1/$tfile
3117         sleep 1
3118
3119         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3120         local multipid=$!
3121         local mtime1=`stat -c %Y $DIR1/$tfile`
3122
3123         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3124
3125         kill -USR1 $multipid
3126         wait $multipid || error "multiop close failed"
3127
3128         for (( i=0; i < 2; i++ )) ; do
3129                 local mtime2=`stat -c %Y $DIR1/$tfile`
3130
3131                 [ "$mtime2" = $TEST_39_MTIME ] || \
3132                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3133
3134                 cancel_lru_locks osc
3135                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3136         done
3137 }
3138 run_test 39k "write, utime, close, stat ========================"
3139
3140 # this should be set to future
3141 TEST_39_ATIME=`date -d "1 year" +%s`
3142
3143 test_39l() {
3144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3145         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3146         local atime_diff=$(do_facet $SINGLEMDS \
3147                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3148         rm -rf $DIR/$tdir
3149         mkdir -p $DIR/$tdir
3150
3151         # test setting directory atime to future
3152         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3153         local atime=$(stat -c %X $DIR/$tdir)
3154         [ "$atime" = $TEST_39_ATIME ] || \
3155                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3156
3157         # test setting directory atime from future to now
3158         local d1=$(date +%s)
3159         ls $DIR/$tdir
3160         local d2=$(date +%s)
3161
3162         cancel_lru_locks mdc
3163         atime=$(stat -c %X $DIR/$tdir)
3164         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3165                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3166
3167         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3168         sleep 3
3169
3170         # test setting directory atime when now > dir atime + atime_diff
3171         d1=$(date +%s)
3172         ls $DIR/$tdir
3173         d2=$(date +%s)
3174         cancel_lru_locks mdc
3175         atime=$(stat -c %X $DIR/$tdir)
3176         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3177                 error "atime is not updated  : $atime, should be $d2"
3178
3179         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3180         sleep 3
3181
3182         # test not setting directory atime when now < dir atime + atime_diff
3183         ls $DIR/$tdir
3184         cancel_lru_locks mdc
3185         atime=$(stat -c %X $DIR/$tdir)
3186         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3187                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3188
3189         do_facet $SINGLEMDS \
3190                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3191 }
3192 run_test 39l "directory atime update ==========================="
3193
3194 test_39m() {
3195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3196         touch $DIR1/$tfile
3197         sleep 2
3198         local far_past_mtime=$(date -d "May 29 1953" +%s)
3199         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3200
3201         touch -m -d @$far_past_mtime $DIR1/$tfile
3202         touch -a -d @$far_past_atime $DIR1/$tfile
3203
3204         for (( i=0; i < 2; i++ )) ; do
3205                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3206                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3207                         error "atime or mtime set incorrectly"
3208
3209                 cancel_lru_locks osc
3210                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3211         done
3212 }
3213 run_test 39m "test atime and mtime before 1970"
3214
3215 test_39n() { # LU-3832
3216         local atime_diff=$(do_facet $SINGLEMDS \
3217                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3218         local atime0
3219         local atime1
3220         local atime2
3221
3222         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3223
3224         rm -rf $DIR/$tfile
3225         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3226         atime0=$(stat -c %X $DIR/$tfile)
3227
3228         sleep 5
3229         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3230         atime1=$(stat -c %X $DIR/$tfile)
3231
3232         sleep 5
3233         cancel_lru_locks mdc
3234         cancel_lru_locks osc
3235         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3236         atime2=$(stat -c %X $DIR/$tfile)
3237
3238         do_facet $SINGLEMDS \
3239                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3240
3241         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3242         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3243 }
3244 run_test 39n "check that O_NOATIME is honored"
3245
3246 test_39o() {
3247         TESTDIR=$DIR/$tdir/$tfile
3248         [ -e $TESTDIR ] && rm -rf $TESTDIR
3249         test_mkdir -p $TESTDIR
3250         cd $TESTDIR
3251         links1=2
3252         ls
3253         mkdir a b
3254         ls
3255         links2=$(stat -c %h .)
3256         [ $(($links1 + 2)) != $links2 ] &&
3257                 error "wrong links count $(($links1 + 2)) != $links2"
3258         rmdir b
3259         links3=$(stat -c %h .)
3260         [ $(($links1 + 1)) != $links3 ] &&
3261                 error "wrong links count $links1 != $links3"
3262         return 0
3263 }
3264 run_test 39o "directory cached attributes updated after create ========"
3265
3266 test_39p() {
3267         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3268         local MDTIDX=1
3269         TESTDIR=$DIR/$tdir/$tfile
3270         [ -e $TESTDIR ] && rm -rf $TESTDIR
3271         mkdir -p $TESTDIR
3272         cd $TESTDIR
3273         links1=2
3274         ls
3275         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3276         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3277         ls
3278         links2=$(stat -c %h .)
3279         [ $(($links1 + 2)) != $links2 ] &&
3280                 error "wrong links count $(($links1 + 2)) != $links2"
3281         rmdir remote_dir2
3282         links3=$(stat -c %h .)
3283         [ $(($links1 + 1)) != $links3 ] &&
3284                 error "wrong links count $links1 != $links3"
3285         return 0
3286 }
3287 run_test 39p "remote directory cached attributes updated after create ========"
3288
3289
3290 test_40() {
3291         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3292         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3293                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3294         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3295                 error "$tfile is not 4096 bytes in size"
3296 }
3297 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3298
3299 test_41() {
3300         # bug 1553
3301         small_write $DIR/f41 18
3302 }
3303 run_test 41 "test small file write + fstat ====================="
3304
3305 count_ost_writes() {
3306         lctl get_param -n osc.*.stats |
3307                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3308                         END { printf("%0.0f", writes) }'
3309 }
3310
3311 # decent default
3312 WRITEBACK_SAVE=500
3313 DIRTY_RATIO_SAVE=40
3314 MAX_DIRTY_RATIO=50
3315 BG_DIRTY_RATIO_SAVE=10
3316 MAX_BG_DIRTY_RATIO=25
3317
3318 start_writeback() {
3319         trap 0
3320         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3321         # dirty_ratio, dirty_background_ratio
3322         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3323                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3324                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3325                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3326         else
3327                 # if file not here, we are a 2.4 kernel
3328                 kill -CONT `pidof kupdated`
3329         fi
3330 }
3331
3332 stop_writeback() {
3333         # setup the trap first, so someone cannot exit the test at the
3334         # exact wrong time and mess up a machine
3335         trap start_writeback EXIT
3336         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3337         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3338                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3339                 sysctl -w vm.dirty_writeback_centisecs=0
3340                 sysctl -w vm.dirty_writeback_centisecs=0
3341                 # save and increase /proc/sys/vm/dirty_ratio
3342                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3343                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3344                 # save and increase /proc/sys/vm/dirty_background_ratio
3345                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3346                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3347         else
3348                 # if file not here, we are a 2.4 kernel
3349                 kill -STOP `pidof kupdated`
3350         fi
3351 }
3352
3353 # ensure that all stripes have some grant before we test client-side cache
3354 setup_test42() {
3355         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3356                 dd if=/dev/zero of=$i bs=4k count=1
3357                 rm $i
3358         done
3359 }
3360
3361 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3362 # file truncation, and file removal.
3363 test_42a() {
3364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3365         setup_test42
3366         cancel_lru_locks osc
3367         stop_writeback
3368         sync; sleep 1; sync # just to be safe
3369         BEFOREWRITES=`count_ost_writes`
3370         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3371         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3372         AFTERWRITES=`count_ost_writes`
3373         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3374                 error "$BEFOREWRITES < $AFTERWRITES"
3375         start_writeback
3376 }
3377 run_test 42a "ensure that we don't flush on close =============="
3378
3379 test_42b() {
3380         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3381         setup_test42
3382         cancel_lru_locks osc
3383         stop_writeback
3384         sync
3385         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3386         BEFOREWRITES=$(count_ost_writes)
3387         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3388         AFTERWRITES=$(count_ost_writes)
3389         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3390                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3391         fi
3392         BEFOREWRITES=$(count_ost_writes)
3393         sync || error "sync: $?"
3394         AFTERWRITES=$(count_ost_writes)
3395         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3396                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3397         fi
3398         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3399         start_writeback
3400         return 0
3401 }
3402 run_test 42b "test destroy of file with cached dirty data ======"
3403
3404 # if these tests just want to test the effect of truncation,
3405 # they have to be very careful.  consider:
3406 # - the first open gets a {0,EOF}PR lock
3407 # - the first write conflicts and gets a {0, count-1}PW
3408 # - the rest of the writes are under {count,EOF}PW
3409 # - the open for truncate tries to match a {0,EOF}PR
3410 #   for the filesize and cancels the PWs.
3411 # any number of fixes (don't get {0,EOF} on open, match
3412 # composite locks, do smarter file size management) fix
3413 # this, but for now we want these tests to verify that
3414 # the cancellation with truncate intent works, so we
3415 # start the file with a full-file pw lock to match against
3416 # until the truncate.
3417 trunc_test() {
3418         test=$1
3419         file=$DIR/$test
3420         offset=$2
3421         cancel_lru_locks osc
3422         stop_writeback
3423         # prime the file with 0,EOF PW to match
3424         touch $file
3425         $TRUNCATE $file 0
3426         sync; sync
3427         # now the real test..
3428         dd if=/dev/zero of=$file bs=1024 count=100
3429         BEFOREWRITES=`count_ost_writes`
3430         $TRUNCATE $file $offset
3431         cancel_lru_locks osc
3432         AFTERWRITES=`count_ost_writes`
3433         start_writeback
3434 }
3435
3436 test_42c() {
3437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3438         trunc_test 42c 1024
3439         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3440             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3441         rm $file
3442 }
3443 run_test 42c "test partial truncate of file with cached dirty data"
3444
3445 test_42d() {
3446         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3447         trunc_test 42d 0
3448         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3449             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3450         rm $file
3451 }
3452 run_test 42d "test complete truncate of file with cached dirty data"
3453
3454 test_42e() { # bug22074
3455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3456         local TDIR=$DIR/${tdir}e
3457         local pagesz=$(page_size)
3458         local pages=16 # hardcoded 16 pages, don't change it.
3459         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3460         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3461         local max_dirty_mb
3462         local warmup_files
3463
3464         test_mkdir -p $DIR/${tdir}e
3465         $SETSTRIPE -c 1 $TDIR
3466         createmany -o $TDIR/f $files
3467
3468         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3469
3470         # we assume that with $OSTCOUNT files, at least one of them will
3471         # be allocated on OST0.
3472         warmup_files=$((OSTCOUNT * max_dirty_mb))
3473         createmany -o $TDIR/w $warmup_files
3474
3475         # write a large amount of data into one file and sync, to get good
3476         # avail_grant number from OST.
3477         for ((i=0; i<$warmup_files; i++)); do
3478                 idx=$($GETSTRIPE -i $TDIR/w$i)
3479                 [ $idx -ne 0 ] && continue
3480                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3481                 break
3482         done
3483         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3484         sync
3485         $LCTL get_param $proc_osc0/cur_dirty_bytes
3486         $LCTL get_param $proc_osc0/cur_grant_bytes
3487
3488         # create as much dirty pages as we can while not to trigger the actual
3489         # RPCs directly. but depends on the env, VFS may trigger flush during this
3490         # period, hopefully we are good.
3491         for ((i=0; i<$warmup_files; i++)); do
3492                 idx=$($GETSTRIPE -i $TDIR/w$i)
3493                 [ $idx -ne 0 ] && continue
3494                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3495         done
3496         $LCTL get_param $proc_osc0/cur_dirty_bytes
3497         $LCTL get_param $proc_osc0/cur_grant_bytes
3498
3499         # perform the real test
3500         $LCTL set_param $proc_osc0/rpc_stats 0
3501         for ((;i<$files; i++)); do
3502                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3503                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3504         done
3505         sync
3506         $LCTL get_param $proc_osc0/rpc_stats
3507
3508         local percent=0
3509         local have_ppr=false
3510         $LCTL get_param $proc_osc0/rpc_stats |
3511                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3512                         # skip lines until we are at the RPC histogram data
3513                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3514                         $have_ppr || continue
3515
3516                         # we only want the percent stat for < 16 pages
3517                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3518
3519                         percent=$((percent + WPCT))
3520                         if [[ $percent -gt 15 ]]; then
3521                                 error "less than 16-pages write RPCs" \
3522                                       "$percent% > 15%"
3523                                 break
3524                         fi
3525                 done
3526         rm -rf $TDIR
3527 }
3528 run_test 42e "verify sub-RPC writes are not done synchronously"
3529
3530 test_43() {
3531         test_mkdir -p $DIR/$tdir
3532         cp -p /bin/ls $DIR/$tdir/$tfile
3533         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3534         pid=$!
3535         # give multiop a chance to open
3536         sleep 1
3537
3538         $DIR/$tdir/$tfile && error || true
3539         kill -USR1 $pid
3540 }
3541 run_test 43 "execution of file opened for write should return -ETXTBSY"
3542
3543 test_43a() {
3544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3545         test_mkdir -p $DIR/$tdir
3546         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3547                         cp -p multiop $DIR/$tdir/multiop
3548         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3549                         return 1
3550         MULTIOP_PID=$!
3551         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3552         kill -USR1 $MULTIOP_PID || return 2
3553         wait $MULTIOP_PID || return 3
3554         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3555 }
3556 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3557
3558 test_43b() {
3559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3560         test_mkdir -p $DIR/$tdir
3561         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3562                         cp -p multiop $DIR/$tdir/multiop
3563         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3564                         return 1
3565         MULTIOP_PID=$!
3566         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3567         kill -USR1 $MULTIOP_PID || return 2
3568         wait $MULTIOP_PID || return 3
3569         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3570 }
3571 run_test 43b "truncate of file being executed should return -ETXTBSY"
3572
3573 test_43c() {
3574         local testdir="$DIR/$tdir"
3575         test_mkdir -p $DIR/$tdir
3576         cp $SHELL $testdir/
3577         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3578                 ( cd $testdir && md5sum -c)
3579 }
3580 run_test 43c "md5sum of copy into lustre========================"
3581
3582 test_44() {
3583         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3584         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3585         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3586 }
3587 run_test 44 "zero length read from a sparse stripe ============="
3588
3589 test_44a() {
3590         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3591                 awk '{ print $2 }')
3592         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3593         [[ $nstripe -gt $OSTCOUNT ]] &&
3594             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3595             return
3596         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3597                 awk '{ print $2 }')
3598         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3599                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3600                         awk '{ print $2 }')
3601         fi
3602
3603         OFFSETS="0 $((stride/2)) $((stride-1))"
3604         for offset in $OFFSETS; do
3605                 for i in $(seq 0 $((nstripe-1))); do
3606                         local GLOBALOFFSETS=""
3607                         # size in Bytes
3608                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3609                         local myfn=$DIR/d44a-$size
3610                         echo "--------writing $myfn at $size"
3611                         ll_sparseness_write $myfn $size ||
3612                                 error "ll_sparseness_write"
3613                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3614                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3615                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3616
3617                         for j in $(seq 0 $((nstripe-1))); do
3618                                 # size in Bytes
3619                                 size=$((((j + $nstripe )*$stride + $offset)))
3620                                 ll_sparseness_write $myfn $size ||
3621                                         error "ll_sparseness_write"
3622                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3623                         done
3624                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3625                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3626                         rm -f $myfn
3627                 done
3628         done
3629 }
3630 run_test 44a "test sparse pwrite ==============================="
3631
3632 dirty_osc_total() {
3633         tot=0
3634         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3635                 tot=$(($tot + $d))
3636         done
3637         echo $tot
3638 }
3639 do_dirty_record() {
3640         before=`dirty_osc_total`
3641         echo executing "\"$*\""
3642         eval $*
3643         after=`dirty_osc_total`
3644         echo before $before, after $after
3645 }
3646 test_45() {
3647         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3648         f="$DIR/f45"
3649         # Obtain grants from OST if it supports it
3650         echo blah > ${f}_grant
3651         stop_writeback
3652         sync
3653         do_dirty_record "echo blah > $f"
3654         [[ $before -eq $after ]] && error "write wasn't cached"
3655         do_dirty_record "> $f"
3656         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3657         do_dirty_record "echo blah > $f"
3658         [[ $before -eq $after ]] && error "write wasn't cached"
3659         do_dirty_record "sync"
3660         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3661         do_dirty_record "echo blah > $f"
3662         [[ $before -eq $after ]] && error "write wasn't cached"
3663         do_dirty_record "cancel_lru_locks osc"
3664         [[ $before -gt $after ]] ||
3665                 error "lock cancellation didn't lower dirty count"
3666         start_writeback
3667 }
3668 run_test 45 "osc io page accounting ============================"
3669
3670 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3671 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3672 # objects offset and an assert hit when an rpc was built with 1023's mapped
3673 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3674 test_46() {
3675         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3676         f="$DIR/f46"
3677         stop_writeback
3678         sync
3679         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3680         sync
3681         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3682         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3683         sync
3684         start_writeback
3685 }
3686 run_test 46 "dirtying a previously written page ================"
3687
3688 # test_47 is removed "Device nodes check" is moved to test_28
3689
3690 test_48a() { # bug 2399
3691         check_kernel_version 34 || return 0
3692         test_mkdir -p $DIR/$tdir
3693         cd $DIR/$tdir
3694         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3695         test_mkdir $DIR/$tdir || error "recreate directory failed"
3696         touch foo || error "'touch foo' failed after recreating cwd"
3697         test_mkdir $DIR/$tdir/bar ||
3698                      error "'mkdir foo' failed after recreating cwd"
3699         if check_kernel_version 44; then
3700                 touch .foo || error "'touch .foo' failed after recreating cwd"
3701                 test_mkdir $DIR/$tdir/.bar ||
3702                               error "'mkdir .foo' failed after recreating cwd"
3703         fi
3704         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3705         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3706         cd . || error "'cd .' failed after recreating cwd"
3707         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3708         rmdir . && error "'rmdir .' worked after recreating cwd"
3709         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3710         cd .. || error "'cd ..' failed after recreating cwd"
3711 }
3712 run_test 48a "Access renamed working dir (should return errors)="
3713
3714 test_48b() { # bug 2399
3715         check_kernel_version 34 || return 0
3716         rm -rf $DIR/$tdir
3717         test_mkdir -p $DIR/$tdir
3718         cd $DIR/$tdir
3719         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3720         touch foo && error "'touch foo' worked after removing cwd"
3721         test_mkdir $DIR/$tdir/foo &&
3722                      error "'mkdir foo' worked after removing cwd"
3723         if check_kernel_version 44; then
3724                 touch .foo && error "'touch .foo' worked after removing cwd"
3725                 test_mkdir $DIR/$tdir/.foo &&
3726                               error "'mkdir .foo' worked after removing cwd"
3727         fi
3728         ls . > /dev/null && error "'ls .' worked after removing cwd"
3729         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3730         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3731         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3732         rmdir . && error "'rmdir .' worked after removing cwd"
3733         ln -s . foo && error "'ln -s .' worked after removing cwd"
3734         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3735 }
3736 run_test 48b "Access removed working dir (should return errors)="
3737
3738 test_48c() { # bug 2350
3739         check_kernel_version 36 || return 0
3740         #lctl set_param debug=-1
3741         #set -vx
3742         rm -rf $DIR/$tdir
3743         test_mkdir -p $DIR/$tdir/dir
3744         cd $DIR/$tdir/dir
3745         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3746         $TRACE touch foo && error "touch foo worked after removing cwd"
3747         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3748         if check_kernel_version 44; then
3749                 touch .foo && error "touch .foo worked after removing cwd"
3750                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3751         fi
3752         $TRACE ls . && error "'ls .' worked after removing cwd"
3753         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3754         is_patchless || ( $TRACE cd . &&
3755                         error "'cd .' worked after removing cwd" )
3756         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3757         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3758         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3759         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3760 }
3761 run_test 48c "Access removed working subdir (should return errors)"
3762
3763 test_48d() { # bug 2350
3764         check_kernel_version 36 || return 0
3765         #lctl set_param debug=-1
3766         #set -vx
3767         rm -rf $DIR/$tdir
3768         test_mkdir -p $DIR/$tdir/dir
3769         cd $DIR/$tdir/dir
3770         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3771         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3772         $TRACE touch foo && error "'touch foo' worked after removing parent"
3773         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3774         if check_kernel_version 44; then
3775                 touch .foo && error "'touch .foo' worked after removing parent"
3776                 test_mkdir .foo &&
3777                               error "mkdir .foo worked after removing parent"
3778         fi
3779         $TRACE ls . && error "'ls .' worked after removing parent"
3780         $TRACE ls .. && error "'ls ..' worked after removing parent"
3781         is_patchless || ( $TRACE cd . &&
3782                         error "'cd .' worked after recreate parent" )
3783         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3784         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3785         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3786         is_patchless || ( $TRACE cd .. &&
3787                         error "'cd ..' worked after removing parent" || true )
3788 }
3789 run_test 48d "Access removed parent subdir (should return errors)"
3790
3791 test_48e() { # bug 4134
3792         check_kernel_version 41 || return 0
3793         #lctl set_param debug=-1
3794         #set -vx
3795         rm -rf $DIR/$tdir
3796         test_mkdir -p $DIR/$tdir/dir
3797         cd $DIR/$tdir/dir
3798         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3799         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3800         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3801         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3802         # On a buggy kernel addition of "touch foo" after cd .. will
3803         # produce kernel oops in lookup_hash_it
3804         touch ../foo && error "'cd ..' worked after recreate parent"
3805         cd $DIR
3806         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3807 }
3808 run_test 48e "Access to recreated parent subdir (should return errors)"
3809
3810 test_49() { # LU-1030
3811         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3812         # get ost1 size - lustre-OST0000
3813         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3814                 awk '{ print $4 }')
3815         # write 800M at maximum
3816         [[ $ost1_size -lt 2 ]] && ost1_size=2
3817         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3818
3819         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3820         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3821         local dd_pid=$!
3822
3823         # change max_pages_per_rpc while writing the file
3824         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3825         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3826         # loop until dd process exits
3827         while ps ax -opid | grep -wq $dd_pid; do
3828                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3829                 sleep $((RANDOM % 5 + 1))
3830         done
3831         # restore original max_pages_per_rpc
3832         $LCTL set_param $osc1_mppc=$orig_mppc
3833         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3834 }
3835 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3836
3837 test_50() {
3838         # bug 1485
3839         test_mkdir $DIR/$tdir
3840         cd $DIR/$tdir
3841         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3842 }
3843 run_test 50 "special situations: /proc symlinks  ==============="
3844
3845 test_51a() {    # was test_51
3846         # bug 1516 - create an empty entry right after ".." then split dir
3847         test_mkdir -c1 $DIR/$tdir
3848         touch $DIR/$tdir/foo
3849         $MCREATE $DIR/$tdir/bar
3850         rm $DIR/$tdir/foo
3851         createmany -m $DIR/$tdir/longfile 201
3852         FNUM=202
3853         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
3854                 $MCREATE $DIR/$tdir/longfile$FNUM
3855                 FNUM=$(($FNUM + 1))
3856                 echo -n "+"
3857         done
3858         echo
3859         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
3860 }
3861 run_test 51a "special situations: split htree with empty entry =="
3862
3863 export NUMTEST=70000
3864 test_51b() {
3865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3866         local BASE=$DIR/d${base}.${TESTSUITE}
3867
3868         # cleanup the directory
3869         rm -fr $BASE
3870
3871         test_mkdir -p -c1 $BASE
3872
3873         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
3874         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
3875         [[ $numfree -lt 21000 ]] && skip "not enough free inodes ($numfree)" &&
3876                 return
3877
3878         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3879                 echo "reduced count to $NUMTEST due to inodes"
3880
3881         # need to check free space for the directories as well
3882         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
3883         numfree=$((blkfree / 4))
3884         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
3885                 echo "reduced count to $NUMTEST due to blocks"
3886
3887         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
3888                 echo "failed" > $BASE/fnum
3889 }
3890 run_test 51b "exceed 64k subdirectory nlink limit"
3891
3892 test_51ba() { # LU-993
3893         local BASE=$DIR/d${base}.${TESTSUITE}
3894         # unlink all but 100 subdirectories, then check it still works
3895         local LEFT=100
3896         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
3897
3898         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
3899         local DELETE=$((NUMTEST - LEFT))
3900
3901         # continue on to run this test even if 51b didn't finish,
3902         # just to delete the many subdirectories created.
3903         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
3904
3905         # for ldiskfs the nlink count should be 1, but this is OSD specific
3906         # and so this is listed for informational purposes only
3907         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
3908         unlinkmany -d $BASE/d $DELETE
3909         RC=$?
3910
3911         if [ $RC -ne 0 ]; then
3912                 if [ "$NUMPREV" == "failed" ]; then
3913                         skip "previous setup failed"
3914                         return 0
3915                 else
3916                         error "unlink of first $DELETE subdirs failed"
3917                         return $RC
3918                 fi
3919         fi
3920
3921         echo "nlink between: $(stat -c %h $BASE)"
3922         # trim the first line of ls output
3923         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
3924         [ $FOUND -ne $LEFT ] &&
3925                 error "can't find subdirs: found only $FOUND/$LEFT"
3926
3927         unlinkmany -d $BASE/d $DELETE $LEFT ||
3928                 error "unlink of second $LEFT subdirs failed"
3929         # regardless of whether the backing filesystem tracks nlink accurately
3930         # or not, the nlink count shouldn't be more than "." and ".." here
3931         local AFTER=$(stat -c %h $BASE)
3932         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
3933                 echo "nlink after: $AFTER"
3934 }
3935 run_test 51ba "verify nlink for many subdirectory cleanup"
3936
3937 test_51d() {
3938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3939         [[ $OSTCOUNT -lt 3 ]] &&
3940                 skip_env "skipping test with few OSTs" && return
3941         test_mkdir -p $DIR/$tdir
3942         createmany -o $DIR/$tdir/t- 1000
3943         $GETSTRIPE $DIR/$tdir > $TMP/files
3944         for N in $(seq 0 $((OSTCOUNT - 1))); do
3945                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
3946                         END { printf("%0.0f", objs) }' $TMP/files)
3947                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
3948                         '($1 == '$N') { objs += 1 } \
3949                         END { printf("%0.0f", objs) }')
3950                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
3951         done
3952         unlinkmany $DIR/$tdir/t- 1000
3953
3954         NLAST=0
3955         for N in $(seq 1 $((OSTCOUNT - 1))); do
3956                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
3957                         error "OST $N has less objects vs OST $NLAST" \
3958                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
3959                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
3960                         error "OST $N has less objects vs OST $NLAST" \
3961                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
3962
3963                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
3964                         error "OST $N has less #0 objects vs OST $NLAST" \
3965                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3966                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
3967                         error "OST $N has less #0 objects vs OST $NLAST" \
3968                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
3969                 NLAST=$N
3970         done
3971 }
3972 run_test 51d "check object distribution ===================="
3973
3974 test_52a() {
3975         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
3976         test_mkdir -p $DIR/$tdir
3977         touch $DIR/$tdir/foo
3978         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
3979         echo bar >> $DIR/$tdir/foo || error "append bar failed"
3980         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
3981         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
3982         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
3983                                         error "link worked"
3984         echo foo >> $DIR/$tdir/foo || error "append foo failed"
3985         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
3986         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
3987                                                      error "lsattr"
3988         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
3989         cp -r $DIR/$tdir /tmp/
3990         rm -fr $DIR/$tdir || error "cleanup rm failed"
3991 }
3992 run_test 52a "append-only flag test (should return errors) ====="
3993
3994 test_52b() {
3995         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
3996         test_mkdir -p $DIR/$tdir
3997         touch $DIR/$tdir/foo
3998         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
3999         cat test > $DIR/$tdir/foo && error "cat test worked"
4000         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4001         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4002         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4003                                         error "link worked"
4004         echo foo >> $DIR/$tdir/foo && error "echo worked"
4005         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4006         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4007         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4008         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4009                                                         error "lsattr"
4010         chattr -i $DIR/$tdir/foo || error "chattr failed"
4011
4012         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4013 }
4014 run_test 52b "immutable flag test (should return errors) ======="
4015
4016 test_53() {
4017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4018         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4019         remote_ost_nodsh && skip "remote OST with nodsh" && return
4020
4021         local param
4022         local param_seq
4023         local ostname
4024         local mds_last
4025         local mds_last_seq
4026         local ost_last
4027         local ost_last_seq
4028         local ost_last_id
4029         local ostnum
4030         local node
4031         local found=false
4032         local support_last_seq=true
4033
4034         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4035                 support_last_seq=false
4036
4037         # only test MDT0000
4038         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4039         local value
4040         for value in $(do_facet $SINGLEMDS \
4041                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4042                 param=$(echo ${value[0]} | cut -d "=" -f1)
4043                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4044
4045                 if $support_last_seq; then
4046                         param_seq=$(echo $param |
4047                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4048                         mds_last_seq=$(do_facet $SINGLEMDS \
4049                                        $LCTL get_param -n $param_seq)
4050                 fi
4051                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4052
4053                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4054                 node=$(facet_active_host ost$((ostnum+1)))
4055                 param="obdfilter.$ostname.last_id"
4056                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4057                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4058                         ost_last_id=$ost_last
4059
4060                         if $support_last_seq; then
4061                                 ost_last_id=$(echo $ost_last |
4062                                               awk -F':' '{print $2}' |
4063                                               sed -e "s/^0x//g")
4064                                 ost_last_seq=$(echo $ost_last |
4065                                                awk -F':' '{print $1}')
4066                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4067                         fi
4068
4069                         if [[ $ost_last_id != $mds_last ]]; then
4070                                 error "$ost_last_id != $mds_last"
4071                         else
4072                                 found=true
4073                                 break
4074                         fi
4075                 done
4076         done
4077         $found || error "can not match last_seq/last_id for $mdtosc"
4078         return 0
4079 }
4080 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4081
4082 test_54a() {
4083         $SOCKETSERVER $DIR/socket ||
4084                 error "$SOCKETSERVER $DIR/socket failed: $?"
4085         $SOCKETCLIENT $DIR/socket ||
4086                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4087         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4088 }
4089 run_test 54a "unix domain socket test =========================="
4090
4091 test_54b() {
4092         f="$DIR/f54b"
4093         mknod $f c 1 3
4094         chmod 0666 $f
4095         dd if=/dev/zero of=$f bs=$(page_size) count=1
4096 }
4097 run_test 54b "char device works in lustre ======================"
4098
4099 find_loop_dev() {
4100         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4101         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4102         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4103
4104         for i in $(seq 3 7); do
4105                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4106                 LOOPDEV=$LOOPBASE$i
4107                 LOOPNUM=$i
4108                 break
4109         done
4110 }
4111
4112 cleanup_54c() {
4113         loopdev="$DIR/loop54c"
4114
4115         trap 0
4116         $UMOUNT -d $tdir || rc=$?
4117         losetup -d $loopdev || true
4118         rm $loopdev
4119         return $rc
4120 }
4121
4122 test_54c() {
4123         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4124         tfile="$DIR/f54c"
4125         tdir="$DIR/d54c"
4126         loopdev="$DIR/loop54c"
4127
4128         find_loop_dev
4129         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4130         mknod $loopdev b 7 $LOOPNUM
4131         echo "make a loop file system with $tfile on $loopdev ($LOOPNUM)..."
4132         dd if=/dev/zero of=$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4133         losetup $loopdev $tfile || error "can't set up $loopdev for $tfile"
4134         trap cleanup_54c EXIT
4135         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4136         test_mkdir -p $tdir
4137         mount -t ext2 $loopdev $tdir || error "error mounting $loopdev on $tdir"
4138         dd if=/dev/zero of=$tdir/tmp bs=`page_size` count=30 || error "dd write"
4139         df $tdir
4140         dd if=$tdir/tmp of=/dev/zero bs=`page_size` count=30 || error "dd read"
4141         cleanup_54c
4142 }
4143 run_test 54c "block device works in lustre ====================="
4144
4145 test_54d() {
4146         f="$DIR/f54d"
4147         string="aaaaaa"
4148         mknod $f p
4149         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4150 }
4151 run_test 54d "fifo device works in lustre ======================"
4152
4153 test_54e() {
4154         check_kernel_version 46 || return 0
4155         f="$DIR/f54e"
4156         string="aaaaaa"
4157         cp -aL /dev/console $f
4158         echo $string > $f || error "echo $string to $f failed"
4159 }
4160 run_test 54e "console/tty device works in lustre ======================"
4161
4162 #The test_55 used to be iopen test and it was removed by bz#24037.
4163 #run_test 55 "check iopen_connect_dentry() ======================"
4164
4165 test_56a() {    # was test_56
4166         rm -rf $DIR/$tdir
4167         $SETSTRIPE -d $DIR
4168         test_mkdir -p $DIR/$tdir/dir
4169         NUMFILES=3
4170         NUMFILESx2=$(($NUMFILES * 2))
4171         for i in $(seq 1 $NUMFILES); do
4172                 touch $DIR/$tdir/file$i
4173                 touch $DIR/$tdir/dir/file$i
4174         done
4175
4176         # test lfs getstripe with --recursive
4177         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4178         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4179                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4180         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4181         [[ $FILENUM -eq $NUMFILES ]] ||
4182                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4183         echo "$GETSTRIPE --recursive passed."
4184
4185         # test lfs getstripe with file instead of dir
4186         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4187         [[ $FILENUM -eq 1 ]] ||
4188                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4189         echo "$GETSTRIPE file1 passed."
4190
4191         #test lfs getstripe with --verbose
4192         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4193                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4194                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4195         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4196                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4197         echo "$GETSTRIPE --verbose passed."
4198
4199         #test lfs getstripe with --obd
4200         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4201                 grep -q "unknown obduuid" ||
4202                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4203
4204         [[ $OSTCOUNT -lt 2 ]] &&
4205                 skip_env "skipping other $GETSTRIPE --obd test" && return
4206
4207         OSTIDX=1
4208         OBDUUID=$(ostuuid_from_index $OSTIDX)
4209         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4210         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4211         [[ $FOUND -eq $FILENUM ]] ||
4212                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4213         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4214                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4215                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4216                 error "$GETSTRIPE --obd: should not show file on other obd"
4217         echo "$GETSTRIPE --obd passed"
4218 }
4219 run_test 56a "check $GETSTRIPE"
4220
4221 NUMFILES=3
4222 NUMDIRS=3
4223 setup_56() {
4224         local LOCAL_NUMFILES="$1"
4225         local LOCAL_NUMDIRS="$2"
4226         local MKDIR_PARAMS="$3"
4227         local DIR_STRIPE_PARAMS="$4"
4228
4229         if [ ! -d "$TDIR" ] ; then
4230                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4231                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4232                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4233                         touch $TDIR/file$i
4234                 done
4235                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4236                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4237                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4238                                 touch $TDIR/dir$i/file$j
4239                         done
4240                 done
4241         fi
4242 }
4243
4244 setup_56_special() {
4245         LOCAL_NUMFILES=$1
4246         LOCAL_NUMDIRS=$2
4247         setup_56 $1 $2
4248         if [ ! -e "$TDIR/loop1b" ] ; then
4249                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4250                         mknod $TDIR/loop${i}b b 7 $i
4251                         mknod $TDIR/null${i}c c 1 3
4252                         ln -s $TDIR/file1 $TDIR/link${i}l
4253                 done
4254                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4255                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4256                         mknod $TDIR/dir$i/null${i}c c 1 3
4257                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4258                 done
4259         fi
4260 }
4261
4262 test_56g() {
4263         $SETSTRIPE -d $DIR
4264
4265         TDIR=$DIR/${tdir}g
4266         setup_56 $NUMFILES $NUMDIRS
4267
4268         EXPECTED=$(($NUMDIRS + 2))
4269         # test lfs find with -name
4270         for i in $(seq 1 $NUMFILES) ; do
4271                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4272                 [ $NUMS -eq $EXPECTED ] ||
4273                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4274                               "found $NUMS, expected $EXPECTED"
4275         done
4276 }
4277 run_test 56g "check lfs find -name ============================="
4278
4279 test_56h() {
4280         $SETSTRIPE -d $DIR
4281
4282         TDIR=$DIR/${tdir}g
4283         setup_56 $NUMFILES $NUMDIRS
4284
4285         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4286         # test lfs find with ! -name
4287         for i in $(seq 1 $NUMFILES) ; do
4288                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4289                 [ $NUMS -eq $EXPECTED ] ||
4290                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4291                               "found $NUMS, expected $EXPECTED"
4292         done
4293 }
4294 run_test 56h "check lfs find ! -name ============================="
4295
4296 test_56i() {
4297        tdir=${tdir}i
4298        test_mkdir -p $DIR/$tdir
4299        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4300        CMD="$LFIND -ost $UUID $DIR/$tdir"
4301        OUT=$($CMD)
4302        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4303 }
4304 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4305
4306 test_56j() {
4307         TDIR=$DIR/${tdir}g
4308         setup_56_special $NUMFILES $NUMDIRS
4309
4310         EXPECTED=$((NUMDIRS + 1))
4311         CMD="$LFIND -type d $TDIR"
4312         NUMS=$($CMD | wc -l)
4313         [ $NUMS -eq $EXPECTED ] ||
4314                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4315 }
4316 run_test 56j "check lfs find -type d ============================="
4317
4318 test_56k() {
4319         TDIR=$DIR/${tdir}g
4320         setup_56_special $NUMFILES $NUMDIRS
4321
4322         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4323         CMD="$LFIND -type f $TDIR"
4324         NUMS=$($CMD | wc -l)
4325         [ $NUMS -eq $EXPECTED ] ||
4326                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4327 }
4328 run_test 56k "check lfs find -type f ============================="
4329
4330 test_56l() {
4331         TDIR=$DIR/${tdir}g
4332         setup_56_special $NUMFILES $NUMDIRS
4333
4334         EXPECTED=$((NUMDIRS + NUMFILES))
4335         CMD="$LFIND -type b $TDIR"
4336         NUMS=$($CMD | wc -l)
4337         [ $NUMS -eq $EXPECTED ] ||
4338                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4339 }
4340 run_test 56l "check lfs find -type b ============================="
4341
4342 test_56m() {
4343         TDIR=$DIR/${tdir}g
4344         setup_56_special $NUMFILES $NUMDIRS
4345
4346         EXPECTED=$((NUMDIRS + NUMFILES))
4347         CMD="$LFIND -type c $TDIR"
4348         NUMS=$($CMD | wc -l)
4349         [ $NUMS -eq $EXPECTED ] ||
4350                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4351 }
4352 run_test 56m "check lfs find -type c ============================="
4353
4354 test_56n() {
4355         TDIR=$DIR/${tdir}g
4356         setup_56_special $NUMFILES $NUMDIRS
4357
4358         EXPECTED=$((NUMDIRS + NUMFILES))
4359         CMD="$LFIND -type l $TDIR"
4360         NUMS=$($CMD | wc -l)
4361         [ $NUMS -eq $EXPECTED ] ||
4362                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4363 }
4364 run_test 56n "check lfs find -type l ============================="
4365
4366 test_56o() {
4367         TDIR=$DIR/${tdir}o
4368         setup_56 $NUMFILES $NUMDIRS
4369         utime $TDIR/file1 > /dev/null || error "utime (1)"
4370         utime $TDIR/file2 > /dev/null || error "utime (2)"
4371         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4372         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4373         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4374         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4375
4376         EXPECTED=4
4377         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4378         [ $NUMS -eq $EXPECTED ] || \
4379                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4380
4381         EXPECTED=12
4382         CMD="$LFIND -mtime 0 $TDIR"
4383         NUMS=$($CMD | wc -l)
4384         [ $NUMS -eq $EXPECTED ] ||
4385                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4386 }
4387 run_test 56o "check lfs find -mtime for old files =========================="
4388
4389 test_56p() {
4390         [ $RUNAS_ID -eq $UID ] &&
4391                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4392
4393         TDIR=$DIR/${tdir}p
4394         setup_56 $NUMFILES $NUMDIRS
4395
4396         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4397         EXPECTED=$NUMFILES
4398         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4399         NUMS=$($CMD | wc -l)
4400         [ $NUMS -eq $EXPECTED ] || \
4401                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4402
4403         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4404         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4405         NUMS=$($CMD | wc -l)
4406         [ $NUMS -eq $EXPECTED ] || \
4407                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4408 }
4409 run_test 56p "check lfs find -uid and ! -uid ==============================="
4410
4411 test_56q() {
4412         [ $RUNAS_ID -eq $UID ] &&
4413                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4414
4415         TDIR=$DIR/${tdir}q
4416         setup_56 $NUMFILES $NUMDIRS
4417
4418         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4419
4420         EXPECTED=$NUMFILES
4421         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4422         NUMS=$($CMD | wc -l)
4423         [ $NUMS -eq $EXPECTED ] ||
4424                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4425
4426         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4427         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4428         NUMS=$($CMD | wc -l)
4429         [ $NUMS -eq $EXPECTED ] ||
4430                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4431 }
4432 run_test 56q "check lfs find -gid and ! -gid ==============================="
4433
4434 test_56r() {
4435         TDIR=$DIR/${tdir}r
4436         setup_56 $NUMFILES $NUMDIRS
4437
4438         EXPECTED=12
4439         CMD="$LFIND -size 0 -type f $TDIR"
4440         NUMS=$($CMD | wc -l)
4441         [ $NUMS -eq $EXPECTED ] ||
4442                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4443         EXPECTED=0
4444         CMD="$LFIND ! -size 0 -type f $TDIR"
4445         NUMS=$($CMD | wc -l)
4446         [ $NUMS -eq $EXPECTED ] ||
4447                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4448         echo "test" > $TDIR/$tfile
4449         echo "test2" > $TDIR/$tfile.2 && sync
4450         EXPECTED=1
4451         CMD="$LFIND -size 5 -type f $TDIR"
4452         NUMS=$($CMD | wc -l)
4453         [ $NUMS -eq $EXPECTED ] ||
4454                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4455         EXPECTED=1
4456         CMD="$LFIND -size +5 -type f $TDIR"
4457         NUMS=$($CMD | wc -l)
4458         [ $NUMS -eq $EXPECTED ] ||
4459                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4460         EXPECTED=2
4461         CMD="$LFIND -size +0 -type f $TDIR"
4462         NUMS=$($CMD | wc -l)
4463         [ $NUMS -eq $EXPECTED ] ||
4464                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4465         EXPECTED=2
4466         CMD="$LFIND ! -size -5 -type f $TDIR"
4467         NUMS=$($CMD | wc -l)
4468         [ $NUMS -eq $EXPECTED ] ||
4469                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4470         EXPECTED=12
4471         CMD="$LFIND -size -5 -type f $TDIR"
4472         NUMS=$($CMD | wc -l)
4473         [ $NUMS -eq $EXPECTED ] ||
4474                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4475 }
4476 run_test 56r "check lfs find -size works =========================="
4477
4478 test_56s() { # LU-611
4479         TDIR=$DIR/${tdir}s
4480         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4481
4482         if [[ $OSTCOUNT -gt 1 ]]; then
4483                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4484                 ONESTRIPE=4
4485                 EXTRA=4
4486         else
4487                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4488                 EXTRA=0
4489         fi
4490
4491         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4492         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4493         NUMS=$($CMD | wc -l)
4494         [ $NUMS -eq $EXPECTED ] || {
4495                 $GETSTRIPE -R $TDIR
4496                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4497         }
4498
4499         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4500         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4501         NUMS=$($CMD | wc -l)
4502         [ $NUMS -eq $EXPECTED ] || {
4503                 $GETSTRIPE -R $TDIR
4504                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4505         }
4506
4507         EXPECTED=$ONESTRIPE
4508         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4509         NUMS=$($CMD | wc -l)
4510         [ $NUMS -eq $EXPECTED ] || {
4511                 $GETSTRIPE -R $TDIR
4512                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4513         }
4514
4515         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4516         NUMS=$($CMD | wc -l)
4517         [ $NUMS -eq $EXPECTED ] || {
4518                 $GETSTRIPE -R $TDIR
4519                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4520         }
4521
4522         EXPECTED=0
4523         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4524         NUMS=$($CMD | wc -l)
4525         [ $NUMS -eq $EXPECTED ] || {
4526                 $GETSTRIPE -R $TDIR
4527                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4528         }
4529 }
4530 run_test 56s "check lfs find -stripe-count works"
4531
4532 test_56t() { # LU-611
4533         TDIR=$DIR/${tdir}t
4534         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4535
4536         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4537
4538         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4539         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4540         NUMS=$($CMD | wc -l)
4541         [ $NUMS -eq $EXPECTED ] ||
4542                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4543
4544         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4545         NUMS=$($CMD | wc -l)
4546         [ $NUMS -eq $EXPECTED ] ||
4547                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4548
4549         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4550         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4551         NUMS=$($CMD | wc -l)
4552         [ $NUMS -eq $EXPECTED ] ||
4553                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4554
4555         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4556         NUMS=$($CMD | wc -l)
4557         [ $NUMS -eq $EXPECTED ] ||
4558                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4559
4560         EXPECTED=4
4561         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4562         NUMS=$($CMD | wc -l)
4563         [ $NUMS -eq $EXPECTED ] ||
4564                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4565
4566         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4567         NUMS=$($CMD | wc -l)
4568         [ $NUMS -eq $EXPECTED ] ||
4569                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4570
4571         EXPECTED=0
4572         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4573         NUMS=$($CMD | wc -l)
4574         [ $NUMS -eq $EXPECTED ] ||
4575                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4576 }
4577 run_test 56t "check lfs find -stripe-size works"
4578
4579 test_56u() { # LU-611
4580         TDIR=$DIR/${tdir}u
4581         setup_56 $NUMFILES $NUMDIRS "-i 0"
4582
4583         if [[ $OSTCOUNT -gt 1 ]]; then
4584                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4585                 ONESTRIPE=4
4586         else
4587                 ONESTRIPE=0
4588         fi
4589
4590         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4591         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4592         NUMS=$($CMD | wc -l)
4593         [ $NUMS -eq $EXPECTED ] ||
4594                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4595
4596         EXPECTED=$ONESTRIPE
4597         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4598         NUMS=$($CMD | wc -l)
4599         [ $NUMS -eq $EXPECTED ] ||
4600                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4601
4602         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4603         NUMS=$($CMD | wc -l)
4604         [ $NUMS -eq $EXPECTED ] ||
4605                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4606
4607         EXPECTED=0
4608         # This should produce an error and not return any files
4609         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4610         NUMS=$($CMD 2>/dev/null | wc -l)
4611         [ $NUMS -eq $EXPECTED ] ||
4612                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4613
4614         if [[ $OSTCOUNT -gt 1 ]]; then
4615                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4616                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4617                 NUMS=$($CMD | wc -l)
4618                 [ $NUMS -eq $EXPECTED ] ||
4619                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4620         fi
4621 }
4622 run_test 56u "check lfs find -stripe-index works"
4623
4624 test_56v() {
4625     local MDT_IDX=0
4626
4627     TDIR=$DIR/${tdir}v
4628     rm -rf $TDIR
4629     setup_56 $NUMFILES $NUMDIRS
4630
4631     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4632     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4633
4634     for file in $($LFIND -mdt $UUID $TDIR); do
4635         file_mdt_idx=$($GETSTRIPE -M $file)
4636         [ $file_mdt_idx -eq $MDT_IDX ] ||
4637             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4638     done
4639 }
4640 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4641
4642 # Get and check the actual stripe count of one file.
4643 # Usage: check_stripe_count <file> <expected_stripe_count>
4644 check_stripe_count() {
4645     local file=$1
4646     local expected=$2
4647     local actual
4648
4649     [[ -z "$file" || -z "$expected" ]] &&
4650         error "check_stripe_count: invalid argument!"
4651
4652     local cmd="$GETSTRIPE -c $file"
4653     actual=$($cmd) || error "$cmd failed"
4654     actual=${actual%% *}
4655
4656     if [[ $actual -ne $expected ]]; then
4657         [[ $expected -eq -1 ]] ||
4658             error "$cmd wrong: found $actual, expected $expected"
4659         [[ $actual -eq $OSTCOUNT ]] ||
4660             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4661     fi
4662 }
4663
4664 test_56w() {
4665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4666         TDIR=$DIR/${tdir}w
4667
4668     rm -rf $TDIR || error "remove $TDIR failed"
4669     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4670
4671     local stripe_size
4672     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4673         error "$GETSTRIPE -S -d $TDIR failed"
4674     stripe_size=${stripe_size%% *}
4675
4676     local file_size=$((stripe_size * OSTCOUNT))
4677     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4678     local required_space=$((file_num * file_size))
4679     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4680     [[ $free_space -le $((required_space / 1024)) ]] &&
4681         skip_env "need at least $required_space bytes free space," \
4682                  "have $free_space kbytes" && return
4683
4684     local dd_bs=65536
4685     local dd_count=$((file_size / dd_bs))
4686
4687     # write data into the files
4688     local i
4689     local j
4690     local file
4691     for i in $(seq 1 $NUMFILES); do
4692         file=$TDIR/file$i
4693         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4694             error "write data into $file failed"
4695     done
4696     for i in $(seq 1 $NUMDIRS); do
4697         for j in $(seq 1 $NUMFILES); do
4698             file=$TDIR/dir$i/file$j
4699             yes | dd bs=$dd_bs count=$dd_count of=$file \
4700                 >/dev/null 2>&1 ||
4701                 error "write data into $file failed"
4702         done
4703     done
4704
4705     local expected=-1
4706     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4707
4708     # lfs_migrate file
4709     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4710     echo "$cmd"
4711     eval $cmd || error "$cmd failed"
4712
4713     check_stripe_count $TDIR/file1 $expected
4714
4715     # lfs_migrate dir
4716     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4717     echo "$cmd"
4718     eval $cmd || error "$cmd failed"
4719
4720     for j in $(seq 1 $NUMFILES); do
4721         check_stripe_count $TDIR/dir1/file$j $expected
4722     done
4723
4724     # lfs_migrate works with lfs find
4725     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4726          $LFS_MIGRATE -y -c $expected"
4727     echo "$cmd"
4728     eval $cmd || error "$cmd failed"
4729
4730     for i in $(seq 2 $NUMFILES); do
4731         check_stripe_count $TDIR/file$i $expected
4732     done
4733     for i in $(seq 2 $NUMDIRS); do
4734         for j in $(seq 1 $NUMFILES); do
4735             check_stripe_count $TDIR/dir$i/file$j $expected
4736         done
4737     done
4738 }
4739 run_test 56w "check lfs_migrate -c stripe_count works"
4740
4741 test_56x() {
4742         check_swap_layouts_support && return 0
4743         [[ $OSTCOUNT -lt 2 ]] &&
4744                 skip_env "need 2 OST, skipping test" && return
4745
4746         local dir0=$DIR/$tdir/$testnum
4747         mkdir -p $dir0 || error "creating dir $dir0"
4748
4749         local ref1=/etc/passwd
4750         local file1=$dir0/file1
4751
4752         $SETSTRIPE -c 2 $file1
4753         cp $ref1 $file1
4754         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4755         stripe=$($GETSTRIPE -c $file1)
4756         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4757         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4758
4759         # clean up
4760         rm -f $file1
4761 }
4762 run_test 56x "lfs migration support"
4763
4764 test_56y() {
4765         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4766                 skip "No HSM support on MDS of $(get_lustre_version)," \
4767                          "need 2.4.53 at least" && return
4768         local res=""
4769
4770         local dir0=$DIR/$tdir/$testnum
4771         mkdir -p $dir0 || error "creating dir $dir0"
4772         local f1=$dir0/file1
4773         local f2=$dir0/file2
4774
4775         touch $f1 || error "creating std file $f1"
4776         $MULTIOP $f2 H2c || error "creating released file $f2"
4777
4778         # a directory can be raid0, so ask only for files
4779         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4780         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4781
4782         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4783         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4784
4785         # only files can be released, so no need to force file search
4786         res=$($LFIND $dir0 -L released)
4787         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4788
4789         res=$($LFIND $dir0 \! -L released)
4790         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4791
4792 }
4793 run_test 56y "lfs find -L raid0|released"
4794
4795 test_57a() {
4796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4797         # note test will not do anything if MDS is not local
4798         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4799                 skip "Only applicable to ldiskfs-based MDTs"
4800                 return
4801         fi
4802
4803         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4804         local MNTDEV="osd*.*MDT*.mntdev"
4805         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4806         [ -z "$DEV" ] && error "can't access $MNTDEV"
4807         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4808                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4809                         error "can't access $DEV"
4810                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4811                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4812                 rm $TMP/t57a.dump
4813         done
4814 }
4815 run_test 57a "verify MDS filesystem created with large inodes =="
4816
4817 test_57b() {
4818         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4819         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4820                 skip "Only applicable to ldiskfs-based MDTs"
4821                 return
4822         fi
4823
4824         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4825         local dir=$DIR/d57b
4826
4827         local FILECOUNT=100
4828         local FILE1=$dir/f1
4829         local FILEN=$dir/f$FILECOUNT
4830
4831         rm -rf $dir || error "removing $dir"
4832         test_mkdir -p $dir || error "creating $dir"
4833         local num=$(get_mds_dir $dir)
4834         local mymds=mds$num
4835
4836         echo "mcreating $FILECOUNT files"
4837         createmany -m $dir/f 1 $FILECOUNT || \
4838                 error "creating files in $dir"
4839
4840         # verify that files do not have EAs yet
4841         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4842         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4843
4844         sync
4845         sleep 1
4846         df $dir  #make sure we get new statfs data
4847         local MDSFREE=$(do_facet $mymds \
4848                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4849         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4850         echo "opening files to create objects/EAs"
4851         local FILE
4852         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4853                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4854         done
4855
4856         # verify that files have EAs now
4857         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4858         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4859
4860         sleep 1  #make sure we get new statfs data
4861         df $dir
4862         local MDSFREE2=$(do_facet $mymds \
4863                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4864         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4865         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
4866                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4867                         error "MDC before $MDCFREE != after $MDCFREE2"
4868                 else
4869                         echo "MDC before $MDCFREE != after $MDCFREE2"
4870                         echo "unable to confirm if MDS has large inodes"
4871                 fi
4872         fi
4873         rm -rf $dir
4874 }
4875 run_test 57b "default LOV EAs are stored inside large inodes ==="
4876
4877 test_58() {
4878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4879         [ -z "$(which wiretest 2>/dev/null)" ] &&
4880                         skip_env "could not find wiretest" && return
4881         wiretest
4882 }
4883 run_test 58 "verify cross-platform wire constants =============="
4884
4885 test_59() {
4886         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4887         echo "touch 130 files"
4888         createmany -o $DIR/f59- 130
4889         echo "rm 130 files"
4890         unlinkmany $DIR/f59- 130
4891         sync
4892         # wait for commitment of removal
4893         wait_delete_completed
4894 }
4895 run_test 59 "verify cancellation of llog records async ========="
4896
4897 TEST60_HEAD="test_60 run $RANDOM"
4898 test_60a() {
4899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4900         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4901         do_facet mgs "! which run-llog.sh &> /dev/null" &&
4902                 skip_env "missing subtest run-llog.sh" && return
4903         log "$TEST60_HEAD - from kernel mode"
4904         do_facet mgs sh run-llog.sh
4905 }
4906 run_test 60a "llog sanity tests run from kernel module =========="
4907
4908 test_60b() { # bug 6411
4909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4910         dmesg > $DIR/$tfile
4911         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4912                                  /llog.test/ {
4913                                          if (marker)
4914                                                  from_marker++
4915                                          from_begin++
4916                                  }
4917                                  END {
4918                                          if (marker)
4919                                                  print from_marker
4920                                          else
4921                                                  print from_begin
4922                                  }"`
4923         [[ $LLOG_COUNT -gt 50 ]] &&
4924                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
4925 }
4926 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4927
4928 test_60c() {
4929         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4930         echo "create 5000 files"
4931         createmany -o $DIR/f60c- 5000
4932 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4933         lctl set_param fail_loc=0x80000137
4934         unlinkmany $DIR/f60c- 5000
4935         lctl set_param fail_loc=0
4936 }
4937 run_test 60c "unlink file when mds full"
4938
4939 test_60d() {
4940         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4941         SAVEPRINTK=$(lctl get_param -n printk)
4942
4943         # verify "lctl mark" is even working"
4944         MESSAGE="test message ID $RANDOM $$"
4945         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4946         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4947
4948         lctl set_param printk=0 || error "set lnet.printk failed"
4949         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4950         MESSAGE="new test message ID $RANDOM $$"
4951         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4952         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4953         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4954
4955         lctl set_param -n printk="$SAVEPRINTK"
4956 }
4957 run_test 60d "test printk console message masking"
4958
4959 test_61() {
4960         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4961         f="$DIR/f61"
4962         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
4963         cancel_lru_locks osc
4964         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
4965         sync
4966 }
4967 run_test 61 "mmap() writes don't make sync hang ================"
4968
4969 # bug 2330 - insufficient obd_match error checking causes LBUG
4970 test_62() {
4971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4972         f="$DIR/f62"
4973         echo foo > $f
4974         cancel_lru_locks osc
4975         lctl set_param fail_loc=0x405
4976         cat $f && error "cat succeeded, expect -EIO"
4977         lctl set_param fail_loc=0
4978 }
4979 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4980 # match every page all of the time.
4981 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4982
4983 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4984 # Though this test is irrelevant anymore, it helped to reveal some
4985 # other grant bugs (LU-4482), let's keep it.
4986 test_63a() {   # was test_63
4987         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4988         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4989         for i in `seq 10` ; do
4990                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4991                 sleep 5
4992                 kill $!
4993                 sleep 1
4994         done
4995
4996         rm -f $DIR/f63 || true
4997 }
4998 run_test 63a "Verify oig_wait interruption does not crash ======="
4999
5000 # bug 2248 - async write errors didn't return to application on sync
5001 # bug 3677 - async write errors left page locked
5002 test_63b() {
5003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5004         debugsave
5005         lctl set_param debug=-1
5006
5007         # ensure we have a grant to do async writes
5008         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5009         rm $DIR/$tfile
5010
5011         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5012         lctl set_param fail_loc=0x80000406
5013         $MULTIOP $DIR/$tfile Owy && \
5014                 error "sync didn't return ENOMEM"
5015         sync; sleep 2; sync     # do a real sync this time to flush page
5016         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5017                 error "locked page left in cache after async error" || true
5018         debugrestore
5019 }
5020 run_test 63b "async write errors should be returned to fsync ==="
5021
5022 test_64a () {
5023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5024         df $DIR
5025         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5026 }
5027 run_test 64a "verify filter grant calculations (in kernel) ====="
5028
5029 test_64b () {
5030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5031         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5032 }
5033 run_test 64b "check out-of-space detection on client ==========="
5034
5035 test_64c() {
5036         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5037 }
5038 run_test 64c "verify grant shrink ========================------"
5039
5040 # bug 1414 - set/get directories' stripe info
5041 test_65a() {
5042         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5043         test_mkdir -p $DIR/$tdir
5044         touch $DIR/$tdir/f1
5045         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5046 }
5047 run_test 65a "directory with no stripe info ===================="
5048
5049 test_65b() {
5050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5051         test_mkdir -p $DIR/$tdir
5052         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5053                                                 error "setstripe"
5054         touch $DIR/$tdir/f2
5055         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5056 }
5057 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5058
5059 test_65c() {
5060         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5061         if [[ $OSTCOUNT -gt 1 ]]; then
5062                 test_mkdir -p $DIR/$tdir
5063                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5064                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5065                 touch $DIR/$tdir/f3
5066                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5067         fi
5068 }
5069 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5070
5071 test_65d() {
5072         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5073         test_mkdir -p $DIR/$tdir
5074         if [[ $STRIPECOUNT -le 0 ]]; then
5075                 sc=1
5076         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5077 #LOV_MAX_STRIPE_COUNT is 2000
5078                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5079         else
5080                 sc=$(($STRIPECOUNT - 1))
5081         fi
5082         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5083         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5084         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5085                 error "lverify failed"
5086 }
5087 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5088
5089 test_65e() {
5090         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5091         test_mkdir -p $DIR/$tdir
5092
5093         $SETSTRIPE $DIR/$tdir || error "setstripe"
5094         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5095                                         error "no stripe info failed"
5096         touch $DIR/$tdir/f6
5097         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5098 }
5099 run_test 65e "directory setstripe defaults ======================="
5100
5101 test_65f() {
5102         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5103         test_mkdir -p $DIR/${tdir}f
5104         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5105 }
5106 run_test 65f "dir setstripe permission (should return error) ==="
5107
5108 test_65g() {
5109         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5110         test_mkdir -p $DIR/$tdir
5111         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5112                                                         error "setstripe"
5113         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5114         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5115                 error "delete default stripe failed"
5116 }
5117 run_test 65g "directory setstripe -d ==========================="
5118
5119 test_65h() {
5120         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5121         test_mkdir -p $DIR/$tdir
5122         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5123                                                         error "setstripe"
5124         test_mkdir -p $DIR/$tdir/dd1
5125         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5126                 error "stripe info inherit failed"
5127 }
5128 run_test 65h "directory stripe info inherit ===================="
5129
5130 test_65i() { # bug6367
5131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5132         $SETSTRIPE -S 65536 -c -1 $MOUNT
5133 }
5134 run_test 65i "set non-default striping on root directory (bug 6367)="
5135
5136 test_65ia() { # bug12836
5137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5138         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5139 }
5140 run_test 65ia "getstripe on -1 default directory striping"
5141
5142 test_65ib() { # bug12836
5143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5144         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5145 }
5146 run_test 65ib "getstripe -v on -1 default directory striping"
5147
5148 test_65ic() { # bug12836
5149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5150         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5151 }
5152 run_test 65ic "new find on -1 default directory striping"
5153
5154 test_65j() { # bug6367
5155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5156         sync; sleep 1
5157         # if we aren't already remounting for each test, do so for this test
5158         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5159                 cleanup || error "failed to unmount"
5160                 setup
5161         fi
5162         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5163 }
5164 run_test 65j "set default striping on root directory (bug 6367)="
5165
5166 test_65k() { # bug11679
5167         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5168         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5169         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5170
5171     echo "Check OST status: "
5172     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5173               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5174
5175     for OSC in $MDS_OSCS; do
5176         echo $OSC "is activate"
5177         do_facet $SINGLEMDS lctl --device %$OSC activate
5178     done
5179
5180     mkdir -p $DIR/$tdir
5181     for INACTIVE_OSC in $MDS_OSCS; do
5182         echo "Deactivate: " $INACTIVE_OSC
5183         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5184         for STRIPE_OSC in $MDS_OSCS; do
5185             OST=`osc_to_ost $STRIPE_OSC`
5186             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5187                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5188
5189             [ -f $DIR/$tdir/$IDX ] && continue
5190             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5191             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5192             RC=$?
5193             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5194         done
5195         rm -f $DIR/$tdir/*
5196         echo $INACTIVE_OSC "is Activate."
5197         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5198     done
5199 }
5200 run_test 65k "validate manual striping works properly with deactivated OSCs"
5201
5202 test_65l() { # bug 12836
5203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5204         test_mkdir -p $DIR/$tdir/test_dir
5205         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5206         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5207 }
5208 run_test 65l "lfs find on -1 stripe dir ========================"
5209
5210 # bug 2543 - update blocks count on client
5211 test_66() {
5212         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5213         COUNT=${COUNT:-8}
5214         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5215         sync; sync_all_data; sync; sync_all_data
5216         cancel_lru_locks osc
5217         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5218         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5219 }
5220 run_test 66 "update inode blocks count on client ==============="
5221
5222 LLOOP=
5223 LLITELOOPLOAD=
5224 cleanup_68() {
5225         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5226         trap 0
5227         if [ ! -z "$LLOOP" ]; then
5228                 if swapon -s | grep -q $LLOOP; then
5229                         swapoff $LLOOP || error "swapoff failed"
5230                 fi
5231
5232                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5233                 rm -f $LLOOP
5234                 unset LLOOP
5235         fi
5236         if [ ! -z "$LLITELOOPLOAD" ]; then
5237                 rmmod llite_lloop
5238                 unset LLITELOOPLOAD
5239         fi
5240         rm -f $DIR/f68*
5241 }
5242
5243 meminfo() {
5244         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5245 }
5246
5247 swap_used() {
5248         swapon -s | awk '($1 == "'$1'") { print $4 }'
5249 }
5250
5251 # test case for lloop driver, basic function
5252 test_68a() {
5253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5254         [ "$UID" != 0 ] && skip_env "must run as root" && return
5255         llite_lloop_enabled || \
5256                 { skip_env "llite_lloop module disabled" && return; }
5257
5258         trap cleanup_68 EXIT
5259
5260         if ! module_loaded llite_lloop; then
5261                 if load_module llite/llite_lloop; then
5262                         LLITELOOPLOAD=yes
5263                 else
5264                         skip_env "can't find module llite_lloop"
5265                         return
5266                 fi
5267         fi
5268
5269         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5270         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5271         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5272
5273         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5274         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5275
5276         cleanup_68
5277 }
5278 run_test 68a "lloop driver - basic test ========================"
5279
5280 # excercise swapping to lustre by adding a high priority swapfile entry
5281 # and then consuming memory until it is used.
5282 test_68b() {  # was test_68
5283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5284         [ "$UID" != 0 ] && skip_env "must run as root" && return
5285         lctl get_param -n devices | grep -q obdfilter && \
5286                 skip "local OST" && return
5287
5288         grep -q llite_lloop /proc/modules
5289         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5290
5291         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5292                 skip "can't reliably test swap with TCP" && return
5293
5294         MEMTOTAL=`meminfo MemTotal`
5295         NR_BLOCKS=$((MEMTOTAL>>8))
5296         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5297
5298         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5299         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5300         mkswap $DIR/f68b
5301
5302         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5303
5304         trap cleanup_68 EXIT
5305
5306         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5307
5308         echo "before: `swapon -s | grep $LLOOP`"
5309         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5310         echo "after: `swapon -s | grep $LLOOP`"
5311         SWAPUSED=`swap_used $LLOOP`
5312
5313         cleanup_68
5314
5315         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5316 }
5317 run_test 68b "support swapping to Lustre ========================"
5318
5319 # bug5265, obdfilter oa2dentry return -ENOENT
5320 # #define OBD_FAIL_OST_ENOENT 0x217
5321 test_69() {
5322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5323         remote_ost_nodsh && skip "remote OST with nodsh" && return
5324
5325         f="$DIR/$tfile"
5326         $SETSTRIPE -c 1 -i 0 $f
5327
5328         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5329
5330         do_facet ost1 lctl set_param fail_loc=0x217
5331         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5332         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5333
5334         do_facet ost1 lctl set_param fail_loc=0
5335         $DIRECTIO write $f 0 2 || error "write error"
5336
5337         cancel_lru_locks osc
5338         $DIRECTIO read $f 0 1 || error "read error"
5339
5340         do_facet ost1 lctl set_param fail_loc=0x217
5341         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5342
5343         do_facet ost1 lctl set_param fail_loc=0
5344         rm -f $f
5345 }
5346 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5347
5348 test_71() {
5349     test_mkdir -p $DIR/$tdir
5350     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5351 }
5352 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5353
5354 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5355         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5356         [ "$RUNAS_ID" = "$UID" ] &&
5357                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5358
5359         # Check that testing environment is properly set up. Skip if not
5360         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5361                 skip_env "User $RUNAS_ID does not exist - skipping"
5362                 return 0
5363         }
5364         # We had better clear the $DIR to get enough space for dd
5365         rm -rf $DIR/*
5366         touch $DIR/$tfile
5367         chmod 777 $DIR/$tfile
5368         chmod ug+s $DIR/$tfile
5369         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5370                 error "$RUNAS dd $DIR/$tfile failed"
5371         # See if we are still setuid/sgid
5372         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5373                 error "S/gid is not dropped on write"
5374         # Now test that MDS is updated too
5375         cancel_lru_locks mdc
5376         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5377                 error "S/gid is not dropped on MDS"
5378         rm -f $DIR/$tfile
5379 }
5380 run_test 72a "Test that remove suid works properly (bug5695) ===="
5381
5382 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5383         local perm
5384
5385         [ "$RUNAS_ID" = "$UID" ] && \
5386                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5387         [ "$RUNAS_ID" -eq 0 ] && \
5388                 skip_env "RUNAS_ID = 0 -- skipping" && return
5389
5390         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5391         # Check that testing environment is properly set up. Skip if not
5392         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5393                 skip_env "User $RUNAS_ID does not exist - skipping"
5394                 return 0
5395         }
5396         touch $DIR/${tfile}-f{g,u}
5397         test_mkdir $DIR/${tfile}-dg
5398         test_mkdir $DIR/${tfile}-du
5399         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5400         chmod g+s $DIR/${tfile}-{f,d}g
5401         chmod u+s $DIR/${tfile}-{f,d}u
5402         for perm in 777 2777 4777; do
5403                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5404                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5405                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5406                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5407         done
5408         true
5409 }
5410 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5411
5412 # bug 3462 - multiple simultaneous MDC requests
5413 test_73() {
5414         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5415         test_mkdir $DIR/d73-1
5416         test_mkdir $DIR/d73-2
5417         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5418         pid1=$!
5419
5420         lctl set_param fail_loc=0x80000129
5421         $MULTIOP $DIR/d73-1/f73-2 Oc &
5422         sleep 1
5423         lctl set_param fail_loc=0
5424
5425         $MULTIOP $DIR/d73-2/f73-3 Oc &
5426         pid3=$!
5427
5428         kill -USR1 $pid1
5429         wait $pid1 || return 1
5430
5431         sleep 25
5432
5433         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5434         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5435         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5436
5437         rm -rf $DIR/d73-*
5438 }
5439 run_test 73 "multiple MDC requests (should not deadlock)"
5440
5441 test_74a() { # bug 6149, 6184
5442         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5443         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5444         #
5445         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5446         # will spin in a tight reconnection loop
5447         touch $DIR/f74a
5448         $LCTL set_param fail_loc=0x8000030e
5449         # get any lock that won't be difficult - lookup works.
5450         ls $DIR/f74a
5451         $LCTL set_param fail_loc=0
5452         rm -f $DIR/f74a
5453         true
5454 }
5455 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5456
5457 test_74b() { # bug 13310
5458         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5459         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5460         #
5461         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5462         # will spin in a tight reconnection loop
5463         $LCTL set_param fail_loc=0x8000030e
5464         # get a "difficult" lock
5465         touch $DIR/f74b
5466         $LCTL set_param fail_loc=0
5467         rm -f $DIR/f74b
5468         true
5469 }
5470 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5471
5472 test_74c() {
5473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5474         #define OBD_FAIL_LDLM_NEW_LOCK
5475         $LCTL set_param fail_loc=0x319
5476         touch $DIR/$tfile && error "touch successful"
5477         $LCTL set_param fail_loc=0
5478         true
5479 }
5480 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5481
5482 num_inodes() {
5483         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5484 }
5485
5486 get_inode_slab_tunables() {
5487         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5488 }
5489
5490 set_inode_slab_tunables() {
5491         echo "lustre_inode_cache $1" > /proc/slabinfo
5492 }
5493
5494 test_76() { # Now for bug 20433, added originally in bug 1443
5495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5496         local SLAB_SETTINGS=`get_inode_slab_tunables`
5497         local CPUS=`getconf _NPROCESSORS_ONLN`
5498         # we cannot set limit below 1 which means 1 inode in each
5499         # per-cpu cache is still allowed
5500         set_inode_slab_tunables "1 1 0"
5501         cancel_lru_locks osc
5502         BEFORE_INODES=$(num_inodes)
5503         echo "before inodes: $BEFORE_INODES"
5504         local COUNT=1000
5505         [ "$SLOW" = "no" ] && COUNT=100
5506         for i in $(seq $COUNT); do
5507                 touch $DIR/$tfile
5508                 rm -f $DIR/$tfile
5509         done
5510         cancel_lru_locks osc
5511         AFTER_INODES=$(num_inodes)
5512         echo "after inodes: $AFTER_INODES"
5513         local wait=0
5514         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5515                 sleep 2
5516                 AFTER_INODES=$(num_inodes)
5517                 wait=$((wait+2))
5518                 echo "wait $wait seconds inodes: $AFTER_INODES"
5519                 if [ $wait -gt 30 ]; then
5520                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5521                 fi
5522         done
5523         set_inode_slab_tunables "$SLAB_SETTINGS"
5524 }
5525 run_test 76 "confirm clients recycle inodes properly ===="
5526
5527
5528 export ORIG_CSUM=""
5529 set_checksums()
5530 {
5531         # Note: in sptlrpc modes which enable its own bulk checksum, the
5532         # original crc32_le bulk checksum will be automatically disabled,
5533         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5534         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5535         # In this case set_checksums() will not be no-op, because sptlrpc
5536         # bulk checksum will be enabled all through the test.
5537
5538         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5539         lctl set_param -n osc.*.checksums $1
5540         return 0
5541 }
5542
5543 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5544                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5545 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5546 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5547 set_checksum_type()
5548 {
5549         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5550         log "set checksum type to $1"
5551         return 0
5552 }
5553 F77_TMP=$TMP/f77-temp
5554 F77SZ=8
5555 setup_f77() {
5556         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5557                 error "error writing to $F77_TMP"
5558 }
5559
5560 test_77a() { # bug 10889
5561         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5562         $GSS && skip "could not run with gss" && return
5563         [ ! -f $F77_TMP ] && setup_f77
5564         set_checksums 1
5565         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5566         set_checksums 0
5567         rm -f $DIR/$tfile
5568 }
5569 run_test 77a "normal checksum read/write operation"
5570
5571 test_77b() { # bug 10889
5572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5573         $GSS && skip "could not run with gss" && return
5574         [ ! -f $F77_TMP ] && setup_f77
5575         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5576         $LCTL set_param fail_loc=0x80000409
5577         set_checksums 1
5578
5579         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5580                 error "dd error: $?"
5581         $LCTL set_param fail_loc=0
5582
5583         for algo in $CKSUM_TYPES; do
5584                 cancel_lru_locks osc
5585                 set_checksum_type $algo
5586                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5587                 $LCTL set_param fail_loc=0x80000408
5588                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5589                 $LCTL set_param fail_loc=0
5590         done
5591         set_checksums 0
5592         set_checksum_type $ORIG_CSUM_TYPE
5593         rm -f $DIR/$tfile
5594 }
5595 run_test 77b "checksum error on client write, read"
5596
5597 test_77d() { # bug 10889
5598         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5599         $GSS && skip "could not run with gss" && return
5600         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5601         $LCTL set_param fail_loc=0x80000409
5602         set_checksums 1
5603         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5604                 error "direct write: rc=$?"
5605         $LCTL set_param fail_loc=0
5606         set_checksums 0
5607
5608         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5609         $LCTL set_param fail_loc=0x80000408
5610         set_checksums 1
5611         cancel_lru_locks osc
5612         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5613                 error "direct read: rc=$?"
5614         $LCTL set_param fail_loc=0
5615         set_checksums 0
5616 }
5617 run_test 77d "checksum error on OST direct write, read"
5618
5619 test_77f() { # bug 10889
5620         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5621         $GSS && skip "could not run with gss" && return
5622         set_checksums 1
5623         for algo in $CKSUM_TYPES; do
5624                 cancel_lru_locks osc
5625                 set_checksum_type $algo
5626                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5627                 $LCTL set_param fail_loc=0x409
5628                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5629                         error "direct write succeeded"
5630                 $LCTL set_param fail_loc=0
5631         done
5632         set_checksum_type $ORIG_CSUM_TYPE
5633         set_checksums 0
5634 }
5635 run_test 77f "repeat checksum error on write (expect error)"
5636
5637 test_77g() { # bug 10889
5638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5639         $GSS && skip "could not run with gss" && return
5640         remote_ost_nodsh && skip "remote OST with nodsh" && return
5641
5642         [ ! -f $F77_TMP ] && setup_f77
5643
5644         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5645         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5646         do_facet ost1 lctl set_param fail_loc=0x8000021a
5647         set_checksums 1
5648         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5649                 error "write error: rc=$?"
5650         do_facet ost1 lctl set_param fail_loc=0
5651         set_checksums 0
5652
5653         cancel_lru_locks osc
5654         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5655         do_facet ost1 lctl set_param fail_loc=0x8000021b
5656         set_checksums 1
5657         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5658         do_facet ost1 lctl set_param fail_loc=0
5659         set_checksums 0
5660 }
5661 run_test 77g "checksum error on OST write, read"
5662
5663 test_77i() { # bug 13805
5664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5665         $GSS && skip "could not run with gss" && return
5666         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5667         lctl set_param fail_loc=0x40b
5668         remount_client $MOUNT
5669         lctl set_param fail_loc=0
5670         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5671                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5672                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5673                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5674         done
5675         remount_client $MOUNT
5676 }
5677 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5678
5679 test_77j() { # bug 13805
5680         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5681         $GSS && skip "could not run with gss" && return
5682         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5683         lctl set_param fail_loc=0x40c
5684         remount_client $MOUNT
5685         lctl set_param fail_loc=0
5686         sleep 2 # wait async osc connect to finish
5687         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5688                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5689                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5690                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5691         done
5692         remount_client $MOUNT
5693 }
5694 run_test 77j "client only supporting ADLER32"
5695
5696 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5697 rm -f $F77_TMP
5698 unset F77_TMP
5699
5700 test_78() { # bug 10901
5701         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5702         remote_ost || { skip_env "local OST" && return; }
5703
5704         NSEQ=5
5705         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5706         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5707         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5708         echo "MemTotal: $MEMTOTAL"
5709
5710         # reserve 256MB of memory for the kernel and other running processes,
5711         # and then take 1/2 of the remaining memory for the read/write buffers.
5712         if [ $MEMTOTAL -gt 512 ] ;then
5713                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5714         else
5715                 # for those poor memory-starved high-end clusters...
5716                 MEMTOTAL=$((MEMTOTAL / 2))
5717         fi
5718         echo "Mem to use for directio: $MEMTOTAL"
5719
5720         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5721         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5722         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5723         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5724                 head -n1)
5725         echo "Smallest OST: $SMALLESTOST"
5726         [[ $SMALLESTOST -lt 10240 ]] &&
5727                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5728
5729         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5730                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5731
5732         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5733         echo "File size: $F78SIZE"
5734         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5735         for i in $(seq 1 $NSEQ); do
5736                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5737                 echo directIO rdwr round $i of $NSEQ
5738                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5739         done
5740
5741         rm -f $DIR/$tfile
5742 }
5743 run_test 78 "handle large O_DIRECT writes correctly ============"
5744
5745 test_79() { # bug 12743
5746         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5747         wait_delete_completed
5748
5749         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5750         BKFREE=$(calc_osc_kbytes kbytesfree)
5751         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5752
5753         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5754         DFTOTAL=`echo $STRING | cut -d, -f1`
5755         DFUSED=`echo $STRING  | cut -d, -f2`
5756         DFAVAIL=`echo $STRING | cut -d, -f3`
5757         DFFREE=$(($DFTOTAL - $DFUSED))
5758
5759         ALLOWANCE=$((64 * $OSTCOUNT))
5760
5761         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5762            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5763                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5764         fi
5765         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5766            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5767                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5768         fi
5769         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5770            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5771                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5772         fi
5773 }
5774 run_test 79 "df report consistency check ======================="
5775
5776 test_80() { # bug 10718
5777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5778         # relax strong synchronous semantics for slow backends like ZFS
5779         local soc="obdfilter.*.sync_on_lock_cancel"
5780         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5781         local hosts=
5782         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5783                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5784                           facet_active_host $host; done | sort -u)
5785                 do_nodes $hosts lctl set_param $soc=never
5786         fi
5787
5788         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5789         sync; sleep 1; sync
5790         local BEFORE=`date +%s`
5791         cancel_lru_locks osc
5792         local AFTER=`date +%s`
5793         local DIFF=$((AFTER-BEFORE))
5794         if [ $DIFF -gt 1 ] ; then
5795                 error "elapsed for 1M@1T = $DIFF"
5796         fi
5797
5798         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5799
5800         rm -f $DIR/$tfile
5801 }
5802 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5803
5804 test_81a() { # LU-456
5805         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5806         remote_ost_nodsh && skip "remote OST with nodsh" && return
5807         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5808         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5809         do_facet ost1 lctl set_param fail_loc=0x80000228
5810
5811         # write should trigger a retry and success
5812         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5813         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5814         RC=$?
5815         if [ $RC -ne 0 ] ; then
5816                 error "write should success, but failed for $RC"
5817         fi
5818 }
5819 run_test 81a "OST should retry write when get -ENOSPC ==============="
5820
5821 test_81b() { # LU-456
5822         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5823         remote_ost_nodsh && skip "remote OST with nodsh" && return
5824         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5825         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5826         do_facet ost1 lctl set_param fail_loc=0x228
5827
5828         # write should retry several times and return -ENOSPC finally
5829         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5830         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5831         RC=$?
5832         ENOSPC=28
5833         if [ $RC -ne $ENOSPC ] ; then
5834                 error "dd should fail for -ENOSPC, but succeed."
5835         fi
5836 }
5837 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5838
5839 test_82() { # LU-1031
5840         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5841         local gid1=14091995
5842         local gid2=16022000
5843
5844         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5845         local MULTIPID1=$!
5846         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5847         local MULTIPID2=$!
5848         kill -USR1 $MULTIPID2
5849         sleep 2
5850         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5851                 error "First grouplock does not block second one"
5852         else
5853                 echo "Second grouplock blocks first one"
5854         fi
5855         kill -USR1 $MULTIPID1
5856         wait $MULTIPID1
5857         wait $MULTIPID2
5858 }
5859 run_test 82 "Basic grouplock test ==============================="
5860
5861 test_99a() {
5862         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5863                 return
5864         test_mkdir -p $DIR/d99cvsroot
5865         chown $RUNAS_ID $DIR/d99cvsroot
5866         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5867         cd $TMP
5868
5869         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5870         cd $oldPWD
5871 }
5872 run_test 99a "cvs init ========================================="
5873
5874 test_99b() {
5875         [ -z "$(which cvs 2>/dev/null)" ] &&
5876                 skip_env "could not find cvs" && return
5877         [ ! -d $DIR/d99cvsroot ] && test_99a
5878         cd /etc/init.d
5879         # some versions of cvs import exit(1) when asked to import links or
5880         # files they can't read.  ignore those files.
5881         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5882                         ! -perm +4 -printf '-I %f\n')
5883         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5884                 d99reposname vtag rtag
5885 }
5886 run_test 99b "cvs import ======================================="
5887
5888 test_99c() {
5889         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5890         [ ! -d $DIR/d99cvsroot ] && test_99b
5891         cd $DIR
5892         test_mkdir -p $DIR/d99reposname
5893         chown $RUNAS_ID $DIR/d99reposname
5894         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5895 }
5896 run_test 99c "cvs checkout ====================================="
5897
5898 test_99d() {
5899         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5900         [ ! -d $DIR/d99cvsroot ] && test_99c
5901         cd $DIR/d99reposname
5902         $RUNAS touch foo99
5903         $RUNAS cvs add -m 'addmsg' foo99
5904 }
5905 run_test 99d "cvs add =========================================="
5906
5907 test_99e() {
5908         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5909         [ ! -d $DIR/d99cvsroot ] && test_99c
5910         cd $DIR/d99reposname
5911         $RUNAS cvs update
5912 }
5913 run_test 99e "cvs update ======================================="
5914
5915 test_99f() {
5916         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5917         [ ! -d $DIR/d99cvsroot ] && test_99d
5918         cd $DIR/d99reposname
5919         $RUNAS cvs commit -m 'nomsg' foo99
5920     rm -fr $DIR/d99cvsroot
5921 }
5922 run_test 99f "cvs commit ======================================="
5923
5924 test_100() {
5925         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5926         [ "$NETTYPE" = tcp ] || \
5927                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5928                         return ; }
5929
5930         remote_ost_nodsh && skip "remote OST with nodsh" && return
5931         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5932         remote_servers || \
5933                 { skip "useless for local single node setup" && return; }
5934
5935         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5936                 [ "$PROT" != "tcp" ] && continue
5937                 RPORT=$(echo $REMOTE | cut -d: -f2)
5938                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5939
5940                 rc=0
5941                 LPORT=`echo $LOCAL | cut -d: -f2`
5942                 if [ $LPORT -ge 1024 ]; then
5943                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5944                         netstat -tna
5945                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5946                 fi
5947         done
5948         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5949 }
5950 run_test 100 "check local port using privileged port ==========="
5951
5952 function get_named_value()
5953 {
5954     local tag
5955
5956     tag=$1
5957     while read ;do
5958         line=$REPLY
5959         case $line in
5960         $tag*)
5961             echo $line | sed "s/^$tag[ ]*//"
5962             break
5963             ;;
5964         esac
5965     done
5966 }
5967
5968 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5969                    awk '/^max_cached_mb/ { print $2 }')
5970
5971 cleanup_101a() {
5972         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5973         trap 0
5974 }
5975
5976 test_101a() {
5977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5978         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
5979         local s
5980         local discard
5981         local nreads=10000
5982         local cache_limit=32
5983
5984         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5985         trap cleanup_101a EXIT
5986         $LCTL set_param -n llite.*.read_ahead_stats 0
5987         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5988
5989         #
5990         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5991         #
5992         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5993         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5994
5995         discard=0
5996         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
5997                 get_named_value 'read but discarded' | cut -d" " -f1); do
5998                         discard=$(($discard + $s))
5999         done
6000         cleanup_101a
6001
6002         if [[ $(($discard * 10)) -gt $nreads ]]; then
6003                 $LCTL get_param osc.*-osc*.rpc_stats
6004                 $LCTL get_param llite.*.read_ahead_stats
6005                 error "too many ($discard) discarded pages"
6006         fi
6007         rm -f $DIR/$tfile || true
6008 }
6009 run_test 101a "check read-ahead for random reads ================"
6010
6011 setup_test101bc() {
6012         test_mkdir -p $DIR/$tdir
6013         local STRIPE_SIZE=$1
6014         local FILE_LENGTH=$2
6015         STRIPE_OFFSET=0
6016
6017         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6018
6019         local list=$(comma_list $(osts_nodes))
6020         set_osd_param $list '' read_cache_enable 0
6021         set_osd_param $list '' writethrough_cache_enable 0
6022
6023         trap cleanup_test101bc EXIT
6024         # prepare the read-ahead file
6025         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6026
6027         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6028                                 count=$FILE_SIZE_MB 2> /dev/null
6029
6030 }
6031
6032 cleanup_test101bc() {
6033         trap 0
6034         rm -rf $DIR/$tdir
6035         rm -f $DIR/$tfile
6036
6037         local list=$(comma_list $(osts_nodes))
6038         set_osd_param $list '' read_cache_enable 1
6039         set_osd_param $list '' writethrough_cache_enable 1
6040 }
6041
6042 calc_total() {
6043         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6044 }
6045
6046 ra_check_101() {
6047         local READ_SIZE=$1
6048         local STRIPE_SIZE=$2
6049         local FILE_LENGTH=$3
6050         local RA_INC=1048576
6051         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6052         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6053                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6054         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6055                         get_named_value 'read but discarded' |
6056                         cut -d" " -f1 | calc_total)
6057         if [[ $DISCARD -gt $discard_limit ]]; then
6058                 $LCTL get_param llite.*.read_ahead_stats
6059                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6060         else
6061                 echo "Read-ahead success for size ${READ_SIZE}"
6062         fi
6063 }
6064
6065 test_101b() {
6066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6067         [[ $OSTCOUNT -lt 2 ]] &&
6068                 skip_env "skipping stride IO stride-ahead test" && return
6069         local STRIPE_SIZE=1048576
6070         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6071         if [ $SLOW == "yes" ]; then
6072                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6073         else
6074                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6075         fi
6076
6077         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6078
6079         # prepare the read-ahead file
6080         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6081         cancel_lru_locks osc
6082         for BIDX in 2 4 8 16 32 64 128 256
6083         do
6084                 local BSIZE=$((BIDX*4096))
6085                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6086                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6087                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6088                 $LCTL set_param -n llite.*.read_ahead_stats 0
6089                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6090                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6091                 cancel_lru_locks osc
6092                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6093         done
6094         cleanup_test101bc
6095         true
6096 }
6097 run_test 101b "check stride-io mode read-ahead ================="
6098
6099 test_101c() {
6100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6101         local STRIPE_SIZE=1048576
6102         local FILE_LENGTH=$((STRIPE_SIZE*100))
6103         local nreads=10000
6104         local osc_rpc_stats
6105
6106         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6107
6108         cancel_lru_locks osc
6109         $LCTL set_param osc.*.rpc_stats 0
6110         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6111         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6112                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6113                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6114                 local size
6115
6116                 if [ $lines -le 20 ]; then
6117                         continue
6118                 fi
6119                 for size in 1 2 4 8; do
6120                         local rpc=$(echo "$stats" |
6121                                     awk '($1 == "'$size':") {print $2; exit; }')
6122                         [ $rpc != 0 ] &&
6123                                 error "Small $((size*4))k read IO $rpc !"
6124                 done
6125                 echo "$osc_rpc_stats check passed!"
6126         done
6127         cleanup_test101bc
6128         true
6129 }
6130 run_test 101c "check stripe_size aligned read-ahead ================="
6131
6132 set_read_ahead() {
6133         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6134         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6135 }
6136
6137 test_101d() {
6138         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6139         local file=$DIR/$tfile
6140         local sz_MB=${FILESIZE_101d:-500}
6141         local ra_MB=${READAHEAD_MB:-40}
6142
6143         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6144         [ $free_MB -lt $sz_MB ] &&
6145                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6146
6147         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6148         $SETSTRIPE -c -1 $file || error "setstripe failed"
6149
6150         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6151         echo Cancel LRU locks on lustre client to flush the client cache
6152         cancel_lru_locks osc
6153
6154         echo Disable read-ahead
6155         local old_READAHEAD=$(set_read_ahead 0)
6156
6157         echo Reading the test file $file with read-ahead disabled
6158         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6159
6160         echo Cancel LRU locks on lustre client to flush the client cache
6161         cancel_lru_locks osc
6162         echo Enable read-ahead with ${ra_MB}MB
6163         set_read_ahead $ra_MB
6164
6165         echo Reading the test file $file with read-ahead enabled
6166         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6167
6168         echo "read-ahead disabled time read $raOFF"
6169         echo "read-ahead enabled  time read $raON"
6170
6171         set_read_ahead $old_READAHEAD
6172         rm -f $file
6173         wait_delete_completed
6174
6175         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6176                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6177 }
6178 run_test 101d "file read with and without read-ahead enabled"
6179
6180 test_101e() {
6181         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6182         local file=$DIR/$tfile
6183         local size_KB=500  #KB
6184         local count=100
6185         local bsize=1024
6186
6187         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6188         local need_KB=$((count * size_KB))
6189         [[ $free_KB -le $need_KB ]] &&
6190                 skip_env "Need free space $need_KB, have $free_KB" && return
6191
6192         echo "Creating $count ${size_KB}K test files"
6193         for ((i = 0; i < $count; i++)); do
6194                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6195         done
6196
6197         echo "Cancel LRU locks on lustre client to flush the client cache"
6198         cancel_lru_locks osc
6199
6200         echo "Reset readahead stats"
6201         $LCTL set_param -n llite.*.read_ahead_stats 0
6202
6203         for ((i = 0; i < $count; i++)); do
6204                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6205         done
6206
6207         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6208                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6209
6210         for ((i = 0; i < $count; i++)); do
6211                 rm -rf $file.$i 2>/dev/null
6212         done
6213
6214         #10000 means 20% reads are missing in readahead
6215         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6216 }
6217 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6218
6219 cleanup_test101f() {
6220     trap 0
6221     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6222     rm -rf $DIR/$tfile 2>/dev/null
6223 }
6224
6225 test_101f() {
6226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6227     local file=$DIR/$tfile
6228     local nreads=1000
6229
6230     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6231     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6232     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6233     trap cleanup_test101f EXIT
6234
6235     echo Cancel LRU locks on lustre client to flush the client cache
6236     cancel_lru_locks osc
6237
6238     echo Reset readahead stats
6239     $LCTL set_param -n llite.*.read_ahead_stats 0
6240     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6241     # readahead should read in 2M file on second read, so only miss
6242     # 2 pages.
6243     echo Random 4K reads on 2M file for 1000 times
6244     $READS -f $file -s 2097152 -b 4096 -n $nreads
6245
6246     echo checking missing pages
6247     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6248           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6249
6250     [ $miss -lt 3 ] || error "misses too much pages!"
6251     cleanup_test101f
6252 }
6253 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6254
6255 setup_test102() {
6256         test_mkdir -p $DIR/$tdir
6257         chown $RUNAS_ID $DIR/$tdir
6258         STRIPE_SIZE=65536
6259         STRIPE_OFFSET=1
6260         STRIPE_COUNT=$OSTCOUNT
6261         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6262
6263         trap cleanup_test102 EXIT
6264         cd $DIR
6265         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6266         cd $DIR/$tdir
6267         for num in 1 2 3 4; do
6268                 for count in $(seq 1 $STRIPE_COUNT); do
6269                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6270                                 local size=`expr $STRIPE_SIZE \* $num`
6271                                 local file=file"$num-$idx-$count"
6272                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6273                         done
6274                 done
6275         done
6276
6277         cd $DIR
6278         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6279 }
6280
6281 cleanup_test102() {
6282         trap 0
6283         rm -f $TMP/f102.tar
6284         rm -rf $DIR/d0.sanity/d102
6285 }
6286
6287 test_102a() {
6288         local testfile=$DIR/xattr_testfile
6289
6290         touch $testfile
6291
6292         [ "$UID" != 0 ] && skip_env "must run as root" && return
6293         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6294                 skip_env "must have user_xattr" && return
6295
6296         [ -z "$(which setfattr 2>/dev/null)" ] &&
6297                 skip_env "could not find setfattr" && return
6298
6299         echo "set/get xattr..."
6300         setfattr -n trusted.name1 -v value1 $testfile ||
6301                 error "setfattr -n trusted.name1=value1 $testfile failed"
6302         getfattr -n trusted.name1 $testfile 2> /dev/null |
6303           grep "trusted.name1=.value1" ||
6304                 error "$testfile missing trusted.name1=value1"
6305
6306         setfattr -n user.author1 -v author1 $testfile ||
6307                 error "setfattr -n user.author1=author1 $testfile failed"
6308         getfattr -n user.author1 $testfile 2> /dev/null |
6309           grep "user.author1=.author1" ||
6310                 error "$testfile missing trusted.author1=author1"
6311
6312         echo "listxattr..."
6313         setfattr -n trusted.name2 -v value2 $testfile ||
6314                 error "$testfile unable to set trusted.name2"
6315         setfattr -n trusted.name3 -v value3 $testfile ||
6316                 error "$testfile unable to set trusted.name3"
6317         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6318             grep "trusted.name" | wc -l) -eq 3 ] ||
6319                 error "$testfile missing 3 trusted.name xattrs"
6320
6321         setfattr -n user.author2 -v author2 $testfile ||
6322                 error "$testfile unable to set user.author2"
6323         setfattr -n user.author3 -v author3 $testfile ||
6324                 error "$testfile unable to set user.author3"
6325         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6326             grep "user.author" | wc -l) -eq 3 ] ||
6327                 error "$testfile missing 3 user.author xattrs"
6328
6329         echo "remove xattr..."
6330         setfattr -x trusted.name1 $testfile ||
6331                 error "$testfile error deleting trusted.name1"
6332         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6333                 error "$testfile did not delete trusted.name1 xattr"
6334
6335         setfattr -x user.author1 $testfile ||
6336                 error "$testfile error deleting user.author1"
6337         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6338                 error "$testfile did not delete trusted.name1 xattr"
6339
6340         # b10667: setting lustre special xattr be silently discarded
6341         echo "set lustre special xattr ..."
6342         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6343                 error "$testfile allowed setting trusted.lov"
6344 }
6345 run_test 102a "user xattr test =================================="
6346
6347 test_102b() {
6348         [ -z "$(which setfattr 2>/dev/null)" ] &&
6349                 skip_env "could not find setfattr" && return
6350
6351         # b10930: get/set/list trusted.lov xattr
6352         echo "get/set/list trusted.lov xattr ..."
6353         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6354         local testfile=$DIR/$tfile
6355         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6356                 error "setstripe failed"
6357         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6358                 error "getstripe failed"
6359         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6360                 error "can't get trusted.lov from $testfile"
6361
6362         local testfile2=${testfile}2
6363         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6364                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6365
6366         $MCREATE $testfile2
6367         setfattr -n trusted.lov -v $value $testfile2
6368         local stripe_size=$($GETSTRIPE -S $testfile2)
6369         local stripe_count=$($GETSTRIPE -c $testfile2)
6370         [[ $stripe_size -eq 65536 ]] ||
6371                 error "stripe size $stripe_size != 65536"
6372         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6373                 error "stripe count $stripe_count != $STRIPECOUNT"
6374         rm -f $DIR/$tfile
6375 }
6376 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6377
6378 test_102c() {
6379         [ -z "$(which setfattr 2>/dev/null)" ] &&
6380                 skip_env "could not find setfattr" && return
6381
6382         # b10930: get/set/list lustre.lov xattr
6383         echo "get/set/list lustre.lov xattr ..."
6384         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6385         test_mkdir -p $DIR/$tdir
6386         chown $RUNAS_ID $DIR/$tdir
6387         local testfile=$DIR/$tdir/$tfile
6388         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6389                 error "setstripe failed"
6390         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6391                 error "getstripe failed"
6392         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6393         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6394
6395         local testfile2=${testfile}2
6396         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6397                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6398
6399         $RUNAS $MCREATE $testfile2
6400         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6401         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6402         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6403         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6404         [ $stripe_count -eq $STRIPECOUNT ] ||
6405                 error "stripe count $stripe_count != $STRIPECOUNT"
6406 }
6407 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6408
6409 compare_stripe_info1() {
6410         local stripe_index_all_zero=true
6411
6412         for num in 1 2 3 4; do
6413                 for count in $(seq 1 $STRIPE_COUNT); do
6414                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6415                                 local size=$((STRIPE_SIZE * num))
6416                                 local file=file"$num-$offset-$count"
6417                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6418                                 [[ $stripe_size -ne $size ]] &&
6419                                     error "$file: size $stripe_size != $size"
6420                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6421                                 # allow fewer stripes to be created, ORI-601
6422                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6423                                     error "$file: count $stripe_count != $count"
6424                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6425                                 [[ $stripe_index -ne 0 ]] &&
6426                                         stripe_index_all_zero=false
6427                         done
6428                 done
6429         done
6430         $stripe_index_all_zero &&
6431                 error "all files are being extracted starting from OST index 0"
6432         return 0
6433 }
6434
6435 find_lustre_tar() {
6436         [ -n "$(which tar 2>/dev/null)" ] &&
6437                 strings $(which tar) | grep -q "lustre" && echo tar
6438 }
6439
6440 test_102d() {
6441         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6442         # b10930: tar test for trusted.lov xattr
6443         TAR=$(find_lustre_tar)
6444         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6445         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6446         setup_test102
6447         test_mkdir -p $DIR/d102d
6448         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6449         cd $DIR/d102d/$tdir
6450         compare_stripe_info1
6451 }
6452 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6453
6454 test_102f() {
6455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6456         # b10930: tar test for trusted.lov xattr
6457         TAR=$(find_lustre_tar)
6458         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6459         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6460         setup_test102
6461         test_mkdir -p $DIR/d102f
6462         cd $DIR
6463         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6464         cd $DIR/d102f/$tdir
6465         compare_stripe_info1
6466 }
6467 run_test 102f "tar copy files, not keep osts ==========="
6468
6469 grow_xattr() {
6470         local xsize=${1:-1024}  # in bytes
6471         local file=$DIR/$tfile
6472
6473         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6474                 skip "must have user_xattr" && return 0
6475         [ -z "$(which setfattr 2>/dev/null)" ] &&
6476                 skip_env "could not find setfattr" && return 0
6477         [ -z "$(which getfattr 2>/dev/null)" ] &&
6478                 skip_env "could not find getfattr" && return 0
6479
6480         touch $file
6481
6482         local value="$(generate_string $xsize)"
6483
6484         local xbig=trusted.big
6485         log "save $xbig on $file"
6486         setfattr -n $xbig -v $value $file ||
6487                 error "saving $xbig on $file failed"
6488
6489         local orig=$(get_xattr_value $xbig $file)
6490         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6491
6492         local xsml=trusted.sml
6493         log "save $xsml on $file"
6494         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6495
6496         local new=$(get_xattr_value $xbig $file)
6497         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6498
6499         log "grow $xsml on $file"
6500         setfattr -n $xsml -v "$value" $file ||
6501                 error "growing $xsml on $file failed"
6502
6503         new=$(get_xattr_value $xbig $file)
6504         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6505         log "$xbig still valid after growing $xsml"
6506
6507         rm -f $file
6508 }
6509
6510 test_102h() { # bug 15777
6511         grow_xattr 1024
6512 }
6513 run_test 102h "grow xattr from inside inode to external block"
6514
6515 test_102ha() {
6516         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6517         grow_xattr $(max_xattr_size)
6518 }
6519 run_test 102ha "grow xattr from inside inode to external inode"
6520
6521 test_102i() { # bug 17038
6522         [ -z "$(which getfattr 2>/dev/null)" ] &&
6523                 skip "could not find getfattr" && return
6524         touch $DIR/$tfile
6525         ln -s $DIR/$tfile $DIR/${tfile}link
6526         getfattr -n trusted.lov $DIR/$tfile ||
6527                 error "lgetxattr on $DIR/$tfile failed"
6528         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6529                 grep -i "no such attr" ||
6530                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6531         rm -f $DIR/$tfile $DIR/${tfile}link
6532 }
6533 run_test 102i "lgetxattr test on symbolic link ============"
6534
6535 test_102j() {
6536         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6537         TAR=$(find_lustre_tar)
6538         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6539         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6540         setup_test102 "$RUNAS"
6541         test_mkdir -p $DIR/d102j
6542         chown $RUNAS_ID $DIR/d102j
6543         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6544         cd $DIR/d102j/$tdir
6545         compare_stripe_info1 "$RUNAS"
6546 }
6547 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6548
6549 test_102k() {
6550         [ -z "$(which setfattr 2>/dev/null)" ] &&
6551                 skip "could not find setfattr" && return
6552         touch $DIR/$tfile
6553         # b22187 just check that does not crash for regular file.
6554         setfattr -n trusted.lov $DIR/$tfile
6555         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6556         local test_kdir=$DIR/d102k
6557         test_mkdir $test_kdir
6558         local default_size=`$GETSTRIPE -S $test_kdir`
6559         local default_count=`$GETSTRIPE -c $test_kdir`
6560         local default_offset=`$GETSTRIPE -i $test_kdir`
6561         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6562                 error 'dir setstripe failed'
6563         setfattr -n trusted.lov $test_kdir
6564         local stripe_size=`$GETSTRIPE -S $test_kdir`
6565         local stripe_count=`$GETSTRIPE -c $test_kdir`
6566         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6567         [ $stripe_size -eq $default_size ] ||
6568                 error "stripe size $stripe_size != $default_size"
6569         [ $stripe_count -eq $default_count ] ||
6570                 error "stripe count $stripe_count != $default_count"
6571         [ $stripe_offset -eq $default_offset ] ||
6572                 error "stripe offset $stripe_offset != $default_offset"
6573         rm -rf $DIR/$tfile $test_kdir
6574 }
6575 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6576
6577 test_102l() {
6578         [ -z "$(which getfattr 2>/dev/null)" ] &&
6579                 skip "could not find getfattr" && return
6580
6581         # LU-532 trusted. xattr is invisible to non-root
6582         local testfile=$DIR/$tfile
6583
6584         touch $testfile
6585
6586         echo "listxattr as user..."
6587         chown $RUNAS_ID $testfile
6588         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6589             grep -q "trusted" &&
6590                 error "$testfile trusted xattrs are user visible"
6591
6592         return 0;
6593 }
6594 run_test 102l "listxattr size test =================================="
6595
6596 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6597         local path=$DIR/$tfile
6598         touch $path
6599
6600         listxattr_size_check $path || error "listattr_size_check $path failed"
6601 }
6602 run_test 102m "Ensure listxattr fails on small bufffer ========"
6603
6604 cleanup_test102
6605
6606 getxattr() { # getxattr path name
6607         # Return the base64 encoding of the value of xattr name on path.
6608         local path=$1
6609         local name=$2
6610
6611         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6612         # file: $path
6613         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6614         #
6615         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6616
6617         getfattr --absolute-names --encoding=base64 --name=$name $path |
6618                 awk -F= -v name=$name '$1 == name {
6619                         print substr($0, index($0, "=") + 1);
6620         }'
6621 }
6622
6623 test_102n() { # LU-4101 mdt: protect internal xattrs
6624         local file0=$DIR/$tfile.0
6625         local file1=$DIR/$tfile.1
6626         local xattr0=$TMP/$tfile.0
6627         local xattr1=$TMP/$tfile.1
6628         local name
6629         local value
6630
6631         [ -z "$(which setfattr 2>/dev/null)" ] &&
6632                 skip "could not find setfattr" && return
6633
6634         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6635         then
6636                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6637                 return
6638         fi
6639
6640         rm -rf $file0 $file1 $xattr0 $xattr1
6641         touch $file0 $file1
6642
6643         # Get 'before' xattrs of $file1.
6644         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6645
6646         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6647                 # Try to copy xattr from $file0 to $file1.
6648                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6649
6650                 setfattr --name=trusted.$name --value="$value" $file1 ||
6651                         error "setxattr 'trusted.$name' failed"
6652
6653                 # Try to set a garbage xattr.
6654                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6655
6656                 setfattr --name=trusted.$name --value="$value" $file1 ||
6657                         error "setxattr 'trusted.$name' failed"
6658
6659                 # Try to remove the xattr from $file1. We don't care if this
6660                 # appears to succeed or fail, we just don't want there to be
6661                 # any changes or crashes.
6662                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6663         done
6664
6665         # Get 'after' xattrs of file1.
6666         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6667
6668         if ! diff $xattr0 $xattr1; then
6669                 error "before and after xattrs of '$file1' differ"
6670         fi
6671
6672         rm -rf $file0 $file1 $xattr0 $xattr1
6673
6674         return 0
6675 }
6676 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6677
6678 test_102p() { # LU-4703 setxattr did not check ownership
6679         local testfile=$DIR/$tfile
6680
6681         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6682                 skip "MDS needs to be at least 2.5.56" && return
6683
6684         touch $testfile
6685
6686         echo "setfacl as user..."
6687         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6688         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6689
6690         echo "setfattr as user..."
6691         setfacl -m "u:$RUNAS_ID:---" $testfile
6692         $RUNAS setfattr -x system.posix_acl_access $testfile
6693         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6694 }
6695 run_test 102p "check setxattr(2) correctly fails without permission"
6696
6697 run_acl_subtest()
6698 {
6699     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6700     return $?
6701 }
6702
6703 test_103 () {
6704         [ "$UID" != 0 ] && skip_env "must run as root" && return
6705         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6706                 skip "must have acl enabled" && return
6707         [ -z "$(which setfacl 2>/dev/null)" ] &&
6708                 skip_env "could not find setfacl" && return
6709         $GSS && skip "could not run under gss" && return
6710
6711         declare -a identity_old
6712
6713         for num in $(seq $MDSCOUNT); do
6714                 switch_identity $num true || identity_old[$num]=$?
6715         done
6716
6717         SAVE_UMASK=$(umask)
6718         umask 0022
6719         cd $DIR
6720
6721         echo "performing cp ..."
6722         run_acl_subtest cp || error "run_acl_subtest cp failed"
6723         echo "performing getfacl-noacl..."
6724         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6725         echo "performing misc..."
6726         run_acl_subtest misc || error  "misc test failed"
6727         echo "performing permissions..."
6728         run_acl_subtest permissions || error "permissions failed"
6729         echo "performing setfacl..."
6730         run_acl_subtest setfacl || error  "setfacl test failed"
6731
6732         # inheritance test got from HP
6733         echo "performing inheritance..."
6734         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6735         chmod +x make-tree || error "chmod +x failed"
6736         run_acl_subtest inheritance || error "inheritance test failed"
6737         rm -f make-tree
6738
6739         echo "LU-974 ignore umask when acl is enabled..."
6740         run_acl_subtest 974 || error "LU-974 umask test failed"
6741         if [ $MDSCOUNT -ge 2 ]; then
6742                 run_acl_subtest 974_remote ||
6743                         error "LU-974 umask test failed under remote dir"
6744         fi
6745
6746         echo "LU-2561 newly created file is same size as directory..."
6747         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6748                 run_acl_subtest 2561 || error "LU-2561 test failed"
6749         else
6750                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6751         fi
6752
6753         run_acl_subtest 4924 || error "LU-4924 test failed"
6754
6755         cd $SAVE_PWD
6756         umask $SAVE_UMASK
6757
6758         for num in $(seq $MDSCOUNT); do
6759                 if [ "${identity_old[$num]}" = 1 ]; then
6760                         switch_identity $num false || identity_old[$num]=$?
6761                 fi
6762         done
6763 }
6764 run_test 103 "acl test ========================================="
6765
6766 test_104a() {
6767         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6768         touch $DIR/$tfile
6769         lfs df || error "lfs df failed"
6770         lfs df -ih || error "lfs df -ih failed"
6771         lfs df -h $DIR || error "lfs df -h $DIR failed"
6772         lfs df -i $DIR || error "lfs df -i $DIR failed"
6773         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6774         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6775
6776         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6777         lctl --device %$OSC deactivate
6778         lfs df || error "lfs df with deactivated OSC failed"
6779         lctl --device %$OSC activate
6780         # wait the osc back to normal
6781         wait_osc_import_state client ost FULL
6782
6783         lfs df || error "lfs df with reactivated OSC failed"
6784         rm -f $DIR/$tfile
6785 }
6786 run_test 104a "lfs df [-ih] [path] test ========================="
6787
6788 test_104b() {
6789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6790         [ $RUNAS_ID -eq $UID ] &&
6791                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6792         chmod 666 /dev/obd
6793         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6794                         grep "Permission denied" | wc -l)))
6795         if [ $denied_cnt -ne 0 ]; then
6796                 error "lfs check servers test failed"
6797         fi
6798 }
6799 run_test 104b "$RUNAS lfs check servers test ===================="
6800
6801 test_105a() {
6802         # doesn't work on 2.4 kernels
6803         touch $DIR/$tfile
6804         if $(flock_is_enabled); then
6805                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6806         else
6807                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6808         fi
6809         rm -f $DIR/$tfile
6810 }
6811 run_test 105a "flock when mounted without -o flock test ========"
6812
6813 test_105b() {
6814         touch $DIR/$tfile
6815         if $(flock_is_enabled); then
6816                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6817         else
6818                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6819         fi
6820         rm -f $DIR/$tfile
6821 }
6822 run_test 105b "fcntl when mounted without -o flock test ========"
6823
6824 test_105c() {
6825         touch $DIR/$tfile
6826         if $(flock_is_enabled); then
6827                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6828         else
6829                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6830         fi
6831         rm -f $DIR/$tfile
6832 }
6833 run_test 105c "lockf when mounted without -o flock test ========"
6834
6835 test_105d() { # bug 15924
6836         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6837         test_mkdir -p $DIR/$tdir
6838         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6839         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6840         $LCTL set_param fail_loc=0x80000315
6841         flocks_test 2 $DIR/$tdir
6842 }
6843 run_test 105d "flock race (should not freeze) ========"
6844
6845 test_105e() { # bug 22660 && 22040
6846         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6847         touch $DIR/$tfile
6848         flocks_test 3 $DIR/$tfile
6849 }
6850 run_test 105e "Two conflicting flocks from same process ======="
6851
6852 test_106() { #bug 10921
6853         test_mkdir -p $DIR/$tdir
6854         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6855         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6856 }
6857 run_test 106 "attempt exec of dir followed by chown of that dir"
6858
6859 test_107() {
6860         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6861         CDIR=`pwd`
6862         cd $DIR
6863
6864         local file=core
6865         rm -f $file
6866
6867         local save_pattern=$(sysctl -n kernel.core_pattern)
6868         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6869         sysctl -w kernel.core_pattern=$file
6870         sysctl -w kernel.core_uses_pid=0
6871
6872         ulimit -c unlimited
6873         sleep 60 &
6874         SLEEPPID=$!
6875
6876         sleep 1
6877
6878         kill -s 11 $SLEEPPID
6879         wait $SLEEPPID
6880         if [ -e $file ]; then
6881                 size=`stat -c%s $file`
6882                 [ $size -eq 0 ] && error "Fail to create core file $file"
6883         else
6884                 error "Fail to create core file $file"
6885         fi
6886         rm -f $file
6887         sysctl -w kernel.core_pattern=$save_pattern
6888         sysctl -w kernel.core_uses_pid=$save_uses_pid
6889         cd $CDIR
6890 }
6891 run_test 107 "Coredump on SIG"
6892
6893 test_110() {
6894         test_mkdir -p $DIR/$tdir
6895         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6896                 error "mkdir with 255 char failed"
6897         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6898                 error "mkdir with 256 char should fail, but did not"
6899         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6900                 error "create with 255 char failed"
6901         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6902                 error "create with 256 char should fail, but did not"
6903
6904         ls -l $DIR/$tdir
6905         rm -rf $DIR/$tdir
6906 }
6907 run_test 110 "filename length checking"
6908
6909 test_115() {
6910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6911         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
6912                 tail -1 | cut -c11-20)
6913         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
6914         echo "Starting with $OSTIO_pre threads"
6915
6916         NUMTEST=20000
6917         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
6918         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
6919         echo "$NUMTEST creates/unlinks"
6920         test_mkdir -p $DIR/$tdir
6921         createmany -o $DIR/$tdir/$tfile $NUMTEST
6922         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6923
6924         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
6925                 tail -1 | cut -c11-20)
6926
6927         # don't return an error
6928         [ $OSTIO_post == $OSTIO_pre ] && echo \
6929             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6930             echo "This may be fine, depending on what ran before this test" &&
6931             echo "and how fast this system is." && return
6932
6933         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6934 }
6935 run_test 115 "verify dynamic thread creation===================="
6936
6937 free_min_max () {
6938         wait_delete_completed
6939         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6940         echo OST kbytes available: ${AVAIL[@]}
6941         MAXI=0; MAXV=${AVAIL[0]}
6942         MINI=0; MINV=${AVAIL[0]}
6943         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6944                 #echo OST $i: ${AVAIL[i]}kb
6945                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
6946                         MAXV=${AVAIL[i]}; MAXI=$i
6947                 fi
6948                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
6949                         MINV=${AVAIL[i]}; MINI=$i
6950                 fi
6951         done
6952         echo Min free space: OST $MINI: $MINV
6953         echo Max free space: OST $MAXI: $MAXV
6954 }
6955
6956 test_116a() { # was previously test_116()
6957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6958         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
6959
6960         echo -n "Free space priority "
6961         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
6962                 head -n1
6963         declare -a AVAIL
6964         free_min_max
6965
6966         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
6967         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
6968                 && return
6969         trap simple_cleanup_common EXIT
6970
6971
6972         # Check if we need to generate uneven OSTs
6973         test_mkdir -p $DIR/$tdir/OST${MINI}
6974         local FILL=$(($MINV / 4))
6975         local DIFF=$(($MAXV - $MINV))
6976         local DIFF2=$(($DIFF * 100 / $MINV))
6977
6978         local threshold=$(do_facet $SINGLEMDS \
6979                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
6980         threshold=${threshold%%%}
6981         echo -n "Check for uneven OSTs: "
6982         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
6983
6984         if [[ $DIFF2 -gt $threshold ]]; then
6985                 echo "ok"
6986                 echo "Don't need to fill OST$MINI"
6987         else
6988                 # generate uneven OSTs. Write 2% over the QOS threshold value
6989                 echo "no"
6990                 DIFF=$(($threshold - $DIFF2 + 2))
6991                 DIFF2=$(( ($MINV * $DIFF)/100 ))
6992                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
6993                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
6994                         error "setstripe failed"
6995                 DIFF=$(($DIFF2 / 2048))
6996                 i=0
6997                 while [ $i -lt $DIFF ]; do
6998                         i=$(($i + 1))
6999                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7000                                 bs=2M count=1 2>/dev/null
7001                         echo -n .
7002                 done
7003                 echo .
7004                 sync
7005                 sleep_maxage
7006                 free_min_max
7007         fi
7008
7009         DIFF=$(($MAXV - $MINV))
7010         DIFF2=$(($DIFF * 100 / $MINV))
7011         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7012         if [[ $DIFF2 -gt $threshold ]]; then
7013                 echo "ok"
7014         else
7015                 echo "failed - QOS mode won't be used"
7016                 skip "QOS imbalance criteria not met"
7017                 simple_cleanup_common
7018                 return
7019         fi
7020
7021         MINI1=$MINI; MINV1=$MINV
7022         MAXI1=$MAXI; MAXV1=$MAXV
7023
7024         # now fill using QOS
7025         $SETSTRIPE -c 1 $DIR/$tdir
7026         FILL=$(($FILL / 200))
7027         if [ $FILL -gt 600 ]; then
7028                 FILL=600
7029         fi
7030         echo "writing $FILL files to QOS-assigned OSTs"
7031         i=0
7032         while [ $i -lt $FILL ]; do
7033                 i=$((i + 1))
7034                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7035                         count=1 2>/dev/null
7036                 echo -n .
7037         done
7038         echo "wrote $i 200k files"
7039         sync
7040         sleep_maxage
7041
7042         echo "Note: free space may not be updated, so measurements might be off"
7043         free_min_max
7044         DIFF2=$(($MAXV - $MINV))
7045         echo "free space delta: orig $DIFF final $DIFF2"
7046         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7047         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7048         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7049         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7050         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7051         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7052         [ $DIFF -gt 0 ] &&
7053                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7054
7055         # Figure out which files were written where
7056         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7057                   awk '/'$MINI1': / {print $2; exit}')
7058         echo $UUID
7059         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7060         echo "$MINC files created on smaller OST $MINI1"
7061         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7062                   awk '/'$MAXI1': / {print $2; exit}')
7063         echo $UUID
7064         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7065         echo "$MAXC files created on larger OST $MAXI1"
7066         FILL=$(($MAXC * 100 / $MINC - 100))
7067         [[ $MINC -gt 0 ]] &&
7068                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7069         [[ $MAXC -gt $MINC ]] ||
7070                 error_ignore LU-9 "stripe QOS didn't balance free space"
7071         simple_cleanup_common
7072 }
7073 run_test 116a "stripe QOS: free space balance ==================="
7074
7075 test_116b() { # LU-2093
7076         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7077         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7078                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7079 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7080         local old_rr
7081         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7082                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7083         do_facet $SINGLEMDS lctl set_param \
7084                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7085         mkdir -p $DIR/$tdir
7086         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7087         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7088         do_facet $SINGLEMDS lctl set_param fail_loc=0
7089         rm -rf $DIR/$tdir
7090         do_facet $SINGLEMDS lctl set_param \
7091                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7092 }
7093 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7094
7095 test_117() # bug 10891
7096 {
7097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7098         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7099         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7100         lctl set_param fail_loc=0x21e
7101         > $DIR/$tfile || error "truncate failed"
7102         lctl set_param fail_loc=0
7103         echo "Truncate succeeded."
7104         rm -f $DIR/$tfile
7105 }
7106 run_test 117 "verify osd extend =========="
7107
7108 NO_SLOW_RESENDCOUNT=4
7109 export OLD_RESENDCOUNT=""
7110 set_resend_count () {
7111         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7112         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7113         lctl set_param -n $PROC_RESENDCOUNT $1
7114         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7115 }
7116
7117 # for reduce test_118* time (b=14842)
7118 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7119
7120 # Reset async IO behavior after error case
7121 reset_async() {
7122         FILE=$DIR/reset_async
7123
7124         # Ensure all OSCs are cleared
7125         $SETSTRIPE -c -1 $FILE
7126         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7127         sync
7128         rm $FILE
7129 }
7130
7131 test_118a() #bug 11710
7132 {
7133         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7134         reset_async
7135
7136         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7137         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7138         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7139
7140         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7141                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7142                 return 1;
7143         fi
7144         rm -f $DIR/$tfile
7145 }
7146 run_test 118a "verify O_SYNC works =========="
7147
7148 test_118b()
7149 {
7150         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7151         remote_ost_nodsh && skip "remote OST with nodsh" && return
7152
7153         reset_async
7154
7155         #define OBD_FAIL_OST_ENOENT 0x217
7156         set_nodes_failloc "$(osts_nodes)" 0x217
7157         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7158         RC=$?
7159         set_nodes_failloc "$(osts_nodes)" 0
7160         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7161         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7162                     grep -c writeback)
7163
7164         if [[ $RC -eq 0 ]]; then
7165                 error "Must return error due to dropped pages, rc=$RC"
7166                 return 1;
7167         fi
7168
7169         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7170                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7171                 return 1;
7172         fi
7173
7174         echo "Dirty pages not leaked on ENOENT"
7175
7176         # Due to the above error the OSC will issue all RPCs syncronously
7177         # until a subsequent RPC completes successfully without error.
7178         $MULTIOP $DIR/$tfile Ow4096yc
7179         rm -f $DIR/$tfile
7180
7181         return 0
7182 }
7183 run_test 118b "Reclaim dirty pages on fatal error =========="
7184
7185 test_118c()
7186 {
7187         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7188
7189         # for 118c, restore the original resend count, LU-1940
7190         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7191                                 set_resend_count $OLD_RESENDCOUNT
7192         remote_ost_nodsh && skip "remote OST with nodsh" && return
7193
7194         reset_async
7195
7196         #define OBD_FAIL_OST_EROFS               0x216
7197         set_nodes_failloc "$(osts_nodes)" 0x216
7198
7199         # multiop should block due to fsync until pages are written
7200         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7201         MULTIPID=$!
7202         sleep 1
7203
7204         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7205                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7206         fi
7207
7208         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7209                     grep -c writeback)
7210         if [[ $WRITEBACK -eq 0 ]]; then
7211                 error "No page in writeback, writeback=$WRITEBACK"
7212         fi
7213
7214         set_nodes_failloc "$(osts_nodes)" 0
7215         wait $MULTIPID
7216         RC=$?
7217         if [[ $RC -ne 0 ]]; then
7218                 error "Multiop fsync failed, rc=$RC"
7219         fi
7220
7221         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7222         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7223                     grep -c writeback)
7224         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7225                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7226         fi
7227
7228         rm -f $DIR/$tfile
7229         echo "Dirty pages flushed via fsync on EROFS"
7230         return 0
7231 }
7232 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7233
7234 # continue to use small resend count to reduce test_118* time (b=14842)
7235 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7236
7237 test_118d()
7238 {
7239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7240         remote_ost_nodsh && skip "remote OST with nodsh" && return
7241
7242         reset_async
7243
7244         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7245         set_nodes_failloc "$(osts_nodes)" 0x214
7246         # multiop should block due to fsync until pages are written
7247         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7248         MULTIPID=$!
7249         sleep 1
7250
7251         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7252                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7253         fi
7254
7255         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7256                     grep -c writeback)
7257         if [[ $WRITEBACK -eq 0 ]]; then
7258                 error "No page in writeback, writeback=$WRITEBACK"
7259         fi
7260
7261         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7262         set_nodes_failloc "$(osts_nodes)" 0
7263
7264         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7265         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7266                     grep -c writeback)
7267         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7268                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7269         fi
7270
7271         rm -f $DIR/$tfile
7272         echo "Dirty pages gaurenteed flushed via fsync"
7273         return 0
7274 }
7275 run_test 118d "Fsync validation inject a delay of the bulk =========="
7276
7277 test_118f() {
7278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7279         reset_async
7280
7281         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7282         lctl set_param fail_loc=0x8000040a
7283
7284         # Should simulate EINVAL error which is fatal
7285         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7286         RC=$?
7287         if [[ $RC -eq 0 ]]; then
7288                 error "Must return error due to dropped pages, rc=$RC"
7289         fi
7290
7291         lctl set_param fail_loc=0x0
7292
7293         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7294         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7295         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7296                     grep -c writeback)
7297         if [[ $LOCKED -ne 0 ]]; then
7298                 error "Locked pages remain in cache, locked=$LOCKED"
7299         fi
7300
7301         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7302                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7303         fi
7304
7305         rm -f $DIR/$tfile
7306         echo "No pages locked after fsync"
7307
7308         reset_async
7309         return 0
7310 }
7311 run_test 118f "Simulate unrecoverable OSC side error =========="
7312
7313 test_118g() {
7314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7315         reset_async
7316
7317         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7318         lctl set_param fail_loc=0x406
7319
7320         # simulate local -ENOMEM
7321         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7322         RC=$?
7323
7324         lctl set_param fail_loc=0
7325         if [[ $RC -eq 0 ]]; then
7326                 error "Must return error due to dropped pages, rc=$RC"
7327         fi
7328
7329         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7330         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7331         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7332                         grep -c writeback)
7333         if [[ $LOCKED -ne 0 ]]; then
7334                 error "Locked pages remain in cache, locked=$LOCKED"
7335         fi
7336
7337         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7338                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7339         fi
7340
7341         rm -f $DIR/$tfile
7342         echo "No pages locked after fsync"
7343
7344         reset_async
7345         return 0
7346 }
7347 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7348
7349 test_118h() {
7350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7351         remote_ost_nodsh && skip "remote OST with nodsh" && return
7352
7353         reset_async
7354
7355         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7356         set_nodes_failloc "$(osts_nodes)" 0x20e
7357         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7358         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7359         RC=$?
7360
7361         set_nodes_failloc "$(osts_nodes)" 0
7362         if [[ $RC -eq 0 ]]; then
7363                 error "Must return error due to dropped pages, rc=$RC"
7364         fi
7365
7366         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7367         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7368         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7369                     grep -c writeback)
7370         if [[ $LOCKED -ne 0 ]]; then
7371                 error "Locked pages remain in cache, locked=$LOCKED"
7372         fi
7373
7374         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7375                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7376         fi
7377
7378         rm -f $DIR/$tfile
7379         echo "No pages locked after fsync"
7380
7381         return 0
7382 }
7383 run_test 118h "Verify timeout in handling recoverables errors  =========="
7384
7385 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7386
7387 test_118i() {
7388         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7389         remote_ost_nodsh && skip "remote OST with nodsh" && return
7390
7391         reset_async
7392
7393         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7394         set_nodes_failloc "$(osts_nodes)" 0x20e
7395
7396         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7397         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7398         PID=$!
7399         sleep 5
7400         set_nodes_failloc "$(osts_nodes)" 0
7401
7402         wait $PID
7403         RC=$?
7404         if [[ $RC -ne 0 ]]; then
7405                 error "got error, but should be not, rc=$RC"
7406         fi
7407
7408         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7409         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7410         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7411         if [[ $LOCKED -ne 0 ]]; then
7412                 error "Locked pages remain in cache, locked=$LOCKED"
7413         fi
7414
7415         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7416                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7417         fi
7418
7419         rm -f $DIR/$tfile
7420         echo "No pages locked after fsync"
7421
7422         return 0
7423 }
7424 run_test 118i "Fix error before timeout in recoverable error  =========="
7425
7426 [ "$SLOW" = "no" ] && set_resend_count 4
7427
7428 test_118j() {
7429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7430         remote_ost_nodsh && skip "remote OST with nodsh" && return
7431
7432         reset_async
7433
7434         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7435         set_nodes_failloc "$(osts_nodes)" 0x220
7436
7437         # return -EIO from OST
7438         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7439         RC=$?
7440         set_nodes_failloc "$(osts_nodes)" 0x0
7441         if [[ $RC -eq 0 ]]; then
7442                 error "Must return error due to dropped pages, rc=$RC"
7443         fi
7444
7445         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7446         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7447         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7448         if [[ $LOCKED -ne 0 ]]; then
7449                 error "Locked pages remain in cache, locked=$LOCKED"
7450         fi
7451
7452         # in recoverable error on OST we want resend and stay until it finished
7453         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7454                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7455         fi
7456
7457         rm -f $DIR/$tfile
7458         echo "No pages locked after fsync"
7459
7460         return 0
7461 }
7462 run_test 118j "Simulate unrecoverable OST side error =========="
7463
7464 test_118k()
7465 {
7466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7467         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7468
7469         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7470         set_nodes_failloc "$(osts_nodes)" 0x20e
7471         test_mkdir -p $DIR/$tdir
7472
7473         for ((i=0;i<10;i++)); do
7474                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7475                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7476                 SLEEPPID=$!
7477                 sleep 0.500s
7478                 kill $SLEEPPID
7479                 wait $SLEEPPID
7480         done
7481
7482         set_nodes_failloc "$(osts_nodes)" 0
7483         rm -rf $DIR/$tdir
7484 }
7485 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7486
7487 test_118l()
7488 {
7489         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7490         # LU-646
7491         test_mkdir -p $DIR/$tdir
7492         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7493         rm -rf $DIR/$tdir
7494 }
7495 run_test 118l "fsync dir ========="
7496
7497 test_118m() # LU-3066
7498 {
7499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7500         test_mkdir -p $DIR/$tdir
7501         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7502         rm -rf $DIR/$tdir
7503 }
7504 run_test 118m "fdatasync dir ========="
7505
7506 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7507
7508 test_119a() # bug 11737
7509 {
7510         BSIZE=$((512 * 1024))
7511         directio write $DIR/$tfile 0 1 $BSIZE
7512         # We ask to read two blocks, which is more than a file size.
7513         # directio will indicate an error when requested and actual
7514         # sizes aren't equeal (a normal situation in this case) and
7515         # print actual read amount.
7516         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7517         if [ "$NOB" != "$BSIZE" ]; then
7518                 error "read $NOB bytes instead of $BSIZE"
7519         fi
7520         rm -f $DIR/$tfile
7521 }
7522 run_test 119a "Short directIO read must return actual read amount"
7523
7524 test_119b() # bug 11737
7525 {
7526         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7527
7528         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7529         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7530         sync
7531         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7532                 error "direct read failed"
7533         rm -f $DIR/$tfile
7534 }
7535 run_test 119b "Sparse directIO read must return actual read amount"
7536
7537 test_119c() # bug 13099
7538 {
7539         BSIZE=1048576
7540         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7541         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7542         rm -f $DIR/$tfile
7543 }
7544 run_test 119c "Testing for direct read hitting hole"
7545
7546 test_119d() # bug 15950
7547 {
7548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7549         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7550         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7551         BSIZE=1048576
7552         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7553         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7554         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7555         lctl set_param fail_loc=0x40d
7556         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7557         pid_dio=$!
7558         sleep 1
7559         cat $DIR/$tfile > /dev/null &
7560         lctl set_param fail_loc=0
7561         pid_reads=$!
7562         wait $pid_dio
7563         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7564         sleep 2
7565         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7566         error "the read rpcs have not completed in 2s"
7567         rm -f $DIR/$tfile
7568         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7569 }
7570 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7571
7572 test_120a() {
7573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7574         test_mkdir -p $DIR/$tdir
7575         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7576                skip "no early lock cancel on server" && return 0
7577
7578         lru_resize_disable mdc
7579         lru_resize_disable osc
7580         cancel_lru_locks mdc
7581         # asynchronous object destroy at MDT could cause bl ast to client
7582         cancel_lru_locks osc
7583
7584         stat $DIR/$tdir > /dev/null
7585         can1=$(do_facet $SINGLEMDS \
7586                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7587                awk '/ldlm_cancel/ {print $2}')
7588         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7589                awk '/ldlm_bl_callback/ {print $2}')
7590         test_mkdir -c1 $DIR/$tdir/d1
7591         can2=$(do_facet $SINGLEMDS \
7592                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7593                awk '/ldlm_cancel/ {print $2}')
7594         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7595                awk '/ldlm_bl_callback/ {print $2}')
7596         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7597         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7598         lru_resize_enable mdc
7599         lru_resize_enable osc
7600 }
7601 run_test 120a "Early Lock Cancel: mkdir test"
7602
7603 test_120b() {
7604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7605         test_mkdir $DIR/$tdir
7606         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7607                skip "no early lock cancel on server" && return 0
7608         lru_resize_disable mdc
7609         lru_resize_disable osc
7610         cancel_lru_locks mdc
7611         stat $DIR/$tdir > /dev/null
7612         can1=$(do_facet $SINGLEMDS \
7613                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7614                awk '/ldlm_cancel/ {print $2}')
7615         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7616                awk '/ldlm_bl_callback/ {print $2}')
7617         touch $DIR/$tdir/f1
7618         can2=$(do_facet $SINGLEMDS \
7619                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7620                awk '/ldlm_cancel/ {print $2}')
7621         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7622                awk '/ldlm_bl_callback/ {print $2}')
7623         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7624         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7625         lru_resize_enable mdc
7626         lru_resize_enable osc
7627 }
7628 run_test 120b "Early Lock Cancel: create test"
7629
7630 test_120c() {
7631         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7632         test_mkdir -c1 $DIR/$tdir
7633         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7634                skip "no early lock cancel on server" && return 0
7635         lru_resize_disable mdc
7636         lru_resize_disable osc
7637         test_mkdir -p -c1 $DIR/$tdir/d1
7638         test_mkdir -p -c1 $DIR/$tdir/d2
7639         touch $DIR/$tdir/d1/f1
7640         cancel_lru_locks mdc
7641         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7642         can1=$(do_facet $SINGLEMDS \
7643                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7644                awk '/ldlm_cancel/ {print $2}')
7645         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7646                awk '/ldlm_bl_callback/ {print $2}')
7647         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7648         can2=$(do_facet $SINGLEMDS \
7649                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7650                awk '/ldlm_cancel/ {print $2}')
7651         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7652                awk '/ldlm_bl_callback/ {print $2}')
7653         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7654         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7655         lru_resize_enable mdc
7656         lru_resize_enable osc
7657 }
7658 run_test 120c "Early Lock Cancel: link test"
7659
7660 test_120d() {
7661         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7662         test_mkdir -p -c1 $DIR/$tdir
7663         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7664                skip "no early lock cancel on server" && return 0
7665         lru_resize_disable mdc
7666         lru_resize_disable osc
7667         touch $DIR/$tdir
7668         cancel_lru_locks mdc
7669         stat $DIR/$tdir > /dev/null
7670         can1=$(do_facet $SINGLEMDS \
7671                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7672                awk '/ldlm_cancel/ {print $2}')
7673         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7674                awk '/ldlm_bl_callback/ {print $2}')
7675         chmod a+x $DIR/$tdir
7676         can2=$(do_facet $SINGLEMDS \
7677                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7678                awk '/ldlm_cancel/ {print $2}')
7679         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7680                awk '/ldlm_bl_callback/ {print $2}')
7681         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7682         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7683         lru_resize_enable mdc
7684         lru_resize_enable osc
7685 }
7686 run_test 120d "Early Lock Cancel: setattr test"
7687
7688 test_120e() {
7689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7690         test_mkdir -p -c1 $DIR/$tdir
7691         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7692                skip "no early lock cancel on server" && return 0
7693         lru_resize_disable mdc
7694         lru_resize_disable osc
7695         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7696         cancel_lru_locks mdc
7697         cancel_lru_locks osc
7698         dd if=$DIR/$tdir/f1 of=/dev/null
7699         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7700         # XXX client can not do early lock cancel of OST lock
7701         # during unlink (LU-4206), so cancel osc lock now.
7702         cancel_lru_locks osc
7703         can1=$(do_facet $SINGLEMDS \
7704                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7705                awk '/ldlm_cancel/ {print $2}')
7706         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7707                awk '/ldlm_bl_callback/ {print $2}')
7708         unlink $DIR/$tdir/f1
7709         sleep 5
7710         can2=$(do_facet $SINGLEMDS \
7711                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7712                awk '/ldlm_cancel/ {print $2}')
7713         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7714                awk '/ldlm_bl_callback/ {print $2}')
7715         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7716         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7717         lru_resize_enable mdc
7718         lru_resize_enable osc
7719 }
7720 run_test 120e "Early Lock Cancel: unlink test"
7721
7722 test_120f() {
7723         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7724         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7725                skip "no early lock cancel on server" && return 0
7726         test_mkdir -p -c1 $DIR/$tdir
7727         lru_resize_disable mdc
7728         lru_resize_disable osc
7729         test_mkdir -p -c1 $DIR/$tdir/d1
7730         test_mkdir -p -c1 $DIR/$tdir/d2
7731         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7732         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7733         cancel_lru_locks mdc
7734         cancel_lru_locks osc
7735         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7736         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7737         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7738         # XXX client can not do early lock cancel of OST lock
7739         # during rename (LU-4206), so cancel osc lock now.
7740         cancel_lru_locks osc
7741         can1=$(do_facet $SINGLEMDS \
7742                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7743                awk '/ldlm_cancel/ {print $2}')
7744         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7745                awk '/ldlm_bl_callback/ {print $2}')
7746         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7747         sleep 5
7748         can2=$(do_facet $SINGLEMDS \
7749                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7750                awk '/ldlm_cancel/ {print $2}')
7751         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7752                awk '/ldlm_bl_callback/ {print $2}')
7753         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7754         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7755         lru_resize_enable mdc
7756         lru_resize_enable osc
7757 }
7758 run_test 120f "Early Lock Cancel: rename test"
7759
7760 test_120g() {
7761         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7762         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7763                skip "no early lock cancel on server" && return 0
7764         lru_resize_disable mdc
7765         lru_resize_disable osc
7766         count=10000
7767         echo create $count files
7768         test_mkdir -p $DIR/$tdir
7769         cancel_lru_locks mdc
7770         cancel_lru_locks osc
7771         t0=`date +%s`
7772
7773         can0=$(do_facet $SINGLEMDS \
7774                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7775                awk '/ldlm_cancel/ {print $2}')
7776         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7777                awk '/ldlm_bl_callback/ {print $2}')
7778         createmany -o $DIR/$tdir/f $count
7779         sync
7780         can1=$(do_facet $SINGLEMDS \
7781                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7782                awk '/ldlm_cancel/ {print $2}')
7783         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7784                awk '/ldlm_bl_callback/ {print $2}')
7785         t1=$(date +%s)
7786         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7787         echo rm $count files
7788         rm -r $DIR/$tdir
7789         sync
7790         can2=$(do_facet $SINGLEMDS \
7791                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7792                awk '/ldlm_cancel/ {print $2}')
7793         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7794                awk '/ldlm_bl_callback/ {print $2}')
7795         t2=$(date +%s)
7796         echo total: $count removes in $((t2-t1))
7797         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7798         sleep 2
7799         # wait for commitment of removal
7800         lru_resize_enable mdc
7801         lru_resize_enable osc
7802 }
7803 run_test 120g "Early Lock Cancel: performance test"
7804
7805 test_121() { #bug #10589
7806         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7807         rm -rf $DIR/$tfile
7808         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7809 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7810         lctl set_param fail_loc=0x310
7811         cancel_lru_locks osc > /dev/null
7812         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7813         lctl set_param fail_loc=0
7814         [[ $reads -eq $writes ]] ||
7815                 error "read $reads blocks, must be $writes blocks"
7816 }
7817 run_test 121 "read cancel race ========="
7818
7819 test_123a() { # was test 123, statahead(bug 11401)
7820         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7821         SLOWOK=0
7822         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7823             log "testing on UP system. Performance may be not as good as expected."
7824                         SLOWOK=1
7825         fi
7826
7827         rm -rf $DIR/$tdir
7828         test_mkdir -p $DIR/$tdir
7829         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7830         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7831         MULT=10
7832         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7833                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7834
7835                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7836                 lctl set_param -n llite.*.statahead_max 0
7837                 lctl get_param llite.*.statahead_max
7838                 cancel_lru_locks mdc
7839                 cancel_lru_locks osc
7840                 stime=`date +%s`
7841                 time ls -l $DIR/$tdir | wc -l
7842                 etime=`date +%s`
7843                 delta=$((etime - stime))
7844                 log "ls $i files without statahead: $delta sec"
7845                 lctl set_param llite.*.statahead_max=$max
7846
7847                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7848                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7849                 cancel_lru_locks mdc
7850                 cancel_lru_locks osc
7851                 stime=`date +%s`
7852                 time ls -l $DIR/$tdir | wc -l
7853                 etime=`date +%s`
7854                 delta_sa=$((etime - stime))
7855                 log "ls $i files with statahead: $delta_sa sec"
7856                 lctl get_param -n llite.*.statahead_stats
7857                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7858
7859                 [[ $swrong -lt $ewrong ]] &&
7860                         log "statahead was stopped, maybe too many locks held!"
7861                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
7862
7863                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7864                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7865                     lctl set_param -n llite.*.statahead_max 0
7866                     lctl get_param llite.*.statahead_max
7867                     cancel_lru_locks mdc
7868                     cancel_lru_locks osc
7869                     stime=`date +%s`
7870                     time ls -l $DIR/$tdir | wc -l
7871                     etime=`date +%s`
7872                     delta=$((etime - stime))
7873                     log "ls $i files again without statahead: $delta sec"
7874                     lctl set_param llite.*.statahead_max=$max
7875                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7876                         if [  $SLOWOK -eq 0 ]; then
7877                                 error "ls $i files is slower with statahead!"
7878                         else
7879                                 log "ls $i files is slower with statahead!"
7880                         fi
7881                         break
7882                     fi
7883                 fi
7884
7885                 [ $delta -gt 20 ] && break
7886                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7887                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7888         done
7889         log "ls done"
7890
7891         stime=`date +%s`
7892         rm -r $DIR/$tdir
7893         sync
7894         etime=`date +%s`
7895         delta=$((etime - stime))
7896         log "rm -r $DIR/$tdir/: $delta seconds"
7897         log "rm done"
7898         lctl get_param -n llite.*.statahead_stats
7899 }
7900 run_test 123a "verify statahead work"
7901
7902 test_123b () { # statahead(bug 15027)
7903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7904         test_mkdir -p $DIR/$tdir
7905         createmany -o $DIR/$tdir/$tfile-%d 1000
7906
7907         cancel_lru_locks mdc
7908         cancel_lru_locks osc
7909
7910 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7911         lctl set_param fail_loc=0x80000803
7912         ls -lR $DIR/$tdir > /dev/null
7913         log "ls done"
7914         lctl set_param fail_loc=0x0
7915         lctl get_param -n llite.*.statahead_stats
7916         rm -r $DIR/$tdir
7917         sync
7918
7919 }
7920 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7921
7922 test_124a() {
7923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7924         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
7925                 skip "no lru resize on server" && return 0
7926         local NR=2000
7927         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7928
7929         log "create $NR files at $DIR/$tdir"
7930         createmany -o $DIR/$tdir/f $NR ||
7931                 error "failed to create $NR files in $DIR/$tdir"
7932
7933         cancel_lru_locks mdc
7934         ls -l $DIR/$tdir > /dev/null
7935
7936         local NSDIR=""
7937         local LRU_SIZE=0
7938         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
7939                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
7940                 LRU_SIZE=$($LCTL get_param -n $PARAM)
7941                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
7942                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7943                         log "NSDIR=$NSDIR"
7944                         log "NS=$(basename $NSDIR)"
7945                         break
7946                 fi
7947         done
7948
7949         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
7950                 skip "Not enough cached locks created!"
7951                 return 0
7952         fi
7953         log "LRU=$LRU_SIZE"
7954
7955         local SLEEP=30
7956
7957         # We know that lru resize allows one client to hold $LIMIT locks
7958         # for 10h. After that locks begin to be killed by client.
7959         local MAX_HRS=10
7960         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
7961         log "LIMIT=$LIMIT"
7962
7963         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7964         # killing locks. Some time was spent for creating locks. This means
7965         # that up to the moment of sleep finish we must have killed some of
7966         # them (10-100 locks). This depends on how fast ther were created.
7967         # Many of them were touched in almost the same moment and thus will
7968         # be killed in groups.
7969         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7970
7971         # Use $LRU_SIZE_B here to take into account real number of locks
7972         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7973         local LRU_SIZE_B=$LRU_SIZE
7974         log "LVF=$LVF"
7975         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
7976         log "OLD_LVF=$OLD_LVF"
7977         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
7978
7979         # Let's make sure that we really have some margin. Client checks
7980         # cached locks every 10 sec.
7981         SLEEP=$((SLEEP+20))
7982         log "Sleep ${SLEEP} sec"
7983         local SEC=0
7984         while ((SEC<$SLEEP)); do
7985                 echo -n "..."
7986                 sleep 5
7987                 SEC=$((SEC+5))
7988                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
7989                 echo -n "$LRU_SIZE"
7990         done
7991         echo ""
7992         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7993         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
7994
7995         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
7996                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7997                 unlinkmany $DIR/$tdir/f $NR
7998                 return
7999         }
8000
8001         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8002         log "unlink $NR files at $DIR/$tdir"
8003         unlinkmany $DIR/$tdir/f $NR
8004 }
8005 run_test 124a "lru resize ======================================="
8006
8007 get_max_pool_limit()
8008 {
8009         local limit=$($LCTL get_param \
8010                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8011         local max=0
8012         for l in $limit; do
8013                 if [[ $l -gt $max ]]; then
8014                         max=$l
8015                 fi
8016         done
8017         echo $max
8018 }
8019
8020 test_124b() {
8021         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8022         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8023                 skip "no lru resize on server" && return 0
8024
8025         LIMIT=$(get_max_pool_limit)
8026
8027         NR=$(($(default_lru_size)*20))
8028         if [[ $NR -gt $LIMIT ]]; then
8029                 log "Limit lock number by $LIMIT locks"
8030                 NR=$LIMIT
8031         fi
8032         lru_resize_disable mdc
8033         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8034                 error "failed to create $DIR/$tdir/disable_lru_resize"
8035
8036         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8037         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8038         cancel_lru_locks mdc
8039         stime=`date +%s`
8040         PID=""
8041         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8042         PID="$PID $!"
8043         sleep 2
8044         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8045         PID="$PID $!"
8046         sleep 2
8047         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8048         PID="$PID $!"
8049         wait $PID
8050         etime=`date +%s`
8051         nolruresize_delta=$((etime-stime))
8052         log "ls -la time: $nolruresize_delta seconds"
8053         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8054         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8055
8056         lru_resize_enable mdc
8057         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8058                 error "failed to create $DIR/$tdir/enable_lru_resize"
8059
8060         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8061         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8062         cancel_lru_locks mdc
8063         stime=`date +%s`
8064         PID=""
8065         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8066         PID="$PID $!"
8067         sleep 2
8068         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8069         PID="$PID $!"
8070         sleep 2
8071         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8072         PID="$PID $!"
8073         wait $PID
8074         etime=`date +%s`
8075         lruresize_delta=$((etime-stime))
8076         log "ls -la time: $lruresize_delta seconds"
8077         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8078
8079         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8080                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8081         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8082                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8083         else
8084                 log "lru resize performs the same with no lru resize"
8085         fi
8086         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8087 }
8088 run_test 124b "lru resize (performance test) ======================="
8089
8090 test_125() { # 13358
8091         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8092         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8093         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8094         test_mkdir -p $DIR/d125 || error "mkdir failed"
8095         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8096         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8097         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8098 }
8099 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8100
8101 test_126() { # bug 12829/13455
8102         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8103         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8104         $GSS && skip "must run as gss disabled" && return
8105
8106         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8107         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8108         rm -f $DIR/$tfile
8109         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8110 }
8111 run_test 126 "check that the fsgid provided by the client is taken into account"
8112
8113 test_127a() { # bug 15521
8114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8115         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8116         $LCTL set_param osc.*.stats=0
8117         FSIZE=$((2048 * 1024))
8118         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8119         cancel_lru_locks osc
8120         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8121
8122         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8123         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8124                 echo "got $COUNT $NAME"
8125                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8126                 eval $NAME=$COUNT || error "Wrong proc format"
8127
8128                 case $NAME in
8129                         read_bytes|write_bytes)
8130                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8131                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8132                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8133                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8134                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8135                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8136                                 error "sumsquare is too small: $SUMSQ"
8137                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8138                                 error "sumsquare is too big: $SUMSQ"
8139                         ;;
8140                         *) ;;
8141                 esac
8142         done < $DIR/${tfile}.tmp
8143
8144         #check that we actually got some stats
8145         [ "$read_bytes" ] || error "Missing read_bytes stats"
8146         [ "$write_bytes" ] || error "Missing write_bytes stats"
8147         [ "$read_bytes" != 0 ] || error "no read done"
8148         [ "$write_bytes" != 0 ] || error "no write done"
8149 }
8150 run_test 127a "verify the client stats are sane"
8151
8152 test_127b() { # bug LU-333
8153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8154         $LCTL set_param llite.*.stats=0
8155         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8156         # perform 2 reads and writes so MAX is different from SUM.
8157         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8158         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8159         cancel_lru_locks osc
8160         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8161         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8162
8163         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8164         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8165                 echo "got $COUNT $NAME"
8166                 eval $NAME=$COUNT || error "Wrong proc format"
8167
8168         case $NAME in
8169                 read_bytes)
8170                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8171                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8172                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8173                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8174                         ;;
8175                 write_bytes)
8176                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8177                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8178                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8179                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8180                         ;;
8181                         *) ;;
8182                 esac
8183         done < $TMP/${tfile}.tmp
8184
8185         #check that we actually got some stats
8186         [ "$read_bytes" ] || error "Missing read_bytes stats"
8187         [ "$write_bytes" ] || error "Missing write_bytes stats"
8188         [ "$read_bytes" != 0 ] || error "no read done"
8189         [ "$write_bytes" != 0 ] || error "no write done"
8190 }
8191 run_test 127b "verify the llite client stats are sane"
8192
8193 test_128() { # bug 15212
8194         touch $DIR/$tfile
8195         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8196                 find $DIR/$tfile
8197                 find $DIR/$tfile
8198         EOF
8199
8200         result=$(grep error $TMP/$tfile.log)
8201         rm -f $DIR/$tfile
8202         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8203 }
8204 run_test 128 "interactive lfs for 2 consecutive find's"
8205
8206 set_dir_limits () {
8207         local mntdev
8208         local canondev
8209         local node
8210
8211         local LDPROC=/proc/fs/ldiskfs
8212         local facets=$(get_facets MDS)
8213
8214         for facet in ${facets//,/ }; do
8215                 canondev=$(ldiskfs_canon \
8216                            *.$(convert_facet2label $facet).mntdev $facet)
8217                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8218                                                 LDPROC=/sys/fs/ldiskfs
8219                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8220         done
8221 }
8222
8223 test_129() {
8224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8225         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8226                 skip "Only applicable to ldiskfs-based MDTs"
8227                 return
8228         fi
8229         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8230         ENOSPC=28
8231         EFBIG=27
8232
8233         rm -rf $DIR/$tdir
8234         test_mkdir -p $DIR/$tdir
8235
8236         # block size of mds1
8237         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8238         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8239         local MAX=$((MDSBLOCKSIZE * 3))
8240         set_dir_limits $MAX
8241         local I=$(stat -c%s "$DIR/$tdir")
8242         local J=0
8243         local STRIPE_COUNT=1
8244         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8245         MAX=$((MAX*STRIPE_COUNT))
8246         while [[ $I -le $MAX ]]; do
8247                 $MULTIOP $DIR/$tdir/$J Oc
8248                 rc=$?
8249                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8250                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8251                 #and EFBIG for previous versions
8252                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8253                         set_dir_limits 0
8254                         echo "return code $rc received as expected"
8255                         multiop $DIR/$tdir/$J Oc ||
8256                                 error_exit "multiop failed w/o dir size limit"
8257
8258                         I=$(stat -c%s "$DIR/$tdir")
8259
8260                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8261                                         $(version_code 2.4.51) ]
8262                         then
8263                                 [[ $I -eq $MAX ]] && return 0
8264                         else
8265                                 [[ $I -gt $MAX ]] && return 0
8266                         fi
8267                         error_exit "current dir size $I, previous limit $MAX"
8268                 elif [ $rc -ne 0 ]; then
8269                         set_dir_limits 0
8270                         error_exit "return code $rc received instead of expected " \
8271                                    "$EFBIG or $ENOSPC, files in dir $I"
8272                 fi
8273                 J=$((J+1))
8274                 I=$(stat -c%s "$DIR/$tdir")
8275         done
8276
8277         set_dir_limits 0
8278         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8279 }
8280 run_test 129 "test directory size limit ========================"
8281
8282 OLDIFS="$IFS"
8283 cleanup_130() {
8284         trap 0
8285         IFS="$OLDIFS"
8286 }
8287
8288 test_130a() {
8289         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8290         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8291                 return
8292
8293         trap cleanup_130 EXIT RETURN
8294
8295         local fm_file=$DIR/$tfile
8296         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8297         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8298                 error "dd failed for $fm_file"
8299
8300         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8301         filefrag -ves $fm_file
8302         RC=$?
8303         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8304                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8305         [ $RC != 0 ] && error "filefrag $fm_file failed"
8306
8307         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8308                       grep -v "ext:" | grep -v "found")
8309         lun=$($GETSTRIPE -i $fm_file)
8310
8311         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8312         IFS=$'\n'
8313         tot_len=0
8314         for line in $filefrag_op
8315         do
8316                 frag_lun=`echo $line | cut -d: -f5`
8317                 ext_len=`echo $line | cut -d: -f4`
8318                 if (( $frag_lun != $lun )); then
8319                         cleanup_130
8320                         error "FIEMAP on 1-stripe file($fm_file) failed"
8321                         return
8322                 fi
8323                 (( tot_len += ext_len ))
8324         done
8325
8326         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8327                 cleanup_130
8328                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8329                 return
8330         fi
8331
8332         cleanup_130
8333
8334         echo "FIEMAP on single striped file succeeded"
8335 }
8336 run_test 130a "FIEMAP (1-stripe file)"
8337
8338 test_130b() {
8339         [ "$OSTCOUNT" -lt "2" ] &&
8340                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8341
8342         [ "$OSTCOUNT" -ge "10" ] &&
8343                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8344
8345         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8346         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8347                 return
8348
8349         trap cleanup_130 EXIT RETURN
8350
8351         local fm_file=$DIR/$tfile
8352         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8353         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8354                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8355
8356         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8357                 error "dd failed on $fm_file"
8358
8359         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8360         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8361                       grep -v "ext:" | grep -v "found")
8362
8363         last_lun=$(echo $filefrag_op | cut -d: -f5)
8364
8365         IFS=$'\n'
8366         tot_len=0
8367         num_luns=1
8368         for line in $filefrag_op
8369         do
8370                 frag_lun=`echo $line | cut -d: -f5`
8371                 ext_len=`echo $line | cut -d: -f4`
8372                 if (( $frag_lun != $last_lun )); then
8373                         if (( tot_len != 1024 )); then
8374                                 cleanup_130
8375                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8376                                 return
8377                         else
8378                                 (( num_luns += 1 ))
8379                                 tot_len=0
8380                         fi
8381                 fi
8382                 (( tot_len += ext_len ))
8383                 last_lun=$frag_lun
8384         done
8385         if (( num_luns != 2 || tot_len != 1024 )); then
8386                 cleanup_130
8387                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8388                 return
8389         fi
8390
8391         cleanup_130
8392
8393         echo "FIEMAP on 2-stripe file succeeded"
8394 }
8395 run_test 130b "FIEMAP (2-stripe file)"
8396
8397 test_130c() {
8398         [ "$OSTCOUNT" -lt "2" ] &&
8399                 skip_env "skipping FIEMAP on 2-stripe file" && return
8400
8401         [ "$OSTCOUNT" -ge "10" ] &&
8402                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8403
8404         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8405         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8406                 return
8407
8408         trap cleanup_130 EXIT RETURN
8409
8410         local fm_file=$DIR/$tfile
8411         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8412         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8413                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8414
8415         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8416
8417         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8418         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8419
8420         last_lun=`echo $filefrag_op | cut -d: -f5`
8421
8422         IFS=$'\n'
8423         tot_len=0
8424         num_luns=1
8425         for line in $filefrag_op
8426         do
8427                 frag_lun=`echo $line | cut -d: -f5`
8428                 ext_len=`echo $line | cut -d: -f4`
8429                 if (( $frag_lun != $last_lun )); then
8430                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8431                         if (( logical != 512 )); then
8432                                 cleanup_130
8433                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8434                                 return
8435                         fi
8436                         if (( tot_len != 512 )); then
8437                                 cleanup_130
8438                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8439                                 return
8440                         else
8441                                 (( num_luns += 1 ))
8442                                 tot_len=0
8443                         fi
8444                 fi
8445                 (( tot_len += ext_len ))
8446                 last_lun=$frag_lun
8447         done
8448         if (( num_luns != 2 || tot_len != 512 )); then
8449                 cleanup_130
8450                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8451                 return
8452         fi
8453
8454         cleanup_130
8455
8456         echo "FIEMAP on 2-stripe file with hole succeeded"
8457 }
8458 run_test 130c "FIEMAP (2-stripe file with hole)"
8459
8460 test_130d() {
8461         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8462
8463         [ "$OSTCOUNT" -ge "10" ] &&
8464                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8465
8466         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8467         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8468
8469         trap cleanup_130 EXIT RETURN
8470
8471         local fm_file=$DIR/$tfile
8472         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8473         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8474                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8475
8476         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8477         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8478                 error "dd failed on $fm_file"
8479
8480         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8481         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8482                 grep -v "ext:" | grep -v "found"`
8483
8484         last_lun=`echo $filefrag_op | cut -d: -f5`
8485
8486         IFS=$'\n'
8487         tot_len=0
8488         num_luns=1
8489         for line in $filefrag_op
8490         do
8491                 frag_lun=`echo $line | cut -d: -f5`
8492                 ext_len=`echo $line | cut -d: -f4`
8493                 if (( $frag_lun != $last_lun )); then
8494                         if (( tot_len != 1024 )); then
8495                                 cleanup_130
8496                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8497                                 return
8498                         else
8499                                 (( num_luns += 1 ))
8500                                 tot_len=0
8501                         fi
8502                 fi
8503                 (( tot_len += ext_len ))
8504                 last_lun=$frag_lun
8505         done
8506         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8507                 cleanup_130
8508                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8509                 return
8510         fi
8511
8512         cleanup_130
8513
8514         echo "FIEMAP on N-stripe file succeeded"
8515 }
8516 run_test 130d "FIEMAP (N-stripe file)"
8517
8518 test_130e() {
8519         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8520
8521         [ "$OSTCOUNT" -ge "10" ] &&
8522                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8523
8524         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8525         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8526
8527         trap cleanup_130 EXIT RETURN
8528
8529         local fm_file=$DIR/$tfile
8530         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8531         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8532                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8533
8534         NUM_BLKS=512
8535         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8536         for ((i = 0; i < $NUM_BLKS; i++))
8537         do
8538                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8539         done
8540
8541         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8542         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8543
8544         last_lun=`echo $filefrag_op | cut -d: -f5`
8545
8546         IFS=$'\n'
8547         tot_len=0
8548         num_luns=1
8549         for line in $filefrag_op
8550         do
8551                 frag_lun=`echo $line | cut -d: -f5`
8552                 ext_len=`echo $line | cut -d: -f4`
8553                 if (( $frag_lun != $last_lun )); then
8554                         if (( tot_len != $EXPECTED_LEN )); then
8555                                 cleanup_130
8556                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8557                                 return
8558                         else
8559                                 (( num_luns += 1 ))
8560                                 tot_len=0
8561                         fi
8562                 fi
8563                 (( tot_len += ext_len ))
8564                 last_lun=$frag_lun
8565         done
8566         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8567                 cleanup_130
8568                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8569                 return
8570         fi
8571
8572         cleanup_130
8573
8574         echo "FIEMAP with continuation calls succeeded"
8575 }
8576 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8577
8578 # Test for writev/readv
8579 test_131a() {
8580         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8581         error "writev test failed"
8582         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8583         error "readv failed"
8584         rm -f $DIR/$tfile
8585 }
8586 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8587
8588 test_131b() {
8589         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8590         error "append writev test failed"
8591         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8592         error "append writev test failed"
8593         rm -f $DIR/$tfile
8594 }
8595 run_test 131b "test append writev"
8596
8597 test_131c() {
8598         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8599         error "NOT PASS"
8600 }
8601 run_test 131c "test read/write on file w/o objects"
8602
8603 test_131d() {
8604         rwv -f $DIR/$tfile -w -n 1 1572864
8605         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8606         if [ "$NOB" != 1572864 ]; then
8607                 error "Short read filed: read $NOB bytes instead of 1572864"
8608         fi
8609         rm -f $DIR/$tfile
8610 }
8611 run_test 131d "test short read"
8612
8613 test_131e() {
8614         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8615         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8616         error "read hitting hole failed"
8617         rm -f $DIR/$tfile
8618 }
8619 run_test 131e "test read hitting hole"
8620
8621 get_ost_param() {
8622         local token=$1
8623         local gl_sum=0
8624         for node in $(osts_nodes); do
8625                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8626                 [ x$gl = x"" ] && gl=0
8627                 gl_sum=$((gl_sum + gl))
8628         done
8629         echo $gl_sum
8630 }
8631
8632 som_mode_switch() {
8633         local som=$1
8634         local gl1=$2
8635         local gl2=$3
8636
8637         if [ x$som = x"enabled" ]; then
8638                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8639                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8640                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8641         else
8642                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8643                 MOUNTOPT="$MOUNTOPT,som_preview"
8644                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8645         fi
8646
8647         # do remount to make new mount-conf parameters actual
8648         echo remounting...
8649         sync
8650         stopall
8651         setupall
8652 }
8653
8654 test_132() { #1028, SOM
8655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8656         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8657         local num=$(get_mds_dir $DIR)
8658         local mymds=mds${num}
8659         local MOUNTOPT_SAVE=$MOUNTOPT
8660
8661         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8662         cancel_lru_locks osc
8663
8664         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8665
8666         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8667         stat $DIR/$tfile >/dev/null
8668         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8669         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8670         rm $DIR/$tfile
8671         som_mode_switch $som1 $gl1 $gl2
8672
8673         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8674         cancel_lru_locks osc
8675
8676         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8677         if [ $som1 == $som2 ]; then
8678             error "som is still "$som2
8679             if [ x$som2 = x"enabled" ]; then
8680                 som2="disabled"
8681             else
8682                 som2="enabled"
8683             fi
8684         fi
8685
8686         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8687         stat $DIR/$tfile >/dev/null
8688         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8689         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8690         som_mode_switch $som2 $gl1 $gl2
8691         MOUNTOPT=$MOUNTOPT_SAVE
8692 }
8693 run_test 132 "som avoids glimpse rpc"
8694
8695 check_stats() {
8696         local res
8697         local count
8698         case $1 in
8699         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8700                  ;;
8701         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8702                  ;;
8703         *) error "Wrong argument $1" ;;
8704         esac
8705         echo $res
8706         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8707         # if the argument $3 is zero, it means any stat increment is ok.
8708         if [[ $3 -gt 0 ]]; then
8709                 count=$(echo $res | awk '{ print $2 }')
8710                 [[ $count -ne $3 ]] &&
8711                         error "The $2 counter on $1 is wrong - expected $3"
8712         fi
8713 }
8714
8715 test_133a() {
8716         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8717         remote_ost_nodsh && skip "remote OST with nodsh" && return
8718         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8719
8720         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8721                 { skip "MDS doesn't support rename stats"; return; }
8722         local testdir=$DIR/${tdir}/stats_testdir
8723         mkdir -p $DIR/${tdir}
8724
8725         # clear stats.
8726         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8727         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8728
8729         # verify mdt stats first.
8730         mkdir ${testdir} || error "mkdir failed"
8731         check_stats $SINGLEMDS "mkdir" 1
8732         touch ${testdir}/${tfile} || "touch failed"
8733         check_stats $SINGLEMDS "open" 1
8734         check_stats $SINGLEMDS "close" 1
8735         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8736         check_stats $SINGLEMDS "mknod" 1
8737         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8738         check_stats $SINGLEMDS "unlink" 1
8739         rm -f ${testdir}/${tfile} || error "file remove failed"
8740         check_stats $SINGLEMDS "unlink" 2
8741
8742         # remove working dir and check mdt stats again.
8743         rmdir ${testdir} || error "rmdir failed"
8744         check_stats $SINGLEMDS "rmdir" 1
8745
8746         local testdir1=$DIR/${tdir}/stats_testdir1
8747         mkdir -p ${testdir}
8748         mkdir -p ${testdir1}
8749         touch ${testdir1}/test1
8750         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8751         check_stats $SINGLEMDS "crossdir_rename" 1
8752
8753         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8754         check_stats $SINGLEMDS "samedir_rename" 1
8755
8756         rm -rf $DIR/${tdir}
8757 }
8758 run_test 133a "Verifying MDT stats ========================================"
8759
8760 test_133b() {
8761         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8762         remote_ost_nodsh && skip "remote OST with nodsh" && return
8763         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8764         local testdir=$DIR/${tdir}/stats_testdir
8765         mkdir -p ${testdir} || error "mkdir failed"
8766         touch ${testdir}/${tfile} || "touch failed"
8767         cancel_lru_locks mdc
8768
8769         # clear stats.
8770         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8771         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8772
8773         # extra mdt stats verification.
8774         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8775         check_stats $SINGLEMDS "setattr" 1
8776         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8777         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8778         then            # LU-1740
8779                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8780                 check_stats $SINGLEMDS "getattr" 1
8781         fi
8782         $LFS df || error "lfs failed"
8783         check_stats $SINGLEMDS "statfs" 1
8784
8785         rm -rf $DIR/${tdir}
8786 }
8787 run_test 133b "Verifying extra MDT stats =================================="
8788
8789 test_133c() {
8790         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8791         remote_ost_nodsh && skip "remote OST with nodsh" && return
8792         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8793         local testdir=$DIR/${tdir}/stats_testdir
8794         test_mkdir -p ${testdir} || error "mkdir failed"
8795
8796         # verify obdfilter stats.
8797         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8798         sync
8799         cancel_lru_locks osc
8800         wait_delete_completed
8801
8802         # clear stats.
8803         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8804         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8805
8806         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8807         sync
8808         cancel_lru_locks osc
8809         check_stats ost "write" 1
8810
8811         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8812         check_stats ost "read" 1
8813
8814         > ${testdir}/${tfile} || error "truncate failed"
8815         check_stats ost "punch" 1
8816
8817         rm -f ${testdir}/${tfile} || error "file remove failed"
8818         wait_delete_completed
8819         check_stats ost "destroy" 1
8820
8821         rm -rf $DIR/${tdir}
8822 }
8823 run_test 133c "Verifying OST stats ========================================"
8824
8825 order_2() {
8826         local value=$1
8827         local orig=$value
8828         local order=1
8829
8830         while [ $value -ge 2 ]; do
8831                 order=$((order*2))
8832                 value=$((value/2))
8833         done
8834
8835         if [ $orig -gt $order ]; then
8836                 order=$((order*2))
8837         fi
8838         echo $order
8839 }
8840
8841 size_in_KMGT() {
8842     local value=$1
8843     local size=('K' 'M' 'G' 'T');
8844     local i=0
8845     local size_string=$value
8846
8847     while [ $value -ge 1024 ]; do
8848         if [ $i -gt 3 ]; then
8849             #T is the biggest unit we get here, if that is bigger,
8850             #just return XXXT
8851             size_string=${value}T
8852             break
8853         fi
8854         value=$((value >> 10))
8855         if [ $value -lt 1024 ]; then
8856             size_string=${value}${size[$i]}
8857             break
8858         fi
8859         i=$((i + 1))
8860     done
8861
8862     echo $size_string
8863 }
8864
8865 get_rename_size() {
8866     local size=$1
8867     local context=${2:-.}
8868     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8869                 grep -A1 $context |
8870                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8871     echo $sample
8872 }
8873
8874 test_133d() {
8875         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8876         remote_ost_nodsh && skip "remote OST with nodsh" && return
8877         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8878         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8879         { skip "MDS doesn't support rename stats"; return; }
8880
8881         local testdir1=$DIR/${tdir}/stats_testdir1
8882         local testdir2=$DIR/${tdir}/stats_testdir2
8883
8884         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8885
8886         mkdir -p ${testdir1} || error "mkdir failed"
8887         mkdir -p ${testdir2} || error "mkdir failed"
8888
8889         createmany -o $testdir1/test 512 || error "createmany failed"
8890
8891         # check samedir rename size
8892         mv ${testdir1}/test0 ${testdir1}/test_0
8893
8894         local testdir1_size=$(ls -l $DIR/${tdir} |
8895                 awk '/stats_testdir1/ {print $5}')
8896         local testdir2_size=$(ls -l $DIR/${tdir} |
8897                 awk '/stats_testdir2/ {print $5}')
8898
8899         testdir1_size=$(order_2 $testdir1_size)
8900         testdir2_size=$(order_2 $testdir2_size)
8901
8902         testdir1_size=$(size_in_KMGT $testdir1_size)
8903         testdir2_size=$(size_in_KMGT $testdir2_size)
8904
8905         echo "source rename dir size: ${testdir1_size}"
8906         echo "target rename dir size: ${testdir2_size}"
8907
8908         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8909         eval $cmd || error "$cmd failed"
8910         local samedir=$($cmd | grep 'same_dir')
8911         local same_sample=$(get_rename_size $testdir1_size)
8912         [ -z "$samedir" ] && error "samedir_rename_size count error"
8913         [[ $same_sample -eq 1 ]] ||
8914                 error "samedir_rename_size error $same_sample"
8915         echo "Check same dir rename stats success"
8916
8917         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8918
8919         # check crossdir rename size
8920         mv ${testdir1}/test_0 ${testdir2}/test_0
8921
8922         testdir1_size=$(ls -l $DIR/${tdir} |
8923                 awk '/stats_testdir1/ {print $5}')
8924         testdir2_size=$(ls -l $DIR/${tdir} |
8925                 awk '/stats_testdir2/ {print $5}')
8926
8927         testdir1_size=$(order_2 $testdir1_size)
8928         testdir2_size=$(order_2 $testdir2_size)
8929
8930         testdir1_size=$(size_in_KMGT $testdir1_size)
8931         testdir2_size=$(size_in_KMGT $testdir2_size)
8932
8933         echo "source rename dir size: ${testdir1_size}"
8934         echo "target rename dir size: ${testdir2_size}"
8935
8936         eval $cmd || error "$cmd failed"
8937         local crossdir=$($cmd | grep 'crossdir')
8938         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8939         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8940         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8941         [[ $src_sample -eq 1 ]] ||
8942                 error "crossdir_rename_size error $src_sample"
8943         [[ $tgt_sample -eq 1 ]] ||
8944                 error "crossdir_rename_size error $tgt_sample"
8945         echo "Check cross dir rename stats success"
8946         rm -rf $DIR/${tdir}
8947 }
8948 run_test 133d "Verifying rename_stats ========================================"
8949
8950 test_133e() {
8951         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8952         local testdir=$DIR/${tdir}/stats_testdir
8953         local ctr f0 f1 bs=32768 count=42 sum
8954
8955         remote_ost_nodsh && skip "remote OST with nodsh" && return
8956         mkdir -p ${testdir} || error "mkdir failed"
8957
8958         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8959
8960         for ctr in {write,read}_bytes; do
8961                 sync
8962                 cancel_lru_locks osc
8963
8964                 do_facet ost1 $LCTL set_param -n \
8965                         "obdfilter.*.exports.clear=clear"
8966
8967                 if [ $ctr = write_bytes ]; then
8968                         f0=/dev/zero
8969                         f1=${testdir}/${tfile}
8970                 else
8971                         f0=${testdir}/${tfile}
8972                         f1=/dev/null
8973                 fi
8974
8975                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8976                         error "dd failed"
8977                 sync
8978                 cancel_lru_locks osc
8979
8980                 sum=$(do_facet ost1 $LCTL get_param \
8981                         "obdfilter.*.exports.*.stats" |
8982                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
8983                                 $1 == ctr { sum += $7 }
8984                                 END { printf("%0.0f", sum) }')
8985
8986                 if ((sum != bs * count)); then
8987                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8988                 fi
8989         done
8990
8991         rm -rf $DIR/${tdir}
8992 }
8993 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8994
8995 test_133f() {
8996         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8997         local facet
8998
8999         # First without trusting modes.
9000         find $proc_dirs -exec cat '{}' \; &> /dev/null
9001
9002         # Second verifying readability.
9003         find $proc_dirs \
9004                 -type f \
9005                 -exec cat '{}' \; &> /dev/null ||
9006                         error "proc file read failed"
9007
9008         for facet in $SINGLEMDS ost1; do
9009                 do_facet $facet find $proc_dirs \
9010                         ! -name req_history \
9011                         -exec cat '{}' \\\; &> /dev/null
9012
9013                 do_facet $facet find $proc_dirs \
9014                         ! -name req_history \
9015                         -type f \
9016                         -exec cat '{}' \\\; &> /dev/null ||
9017                                 error "proc file read failed"
9018         done
9019 }
9020 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9021
9022 test_133g() {
9023         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9024         local facet
9025
9026         # Second verifying readability.
9027         find $proc_dirs \
9028                 -type f \
9029                 -not -name force_lbug \
9030                 -not -name changelog_mask \
9031                 -exec badarea_io '{}' \; &> /dev/null ||
9032                 error "find $proc_dirs failed"
9033
9034         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9035                 skip "Too old lustre on MDS"
9036
9037         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9038                 skip "Too old lustre on ost1"
9039
9040         for facet in $SINGLEMDS ost1; do
9041                 do_facet $facet find $proc_dirs \
9042                         -type f \
9043                         -not -name force_lbug \
9044                         -not -name changelog_mask \
9045                         -exec badarea_io '{}' \\\; &> /dev/null ||
9046                 error "$facet find $proc_dirs failed"
9047
9048         done
9049
9050         # remount the FS in case writes/reads /proc break the FS
9051         cleanup || error "failed to unmount"
9052         setup || error "failed to setup"
9053         true
9054 }
9055 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9056
9057 test_140() { #bug-17379
9058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9059         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9060         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9061         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9062
9063         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9064         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9065         local i=0
9066         while i=`expr $i + 1`; do
9067                 test_mkdir -p $i || error "Creating dir $i"
9068                 cd $i || error "Changing to $i"
9069                 ln -s ../stat stat || error "Creating stat symlink"
9070                 # Read the symlink until ELOOP present,
9071                 # not LBUGing the system is considered success,
9072                 # we didn't overrun the stack.
9073                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9074                 [ $ret -ne 0 ] && {
9075                         if [ $ret -eq 40 ]; then
9076                                 break  # -ELOOP
9077                         else
9078                                 error "Open stat symlink"
9079                                 return
9080                         fi
9081                 }
9082         done
9083         i=`expr $i - 1`
9084         echo "The symlink depth = $i"
9085         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9086                                         error "Invalid symlink depth"
9087
9088         # Test recursive symlink
9089         ln -s symlink_self symlink_self
9090         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9091         echo "open symlink_self returns $ret"
9092         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9093 }
9094 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9095
9096 test_150() {
9097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9098         local TF="$TMP/$tfile"
9099
9100         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9101         cp $TF $DIR/$tfile
9102         cancel_lru_locks osc
9103         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9104         remount_client $MOUNT
9105         df -P $MOUNT
9106         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9107
9108         $TRUNCATE $TF 6000
9109         $TRUNCATE $DIR/$tfile 6000
9110         cancel_lru_locks osc
9111         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9112
9113         echo "12345" >>$TF
9114         echo "12345" >>$DIR/$tfile
9115         cancel_lru_locks osc
9116         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9117
9118         echo "12345" >>$TF
9119         echo "12345" >>$DIR/$tfile
9120         cancel_lru_locks osc
9121         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9122
9123         rm -f $TF
9124         true
9125 }
9126 run_test 150 "truncate/append tests"
9127
9128 #LU-2902 roc_hit was not able to read all values from lproc
9129 function roc_hit_init() {
9130         local list=$(comma_list $(osts_nodes))
9131         local dir=$DIR/$tdir-check
9132         local file=$dir/file
9133         local BEFORE
9134         local AFTER
9135         local idx
9136
9137         test_mkdir -p $dir
9138         #use setstripe to do a write to every ost
9139         for i in $(seq 0 $((OSTCOUNT-1))); do
9140                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9141                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9142                 idx=$(printf %04x $i)
9143                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9144                         awk '$1 == "cache_access" {sum += $2}
9145                                 END { printf("%0.0f", sum) }')
9146
9147                 cancel_lru_locks osc
9148                 cat $file >/dev/null
9149
9150                 AFTER=$(get_osd_param $list *OST*$idx stats |
9151                         awk '$1 == "cache_access" {sum += $2}
9152                                 END { printf("%0.0f", sum) }')
9153
9154                 echo BEFORE:$BEFORE AFTER:$AFTER
9155                 if ! let "AFTER - BEFORE == 4"; then
9156                         rm -rf $dir
9157                         error "roc_hit is not safe to use"
9158                 fi
9159                 rm $file
9160         done
9161
9162         rm -rf $dir
9163 }
9164
9165 function roc_hit() {
9166         local list=$(comma_list $(osts_nodes))
9167         echo $(get_osd_param $list '' stats |
9168                 awk '$1 == "cache_hit" {sum += $2}
9169                         END { printf("%0.0f", sum) }')
9170 }
9171
9172 function set_cache() {
9173         local on=1
9174
9175         if [ "$2" == "off" ]; then
9176                 on=0;
9177         fi
9178         local list=$(comma_list $(osts_nodes))
9179         set_osd_param $list '' $1_cache_enable $on
9180
9181         cancel_lru_locks osc
9182 }
9183
9184 test_151() {
9185         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9186         remote_ost_nodsh && skip "remote OST with nodsh" && return
9187
9188         local CPAGES=3
9189         local list=$(comma_list $(osts_nodes))
9190
9191         # check whether obdfilter is cache capable at all
9192         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9193                 echo "not cache-capable obdfilter"
9194                 return 0
9195         fi
9196
9197         # check cache is enabled on all obdfilters
9198         if get_osd_param $list '' read_cache_enable | grep 0; then
9199                 echo "oss cache is disabled"
9200                 return 0
9201         fi
9202
9203         set_osd_param $list '' writethrough_cache_enable 1
9204
9205         # check write cache is enabled on all obdfilters
9206         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9207                 echo "oss write cache is NOT enabled"
9208                 return 0
9209         fi
9210
9211         roc_hit_init
9212
9213         #define OBD_FAIL_OBD_NO_LRU  0x609
9214         do_nodes $list $LCTL set_param fail_loc=0x609
9215
9216         # pages should be in the case right after write
9217         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9218                 error "dd failed"
9219
9220         local BEFORE=$(roc_hit)
9221         cancel_lru_locks osc
9222         cat $DIR/$tfile >/dev/null
9223         local AFTER=$(roc_hit)
9224
9225         do_nodes $list $LCTL set_param fail_loc=0
9226
9227         if ! let "AFTER - BEFORE == CPAGES"; then
9228                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9229         fi
9230
9231         # the following read invalidates the cache
9232         cancel_lru_locks osc
9233         set_osd_param $list '' read_cache_enable 0
9234         cat $DIR/$tfile >/dev/null
9235
9236         # now data shouldn't be found in the cache
9237         BEFORE=$(roc_hit)
9238         cancel_lru_locks osc
9239         cat $DIR/$tfile >/dev/null
9240         AFTER=$(roc_hit)
9241         if let "AFTER - BEFORE != 0"; then
9242                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9243         fi
9244
9245         set_osd_param $list '' read_cache_enable 1
9246         rm -f $DIR/$tfile
9247 }
9248 run_test 151 "test cache on oss and controls ==============================="
9249
9250 test_152() {
9251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9252         local TF="$TMP/$tfile"
9253
9254         # simulate ENOMEM during write
9255 #define OBD_FAIL_OST_NOMEM      0x226
9256         lctl set_param fail_loc=0x80000226
9257         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9258         cp $TF $DIR/$tfile
9259         sync || error "sync failed"
9260         lctl set_param fail_loc=0
9261
9262         # discard client's cache
9263         cancel_lru_locks osc
9264
9265         # simulate ENOMEM during read
9266         lctl set_param fail_loc=0x80000226
9267         cmp $TF $DIR/$tfile || error "cmp failed"
9268         lctl set_param fail_loc=0
9269
9270         rm -f $TF
9271 }
9272 run_test 152 "test read/write with enomem ============================"
9273
9274 test_153() {
9275         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9276 }
9277 run_test 153 "test if fdatasync does not crash ======================="
9278
9279 dot_lustre_fid_permission_check() {
9280         local fid=$1
9281         local ffid=$MOUNT/.lustre/fid/$fid
9282         local test_dir=$2
9283
9284         echo "stat fid $fid"
9285         stat $ffid > /dev/null || error "stat $ffid failed."
9286         echo "touch fid $fid"
9287         touch $ffid || error "touch $ffid failed."
9288         echo "write to fid $fid"
9289         cat /etc/hosts > $ffid || error "write $ffid failed."
9290         echo "read fid $fid"
9291         diff /etc/hosts $ffid || error "read $ffid failed."
9292         echo "append write to fid $fid"
9293         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9294         echo "rename fid $fid"
9295         mv $ffid $test_dir/$tfile.1 &&
9296                 error "rename $ffid to $tfile.1 should fail."
9297         touch $test_dir/$tfile.1
9298         mv $test_dir/$tfile.1 $ffid &&
9299                 error "rename $tfile.1 to $ffid should fail."
9300         rm -f $test_dir/$tfile.1
9301         echo "truncate fid $fid"
9302         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9303         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9304                 echo "link fid $fid"
9305                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9306         fi
9307         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9308                 echo "setfacl fid $fid"
9309                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9310                 echo "getfacl fid $fid"
9311                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9312         fi
9313         echo "unlink fid $fid"
9314         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9315         echo "mknod fid $fid"
9316         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9317
9318         fid=[0xf00000400:0x1:0x0]
9319         ffid=$MOUNT/.lustre/fid/$fid
9320
9321         echo "stat non-exist fid $fid"
9322         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9323         echo "write to non-exist fid $fid"
9324         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9325         echo "link new fid $fid"
9326         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9327
9328         mkdir -p $test_dir/$tdir
9329         touch $test_dir/$tdir/$tfile
9330         fid=$($LFS path2fid $test_dir/$tdir)
9331         rc=$?
9332         [ $rc -ne 0 ] &&
9333                 error "error: could not get fid for $test_dir/$dir/$tfile."
9334
9335         ffid=$MOUNT/.lustre/fid/$fid
9336
9337         echo "ls $fid"
9338         ls $ffid > /dev/null || error "ls $ffid failed."
9339         echo "touch $fid/$tfile.1"
9340         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9341
9342         echo "touch $MOUNT/.lustre/fid/$tfile"
9343         touch $MOUNT/.lustre/fid/$tfile && \
9344                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9345
9346         echo "setxattr to $MOUNT/.lustre/fid"
9347         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9348
9349         echo "listxattr for $MOUNT/.lustre/fid"
9350         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9351
9352         echo "delxattr from $MOUNT/.lustre/fid"
9353         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9354
9355         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9356         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9357                 error "touch invalid fid should fail."
9358
9359         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9360         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9361                 error "touch non-normal fid should fail."
9362
9363         echo "rename $tdir to $MOUNT/.lustre/fid"
9364         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9365                 error "rename to $MOUNT/.lustre/fid should fail."
9366
9367         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9368         then            # LU-3547
9369                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9370                 local new_obf_mode=777
9371
9372                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9373                 chmod $new_obf_mode $DIR/.lustre/fid ||
9374                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9375
9376                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9377                 [ $obf_mode -eq $new_obf_mode ] ||
9378                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9379
9380                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9381                 chmod $old_obf_mode $DIR/.lustre/fid ||
9382                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9383         fi
9384
9385         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9386         fid=$($LFS path2fid $test_dir/$tfile-2)
9387         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9388         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9389                 error "create lov data thru .lustre should fail."
9390         echo "cp /etc/passwd $test_dir/$tfile-2"
9391         cp /etc/passwd $test_dir/$tfile-2 ||
9392                 error "copy to $test_dir/$tfile-2 failed."
9393         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9394         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9395                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9396
9397         rm -rf $test_dir/tfile.lnk
9398         rm -rf $test_dir/$tfile-2
9399 }
9400
9401 test_154A() {
9402         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9403                 skip "Need MDS version at least 2.4.1" && return
9404
9405         touch $DIR/$tfile
9406         local FID=$($LFS path2fid $DIR/$tfile)
9407         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9408
9409         # check that we get the same pathname back
9410         local FOUND=$($LFS fid2path $MOUNT $FID)
9411         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9412         [ "$FOUND" != "$DIR/$tfile" ] &&
9413                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9414
9415         rm -rf $DIR/$tfile
9416 }
9417 run_test 154A "lfs path2fid and fid2path basic checks"
9418
9419 test_154a() {
9420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9421         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9422                 { skip "Need MDS version at least 2.2.51"; return 0; }
9423         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9424
9425         cp /etc/hosts $DIR/$tfile
9426
9427         fid=$($LFS path2fid $DIR/$tfile)
9428         rc=$?
9429         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9430
9431         dot_lustre_fid_permission_check "$fid" $DIR ||
9432                 error "dot lustre permission check $fid failed"
9433
9434         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9435
9436         touch $MOUNT/.lustre/file &&
9437                 error "creation is not allowed under .lustre"
9438
9439         mkdir $MOUNT/.lustre/dir &&
9440                 error "mkdir is not allowed under .lustre"
9441
9442         rm -rf $DIR/$tfile
9443 }
9444 run_test 154a "Open-by-FID"
9445
9446 test_154b() {
9447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9448         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9449                 { skip "Need MDS version at least 2.2.51"; return 0; }
9450
9451         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9452
9453         local remote_dir=$DIR/$tdir/remote_dir
9454         local MDTIDX=1
9455         local rc=0
9456
9457         mkdir -p $DIR/$tdir
9458         $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
9459                 error "create remote directory failed"
9460
9461         cp /etc/hosts $remote_dir/$tfile
9462
9463         fid=$($LFS path2fid $remote_dir/$tfile)
9464         rc=$?
9465         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9466
9467         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9468                 error "dot lustre permission check $fid failed"
9469         rm -rf $DIR/$tdir
9470 }
9471 run_test 154b "Open-by-FID for remote directory"
9472
9473 test_154c() {
9474         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9475                 skip "Need MDS version at least 2.4.1" && return
9476
9477         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9478         local FID1=$($LFS path2fid $DIR/$tfile.1)
9479         local FID2=$($LFS path2fid $DIR/$tfile.2)
9480         local FID3=$($LFS path2fid $DIR/$tfile.3)
9481
9482         local N=1
9483         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9484                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9485                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9486                 local want=FID$N
9487                 [ "$FID" = "${!want}" ] ||
9488                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9489                 N=$((N + 1))
9490         done
9491
9492         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9493                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9494                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9495                 N=$((N + 1))
9496         done
9497 }
9498 run_test 154c "lfs path2fid and fid2path multiple arguments"
9499
9500 test_154d() {
9501         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9502                 skip "Need MDS version at least 2.5.53" && return
9503
9504         if remote_mds; then
9505                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9506         else
9507                 nid="0@lo"
9508         fi
9509         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9510         local fd
9511         local cmd
9512
9513         rm -f $DIR/$tfile
9514         touch $DIR/$tfile
9515
9516         fid=$($LFS path2fid $DIR/$tfile)
9517         # Open the file
9518         fd=$(free_fd)
9519         cmd="exec $fd<$DIR/$tfile"
9520         eval $cmd
9521         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9522         echo $fid_list | grep $fid
9523         rc=$?
9524
9525         cmd="exec $fd>/dev/null"
9526         eval $cmd
9527         if [ $rc -ne 0 ]; then
9528                 error "FID $fid not found in open files list $fid_list"
9529         fi
9530 }
9531 run_test 154d "Verify open file fid"
9532
9533 test_155_small_load() {
9534     local temp=$TMP/$tfile
9535     local file=$DIR/$tfile
9536
9537     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9538         error "dd of=$temp bs=6096 count=1 failed"
9539     cp $temp $file
9540     cancel_lru_locks osc
9541     cmp $temp $file || error "$temp $file differ"
9542
9543     $TRUNCATE $temp 6000
9544     $TRUNCATE $file 6000
9545     cmp $temp $file || error "$temp $file differ (truncate1)"
9546
9547     echo "12345" >>$temp
9548     echo "12345" >>$file
9549     cmp $temp $file || error "$temp $file differ (append1)"
9550
9551     echo "12345" >>$temp
9552     echo "12345" >>$file
9553     cmp $temp $file || error "$temp $file differ (append2)"
9554
9555     rm -f $temp $file
9556     true
9557 }
9558
9559 test_155_big_load() {
9560     remote_ost_nodsh && skip "remote OST with nodsh" && return
9561     local temp=$TMP/$tfile
9562     local file=$DIR/$tfile
9563
9564     free_min_max
9565     local cache_size=$(do_facet ost$((MAXI+1)) \
9566         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9567     local large_file_size=$((cache_size * 2))
9568
9569     echo "OSS cache size: $cache_size KB"
9570     echo "Large file size: $large_file_size KB"
9571
9572     [ $MAXV -le $large_file_size ] && \
9573         skip_env "max available OST size needs > $large_file_size KB" && \
9574         return 0
9575
9576     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9577
9578     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9579         error "dd of=$temp bs=$large_file_size count=1k failed"
9580     cp $temp $file
9581     ls -lh $temp $file
9582     cancel_lru_locks osc
9583     cmp $temp $file || error "$temp $file differ"
9584
9585     rm -f $temp $file
9586     true
9587 }
9588
9589 test_155a() {
9590         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9591         set_cache read on
9592         set_cache writethrough on
9593         test_155_small_load
9594 }
9595 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9596
9597 test_155b() {
9598         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9599         set_cache read on
9600         set_cache writethrough off
9601         test_155_small_load
9602 }
9603 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9604
9605 test_155c() {
9606         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9607         set_cache read off
9608         set_cache writethrough on
9609         test_155_small_load
9610 }
9611 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9612
9613 test_155d() {
9614         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9615         set_cache read off
9616         set_cache writethrough off
9617         test_155_small_load
9618 }
9619 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9620
9621 test_155e() {
9622         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9623         set_cache read on
9624         set_cache writethrough on
9625         test_155_big_load
9626 }
9627 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9628
9629 test_155f() {
9630         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9631         set_cache read on
9632         set_cache writethrough off
9633         test_155_big_load
9634 }
9635 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9636
9637 test_155g() {
9638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9639         set_cache read off
9640         set_cache writethrough on
9641         test_155_big_load
9642 }
9643 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9644
9645 test_155h() {
9646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9647         set_cache read off
9648         set_cache writethrough off
9649         test_155_big_load
9650 }
9651 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9652
9653 test_156() {
9654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9655         local CPAGES=3
9656         local BEFORE
9657         local AFTER
9658         local file="$DIR/$tfile"
9659
9660         [ "$(facet_fstype ost1)" = "zfs" ] &&
9661                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9662                 return
9663
9664         roc_hit_init
9665
9666     log "Turn on read and write cache"
9667     set_cache read on
9668     set_cache writethrough on
9669
9670     log "Write data and read it back."
9671     log "Read should be satisfied from the cache."
9672     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9673     BEFORE=`roc_hit`
9674     cancel_lru_locks osc
9675     cat $file >/dev/null
9676     AFTER=`roc_hit`
9677     if ! let "AFTER - BEFORE == CPAGES"; then
9678         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9679     else
9680         log "cache hits:: before: $BEFORE, after: $AFTER"
9681     fi
9682
9683     log "Read again; it should be satisfied from the cache."
9684     BEFORE=$AFTER
9685     cancel_lru_locks osc
9686     cat $file >/dev/null
9687     AFTER=`roc_hit`
9688     if ! let "AFTER - BEFORE == CPAGES"; then
9689         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9690     else
9691         log "cache hits:: before: $BEFORE, after: $AFTER"
9692     fi
9693
9694
9695     log "Turn off the read cache and turn on the write cache"
9696     set_cache read off
9697     set_cache writethrough on
9698
9699     log "Read again; it should be satisfied from the cache."
9700     BEFORE=`roc_hit`
9701     cancel_lru_locks osc
9702     cat $file >/dev/null
9703     AFTER=`roc_hit`
9704     if ! let "AFTER - BEFORE == CPAGES"; then
9705         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9706     else
9707         log "cache hits:: before: $BEFORE, after: $AFTER"
9708     fi
9709
9710     log "Read again; it should not be satisfied from the cache."
9711     BEFORE=$AFTER
9712     cancel_lru_locks osc
9713     cat $file >/dev/null
9714     AFTER=`roc_hit`
9715     if ! let "AFTER - BEFORE == 0"; then
9716         error "IN CACHE: before: $BEFORE, after: $AFTER"
9717     else
9718         log "cache hits:: before: $BEFORE, after: $AFTER"
9719     fi
9720
9721     log "Write data and read it back."
9722     log "Read should be satisfied from the cache."
9723     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9724     BEFORE=`roc_hit`
9725     cancel_lru_locks osc
9726     cat $file >/dev/null
9727     AFTER=`roc_hit`
9728     if ! let "AFTER - BEFORE == CPAGES"; then
9729         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9730     else
9731         log "cache hits:: before: $BEFORE, after: $AFTER"
9732     fi
9733
9734     log "Read again; it should not be satisfied from the cache."
9735     BEFORE=$AFTER
9736     cancel_lru_locks osc
9737     cat $file >/dev/null
9738     AFTER=`roc_hit`
9739     if ! let "AFTER - BEFORE == 0"; then
9740         error "IN CACHE: before: $BEFORE, after: $AFTER"
9741     else
9742         log "cache hits:: before: $BEFORE, after: $AFTER"
9743     fi
9744
9745
9746     log "Turn off read and write cache"
9747     set_cache read off
9748     set_cache writethrough off
9749
9750     log "Write data and read it back"
9751     log "It should not be satisfied from the cache."
9752     rm -f $file
9753     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9754     cancel_lru_locks osc
9755     BEFORE=`roc_hit`
9756     cat $file >/dev/null
9757     AFTER=`roc_hit`
9758         if ! let "AFTER - BEFORE == 0"; then
9759                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9760         else
9761                 log "cache hits:: before: $BEFORE, after: $AFTER"
9762         fi
9763
9764     log "Turn on the read cache and turn off the write cache"
9765     set_cache read on
9766     set_cache writethrough off
9767
9768     log "Write data and read it back"
9769     log "It should not be satisfied from the cache."
9770     rm -f $file
9771     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9772     BEFORE=`roc_hit`
9773     cancel_lru_locks osc
9774     cat $file >/dev/null
9775     AFTER=`roc_hit`
9776         if ! let "AFTER - BEFORE == 0"; then
9777                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9778         else
9779                 log "cache hits:: before: $BEFORE, after: $AFTER"
9780         fi
9781
9782     log "Read again; it should be satisfied from the cache."
9783     BEFORE=`roc_hit`
9784     cancel_lru_locks osc
9785     cat $file >/dev/null
9786     AFTER=`roc_hit`
9787     if ! let "AFTER - BEFORE == CPAGES"; then
9788         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9789     else
9790         log "cache hits:: before: $BEFORE, after: $AFTER"
9791     fi
9792
9793     rm -f $file
9794 }
9795 run_test 156 "Verification of tunables ============================"
9796
9797 #Changelogs
9798 err17935 () {
9799         if [[ $MDSCOUNT -gt 1 ]]; then
9800                 error_ignore bz17935 $*
9801         else
9802                 error $*
9803         fi
9804 }
9805
9806 changelog_chmask()
9807 {
9808         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9809
9810         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9811
9812         if [ $MASK -eq 1 ]; then
9813                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9814         else
9815                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9816         fi
9817 }
9818
9819 changelog_extract_field() {
9820         local mdt=$1
9821         local cltype=$2
9822         local file=$3
9823         local identifier=$4
9824
9825         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9826                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9827                 tail -1
9828 }
9829
9830 test_160a() {
9831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9832         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9833         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9834                 { skip "Need MDS version at least 2.2.0"; return; }
9835
9836         local CL_USERS="mdd.$MDT0.changelog_users"
9837         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9838         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9839         echo "Registered as changelog user $USER"
9840         $GET_CL_USERS | grep -q $USER ||
9841                 error "User $USER not found in changelog_users"
9842
9843         # change something
9844         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9845         touch $DIR/$tdir/pics/2008/zachy/timestamp
9846         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9847         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9848         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9849         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9850         rm $DIR/$tdir/pics/desktop.jpg
9851
9852         $LFS changelog $MDT0 | tail -5
9853
9854         echo "verifying changelog mask"
9855         changelog_chmask "MKDIR"
9856         changelog_chmask "CLOSE"
9857
9858         test_mkdir -p $DIR/$tdir/pics/zach/sofia
9859         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9860
9861         changelog_chmask "MKDIR"
9862         changelog_chmask "CLOSE"
9863
9864         test_mkdir -p $DIR/$tdir/pics/2008/sofia
9865         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9866
9867         $LFS changelog $MDT0
9868         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9869         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9870         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9871         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9872
9873         # verify contents
9874         echo "verifying target fid"
9875         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
9876         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9877         [ "$fidc" == "$fidf" ] ||
9878                 err17935 "fid in changelog $fidc != file fid $fidf"
9879         echo "verifying parent fid"
9880         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
9881         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9882         [ "$fidc" == "$fidf" ] ||
9883                 err17935 "pfid in changelog $fidc != dir fid $fidf"
9884
9885         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9886         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9887         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9888         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9889         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
9890                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
9891
9892         MIN_REC=$($GET_CL_USERS |
9893                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
9894         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
9895         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9896         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
9897                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
9898
9899         # LU-3446 changelog index reset on MDT restart
9900         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9901         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9902         $LFS changelog_clear $MDT0 $USER 0
9903         stop $SINGLEMDS || error "Fail to stop MDT."
9904         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9905         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9906         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
9907         [ $CUR_REC1 == $CUR_REC2 ] ||
9908                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
9909
9910         echo "verifying user deregister"
9911         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9912         $GET_CL_USERS | grep -q $USER &&
9913                 error "User $USER still in changelog_users"
9914
9915         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
9916         if [ $USERS -eq 0 ]; then
9917                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9918                 touch $DIR/$tdir/chloe
9919                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9920                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
9921                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9922         else
9923                 echo "$USERS other changelog users; can't verify off"
9924         fi
9925 }
9926 run_test 160a "changelog sanity"
9927
9928 test_160b() { # LU-3587
9929         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9930         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9931         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9932                 { skip "Need MDS version at least 2.2.0"; return; }
9933
9934         local CL_USERS="mdd.$MDT0.changelog_users"
9935         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9936         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9937         echo "Registered as changelog user $USER"
9938         $GET_CL_USERS | grep -q $USER ||
9939                 error "User $USER not found in changelog_users"
9940
9941         local LONGNAME1=$(str_repeat a 255)
9942         local LONGNAME2=$(str_repeat b 255)
9943
9944         cd $DIR
9945         echo "creating very long named file"
9946         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
9947         echo "moving very long named file"
9948         mv $LONGNAME1 $LONGNAME2
9949
9950         $LFS changelog $MDT0 | grep RENME
9951
9952         echo "deregistering $USER"
9953         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9954
9955         rm -f $LONGNAME2
9956 }
9957 run_test 160b "Verify that very long rename doesn't crash in changelog"
9958
9959 test_160c() {
9960         local rc=0
9961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9962
9963         # Registration step
9964         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
9965                 changelog_register -n)
9966
9967         rm -rf $DIR/$tdir
9968         mkdir -p $DIR/$tdir
9969         $MCREATE $DIR/$tdir/foo_160c
9970         changelog_chmask "TRUNC"
9971         $TRUNCATE $DIR/$tdir/foo_160c 200
9972         changelog_chmask "TRUNC"
9973         $TRUNCATE $DIR/$tdir/foo_160c 199
9974         $LFS changelog $MDT0
9975         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
9976         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
9977         $LFS changelog_clear $MDT0 $USER 0
9978
9979         # Deregistration step
9980         echo "deregistering $USER"
9981         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9982 }
9983 run_test 160c "verify that changelog log catch the truncate event"
9984
9985 test_161a() {
9986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9987         test_mkdir -p -c1 $DIR/$tdir
9988         cp /etc/hosts $DIR/$tdir/$tfile
9989         test_mkdir -c1 $DIR/$tdir/foo1
9990         test_mkdir -c1 $DIR/$tdir/foo2
9991         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9992         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9993         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9994         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9995         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
9996         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9997                 $LFS fid2path $DIR $FID
9998                 err17935 "bad link ea"
9999         fi
10000     # middle
10001     rm $DIR/$tdir/foo2/zachary
10002     # last
10003     rm $DIR/$tdir/foo2/thor
10004     # first
10005     rm $DIR/$tdir/$tfile
10006     # rename
10007     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10008     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10009         then
10010         $LFS fid2path $DIR $FID
10011         err17935 "bad link rename"
10012     fi
10013     rm $DIR/$tdir/foo2/maggie
10014
10015         # overflow the EA
10016         local longname=filename_avg_len_is_thirty_two_
10017         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10018                 error "failed to hardlink many files"
10019         links=$($LFS fid2path $DIR $FID | wc -l)
10020         echo -n "${links}/1000 links in link EA"
10021         [[ $links -gt 60 ]] ||
10022                 err17935 "expected at least 60 links in link EA"
10023         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10024                 error "failed to unlink many hardlinks"
10025 }
10026 run_test 161a "link ea sanity"
10027
10028 test_161b() {
10029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10030         [ $MDSCOUNT -lt 2 ] &&
10031                 skip "skipping remote directory test" && return
10032         local MDTIDX=1
10033         local remote_dir=$DIR/$tdir/remote_dir
10034
10035         mkdir -p $DIR/$tdir
10036         $LFS mkdir -i $MDTIDX $remote_dir ||
10037                 error "create remote directory failed"
10038
10039         cp /etc/hosts $remote_dir/$tfile
10040         mkdir -p $remote_dir/foo1
10041         mkdir -p $remote_dir/foo2
10042         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10043         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10044         ln $remote_dir/$tfile $remote_dir/foo1/luna
10045         ln $remote_dir/$tfile $remote_dir/foo2/thor
10046
10047         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10048                      tr -d ']')
10049         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10050                 $LFS fid2path $DIR $FID
10051                 err17935 "bad link ea"
10052         fi
10053         # middle
10054         rm $remote_dir/foo2/zachary
10055         # last
10056         rm $remote_dir/foo2/thor
10057         # first
10058         rm $remote_dir/$tfile
10059         # rename
10060         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10061         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10062         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10063                 $LFS fid2path $DIR $FID
10064                 err17935 "bad link rename"
10065         fi
10066         rm $remote_dir/foo2/maggie
10067
10068         # overflow the EA
10069         local longname=filename_avg_len_is_thirty_two_
10070         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10071                 error "failed to hardlink many files"
10072         links=$($LFS fid2path $DIR $FID | wc -l)
10073         echo -n "${links}/1000 links in link EA"
10074         [[ ${links} -gt 60 ]] ||
10075                 err17935 "expected at least 60 links in link EA"
10076         unlinkmany $remote_dir/foo2/$longname 1000 ||
10077         error "failed to unlink many hardlinks"
10078 }
10079 run_test 161b "link ea sanity under remote directory"
10080
10081 test_161c() {
10082         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10083         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10084                 skip "Need MDS version at least 2.1.5" && return
10085
10086         # define CLF_RENAME_LAST 0x0001
10087         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10088         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10089                 changelog_register -n)
10090         rm -rf $DIR/$tdir
10091         mkdir -p $DIR/$tdir
10092         touch $DIR/$tdir/foo_161c
10093         touch $DIR/$tdir/bar_161c
10094         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10095         $LFS changelog $MDT0 | grep RENME
10096         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10097                 cut -f5 -d' ')
10098         $LFS changelog_clear $MDT0 $USER 0
10099         if [ x$flags != "x0x1" ]; then
10100                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10101                         $USER
10102                 error "flag $flags is not 0x1"
10103         fi
10104         echo "rename overwrite a target having nlink = 1," \
10105                 "changelog record has flags of $flags"
10106
10107         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10108         touch $DIR/$tdir/foo_161c
10109         touch $DIR/$tdir/bar_161c
10110         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10111         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10112         $LFS changelog $MDT0 | grep RENME
10113         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10114         $LFS changelog_clear $MDT0 $USER 0
10115         if [ x$flags != "x0x0" ]; then
10116                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10117                         $USER
10118                 error "flag $flags is not 0x0"
10119         fi
10120         echo "rename overwrite a target having nlink > 1," \
10121                 "changelog record has flags of $flags"
10122
10123         # rename doesn't overwrite a target (changelog flag 0x0)
10124         touch $DIR/$tdir/foo_161c
10125         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10126         $LFS changelog $MDT0 | grep RENME
10127         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10128         $LFS changelog_clear $MDT0 $USER 0
10129         if [ x$flags != "x0x0" ]; then
10130                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10131                         $USER
10132                 error "flag $flags is not 0x0"
10133         fi
10134         echo "rename doesn't overwrite a target," \
10135                 "changelog record has flags of $flags"
10136
10137         # define CLF_UNLINK_LAST 0x0001
10138         # unlink a file having nlink = 1 (changelog flag 0x1)
10139         rm -f $DIR/$tdir/foo2_161c
10140         $LFS changelog $MDT0 | grep UNLNK
10141         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10142         $LFS changelog_clear $MDT0 $USER 0
10143         if [ x$flags != "x0x1" ]; then
10144                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10145                         $USER
10146                 error "flag $flags is not 0x1"
10147         fi
10148         echo "unlink a file having nlink = 1," \
10149                 "changelog record has flags of $flags"
10150
10151         # unlink a file having nlink > 1 (changelog flag 0x0)
10152         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10153         rm -f $DIR/$tdir/foobar_161c
10154         $LFS changelog $MDT0 | grep UNLNK
10155         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10156         $LFS changelog_clear $MDT0 $USER 0
10157         if [ x$flags != "x0x0" ]; then
10158                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10159                         $USER
10160                 error "flag $flags is not 0x0"
10161         fi
10162         echo "unlink a file having nlink > 1," \
10163                 "changelog record has flags of $flags"
10164         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10165 }
10166 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10167
10168 check_path() {
10169     local expected=$1
10170     shift
10171     local fid=$2
10172
10173     local path=$(${LFS} fid2path $*)
10174     RC=$?
10175
10176     if [ $RC -ne 0 ]; then
10177         err17935 "path looked up of $expected failed. Error $RC"
10178         return $RC
10179     elif [ "${path}" != "${expected}" ]; then
10180         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10181         return 2
10182     fi
10183     echo "fid $fid resolves to path $path (expected $expected)"
10184 }
10185
10186 test_162() {
10187         # Make changes to filesystem
10188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10189         test_mkdir -p -c1 $DIR/$tdir/d2
10190         touch $DIR/$tdir/d2/$tfile
10191         touch $DIR/$tdir/d2/x1
10192         touch $DIR/$tdir/d2/x2
10193         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10194         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10195         # regular file
10196         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10197         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
10198
10199         # softlink
10200         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10201         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10202         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
10203
10204         # softlink to wrong file
10205         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10206         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10207         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
10208
10209         # hardlink
10210         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10211         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10212         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10213         # fid2path dir/fsname should both work
10214         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
10215         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
10216
10217         # hardlink count: check that there are 2 links
10218         # Doesnt work with CMD yet: 17935
10219         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10220                 err17935 "expected 2 links"
10221
10222         # hardlink indexing: remove the first link
10223         rm $DIR/$tdir/d2/p/q/r/hlink
10224         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
10225
10226         return 0
10227 }
10228 run_test 162 "path lookup sanity"
10229
10230 test_162b() {
10231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10232         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10233
10234         mkdir $DIR/$tdir
10235         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10236                                 error "create striped dir failed"
10237
10238         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10239                                         tail -n 1 | awk '{print $2}')
10240         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10241
10242         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10243         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10244
10245         # regular file
10246         for ((i=0;i<5;i++)); do
10247                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10248                         error "get fid for f$i failed"
10249                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
10250
10251                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10252                         error "get fid for d$i failed"
10253                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
10254         done
10255
10256         return 0
10257 }
10258 run_test 162b "striped directory path lookup sanity"
10259
10260 test_169() {
10261         # do directio so as not to populate the page cache
10262         log "creating a 10 Mb file"
10263         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10264         log "starting reads"
10265         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10266         log "truncating the file"
10267         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10268         log "killing dd"
10269         kill %+ || true # reads might have finished
10270         echo "wait until dd is finished"
10271         wait
10272         log "removing the temporary file"
10273         rm -rf $DIR/$tfile || error "tmp file removal failed"
10274 }
10275 run_test 169 "parallel read and truncate should not deadlock"
10276
10277 test_170() {
10278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10279         $LCTL clear     # bug 18514
10280         $LCTL debug_daemon start $TMP/${tfile}_log_good
10281         touch $DIR/$tfile
10282         $LCTL debug_daemon stop
10283         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10284                error "sed failed to read log_good"
10285
10286         $LCTL debug_daemon start $TMP/${tfile}_log_good
10287         rm -rf $DIR/$tfile
10288         $LCTL debug_daemon stop
10289
10290         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10291                error "lctl df log_bad failed"
10292
10293         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10294         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10295
10296         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10297         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10298
10299         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10300                 error "bad_line good_line1 good_line2 are empty"
10301
10302         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10303         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10304         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10305
10306         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10307         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10308         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10309
10310         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10311                 error "bad_line_new good_line_new are empty"
10312
10313         local expected_good=$((good_line1 + good_line2*2))
10314
10315         rm -f $TMP/${tfile}*
10316         # LU-231, short malformed line may not be counted into bad lines
10317         if [ $bad_line -ne $bad_line_new ] &&
10318                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10319                 error "expected $bad_line bad lines, but got $bad_line_new"
10320                 return 1
10321         fi
10322
10323         if [ $expected_good -ne $good_line_new ]; then
10324                 error "expected $expected_good good lines, but got $good_line_new"
10325                 return 2
10326         fi
10327         true
10328 }
10329 run_test 170 "test lctl df to handle corrupted log ====================="
10330
10331 test_171() { # bug20592
10332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10333 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10334         $LCTL set_param fail_loc=0x50e
10335         $LCTL set_param fail_val=3000
10336         multiop_bg_pause $DIR/$tfile O_s || true
10337         local MULTIPID=$!
10338         kill -USR1 $MULTIPID
10339         # cause log dump
10340         sleep 3
10341         wait $MULTIPID
10342         if dmesg | grep "recursive fault"; then
10343                 error "caught a recursive fault"
10344         fi
10345         $LCTL set_param fail_loc=0
10346         true
10347 }
10348 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10349
10350 # it would be good to share it with obdfilter-survey/libecho code
10351 setup_obdecho_osc () {
10352         local rc=0
10353         local ost_nid=$1
10354         local obdfilter_name=$2
10355         echo "Creating new osc for $obdfilter_name on $ost_nid"
10356         # make sure we can find loopback nid
10357         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10358
10359         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10360                            ${obdfilter_name}_osc_UUID || rc=2; }
10361         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10362                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10363         return $rc
10364 }
10365
10366 cleanup_obdecho_osc () {
10367         local obdfilter_name=$1
10368         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10369         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10370         return 0
10371 }
10372
10373 obdecho_test() {
10374         local OBD=$1
10375         local node=$2
10376         local pages=${3:-64}
10377         local rc=0
10378         local id
10379
10380         local count=10
10381         local obd_size=$(get_obd_size $node $OBD)
10382         local page_size=$(get_page_size $node)
10383         if [[ -n "$obd_size" ]]; then
10384                 local new_count=$((obd_size / (pages * page_size / 1024)))
10385                 [[ $new_count -ge $count ]] || count=$new_count
10386         fi
10387
10388         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10389         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10390                            rc=2; }
10391         if [ $rc -eq 0 ]; then
10392             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10393             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10394         fi
10395         echo "New object id is $id"
10396         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10397                            rc=4; }
10398         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10399                            "test_brw $count w v $pages $id" || rc=4; }
10400         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10401                            rc=4; }
10402         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10403                                         "cleanup" || rc=5; }
10404         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10405                                         "detach" || rc=6; }
10406         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10407         return $rc
10408 }
10409
10410 test_180a() {
10411         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10412         remote_ost_nodsh && skip "remote OST with nodsh" && return
10413         local rc=0
10414         local rmmod_local=0
10415
10416         if ! module_loaded obdecho; then
10417             load_module obdecho/obdecho
10418             rmmod_local=1
10419         fi
10420
10421         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10422         local host=$(lctl get_param -n osc.$osc.import |
10423                              awk '/current_connection:/ {print $2}' )
10424         local target=$(lctl get_param -n osc.$osc.import |
10425                              awk '/target:/ {print $2}' )
10426         target=${target%_UUID}
10427
10428         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10429         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10430         [[ -n $target ]] && cleanup_obdecho_osc $target
10431         [ $rmmod_local -eq 1 ] && rmmod obdecho
10432         return $rc
10433 }
10434 run_test 180a "test obdecho on osc"
10435
10436 test_180b() {
10437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10438         remote_ost_nodsh && skip "remote OST with nodsh" && return
10439         local rc=0
10440         local rmmod_remote=0
10441
10442         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10443                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10444                       "modprobe obdecho; }" && rmmod_remote=1
10445         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10446         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10447         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10448         return $rc
10449 }
10450 run_test 180b "test obdecho directly on obdfilter"
10451
10452 test_180c() { # LU-2598
10453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10454         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10455                 skip "Need MDS version at least 2.4.0" && return
10456
10457         local rc=0
10458         local rmmod_remote=false
10459         local pages=16384 # 64MB bulk I/O RPC size
10460         local target
10461
10462         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10463                 rmmod_remote=true || error "failed to load module obdecho"
10464
10465         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10466                 head -n1)
10467         if [ -n "$target" ]; then
10468                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10469         else
10470                 echo "there is no obdfilter target on ost1"
10471                 rc=2
10472         fi
10473         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10474         return $rc
10475 }
10476 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10477
10478 test_181() { # bug 22177
10479         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10480         # create enough files to index the directory
10481         createmany -o $DIR/$tdir/foobar 4000
10482         # print attributes for debug purpose
10483         lsattr -d .
10484         # open dir
10485         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10486         MULTIPID=$!
10487         # remove the files & current working dir
10488         unlinkmany $DIR/$tdir/foobar 4000
10489         rmdir $DIR/$tdir
10490         kill -USR1 $MULTIPID
10491         wait $MULTIPID
10492         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10493         return 0
10494 }
10495 run_test 181 "Test open-unlinked dir ========================"
10496
10497 test_182() {
10498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10499         # disable MDC RPC lock wouldn't crash client
10500         local fcount=1000
10501         local tcount=4
10502
10503         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10504 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10505         $LCTL set_param fail_loc=0x804
10506
10507         for (( i=0; i < $tcount; i++ )) ; do
10508                 mkdir $DIR/$tdir/$i
10509                 createmany -o $DIR/$tdir/$i/f- $fcount &
10510         done
10511         wait
10512
10513         for (( i=0; i < $tcount; i++ )) ; do
10514                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10515         done
10516         wait
10517
10518         rm -rf $DIR/$tdir
10519
10520         $LCTL set_param fail_loc=0
10521 }
10522 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10523
10524 test_183() { # LU-2275
10525         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10526                 skip "Need MDS version at least 2.3.56" && return
10527
10528         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10529         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10530         echo aaa > $DIR/$tdir/$tfile
10531
10532 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10533         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10534
10535         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10536         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10537
10538         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10539
10540         # Flush negative dentry cache
10541         touch $DIR/$tdir/$tfile
10542
10543         # We are not checking for any leaked references here, they'll
10544         # become evident next time we do cleanup with module unload.
10545         rm -rf $DIR/$tdir
10546 }
10547 run_test 183 "No crash or request leak in case of strange dispositions ========"
10548
10549 # test suite 184 is for LU-2016, LU-2017
10550 test_184a() {
10551         check_swap_layouts_support && return 0
10552
10553         dir0=$DIR/$tdir/$testnum
10554         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10555         ref1=/etc/passwd
10556         ref2=/etc/group
10557         file1=$dir0/f1
10558         file2=$dir0/f2
10559         $SETSTRIPE -c1 $file1
10560         cp $ref1 $file1
10561         $SETSTRIPE -c2 $file2
10562         cp $ref2 $file2
10563         gen1=$($GETSTRIPE -g $file1)
10564         gen2=$($GETSTRIPE -g $file2)
10565
10566         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10567         gen=$($GETSTRIPE -g $file1)
10568         [[ $gen1 != $gen ]] ||
10569                 "Layout generation on $file1 does not change"
10570         gen=$($GETSTRIPE -g $file2)
10571         [[ $gen2 != $gen ]] ||
10572                 "Layout generation on $file2 does not change"
10573
10574         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10575         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10576 }
10577 run_test 184a "Basic layout swap"
10578
10579 test_184b() {
10580         check_swap_layouts_support && return 0
10581
10582         dir0=$DIR/$tdir/$testnum
10583         mkdir -p $dir0 || error "creating dir $dir0"
10584         file1=$dir0/f1
10585         file2=$dir0/f2
10586         file3=$dir0/f3
10587         dir1=$dir0/d1
10588         dir2=$dir0/d2
10589         mkdir $dir1 $dir2
10590         $SETSTRIPE -c1 $file1
10591         $SETSTRIPE -c2 $file2
10592         $SETSTRIPE -c1 $file3
10593         chown $RUNAS_ID $file3
10594         gen1=$($GETSTRIPE -g $file1)
10595         gen2=$($GETSTRIPE -g $file2)
10596
10597         $LFS swap_layouts $dir1 $dir2 &&
10598                 error "swap of directories layouts should fail"
10599         $LFS swap_layouts $dir1 $file1 &&
10600                 error "swap of directory and file layouts should fail"
10601         $RUNAS $LFS swap_layouts $file1 $file2 &&
10602                 error "swap of file we cannot write should fail"
10603         $LFS swap_layouts $file1 $file3 &&
10604                 error "swap of file with different owner should fail"
10605         /bin/true # to clear error code
10606 }
10607 run_test 184b "Forbidden layout swap (will generate errors)"
10608
10609 test_184c() {
10610         check_swap_layouts_support && return 0
10611
10612         local dir0=$DIR/$tdir/$testnum
10613         mkdir -p $dir0 || error "creating dir $dir0"
10614
10615         local ref1=$dir0/ref1
10616         local ref2=$dir0/ref2
10617         local file1=$dir0/file1
10618         local file2=$dir0/file2
10619         # create a file large enough for the concurent test
10620         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10621         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10622         echo "ref file size: ref1($(stat -c %s $ref1))," \
10623              "ref2($(stat -c %s $ref2))"
10624
10625         cp $ref2 $file2
10626         dd if=$ref1 of=$file1 bs=16k &
10627         local DD_PID=$!
10628
10629         # Make sure dd starts to copy file
10630         while [ ! -f $file1 ]; do sleep 0.1; done
10631
10632         $LFS swap_layouts $file1 $file2
10633         local rc=$?
10634         wait $DD_PID
10635         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10636         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10637
10638         # how many bytes copied before swapping layout
10639         local copied=$(stat -c %s $file2)
10640         local remaining=$(stat -c %s $ref1)
10641         remaining=$((remaining - copied))
10642         echo "Copied $copied bytes before swapping layout..."
10643
10644         cmp -n $copied $file1 $ref2 | grep differ &&
10645                 error "Content mismatch [0, $copied) of ref2 and file1"
10646         cmp -n $copied $file2 $ref1 ||
10647                 error "Content mismatch [0, $copied) of ref1 and file2"
10648         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10649                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10650
10651         # clean up
10652         rm -f $ref1 $ref2 $file1 $file2
10653 }
10654 run_test 184c "Concurrent write and layout swap"
10655
10656 test_184d() {
10657         check_swap_layouts_support && return 0
10658         [ -z "$(which getfattr 2>/dev/null)" ] &&
10659                 skip "no getfattr command" && return 0
10660
10661         local file1=$DIR/$tdir/$tfile-1
10662         local file2=$DIR/$tdir/$tfile-2
10663         local file3=$DIR/$tdir/$tfile-3
10664         local lovea1
10665         local lovea2
10666
10667         mkdir -p $DIR/$tdir
10668         touch $file1 || error "create $file1 failed"
10669         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10670                 error "create $file2 failed"
10671         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10672                 error "create $file3 failed"
10673         lovea1=$($LFS getstripe $file1 | sed 1d)
10674
10675         $LFS swap_layouts $file2 $file3 ||
10676                 error "swap $file2 $file3 layouts failed"
10677         $LFS swap_layouts $file1 $file2 ||
10678                 error "swap $file1 $file2 layouts failed"
10679
10680         lovea2=$($LFS getstripe $file2 | sed 1d)
10681         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10682
10683         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10684         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10685 }
10686 run_test 184d "allow stripeless layouts swap"
10687
10688
10689 test_185() { # LU-2441
10690         # LU-3553 - no volatile file support in old servers
10691         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10692                 { skip "Need MDS version at least 2.3.60"; return 0; }
10693
10694         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10695         touch $DIR/$tdir/spoo
10696         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10697         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10698                 error "cannot create/write a volatile file"
10699         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10700                 error "FID is still valid after close"
10701
10702         multiop_bg_pause $DIR/$tdir vVw4096_c
10703         local multi_pid=$!
10704
10705         local OLD_IFS=$IFS
10706         IFS=":"
10707         local fidv=($fid)
10708         IFS=$OLD_IFS
10709         # assume that the next FID for this client is sequential, since stdout
10710         # is unfortunately eaten by multiop_bg_pause
10711         local n=$((${fidv[1]} + 1))
10712         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10713         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10714                 error "FID is missing before close"
10715         kill -USR1 $multi_pid
10716         # 1 second delay, so if mtime change we will see it
10717         sleep 1
10718         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10719         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10720 }
10721 run_test 185 "Volatile file support"
10722
10723 test_187a() {
10724         local dir0=$DIR/$tdir/$testnum
10725         mkdir -p $dir0 || error "creating dir $dir0"
10726
10727         local file=$dir0/file1
10728         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10729         local dv1=$($LFS data_version $file)
10730         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10731         local dv2=$($LFS data_version $file)
10732         [[ $dv1 != $dv2 ]] ||
10733                 error "data version did not change on write $dv1 == $dv2"
10734
10735         # clean up
10736         rm -f $file1
10737 }
10738 run_test 187a "Test data version change"
10739
10740 test_187b() {
10741         local dir0=$DIR/$tdir/$testnum
10742         mkdir -p $dir0 || error "creating dir $dir0"
10743
10744         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10745         [[ ${DV[0]} != ${DV[1]} ]] ||
10746                 error "data version did not change on write"\
10747                       " ${DV[0]} == ${DV[1]}"
10748
10749         # clean up
10750         rm -f $file1
10751 }
10752 run_test 187b "Test data version change on volatile file"
10753
10754 # OST pools tests
10755 check_file_in_pool()
10756 {
10757         local file=$1
10758         local pool=$2
10759         local tlist="$3"
10760         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10761         for i in $res
10762         do
10763                 for t in $tlist ; do
10764                         [ "$i" -eq "$t" ] && continue 2
10765                 done
10766
10767                 echo "pool list: $tlist"
10768                 echo "striping: $res"
10769                 error_noexit "$file not allocated in $pool"
10770                 return 1
10771         done
10772         return 0
10773 }
10774
10775 pool_add() {
10776         echo "Creating new pool"
10777         local pool=$1
10778
10779         create_pool $FSNAME.$pool ||
10780                 { error_noexit "No pool created, result code $?"; return 1; }
10781         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10782                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10783 }
10784
10785 pool_add_targets() {
10786         echo "Adding targets to pool"
10787         local pool=$1
10788         local first=$2
10789         local last=$3
10790         local step=${4:-1}
10791
10792         local list=$(seq $first $step $last)
10793
10794         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10795         do_facet mgs $LCTL pool_add \
10796                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10797         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10798                         | sort -u | tr '\n' ' ' " "$t" || { 
10799                 error_noexit "Add to pool failed"
10800                 return 1
10801         }
10802         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10803         local addcount=$(((last - first) / step + 1))
10804         [ $lfscount -eq $addcount ] || {
10805                 error_noexit "lfs pool_list bad ost count" \
10806                                                 "$lfscount != $addcount"
10807                 return 2
10808         }
10809 }
10810
10811 pool_set_dir() {
10812         local pool=$1
10813         local tdir=$2
10814         echo "Setting pool on directory $tdir"
10815
10816         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10817
10818         error_noexit "Cannot set pool $pool to $tdir"
10819         return 1
10820 }
10821
10822 pool_check_dir() {
10823         local pool=$1
10824         local tdir=$2
10825         echo "Checking pool on directory $tdir"
10826
10827         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10828         [ "$res" = "$pool" ] && return 0
10829
10830         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10831         return 1
10832 }
10833
10834 pool_dir_rel_path() {
10835         echo "Testing relative path works well"
10836         local pool=$1
10837         local tdir=$2
10838         local root=$3
10839
10840         mkdir -p $root/$tdir/$tdir
10841         cd $root/$tdir
10842         pool_set_dir $pool $tdir          || return 1
10843         pool_set_dir $pool ./$tdir        || return 2
10844         pool_set_dir $pool ../$tdir       || return 3
10845         pool_set_dir $pool ../$tdir/$tdir || return 4
10846         rm -rf $tdir; cd - > /dev/null
10847 }
10848
10849 pool_alloc_files() {
10850         echo "Checking files allocation from directory pool"
10851         local pool=$1
10852         local tdir=$2
10853         local count=$3
10854         local tlist="$4"
10855
10856         local failed=0
10857         for i in $(seq -w 1 $count)
10858         do
10859                 local file=$tdir/file-$i
10860                 touch $file
10861                 check_file_in_pool $file $pool "$tlist" || \
10862                         failed=$((failed + 1))
10863         done
10864         [ "$failed" = 0 ] && return 0
10865
10866         error_noexit "$failed files not allocated in $pool"
10867         return 1
10868 }
10869
10870 pool_create_files() {
10871         echo "Creating files in pool"
10872         local pool=$1
10873         local tdir=$2
10874         local count=$3
10875         local tlist="$4"
10876
10877         mkdir -p $tdir
10878         local failed=0
10879         for i in $(seq -w 1 $count)
10880         do
10881                 local file=$tdir/spoo-$i
10882                 $SETSTRIPE -p $pool $file
10883                 check_file_in_pool $file $pool "$tlist" || \
10884                         failed=$((failed + 1))
10885         done
10886         [ "$failed" = 0 ] && return 0
10887
10888         error_noexit "$failed files not allocated in $pool"
10889         return 1
10890 }
10891
10892 pool_lfs_df() {
10893         echo "Checking 'lfs df' output"
10894         local pool=$1
10895
10896         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
10897                         tr '\n' ' ')
10898         local res=$($LFS df --pool $FSNAME.$pool |
10899                         awk '{print $1}' |
10900                         grep "$FSNAME-OST" |
10901                         tr '\n' ' ')
10902         [ "$res" = "$t" ] && return 0
10903
10904         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
10905         return 1
10906 }
10907
10908 pool_file_rel_path() {
10909         echo "Creating files in a pool with relative pathname"
10910         local pool=$1
10911         local tdir=$2
10912
10913         mkdir -p $tdir ||
10914                 { error_noexit "unable to create $tdir"; return 1 ; }
10915         local file="/..$tdir/$tfile-1"
10916         $SETSTRIPE -p $pool $file ||
10917                 { error_noexit "unable to create $file" ; return 2 ; }
10918
10919         cd $tdir
10920         $SETSTRIPE -p $pool $tfile-2 || {
10921                 error_noexit "unable to create $tfile-2 in $tdir"
10922                 return 3
10923         }
10924 }
10925
10926 pool_remove_first_target() {
10927         echo "Removing first target from a pool"
10928         local pool=$1
10929
10930         local pname="lov.$FSNAME-*.pools.$pool"
10931         local t=$($LCTL get_param -n $pname | head -n1)
10932         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10933         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
10934                 error_noexit "$t not removed from $FSNAME.$pool"
10935                 return 1
10936         }
10937 }
10938
10939 pool_remove_all_targets() {
10940         echo "Removing all targets from pool"
10941         local pool=$1
10942         local file=$2
10943         local pname="lov.$FSNAME-*.pools.$pool"
10944         for t in $($LCTL get_param -n $pname | sort -u)
10945         do
10946                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10947         done
10948         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
10949                 error_noexit "Pool $FSNAME.$pool cannot be drained"
10950                 return 1
10951         }
10952         # striping on an empty/nonexistant pool should fall back
10953         # to "pool of everything"
10954         touch $file || {
10955                 error_noexit "failed to use fallback striping for empty pool"
10956                 return 2
10957         }
10958         # setstripe on an empty pool should fail
10959         $SETSTRIPE -p $pool $file 2>/dev/null && {
10960                 error_noexit "expected failure when creating file" \
10961                                                         "with empty pool"
10962                 return 3
10963         }
10964         return 0
10965 }
10966
10967 pool_remove() {
10968         echo "Destroying pool"
10969         local pool=$1
10970         local file=$2
10971
10972         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
10973
10974         sleep 2
10975         # striping on an empty/nonexistant pool should fall back 
10976         # to "pool of everything"
10977         touch $file || {
10978                 error_noexit "failed to use fallback striping for missing pool"
10979                 return 1
10980         }
10981         # setstripe on an empty pool should fail
10982         $SETSTRIPE -p $pool $file 2>/dev/null && {
10983                 error_noexit "expected failure when creating file" \
10984                                                         "with missing pool"
10985                 return 2
10986         }
10987
10988         # get param should return err once pool is gone
10989         if wait_update $HOSTNAME "lctl get_param -n \
10990                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
10991         then
10992                 remove_pool_from_list $FSNAME.$pool
10993                 return 0
10994         fi
10995         error_noexit "Pool $FSNAME.$pool is not destroyed"
10996         return 3
10997 }
10998
10999 test_200() {
11000         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11001         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11002
11003         local POOL=${POOL:-cea1}
11004         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11005         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11006         # Pool OST targets
11007         local first_ost=0
11008         local last_ost=$(($OSTCOUNT - 1))
11009         local ost_step=2
11010         local ost_list=$(seq $first_ost $ost_step $last_ost)
11011         local ost_range="$first_ost $last_ost $ost_step"
11012         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11013         local file_dir=$POOL_ROOT/file_tst
11014
11015         local rc=0
11016         while : ; do
11017                 # former test_200a test_200b
11018                 pool_add $POOL                          || { rc=$? ; break; }
11019                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11020                 # former test_200c test_200d
11021                 mkdir -p $test_path
11022                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11023                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11024                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11025                                                         || { rc=$? ; break; }
11026                 # former test_200e test_200f
11027                 local files=$((OSTCOUNT*3))
11028                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11029                                                         || { rc=$? ; break; }
11030                 pool_create_files $POOL $file_dir $files "$ost_list" \
11031                                                         || { rc=$? ; break; }
11032                 # former test_200g test_200h
11033                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11034                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11035
11036                 # former test_201a test_201b test_201c
11037                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11038
11039                 local f=$test_path/$tfile
11040                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11041                 pool_remove $POOL $f                    || { rc=$? ; break; }
11042                 break
11043         done
11044
11045         cleanup_pools
11046         return $rc
11047 }
11048 run_test 200 "OST pools"
11049
11050 # usage: default_attr <count | size | offset>
11051 default_attr() {
11052         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11053 }
11054
11055 # usage: check_default_stripe_attr
11056 check_default_stripe_attr() {
11057         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11058         case $1 in
11059         --stripe-count|--count)
11060                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11061         --stripe-size|--size)
11062                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11063         --stripe-index|--index)
11064                 EXPECTED=-1;;
11065         *)
11066                 error "unknown getstripe attr '$1'"
11067         esac
11068
11069         [ $ACTUAL != $EXPECTED ] &&
11070                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11071 }
11072
11073 test_204a() {
11074         test_mkdir -p $DIR/$tdir
11075         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11076
11077         check_default_stripe_attr --stripe-count
11078         check_default_stripe_attr --stripe-size
11079         check_default_stripe_attr --stripe-index
11080
11081         return 0
11082 }
11083 run_test 204a "Print default stripe attributes ================="
11084
11085 test_204b() {
11086         test_mkdir -p $DIR/$tdir
11087         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11088
11089         check_default_stripe_attr --stripe-size
11090         check_default_stripe_attr --stripe-index
11091
11092         return 0
11093 }
11094 run_test 204b "Print default stripe size and offset  ==========="
11095
11096 test_204c() {
11097         test_mkdir -p $DIR/$tdir
11098         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11099
11100         check_default_stripe_attr --stripe-count
11101         check_default_stripe_attr --stripe-index
11102
11103         return 0
11104 }
11105 run_test 204c "Print default stripe count and offset ==========="
11106
11107 test_204d() {
11108         test_mkdir -p $DIR/$tdir
11109         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11110
11111         check_default_stripe_attr --stripe-count
11112         check_default_stripe_attr --stripe-size
11113
11114         return 0
11115 }
11116 run_test 204d "Print default stripe count and size ============="
11117
11118 test_204e() {
11119         test_mkdir -p $DIR/$tdir
11120         $SETSTRIPE -d $DIR/$tdir
11121
11122         check_default_stripe_attr --stripe-count --raw
11123         check_default_stripe_attr --stripe-size --raw
11124         check_default_stripe_attr --stripe-index --raw
11125
11126         return 0
11127 }
11128 run_test 204e "Print raw stripe attributes ================="
11129
11130 test_204f() {
11131         test_mkdir -p $DIR/$tdir
11132         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11133
11134         check_default_stripe_attr --stripe-size --raw
11135         check_default_stripe_attr --stripe-index --raw
11136
11137         return 0
11138 }
11139 run_test 204f "Print raw stripe size and offset  ==========="
11140
11141 test_204g() {
11142         test_mkdir -p $DIR/$tdir
11143         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11144
11145         check_default_stripe_attr --stripe-count --raw
11146         check_default_stripe_attr --stripe-index --raw
11147
11148         return 0
11149 }
11150 run_test 204g "Print raw stripe count and offset ==========="
11151
11152 test_204h() {
11153         test_mkdir -p $DIR/$tdir
11154         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11155
11156         check_default_stripe_attr --stripe-count --raw
11157         check_default_stripe_attr --stripe-size --raw
11158
11159         return 0
11160 }
11161 run_test 204h "Print raw stripe count and size ============="
11162
11163 # Figure out which job scheduler is being used, if any,
11164 # or use a fake one
11165 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11166         JOBENV=SLURM_JOB_ID
11167 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11168         JOBENV=LSB_JOBID
11169 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11170         JOBENV=PBS_JOBID
11171 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11172         JOBENV=LOADL_STEP_ID
11173 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11174         JOBENV=JOB_ID
11175 else
11176         JOBENV=FAKE_JOBID
11177 fi
11178
11179 verify_jobstats() {
11180         local cmd=$1
11181         local target=$2
11182
11183         # clear old jobstats
11184         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11185         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11186
11187         # use a new JobID for this test, or we might see an old one
11188         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11189
11190         JOBVAL=${!JOBENV}
11191         log "Test: $cmd"
11192         log "Using JobID environment variable $JOBENV=$JOBVAL"
11193
11194         if [ $JOBENV = "FAKE_JOBID" ]; then
11195                 FAKE_JOBID=$JOBVAL $cmd
11196         else
11197                 $cmd
11198         fi
11199
11200         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11201                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11202                 do_facet $FACET lctl get_param mdt.*.job_stats |
11203                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11204         fi
11205         if [ "$target" = "ost" -o "$target" = "both" ]; then
11206                 FACET=ost1
11207                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11208                         grep $JOBVAL || error "No job stats found on OST $FACET"
11209         fi
11210 }
11211
11212 jobstats_set() {
11213         trap 0
11214         NEW_JOBENV=${1:-$OLD_JOBENV}
11215         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11216         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11217 }
11218
11219 test_205() { # Job stats
11220         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11221         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11222                 skip "Server doesn't support jobstats" && return 0
11223         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11224
11225         local cmd
11226         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11227         if [ $OLD_JOBENV != $JOBENV ]; then
11228                 jobstats_set $JOBENV
11229                 trap jobstats_set EXIT
11230         fi
11231
11232         # mkdir
11233         cmd="mkdir $DIR/$tfile"
11234         verify_jobstats "$cmd" "mdt"
11235         # rmdir
11236         cmd="rm -fr $DIR/$tfile"
11237         verify_jobstats "$cmd" "mdt"
11238         # mknod
11239         cmd="mknod $DIR/$tfile c 1 3"
11240         verify_jobstats "$cmd" "mdt"
11241         # unlink
11242         cmd="rm -f $DIR/$tfile"
11243         verify_jobstats "$cmd" "mdt"
11244         # open & close
11245         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11246         verify_jobstats "$cmd" "mdt"
11247         # setattr
11248         cmd="touch $DIR/$tfile"
11249         verify_jobstats "$cmd" "both"
11250         # write
11251         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11252         verify_jobstats "$cmd" "ost"
11253         # read
11254         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11255         verify_jobstats "$cmd" "ost"
11256         # truncate
11257         cmd="$TRUNCATE $DIR/$tfile 0"
11258         verify_jobstats "$cmd" "both"
11259         # rename
11260         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11261         verify_jobstats "$cmd" "mdt"
11262
11263         # cleanup
11264         rm -f $DIR/jobstats_test_rename
11265
11266         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11267 }
11268 run_test 205 "Verify job stats"
11269
11270 # LU-1480, LU-1773 and LU-1657
11271 test_206() {
11272         mkdir -p $DIR/$tdir
11273         $SETSTRIPE -c -1 $DIR/$tdir
11274 #define OBD_FAIL_LOV_INIT 0x1403
11275         $LCTL set_param fail_loc=0xa0001403
11276         $LCTL set_param fail_val=1
11277         touch $DIR/$tdir/$tfile || true
11278 }
11279 run_test 206 "fail lov_init_raid0() doesn't lbug"
11280
11281 test_207a() {
11282         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11283         local fsz=`stat -c %s $DIR/$tfile`
11284         cancel_lru_locks mdc
11285
11286         # do not return layout in getattr intent
11287 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11288         $LCTL set_param fail_loc=0x170
11289         local sz=`stat -c %s $DIR/$tfile`
11290
11291         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11292
11293         rm -rf $DIR/$tfile
11294 }
11295 run_test 207a "can refresh layout at glimpse"
11296
11297 test_207b() {
11298         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11299         local cksum=`md5sum $DIR/$tfile`
11300         local fsz=`stat -c %s $DIR/$tfile`
11301         cancel_lru_locks mdc
11302         cancel_lru_locks osc
11303
11304         # do not return layout in getattr intent
11305 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11306         $LCTL set_param fail_loc=0x171
11307
11308         # it will refresh layout after the file is opened but before read issues
11309         echo checksum is "$cksum"
11310         echo "$cksum" |md5sum -c --quiet || error "file differs"
11311
11312         rm -rf $DIR/$tfile
11313 }
11314 run_test 207b "can refresh layout at open"
11315
11316 test_208() {
11317         # FIXME: in this test suite, only RD lease is used. This is okay
11318         # for now as only exclusive open is supported. After generic lease
11319         # is done, this test suite should be revised. - Jinshan
11320
11321         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11322                 { skip "Need MDS version at least 2.4.52"; return 0; }
11323
11324         echo "==== test 1: verify get lease work"
11325         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11326
11327         echo "==== test 2: verify lease can be broken by upcoming open"
11328         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11329         local PID=$!
11330         sleep 1
11331
11332         $MULTIOP $DIR/$tfile oO_RDONLY:c
11333         kill -USR1 $PID && wait $PID || error "break lease error"
11334
11335         echo "==== test 3: verify lease can't be granted if an open already exists"
11336         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11337         local PID=$!
11338         sleep 1
11339
11340         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11341         kill -USR1 $PID && wait $PID || error "open file error"
11342
11343         echo "==== test 4: lease can sustain over recovery"
11344         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11345         PID=$!
11346         sleep 1
11347
11348         fail mds1
11349
11350         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11351
11352         echo "==== test 5: lease broken can't be regained by replay"
11353         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11354         PID=$!
11355         sleep 1
11356
11357         # open file to break lease and then recovery
11358         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11359         fail mds1
11360
11361         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11362
11363         rm -f $DIR/$tfile
11364 }
11365 run_test 208 "Exclusive open"
11366
11367 test_209() {
11368         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11369                 skip_env "must have disp_stripe" && return
11370
11371         touch $DIR/$tfile
11372         sync; sleep 5; sync;
11373
11374         echo 3 > /proc/sys/vm/drop_caches
11375         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11376
11377         # open/close 500 times
11378         for i in $(seq 500); do
11379                 cat $DIR/$tfile
11380         done
11381
11382         echo 3 > /proc/sys/vm/drop_caches
11383         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11384
11385         echo "before: $req_before, after: $req_after"
11386         [ $((req_after - req_before)) -ge 300 ] &&
11387                 error "open/close requests are not freed"
11388         return 0
11389 }
11390 run_test 209 "read-only open/close requests should be freed promptly"
11391
11392 test_212() {
11393         size=`date +%s`
11394         size=$((size % 8192 + 1))
11395         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11396         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11397         rm -f $DIR/f212 $DIR/f212.xyz
11398 }
11399 run_test 212 "Sendfile test ============================================"
11400
11401 test_213() {
11402         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11403         cancel_lru_locks osc
11404         lctl set_param fail_loc=0x8000040f
11405         # generate a read lock
11406         cat $DIR/$tfile > /dev/null
11407         # write to the file, it will try to cancel the above read lock.
11408         cat /etc/hosts >> $DIR/$tfile
11409 }
11410 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11411
11412 test_214() { # for bug 20133
11413         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11414         for (( i=0; i < 340; i++ )) ; do
11415                 touch $DIR/$tdir/d214c/a$i
11416         done
11417
11418         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11419         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11420         ls $DIR/d214c || error "ls $DIR/d214c failed"
11421         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11422         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11423 }
11424 run_test 214 "hash-indexed directory test - bug 20133"
11425
11426 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11427 create_lnet_proc_files() {
11428         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11429         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11430
11431         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11432         rm -f "$TMP/lnet_$1.sys_tmp"
11433 }
11434
11435 # counterpart of create_lnet_proc_files
11436 remove_lnet_proc_files() {
11437         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11438 }
11439
11440 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11441 # 3rd arg as regexp for body
11442 check_lnet_proc_stats() {
11443         local l=$(cat "$TMP/lnet_$1" |wc -l)
11444         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11445
11446         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11447 }
11448
11449 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11450 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11451 # optional and can be regexp for 2nd line (lnet.routes case)
11452 check_lnet_proc_entry() {
11453         local blp=2          # blp stands for 'position of 1st line of body'
11454         [ -z "$5" ] || blp=3 # lnet.routes case
11455
11456         local l=$(cat "$TMP/lnet_$1" |wc -l)
11457         # subtracting one from $blp because the body can be empty
11458         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11459
11460         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11461                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11462
11463         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11464                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11465
11466         # bail out if any unexpected line happened
11467         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11468         [ "$?" != 0 ] || error "$2 misformatted"
11469 }
11470
11471 test_215() { # for bugs 18102, 21079, 21517
11472         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11473         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11474         local P='[1-9][0-9]*'           # positive numeric
11475         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11476         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11477         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11478         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11479
11480         local L1 # regexp for 1st line
11481         local L2 # regexp for 2nd line (optional)
11482         local BR # regexp for the rest (body)
11483
11484         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11485         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11486         create_lnet_proc_files "stats"
11487         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11488         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11489         remove_lnet_proc_files "stats"
11490
11491         # /proc/sys/lnet/routes should look like this:
11492         # Routing disabled/enabled
11493         # net hops priority state router
11494         # where net is a string like tcp0, hops > 0, priority >= 0,
11495         # state is up/down,
11496         # router is a string like 192.168.1.1@tcp2
11497         L1="^Routing (disabled|enabled)$"
11498         L2="^net +hops +priority +state +router$"
11499         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11500         create_lnet_proc_files "routes"
11501         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11502         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11503         remove_lnet_proc_files "routes"
11504
11505         # /proc/sys/lnet/routers should look like this:
11506         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11507         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11508         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11509         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11510         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11511         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11512         create_lnet_proc_files "routers"
11513         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11514         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11515         remove_lnet_proc_files "routers"
11516
11517         # /proc/sys/lnet/peers should look like this:
11518         # nid refs state last max rtr min tx min queue
11519         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11520         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11521         # numeric (0 or >0 or <0), queue >= 0.
11522         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11523         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11524         create_lnet_proc_files "peers"
11525         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11526         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11527         remove_lnet_proc_files "peers"
11528
11529         # /proc/sys/lnet/buffers  should look like this:
11530         # pages count credits min
11531         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11532         L1="^pages +count +credits +min$"
11533         BR="^ +$N +$N +$I +$I$"
11534         create_lnet_proc_files "buffers"
11535         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11536         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11537         remove_lnet_proc_files "buffers"
11538
11539         # /proc/sys/lnet/nis should look like this:
11540         # nid status alive refs peer rtr max tx min
11541         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11542         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11543         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11544         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11545         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11546         create_lnet_proc_files "nis"
11547         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11548         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11549         remove_lnet_proc_files "nis"
11550
11551         # can we successfully write to /proc/sys/lnet/stats?
11552         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11553         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11554 }
11555 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11556
11557 test_216() { # bug 20317
11558         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11559         remote_ost_nodsh && skip "remote OST with nodsh" && return
11560
11561         local node
11562         local facets=$(get_facets OST)
11563         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11564
11565         save_lustre_params client "osc.*.contention_seconds" > $p
11566         save_lustre_params $facets \
11567                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11568         save_lustre_params $facets \
11569                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11570         save_lustre_params $facets \
11571                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11572         clear_osc_stats
11573
11574         # agressive lockless i/o settings
11575         for node in $(osts_nodes); do
11576                 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'
11577         done
11578         lctl set_param -n osc.*.contention_seconds 60
11579
11580         $DIRECTIO write $DIR/$tfile 0 10 4096
11581         $CHECKSTAT -s 40960 $DIR/$tfile
11582
11583         # disable lockless i/o
11584         for node in $(osts_nodes); do
11585                 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'
11586         done
11587         lctl set_param -n osc.*.contention_seconds 0
11588         clear_osc_stats
11589
11590         dd if=/dev/zero of=$DIR/$tfile count=0
11591         $CHECKSTAT -s 0 $DIR/$tfile
11592
11593         restore_lustre_params <$p
11594         rm -f $p
11595         rm $DIR/$tfile
11596 }
11597 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11598
11599 test_217() { # bug 22430
11600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11601         local node
11602         local nid
11603
11604         for node in $(nodes_list); do
11605                 nid=$(host_nids_address $node $NETTYPE)
11606                 if [[ $nid = *-* ]] ; then
11607                         echo "lctl ping $nid@$NETTYPE"
11608                         lctl ping $nid@$NETTYPE
11609                 else
11610                         echo "skipping $node (no hyphen detected)"
11611                 fi
11612         done
11613 }
11614 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11615
11616 test_218() {
11617        # do directio so as not to populate the page cache
11618        log "creating a 10 Mb file"
11619        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11620        log "starting reads"
11621        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11622        log "truncating the file"
11623        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11624        log "killing dd"
11625        kill %+ || true # reads might have finished
11626        echo "wait until dd is finished"
11627        wait
11628        log "removing the temporary file"
11629        rm -rf $DIR/$tfile || error "tmp file removal failed"
11630 }
11631 run_test 218 "parallel read and truncate should not deadlock ======================="
11632
11633 test_219() {
11634         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11635         # write one partial page
11636         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11637         # set no grant so vvp_io_commit_write will do sync write
11638         $LCTL set_param fail_loc=0x411
11639         # write a full page at the end of file
11640         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11641
11642         $LCTL set_param fail_loc=0
11643         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11644         $LCTL set_param fail_loc=0x411
11645         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11646
11647         # LU-4201
11648         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11649         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11650 }
11651 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11652
11653 test_220() { #LU-325
11654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11655         remote_ost_nodsh && skip "remote OST with nodsh" && return
11656         local OSTIDX=0
11657
11658         test_mkdir -p $DIR/$tdir
11659         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11660                 awk '{print $2}' | sed -e 's/_UUID$//')
11661
11662         # on the mdt's osc
11663         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11664         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11665                         osc.$mdtosc_proc1.prealloc_last_id)
11666         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11667                         osc.$mdtosc_proc1.prealloc_next_id)
11668
11669         $LFS df -i
11670
11671         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11672         #define OBD_FAIL_OST_ENOINO              0x229
11673         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11674         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11675         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11676
11677         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11678
11679         MDSOBJS=$((last_id - next_id))
11680         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11681
11682         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11683         echo "OST still has $count kbytes free"
11684
11685         echo "create $MDSOBJS files @next_id..."
11686         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11687
11688         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11689                         osc.$mdtosc_proc1.prealloc_last_id)
11690         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11691                         osc.$mdtosc_proc1.prealloc_next_id)
11692
11693         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11694         $LFS df -i
11695
11696         echo "cleanup..."
11697
11698         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11699         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11700
11701         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11702         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11703         echo "unlink $MDSOBJS files @$next_id..."
11704         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11705 }
11706 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11707
11708 test_221() {
11709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11710         dd if=`which date` of=$MOUNT/date oflag=sync
11711         chmod +x $MOUNT/date
11712
11713         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11714         $LCTL set_param fail_loc=0x80001401
11715
11716         $MOUNT/date > /dev/null
11717         rm -f $MOUNT/date
11718 }
11719 run_test 221 "make sure fault and truncate race to not cause OOM"
11720
11721 test_222a () {
11722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11723        rm -rf $DIR/$tdir
11724        test_mkdir -p $DIR/$tdir
11725        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11726        createmany -o $DIR/$tdir/$tfile 10
11727        cancel_lru_locks mdc
11728        cancel_lru_locks osc
11729        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11730        $LCTL set_param fail_loc=0x31a
11731        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11732        $LCTL set_param fail_loc=0
11733        rm -r $DIR/$tdir
11734 }
11735 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11736
11737 test_222b () {
11738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11739        rm -rf $DIR/$tdir
11740        test_mkdir -p $DIR/$tdir
11741        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11742        createmany -o $DIR/$tdir/$tfile 10
11743        cancel_lru_locks mdc
11744        cancel_lru_locks osc
11745        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11746        $LCTL set_param fail_loc=0x31a
11747        rm -r $DIR/$tdir || "AGL for rmdir failed"
11748        $LCTL set_param fail_loc=0
11749 }
11750 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11751
11752 test_223 () {
11753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11754        rm -rf $DIR/$tdir
11755        test_mkdir -p $DIR/$tdir
11756        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11757        createmany -o $DIR/$tdir/$tfile 10
11758        cancel_lru_locks mdc
11759        cancel_lru_locks osc
11760        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11761        $LCTL set_param fail_loc=0x31b
11762        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11763        $LCTL set_param fail_loc=0
11764        rm -r $DIR/$tdir
11765 }
11766 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11767
11768 test_224a() { # LU-1039, MRP-303
11769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11770         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11771         $LCTL set_param fail_loc=0x508
11772         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11773         $LCTL set_param fail_loc=0
11774         df $DIR
11775 }
11776 run_test 224a "Don't panic on bulk IO failure"
11777
11778 test_224b() { # LU-1039, MRP-303
11779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11780         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11781         cancel_lru_locks osc
11782         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11783         $LCTL set_param fail_loc=0x515
11784         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11785         $LCTL set_param fail_loc=0
11786         df $DIR
11787 }
11788 run_test 224b "Don't panic on bulk IO failure"
11789
11790 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11791 test_225a () {
11792         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11793         if [ -z ${MDSSURVEY} ]; then
11794               skip_env "mds-survey not found" && return
11795         fi
11796
11797         [ $MDSCOUNT -ge 2 ] &&
11798                 skip "skipping now for more than one MDT" && return
11799
11800        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11801             { skip "Need MDS version at least 2.2.51"; return; }
11802
11803        local mds=$(facet_host $SINGLEMDS)
11804        local target=$(do_nodes $mds 'lctl dl' | \
11805                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11806
11807        local cmd1="file_count=1000 thrhi=4"
11808        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11809        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11810        local cmd="$cmd1 $cmd2 $cmd3"
11811
11812        rm -f ${TMP}/mds_survey*
11813        echo + $cmd
11814        eval $cmd || error "mds-survey with zero-stripe failed"
11815        cat ${TMP}/mds_survey*
11816        rm -f ${TMP}/mds_survey*
11817 }
11818 run_test 225a "Metadata survey sanity with zero-stripe"
11819
11820 test_225b () {
11821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11822
11823         if [ -z ${MDSSURVEY} ]; then
11824               skip_env "mds-survey not found" && return
11825         fi
11826         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11827             { skip "Need MDS version at least 2.2.51"; return; }
11828
11829         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11830               skip_env "Need to mount OST to test" && return
11831         fi
11832
11833         [ $MDSCOUNT -ge 2 ] &&
11834                 skip "skipping now for more than one MDT" && return
11835        local mds=$(facet_host $SINGLEMDS)
11836        local target=$(do_nodes $mds 'lctl dl' | \
11837                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11838
11839        local cmd1="file_count=1000 thrhi=4"
11840        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11841        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11842        local cmd="$cmd1 $cmd2 $cmd3"
11843
11844        rm -f ${TMP}/mds_survey*
11845        echo + $cmd
11846        eval $cmd || error "mds-survey with stripe_count failed"
11847        cat ${TMP}/mds_survey*
11848        rm -f ${TMP}/mds_survey*
11849 }
11850 run_test 225b "Metadata survey sanity with stripe_count = 1"
11851
11852 mcreate_path2fid () {
11853         local mode=$1
11854         local major=$2
11855         local minor=$3
11856         local name=$4
11857         local desc=$5
11858         local path=$DIR/$tdir/$name
11859         local fid
11860         local rc
11861         local fid_path
11862
11863         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11864                 error "cannot create $desc"
11865
11866         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11867         rc=$?
11868         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11869
11870         fid_path=$($LFS fid2path $MOUNT $fid)
11871         rc=$?
11872         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11873
11874         [ "$path" == "$fid_path" ] ||
11875                 error "fid2path returned $fid_path, expected $path"
11876
11877         echo "pass with $path and $fid"
11878 }
11879
11880 test_226a () {
11881         rm -rf $DIR/$tdir
11882         mkdir -p $DIR/$tdir
11883
11884         mcreate_path2fid 0010666 0 0 fifo "FIFO"
11885         mcreate_path2fid 0020666 1 3 null "character special file (null)"
11886         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
11887         mcreate_path2fid 0040666 0 0 dir "directory"
11888         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
11889         mcreate_path2fid 0100666 0 0 file "regular file"
11890         mcreate_path2fid 0120666 0 0 link "symbolic link"
11891         mcreate_path2fid 0140666 0 0 sock "socket"
11892 }
11893 run_test 226a "call path2fid and fid2path on files of all type"
11894
11895 test_226b () {
11896         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11897         rm -rf $DIR/$tdir
11898         local MDTIDX=1
11899
11900         mkdir -p $DIR/$tdir
11901         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
11902                 error "create remote directory failed"
11903         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
11904         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
11905                                 "character special file (null)"
11906         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
11907                                 "character special file (no device)"
11908         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
11909         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
11910                                 "block special file (loop)"
11911         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
11912         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
11913         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
11914 }
11915 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
11916
11917 # LU-1299 Executing or running ldd on a truncated executable does not
11918 # cause an out-of-memory condition.
11919 test_227() {
11920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11921         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
11922         dd if=$(which date) of=$MOUNT/date bs=1k count=1
11923         chmod +x $MOUNT/date
11924
11925         $MOUNT/date > /dev/null
11926         ldd $MOUNT/date > /dev/null
11927         rm -f $MOUNT/date
11928 }
11929 run_test 227 "running truncated executable does not cause OOM"
11930
11931 # LU-1512 try to reuse idle OI blocks
11932 test_228a() {
11933         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11934         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11935                 skip "non-ldiskfs backend" && return
11936
11937         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11938         local myDIR=$DIR/$tdir
11939
11940         mkdir -p $myDIR
11941         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11942         $LCTL set_param fail_loc=0x80001002
11943         createmany -o $myDIR/t- 10000
11944         $LCTL set_param fail_loc=0
11945         # The guard is current the largest FID holder
11946         touch $myDIR/guard
11947         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11948                     tr -d '[')
11949         local IDX=$(($SEQ % 64))
11950
11951         do_facet $SINGLEMDS sync
11952         # Make sure journal flushed.
11953         sleep 6
11954         local blk1=$(do_facet $SINGLEMDS \
11955                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11956                      grep Blockcount | awk '{print $4}')
11957
11958         # Remove old files, some OI blocks will become idle.
11959         unlinkmany $myDIR/t- 10000
11960         # Create new files, idle OI blocks should be reused.
11961         createmany -o $myDIR/t- 2000
11962         do_facet $SINGLEMDS sync
11963         # Make sure journal flushed.
11964         sleep 6
11965         local blk2=$(do_facet $SINGLEMDS \
11966                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11967                      grep Blockcount | awk '{print $4}')
11968
11969         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11970 }
11971 run_test 228a "try to reuse idle OI blocks"
11972
11973 test_228b() {
11974         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11975         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11976                 skip "non-ldiskfs backend" && return
11977
11978         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11979         local myDIR=$DIR/$tdir
11980
11981         mkdir -p $myDIR
11982         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11983         $LCTL set_param fail_loc=0x80001002
11984         createmany -o $myDIR/t- 10000
11985         $LCTL set_param fail_loc=0
11986         # The guard is current the largest FID holder
11987         touch $myDIR/guard
11988         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11989                     tr -d '[')
11990         local IDX=$(($SEQ % 64))
11991
11992         do_facet $SINGLEMDS sync
11993         # Make sure journal flushed.
11994         sleep 6
11995         local blk1=$(do_facet $SINGLEMDS \
11996                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11997                      grep Blockcount | awk '{print $4}')
11998
11999         # Remove old files, some OI blocks will become idle.
12000         unlinkmany $myDIR/t- 10000
12001
12002         # stop the MDT
12003         stop $SINGLEMDS || error "Fail to stop MDT."
12004         # remount the MDT
12005         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12006
12007         df $MOUNT || error "Fail to df."
12008         # Create new files, idle OI blocks should be reused.
12009         createmany -o $myDIR/t- 2000
12010         do_facet $SINGLEMDS sync
12011         # Make sure journal flushed.
12012         sleep 6
12013         local blk2=$(do_facet $SINGLEMDS \
12014                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12015                      grep Blockcount | awk '{print $4}')
12016
12017         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12018 }
12019 run_test 228b "idle OI blocks can be reused after MDT restart"
12020
12021 #LU-1881
12022 test_228c() {
12023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12024         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12025                 skip "non-ldiskfs backend" && return
12026
12027         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12028         local myDIR=$DIR/$tdir
12029
12030         mkdir -p $myDIR
12031         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12032         $LCTL set_param fail_loc=0x80001002
12033         # 20000 files can guarantee there are index nodes in the OI file
12034         createmany -o $myDIR/t- 20000
12035         $LCTL set_param fail_loc=0
12036         # The guard is current the largest FID holder
12037         touch $myDIR/guard
12038         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12039                     tr -d '[')
12040         local IDX=$(($SEQ % 64))
12041
12042         do_facet $SINGLEMDS sync
12043         # Make sure journal flushed.
12044         sleep 6
12045         local blk1=$(do_facet $SINGLEMDS \
12046                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12047                      grep Blockcount | awk '{print $4}')
12048
12049         # Remove old files, some OI blocks will become idle.
12050         unlinkmany $myDIR/t- 20000
12051         rm -f $myDIR/guard
12052         # The OI file should become empty now
12053
12054         # Create new files, idle OI blocks should be reused.
12055         createmany -o $myDIR/t- 2000
12056         do_facet $SINGLEMDS sync
12057         # Make sure journal flushed.
12058         sleep 6
12059         local blk2=$(do_facet $SINGLEMDS \
12060                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12061                      grep Blockcount | awk '{print $4}')
12062
12063         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12064 }
12065 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12066
12067 test_229() { # LU-2482, LU-3448
12068         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12069                 skip "No HSM support on MDS of $(get_lustre_version)," \
12070                          "need 2.4.53 at least" && return
12071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12072         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12073
12074         rm -f $DIR/$tfile
12075
12076         # Create a file with a released layout and stripe count 2.
12077         $MULTIOP $DIR/$tfile H2c ||
12078                 error "failed to create file with released layout"
12079
12080         $GETSTRIPE -v $DIR/$tfile
12081
12082         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12083         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12084
12085         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12086         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12087         stat $DIR/$tfile || error "failed to stat released file"
12088
12089         chown $RUNAS_ID $DIR/$tfile ||
12090                 error "chown $RUNAS_ID $DIR/$tfile failed"
12091
12092         chgrp $RUNAS_ID $DIR/$tfile ||
12093                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12094
12095         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12096         rm $DIR/$tfile || error "failed to remove released file"
12097 }
12098 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12099
12100 test_230a() {
12101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12102         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12103         local MDTIDX=1
12104
12105         mkdir -p $DIR/$tdir/test_230_local
12106         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12107         [ $mdt_idx -ne 0 ] &&
12108                 error "create local directory on wrong MDT $mdt_idx"
12109
12110         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12111                         error "create remote directory failed"
12112         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12113         [ $mdt_idx -ne $MDTIDX ] &&
12114                 error "create remote directory on wrong MDT $mdt_idx"
12115
12116         createmany -o $DIR/$tdir/test_230/t- 10 ||
12117                 error "create files on remote directory failed"
12118         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12119         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12120         rm -r $DIR/$tdir || error "unlink remote directory failed"
12121 }
12122 run_test 230a "Create remote directory and files under the remote directory"
12123
12124 test_230b() {
12125         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12126         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12127         local MDTIDX=1
12128         local mdt_index
12129         local i
12130         local file
12131         local pid
12132         local stripe_count
12133         local migrate_dir=$DIR/$tdir/migrate_dir
12134         local other_dir=$DIR/$tdir/other_dir
12135
12136         mkdir -p $migrate_dir
12137         mkdir -p $other_dir
12138         for ((i=0; i<10; i++)); do
12139                 mkdir -p $migrate_dir/dir_${i}
12140                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12141                         error "create files under remote dir failed $i"
12142         done
12143
12144         cp /etc/passwd $migrate_dir/$tfile
12145         cp /etc/passwd $other_dir/$tfile
12146         chattr +SAD $migrate_dir
12147         chattr +SAD $migrate_dir/$tfile
12148
12149         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12150         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12151         local old_dir_mode=$(stat -c%f $migrate_dir)
12152         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12153
12154         mkdir -p $migrate_dir/dir_default_stripe2
12155         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12156         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12157
12158         mkdir -p $other_dir
12159         ln $migrate_dir/$tfile $other_dir/luna
12160         ln $migrate_dir/$tfile $migrate_dir/sofia
12161         ln $other_dir/$tfile $migrate_dir/david
12162         ln -s $migrate_dir/$tfile $other_dir/zachary
12163         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12164         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12165
12166         $LFS mv -v -M $MDTIDX $migrate_dir ||
12167                         error "migrate remote dir error"
12168
12169         echo "migratate to MDT1, then checking.."
12170         for ((i=0; i<10; i++)); do
12171                 for file in $(find $migrate_dir/dir_${i}); do
12172                         mdt_index=$($LFS getstripe -M $file)
12173                         [ $mdt_index == $MDTIDX ] ||
12174                                 error "$file is not on MDT${MDTIDX}"
12175                 done
12176         done
12177
12178         # the multiple link file should still in MDT0
12179         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12180         [ $mdt_index == 0 ] ||
12181                 error "$file is not on MDT${MDTIDX}"
12182
12183         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12184         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12185                 error " expect $old_dir_flag get $new_dir_flag"
12186
12187         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12188         [ "$old_file_flag" = "$new_file_flag" ] ||
12189                 error " expect $old_file_flag get $new_file_flag"
12190
12191         local new_dir_mode=$(stat -c%f $migrate_dir)
12192         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12193                 error "expect mode $old_dir_mode get $new_dir_mode"
12194
12195         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12196         [ "$old_file_mode" = "$new_file_mode" ] ||
12197                 error "expect mode $old_file_mode get $new_file_mode"
12198
12199         diff /etc/passwd $migrate_dir/$tfile ||
12200                 error "$tfile different after migration"
12201
12202         diff /etc/passwd $other_dir/luna ||
12203                 error "luna different after migration"
12204
12205         diff /etc/passwd $migrate_dir/sofia ||
12206                 error "sofia different after migration"
12207
12208         diff /etc/passwd $migrate_dir/david ||
12209                 error "david different after migration"
12210
12211         diff /etc/passwd $other_dir/zachary ||
12212                 error "zachary different after migration"
12213
12214         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12215                 error "${tfile}_ln different after migration"
12216
12217         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12218                 error "${tfile}_ln_other different after migration"
12219
12220         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12221         [ $stripe_count = 2 ] ||
12222                         error "dir strpe_count $d != 2 after migration."
12223
12224         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12225         [ $stripe_count = 2 ] ||
12226                         error "file strpe_count $d != 2 after migration."
12227
12228         #migrate back to MDT0
12229         MDTIDX=0
12230         $LFS mv -v -M $MDTIDX $migrate_dir ||
12231                         error "migrate remote dir error"
12232
12233         echo "migrate back to MDT0, checking.."
12234         for file in $(find $migrate_dir); do
12235                 mdt_index=$($LFS getstripe -M $file)
12236                 [ $mdt_index == $MDTIDX ] ||
12237                         error "$file is not on MDT${MDTIDX}"
12238         done
12239
12240         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12241         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12242                 error " expect $old_dir_flag get $new_dir_flag"
12243
12244         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12245         [ "$old_file_flag" = "$new_file_flag" ] ||
12246                 error " expect $old_file_flag get $new_file_flag"
12247
12248         local new_dir_mode=$(stat -c%f $migrate_dir)
12249         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12250                 error "expect mode $old_dir_mode get $new_dir_mode"
12251
12252         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12253         [ "$old_file_mode" = "$new_file_mode" ] ||
12254                 error "expect mode $old_file_mode get $new_file_mode"
12255
12256         diff /etc/passwd ${migrate_dir}/$tfile ||
12257                 error "$tfile different after migration"
12258
12259         diff /etc/passwd ${other_dir}/luna ||
12260                 error "luna different after migration"
12261
12262         diff /etc/passwd ${migrate_dir}/sofia ||
12263                 error "sofia different after migration"
12264
12265         diff /etc/passwd ${other_dir}/zachary ||
12266                 error "zachary different after migration"
12267
12268         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12269                 error "${tfile}_ln different after migration"
12270
12271         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12272                 error "${tfile}_ln_other different after migration"
12273
12274         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12275         [ $stripe_count = 2 ] ||
12276                 error "dir strpe_count $d != 2 after migration."
12277
12278         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12279         [ $stripe_count = 2 ] ||
12280                 error "file strpe_count $d != 2 after migration."
12281
12282         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12283 }
12284 run_test 230b "migrate directory"
12285
12286 test_230c() {
12287         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12288         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12289         local MDTIDX=1
12290         local mdt_index
12291         local file
12292
12293         #If migrating directory fails in the middle, all entries of
12294         #the directory is still accessiable.
12295         mkdir -p $DIR/$tdir
12296         stat $DIR/$tdir
12297         createmany -o $DIR/$tdir/f 10 ||
12298                 error "create files under ${tdir} failed"
12299
12300         #failed after migrating 5 entries
12301         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12302         do_facet mds1 lctl set_param fail_loc=0x20001801
12303         do_facet mds1 lctl  set_param fail_val=5
12304         local t=`ls $DIR/$tdir | wc -l`
12305         $LFS mv -M $MDTIDX $DIR/$tdir &&
12306                 error "migrate should failed after 5 entries"
12307         local u=`ls $DIR/$tdir | wc -l`
12308         [ "$u" == "$t" ] || error "$u != $t during migration"
12309
12310         for file in $(find $DIR/$tdir); do
12311                 stat $file || error "stat $file failed"
12312         done
12313
12314         do_facet mds1 lctl set_param fail_loc=0
12315         do_facet mds1 lctl set_param fail_val=0
12316
12317         $LFS mv -M $MDTIDX $DIR/$tdir ||
12318                 error "migrate open files should failed with open files"
12319
12320         echo "Finish migration, then checking.."
12321         for file in $(find $DIR/$tdir); do
12322                 mdt_index=$($LFS getstripe -M $file)
12323                 [ $mdt_index == $MDTIDX ] ||
12324                         error "$file is not on MDT${MDTIDX}"
12325         done
12326
12327         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12328 }
12329 run_test 230c "check directory accessiblity if migration is failed"
12330
12331 test_230d() {
12332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12333         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12334         local MDTIDX=1
12335         local mdt_index
12336         local i
12337         local j
12338
12339         mkdir -p $DIR/$tdir
12340
12341         for ((i=0; i<100; i++)); do
12342                 mkdir -p $DIR/$tdir/dir_${i}
12343                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12344                         error "create files under remote dir failed $i"
12345         done
12346
12347         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12348
12349         echo "Finish migration, then checking.."
12350         for file in $(find $DIR/$tdir); do
12351                 mdt_index=$($LFS getstripe -M $file)
12352                 [ $mdt_index == $MDTIDX ] ||
12353                         error "$file is not on MDT${MDTIDX}"
12354         done
12355
12356         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12357 }
12358 run_test 230d "check migrate big directory"
12359
12360 test_231a()
12361 {
12362         # For simplicity this test assumes that max_pages_per_rpc
12363         # is the same across all OSCs
12364         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12365         local bulk_size=$((max_pages * 4096))
12366
12367         mkdir -p $DIR/$tdir
12368
12369         # clear the OSC stats
12370         $LCTL set_param osc.*.stats=0 &>/dev/null
12371
12372         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12373         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12374                 oflag=direct &>/dev/null || error "dd failed"
12375
12376         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12377         if [ x$nrpcs != "x1" ]; then
12378                 error "found $nrpc ost_write RPCs, not 1 as expected"
12379         fi
12380
12381         # Drop the OSC cache, otherwise we will read from it
12382         cancel_lru_locks osc
12383
12384         # clear the OSC stats
12385         $LCTL set_param osc.*.stats=0 &>/dev/null
12386
12387         # Client reads $bulk_size.
12388         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12389                 iflag=direct &>/dev/null || error "dd failed"
12390
12391         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12392         if [ x$nrpcs != "x1" ]; then
12393                 error "found $nrpc ost_read RPCs, not 1 as expected"
12394         fi
12395 }
12396 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12397
12398 test_231b() {
12399         mkdir -p $DIR/$tdir
12400         local i
12401         for i in {0..1023}; do
12402                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12403                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12404                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12405         done
12406         sync
12407 }
12408 run_test 231b "must not assert on fully utilized OST request buffer"
12409
12410 test_232() {
12411         mkdir -p $DIR/$tdir
12412         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12413         $LCTL set_param fail_loc=0x31c
12414
12415         # ignore dd failure
12416         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12417
12418         $LCTL set_param fail_loc=0
12419         umount_client $MOUNT || error "umount failed"
12420         mount_client $MOUNT || error "mount failed"
12421 }
12422 run_test 232 "failed lock should not block umount"
12423
12424 test_233a() {
12425         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12426         { skip "Need MDS version at least 2.3.64"; return; }
12427
12428         local fid=$($LFS path2fid $MOUNT)
12429         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12430                 error "cannot access $MOUNT using its FID '$fid'"
12431 }
12432 run_test 233a "checking that OBF of the FS root succeeds"
12433
12434 test_233b() {
12435         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12436         { skip "Need MDS version at least 2.5.90"; return; }
12437
12438         local fid=$($LFS path2fid $MOUNT/.lustre)
12439         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12440                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12441
12442         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12443         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12444                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12445 }
12446 run_test 233b "checking that OBF of the FS .lustre succeeds"
12447
12448 test_234() {
12449         local p="$TMP/sanityN-$TESTNAME.parameters"
12450         save_lustre_params client "llite.*.xattr_cache" > $p
12451         lctl set_param llite.*.xattr_cache 1 ||
12452                 { skip "xattr cache is not supported"; return 0; }
12453
12454         mkdir -p $DIR/$tdir || error "mkdir failed"
12455         touch $DIR/$tdir/$tfile || error "touch failed"
12456         # OBD_FAIL_LLITE_XATTR_ENOMEM
12457         $LCTL set_param fail_loc=0x1405
12458         if [ ! -f /etc/SuSE-release ]; then
12459                 # attr pre-2.4.44-7 had a bug with rc
12460                 # LU-3703 - SLES clients have older attr
12461                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12462                         error "getfattr should have failed with ENOMEM"
12463         fi
12464         $LCTL set_param fail_loc=0x0
12465         rm -rf $DIR/$tdir
12466
12467         restore_lustre_params < $p
12468         rm -f $p
12469 }
12470 run_test 234 "xattr cache should not crash on ENOMEM"
12471
12472 test_235() {
12473         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12474                 skip "Need MDS version at least 2.4.52" && return
12475         flock_deadlock $DIR/$tfile
12476         local RC=$?
12477         case $RC in
12478                 0)
12479                 ;;
12480                 124) error "process hangs on a deadlock"
12481                 ;;
12482                 *) error "error executing flock_deadlock $DIR/$tfile"
12483                 ;;
12484         esac
12485 }
12486 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12487
12488 #LU-2935
12489 test_236() {
12490         check_swap_layouts_support && return 0
12491         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12492
12493         local ref1=/etc/passwd
12494         local ref2=/etc/group
12495         local file1=$DIR/$tdir/f1
12496         local file2=$DIR/$tdir/f2
12497
12498         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12499         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12500         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12501         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12502         local fd=$(free_fd)
12503         local cmd="exec $fd<>$file2"
12504         eval $cmd
12505         rm $file2
12506         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12507                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12508         cmd="exec $fd>&-"
12509         eval $cmd
12510         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12511
12512         #cleanup
12513         rm -rf $DIR/$tdir
12514 }
12515 run_test 236 "Layout swap on open unlinked file"
12516
12517 # test to verify file handle related system calls
12518 # (name_to_handle_at/open_by_handle_at)
12519 # The new system calls are supported in glibc >= 2.14.
12520
12521 test_237() {
12522         echo "Test file_handle syscalls" > $DIR/$tfile
12523         check_fhandle_syscalls $DIR/$tfile ||
12524                 error "check_fhandle_syscalls failed"
12525 }
12526 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12527
12528 # LU-4659 linkea consistency
12529 test_238() {
12530         touch $DIR/$tfile
12531         ln $DIR/$tfile $DIR/$tfile.lnk
12532         touch $DIR/$tfile.new
12533         mv $DIR/$tfile.new $DIR/$tfile
12534         local fid1=$(lfs path2fid $DIR/$tfile)
12535         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12536         local path1=$(lfs fid2path $FSNAME $fid1)
12537         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12538         local path2=$(lfs fid2path $FSNAME $fid2)
12539         [ $tfile.lnk == $path2 ] ||
12540                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12541         rm -f $DIR/$tfile*
12542 }
12543 run_test 238 "Verify linkea consistency"
12544
12545 test_striped_dir() {
12546         local mdt_index=$1
12547         local stripe_count
12548         local stripe_index
12549
12550         mkdir -p $DIR/$tdir
12551         $LFS setdirstripe -i $mdt_index -c 2 -t all_char $DIR/$tdir/striped_dir ||
12552                 error "set striped dir error"
12553
12554         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12555         if [ "$stripe_count" != "2" ]; then
12556                 error "stripe_count is $stripe_count, expect 2"
12557         fi
12558
12559         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12560         if [ "$stripe_index" != "$mdt_index" ]; then
12561                 error "stripe_index is $stripe_index, expect $mdt_index"
12562         fi
12563
12564         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12565                 error "nlink error after create striped dir"
12566
12567         mkdir $DIR/$tdir/striped_dir/a
12568         mkdir $DIR/$tdir/striped_dir/b
12569
12570         stat $DIR/$tdir/striped_dir/a ||
12571                 error "create dir under striped dir failed"
12572         stat $DIR/$tdir/striped_dir/b ||
12573                 error "create dir under striped dir failed"
12574
12575         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12576                 error "nlink error after mkdir"
12577
12578         rmdir $DIR/$tdir/striped_dir/a
12579         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12580                 error "nlink error after rmdir"
12581
12582         rmdir $DIR/$tdir/striped_dir/b
12583         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12584                 error "nlink error after rmdir"
12585
12586         rmdir $DIR/$tdir/striped_dir ||
12587                 error "rmdir striped dir error"
12588         true
12589 }
12590
12591 test_300a() {
12592         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12593         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12594
12595         test_striped_dir 0 || error "failed on striped dir on MDT0"
12596         test_striped_dir 1 || error "failed on striped dir on MDT0"
12597 }
12598 run_test 300a "basic striped dir sanity test"
12599
12600 test_300b() {
12601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12602         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12603         local i
12604         local mtime1
12605         local mtime2
12606         local mtime3
12607
12608         test_mkdir $DIR/$tdir || error "mkdir fail"
12609         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12610                 error "set striped dir error"
12611         for ((i=0; i<10; i++)); do
12612                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12613                 sleep 1
12614                 touch $DIR/$tdir/striped_dir/file_$i ||
12615                                         error "touch error $i"
12616                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12617                 [ $mtime1 -eq $mtime2 ] &&
12618                         error "mtime not change after create"
12619                 sleep 1
12620                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12621                                         error "unlink error $i"
12622                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12623                 [ $mtime2 -eq $mtime3 ] &&
12624                         error "mtime did not change after unlink"
12625         done
12626         true
12627 }
12628 run_test 300b "check ctime/mtime for striped dir"
12629
12630 test_300c() {
12631         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12632         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12633         local file_count
12634
12635         mkdir -p $DIR/$tdir
12636         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12637                 error "set striped dir error"
12638
12639         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12640                 error "chown striped dir failed"
12641
12642         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12643                 error "create 5k files failed"
12644
12645         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12646
12647         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12648
12649         rm -rf $DIR/$tdir
12650 }
12651 run_test 300c "chown && check ls under striped directory"
12652
12653 test_300d() {
12654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12655         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12656         local stripe_count
12657         local file
12658
12659         mkdir -p $DIR/$tdir
12660         $SETSTRIPE -c 2 $DIR/$tdir
12661
12662         #local striped directory
12663         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12664                 error "set striped dir error"
12665         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12666                 error "create 10 files failed"
12667
12668         #remote striped directory
12669         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12670                 error "set striped dir error"
12671         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12672                 error "create 10 files failed"
12673
12674         for file in $(find $DIR/$tdir); do
12675                 stripe_count=$($GETSTRIPE -c $file)
12676                 [ $stripe_count -eq 2 ] ||
12677                         error "wrong stripe $stripe_count for $file"
12678         done
12679
12680         rm -rf $DIR/$tdir
12681 }
12682 run_test 300d "check default stripe under striped directory"
12683
12684 test_300e() {
12685         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12686         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12687         local stripe_count
12688         local file
12689
12690         mkdir -p $DIR/$tdir
12691
12692         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12693                 error "set striped dir error"
12694
12695         touch $DIR/$tdir/striped_dir/a
12696         touch $DIR/$tdir/striped_dir/b
12697         touch $DIR/$tdir/striped_dir/c
12698
12699         mkdir $DIR/$tdir/striped_dir/dir_a
12700         mkdir $DIR/$tdir/striped_dir/dir_b
12701         mkdir $DIR/$tdir/striped_dir/dir_c
12702
12703         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12704                 error "set striped dir under striped dir error"
12705
12706         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12707                 error "set striped dir under striped dir error"
12708
12709         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12710                 error "set striped dir under striped dir error"
12711
12712         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12713                 error "rename file under striped dir should fail"
12714
12715         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12716                 error "rename dir under striped dir should fail"
12717
12718         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12719                 error "rename dir under different stripes should fail"
12720
12721         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12722                 error "rename file under striped dir should succeed"
12723
12724         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12725                 error "rename dir under striped dir should succeed"
12726
12727         rm -rf $DIR/$tdir
12728 }
12729 run_test 300e "check rename under striped directory"
12730
12731 test_300f() {
12732         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12733         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12734         local stripe_count
12735         local file
12736
12737         rm -rf $DIR/$tdir
12738         mkdir -p $DIR/$tdir
12739
12740         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12741                 error "set striped dir error"
12742
12743         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12744                 error "set striped dir error"
12745
12746         touch $DIR/$tdir/striped_dir/a
12747         mkdir $DIR/$tdir/striped_dir/dir_a
12748         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12749                 error "create striped dir under striped dir fails"
12750
12751         touch $DIR/$tdir/striped_dir1/b
12752         mkdir $DIR/$tdir/striped_dir1/dir_b
12753         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12754                 error "create striped dir under striped dir fails"
12755
12756         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12757                 error "rename file under different striped dir should fail"
12758
12759         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12760                 error "rename dir under different striped dir should fail"
12761
12762         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12763                 error "rename striped dir under diff striped dir should fail"
12764
12765         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12766                 error "rename file under diff striped dirs fails"
12767
12768         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12769                 error "rename dir under diff striped dirs fails"
12770
12771         rm -rf $DIR/$tdir
12772 }
12773 run_test 300f "check rename cross striped directory"
12774
12775 test_300g() {
12776         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12777         local stripe_count
12778         local dir
12779
12780         mkdir $DIR/$tdir
12781         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
12782                                         $DIR/$tdir/striped_dir ||
12783                 error "set striped dir error"
12784
12785         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12786                 error "set default stripe on striped dir error"
12787
12788         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12789
12790         for dir in $(find $DIR/$tdir/striped_dir/*); do
12791                 stripe_count=$($LFS getdirstripe -c $dir)
12792                 [ $stripe_count -eq $MDSCOUNT ] ||
12793                         error "expect $MDSCOUNT get $stripe_count for $dir"
12794         done
12795
12796         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
12797         #change default stripe count to 2
12798         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
12799                 error "set default stripe on striped dir error"
12800
12801         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12802
12803         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
12804
12805         #change default stripe count to 1
12806         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
12807                 error "set default stripe on striped dir error"
12808
12809         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12810         for dir in $(find $DIR/$tdir/striped_dir/*); do
12811                 stripe_count=$($LFS getdirstripe -c $dir)
12812                 [ $stripe_count -eq 1 ] ||
12813                         error "expect 1 get $stripe_count for $dir"
12814         done
12815         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
12816 }
12817 run_test 300g "check default striped directory for striped directory"
12818
12819 test_400a() { # LU-1606, was conf-sanity test_74
12820         local extra_flags=''
12821         local out=$TMP/$tfile
12822         local prefix=/usr/include/lustre
12823         local prog
12824
12825         if ! which $CC > /dev/null 2>&1; then
12826                 skip_env "$CC is not installed"
12827                 return 0
12828         fi
12829
12830         if ! [[ -d $prefix ]]; then
12831                 # Assume we're running in tree and fixup the include path.
12832                 extra_flags+=" -I$LUSTRE/../libcfs/include"
12833                 extra_flags+=" -I$LUSTRE/include"
12834                 extra_flags+=" -L$LUSTRE/utils"
12835         fi
12836
12837         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
12838                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
12839                         error "client api broken"
12840         done
12841 }
12842 run_test 400a "Lustre client api program can compile and link"
12843
12844 test_400b() { # LU-1606, LU-5011
12845         local header
12846         local out=$TMP/$tfile
12847         local prefix=/usr/include/lustre
12848
12849         # We use a hard coded prefix so that this test will not fail
12850         # when run in tree. There are headers in lustre/include/lustre/
12851         # that are not packaged (like lustre_idl.h) and have more
12852         # complicated include dependencies (like config.h and lnet/types.h).
12853         # Since this test about correct packaging we just skip them when
12854         # they don't exist (see below) rather than try to fixup cppflags.
12855
12856         if ! which $CC > /dev/null 2>&1; then
12857                 skip_env "$CC is not installed"
12858                 return 0
12859         fi
12860
12861         for header in $prefix/*.h; do
12862                 if ! [[ -f "$header" ]]; then
12863                         continue
12864                 fi
12865
12866                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
12867                         continue # liblustreapi.h is deprecated.
12868                 fi
12869
12870                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
12871                         error "cannot compile '$header'"
12872         done
12873 }
12874 run_test 400b "packaged headers can be compiled"
12875
12876 #
12877 # tests that do cleanup/setup should be run at the end
12878 #
12879
12880 test_900() {
12881         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12882         local ls
12883         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
12884         $LCTL set_param fail_loc=0x903
12885         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
12886         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
12887                 echo "clear" > $ls
12888         done
12889         FAIL_ON_ERROR=true cleanup
12890         FAIL_ON_ERROR=true setup
12891 }
12892 run_test 900 "umount should not race with any mgc requeue thread"
12893
12894 complete $SECONDS
12895 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
12896 check_and_cleanup_lustre
12897 if [ "$I_MOUNTED" != "yes" ]; then
12898         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
12899 fi
12900 exit_status