Whamcloud - gitweb
c0a039ac0c7fe46781e223a59936342d6867152f
[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                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4496
4497         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4498         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4499         NUMS=$($CMD | wc -l)
4500         [ $NUMS -eq $EXPECTED ] ||
4501                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4502
4503         EXPECTED=$ONESTRIPE
4504         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4505         NUMS=$($CMD | wc -l)
4506         [ $NUMS -eq $EXPECTED ] ||
4507                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4508
4509         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4510         NUMS=$($CMD | wc -l)
4511         [ $NUMS -eq $EXPECTED ] ||
4512                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4513
4514         EXPECTED=0
4515         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4516         NUMS=$($CMD | wc -l)
4517         [ $NUMS -eq $EXPECTED ] ||
4518                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4519 }
4520 run_test 56s "check lfs find -stripe-count works"
4521
4522 test_56t() { # LU-611
4523         TDIR=$DIR/${tdir}t
4524         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4525
4526         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4527
4528         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4529         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4530         NUMS=$($CMD | wc -l)
4531         [ $NUMS -eq $EXPECTED ] ||
4532                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4533
4534         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4535         NUMS=$($CMD | wc -l)
4536         [ $NUMS -eq $EXPECTED ] ||
4537                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4538
4539         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4540         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4541         NUMS=$($CMD | wc -l)
4542         [ $NUMS -eq $EXPECTED ] ||
4543                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4544
4545         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4546         NUMS=$($CMD | wc -l)
4547         [ $NUMS -eq $EXPECTED ] ||
4548                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4549
4550         EXPECTED=4
4551         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4552         NUMS=$($CMD | wc -l)
4553         [ $NUMS -eq $EXPECTED ] ||
4554                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4555
4556         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4557         NUMS=$($CMD | wc -l)
4558         [ $NUMS -eq $EXPECTED ] ||
4559                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4560
4561         EXPECTED=0
4562         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4563         NUMS=$($CMD | wc -l)
4564         [ $NUMS -eq $EXPECTED ] ||
4565                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4566 }
4567 run_test 56t "check lfs find -stripe-size works"
4568
4569 test_56u() { # LU-611
4570         TDIR=$DIR/${tdir}u
4571         setup_56 $NUMFILES $NUMDIRS "-i 0"
4572
4573         if [[ $OSTCOUNT -gt 1 ]]; then
4574                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4575                 ONESTRIPE=4
4576         else
4577                 ONESTRIPE=0
4578         fi
4579
4580         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4581         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4582         NUMS=$($CMD | wc -l)
4583         [ $NUMS -eq $EXPECTED ] ||
4584                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4585
4586         EXPECTED=$ONESTRIPE
4587         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4588         NUMS=$($CMD | wc -l)
4589         [ $NUMS -eq $EXPECTED ] ||
4590                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4591
4592         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4593         NUMS=$($CMD | wc -l)
4594         [ $NUMS -eq $EXPECTED ] ||
4595                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4596
4597         EXPECTED=0
4598         # This should produce an error and not return any files
4599         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4600         NUMS=$($CMD 2>/dev/null | wc -l)
4601         [ $NUMS -eq $EXPECTED ] ||
4602                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4603
4604         if [[ $OSTCOUNT -gt 1 ]]; then
4605                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4606                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4607                 NUMS=$($CMD | wc -l)
4608                 [ $NUMS -eq $EXPECTED ] ||
4609                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4610         fi
4611 }
4612 run_test 56u "check lfs find -stripe-index works"
4613
4614 test_56v() {
4615     local MDT_IDX=0
4616
4617     TDIR=$DIR/${tdir}v
4618     rm -rf $TDIR
4619     setup_56 $NUMFILES $NUMDIRS
4620
4621     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4622     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4623
4624     for file in $($LFIND -mdt $UUID $TDIR); do
4625         file_mdt_idx=$($GETSTRIPE -M $file)
4626         [ $file_mdt_idx -eq $MDT_IDX ] ||
4627             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4628     done
4629 }
4630 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4631
4632 # Get and check the actual stripe count of one file.
4633 # Usage: check_stripe_count <file> <expected_stripe_count>
4634 check_stripe_count() {
4635     local file=$1
4636     local expected=$2
4637     local actual
4638
4639     [[ -z "$file" || -z "$expected" ]] &&
4640         error "check_stripe_count: invalid argument!"
4641
4642     local cmd="$GETSTRIPE -c $file"
4643     actual=$($cmd) || error "$cmd failed"
4644     actual=${actual%% *}
4645
4646     if [[ $actual -ne $expected ]]; then
4647         [[ $expected -eq -1 ]] ||
4648             error "$cmd wrong: found $actual, expected $expected"
4649         [[ $actual -eq $OSTCOUNT ]] ||
4650             error "$cmd wrong: found $actual, expected $OSTCOUNT"
4651     fi
4652 }
4653
4654 test_56w() {
4655         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4656         TDIR=$DIR/${tdir}w
4657
4658     rm -rf $TDIR || error "remove $TDIR failed"
4659     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4660
4661     local stripe_size
4662     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4663         error "$GETSTRIPE -S -d $TDIR failed"
4664     stripe_size=${stripe_size%% *}
4665
4666     local file_size=$((stripe_size * OSTCOUNT))
4667     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4668     local required_space=$((file_num * file_size))
4669     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4670     [[ $free_space -le $((required_space / 1024)) ]] &&
4671         skip_env "need at least $required_space bytes free space," \
4672                  "have $free_space kbytes" && return
4673
4674     local dd_bs=65536
4675     local dd_count=$((file_size / dd_bs))
4676
4677     # write data into the files
4678     local i
4679     local j
4680     local file
4681     for i in $(seq 1 $NUMFILES); do
4682         file=$TDIR/file$i
4683         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4684             error "write data into $file failed"
4685     done
4686     for i in $(seq 1 $NUMDIRS); do
4687         for j in $(seq 1 $NUMFILES); do
4688             file=$TDIR/dir$i/file$j
4689             yes | dd bs=$dd_bs count=$dd_count of=$file \
4690                 >/dev/null 2>&1 ||
4691                 error "write data into $file failed"
4692         done
4693     done
4694
4695     local expected=-1
4696     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4697
4698     # lfs_migrate file
4699     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4700     echo "$cmd"
4701     eval $cmd || error "$cmd failed"
4702
4703     check_stripe_count $TDIR/file1 $expected
4704
4705     # lfs_migrate dir
4706     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4707     echo "$cmd"
4708     eval $cmd || error "$cmd failed"
4709
4710     for j in $(seq 1 $NUMFILES); do
4711         check_stripe_count $TDIR/dir1/file$j $expected
4712     done
4713
4714     # lfs_migrate works with lfs find
4715     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4716          $LFS_MIGRATE -y -c $expected"
4717     echo "$cmd"
4718     eval $cmd || error "$cmd failed"
4719
4720     for i in $(seq 2 $NUMFILES); do
4721         check_stripe_count $TDIR/file$i $expected
4722     done
4723     for i in $(seq 2 $NUMDIRS); do
4724         for j in $(seq 1 $NUMFILES); do
4725             check_stripe_count $TDIR/dir$i/file$j $expected
4726         done
4727     done
4728 }
4729 run_test 56w "check lfs_migrate -c stripe_count works"
4730
4731 test_56x() {
4732         check_swap_layouts_support && return 0
4733         [[ $OSTCOUNT -lt 2 ]] &&
4734                 skip_env "need 2 OST, skipping test" && return
4735
4736         local dir0=$DIR/$tdir/$testnum
4737         mkdir -p $dir0 || error "creating dir $dir0"
4738
4739         local ref1=/etc/passwd
4740         local file1=$dir0/file1
4741
4742         $SETSTRIPE -c 2 $file1
4743         cp $ref1 $file1
4744         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4745         stripe=$($GETSTRIPE -c $file1)
4746         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4747         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4748
4749         # clean up
4750         rm -f $file1
4751 }
4752 run_test 56x "lfs migration support"
4753
4754 test_56y() {
4755         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4756                 skip "No HSM support on MDS of $(get_lustre_version)," \
4757                          "need 2.4.53 at least" && return
4758         local res=""
4759
4760         local dir0=$DIR/$tdir/$testnum
4761         mkdir -p $dir0 || error "creating dir $dir0"
4762         local f1=$dir0/file1
4763         local f2=$dir0/file2
4764
4765         touch $f1 || error "creating std file $f1"
4766         $MULTIOP $f2 H2c || error "creating released file $f2"
4767
4768         # a directory can be raid0, so ask only for files
4769         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4770         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4771
4772         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4773         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4774
4775         # only files can be released, so no need to force file search
4776         res=$($LFIND $dir0 -L released)
4777         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4778
4779         res=$($LFIND $dir0 \! -L released)
4780         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4781
4782 }
4783 run_test 56y "lfs find -L raid0|released"
4784
4785 test_57a() {
4786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4787         # note test will not do anything if MDS is not local
4788         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4789                 skip "Only applicable to ldiskfs-based MDTs"
4790                 return
4791         fi
4792
4793         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4794         local MNTDEV="osd*.*MDT*.mntdev"
4795         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
4796         [ -z "$DEV" ] && error "can't access $MNTDEV"
4797         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
4798                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
4799                         error "can't access $DEV"
4800                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
4801                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
4802                 rm $TMP/t57a.dump
4803         done
4804 }
4805 run_test 57a "verify MDS filesystem created with large inodes =="
4806
4807 test_57b() {
4808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4809         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4810                 skip "Only applicable to ldiskfs-based MDTs"
4811                 return
4812         fi
4813
4814         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4815         local dir=$DIR/d57b
4816
4817         local FILECOUNT=100
4818         local FILE1=$dir/f1
4819         local FILEN=$dir/f$FILECOUNT
4820
4821         rm -rf $dir || error "removing $dir"
4822         test_mkdir -p $dir || error "creating $dir"
4823         local num=$(get_mds_dir $dir)
4824         local mymds=mds$num
4825
4826         echo "mcreating $FILECOUNT files"
4827         createmany -m $dir/f 1 $FILECOUNT || \
4828                 error "creating files in $dir"
4829
4830         # verify that files do not have EAs yet
4831         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
4832         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
4833
4834         sync
4835         sleep 1
4836         df $dir  #make sure we get new statfs data
4837         local MDSFREE=$(do_facet $mymds \
4838                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4839         local MDCFREE=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4840         echo "opening files to create objects/EAs"
4841         local FILE
4842         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
4843                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
4844         done
4845
4846         # verify that files have EAs now
4847         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
4848         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
4849
4850         sleep 1  #make sure we get new statfs data
4851         df $dir
4852         local MDSFREE2=$(do_facet $mymds \
4853                 lctl get_param -n osd*.*MDT000$((num -1)).kbytesfree)
4854         local MDCFREE2=$(lctl get_param -n mdc.*MDT000$((num -1))-mdc-*.kbytesfree)
4855         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
4856                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
4857                         error "MDC before $MDCFREE != after $MDCFREE2"
4858                 else
4859                         echo "MDC before $MDCFREE != after $MDCFREE2"
4860                         echo "unable to confirm if MDS has large inodes"
4861                 fi
4862         fi
4863         rm -rf $dir
4864 }
4865 run_test 57b "default LOV EAs are stored inside large inodes ==="
4866
4867 test_58() {
4868         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4869         [ -z "$(which wiretest 2>/dev/null)" ] &&
4870                         skip_env "could not find wiretest" && return
4871         wiretest
4872 }
4873 run_test 58 "verify cross-platform wire constants =============="
4874
4875 test_59() {
4876         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4877         echo "touch 130 files"
4878         createmany -o $DIR/f59- 130
4879         echo "rm 130 files"
4880         unlinkmany $DIR/f59- 130
4881         sync
4882         # wait for commitment of removal
4883         wait_delete_completed
4884 }
4885 run_test 59 "verify cancellation of llog records async ========="
4886
4887 TEST60_HEAD="test_60 run $RANDOM"
4888 test_60a() {
4889         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4890         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
4891         do_facet mgs "! which run-llog.sh &> /dev/null" &&
4892                 skip_env "missing subtest run-llog.sh" && return
4893         log "$TEST60_HEAD - from kernel mode"
4894         do_facet mgs sh run-llog.sh
4895 }
4896 run_test 60a "llog sanity tests run from kernel module =========="
4897
4898 test_60b() { # bug 6411
4899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4900         dmesg > $DIR/$tfile
4901         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
4902                                  /llog.test/ {
4903                                          if (marker)
4904                                                  from_marker++
4905                                          from_begin++
4906                                  }
4907                                  END {
4908                                          if (marker)
4909                                                  print from_marker
4910                                          else
4911                                                  print from_begin
4912                                  }"`
4913         [[ $LLOG_COUNT -gt 50 ]] &&
4914                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
4915 }
4916 run_test 60b "limit repeated messages from CERROR/CWARN ========"
4917
4918 test_60c() {
4919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4920         echo "create 5000 files"
4921         createmany -o $DIR/f60c- 5000
4922 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
4923         lctl set_param fail_loc=0x80000137
4924         unlinkmany $DIR/f60c- 5000
4925         lctl set_param fail_loc=0
4926 }
4927 run_test 60c "unlink file when mds full"
4928
4929 test_60d() {
4930         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4931         SAVEPRINTK=$(lctl get_param -n printk)
4932
4933         # verify "lctl mark" is even working"
4934         MESSAGE="test message ID $RANDOM $$"
4935         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4936         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
4937
4938         lctl set_param printk=0 || error "set lnet.printk failed"
4939         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
4940         MESSAGE="new test message ID $RANDOM $$"
4941         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
4942         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
4943         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
4944
4945         lctl set_param -n printk="$SAVEPRINTK"
4946 }
4947 run_test 60d "test printk console message masking"
4948
4949 test_61() {
4950         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4951         f="$DIR/f61"
4952         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
4953         cancel_lru_locks osc
4954         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
4955         sync
4956 }
4957 run_test 61 "mmap() writes don't make sync hang ================"
4958
4959 # bug 2330 - insufficient obd_match error checking causes LBUG
4960 test_62() {
4961         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4962         f="$DIR/f62"
4963         echo foo > $f
4964         cancel_lru_locks osc
4965         lctl set_param fail_loc=0x405
4966         cat $f && error "cat succeeded, expect -EIO"
4967         lctl set_param fail_loc=0
4968 }
4969 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
4970 # match every page all of the time.
4971 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
4972
4973 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
4974 # Though this test is irrelevant anymore, it helped to reveal some
4975 # other grant bugs (LU-4482), let's keep it.
4976 test_63a() {   # was test_63
4977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4978         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
4979         for i in `seq 10` ; do
4980                 dd if=/dev/zero of=$DIR/f63 bs=8k &
4981                 sleep 5
4982                 kill $!
4983                 sleep 1
4984         done
4985
4986         rm -f $DIR/f63 || true
4987 }
4988 run_test 63a "Verify oig_wait interruption does not crash ======="
4989
4990 # bug 2248 - async write errors didn't return to application on sync
4991 # bug 3677 - async write errors left page locked
4992 test_63b() {
4993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4994         debugsave
4995         lctl set_param debug=-1
4996
4997         # ensure we have a grant to do async writes
4998         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
4999         rm $DIR/$tfile
5000
5001         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5002         lctl set_param fail_loc=0x80000406
5003         $MULTIOP $DIR/$tfile Owy && \
5004                 error "sync didn't return ENOMEM"
5005         sync; sleep 2; sync     # do a real sync this time to flush page
5006         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5007                 error "locked page left in cache after async error" || true
5008         debugrestore
5009 }
5010 run_test 63b "async write errors should be returned to fsync ==="
5011
5012 test_64a () {
5013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5014         df $DIR
5015         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5016 }
5017 run_test 64a "verify filter grant calculations (in kernel) ====="
5018
5019 test_64b () {
5020         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5021         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5022 }
5023 run_test 64b "check out-of-space detection on client ==========="
5024
5025 test_64c() {
5026         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5027 }
5028 run_test 64c "verify grant shrink ========================------"
5029
5030 # bug 1414 - set/get directories' stripe info
5031 test_65a() {
5032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5033         test_mkdir -p $DIR/$tdir
5034         touch $DIR/$tdir/f1
5035         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5036 }
5037 run_test 65a "directory with no stripe info ===================="
5038
5039 test_65b() {
5040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5041         test_mkdir -p $DIR/$tdir
5042         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5043                                                 error "setstripe"
5044         touch $DIR/$tdir/f2
5045         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5046 }
5047 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5048
5049 test_65c() {
5050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5051         if [[ $OSTCOUNT -gt 1 ]]; then
5052                 test_mkdir -p $DIR/$tdir
5053                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5054                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5055                 touch $DIR/$tdir/f3
5056                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5057         fi
5058 }
5059 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5060
5061 test_65d() {
5062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5063         test_mkdir -p $DIR/$tdir
5064         if [[ $STRIPECOUNT -le 0 ]]; then
5065                 sc=1
5066         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5067 #LOV_MAX_STRIPE_COUNT is 2000
5068                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5069         else
5070                 sc=$(($STRIPECOUNT - 1))
5071         fi
5072         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5073         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5074         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5075                 error "lverify failed"
5076 }
5077 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5078
5079 test_65e() {
5080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5081         test_mkdir -p $DIR/$tdir
5082
5083         $SETSTRIPE $DIR/$tdir || error "setstripe"
5084         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5085                                         error "no stripe info failed"
5086         touch $DIR/$tdir/f6
5087         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5088 }
5089 run_test 65e "directory setstripe defaults ======================="
5090
5091 test_65f() {
5092         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5093         test_mkdir -p $DIR/${tdir}f
5094         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5095 }
5096 run_test 65f "dir setstripe permission (should return error) ==="
5097
5098 test_65g() {
5099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5100         test_mkdir -p $DIR/$tdir
5101         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5102                                                         error "setstripe"
5103         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5104         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5105                 error "delete default stripe failed"
5106 }
5107 run_test 65g "directory setstripe -d ==========================="
5108
5109 test_65h() {
5110         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5111         test_mkdir -p $DIR/$tdir
5112         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5113                                                         error "setstripe"
5114         test_mkdir -p $DIR/$tdir/dd1
5115         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5116                 error "stripe info inherit failed"
5117 }
5118 run_test 65h "directory stripe info inherit ===================="
5119
5120 test_65i() { # bug6367
5121         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5122         $SETSTRIPE -S 65536 -c -1 $MOUNT
5123 }
5124 run_test 65i "set non-default striping on root directory (bug 6367)="
5125
5126 test_65ia() { # bug12836
5127         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5128         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5129 }
5130 run_test 65ia "getstripe on -1 default directory striping"
5131
5132 test_65ib() { # bug12836
5133         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5134         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5135 }
5136 run_test 65ib "getstripe -v on -1 default directory striping"
5137
5138 test_65ic() { # bug12836
5139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5140         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5141 }
5142 run_test 65ic "new find on -1 default directory striping"
5143
5144 test_65j() { # bug6367
5145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5146         sync; sleep 1
5147         # if we aren't already remounting for each test, do so for this test
5148         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5149                 cleanup || error "failed to unmount"
5150                 setup
5151         fi
5152         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5153 }
5154 run_test 65j "set default striping on root directory (bug 6367)="
5155
5156 test_65k() { # bug11679
5157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5158         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5159         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5160
5161     echo "Check OST status: "
5162     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5163               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5164
5165     for OSC in $MDS_OSCS; do
5166         echo $OSC "is activate"
5167         do_facet $SINGLEMDS lctl --device %$OSC activate
5168     done
5169
5170     mkdir -p $DIR/$tdir
5171     for INACTIVE_OSC in $MDS_OSCS; do
5172         echo "Deactivate: " $INACTIVE_OSC
5173         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5174         for STRIPE_OSC in $MDS_OSCS; do
5175             OST=`osc_to_ost $STRIPE_OSC`
5176             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5177                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5178
5179             [ -f $DIR/$tdir/$IDX ] && continue
5180             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5181             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5182             RC=$?
5183             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5184         done
5185         rm -f $DIR/$tdir/*
5186         echo $INACTIVE_OSC "is Activate."
5187         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5188     done
5189 }
5190 run_test 65k "validate manual striping works properly with deactivated OSCs"
5191
5192 test_65l() { # bug 12836
5193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5194         test_mkdir -p $DIR/$tdir/test_dir
5195         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5196         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5197 }
5198 run_test 65l "lfs find on -1 stripe dir ========================"
5199
5200 # bug 2543 - update blocks count on client
5201 test_66() {
5202         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5203         COUNT=${COUNT:-8}
5204         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5205         sync; sync_all_data; sync; sync_all_data
5206         cancel_lru_locks osc
5207         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5208         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5209 }
5210 run_test 66 "update inode blocks count on client ==============="
5211
5212 LLOOP=
5213 LLITELOOPLOAD=
5214 cleanup_68() {
5215         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5216         trap 0
5217         if [ ! -z "$LLOOP" ]; then
5218                 if swapon -s | grep -q $LLOOP; then
5219                         swapoff $LLOOP || error "swapoff failed"
5220                 fi
5221
5222                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5223                 rm -f $LLOOP
5224                 unset LLOOP
5225         fi
5226         if [ ! -z "$LLITELOOPLOAD" ]; then
5227                 rmmod llite_lloop
5228                 unset LLITELOOPLOAD
5229         fi
5230         rm -f $DIR/f68*
5231 }
5232
5233 meminfo() {
5234         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5235 }
5236
5237 swap_used() {
5238         swapon -s | awk '($1 == "'$1'") { print $4 }'
5239 }
5240
5241 # test case for lloop driver, basic function
5242 test_68a() {
5243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5244         [ "$UID" != 0 ] && skip_env "must run as root" && return
5245         llite_lloop_enabled || \
5246                 { skip_env "llite_lloop module disabled" && return; }
5247
5248         trap cleanup_68 EXIT
5249
5250         if ! module_loaded llite_lloop; then
5251                 if load_module llite/llite_lloop; then
5252                         LLITELOOPLOAD=yes
5253                 else
5254                         skip_env "can't find module llite_lloop"
5255                         return
5256                 fi
5257         fi
5258
5259         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5260         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5261         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5262
5263         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5264         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5265
5266         cleanup_68
5267 }
5268 run_test 68a "lloop driver - basic test ========================"
5269
5270 # excercise swapping to lustre by adding a high priority swapfile entry
5271 # and then consuming memory until it is used.
5272 test_68b() {  # was test_68
5273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5274         [ "$UID" != 0 ] && skip_env "must run as root" && return
5275         lctl get_param -n devices | grep -q obdfilter && \
5276                 skip "local OST" && return
5277
5278         grep -q llite_lloop /proc/modules
5279         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5280
5281         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5282                 skip "can't reliably test swap with TCP" && return
5283
5284         MEMTOTAL=`meminfo MemTotal`
5285         NR_BLOCKS=$((MEMTOTAL>>8))
5286         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5287
5288         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5289         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5290         mkswap $DIR/f68b
5291
5292         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5293
5294         trap cleanup_68 EXIT
5295
5296         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5297
5298         echo "before: `swapon -s | grep $LLOOP`"
5299         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5300         echo "after: `swapon -s | grep $LLOOP`"
5301         SWAPUSED=`swap_used $LLOOP`
5302
5303         cleanup_68
5304
5305         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5306 }
5307 run_test 68b "support swapping to Lustre ========================"
5308
5309 # bug5265, obdfilter oa2dentry return -ENOENT
5310 # #define OBD_FAIL_OST_ENOENT 0x217
5311 test_69() {
5312         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5313         remote_ost_nodsh && skip "remote OST with nodsh" && return
5314
5315         f="$DIR/$tfile"
5316         $SETSTRIPE -c 1 -i 0 $f
5317
5318         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5319
5320         do_facet ost1 lctl set_param fail_loc=0x217
5321         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5322         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5323
5324         do_facet ost1 lctl set_param fail_loc=0
5325         $DIRECTIO write $f 0 2 || error "write error"
5326
5327         cancel_lru_locks osc
5328         $DIRECTIO read $f 0 1 || error "read error"
5329
5330         do_facet ost1 lctl set_param fail_loc=0x217
5331         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5332
5333         do_facet ost1 lctl set_param fail_loc=0
5334         rm -f $f
5335 }
5336 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5337
5338 test_71() {
5339     test_mkdir -p $DIR/$tdir
5340     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5341 }
5342 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5343
5344 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5345         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5346         [ "$RUNAS_ID" = "$UID" ] &&
5347                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5348
5349         # Check that testing environment is properly set up. Skip if not
5350         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5351                 skip_env "User $RUNAS_ID does not exist - skipping"
5352                 return 0
5353         }
5354         # We had better clear the $DIR to get enough space for dd
5355         rm -rf $DIR/*
5356         touch $DIR/$tfile
5357         chmod 777 $DIR/$tfile
5358         chmod ug+s $DIR/$tfile
5359         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5360                 error "$RUNAS dd $DIR/$tfile failed"
5361         # See if we are still setuid/sgid
5362         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5363                 error "S/gid is not dropped on write"
5364         # Now test that MDS is updated too
5365         cancel_lru_locks mdc
5366         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5367                 error "S/gid is not dropped on MDS"
5368         rm -f $DIR/$tfile
5369 }
5370 run_test 72a "Test that remove suid works properly (bug5695) ===="
5371
5372 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5373         local perm
5374
5375         [ "$RUNAS_ID" = "$UID" ] && \
5376                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5377         [ "$RUNAS_ID" -eq 0 ] && \
5378                 skip_env "RUNAS_ID = 0 -- skipping" && return
5379
5380         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5381         # Check that testing environment is properly set up. Skip if not
5382         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5383                 skip_env "User $RUNAS_ID does not exist - skipping"
5384                 return 0
5385         }
5386         touch $DIR/${tfile}-f{g,u}
5387         test_mkdir $DIR/${tfile}-dg
5388         test_mkdir $DIR/${tfile}-du
5389         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5390         chmod g+s $DIR/${tfile}-{f,d}g
5391         chmod u+s $DIR/${tfile}-{f,d}u
5392         for perm in 777 2777 4777; do
5393                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5394                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5395                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5396                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5397         done
5398         true
5399 }
5400 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5401
5402 # bug 3462 - multiple simultaneous MDC requests
5403 test_73() {
5404         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5405         test_mkdir $DIR/d73-1
5406         test_mkdir $DIR/d73-2
5407         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5408         pid1=$!
5409
5410         lctl set_param fail_loc=0x80000129
5411         $MULTIOP $DIR/d73-1/f73-2 Oc &
5412         sleep 1
5413         lctl set_param fail_loc=0
5414
5415         $MULTIOP $DIR/d73-2/f73-3 Oc &
5416         pid3=$!
5417
5418         kill -USR1 $pid1
5419         wait $pid1 || return 1
5420
5421         sleep 25
5422
5423         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5424         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5425         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5426
5427         rm -rf $DIR/d73-*
5428 }
5429 run_test 73 "multiple MDC requests (should not deadlock)"
5430
5431 test_74a() { # bug 6149, 6184
5432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5433         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5434         #
5435         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5436         # will spin in a tight reconnection loop
5437         touch $DIR/f74a
5438         $LCTL set_param fail_loc=0x8000030e
5439         # get any lock that won't be difficult - lookup works.
5440         ls $DIR/f74a
5441         $LCTL set_param fail_loc=0
5442         rm -f $DIR/f74a
5443         true
5444 }
5445 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5446
5447 test_74b() { # bug 13310
5448         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5449         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5450         #
5451         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5452         # will spin in a tight reconnection loop
5453         $LCTL set_param fail_loc=0x8000030e
5454         # get a "difficult" lock
5455         touch $DIR/f74b
5456         $LCTL set_param fail_loc=0
5457         rm -f $DIR/f74b
5458         true
5459 }
5460 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5461
5462 test_74c() {
5463         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5464         #define OBD_FAIL_LDLM_NEW_LOCK
5465         $LCTL set_param fail_loc=0x319
5466         touch $DIR/$tfile && error "touch successful"
5467         $LCTL set_param fail_loc=0
5468         true
5469 }
5470 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5471
5472 num_inodes() {
5473         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5474 }
5475
5476 get_inode_slab_tunables() {
5477         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5478 }
5479
5480 set_inode_slab_tunables() {
5481         echo "lustre_inode_cache $1" > /proc/slabinfo
5482 }
5483
5484 test_76() { # Now for bug 20433, added originally in bug 1443
5485         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5486         local SLAB_SETTINGS=`get_inode_slab_tunables`
5487         local CPUS=`getconf _NPROCESSORS_ONLN`
5488         # we cannot set limit below 1 which means 1 inode in each
5489         # per-cpu cache is still allowed
5490         set_inode_slab_tunables "1 1 0"
5491         cancel_lru_locks osc
5492         BEFORE_INODES=$(num_inodes)
5493         echo "before inodes: $BEFORE_INODES"
5494         local COUNT=1000
5495         [ "$SLOW" = "no" ] && COUNT=100
5496         for i in $(seq $COUNT); do
5497                 touch $DIR/$tfile
5498                 rm -f $DIR/$tfile
5499         done
5500         cancel_lru_locks osc
5501         AFTER_INODES=$(num_inodes)
5502         echo "after inodes: $AFTER_INODES"
5503         local wait=0
5504         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5505                 sleep 2
5506                 AFTER_INODES=$(num_inodes)
5507                 wait=$((wait+2))
5508                 echo "wait $wait seconds inodes: $AFTER_INODES"
5509                 if [ $wait -gt 30 ]; then
5510                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5511                 fi
5512         done
5513         set_inode_slab_tunables "$SLAB_SETTINGS"
5514 }
5515 run_test 76 "confirm clients recycle inodes properly ===="
5516
5517
5518 export ORIG_CSUM=""
5519 set_checksums()
5520 {
5521         # Note: in sptlrpc modes which enable its own bulk checksum, the
5522         # original crc32_le bulk checksum will be automatically disabled,
5523         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5524         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5525         # In this case set_checksums() will not be no-op, because sptlrpc
5526         # bulk checksum will be enabled all through the test.
5527
5528         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5529         lctl set_param -n osc.*.checksums $1
5530         return 0
5531 }
5532
5533 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5534                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5535 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5536 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5537 set_checksum_type()
5538 {
5539         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5540         log "set checksum type to $1"
5541         return 0
5542 }
5543 F77_TMP=$TMP/f77-temp
5544 F77SZ=8
5545 setup_f77() {
5546         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5547                 error "error writing to $F77_TMP"
5548 }
5549
5550 test_77a() { # bug 10889
5551         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5552         $GSS && skip "could not run with gss" && return
5553         [ ! -f $F77_TMP ] && setup_f77
5554         set_checksums 1
5555         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5556         set_checksums 0
5557         rm -f $DIR/$tfile
5558 }
5559 run_test 77a "normal checksum read/write operation"
5560
5561 test_77b() { # bug 10889
5562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5563         $GSS && skip "could not run with gss" && return
5564         [ ! -f $F77_TMP ] && setup_f77
5565         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5566         $LCTL set_param fail_loc=0x80000409
5567         set_checksums 1
5568
5569         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5570                 error "dd error: $?"
5571         $LCTL set_param fail_loc=0
5572
5573         for algo in $CKSUM_TYPES; do
5574                 cancel_lru_locks osc
5575                 set_checksum_type $algo
5576                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5577                 $LCTL set_param fail_loc=0x80000408
5578                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5579                 $LCTL set_param fail_loc=0
5580         done
5581         set_checksums 0
5582         set_checksum_type $ORIG_CSUM_TYPE
5583         rm -f $DIR/$tfile
5584 }
5585 run_test 77b "checksum error on client write, read"
5586
5587 test_77d() { # bug 10889
5588         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5589         $GSS && skip "could not run with gss" && return
5590         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5591         $LCTL set_param fail_loc=0x80000409
5592         set_checksums 1
5593         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5594                 error "direct write: rc=$?"
5595         $LCTL set_param fail_loc=0
5596         set_checksums 0
5597
5598         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5599         $LCTL set_param fail_loc=0x80000408
5600         set_checksums 1
5601         cancel_lru_locks osc
5602         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5603                 error "direct read: rc=$?"
5604         $LCTL set_param fail_loc=0
5605         set_checksums 0
5606 }
5607 run_test 77d "checksum error on OST direct write, read"
5608
5609 test_77f() { # bug 10889
5610         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5611         $GSS && skip "could not run with gss" && return
5612         set_checksums 1
5613         for algo in $CKSUM_TYPES; do
5614                 cancel_lru_locks osc
5615                 set_checksum_type $algo
5616                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5617                 $LCTL set_param fail_loc=0x409
5618                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5619                         error "direct write succeeded"
5620                 $LCTL set_param fail_loc=0
5621         done
5622         set_checksum_type $ORIG_CSUM_TYPE
5623         set_checksums 0
5624 }
5625 run_test 77f "repeat checksum error on write (expect error)"
5626
5627 test_77g() { # bug 10889
5628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5629         $GSS && skip "could not run with gss" && return
5630         remote_ost_nodsh && skip "remote OST with nodsh" && return
5631
5632         [ ! -f $F77_TMP ] && setup_f77
5633
5634         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5635         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5636         do_facet ost1 lctl set_param fail_loc=0x8000021a
5637         set_checksums 1
5638         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5639                 error "write error: rc=$?"
5640         do_facet ost1 lctl set_param fail_loc=0
5641         set_checksums 0
5642
5643         cancel_lru_locks osc
5644         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5645         do_facet ost1 lctl set_param fail_loc=0x8000021b
5646         set_checksums 1
5647         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5648         do_facet ost1 lctl set_param fail_loc=0
5649         set_checksums 0
5650 }
5651 run_test 77g "checksum error on OST write, read"
5652
5653 test_77i() { # bug 13805
5654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5655         $GSS && skip "could not run with gss" && return
5656         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5657         lctl set_param fail_loc=0x40b
5658         remount_client $MOUNT
5659         lctl set_param fail_loc=0
5660         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5661                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5662                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5663                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5664         done
5665         remount_client $MOUNT
5666 }
5667 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5668
5669 test_77j() { # bug 13805
5670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5671         $GSS && skip "could not run with gss" && return
5672         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5673         lctl set_param fail_loc=0x40c
5674         remount_client $MOUNT
5675         lctl set_param fail_loc=0
5676         sleep 2 # wait async osc connect to finish
5677         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5678                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5679                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5680                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5681         done
5682         remount_client $MOUNT
5683 }
5684 run_test 77j "client only supporting ADLER32"
5685
5686 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5687 rm -f $F77_TMP
5688 unset F77_TMP
5689
5690 test_78() { # bug 10901
5691         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5692         remote_ost || { skip_env "local OST" && return; }
5693
5694         NSEQ=5
5695         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5696         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5697         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5698         echo "MemTotal: $MEMTOTAL"
5699
5700         # reserve 256MB of memory for the kernel and other running processes,
5701         # and then take 1/2 of the remaining memory for the read/write buffers.
5702         if [ $MEMTOTAL -gt 512 ] ;then
5703                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5704         else
5705                 # for those poor memory-starved high-end clusters...
5706                 MEMTOTAL=$((MEMTOTAL / 2))
5707         fi
5708         echo "Mem to use for directio: $MEMTOTAL"
5709
5710         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5711         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5712         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5713         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5714                 head -n1)
5715         echo "Smallest OST: $SMALLESTOST"
5716         [[ $SMALLESTOST -lt 10240 ]] &&
5717                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5718
5719         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5720                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5721
5722         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5723         echo "File size: $F78SIZE"
5724         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5725         for i in $(seq 1 $NSEQ); do
5726                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5727                 echo directIO rdwr round $i of $NSEQ
5728                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5729         done
5730
5731         rm -f $DIR/$tfile
5732 }
5733 run_test 78 "handle large O_DIRECT writes correctly ============"
5734
5735 test_79() { # bug 12743
5736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5737         wait_delete_completed
5738
5739         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5740         BKFREE=$(calc_osc_kbytes kbytesfree)
5741         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5742
5743         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5744         DFTOTAL=`echo $STRING | cut -d, -f1`
5745         DFUSED=`echo $STRING  | cut -d, -f2`
5746         DFAVAIL=`echo $STRING | cut -d, -f3`
5747         DFFREE=$(($DFTOTAL - $DFUSED))
5748
5749         ALLOWANCE=$((64 * $OSTCOUNT))
5750
5751         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5752            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5753                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5754         fi
5755         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5756            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5757                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5758         fi
5759         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5760            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5761                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5762         fi
5763 }
5764 run_test 79 "df report consistency check ======================="
5765
5766 test_80() { # bug 10718
5767         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5768         # relax strong synchronous semantics for slow backends like ZFS
5769         local soc="obdfilter.*.sync_on_lock_cancel"
5770         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5771         local hosts=
5772         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5773                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5774                           facet_active_host $host; done | sort -u)
5775                 do_nodes $hosts lctl set_param $soc=never
5776         fi
5777
5778         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
5779         sync; sleep 1; sync
5780         local BEFORE=`date +%s`
5781         cancel_lru_locks osc
5782         local AFTER=`date +%s`
5783         local DIFF=$((AFTER-BEFORE))
5784         if [ $DIFF -gt 1 ] ; then
5785                 error "elapsed for 1M@1T = $DIFF"
5786         fi
5787
5788         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
5789
5790         rm -f $DIR/$tfile
5791 }
5792 run_test 80 "Page eviction is equally fast at high offsets too  ===="
5793
5794 test_81a() { # LU-456
5795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5796         remote_ost_nodsh && skip "remote OST with nodsh" && return
5797         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5798         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
5799         do_facet ost1 lctl set_param fail_loc=0x80000228
5800
5801         # write should trigger a retry and success
5802         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5803         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5804         RC=$?
5805         if [ $RC -ne 0 ] ; then
5806                 error "write should success, but failed for $RC"
5807         fi
5808 }
5809 run_test 81a "OST should retry write when get -ENOSPC ==============="
5810
5811 test_81b() { # LU-456
5812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5813         remote_ost_nodsh && skip "remote OST with nodsh" && return
5814         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
5815         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
5816         do_facet ost1 lctl set_param fail_loc=0x228
5817
5818         # write should retry several times and return -ENOSPC finally
5819         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
5820         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
5821         RC=$?
5822         ENOSPC=28
5823         if [ $RC -ne $ENOSPC ] ; then
5824                 error "dd should fail for -ENOSPC, but succeed."
5825         fi
5826 }
5827 run_test 81b "OST should return -ENOSPC when retry still fails ======="
5828
5829 test_82() { # LU-1031
5830         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
5831         local gid1=14091995
5832         local gid2=16022000
5833
5834         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
5835         local MULTIPID1=$!
5836         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
5837         local MULTIPID2=$!
5838         kill -USR1 $MULTIPID2
5839         sleep 2
5840         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
5841                 error "First grouplock does not block second one"
5842         else
5843                 echo "Second grouplock blocks first one"
5844         fi
5845         kill -USR1 $MULTIPID1
5846         wait $MULTIPID1
5847         wait $MULTIPID2
5848 }
5849 run_test 82 "Basic grouplock test ==============================="
5850
5851 test_99a() {
5852         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
5853                 return
5854         test_mkdir -p $DIR/d99cvsroot
5855         chown $RUNAS_ID $DIR/d99cvsroot
5856         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
5857         cd $TMP
5858
5859         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
5860         cd $oldPWD
5861 }
5862 run_test 99a "cvs init ========================================="
5863
5864 test_99b() {
5865         [ -z "$(which cvs 2>/dev/null)" ] &&
5866                 skip_env "could not find cvs" && return
5867         [ ! -d $DIR/d99cvsroot ] && test_99a
5868         cd /etc/init.d
5869         # some versions of cvs import exit(1) when asked to import links or
5870         # files they can't read.  ignore those files.
5871         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
5872                         ! -perm +4 -printf '-I %f\n')
5873         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
5874                 d99reposname vtag rtag
5875 }
5876 run_test 99b "cvs import ======================================="
5877
5878 test_99c() {
5879         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5880         [ ! -d $DIR/d99cvsroot ] && test_99b
5881         cd $DIR
5882         test_mkdir -p $DIR/d99reposname
5883         chown $RUNAS_ID $DIR/d99reposname
5884         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
5885 }
5886 run_test 99c "cvs checkout ====================================="
5887
5888 test_99d() {
5889         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5890         [ ! -d $DIR/d99cvsroot ] && test_99c
5891         cd $DIR/d99reposname
5892         $RUNAS touch foo99
5893         $RUNAS cvs add -m 'addmsg' foo99
5894 }
5895 run_test 99d "cvs add =========================================="
5896
5897 test_99e() {
5898         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5899         [ ! -d $DIR/d99cvsroot ] && test_99c
5900         cd $DIR/d99reposname
5901         $RUNAS cvs update
5902 }
5903 run_test 99e "cvs update ======================================="
5904
5905 test_99f() {
5906         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
5907         [ ! -d $DIR/d99cvsroot ] && test_99d
5908         cd $DIR/d99reposname
5909         $RUNAS cvs commit -m 'nomsg' foo99
5910     rm -fr $DIR/d99cvsroot
5911 }
5912 run_test 99f "cvs commit ======================================="
5913
5914 test_100() {
5915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5916         [ "$NETTYPE" = tcp ] || \
5917                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
5918                         return ; }
5919
5920         remote_ost_nodsh && skip "remote OST with nodsh" && return
5921         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5922         remote_servers || \
5923                 { skip "useless for local single node setup" && return; }
5924
5925         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
5926                 [ "$PROT" != "tcp" ] && continue
5927                 RPORT=$(echo $REMOTE | cut -d: -f2)
5928                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
5929
5930                 rc=0
5931                 LPORT=`echo $LOCAL | cut -d: -f2`
5932                 if [ $LPORT -ge 1024 ]; then
5933                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
5934                         netstat -tna
5935                         error_exit "local: $LPORT > 1024, remote: $RPORT"
5936                 fi
5937         done
5938         [ "$rc" = 0 ] || error_exit "privileged port not found" )
5939 }
5940 run_test 100 "check local port using privileged port ==========="
5941
5942 function get_named_value()
5943 {
5944     local tag
5945
5946     tag=$1
5947     while read ;do
5948         line=$REPLY
5949         case $line in
5950         $tag*)
5951             echo $line | sed "s/^$tag[ ]*//"
5952             break
5953             ;;
5954         esac
5955     done
5956 }
5957
5958 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
5959                    awk '/^max_cached_mb/ { print $2 }')
5960
5961 cleanup_101a() {
5962         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
5963         trap 0
5964 }
5965
5966 test_101a() {
5967         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5968         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
5969         local s
5970         local discard
5971         local nreads=10000
5972         local cache_limit=32
5973
5974         $LCTL set_param -n osc.*-osc*.rpc_stats 0
5975         trap cleanup_101a EXIT
5976         $LCTL set_param -n llite.*.read_ahead_stats 0
5977         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
5978
5979         #
5980         # randomly read 10000 of 64K chunks from file 3x 32MB in size
5981         #
5982         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
5983         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
5984
5985         discard=0
5986         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
5987                 get_named_value 'read but discarded' | cut -d" " -f1); do
5988                         discard=$(($discard + $s))
5989         done
5990         cleanup_101a
5991
5992         if [[ $(($discard * 10)) -gt $nreads ]]; then
5993                 $LCTL get_param osc.*-osc*.rpc_stats
5994                 $LCTL get_param llite.*.read_ahead_stats
5995                 error "too many ($discard) discarded pages"
5996         fi
5997         rm -f $DIR/$tfile || true
5998 }
5999 run_test 101a "check read-ahead for random reads ================"
6000
6001 setup_test101bc() {
6002         test_mkdir -p $DIR/$tdir
6003         local STRIPE_SIZE=$1
6004         local FILE_LENGTH=$2
6005         STRIPE_OFFSET=0
6006
6007         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6008
6009         local list=$(comma_list $(osts_nodes))
6010         set_osd_param $list '' read_cache_enable 0
6011         set_osd_param $list '' writethrough_cache_enable 0
6012
6013         trap cleanup_test101bc EXIT
6014         # prepare the read-ahead file
6015         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6016
6017         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6018                                 count=$FILE_SIZE_MB 2> /dev/null
6019
6020 }
6021
6022 cleanup_test101bc() {
6023         trap 0
6024         rm -rf $DIR/$tdir
6025         rm -f $DIR/$tfile
6026
6027         local list=$(comma_list $(osts_nodes))
6028         set_osd_param $list '' read_cache_enable 1
6029         set_osd_param $list '' writethrough_cache_enable 1
6030 }
6031
6032 calc_total() {
6033         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6034 }
6035
6036 ra_check_101() {
6037         local READ_SIZE=$1
6038         local STRIPE_SIZE=$2
6039         local FILE_LENGTH=$3
6040         local RA_INC=1048576
6041         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6042         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6043                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6044         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6045                         get_named_value 'read but discarded' |
6046                         cut -d" " -f1 | calc_total)
6047         if [[ $DISCARD -gt $discard_limit ]]; then
6048                 $LCTL get_param llite.*.read_ahead_stats
6049                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6050         else
6051                 echo "Read-ahead success for size ${READ_SIZE}"
6052         fi
6053 }
6054
6055 test_101b() {
6056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6057         [[ $OSTCOUNT -lt 2 ]] &&
6058                 skip_env "skipping stride IO stride-ahead test" && return
6059         local STRIPE_SIZE=1048576
6060         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6061         if [ $SLOW == "yes" ]; then
6062                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6063         else
6064                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6065         fi
6066
6067         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6068
6069         # prepare the read-ahead file
6070         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6071         cancel_lru_locks osc
6072         for BIDX in 2 4 8 16 32 64 128 256
6073         do
6074                 local BSIZE=$((BIDX*4096))
6075                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6076                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6077                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6078                 $LCTL set_param -n llite.*.read_ahead_stats 0
6079                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6080                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6081                 cancel_lru_locks osc
6082                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6083         done
6084         cleanup_test101bc
6085         true
6086 }
6087 run_test 101b "check stride-io mode read-ahead ================="
6088
6089 test_101c() {
6090         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6091         local STRIPE_SIZE=1048576
6092         local FILE_LENGTH=$((STRIPE_SIZE*100))
6093         local nreads=10000
6094         local osc_rpc_stats
6095
6096         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6097
6098         cancel_lru_locks osc
6099         $LCTL set_param osc.*.rpc_stats 0
6100         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6101         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6102                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6103                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6104                 local size
6105
6106                 if [ $lines -le 20 ]; then
6107                         continue
6108                 fi
6109                 for size in 1 2 4 8; do
6110                         local rpc=$(echo "$stats" |
6111                                     awk '($1 == "'$size':") {print $2; exit; }')
6112                         [ $rpc != 0 ] &&
6113                                 error "Small $((size*4))k read IO $rpc !"
6114                 done
6115                 echo "$osc_rpc_stats check passed!"
6116         done
6117         cleanup_test101bc
6118         true
6119 }
6120 run_test 101c "check stripe_size aligned read-ahead ================="
6121
6122 set_read_ahead() {
6123         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6124         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6125 }
6126
6127 test_101d() {
6128         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6129         local file=$DIR/$tfile
6130         local sz_MB=${FILESIZE_101d:-500}
6131         local ra_MB=${READAHEAD_MB:-40}
6132
6133         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6134         [ $free_MB -lt $sz_MB ] &&
6135                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6136
6137         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6138         $SETSTRIPE -c -1 $file || error "setstripe failed"
6139
6140         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6141         echo Cancel LRU locks on lustre client to flush the client cache
6142         cancel_lru_locks osc
6143
6144         echo Disable read-ahead
6145         local old_READAHEAD=$(set_read_ahead 0)
6146
6147         echo Reading the test file $file with read-ahead disabled
6148         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6149
6150         echo Cancel LRU locks on lustre client to flush the client cache
6151         cancel_lru_locks osc
6152         echo Enable read-ahead with ${ra_MB}MB
6153         set_read_ahead $ra_MB
6154
6155         echo Reading the test file $file with read-ahead enabled
6156         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6157
6158         echo "read-ahead disabled time read $raOFF"
6159         echo "read-ahead enabled  time read $raON"
6160
6161         set_read_ahead $old_READAHEAD
6162         rm -f $file
6163         wait_delete_completed
6164
6165         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6166                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6167 }
6168 run_test 101d "file read with and without read-ahead enabled"
6169
6170 test_101e() {
6171         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6172         local file=$DIR/$tfile
6173         local size_KB=500  #KB
6174         local count=100
6175         local bsize=1024
6176
6177         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6178         local need_KB=$((count * size_KB))
6179         [[ $free_KB -le $need_KB ]] &&
6180                 skip_env "Need free space $need_KB, have $free_KB" && return
6181
6182         echo "Creating $count ${size_KB}K test files"
6183         for ((i = 0; i < $count; i++)); do
6184                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6185         done
6186
6187         echo "Cancel LRU locks on lustre client to flush the client cache"
6188         cancel_lru_locks osc
6189
6190         echo "Reset readahead stats"
6191         $LCTL set_param -n llite.*.read_ahead_stats 0
6192
6193         for ((i = 0; i < $count; i++)); do
6194                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6195         done
6196
6197         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6198                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6199
6200         for ((i = 0; i < $count; i++)); do
6201                 rm -rf $file.$i 2>/dev/null
6202         done
6203
6204         #10000 means 20% reads are missing in readahead
6205         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6206 }
6207 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6208
6209 cleanup_test101f() {
6210     trap 0
6211     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6212     rm -rf $DIR/$tfile 2>/dev/null
6213 }
6214
6215 test_101f() {
6216         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6217     local file=$DIR/$tfile
6218     local nreads=1000
6219
6220     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6221     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6222     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6223     trap cleanup_test101f EXIT
6224
6225     echo Cancel LRU locks on lustre client to flush the client cache
6226     cancel_lru_locks osc
6227
6228     echo Reset readahead stats
6229     $LCTL set_param -n llite.*.read_ahead_stats 0
6230     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6231     # readahead should read in 2M file on second read, so only miss
6232     # 2 pages.
6233     echo Random 4K reads on 2M file for 1000 times
6234     $READS -f $file -s 2097152 -b 4096 -n $nreads
6235
6236     echo checking missing pages
6237     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6238           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6239
6240     [ $miss -lt 3 ] || error "misses too much pages!"
6241     cleanup_test101f
6242 }
6243 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6244
6245 setup_test102() {
6246         test_mkdir -p $DIR/$tdir
6247         chown $RUNAS_ID $DIR/$tdir
6248         STRIPE_SIZE=65536
6249         STRIPE_OFFSET=1
6250         STRIPE_COUNT=$OSTCOUNT
6251         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6252
6253         trap cleanup_test102 EXIT
6254         cd $DIR
6255         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6256         cd $DIR/$tdir
6257         for num in 1 2 3 4; do
6258                 for count in $(seq 1 $STRIPE_COUNT); do
6259                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6260                                 local size=`expr $STRIPE_SIZE \* $num`
6261                                 local file=file"$num-$idx-$count"
6262                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6263                         done
6264                 done
6265         done
6266
6267         cd $DIR
6268         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6269 }
6270
6271 cleanup_test102() {
6272         trap 0
6273         rm -f $TMP/f102.tar
6274         rm -rf $DIR/d0.sanity/d102
6275 }
6276
6277 test_102a() {
6278         local testfile=$DIR/xattr_testfile
6279
6280         touch $testfile
6281
6282         [ "$UID" != 0 ] && skip_env "must run as root" && return
6283         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6284                 skip_env "must have user_xattr" && return
6285
6286         [ -z "$(which setfattr 2>/dev/null)" ] &&
6287                 skip_env "could not find setfattr" && return
6288
6289         echo "set/get xattr..."
6290         setfattr -n trusted.name1 -v value1 $testfile ||
6291                 error "setfattr -n trusted.name1=value1 $testfile failed"
6292         getfattr -n trusted.name1 $testfile 2> /dev/null |
6293           grep "trusted.name1=.value1" ||
6294                 error "$testfile missing trusted.name1=value1"
6295
6296         setfattr -n user.author1 -v author1 $testfile ||
6297                 error "setfattr -n user.author1=author1 $testfile failed"
6298         getfattr -n user.author1 $testfile 2> /dev/null |
6299           grep "user.author1=.author1" ||
6300                 error "$testfile missing trusted.author1=author1"
6301
6302         echo "listxattr..."
6303         setfattr -n trusted.name2 -v value2 $testfile ||
6304                 error "$testfile unable to set trusted.name2"
6305         setfattr -n trusted.name3 -v value3 $testfile ||
6306                 error "$testfile unable to set trusted.name3"
6307         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6308             grep "trusted.name" | wc -l) -eq 3 ] ||
6309                 error "$testfile missing 3 trusted.name xattrs"
6310
6311         setfattr -n user.author2 -v author2 $testfile ||
6312                 error "$testfile unable to set user.author2"
6313         setfattr -n user.author3 -v author3 $testfile ||
6314                 error "$testfile unable to set user.author3"
6315         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6316             grep "user.author" | wc -l) -eq 3 ] ||
6317                 error "$testfile missing 3 user.author xattrs"
6318
6319         echo "remove xattr..."
6320         setfattr -x trusted.name1 $testfile ||
6321                 error "$testfile error deleting trusted.name1"
6322         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6323                 error "$testfile did not delete trusted.name1 xattr"
6324
6325         setfattr -x user.author1 $testfile ||
6326                 error "$testfile error deleting user.author1"
6327         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6328                 error "$testfile did not delete trusted.name1 xattr"
6329
6330         # b10667: setting lustre special xattr be silently discarded
6331         echo "set lustre special xattr ..."
6332         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6333                 error "$testfile allowed setting trusted.lov"
6334 }
6335 run_test 102a "user xattr test =================================="
6336
6337 test_102b() {
6338         [ -z "$(which setfattr 2>/dev/null)" ] &&
6339                 skip_env "could not find setfattr" && return
6340
6341         # b10930: get/set/list trusted.lov xattr
6342         echo "get/set/list trusted.lov xattr ..."
6343         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6344         local testfile=$DIR/$tfile
6345         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6346                 error "setstripe failed"
6347         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6348                 error "getstripe failed"
6349         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6350                 error "can't get trusted.lov from $testfile"
6351
6352         local testfile2=${testfile}2
6353         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6354                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6355
6356         $MCREATE $testfile2
6357         setfattr -n trusted.lov -v $value $testfile2
6358         local stripe_size=$($GETSTRIPE -S $testfile2)
6359         local stripe_count=$($GETSTRIPE -c $testfile2)
6360         [[ $stripe_size -eq 65536 ]] ||
6361                 error "stripe size $stripe_size != 65536"
6362         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6363                 error "stripe count $stripe_count != $STRIPECOUNT"
6364         rm -f $DIR/$tfile
6365 }
6366 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6367
6368 test_102c() {
6369         [ -z "$(which setfattr 2>/dev/null)" ] &&
6370                 skip_env "could not find setfattr" && return
6371
6372         # b10930: get/set/list lustre.lov xattr
6373         echo "get/set/list lustre.lov xattr ..."
6374         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6375         test_mkdir -p $DIR/$tdir
6376         chown $RUNAS_ID $DIR/$tdir
6377         local testfile=$DIR/$tdir/$tfile
6378         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6379                 error "setstripe failed"
6380         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6381                 error "getstripe failed"
6382         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6383         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6384
6385         local testfile2=${testfile}2
6386         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6387                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6388
6389         $RUNAS $MCREATE $testfile2
6390         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6391         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6392         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6393         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6394         [ $stripe_count -eq $STRIPECOUNT ] ||
6395                 error "stripe count $stripe_count != $STRIPECOUNT"
6396 }
6397 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6398
6399 compare_stripe_info1() {
6400         local stripe_index_all_zero=true
6401
6402         for num in 1 2 3 4; do
6403                 for count in $(seq 1 $STRIPE_COUNT); do
6404                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6405                                 local size=$((STRIPE_SIZE * num))
6406                                 local file=file"$num-$offset-$count"
6407                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6408                                 [[ $stripe_size -ne $size ]] &&
6409                                     error "$file: size $stripe_size != $size"
6410                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6411                                 # allow fewer stripes to be created, ORI-601
6412                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6413                                     error "$file: count $stripe_count != $count"
6414                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6415                                 [[ $stripe_index -ne 0 ]] &&
6416                                         stripe_index_all_zero=false
6417                         done
6418                 done
6419         done
6420         $stripe_index_all_zero &&
6421                 error "all files are being extracted starting from OST index 0"
6422         return 0
6423 }
6424
6425 find_lustre_tar() {
6426         [ -n "$(which tar 2>/dev/null)" ] &&
6427                 strings $(which tar) | grep -q "lustre" && echo tar
6428 }
6429
6430 test_102d() {
6431         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6432         # b10930: tar test for trusted.lov xattr
6433         TAR=$(find_lustre_tar)
6434         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6435         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6436         setup_test102
6437         test_mkdir -p $DIR/d102d
6438         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6439         cd $DIR/d102d/$tdir
6440         compare_stripe_info1
6441 }
6442 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6443
6444 test_102f() {
6445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6446         # b10930: tar test for trusted.lov xattr
6447         TAR=$(find_lustre_tar)
6448         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6449         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6450         setup_test102
6451         test_mkdir -p $DIR/d102f
6452         cd $DIR
6453         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6454         cd $DIR/d102f/$tdir
6455         compare_stripe_info1
6456 }
6457 run_test 102f "tar copy files, not keep osts ==========="
6458
6459 grow_xattr() {
6460         local xsize=${1:-1024}  # in bytes
6461         local file=$DIR/$tfile
6462
6463         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6464                 skip "must have user_xattr" && return 0
6465         [ -z "$(which setfattr 2>/dev/null)" ] &&
6466                 skip_env "could not find setfattr" && return 0
6467         [ -z "$(which getfattr 2>/dev/null)" ] &&
6468                 skip_env "could not find getfattr" && return 0
6469
6470         touch $file
6471
6472         local value="$(generate_string $xsize)"
6473
6474         local xbig=trusted.big
6475         log "save $xbig on $file"
6476         setfattr -n $xbig -v $value $file ||
6477                 error "saving $xbig on $file failed"
6478
6479         local orig=$(get_xattr_value $xbig $file)
6480         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6481
6482         local xsml=trusted.sml
6483         log "save $xsml on $file"
6484         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6485
6486         local new=$(get_xattr_value $xbig $file)
6487         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6488
6489         log "grow $xsml on $file"
6490         setfattr -n $xsml -v "$value" $file ||
6491                 error "growing $xsml on $file failed"
6492
6493         new=$(get_xattr_value $xbig $file)
6494         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6495         log "$xbig still valid after growing $xsml"
6496
6497         rm -f $file
6498 }
6499
6500 test_102h() { # bug 15777
6501         grow_xattr 1024
6502 }
6503 run_test 102h "grow xattr from inside inode to external block"
6504
6505 test_102ha() {
6506         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6507         grow_xattr $(max_xattr_size)
6508 }
6509 run_test 102ha "grow xattr from inside inode to external inode"
6510
6511 test_102i() { # bug 17038
6512         [ -z "$(which getfattr 2>/dev/null)" ] &&
6513                 skip "could not find getfattr" && return
6514         touch $DIR/$tfile
6515         ln -s $DIR/$tfile $DIR/${tfile}link
6516         getfattr -n trusted.lov $DIR/$tfile ||
6517                 error "lgetxattr on $DIR/$tfile failed"
6518         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6519                 grep -i "no such attr" ||
6520                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6521         rm -f $DIR/$tfile $DIR/${tfile}link
6522 }
6523 run_test 102i "lgetxattr test on symbolic link ============"
6524
6525 test_102j() {
6526         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6527         TAR=$(find_lustre_tar)
6528         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6529         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6530         setup_test102 "$RUNAS"
6531         test_mkdir -p $DIR/d102j
6532         chown $RUNAS_ID $DIR/d102j
6533         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6534         cd $DIR/d102j/$tdir
6535         compare_stripe_info1 "$RUNAS"
6536 }
6537 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6538
6539 test_102k() {
6540         [ -z "$(which setfattr 2>/dev/null)" ] &&
6541                 skip "could not find setfattr" && return
6542         touch $DIR/$tfile
6543         # b22187 just check that does not crash for regular file.
6544         setfattr -n trusted.lov $DIR/$tfile
6545         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6546         local test_kdir=$DIR/d102k
6547         test_mkdir $test_kdir
6548         local default_size=`$GETSTRIPE -S $test_kdir`
6549         local default_count=`$GETSTRIPE -c $test_kdir`
6550         local default_offset=`$GETSTRIPE -i $test_kdir`
6551         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6552                 error 'dir setstripe failed'
6553         setfattr -n trusted.lov $test_kdir
6554         local stripe_size=`$GETSTRIPE -S $test_kdir`
6555         local stripe_count=`$GETSTRIPE -c $test_kdir`
6556         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6557         [ $stripe_size -eq $default_size ] ||
6558                 error "stripe size $stripe_size != $default_size"
6559         [ $stripe_count -eq $default_count ] ||
6560                 error "stripe count $stripe_count != $default_count"
6561         [ $stripe_offset -eq $default_offset ] ||
6562                 error "stripe offset $stripe_offset != $default_offset"
6563         rm -rf $DIR/$tfile $test_kdir
6564 }
6565 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6566
6567 test_102l() {
6568         [ -z "$(which getfattr 2>/dev/null)" ] &&
6569                 skip "could not find getfattr" && return
6570
6571         # LU-532 trusted. xattr is invisible to non-root
6572         local testfile=$DIR/$tfile
6573
6574         touch $testfile
6575
6576         echo "listxattr as user..."
6577         chown $RUNAS_ID $testfile
6578         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6579             grep -q "trusted" &&
6580                 error "$testfile trusted xattrs are user visible"
6581
6582         return 0;
6583 }
6584 run_test 102l "listxattr size test =================================="
6585
6586 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6587         local path=$DIR/$tfile
6588         touch $path
6589
6590         listxattr_size_check $path || error "listattr_size_check $path failed"
6591 }
6592 run_test 102m "Ensure listxattr fails on small bufffer ========"
6593
6594 cleanup_test102
6595
6596 getxattr() { # getxattr path name
6597         # Return the base64 encoding of the value of xattr name on path.
6598         local path=$1
6599         local name=$2
6600
6601         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6602         # file: $path
6603         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6604         #
6605         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6606
6607         getfattr --absolute-names --encoding=base64 --name=$name $path |
6608                 awk -F= -v name=$name '$1 == name {
6609                         print substr($0, index($0, "=") + 1);
6610         }'
6611 }
6612
6613 test_102n() { # LU-4101 mdt: protect internal xattrs
6614         local file0=$DIR/$tfile.0
6615         local file1=$DIR/$tfile.1
6616         local xattr0=$TMP/$tfile.0
6617         local xattr1=$TMP/$tfile.1
6618         local name
6619         local value
6620
6621         [ -z "$(which setfattr 2>/dev/null)" ] &&
6622                 skip "could not find setfattr" && return
6623
6624         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6625         then
6626                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6627                 return
6628         fi
6629
6630         rm -rf $file0 $file1 $xattr0 $xattr1
6631         touch $file0 $file1
6632
6633         # Get 'before' xattrs of $file1.
6634         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6635
6636         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6637                 # Try to copy xattr from $file0 to $file1.
6638                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6639
6640                 setfattr --name=trusted.$name --value="$value" $file1 ||
6641                         error "setxattr 'trusted.$name' failed"
6642
6643                 # Try to set a garbage xattr.
6644                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6645
6646                 setfattr --name=trusted.$name --value="$value" $file1 ||
6647                         error "setxattr 'trusted.$name' failed"
6648
6649                 # Try to remove the xattr from $file1. We don't care if this
6650                 # appears to succeed or fail, we just don't want there to be
6651                 # any changes or crashes.
6652                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6653         done
6654
6655         # Get 'after' xattrs of file1.
6656         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6657
6658         if ! diff $xattr0 $xattr1; then
6659                 error "before and after xattrs of '$file1' differ"
6660         fi
6661
6662         rm -rf $file0 $file1 $xattr0 $xattr1
6663
6664         return 0
6665 }
6666 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6667
6668 test_102p() { # LU-4703 setxattr did not check ownership
6669         local testfile=$DIR/$tfile
6670
6671         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6672                 skip "MDS needs to be at least 2.5.56" && return
6673
6674         touch $testfile
6675
6676         echo "setfacl as user..."
6677         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6678         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6679
6680         echo "setfattr as user..."
6681         setfacl -m "u:$RUNAS_ID:---" $testfile
6682         $RUNAS setfattr -x system.posix_acl_access $testfile
6683         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6684 }
6685 run_test 102p "check setxattr(2) correctly fails without permission"
6686
6687 run_acl_subtest()
6688 {
6689     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6690     return $?
6691 }
6692
6693 test_103 () {
6694         [ "$UID" != 0 ] && skip_env "must run as root" && return
6695         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6696                 skip "must have acl enabled" && return
6697         [ -z "$(which setfacl 2>/dev/null)" ] &&
6698                 skip_env "could not find setfacl" && return
6699         $GSS && skip "could not run under gss" && return
6700
6701         declare -a identity_old
6702
6703         for num in $(seq $MDSCOUNT); do
6704                 switch_identity $num true || identity_old[$num]=$?
6705         done
6706
6707         SAVE_UMASK=$(umask)
6708         umask 0022
6709         cd $DIR
6710
6711         echo "performing cp ..."
6712         run_acl_subtest cp || error "run_acl_subtest cp failed"
6713         echo "performing getfacl-noacl..."
6714         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6715         echo "performing misc..."
6716         run_acl_subtest misc || error  "misc test failed"
6717         echo "performing permissions..."
6718         run_acl_subtest permissions || error "permissions failed"
6719         echo "performing setfacl..."
6720         run_acl_subtest setfacl || error  "setfacl test failed"
6721
6722         # inheritance test got from HP
6723         echo "performing inheritance..."
6724         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6725         chmod +x make-tree || error "chmod +x failed"
6726         run_acl_subtest inheritance || error "inheritance test failed"
6727         rm -f make-tree
6728
6729         echo "LU-974 ignore umask when acl is enabled..."
6730         run_acl_subtest 974 || error "LU-974 umask test failed"
6731         if [ $MDSCOUNT -ge 2 ]; then
6732                 run_acl_subtest 974_remote ||
6733                         error "LU-974 umask test failed under remote dir"
6734         fi
6735
6736         echo "LU-2561 newly created file is same size as directory..."
6737         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6738                 run_acl_subtest 2561 || error "LU-2561 test failed"
6739         else
6740                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
6741         fi
6742
6743         run_acl_subtest 4924 || error "LU-4924 test failed"
6744
6745         cd $SAVE_PWD
6746         umask $SAVE_UMASK
6747
6748         for num in $(seq $MDSCOUNT); do
6749                 if [ "${identity_old[$num]}" = 1 ]; then
6750                         switch_identity $num false || identity_old[$num]=$?
6751                 fi
6752         done
6753 }
6754 run_test 103 "acl test ========================================="
6755
6756 test_104a() {
6757         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6758         touch $DIR/$tfile
6759         lfs df || error "lfs df failed"
6760         lfs df -ih || error "lfs df -ih failed"
6761         lfs df -h $DIR || error "lfs df -h $DIR failed"
6762         lfs df -i $DIR || error "lfs df -i $DIR failed"
6763         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
6764         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
6765
6766         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
6767         lctl --device %$OSC deactivate
6768         lfs df || error "lfs df with deactivated OSC failed"
6769         lctl --device %$OSC activate
6770         # wait the osc back to normal
6771         wait_osc_import_state client ost FULL
6772
6773         lfs df || error "lfs df with reactivated OSC failed"
6774         rm -f $DIR/$tfile
6775 }
6776 run_test 104a "lfs df [-ih] [path] test ========================="
6777
6778 test_104b() {
6779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6780         [ $RUNAS_ID -eq $UID ] &&
6781                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6782         chmod 666 /dev/obd
6783         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
6784                         grep "Permission denied" | wc -l)))
6785         if [ $denied_cnt -ne 0 ]; then
6786                 error "lfs check servers test failed"
6787         fi
6788 }
6789 run_test 104b "$RUNAS lfs check servers test ===================="
6790
6791 test_105a() {
6792         # doesn't work on 2.4 kernels
6793         touch $DIR/$tfile
6794         if $(flock_is_enabled); then
6795                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
6796         else
6797                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
6798         fi
6799         rm -f $DIR/$tfile
6800 }
6801 run_test 105a "flock when mounted without -o flock test ========"
6802
6803 test_105b() {
6804         touch $DIR/$tfile
6805         if $(flock_is_enabled); then
6806                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
6807         else
6808                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
6809         fi
6810         rm -f $DIR/$tfile
6811 }
6812 run_test 105b "fcntl when mounted without -o flock test ========"
6813
6814 test_105c() {
6815         touch $DIR/$tfile
6816         if $(flock_is_enabled); then
6817                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
6818         else
6819                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
6820         fi
6821         rm -f $DIR/$tfile
6822 }
6823 run_test 105c "lockf when mounted without -o flock test ========"
6824
6825 test_105d() { # bug 15924
6826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6827         test_mkdir -p $DIR/$tdir
6828         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6829         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
6830         $LCTL set_param fail_loc=0x80000315
6831         flocks_test 2 $DIR/$tdir
6832 }
6833 run_test 105d "flock race (should not freeze) ========"
6834
6835 test_105e() { # bug 22660 && 22040
6836         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
6837         touch $DIR/$tfile
6838         flocks_test 3 $DIR/$tfile
6839 }
6840 run_test 105e "Two conflicting flocks from same process ======="
6841
6842 test_106() { #bug 10921
6843         test_mkdir -p $DIR/$tdir
6844         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
6845         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
6846 }
6847 run_test 106 "attempt exec of dir followed by chown of that dir"
6848
6849 test_107() {
6850         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6851         CDIR=`pwd`
6852         cd $DIR
6853
6854         local file=core
6855         rm -f $file
6856
6857         local save_pattern=$(sysctl -n kernel.core_pattern)
6858         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
6859         sysctl -w kernel.core_pattern=$file
6860         sysctl -w kernel.core_uses_pid=0
6861
6862         ulimit -c unlimited
6863         sleep 60 &
6864         SLEEPPID=$!
6865
6866         sleep 1
6867
6868         kill -s 11 $SLEEPPID
6869         wait $SLEEPPID
6870         if [ -e $file ]; then
6871                 size=`stat -c%s $file`
6872                 [ $size -eq 0 ] && error "Fail to create core file $file"
6873         else
6874                 error "Fail to create core file $file"
6875         fi
6876         rm -f $file
6877         sysctl -w kernel.core_pattern=$save_pattern
6878         sysctl -w kernel.core_uses_pid=$save_uses_pid
6879         cd $CDIR
6880 }
6881 run_test 107 "Coredump on SIG"
6882
6883 test_110() {
6884         test_mkdir -p $DIR/$tdir
6885         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
6886                 error "mkdir with 255 char failed"
6887         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
6888                 error "mkdir with 256 char should fail, but did not"
6889         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
6890                 error "create with 255 char failed"
6891         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
6892                 error "create with 256 char should fail, but did not"
6893
6894         ls -l $DIR/$tdir
6895         rm -rf $DIR/$tdir
6896 }
6897 run_test 110 "filename length checking"
6898
6899 test_115() {
6900         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6901         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
6902                 tail -1 | cut -c11-20)
6903         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
6904         echo "Starting with $OSTIO_pre threads"
6905
6906         NUMTEST=20000
6907         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
6908         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
6909         echo "$NUMTEST creates/unlinks"
6910         test_mkdir -p $DIR/$tdir
6911         createmany -o $DIR/$tdir/$tfile $NUMTEST
6912         unlinkmany $DIR/$tdir/$tfile $NUMTEST
6913
6914         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
6915                 tail -1 | cut -c11-20)
6916
6917         # don't return an error
6918         [ $OSTIO_post == $OSTIO_pre ] && echo \
6919             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
6920             echo "This may be fine, depending on what ran before this test" &&
6921             echo "and how fast this system is." && return
6922
6923         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
6924 }
6925 run_test 115 "verify dynamic thread creation===================="
6926
6927 free_min_max () {
6928         wait_delete_completed
6929         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
6930         echo OST kbytes available: ${AVAIL[@]}
6931         MAXI=0; MAXV=${AVAIL[0]}
6932         MINI=0; MINV=${AVAIL[0]}
6933         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
6934                 #echo OST $i: ${AVAIL[i]}kb
6935                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
6936                         MAXV=${AVAIL[i]}; MAXI=$i
6937                 fi
6938                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
6939                         MINV=${AVAIL[i]}; MINI=$i
6940                 fi
6941         done
6942         echo Min free space: OST $MINI: $MINV
6943         echo Max free space: OST $MAXI: $MAXV
6944 }
6945
6946 test_116a() { # was previously test_116()
6947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6948         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
6949
6950         echo -n "Free space priority "
6951         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
6952                 head -n1
6953         declare -a AVAIL
6954         free_min_max
6955
6956         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
6957         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
6958                 && return
6959         trap simple_cleanup_common EXIT
6960
6961
6962         # Check if we need to generate uneven OSTs
6963         test_mkdir -p $DIR/$tdir/OST${MINI}
6964         local FILL=$(($MINV / 4))
6965         local DIFF=$(($MAXV - $MINV))
6966         local DIFF2=$(($DIFF * 100 / $MINV))
6967
6968         local threshold=$(do_facet $SINGLEMDS \
6969                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
6970         threshold=${threshold%%%}
6971         echo -n "Check for uneven OSTs: "
6972         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
6973
6974         if [[ $DIFF2 -gt $threshold ]]; then
6975                 echo "ok"
6976                 echo "Don't need to fill OST$MINI"
6977         else
6978                 # generate uneven OSTs. Write 2% over the QOS threshold value
6979                 echo "no"
6980                 DIFF=$(($threshold - $DIFF2 + 2))
6981                 DIFF2=$(( ($MINV * $DIFF)/100 ))
6982                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
6983                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
6984                         error "setstripe failed"
6985                 DIFF=$(($DIFF2 / 2048))
6986                 i=0
6987                 while [ $i -lt $DIFF ]; do
6988                         i=$(($i + 1))
6989                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
6990                                 bs=2M count=1 2>/dev/null
6991                         echo -n .
6992                 done
6993                 echo .
6994                 sync
6995                 sleep_maxage
6996                 free_min_max
6997         fi
6998
6999         DIFF=$(($MAXV - $MINV))
7000         DIFF2=$(($DIFF * 100 / $MINV))
7001         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7002         if [[ $DIFF2 -gt $threshold ]]; then
7003                 echo "ok"
7004         else
7005                 echo "failed - QOS mode won't be used"
7006                 skip "QOS imbalance criteria not met"
7007                 simple_cleanup_common
7008                 return
7009         fi
7010
7011         MINI1=$MINI; MINV1=$MINV
7012         MAXI1=$MAXI; MAXV1=$MAXV
7013
7014         # now fill using QOS
7015         $SETSTRIPE -c 1 $DIR/$tdir
7016         FILL=$(($FILL / 200))
7017         if [ $FILL -gt 600 ]; then
7018                 FILL=600
7019         fi
7020         echo "writing $FILL files to QOS-assigned OSTs"
7021         i=0
7022         while [ $i -lt $FILL ]; do
7023                 i=$((i + 1))
7024                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7025                         count=1 2>/dev/null
7026                 echo -n .
7027         done
7028         echo "wrote $i 200k files"
7029         sync
7030         sleep_maxage
7031
7032         echo "Note: free space may not be updated, so measurements might be off"
7033         free_min_max
7034         DIFF2=$(($MAXV - $MINV))
7035         echo "free space delta: orig $DIFF final $DIFF2"
7036         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7037         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7038         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7039         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7040         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7041         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7042         [ $DIFF -gt 0 ] &&
7043                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7044
7045         # Figure out which files were written where
7046         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7047                   awk '/'$MINI1': / {print $2; exit}')
7048         echo $UUID
7049         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7050         echo "$MINC files created on smaller OST $MINI1"
7051         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7052                   awk '/'$MAXI1': / {print $2; exit}')
7053         echo $UUID
7054         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7055         echo "$MAXC files created on larger OST $MAXI1"
7056         FILL=$(($MAXC * 100 / $MINC - 100))
7057         [[ $MINC -gt 0 ]] &&
7058                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7059         [[ $MAXC -gt $MINC ]] ||
7060                 error_ignore LU-9 "stripe QOS didn't balance free space"
7061         simple_cleanup_common
7062 }
7063 run_test 116a "stripe QOS: free space balance ==================="
7064
7065 test_116b() { # LU-2093
7066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7067         [ -z "$($LCTL get_param -n lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr |
7068                 head -1 2>/dev/null)" ] && skip "no QOS" && return
7069 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7070         local old_rr
7071         old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7072                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7073         do_facet $SINGLEMDS lctl set_param \
7074                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7075         mkdir -p $DIR/$tdir
7076         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7077         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7078         do_facet $SINGLEMDS lctl set_param fail_loc=0
7079         rm -rf $DIR/$tdir
7080         do_facet $SINGLEMDS lctl set_param \
7081                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7082 }
7083 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7084
7085 test_117() # bug 10891
7086 {
7087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7088         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7089         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7090         lctl set_param fail_loc=0x21e
7091         > $DIR/$tfile || error "truncate failed"
7092         lctl set_param fail_loc=0
7093         echo "Truncate succeeded."
7094         rm -f $DIR/$tfile
7095 }
7096 run_test 117 "verify osd extend =========="
7097
7098 NO_SLOW_RESENDCOUNT=4
7099 export OLD_RESENDCOUNT=""
7100 set_resend_count () {
7101         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7102         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7103         lctl set_param -n $PROC_RESENDCOUNT $1
7104         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7105 }
7106
7107 # for reduce test_118* time (b=14842)
7108 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7109
7110 # Reset async IO behavior after error case
7111 reset_async() {
7112         FILE=$DIR/reset_async
7113
7114         # Ensure all OSCs are cleared
7115         $SETSTRIPE -c -1 $FILE
7116         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7117         sync
7118         rm $FILE
7119 }
7120
7121 test_118a() #bug 11710
7122 {
7123         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7124         reset_async
7125
7126         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7127         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7128         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7129
7130         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7131                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7132                 return 1;
7133         fi
7134         rm -f $DIR/$tfile
7135 }
7136 run_test 118a "verify O_SYNC works =========="
7137
7138 test_118b()
7139 {
7140         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7141         remote_ost_nodsh && skip "remote OST with nodsh" && return
7142
7143         reset_async
7144
7145         #define OBD_FAIL_OST_ENOENT 0x217
7146         set_nodes_failloc "$(osts_nodes)" 0x217
7147         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7148         RC=$?
7149         set_nodes_failloc "$(osts_nodes)" 0
7150         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7151         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7152                     grep -c writeback)
7153
7154         if [[ $RC -eq 0 ]]; then
7155                 error "Must return error due to dropped pages, rc=$RC"
7156                 return 1;
7157         fi
7158
7159         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7160                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7161                 return 1;
7162         fi
7163
7164         echo "Dirty pages not leaked on ENOENT"
7165
7166         # Due to the above error the OSC will issue all RPCs syncronously
7167         # until a subsequent RPC completes successfully without error.
7168         $MULTIOP $DIR/$tfile Ow4096yc
7169         rm -f $DIR/$tfile
7170
7171         return 0
7172 }
7173 run_test 118b "Reclaim dirty pages on fatal error =========="
7174
7175 test_118c()
7176 {
7177         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7178
7179         # for 118c, restore the original resend count, LU-1940
7180         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7181                                 set_resend_count $OLD_RESENDCOUNT
7182         remote_ost_nodsh && skip "remote OST with nodsh" && return
7183
7184         reset_async
7185
7186         #define OBD_FAIL_OST_EROFS               0x216
7187         set_nodes_failloc "$(osts_nodes)" 0x216
7188
7189         # multiop should block due to fsync until pages are written
7190         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7191         MULTIPID=$!
7192         sleep 1
7193
7194         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7195                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7196         fi
7197
7198         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7199                     grep -c writeback)
7200         if [[ $WRITEBACK -eq 0 ]]; then
7201                 error "No page in writeback, writeback=$WRITEBACK"
7202         fi
7203
7204         set_nodes_failloc "$(osts_nodes)" 0
7205         wait $MULTIPID
7206         RC=$?
7207         if [[ $RC -ne 0 ]]; then
7208                 error "Multiop fsync failed, rc=$RC"
7209         fi
7210
7211         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7212         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7213                     grep -c writeback)
7214         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7215                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7216         fi
7217
7218         rm -f $DIR/$tfile
7219         echo "Dirty pages flushed via fsync on EROFS"
7220         return 0
7221 }
7222 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7223
7224 # continue to use small resend count to reduce test_118* time (b=14842)
7225 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7226
7227 test_118d()
7228 {
7229         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7230         remote_ost_nodsh && skip "remote OST with nodsh" && return
7231
7232         reset_async
7233
7234         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7235         set_nodes_failloc "$(osts_nodes)" 0x214
7236         # multiop should block due to fsync until pages are written
7237         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7238         MULTIPID=$!
7239         sleep 1
7240
7241         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7242                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7243         fi
7244
7245         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7246                     grep -c writeback)
7247         if [[ $WRITEBACK -eq 0 ]]; then
7248                 error "No page in writeback, writeback=$WRITEBACK"
7249         fi
7250
7251         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7252         set_nodes_failloc "$(osts_nodes)" 0
7253
7254         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7255         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7256                     grep -c writeback)
7257         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7258                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7259         fi
7260
7261         rm -f $DIR/$tfile
7262         echo "Dirty pages gaurenteed flushed via fsync"
7263         return 0
7264 }
7265 run_test 118d "Fsync validation inject a delay of the bulk =========="
7266
7267 test_118f() {
7268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7269         reset_async
7270
7271         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7272         lctl set_param fail_loc=0x8000040a
7273
7274         # Should simulate EINVAL error which is fatal
7275         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7276         RC=$?
7277         if [[ $RC -eq 0 ]]; then
7278                 error "Must return error due to dropped pages, rc=$RC"
7279         fi
7280
7281         lctl set_param fail_loc=0x0
7282
7283         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7284         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7285         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7286                     grep -c writeback)
7287         if [[ $LOCKED -ne 0 ]]; then
7288                 error "Locked pages remain in cache, locked=$LOCKED"
7289         fi
7290
7291         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7292                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7293         fi
7294
7295         rm -f $DIR/$tfile
7296         echo "No pages locked after fsync"
7297
7298         reset_async
7299         return 0
7300 }
7301 run_test 118f "Simulate unrecoverable OSC side error =========="
7302
7303 test_118g() {
7304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7305         reset_async
7306
7307         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7308         lctl set_param fail_loc=0x406
7309
7310         # simulate local -ENOMEM
7311         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7312         RC=$?
7313
7314         lctl set_param fail_loc=0
7315         if [[ $RC -eq 0 ]]; then
7316                 error "Must return error due to dropped pages, rc=$RC"
7317         fi
7318
7319         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7320         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7321         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7322                         grep -c writeback)
7323         if [[ $LOCKED -ne 0 ]]; then
7324                 error "Locked pages remain in cache, locked=$LOCKED"
7325         fi
7326
7327         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7328                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7329         fi
7330
7331         rm -f $DIR/$tfile
7332         echo "No pages locked after fsync"
7333
7334         reset_async
7335         return 0
7336 }
7337 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7338
7339 test_118h() {
7340         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7341         remote_ost_nodsh && skip "remote OST with nodsh" && return
7342
7343         reset_async
7344
7345         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7346         set_nodes_failloc "$(osts_nodes)" 0x20e
7347         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7348         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7349         RC=$?
7350
7351         set_nodes_failloc "$(osts_nodes)" 0
7352         if [[ $RC -eq 0 ]]; then
7353                 error "Must return error due to dropped pages, rc=$RC"
7354         fi
7355
7356         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7357         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7358         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7359                     grep -c writeback)
7360         if [[ $LOCKED -ne 0 ]]; then
7361                 error "Locked pages remain in cache, locked=$LOCKED"
7362         fi
7363
7364         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7365                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7366         fi
7367
7368         rm -f $DIR/$tfile
7369         echo "No pages locked after fsync"
7370
7371         return 0
7372 }
7373 run_test 118h "Verify timeout in handling recoverables errors  =========="
7374
7375 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7376
7377 test_118i() {
7378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7379         remote_ost_nodsh && skip "remote OST with nodsh" && return
7380
7381         reset_async
7382
7383         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7384         set_nodes_failloc "$(osts_nodes)" 0x20e
7385
7386         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7387         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7388         PID=$!
7389         sleep 5
7390         set_nodes_failloc "$(osts_nodes)" 0
7391
7392         wait $PID
7393         RC=$?
7394         if [[ $RC -ne 0 ]]; then
7395                 error "got error, but should be not, rc=$RC"
7396         fi
7397
7398         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7399         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7400         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7401         if [[ $LOCKED -ne 0 ]]; then
7402                 error "Locked pages remain in cache, locked=$LOCKED"
7403         fi
7404
7405         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7406                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7407         fi
7408
7409         rm -f $DIR/$tfile
7410         echo "No pages locked after fsync"
7411
7412         return 0
7413 }
7414 run_test 118i "Fix error before timeout in recoverable error  =========="
7415
7416 [ "$SLOW" = "no" ] && set_resend_count 4
7417
7418 test_118j() {
7419         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7420         remote_ost_nodsh && skip "remote OST with nodsh" && return
7421
7422         reset_async
7423
7424         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7425         set_nodes_failloc "$(osts_nodes)" 0x220
7426
7427         # return -EIO from OST
7428         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7429         RC=$?
7430         set_nodes_failloc "$(osts_nodes)" 0x0
7431         if [[ $RC -eq 0 ]]; then
7432                 error "Must return error due to dropped pages, rc=$RC"
7433         fi
7434
7435         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7436         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7437         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7438         if [[ $LOCKED -ne 0 ]]; then
7439                 error "Locked pages remain in cache, locked=$LOCKED"
7440         fi
7441
7442         # in recoverable error on OST we want resend and stay until it finished
7443         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7444                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7445         fi
7446
7447         rm -f $DIR/$tfile
7448         echo "No pages locked after fsync"
7449
7450         return 0
7451 }
7452 run_test 118j "Simulate unrecoverable OST side error =========="
7453
7454 test_118k()
7455 {
7456         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7457         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7458
7459         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7460         set_nodes_failloc "$(osts_nodes)" 0x20e
7461         test_mkdir -p $DIR/$tdir
7462
7463         for ((i=0;i<10;i++)); do
7464                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7465                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7466                 SLEEPPID=$!
7467                 sleep 0.500s
7468                 kill $SLEEPPID
7469                 wait $SLEEPPID
7470         done
7471
7472         set_nodes_failloc "$(osts_nodes)" 0
7473         rm -rf $DIR/$tdir
7474 }
7475 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7476
7477 test_118l()
7478 {
7479         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7480         # LU-646
7481         test_mkdir -p $DIR/$tdir
7482         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7483         rm -rf $DIR/$tdir
7484 }
7485 run_test 118l "fsync dir ========="
7486
7487 test_118m() # LU-3066
7488 {
7489         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7490         test_mkdir -p $DIR/$tdir
7491         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7492         rm -rf $DIR/$tdir
7493 }
7494 run_test 118m "fdatasync dir ========="
7495
7496 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7497
7498 test_119a() # bug 11737
7499 {
7500         BSIZE=$((512 * 1024))
7501         directio write $DIR/$tfile 0 1 $BSIZE
7502         # We ask to read two blocks, which is more than a file size.
7503         # directio will indicate an error when requested and actual
7504         # sizes aren't equeal (a normal situation in this case) and
7505         # print actual read amount.
7506         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7507         if [ "$NOB" != "$BSIZE" ]; then
7508                 error "read $NOB bytes instead of $BSIZE"
7509         fi
7510         rm -f $DIR/$tfile
7511 }
7512 run_test 119a "Short directIO read must return actual read amount"
7513
7514 test_119b() # bug 11737
7515 {
7516         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7517
7518         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7519         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7520         sync
7521         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7522                 error "direct read failed"
7523         rm -f $DIR/$tfile
7524 }
7525 run_test 119b "Sparse directIO read must return actual read amount"
7526
7527 test_119c() # bug 13099
7528 {
7529         BSIZE=1048576
7530         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7531         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7532         rm -f $DIR/$tfile
7533 }
7534 run_test 119c "Testing for direct read hitting hole"
7535
7536 test_119d() # bug 15950
7537 {
7538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7539         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7540         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7541         BSIZE=1048576
7542         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7543         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7544         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7545         lctl set_param fail_loc=0x40d
7546         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7547         pid_dio=$!
7548         sleep 1
7549         cat $DIR/$tfile > /dev/null &
7550         lctl set_param fail_loc=0
7551         pid_reads=$!
7552         wait $pid_dio
7553         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7554         sleep 2
7555         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7556         error "the read rpcs have not completed in 2s"
7557         rm -f $DIR/$tfile
7558         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7559 }
7560 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7561
7562 test_120a() {
7563         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7564         test_mkdir -p $DIR/$tdir
7565         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7566                skip "no early lock cancel on server" && return 0
7567
7568         lru_resize_disable mdc
7569         lru_resize_disable osc
7570         cancel_lru_locks mdc
7571         # asynchronous object destroy at MDT could cause bl ast to client
7572         cancel_lru_locks osc
7573
7574         stat $DIR/$tdir > /dev/null
7575         can1=$(do_facet $SINGLEMDS \
7576                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7577                awk '/ldlm_cancel/ {print $2}')
7578         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7579                awk '/ldlm_bl_callback/ {print $2}')
7580         test_mkdir -c1 $DIR/$tdir/d1
7581         can2=$(do_facet $SINGLEMDS \
7582                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7583                awk '/ldlm_cancel/ {print $2}')
7584         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7585                awk '/ldlm_bl_callback/ {print $2}')
7586         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7587         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7588         lru_resize_enable mdc
7589         lru_resize_enable osc
7590 }
7591 run_test 120a "Early Lock Cancel: mkdir test"
7592
7593 test_120b() {
7594         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7595         test_mkdir $DIR/$tdir
7596         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7597                skip "no early lock cancel on server" && return 0
7598         lru_resize_disable mdc
7599         lru_resize_disable osc
7600         cancel_lru_locks mdc
7601         stat $DIR/$tdir > /dev/null
7602         can1=$(do_facet $SINGLEMDS \
7603                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7604                awk '/ldlm_cancel/ {print $2}')
7605         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7606                awk '/ldlm_bl_callback/ {print $2}')
7607         touch $DIR/$tdir/f1
7608         can2=$(do_facet $SINGLEMDS \
7609                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7610                awk '/ldlm_cancel/ {print $2}')
7611         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7612                awk '/ldlm_bl_callback/ {print $2}')
7613         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7614         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7615         lru_resize_enable mdc
7616         lru_resize_enable osc
7617 }
7618 run_test 120b "Early Lock Cancel: create test"
7619
7620 test_120c() {
7621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7622         test_mkdir -c1 $DIR/$tdir
7623         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7624                skip "no early lock cancel on server" && return 0
7625         lru_resize_disable mdc
7626         lru_resize_disable osc
7627         test_mkdir -p -c1 $DIR/$tdir/d1
7628         test_mkdir -p -c1 $DIR/$tdir/d2
7629         touch $DIR/$tdir/d1/f1
7630         cancel_lru_locks mdc
7631         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7632         can1=$(do_facet $SINGLEMDS \
7633                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7634                awk '/ldlm_cancel/ {print $2}')
7635         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7636                awk '/ldlm_bl_callback/ {print $2}')
7637         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7638         can2=$(do_facet $SINGLEMDS \
7639                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7640                awk '/ldlm_cancel/ {print $2}')
7641         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7642                awk '/ldlm_bl_callback/ {print $2}')
7643         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7644         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7645         lru_resize_enable mdc
7646         lru_resize_enable osc
7647 }
7648 run_test 120c "Early Lock Cancel: link test"
7649
7650 test_120d() {
7651         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7652         test_mkdir -p -c1 $DIR/$tdir
7653         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7654                skip "no early lock cancel on server" && return 0
7655         lru_resize_disable mdc
7656         lru_resize_disable osc
7657         touch $DIR/$tdir
7658         cancel_lru_locks mdc
7659         stat $DIR/$tdir > /dev/null
7660         can1=$(do_facet $SINGLEMDS \
7661                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7662                awk '/ldlm_cancel/ {print $2}')
7663         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7664                awk '/ldlm_bl_callback/ {print $2}')
7665         chmod a+x $DIR/$tdir
7666         can2=$(do_facet $SINGLEMDS \
7667                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7668                awk '/ldlm_cancel/ {print $2}')
7669         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7670                awk '/ldlm_bl_callback/ {print $2}')
7671         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7672         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7673         lru_resize_enable mdc
7674         lru_resize_enable osc
7675 }
7676 run_test 120d "Early Lock Cancel: setattr test"
7677
7678 test_120e() {
7679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7680         test_mkdir -p -c1 $DIR/$tdir
7681         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7682                skip "no early lock cancel on server" && return 0
7683         lru_resize_disable mdc
7684         lru_resize_disable osc
7685         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7686         cancel_lru_locks mdc
7687         cancel_lru_locks osc
7688         dd if=$DIR/$tdir/f1 of=/dev/null
7689         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7690         # XXX client can not do early lock cancel of OST lock
7691         # during unlink (LU-4206), so cancel osc lock now.
7692         cancel_lru_locks osc
7693         can1=$(do_facet $SINGLEMDS \
7694                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7695                awk '/ldlm_cancel/ {print $2}')
7696         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7697                awk '/ldlm_bl_callback/ {print $2}')
7698         unlink $DIR/$tdir/f1
7699         sleep 5
7700         can2=$(do_facet $SINGLEMDS \
7701                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7702                awk '/ldlm_cancel/ {print $2}')
7703         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7704                awk '/ldlm_bl_callback/ {print $2}')
7705         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7706         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7707         lru_resize_enable mdc
7708         lru_resize_enable osc
7709 }
7710 run_test 120e "Early Lock Cancel: unlink test"
7711
7712 test_120f() {
7713         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7714         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7715                skip "no early lock cancel on server" && return 0
7716         test_mkdir -p -c1 $DIR/$tdir
7717         lru_resize_disable mdc
7718         lru_resize_disable osc
7719         test_mkdir -p -c1 $DIR/$tdir/d1
7720         test_mkdir -p -c1 $DIR/$tdir/d2
7721         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
7722         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
7723         cancel_lru_locks mdc
7724         cancel_lru_locks osc
7725         dd if=$DIR/$tdir/d1/f1 of=/dev/null
7726         dd if=$DIR/$tdir/d2/f2 of=/dev/null
7727         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
7728         # XXX client can not do early lock cancel of OST lock
7729         # during rename (LU-4206), so cancel osc lock now.
7730         cancel_lru_locks osc
7731         can1=$(do_facet $SINGLEMDS \
7732                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7733                awk '/ldlm_cancel/ {print $2}')
7734         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7735                awk '/ldlm_bl_callback/ {print $2}')
7736         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7737         sleep 5
7738         can2=$(do_facet $SINGLEMDS \
7739                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7740                awk '/ldlm_cancel/ {print $2}')
7741         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7742                awk '/ldlm_bl_callback/ {print $2}')
7743         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7744         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7745         lru_resize_enable mdc
7746         lru_resize_enable osc
7747 }
7748 run_test 120f "Early Lock Cancel: rename test"
7749
7750 test_120g() {
7751         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7752         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7753                skip "no early lock cancel on server" && return 0
7754         lru_resize_disable mdc
7755         lru_resize_disable osc
7756         count=10000
7757         echo create $count files
7758         test_mkdir -p $DIR/$tdir
7759         cancel_lru_locks mdc
7760         cancel_lru_locks osc
7761         t0=`date +%s`
7762
7763         can0=$(do_facet $SINGLEMDS \
7764                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7765                awk '/ldlm_cancel/ {print $2}')
7766         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7767                awk '/ldlm_bl_callback/ {print $2}')
7768         createmany -o $DIR/$tdir/f $count
7769         sync
7770         can1=$(do_facet $SINGLEMDS \
7771                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7772                awk '/ldlm_cancel/ {print $2}')
7773         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7774                awk '/ldlm_bl_callback/ {print $2}')
7775         t1=$(date +%s)
7776         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
7777         echo rm $count files
7778         rm -r $DIR/$tdir
7779         sync
7780         can2=$(do_facet $SINGLEMDS \
7781                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7782                awk '/ldlm_cancel/ {print $2}')
7783         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7784                awk '/ldlm_bl_callback/ {print $2}')
7785         t2=$(date +%s)
7786         echo total: $count removes in $((t2-t1))
7787         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
7788         sleep 2
7789         # wait for commitment of removal
7790         lru_resize_enable mdc
7791         lru_resize_enable osc
7792 }
7793 run_test 120g "Early Lock Cancel: performance test"
7794
7795 test_121() { #bug #10589
7796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7797         rm -rf $DIR/$tfile
7798         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
7799 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
7800         lctl set_param fail_loc=0x310
7801         cancel_lru_locks osc > /dev/null
7802         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
7803         lctl set_param fail_loc=0
7804         [[ $reads -eq $writes ]] ||
7805                 error "read $reads blocks, must be $writes blocks"
7806 }
7807 run_test 121 "read cancel race ========="
7808
7809 test_123a() { # was test 123, statahead(bug 11401)
7810         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7811         SLOWOK=0
7812         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
7813             log "testing on UP system. Performance may be not as good as expected."
7814                         SLOWOK=1
7815         fi
7816
7817         rm -rf $DIR/$tdir
7818         test_mkdir -p $DIR/$tdir
7819         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7820         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
7821         MULT=10
7822         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
7823                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
7824
7825                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7826                 lctl set_param -n llite.*.statahead_max 0
7827                 lctl get_param llite.*.statahead_max
7828                 cancel_lru_locks mdc
7829                 cancel_lru_locks osc
7830                 stime=`date +%s`
7831                 time ls -l $DIR/$tdir | wc -l
7832                 etime=`date +%s`
7833                 delta=$((etime - stime))
7834                 log "ls $i files without statahead: $delta sec"
7835                 lctl set_param llite.*.statahead_max=$max
7836
7837                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7838                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
7839                 cancel_lru_locks mdc
7840                 cancel_lru_locks osc
7841                 stime=`date +%s`
7842                 time ls -l $DIR/$tdir | wc -l
7843                 etime=`date +%s`
7844                 delta_sa=$((etime - stime))
7845                 log "ls $i files with statahead: $delta_sa sec"
7846                 lctl get_param -n llite.*.statahead_stats
7847                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
7848
7849                 [[ $swrong -lt $ewrong ]] &&
7850                         log "statahead was stopped, maybe too many locks held!"
7851                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
7852
7853                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7854                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
7855                     lctl set_param -n llite.*.statahead_max 0
7856                     lctl get_param llite.*.statahead_max
7857                     cancel_lru_locks mdc
7858                     cancel_lru_locks osc
7859                     stime=`date +%s`
7860                     time ls -l $DIR/$tdir | wc -l
7861                     etime=`date +%s`
7862                     delta=$((etime - stime))
7863                     log "ls $i files again without statahead: $delta sec"
7864                     lctl set_param llite.*.statahead_max=$max
7865                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
7866                         if [  $SLOWOK -eq 0 ]; then
7867                                 error "ls $i files is slower with statahead!"
7868                         else
7869                                 log "ls $i files is slower with statahead!"
7870                         fi
7871                         break
7872                     fi
7873                 fi
7874
7875                 [ $delta -gt 20 ] && break
7876                 [ $delta -gt 8 ] && MULT=$((50 / delta))
7877                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
7878         done
7879         log "ls done"
7880
7881         stime=`date +%s`
7882         rm -r $DIR/$tdir
7883         sync
7884         etime=`date +%s`
7885         delta=$((etime - stime))
7886         log "rm -r $DIR/$tdir/: $delta seconds"
7887         log "rm done"
7888         lctl get_param -n llite.*.statahead_stats
7889 }
7890 run_test 123a "verify statahead work"
7891
7892 test_123b () { # statahead(bug 15027)
7893         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7894         test_mkdir -p $DIR/$tdir
7895         createmany -o $DIR/$tdir/$tfile-%d 1000
7896
7897         cancel_lru_locks mdc
7898         cancel_lru_locks osc
7899
7900 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
7901         lctl set_param fail_loc=0x80000803
7902         ls -lR $DIR/$tdir > /dev/null
7903         log "ls done"
7904         lctl set_param fail_loc=0x0
7905         lctl get_param -n llite.*.statahead_stats
7906         rm -r $DIR/$tdir
7907         sync
7908
7909 }
7910 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
7911
7912 test_124a() {
7913         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7914         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
7915                 skip "no lru resize on server" && return 0
7916         local NR=2000
7917         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
7918
7919         log "create $NR files at $DIR/$tdir"
7920         createmany -o $DIR/$tdir/f $NR ||
7921                 error "failed to create $NR files in $DIR/$tdir"
7922
7923         cancel_lru_locks mdc
7924         ls -l $DIR/$tdir > /dev/null
7925
7926         local NSDIR=""
7927         local LRU_SIZE=0
7928         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
7929                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
7930                 LRU_SIZE=$($LCTL get_param -n $PARAM)
7931                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
7932                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
7933                         log "NSDIR=$NSDIR"
7934                         log "NS=$(basename $NSDIR)"
7935                         break
7936                 fi
7937         done
7938
7939         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
7940                 skip "Not enough cached locks created!"
7941                 return 0
7942         fi
7943         log "LRU=$LRU_SIZE"
7944
7945         local SLEEP=30
7946
7947         # We know that lru resize allows one client to hold $LIMIT locks
7948         # for 10h. After that locks begin to be killed by client.
7949         local MAX_HRS=10
7950         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
7951         log "LIMIT=$LIMIT"
7952
7953         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
7954         # killing locks. Some time was spent for creating locks. This means
7955         # that up to the moment of sleep finish we must have killed some of
7956         # them (10-100 locks). This depends on how fast ther were created.
7957         # Many of them were touched in almost the same moment and thus will
7958         # be killed in groups.
7959         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
7960
7961         # Use $LRU_SIZE_B here to take into account real number of locks
7962         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
7963         local LRU_SIZE_B=$LRU_SIZE
7964         log "LVF=$LVF"
7965         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
7966         log "OLD_LVF=$OLD_LVF"
7967         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
7968
7969         # Let's make sure that we really have some margin. Client checks
7970         # cached locks every 10 sec.
7971         SLEEP=$((SLEEP+20))
7972         log "Sleep ${SLEEP} sec"
7973         local SEC=0
7974         while ((SEC<$SLEEP)); do
7975                 echo -n "..."
7976                 sleep 5
7977                 SEC=$((SEC+5))
7978                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
7979                 echo -n "$LRU_SIZE"
7980         done
7981         echo ""
7982         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
7983         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
7984
7985         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
7986                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
7987                 unlinkmany $DIR/$tdir/f $NR
7988                 return
7989         }
7990
7991         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
7992         log "unlink $NR files at $DIR/$tdir"
7993         unlinkmany $DIR/$tdir/f $NR
7994 }
7995 run_test 124a "lru resize ======================================="
7996
7997 get_max_pool_limit()
7998 {
7999         local limit=$($LCTL get_param \
8000                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8001         local max=0
8002         for l in $limit; do
8003                 if [[ $l -gt $max ]]; then
8004                         max=$l
8005                 fi
8006         done
8007         echo $max
8008 }
8009
8010 test_124b() {
8011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8012         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8013                 skip "no lru resize on server" && return 0
8014
8015         LIMIT=$(get_max_pool_limit)
8016
8017         NR=$(($(default_lru_size)*20))
8018         if [[ $NR -gt $LIMIT ]]; then
8019                 log "Limit lock number by $LIMIT locks"
8020                 NR=$LIMIT
8021         fi
8022         lru_resize_disable mdc
8023         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8024                 error "failed to create $DIR/$tdir/disable_lru_resize"
8025
8026         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8027         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8028         cancel_lru_locks mdc
8029         stime=`date +%s`
8030         PID=""
8031         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8032         PID="$PID $!"
8033         sleep 2
8034         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8035         PID="$PID $!"
8036         sleep 2
8037         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8038         PID="$PID $!"
8039         wait $PID
8040         etime=`date +%s`
8041         nolruresize_delta=$((etime-stime))
8042         log "ls -la time: $nolruresize_delta seconds"
8043         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8044         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8045
8046         lru_resize_enable mdc
8047         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8048                 error "failed to create $DIR/$tdir/enable_lru_resize"
8049
8050         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8051         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8052         cancel_lru_locks mdc
8053         stime=`date +%s`
8054         PID=""
8055         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8056         PID="$PID $!"
8057         sleep 2
8058         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8059         PID="$PID $!"
8060         sleep 2
8061         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8062         PID="$PID $!"
8063         wait $PID
8064         etime=`date +%s`
8065         lruresize_delta=$((etime-stime))
8066         log "ls -la time: $lruresize_delta seconds"
8067         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8068
8069         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8070                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8071         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8072                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8073         else
8074                 log "lru resize performs the same with no lru resize"
8075         fi
8076         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8077 }
8078 run_test 124b "lru resize (performance test) ======================="
8079
8080 test_125() { # 13358
8081         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8082         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8083         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8084         test_mkdir -p $DIR/d125 || error "mkdir failed"
8085         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8086         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8087         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8088 }
8089 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8090
8091 test_126() { # bug 12829/13455
8092         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8093         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8094         $GSS && skip "must run as gss disabled" && return
8095
8096         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8097         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8098         rm -f $DIR/$tfile
8099         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8100 }
8101 run_test 126 "check that the fsgid provided by the client is taken into account"
8102
8103 test_127a() { # bug 15521
8104         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8105         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8106         $LCTL set_param osc.*.stats=0
8107         FSIZE=$((2048 * 1024))
8108         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8109         cancel_lru_locks osc
8110         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8111
8112         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8113         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8114                 echo "got $COUNT $NAME"
8115                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8116                 eval $NAME=$COUNT || error "Wrong proc format"
8117
8118                 case $NAME in
8119                         read_bytes|write_bytes)
8120                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8121                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8122                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8123                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8124                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8125                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8126                                 error "sumsquare is too small: $SUMSQ"
8127                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8128                                 error "sumsquare is too big: $SUMSQ"
8129                         ;;
8130                         *) ;;
8131                 esac
8132         done < $DIR/${tfile}.tmp
8133
8134         #check that we actually got some stats
8135         [ "$read_bytes" ] || error "Missing read_bytes stats"
8136         [ "$write_bytes" ] || error "Missing write_bytes stats"
8137         [ "$read_bytes" != 0 ] || error "no read done"
8138         [ "$write_bytes" != 0 ] || error "no write done"
8139 }
8140 run_test 127a "verify the client stats are sane"
8141
8142 test_127b() { # bug LU-333
8143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8144         $LCTL set_param llite.*.stats=0
8145         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8146         # perform 2 reads and writes so MAX is different from SUM.
8147         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8148         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8149         cancel_lru_locks osc
8150         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8151         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8152
8153         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8154         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8155                 echo "got $COUNT $NAME"
8156                 eval $NAME=$COUNT || error "Wrong proc format"
8157
8158         case $NAME in
8159                 read_bytes)
8160                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8161                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8162                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8163                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8164                         ;;
8165                 write_bytes)
8166                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8167                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8168                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8169                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8170                         ;;
8171                         *) ;;
8172                 esac
8173         done < $TMP/${tfile}.tmp
8174
8175         #check that we actually got some stats
8176         [ "$read_bytes" ] || error "Missing read_bytes stats"
8177         [ "$write_bytes" ] || error "Missing write_bytes stats"
8178         [ "$read_bytes" != 0 ] || error "no read done"
8179         [ "$write_bytes" != 0 ] || error "no write done"
8180 }
8181 run_test 127b "verify the llite client stats are sane"
8182
8183 test_128() { # bug 15212
8184         touch $DIR/$tfile
8185         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8186                 find $DIR/$tfile
8187                 find $DIR/$tfile
8188         EOF
8189
8190         result=$(grep error $TMP/$tfile.log)
8191         rm -f $DIR/$tfile
8192         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8193 }
8194 run_test 128 "interactive lfs for 2 consecutive find's"
8195
8196 set_dir_limits () {
8197         local mntdev
8198         local canondev
8199         local node
8200
8201         local LDPROC=/proc/fs/ldiskfs
8202         local facets=$(get_facets MDS)
8203
8204         for facet in ${facets//,/ }; do
8205                 canondev=$(ldiskfs_canon \
8206                            *.$(convert_facet2label $facet).mntdev $facet)
8207                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8208                                                 LDPROC=/sys/fs/ldiskfs
8209                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8210         done
8211 }
8212
8213 test_129() {
8214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8215         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8216                 skip "Only applicable to ldiskfs-based MDTs"
8217                 return
8218         fi
8219         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8220         ENOSPC=28
8221         EFBIG=27
8222
8223         rm -rf $DIR/$tdir
8224         test_mkdir -p $DIR/$tdir
8225
8226         # block size of mds1
8227         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8228         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8229         local MAX=$((MDSBLOCKSIZE * 3))
8230         set_dir_limits $MAX
8231         local I=$(stat -c%s "$DIR/$tdir")
8232         local J=0
8233         local STRIPE_COUNT=1
8234         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8235         MAX=$((MAX*STRIPE_COUNT))
8236         while [[ $I -le $MAX ]]; do
8237                 $MULTIOP $DIR/$tdir/$J Oc
8238                 rc=$?
8239                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8240                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8241                 #and EFBIG for previous versions
8242                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8243                         set_dir_limits 0
8244                         echo "return code $rc received as expected"
8245                         multiop $DIR/$tdir/$J Oc ||
8246                                 error_exit "multiop failed w/o dir size limit"
8247
8248                         I=$(stat -c%s "$DIR/$tdir")
8249
8250                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8251                                         $(version_code 2.4.51) ]
8252                         then
8253                                 [[ $I -eq $MAX ]] && return 0
8254                         else
8255                                 [[ $I -gt $MAX ]] && return 0
8256                         fi
8257                         error_exit "current dir size $I, previous limit $MAX"
8258                 elif [ $rc -ne 0 ]; then
8259                         set_dir_limits 0
8260                         error_exit "return code $rc received instead of expected " \
8261                                    "$EFBIG or $ENOSPC, files in dir $I"
8262                 fi
8263                 J=$((J+1))
8264                 I=$(stat -c%s "$DIR/$tdir")
8265         done
8266
8267         set_dir_limits 0
8268         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8269 }
8270 run_test 129 "test directory size limit ========================"
8271
8272 OLDIFS="$IFS"
8273 cleanup_130() {
8274         trap 0
8275         IFS="$OLDIFS"
8276 }
8277
8278 test_130a() {
8279         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8280         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8281                 return
8282
8283         trap cleanup_130 EXIT RETURN
8284
8285         local fm_file=$DIR/$tfile
8286         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8287         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8288                 error "dd failed for $fm_file"
8289
8290         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8291         filefrag -ves $fm_file
8292         RC=$?
8293         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8294                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8295         [ $RC != 0 ] && error "filefrag $fm_file failed"
8296
8297         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8298                       grep -v "ext:" | grep -v "found")
8299         lun=$($GETSTRIPE -i $fm_file)
8300
8301         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8302         IFS=$'\n'
8303         tot_len=0
8304         for line in $filefrag_op
8305         do
8306                 frag_lun=`echo $line | cut -d: -f5`
8307                 ext_len=`echo $line | cut -d: -f4`
8308                 if (( $frag_lun != $lun )); then
8309                         cleanup_130
8310                         error "FIEMAP on 1-stripe file($fm_file) failed"
8311                         return
8312                 fi
8313                 (( tot_len += ext_len ))
8314         done
8315
8316         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8317                 cleanup_130
8318                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8319                 return
8320         fi
8321
8322         cleanup_130
8323
8324         echo "FIEMAP on single striped file succeeded"
8325 }
8326 run_test 130a "FIEMAP (1-stripe file)"
8327
8328 test_130b() {
8329         [ "$OSTCOUNT" -lt "2" ] &&
8330                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8331
8332         [ "$OSTCOUNT" -ge "10" ] &&
8333                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8334
8335         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8336         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8337                 return
8338
8339         trap cleanup_130 EXIT RETURN
8340
8341         local fm_file=$DIR/$tfile
8342         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8343         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8344                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8345
8346         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8347                 error "dd failed on $fm_file"
8348
8349         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8350         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8351                       grep -v "ext:" | grep -v "found")
8352
8353         last_lun=$(echo $filefrag_op | cut -d: -f5)
8354
8355         IFS=$'\n'
8356         tot_len=0
8357         num_luns=1
8358         for line in $filefrag_op
8359         do
8360                 frag_lun=`echo $line | cut -d: -f5`
8361                 ext_len=`echo $line | cut -d: -f4`
8362                 if (( $frag_lun != $last_lun )); then
8363                         if (( tot_len != 1024 )); then
8364                                 cleanup_130
8365                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8366                                 return
8367                         else
8368                                 (( num_luns += 1 ))
8369                                 tot_len=0
8370                         fi
8371                 fi
8372                 (( tot_len += ext_len ))
8373                 last_lun=$frag_lun
8374         done
8375         if (( num_luns != 2 || tot_len != 1024 )); then
8376                 cleanup_130
8377                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8378                 return
8379         fi
8380
8381         cleanup_130
8382
8383         echo "FIEMAP on 2-stripe file succeeded"
8384 }
8385 run_test 130b "FIEMAP (2-stripe file)"
8386
8387 test_130c() {
8388         [ "$OSTCOUNT" -lt "2" ] &&
8389                 skip_env "skipping FIEMAP on 2-stripe file" && return
8390
8391         [ "$OSTCOUNT" -ge "10" ] &&
8392                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8393
8394         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8395         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8396                 return
8397
8398         trap cleanup_130 EXIT RETURN
8399
8400         local fm_file=$DIR/$tfile
8401         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8402         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8403                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8404
8405         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8406
8407         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8408         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8409
8410         last_lun=`echo $filefrag_op | cut -d: -f5`
8411
8412         IFS=$'\n'
8413         tot_len=0
8414         num_luns=1
8415         for line in $filefrag_op
8416         do
8417                 frag_lun=`echo $line | cut -d: -f5`
8418                 ext_len=`echo $line | cut -d: -f4`
8419                 if (( $frag_lun != $last_lun )); then
8420                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8421                         if (( logical != 512 )); then
8422                                 cleanup_130
8423                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8424                                 return
8425                         fi
8426                         if (( tot_len != 512 )); then
8427                                 cleanup_130
8428                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8429                                 return
8430                         else
8431                                 (( num_luns += 1 ))
8432                                 tot_len=0
8433                         fi
8434                 fi
8435                 (( tot_len += ext_len ))
8436                 last_lun=$frag_lun
8437         done
8438         if (( num_luns != 2 || tot_len != 512 )); then
8439                 cleanup_130
8440                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8441                 return
8442         fi
8443
8444         cleanup_130
8445
8446         echo "FIEMAP on 2-stripe file with hole succeeded"
8447 }
8448 run_test 130c "FIEMAP (2-stripe file with hole)"
8449
8450 test_130d() {
8451         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8452
8453         [ "$OSTCOUNT" -ge "10" ] &&
8454                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8455
8456         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8457         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8458
8459         trap cleanup_130 EXIT RETURN
8460
8461         local fm_file=$DIR/$tfile
8462         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8463         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8464                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8465
8466         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8467         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8468                 error "dd failed on $fm_file"
8469
8470         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8471         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8472                 grep -v "ext:" | grep -v "found"`
8473
8474         last_lun=`echo $filefrag_op | cut -d: -f5`
8475
8476         IFS=$'\n'
8477         tot_len=0
8478         num_luns=1
8479         for line in $filefrag_op
8480         do
8481                 frag_lun=`echo $line | cut -d: -f5`
8482                 ext_len=`echo $line | cut -d: -f4`
8483                 if (( $frag_lun != $last_lun )); then
8484                         if (( tot_len != 1024 )); then
8485                                 cleanup_130
8486                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8487                                 return
8488                         else
8489                                 (( num_luns += 1 ))
8490                                 tot_len=0
8491                         fi
8492                 fi
8493                 (( tot_len += ext_len ))
8494                 last_lun=$frag_lun
8495         done
8496         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8497                 cleanup_130
8498                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8499                 return
8500         fi
8501
8502         cleanup_130
8503
8504         echo "FIEMAP on N-stripe file succeeded"
8505 }
8506 run_test 130d "FIEMAP (N-stripe file)"
8507
8508 test_130e() {
8509         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8510
8511         [ "$OSTCOUNT" -ge "10" ] &&
8512                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8513
8514         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8515         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8516
8517         trap cleanup_130 EXIT RETURN
8518
8519         local fm_file=$DIR/$tfile
8520         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8521         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8522                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8523
8524         NUM_BLKS=512
8525         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8526         for ((i = 0; i < $NUM_BLKS; i++))
8527         do
8528                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8529         done
8530
8531         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8532         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8533
8534         last_lun=`echo $filefrag_op | cut -d: -f5`
8535
8536         IFS=$'\n'
8537         tot_len=0
8538         num_luns=1
8539         for line in $filefrag_op
8540         do
8541                 frag_lun=`echo $line | cut -d: -f5`
8542                 ext_len=`echo $line | cut -d: -f4`
8543                 if (( $frag_lun != $last_lun )); then
8544                         if (( tot_len != $EXPECTED_LEN )); then
8545                                 cleanup_130
8546                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8547                                 return
8548                         else
8549                                 (( num_luns += 1 ))
8550                                 tot_len=0
8551                         fi
8552                 fi
8553                 (( tot_len += ext_len ))
8554                 last_lun=$frag_lun
8555         done
8556         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8557                 cleanup_130
8558                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8559                 return
8560         fi
8561
8562         cleanup_130
8563
8564         echo "FIEMAP with continuation calls succeeded"
8565 }
8566 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8567
8568 # Test for writev/readv
8569 test_131a() {
8570         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8571         error "writev test failed"
8572         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8573         error "readv failed"
8574         rm -f $DIR/$tfile
8575 }
8576 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8577
8578 test_131b() {
8579         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8580         error "append writev test failed"
8581         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8582         error "append writev test failed"
8583         rm -f $DIR/$tfile
8584 }
8585 run_test 131b "test append writev"
8586
8587 test_131c() {
8588         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8589         error "NOT PASS"
8590 }
8591 run_test 131c "test read/write on file w/o objects"
8592
8593 test_131d() {
8594         rwv -f $DIR/$tfile -w -n 1 1572864
8595         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8596         if [ "$NOB" != 1572864 ]; then
8597                 error "Short read filed: read $NOB bytes instead of 1572864"
8598         fi
8599         rm -f $DIR/$tfile
8600 }
8601 run_test 131d "test short read"
8602
8603 test_131e() {
8604         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8605         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8606         error "read hitting hole failed"
8607         rm -f $DIR/$tfile
8608 }
8609 run_test 131e "test read hitting hole"
8610
8611 get_ost_param() {
8612         local token=$1
8613         local gl_sum=0
8614         for node in $(osts_nodes); do
8615                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8616                 [ x$gl = x"" ] && gl=0
8617                 gl_sum=$((gl_sum + gl))
8618         done
8619         echo $gl_sum
8620 }
8621
8622 som_mode_switch() {
8623         local som=$1
8624         local gl1=$2
8625         local gl2=$3
8626
8627         if [ x$som = x"enabled" ]; then
8628                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8629                 MOUNTOPT=`echo $MOUNTOPT | sed 's/som_preview//g'`
8630                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8631         else
8632                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8633                 MOUNTOPT="$MOUNTOPT,som_preview"
8634                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8635         fi
8636
8637         # do remount to make new mount-conf parameters actual
8638         echo remounting...
8639         sync
8640         stopall
8641         setupall
8642 }
8643
8644 test_132() { #1028, SOM
8645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8646         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8647         local num=$(get_mds_dir $DIR)
8648         local mymds=mds${num}
8649         local MOUNTOPT_SAVE=$MOUNTOPT
8650
8651         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8652         cancel_lru_locks osc
8653
8654         som1=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8655
8656         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8657         stat $DIR/$tfile >/dev/null
8658         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8659         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8660         rm $DIR/$tfile
8661         som_mode_switch $som1 $gl1 $gl2
8662
8663         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8664         cancel_lru_locks osc
8665
8666         som2=$(do_facet $mymds "$LCTL get_param mdt.*.som" |  awk -F= ' {print $2}' | head -n 1)
8667         if [ $som1 == $som2 ]; then
8668             error "som is still "$som2
8669             if [ x$som2 = x"enabled" ]; then
8670                 som2="disabled"
8671             else
8672                 som2="enabled"
8673             fi
8674         fi
8675
8676         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8677         stat $DIR/$tfile >/dev/null
8678         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8679         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8680         som_mode_switch $som2 $gl1 $gl2
8681         MOUNTOPT=$MOUNTOPT_SAVE
8682 }
8683 run_test 132 "som avoids glimpse rpc"
8684
8685 check_stats() {
8686         local res
8687         local count
8688         case $1 in
8689         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
8690                  ;;
8691         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
8692                  ;;
8693         *) error "Wrong argument $1" ;;
8694         esac
8695         echo $res
8696         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
8697         # if the argument $3 is zero, it means any stat increment is ok.
8698         if [[ $3 -gt 0 ]]; then
8699                 count=$(echo $res | awk '{ print $2 }')
8700                 [[ $count -ne $3 ]] &&
8701                         error "The $2 counter on $1 is wrong - expected $3"
8702         fi
8703 }
8704
8705 test_133a() {
8706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8707         remote_ost_nodsh && skip "remote OST with nodsh" && return
8708         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8709
8710         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8711                 { skip "MDS doesn't support rename stats"; return; }
8712         local testdir=$DIR/${tdir}/stats_testdir
8713         mkdir -p $DIR/${tdir}
8714
8715         # clear stats.
8716         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8717         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8718
8719         # verify mdt stats first.
8720         mkdir ${testdir} || error "mkdir failed"
8721         check_stats $SINGLEMDS "mkdir" 1
8722         touch ${testdir}/${tfile} || "touch failed"
8723         check_stats $SINGLEMDS "open" 1
8724         check_stats $SINGLEMDS "close" 1
8725         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
8726         check_stats $SINGLEMDS "mknod" 1
8727         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
8728         check_stats $SINGLEMDS "unlink" 1
8729         rm -f ${testdir}/${tfile} || error "file remove failed"
8730         check_stats $SINGLEMDS "unlink" 2
8731
8732         # remove working dir and check mdt stats again.
8733         rmdir ${testdir} || error "rmdir failed"
8734         check_stats $SINGLEMDS "rmdir" 1
8735
8736         local testdir1=$DIR/${tdir}/stats_testdir1
8737         mkdir -p ${testdir}
8738         mkdir -p ${testdir1}
8739         touch ${testdir1}/test1
8740         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
8741         check_stats $SINGLEMDS "crossdir_rename" 1
8742
8743         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
8744         check_stats $SINGLEMDS "samedir_rename" 1
8745
8746         rm -rf $DIR/${tdir}
8747 }
8748 run_test 133a "Verifying MDT stats ========================================"
8749
8750 test_133b() {
8751         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8752         remote_ost_nodsh && skip "remote OST with nodsh" && return
8753         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8754         local testdir=$DIR/${tdir}/stats_testdir
8755         mkdir -p ${testdir} || error "mkdir failed"
8756         touch ${testdir}/${tfile} || "touch failed"
8757         cancel_lru_locks mdc
8758
8759         # clear stats.
8760         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8761         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8762
8763         # extra mdt stats verification.
8764         chmod 444 ${testdir}/${tfile} || error "chmod failed"
8765         check_stats $SINGLEMDS "setattr" 1
8766         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8767         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
8768         then            # LU-1740
8769                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
8770                 check_stats $SINGLEMDS "getattr" 1
8771         fi
8772         $LFS df || error "lfs failed"
8773         check_stats $SINGLEMDS "statfs" 1
8774
8775         rm -rf $DIR/${tdir}
8776 }
8777 run_test 133b "Verifying extra MDT stats =================================="
8778
8779 test_133c() {
8780         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8781         remote_ost_nodsh && skip "remote OST with nodsh" && return
8782         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8783         local testdir=$DIR/${tdir}/stats_testdir
8784         test_mkdir -p ${testdir} || error "mkdir failed"
8785
8786         # verify obdfilter stats.
8787         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8788         sync
8789         cancel_lru_locks osc
8790         wait_delete_completed
8791
8792         # clear stats.
8793         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
8794         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
8795
8796         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
8797         sync
8798         cancel_lru_locks osc
8799         check_stats ost "write" 1
8800
8801         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
8802         check_stats ost "read" 1
8803
8804         > ${testdir}/${tfile} || error "truncate failed"
8805         check_stats ost "punch" 1
8806
8807         rm -f ${testdir}/${tfile} || error "file remove failed"
8808         wait_delete_completed
8809         check_stats ost "destroy" 1
8810
8811         rm -rf $DIR/${tdir}
8812 }
8813 run_test 133c "Verifying OST stats ========================================"
8814
8815 order_2() {
8816         local value=$1
8817         local orig=$value
8818         local order=1
8819
8820         while [ $value -ge 2 ]; do
8821                 order=$((order*2))
8822                 value=$((value/2))
8823         done
8824
8825         if [ $orig -gt $order ]; then
8826                 order=$((order*2))
8827         fi
8828         echo $order
8829 }
8830
8831 size_in_KMGT() {
8832     local value=$1
8833     local size=('K' 'M' 'G' 'T');
8834     local i=0
8835     local size_string=$value
8836
8837     while [ $value -ge 1024 ]; do
8838         if [ $i -gt 3 ]; then
8839             #T is the biggest unit we get here, if that is bigger,
8840             #just return XXXT
8841             size_string=${value}T
8842             break
8843         fi
8844         value=$((value >> 10))
8845         if [ $value -lt 1024 ]; then
8846             size_string=${value}${size[$i]}
8847             break
8848         fi
8849         i=$((i + 1))
8850     done
8851
8852     echo $size_string
8853 }
8854
8855 get_rename_size() {
8856     local size=$1
8857     local context=${2:-.}
8858     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
8859                 grep -A1 $context |
8860                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
8861     echo $sample
8862 }
8863
8864 test_133d() {
8865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8866         remote_ost_nodsh && skip "remote OST with nodsh" && return
8867         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8868         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
8869         { skip "MDS doesn't support rename stats"; return; }
8870
8871         local testdir1=$DIR/${tdir}/stats_testdir1
8872         local testdir2=$DIR/${tdir}/stats_testdir2
8873
8874         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8875
8876         mkdir -p ${testdir1} || error "mkdir failed"
8877         mkdir -p ${testdir2} || error "mkdir failed"
8878
8879         createmany -o $testdir1/test 512 || error "createmany failed"
8880
8881         # check samedir rename size
8882         mv ${testdir1}/test0 ${testdir1}/test_0
8883
8884         local testdir1_size=$(ls -l $DIR/${tdir} |
8885                 awk '/stats_testdir1/ {print $5}')
8886         local testdir2_size=$(ls -l $DIR/${tdir} |
8887                 awk '/stats_testdir2/ {print $5}')
8888
8889         testdir1_size=$(order_2 $testdir1_size)
8890         testdir2_size=$(order_2 $testdir2_size)
8891
8892         testdir1_size=$(size_in_KMGT $testdir1_size)
8893         testdir2_size=$(size_in_KMGT $testdir2_size)
8894
8895         echo "source rename dir size: ${testdir1_size}"
8896         echo "target rename dir size: ${testdir2_size}"
8897
8898         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
8899         eval $cmd || error "$cmd failed"
8900         local samedir=$($cmd | grep 'same_dir')
8901         local same_sample=$(get_rename_size $testdir1_size)
8902         [ -z "$samedir" ] && error "samedir_rename_size count error"
8903         [[ $same_sample -eq 1 ]] ||
8904                 error "samedir_rename_size error $same_sample"
8905         echo "Check same dir rename stats success"
8906
8907         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
8908
8909         # check crossdir rename size
8910         mv ${testdir1}/test_0 ${testdir2}/test_0
8911
8912         testdir1_size=$(ls -l $DIR/${tdir} |
8913                 awk '/stats_testdir1/ {print $5}')
8914         testdir2_size=$(ls -l $DIR/${tdir} |
8915                 awk '/stats_testdir2/ {print $5}')
8916
8917         testdir1_size=$(order_2 $testdir1_size)
8918         testdir2_size=$(order_2 $testdir2_size)
8919
8920         testdir1_size=$(size_in_KMGT $testdir1_size)
8921         testdir2_size=$(size_in_KMGT $testdir2_size)
8922
8923         echo "source rename dir size: ${testdir1_size}"
8924         echo "target rename dir size: ${testdir2_size}"
8925
8926         eval $cmd || error "$cmd failed"
8927         local crossdir=$($cmd | grep 'crossdir')
8928         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
8929         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
8930         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
8931         [[ $src_sample -eq 1 ]] ||
8932                 error "crossdir_rename_size error $src_sample"
8933         [[ $tgt_sample -eq 1 ]] ||
8934                 error "crossdir_rename_size error $tgt_sample"
8935         echo "Check cross dir rename stats success"
8936         rm -rf $DIR/${tdir}
8937 }
8938 run_test 133d "Verifying rename_stats ========================================"
8939
8940 test_133e() {
8941         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8942         local testdir=$DIR/${tdir}/stats_testdir
8943         local ctr f0 f1 bs=32768 count=42 sum
8944
8945         remote_ost_nodsh && skip "remote OST with nodsh" && return
8946         mkdir -p ${testdir} || error "mkdir failed"
8947
8948         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
8949
8950         for ctr in {write,read}_bytes; do
8951                 sync
8952                 cancel_lru_locks osc
8953
8954                 do_facet ost1 $LCTL set_param -n \
8955                         "obdfilter.*.exports.clear=clear"
8956
8957                 if [ $ctr = write_bytes ]; then
8958                         f0=/dev/zero
8959                         f1=${testdir}/${tfile}
8960                 else
8961                         f0=${testdir}/${tfile}
8962                         f1=/dev/null
8963                 fi
8964
8965                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
8966                         error "dd failed"
8967                 sync
8968                 cancel_lru_locks osc
8969
8970                 sum=$(do_facet ost1 $LCTL get_param \
8971                         "obdfilter.*.exports.*.stats" |
8972                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
8973                                 $1 == ctr { sum += $7 }
8974                                 END { printf("%0.0f", sum) }')
8975
8976                 if ((sum != bs * count)); then
8977                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
8978                 fi
8979         done
8980
8981         rm -rf $DIR/${tdir}
8982 }
8983 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
8984
8985 test_133f() {
8986         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
8987         local facet
8988
8989         # First without trusting modes.
8990         find $proc_dirs -exec cat '{}' \; &> /dev/null
8991
8992         # Second verifying readability.
8993         find $proc_dirs \
8994                 -type f \
8995                 -exec cat '{}' \; &> /dev/null ||
8996                         error "proc file read failed"
8997
8998         for facet in $SINGLEMDS ost1; do
8999                 do_facet $facet find $proc_dirs \
9000                         ! -name req_history \
9001                         -exec cat '{}' \\\; &> /dev/null
9002
9003                 do_facet $facet find $proc_dirs \
9004                         ! -name req_history \
9005                         -type f \
9006                         -exec cat '{}' \\\; &> /dev/null ||
9007                                 error "proc file read failed"
9008         done
9009 }
9010 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9011
9012 test_133g() {
9013         local proc_dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/"
9014         local facet
9015
9016         # Second verifying readability.
9017         find $proc_dirs \
9018                 -type f \
9019                 -not -name force_lbug \
9020                 -not -name changelog_mask \
9021                 -exec badarea_io '{}' \; &> /dev/null ||
9022                 error "find $proc_dirs failed"
9023
9024         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9025                 skip "Too old lustre on MDS"
9026
9027         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9028                 skip "Too old lustre on ost1"
9029
9030         for facet in $SINGLEMDS ost1; do
9031                 do_facet $facet find $proc_dirs \
9032                         -type f \
9033                         -not -name force_lbug \
9034                         -not -name changelog_mask \
9035                         -exec badarea_io '{}' \\\; &> /dev/null ||
9036                 error "$facet find $proc_dirs failed"
9037
9038         done
9039
9040         # remount the FS in case writes/reads /proc break the FS
9041         cleanup || error "failed to unmount"
9042         setup || error "failed to setup"
9043         true
9044 }
9045 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9046
9047 test_140() { #bug-17379
9048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9049         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9050         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9051         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9052
9053         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9054         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9055         local i=0
9056         while i=`expr $i + 1`; do
9057                 test_mkdir -p $i || error "Creating dir $i"
9058                 cd $i || error "Changing to $i"
9059                 ln -s ../stat stat || error "Creating stat symlink"
9060                 # Read the symlink until ELOOP present,
9061                 # not LBUGing the system is considered success,
9062                 # we didn't overrun the stack.
9063                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9064                 [ $ret -ne 0 ] && {
9065                         if [ $ret -eq 40 ]; then
9066                                 break  # -ELOOP
9067                         else
9068                                 error "Open stat symlink"
9069                                 return
9070                         fi
9071                 }
9072         done
9073         i=`expr $i - 1`
9074         echo "The symlink depth = $i"
9075         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9076                                         error "Invalid symlink depth"
9077
9078         # Test recursive symlink
9079         ln -s symlink_self symlink_self
9080         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9081         echo "open symlink_self returns $ret"
9082         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9083 }
9084 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9085
9086 test_150() {
9087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9088         local TF="$TMP/$tfile"
9089
9090         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9091         cp $TF $DIR/$tfile
9092         cancel_lru_locks osc
9093         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9094         remount_client $MOUNT
9095         df -P $MOUNT
9096         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9097
9098         $TRUNCATE $TF 6000
9099         $TRUNCATE $DIR/$tfile 6000
9100         cancel_lru_locks osc
9101         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9102
9103         echo "12345" >>$TF
9104         echo "12345" >>$DIR/$tfile
9105         cancel_lru_locks osc
9106         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9107
9108         echo "12345" >>$TF
9109         echo "12345" >>$DIR/$tfile
9110         cancel_lru_locks osc
9111         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9112
9113         rm -f $TF
9114         true
9115 }
9116 run_test 150 "truncate/append tests"
9117
9118 #LU-2902 roc_hit was not able to read all values from lproc
9119 function roc_hit_init() {
9120         local list=$(comma_list $(osts_nodes))
9121         local dir=$DIR/$tdir-check
9122         local file=$dir/file
9123         local BEFORE
9124         local AFTER
9125         local idx
9126
9127         test_mkdir -p $dir
9128         #use setstripe to do a write to every ost
9129         for i in $(seq 0 $((OSTCOUNT-1))); do
9130                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9131                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9132                 idx=$(printf %04x $i)
9133                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9134                         awk '$1 == "cache_access" {sum += $2}
9135                                 END { printf("%0.0f", sum) }')
9136
9137                 cancel_lru_locks osc
9138                 cat $file >/dev/null
9139
9140                 AFTER=$(get_osd_param $list *OST*$idx stats |
9141                         awk '$1 == "cache_access" {sum += $2}
9142                                 END { printf("%0.0f", sum) }')
9143
9144                 echo BEFORE:$BEFORE AFTER:$AFTER
9145                 if ! let "AFTER - BEFORE == 4"; then
9146                         rm -rf $dir
9147                         error "roc_hit is not safe to use"
9148                 fi
9149                 rm $file
9150         done
9151
9152         rm -rf $dir
9153 }
9154
9155 function roc_hit() {
9156         local list=$(comma_list $(osts_nodes))
9157         echo $(get_osd_param $list '' stats |
9158                 awk '$1 == "cache_hit" {sum += $2}
9159                         END { printf("%0.0f", sum) }')
9160 }
9161
9162 function set_cache() {
9163         local on=1
9164
9165         if [ "$2" == "off" ]; then
9166                 on=0;
9167         fi
9168         local list=$(comma_list $(osts_nodes))
9169         set_osd_param $list '' $1_cache_enable $on
9170
9171         cancel_lru_locks osc
9172 }
9173
9174 test_151() {
9175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9176         remote_ost_nodsh && skip "remote OST with nodsh" && return
9177
9178         local CPAGES=3
9179         local list=$(comma_list $(osts_nodes))
9180
9181         # check whether obdfilter is cache capable at all
9182         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9183                 echo "not cache-capable obdfilter"
9184                 return 0
9185         fi
9186
9187         # check cache is enabled on all obdfilters
9188         if get_osd_param $list '' read_cache_enable | grep 0; then
9189                 echo "oss cache is disabled"
9190                 return 0
9191         fi
9192
9193         set_osd_param $list '' writethrough_cache_enable 1
9194
9195         # check write cache is enabled on all obdfilters
9196         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9197                 echo "oss write cache is NOT enabled"
9198                 return 0
9199         fi
9200
9201         roc_hit_init
9202
9203         #define OBD_FAIL_OBD_NO_LRU  0x609
9204         do_nodes $list $LCTL set_param fail_loc=0x609
9205
9206         # pages should be in the case right after write
9207         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9208                 error "dd failed"
9209
9210         local BEFORE=$(roc_hit)
9211         cancel_lru_locks osc
9212         cat $DIR/$tfile >/dev/null
9213         local AFTER=$(roc_hit)
9214
9215         do_nodes $list $LCTL set_param fail_loc=0
9216
9217         if ! let "AFTER - BEFORE == CPAGES"; then
9218                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9219         fi
9220
9221         # the following read invalidates the cache
9222         cancel_lru_locks osc
9223         set_osd_param $list '' read_cache_enable 0
9224         cat $DIR/$tfile >/dev/null
9225
9226         # now data shouldn't be found in the cache
9227         BEFORE=$(roc_hit)
9228         cancel_lru_locks osc
9229         cat $DIR/$tfile >/dev/null
9230         AFTER=$(roc_hit)
9231         if let "AFTER - BEFORE != 0"; then
9232                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9233         fi
9234
9235         set_osd_param $list '' read_cache_enable 1
9236         rm -f $DIR/$tfile
9237 }
9238 run_test 151 "test cache on oss and controls ==============================="
9239
9240 test_152() {
9241         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9242         local TF="$TMP/$tfile"
9243
9244         # simulate ENOMEM during write
9245 #define OBD_FAIL_OST_NOMEM      0x226
9246         lctl set_param fail_loc=0x80000226
9247         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9248         cp $TF $DIR/$tfile
9249         sync || error "sync failed"
9250         lctl set_param fail_loc=0
9251
9252         # discard client's cache
9253         cancel_lru_locks osc
9254
9255         # simulate ENOMEM during read
9256         lctl set_param fail_loc=0x80000226
9257         cmp $TF $DIR/$tfile || error "cmp failed"
9258         lctl set_param fail_loc=0
9259
9260         rm -f $TF
9261 }
9262 run_test 152 "test read/write with enomem ============================"
9263
9264 test_153() {
9265         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9266 }
9267 run_test 153 "test if fdatasync does not crash ======================="
9268
9269 dot_lustre_fid_permission_check() {
9270         local fid=$1
9271         local ffid=$MOUNT/.lustre/fid/$fid
9272         local test_dir=$2
9273
9274         echo "stat fid $fid"
9275         stat $ffid > /dev/null || error "stat $ffid failed."
9276         echo "touch fid $fid"
9277         touch $ffid || error "touch $ffid failed."
9278         echo "write to fid $fid"
9279         cat /etc/hosts > $ffid || error "write $ffid failed."
9280         echo "read fid $fid"
9281         diff /etc/hosts $ffid || error "read $ffid failed."
9282         echo "append write to fid $fid"
9283         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9284         echo "rename fid $fid"
9285         mv $ffid $test_dir/$tfile.1 &&
9286                 error "rename $ffid to $tfile.1 should fail."
9287         touch $test_dir/$tfile.1
9288         mv $test_dir/$tfile.1 $ffid &&
9289                 error "rename $tfile.1 to $ffid should fail."
9290         rm -f $test_dir/$tfile.1
9291         echo "truncate fid $fid"
9292         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9293         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9294                 echo "link fid $fid"
9295                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9296         fi
9297         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9298                 echo "setfacl fid $fid"
9299                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9300                 echo "getfacl fid $fid"
9301                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9302         fi
9303         echo "unlink fid $fid"
9304         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9305         echo "mknod fid $fid"
9306         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9307
9308         fid=[0xf00000400:0x1:0x0]
9309         ffid=$MOUNT/.lustre/fid/$fid
9310
9311         echo "stat non-exist fid $fid"
9312         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9313         echo "write to non-exist fid $fid"
9314         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9315         echo "link new fid $fid"
9316         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9317
9318         mkdir -p $test_dir/$tdir
9319         touch $test_dir/$tdir/$tfile
9320         fid=$($LFS path2fid $test_dir/$tdir)
9321         rc=$?
9322         [ $rc -ne 0 ] &&
9323                 error "error: could not get fid for $test_dir/$dir/$tfile."
9324
9325         ffid=$MOUNT/.lustre/fid/$fid
9326
9327         echo "ls $fid"
9328         ls $ffid > /dev/null || error "ls $ffid failed."
9329         echo "touch $fid/$tfile.1"
9330         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9331
9332         echo "touch $MOUNT/.lustre/fid/$tfile"
9333         touch $MOUNT/.lustre/fid/$tfile && \
9334                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9335
9336         echo "setxattr to $MOUNT/.lustre/fid"
9337         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9338
9339         echo "listxattr for $MOUNT/.lustre/fid"
9340         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9341
9342         echo "delxattr from $MOUNT/.lustre/fid"
9343         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9344
9345         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9346         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9347                 error "touch invalid fid should fail."
9348
9349         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9350         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9351                 error "touch non-normal fid should fail."
9352
9353         echo "rename $tdir to $MOUNT/.lustre/fid"
9354         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9355                 error "rename to $MOUNT/.lustre/fid should fail."
9356
9357         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9358         then            # LU-3547
9359                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9360                 local new_obf_mode=777
9361
9362                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9363                 chmod $new_obf_mode $DIR/.lustre/fid ||
9364                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9365
9366                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9367                 [ $obf_mode -eq $new_obf_mode ] ||
9368                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9369
9370                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9371                 chmod $old_obf_mode $DIR/.lustre/fid ||
9372                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9373         fi
9374
9375         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9376         fid=$($LFS path2fid $test_dir/$tfile-2)
9377         echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9378         cp /etc/passwd $MOUNT/.lustre/fid/$fid &&
9379                 error "create lov data thru .lustre should fail."
9380         echo "cp /etc/passwd $test_dir/$tfile-2"
9381         cp /etc/passwd $test_dir/$tfile-2 ||
9382                 error "copy to $test_dir/$tfile-2 failed."
9383         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9384         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9385                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9386
9387         rm -rf $test_dir/tfile.lnk
9388         rm -rf $test_dir/$tfile-2
9389 }
9390
9391 test_154A() {
9392         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9393                 skip "Need MDS version at least 2.4.1" && return
9394
9395         touch $DIR/$tfile
9396         local FID=$($LFS path2fid $DIR/$tfile)
9397         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9398
9399         # check that we get the same pathname back
9400         local FOUND=$($LFS fid2path $MOUNT $FID)
9401         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9402         [ "$FOUND" != "$DIR/$tfile" ] &&
9403                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9404
9405         rm -rf $DIR/$tfile
9406 }
9407 run_test 154A "lfs path2fid and fid2path basic checks"
9408
9409 test_154a() {
9410         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9411         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9412                 { skip "Need MDS version at least 2.2.51"; return 0; }
9413         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9414
9415         cp /etc/hosts $DIR/$tfile
9416
9417         fid=$($LFS path2fid $DIR/$tfile)
9418         rc=$?
9419         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9420
9421         dot_lustre_fid_permission_check "$fid" $DIR ||
9422                 error "dot lustre permission check $fid failed"
9423
9424         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9425
9426         touch $MOUNT/.lustre/file &&
9427                 error "creation is not allowed under .lustre"
9428
9429         mkdir $MOUNT/.lustre/dir &&
9430                 error "mkdir is not allowed under .lustre"
9431
9432         rm -rf $DIR/$tfile
9433 }
9434 run_test 154a "Open-by-FID"
9435
9436 test_154b() {
9437         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9438         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9439                 { skip "Need MDS version at least 2.2.51"; return 0; }
9440
9441         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9442
9443         local remote_dir=$DIR/$tdir/remote_dir
9444         local MDTIDX=1
9445         local rc=0
9446
9447         mkdir -p $DIR/$tdir
9448         $LFS mkdir -i $MDTIDX -c $MDSCOUNT $remote_dir ||
9449                 error "create remote directory failed"
9450
9451         cp /etc/hosts $remote_dir/$tfile
9452
9453         fid=$($LFS path2fid $remote_dir/$tfile)
9454         rc=$?
9455         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9456
9457         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9458                 error "dot lustre permission check $fid failed"
9459         rm -rf $DIR/$tdir
9460 }
9461 run_test 154b "Open-by-FID for remote directory"
9462
9463 test_154c() {
9464         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9465                 skip "Need MDS version at least 2.4.1" && return
9466
9467         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9468         local FID1=$($LFS path2fid $DIR/$tfile.1)
9469         local FID2=$($LFS path2fid $DIR/$tfile.2)
9470         local FID3=$($LFS path2fid $DIR/$tfile.3)
9471
9472         local N=1
9473         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9474                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9475                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9476                 local want=FID$N
9477                 [ "$FID" = "${!want}" ] ||
9478                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9479                 N=$((N + 1))
9480         done
9481
9482         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9483                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9484                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9485                 N=$((N + 1))
9486         done
9487 }
9488 run_test 154c "lfs path2fid and fid2path multiple arguments"
9489
9490 test_154d() {
9491         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9492                 skip "Need MDS version at least 2.5.53" && return
9493
9494         if remote_mds; then
9495                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9496         else
9497                 nid="0@lo"
9498         fi
9499         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9500         local fd
9501         local cmd
9502
9503         rm -f $DIR/$tfile
9504         touch $DIR/$tfile
9505
9506         fid=$($LFS path2fid $DIR/$tfile)
9507         # Open the file
9508         fd=$(free_fd)
9509         cmd="exec $fd<$DIR/$tfile"
9510         eval $cmd
9511         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9512         echo $fid_list | grep $fid
9513         rc=$?
9514
9515         cmd="exec $fd>/dev/null"
9516         eval $cmd
9517         if [ $rc -ne 0 ]; then
9518                 error "FID $fid not found in open files list $fid_list"
9519         fi
9520 }
9521 run_test 154d "Verify open file fid"
9522
9523 test_155_small_load() {
9524     local temp=$TMP/$tfile
9525     local file=$DIR/$tfile
9526
9527     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9528         error "dd of=$temp bs=6096 count=1 failed"
9529     cp $temp $file
9530     cancel_lru_locks osc
9531     cmp $temp $file || error "$temp $file differ"
9532
9533     $TRUNCATE $temp 6000
9534     $TRUNCATE $file 6000
9535     cmp $temp $file || error "$temp $file differ (truncate1)"
9536
9537     echo "12345" >>$temp
9538     echo "12345" >>$file
9539     cmp $temp $file || error "$temp $file differ (append1)"
9540
9541     echo "12345" >>$temp
9542     echo "12345" >>$file
9543     cmp $temp $file || error "$temp $file differ (append2)"
9544
9545     rm -f $temp $file
9546     true
9547 }
9548
9549 test_155_big_load() {
9550     remote_ost_nodsh && skip "remote OST with nodsh" && return
9551     local temp=$TMP/$tfile
9552     local file=$DIR/$tfile
9553
9554     free_min_max
9555     local cache_size=$(do_facet ost$((MAXI+1)) \
9556         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9557     local large_file_size=$((cache_size * 2))
9558
9559     echo "OSS cache size: $cache_size KB"
9560     echo "Large file size: $large_file_size KB"
9561
9562     [ $MAXV -le $large_file_size ] && \
9563         skip_env "max available OST size needs > $large_file_size KB" && \
9564         return 0
9565
9566     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9567
9568     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9569         error "dd of=$temp bs=$large_file_size count=1k failed"
9570     cp $temp $file
9571     ls -lh $temp $file
9572     cancel_lru_locks osc
9573     cmp $temp $file || error "$temp $file differ"
9574
9575     rm -f $temp $file
9576     true
9577 }
9578
9579 test_155a() {
9580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9581         set_cache read on
9582         set_cache writethrough on
9583         test_155_small_load
9584 }
9585 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
9586
9587 test_155b() {
9588         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9589         set_cache read on
9590         set_cache writethrough off
9591         test_155_small_load
9592 }
9593 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
9594
9595 test_155c() {
9596         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9597         set_cache read off
9598         set_cache writethrough on
9599         test_155_small_load
9600 }
9601 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
9602
9603 test_155d() {
9604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9605         set_cache read off
9606         set_cache writethrough off
9607         test_155_small_load
9608 }
9609 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
9610
9611 test_155e() {
9612         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9613         set_cache read on
9614         set_cache writethrough on
9615         test_155_big_load
9616 }
9617 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
9618
9619 test_155f() {
9620         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9621         set_cache read on
9622         set_cache writethrough off
9623         test_155_big_load
9624 }
9625 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
9626
9627 test_155g() {
9628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9629         set_cache read off
9630         set_cache writethrough on
9631         test_155_big_load
9632 }
9633 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
9634
9635 test_155h() {
9636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9637         set_cache read off
9638         set_cache writethrough off
9639         test_155_big_load
9640 }
9641 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
9642
9643 test_156() {
9644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9645         local CPAGES=3
9646         local BEFORE
9647         local AFTER
9648         local file="$DIR/$tfile"
9649
9650         [ "$(facet_fstype ost1)" = "zfs" ] &&
9651                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
9652                 return
9653
9654         roc_hit_init
9655
9656     log "Turn on read and write cache"
9657     set_cache read on
9658     set_cache writethrough on
9659
9660     log "Write data and read it back."
9661     log "Read should be satisfied from the cache."
9662     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9663     BEFORE=`roc_hit`
9664     cancel_lru_locks osc
9665     cat $file >/dev/null
9666     AFTER=`roc_hit`
9667     if ! let "AFTER - BEFORE == CPAGES"; then
9668         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9669     else
9670         log "cache hits:: before: $BEFORE, after: $AFTER"
9671     fi
9672
9673     log "Read again; it should be satisfied from the cache."
9674     BEFORE=$AFTER
9675     cancel_lru_locks osc
9676     cat $file >/dev/null
9677     AFTER=`roc_hit`
9678     if ! let "AFTER - BEFORE == CPAGES"; then
9679         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9680     else
9681         log "cache hits:: before: $BEFORE, after: $AFTER"
9682     fi
9683
9684
9685     log "Turn off the read cache and turn on the write cache"
9686     set_cache read off
9687     set_cache writethrough on
9688
9689     log "Read again; it should be satisfied from the cache."
9690     BEFORE=`roc_hit`
9691     cancel_lru_locks osc
9692     cat $file >/dev/null
9693     AFTER=`roc_hit`
9694     if ! let "AFTER - BEFORE == CPAGES"; then
9695         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9696     else
9697         log "cache hits:: before: $BEFORE, after: $AFTER"
9698     fi
9699
9700     log "Read again; it should not be satisfied from the cache."
9701     BEFORE=$AFTER
9702     cancel_lru_locks osc
9703     cat $file >/dev/null
9704     AFTER=`roc_hit`
9705     if ! let "AFTER - BEFORE == 0"; then
9706         error "IN CACHE: before: $BEFORE, after: $AFTER"
9707     else
9708         log "cache hits:: before: $BEFORE, after: $AFTER"
9709     fi
9710
9711     log "Write data and read it back."
9712     log "Read should be satisfied from the cache."
9713     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9714     BEFORE=`roc_hit`
9715     cancel_lru_locks osc
9716     cat $file >/dev/null
9717     AFTER=`roc_hit`
9718     if ! let "AFTER - BEFORE == CPAGES"; then
9719         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9720     else
9721         log "cache hits:: before: $BEFORE, after: $AFTER"
9722     fi
9723
9724     log "Read again; it should not be satisfied from the cache."
9725     BEFORE=$AFTER
9726     cancel_lru_locks osc
9727     cat $file >/dev/null
9728     AFTER=`roc_hit`
9729     if ! let "AFTER - BEFORE == 0"; then
9730         error "IN CACHE: before: $BEFORE, after: $AFTER"
9731     else
9732         log "cache hits:: before: $BEFORE, after: $AFTER"
9733     fi
9734
9735
9736     log "Turn off read and write cache"
9737     set_cache read off
9738     set_cache writethrough off
9739
9740     log "Write data and read it back"
9741     log "It should not be satisfied from the cache."
9742     rm -f $file
9743     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9744     cancel_lru_locks osc
9745     BEFORE=`roc_hit`
9746     cat $file >/dev/null
9747     AFTER=`roc_hit`
9748         if ! let "AFTER - BEFORE == 0"; then
9749                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9750         else
9751                 log "cache hits:: before: $BEFORE, after: $AFTER"
9752         fi
9753
9754     log "Turn on the read cache and turn off the write cache"
9755     set_cache read on
9756     set_cache writethrough off
9757
9758     log "Write data and read it back"
9759     log "It should not be satisfied from the cache."
9760     rm -f $file
9761     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
9762     BEFORE=`roc_hit`
9763     cancel_lru_locks osc
9764     cat $file >/dev/null
9765     AFTER=`roc_hit`
9766         if ! let "AFTER - BEFORE == 0"; then
9767                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
9768         else
9769                 log "cache hits:: before: $BEFORE, after: $AFTER"
9770         fi
9771
9772     log "Read again; it should be satisfied from the cache."
9773     BEFORE=`roc_hit`
9774     cancel_lru_locks osc
9775     cat $file >/dev/null
9776     AFTER=`roc_hit`
9777     if ! let "AFTER - BEFORE == CPAGES"; then
9778         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9779     else
9780         log "cache hits:: before: $BEFORE, after: $AFTER"
9781     fi
9782
9783     rm -f $file
9784 }
9785 run_test 156 "Verification of tunables ============================"
9786
9787 #Changelogs
9788 err17935 () {
9789         if [[ $MDSCOUNT -gt 1 ]]; then
9790                 error_ignore bz17935 $*
9791         else
9792                 error $*
9793         fi
9794 }
9795
9796 changelog_chmask()
9797 {
9798         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
9799
9800         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
9801
9802         if [ $MASK -eq 1 ]; then
9803                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
9804         else
9805                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
9806         fi
9807 }
9808
9809 changelog_extract_field() {
9810         local mdt=$1
9811         local cltype=$2
9812         local file=$3
9813         local identifier=$4
9814
9815         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
9816                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
9817                 tail -1
9818 }
9819
9820 test_160a() {
9821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9822         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9823         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9824                 { skip "Need MDS version at least 2.2.0"; return; }
9825
9826         local CL_USERS="mdd.$MDT0.changelog_users"
9827         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9828         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9829         echo "Registered as changelog user $USER"
9830         $GET_CL_USERS | grep -q $USER ||
9831                 error "User $USER not found in changelog_users"
9832
9833         # change something
9834         test_mkdir -p $DIR/$tdir/pics/2008/zachy
9835         touch $DIR/$tdir/pics/2008/zachy/timestamp
9836         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
9837         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
9838         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
9839         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
9840         rm $DIR/$tdir/pics/desktop.jpg
9841
9842         $LFS changelog $MDT0 | tail -5
9843
9844         echo "verifying changelog mask"
9845         changelog_chmask "MKDIR"
9846         changelog_chmask "CLOSE"
9847
9848         test_mkdir -p $DIR/$tdir/pics/zach/sofia
9849         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9850
9851         changelog_chmask "MKDIR"
9852         changelog_chmask "CLOSE"
9853
9854         test_mkdir -p $DIR/$tdir/pics/2008/sofia
9855         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
9856
9857         $LFS changelog $MDT0
9858         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
9859         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
9860         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
9861         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
9862
9863         # verify contents
9864         echo "verifying target fid"
9865         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
9866         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
9867         [ "$fidc" == "$fidf" ] ||
9868                 err17935 "fid in changelog $fidc != file fid $fidf"
9869         echo "verifying parent fid"
9870         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
9871         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
9872         [ "$fidc" == "$fidf" ] ||
9873                 err17935 "pfid in changelog $fidc != dir fid $fidf"
9874
9875         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9876         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
9877         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
9878         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
9879         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
9880                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
9881
9882         MIN_REC=$($GET_CL_USERS |
9883                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
9884         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
9885         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
9886         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
9887                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
9888
9889         # LU-3446 changelog index reset on MDT restart
9890         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
9891         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9892         $LFS changelog_clear $MDT0 $USER 0
9893         stop $SINGLEMDS || error "Fail to stop MDT."
9894         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
9895         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9896         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
9897         [ $CUR_REC1 == $CUR_REC2 ] ||
9898                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
9899
9900         echo "verifying user deregister"
9901         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9902         $GET_CL_USERS | grep -q $USER &&
9903                 error "User $USER still in changelog_users"
9904
9905         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
9906         if [ $USERS -eq 0 ]; then
9907                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9908                 touch $DIR/$tdir/chloe
9909                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
9910                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
9911                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
9912         else
9913                 echo "$USERS other changelog users; can't verify off"
9914         fi
9915 }
9916 run_test 160a "changelog sanity"
9917
9918 test_160b() { # LU-3587
9919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9920         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9921         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
9922                 { skip "Need MDS version at least 2.2.0"; return; }
9923
9924         local CL_USERS="mdd.$MDT0.changelog_users"
9925         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
9926         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
9927         echo "Registered as changelog user $USER"
9928         $GET_CL_USERS | grep -q $USER ||
9929                 error "User $USER not found in changelog_users"
9930
9931         local LONGNAME1=$(str_repeat a 255)
9932         local LONGNAME2=$(str_repeat b 255)
9933
9934         cd $DIR
9935         echo "creating very long named file"
9936         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
9937         echo "moving very long named file"
9938         mv $LONGNAME1 $LONGNAME2
9939
9940         $LFS changelog $MDT0 | grep RENME
9941
9942         echo "deregistering $USER"
9943         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9944
9945         rm -f $LONGNAME2
9946 }
9947 run_test 160b "Verify that very long rename doesn't crash in changelog"
9948
9949 test_160c() {
9950         local rc=0
9951         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9952
9953         # Registration step
9954         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
9955                 changelog_register -n)
9956
9957         rm -rf $DIR/$tdir
9958         mkdir -p $DIR/$tdir
9959         $MCREATE $DIR/$tdir/foo_160c
9960         changelog_chmask "TRUNC"
9961         $TRUNCATE $DIR/$tdir/foo_160c 200
9962         changelog_chmask "TRUNC"
9963         $TRUNCATE $DIR/$tdir/foo_160c 199
9964         $LFS changelog $MDT0
9965         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
9966         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
9967         $LFS changelog_clear $MDT0 $USER 0
9968
9969         # Deregistration step
9970         echo "deregistering $USER"
9971         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
9972 }
9973 run_test 160c "verify that changelog log catch the truncate event"
9974
9975 test_161a() {
9976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9977         test_mkdir -p -c1 $DIR/$tdir
9978         cp /etc/hosts $DIR/$tdir/$tfile
9979         test_mkdir -c1 $DIR/$tdir/foo1
9980         test_mkdir -c1 $DIR/$tdir/foo2
9981         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
9982         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
9983         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
9984         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
9985         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
9986         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
9987                 $LFS fid2path $DIR $FID
9988                 err17935 "bad link ea"
9989         fi
9990     # middle
9991     rm $DIR/$tdir/foo2/zachary
9992     # last
9993     rm $DIR/$tdir/foo2/thor
9994     # first
9995     rm $DIR/$tdir/$tfile
9996     # rename
9997     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
9998     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
9999         then
10000         $LFS fid2path $DIR $FID
10001         err17935 "bad link rename"
10002     fi
10003     rm $DIR/$tdir/foo2/maggie
10004
10005         # overflow the EA
10006         local longname=filename_avg_len_is_thirty_two_
10007         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10008                 error "failed to hardlink many files"
10009         links=$($LFS fid2path $DIR $FID | wc -l)
10010         echo -n "${links}/1000 links in link EA"
10011         [[ $links -gt 60 ]] ||
10012                 err17935 "expected at least 60 links in link EA"
10013         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10014                 error "failed to unlink many hardlinks"
10015 }
10016 run_test 161a "link ea sanity"
10017
10018 test_161b() {
10019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10020         [ $MDSCOUNT -lt 2 ] &&
10021                 skip "skipping remote directory test" && return
10022         local MDTIDX=1
10023         local remote_dir=$DIR/$tdir/remote_dir
10024
10025         mkdir -p $DIR/$tdir
10026         $LFS mkdir -i $MDTIDX $remote_dir ||
10027                 error "create remote directory failed"
10028
10029         cp /etc/hosts $remote_dir/$tfile
10030         mkdir -p $remote_dir/foo1
10031         mkdir -p $remote_dir/foo2
10032         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10033         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10034         ln $remote_dir/$tfile $remote_dir/foo1/luna
10035         ln $remote_dir/$tfile $remote_dir/foo2/thor
10036
10037         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10038                      tr -d ']')
10039         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10040                 $LFS fid2path $DIR $FID
10041                 err17935 "bad link ea"
10042         fi
10043         # middle
10044         rm $remote_dir/foo2/zachary
10045         # last
10046         rm $remote_dir/foo2/thor
10047         # first
10048         rm $remote_dir/$tfile
10049         # rename
10050         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10051         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10052         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10053                 $LFS fid2path $DIR $FID
10054                 err17935 "bad link rename"
10055         fi
10056         rm $remote_dir/foo2/maggie
10057
10058         # overflow the EA
10059         local longname=filename_avg_len_is_thirty_two_
10060         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10061                 error "failed to hardlink many files"
10062         links=$($LFS fid2path $DIR $FID | wc -l)
10063         echo -n "${links}/1000 links in link EA"
10064         [[ ${links} -gt 60 ]] ||
10065                 err17935 "expected at least 60 links in link EA"
10066         unlinkmany $remote_dir/foo2/$longname 1000 ||
10067         error "failed to unlink many hardlinks"
10068 }
10069 run_test 161b "link ea sanity under remote directory"
10070
10071 test_161c() {
10072         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10073         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10074                 skip "Need MDS version at least 2.1.5" && return
10075
10076         # define CLF_RENAME_LAST 0x0001
10077         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10078         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10079                 changelog_register -n)
10080         rm -rf $DIR/$tdir
10081         mkdir -p $DIR/$tdir
10082         touch $DIR/$tdir/foo_161c
10083         touch $DIR/$tdir/bar_161c
10084         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10085         $LFS changelog $MDT0 | grep RENME
10086         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10087                 cut -f5 -d' ')
10088         $LFS changelog_clear $MDT0 $USER 0
10089         if [ x$flags != "x0x1" ]; then
10090                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10091                         $USER
10092                 error "flag $flags is not 0x1"
10093         fi
10094         echo "rename overwrite a target having nlink = 1," \
10095                 "changelog record has flags of $flags"
10096
10097         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10098         touch $DIR/$tdir/foo_161c
10099         touch $DIR/$tdir/bar_161c
10100         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10101         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10102         $LFS changelog $MDT0 | grep RENME
10103         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10104         $LFS changelog_clear $MDT0 $USER 0
10105         if [ x$flags != "x0x0" ]; then
10106                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10107                         $USER
10108                 error "flag $flags is not 0x0"
10109         fi
10110         echo "rename overwrite a target having nlink > 1," \
10111                 "changelog record has flags of $flags"
10112
10113         # rename doesn't overwrite a target (changelog flag 0x0)
10114         touch $DIR/$tdir/foo_161c
10115         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10116         $LFS changelog $MDT0 | grep RENME
10117         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10118         $LFS changelog_clear $MDT0 $USER 0
10119         if [ x$flags != "x0x0" ]; then
10120                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10121                         $USER
10122                 error "flag $flags is not 0x0"
10123         fi
10124         echo "rename doesn't overwrite a target," \
10125                 "changelog record has flags of $flags"
10126
10127         # define CLF_UNLINK_LAST 0x0001
10128         # unlink a file having nlink = 1 (changelog flag 0x1)
10129         rm -f $DIR/$tdir/foo2_161c
10130         $LFS changelog $MDT0 | grep UNLNK
10131         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10132         $LFS changelog_clear $MDT0 $USER 0
10133         if [ x$flags != "x0x1" ]; then
10134                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10135                         $USER
10136                 error "flag $flags is not 0x1"
10137         fi
10138         echo "unlink a file having nlink = 1," \
10139                 "changelog record has flags of $flags"
10140
10141         # unlink a file having nlink > 1 (changelog flag 0x0)
10142         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10143         rm -f $DIR/$tdir/foobar_161c
10144         $LFS changelog $MDT0 | grep UNLNK
10145         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10146         $LFS changelog_clear $MDT0 $USER 0
10147         if [ x$flags != "x0x0" ]; then
10148                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10149                         $USER
10150                 error "flag $flags is not 0x0"
10151         fi
10152         echo "unlink a file having nlink > 1," \
10153                 "changelog record has flags of $flags"
10154         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10155 }
10156 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10157
10158 check_path() {
10159     local expected=$1
10160     shift
10161     local fid=$2
10162
10163     local path=$(${LFS} fid2path $*)
10164     RC=$?
10165
10166     if [ $RC -ne 0 ]; then
10167         err17935 "path looked up of $expected failed. Error $RC"
10168         return $RC
10169     elif [ "${path}" != "${expected}" ]; then
10170         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10171         return 2
10172     fi
10173     echo "fid $fid resolves to path $path (expected $expected)"
10174 }
10175
10176 test_162() {
10177         # Make changes to filesystem
10178         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10179         test_mkdir -p -c1 $DIR/$tdir/d2
10180         touch $DIR/$tdir/d2/$tfile
10181         touch $DIR/$tdir/d2/x1
10182         touch $DIR/$tdir/d2/x2
10183         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10184         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10185         # regular file
10186         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10187         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0
10188
10189         # softlink
10190         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10191         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10192         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0
10193
10194         # softlink to wrong file
10195         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10196         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10197         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0
10198
10199         # hardlink
10200         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10201         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10202         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10203         # fid2path dir/fsname should both work
10204         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1
10205         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0
10206
10207         # hardlink count: check that there are 2 links
10208         # Doesnt work with CMD yet: 17935
10209         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10210                 err17935 "expected 2 links"
10211
10212         # hardlink indexing: remove the first link
10213         rm $DIR/$tdir/d2/p/q/r/hlink
10214         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0
10215
10216         return 0
10217 }
10218 run_test 162 "path lookup sanity"
10219
10220 test_162b() {
10221         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10222         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10223
10224         mkdir $DIR/$tdir
10225         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10226                                 error "create striped dir failed"
10227
10228         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10229                                         tail -n 1 | awk '{print $2}')
10230         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10231
10232         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10233         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10234
10235         # regular file
10236         for ((i=0;i<5;i++)); do
10237                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10238                         error "get fid for f$i failed"
10239                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
10240
10241                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10242                         error "get fid for d$i failed"
10243                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
10244         done
10245
10246         return 0
10247 }
10248 run_test 162b "striped directory path lookup sanity"
10249
10250 test_169() {
10251         # do directio so as not to populate the page cache
10252         log "creating a 10 Mb file"
10253         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10254         log "starting reads"
10255         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10256         log "truncating the file"
10257         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10258         log "killing dd"
10259         kill %+ || true # reads might have finished
10260         echo "wait until dd is finished"
10261         wait
10262         log "removing the temporary file"
10263         rm -rf $DIR/$tfile || error "tmp file removal failed"
10264 }
10265 run_test 169 "parallel read and truncate should not deadlock"
10266
10267 test_170() {
10268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10269         $LCTL clear     # bug 18514
10270         $LCTL debug_daemon start $TMP/${tfile}_log_good
10271         touch $DIR/$tfile
10272         $LCTL debug_daemon stop
10273         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10274                error "sed failed to read log_good"
10275
10276         $LCTL debug_daemon start $TMP/${tfile}_log_good
10277         rm -rf $DIR/$tfile
10278         $LCTL debug_daemon stop
10279
10280         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10281                error "lctl df log_bad failed"
10282
10283         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10284         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10285
10286         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10287         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10288
10289         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10290                 error "bad_line good_line1 good_line2 are empty"
10291
10292         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10293         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10294         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10295
10296         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10297         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10298         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10299
10300         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10301                 error "bad_line_new good_line_new are empty"
10302
10303         local expected_good=$((good_line1 + good_line2*2))
10304
10305         rm -f $TMP/${tfile}*
10306         # LU-231, short malformed line may not be counted into bad lines
10307         if [ $bad_line -ne $bad_line_new ] &&
10308                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10309                 error "expected $bad_line bad lines, but got $bad_line_new"
10310                 return 1
10311         fi
10312
10313         if [ $expected_good -ne $good_line_new ]; then
10314                 error "expected $expected_good good lines, but got $good_line_new"
10315                 return 2
10316         fi
10317         true
10318 }
10319 run_test 170 "test lctl df to handle corrupted log ====================="
10320
10321 test_171() { # bug20592
10322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10323 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10324         $LCTL set_param fail_loc=0x50e
10325         $LCTL set_param fail_val=3000
10326         multiop_bg_pause $DIR/$tfile O_s || true
10327         local MULTIPID=$!
10328         kill -USR1 $MULTIPID
10329         # cause log dump
10330         sleep 3
10331         wait $MULTIPID
10332         if dmesg | grep "recursive fault"; then
10333                 error "caught a recursive fault"
10334         fi
10335         $LCTL set_param fail_loc=0
10336         true
10337 }
10338 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10339
10340 # it would be good to share it with obdfilter-survey/libecho code
10341 setup_obdecho_osc () {
10342         local rc=0
10343         local ost_nid=$1
10344         local obdfilter_name=$2
10345         echo "Creating new osc for $obdfilter_name on $ost_nid"
10346         # make sure we can find loopback nid
10347         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10348
10349         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10350                            ${obdfilter_name}_osc_UUID || rc=2; }
10351         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10352                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10353         return $rc
10354 }
10355
10356 cleanup_obdecho_osc () {
10357         local obdfilter_name=$1
10358         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10359         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10360         return 0
10361 }
10362
10363 obdecho_test() {
10364         local OBD=$1
10365         local node=$2
10366         local pages=${3:-64}
10367         local rc=0
10368         local id
10369
10370         local count=10
10371         local obd_size=$(get_obd_size $node $OBD)
10372         local page_size=$(get_page_size $node)
10373         if [[ -n "$obd_size" ]]; then
10374                 local new_count=$((obd_size / (pages * page_size / 1024)))
10375                 [[ $new_count -ge $count ]] || count=$new_count
10376         fi
10377
10378         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10379         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10380                            rc=2; }
10381         if [ $rc -eq 0 ]; then
10382             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10383             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10384         fi
10385         echo "New object id is $id"
10386         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10387                            rc=4; }
10388         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10389                            "test_brw $count w v $pages $id" || rc=4; }
10390         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10391                            rc=4; }
10392         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10393                                         "cleanup" || rc=5; }
10394         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10395                                         "detach" || rc=6; }
10396         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10397         return $rc
10398 }
10399
10400 test_180a() {
10401         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10402         remote_ost_nodsh && skip "remote OST with nodsh" && return
10403         local rc=0
10404         local rmmod_local=0
10405
10406         if ! module_loaded obdecho; then
10407             load_module obdecho/obdecho
10408             rmmod_local=1
10409         fi
10410
10411         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10412         local host=$(lctl get_param -n osc.$osc.import |
10413                              awk '/current_connection:/ {print $2}' )
10414         local target=$(lctl get_param -n osc.$osc.import |
10415                              awk '/target:/ {print $2}' )
10416         target=${target%_UUID}
10417
10418         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10419         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10420         [[ -n $target ]] && cleanup_obdecho_osc $target
10421         [ $rmmod_local -eq 1 ] && rmmod obdecho
10422         return $rc
10423 }
10424 run_test 180a "test obdecho on osc"
10425
10426 test_180b() {
10427         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10428         remote_ost_nodsh && skip "remote OST with nodsh" && return
10429         local rc=0
10430         local rmmod_remote=0
10431
10432         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10433                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10434                       "modprobe obdecho; }" && rmmod_remote=1
10435         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10436         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10437         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10438         return $rc
10439 }
10440 run_test 180b "test obdecho directly on obdfilter"
10441
10442 test_180c() { # LU-2598
10443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10444         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10445                 skip "Need MDS version at least 2.4.0" && return
10446
10447         local rc=0
10448         local rmmod_remote=false
10449         local pages=16384 # 64MB bulk I/O RPC size
10450         local target
10451
10452         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10453                 rmmod_remote=true || error "failed to load module obdecho"
10454
10455         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10456                 head -n1)
10457         if [ -n "$target" ]; then
10458                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10459         else
10460                 echo "there is no obdfilter target on ost1"
10461                 rc=2
10462         fi
10463         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10464         return $rc
10465 }
10466 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10467
10468 test_181() { # bug 22177
10469         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10470         # create enough files to index the directory
10471         createmany -o $DIR/$tdir/foobar 4000
10472         # print attributes for debug purpose
10473         lsattr -d .
10474         # open dir
10475         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10476         MULTIPID=$!
10477         # remove the files & current working dir
10478         unlinkmany $DIR/$tdir/foobar 4000
10479         rmdir $DIR/$tdir
10480         kill -USR1 $MULTIPID
10481         wait $MULTIPID
10482         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10483         return 0
10484 }
10485 run_test 181 "Test open-unlinked dir ========================"
10486
10487 test_182() {
10488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10489         # disable MDC RPC lock wouldn't crash client
10490         local fcount=1000
10491         local tcount=4
10492
10493         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10494 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10495         $LCTL set_param fail_loc=0x804
10496
10497         for (( i=0; i < $tcount; i++ )) ; do
10498                 mkdir $DIR/$tdir/$i
10499                 createmany -o $DIR/$tdir/$i/f- $fcount &
10500         done
10501         wait
10502
10503         for (( i=0; i < $tcount; i++ )) ; do
10504                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10505         done
10506         wait
10507
10508         rm -rf $DIR/$tdir
10509
10510         $LCTL set_param fail_loc=0
10511 }
10512 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10513
10514 test_183() { # LU-2275
10515         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10516                 skip "Need MDS version at least 2.3.56" && return
10517
10518         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10519         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10520         echo aaa > $DIR/$tdir/$tfile
10521
10522 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10523         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10524
10525         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10526         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
10527
10528         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
10529
10530         # Flush negative dentry cache
10531         touch $DIR/$tdir/$tfile
10532
10533         # We are not checking for any leaked references here, they'll
10534         # become evident next time we do cleanup with module unload.
10535         rm -rf $DIR/$tdir
10536 }
10537 run_test 183 "No crash or request leak in case of strange dispositions ========"
10538
10539 # test suite 184 is for LU-2016, LU-2017
10540 test_184a() {
10541         check_swap_layouts_support && return 0
10542
10543         dir0=$DIR/$tdir/$testnum
10544         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
10545         ref1=/etc/passwd
10546         ref2=/etc/group
10547         file1=$dir0/f1
10548         file2=$dir0/f2
10549         $SETSTRIPE -c1 $file1
10550         cp $ref1 $file1
10551         $SETSTRIPE -c2 $file2
10552         cp $ref2 $file2
10553         gen1=$($GETSTRIPE -g $file1)
10554         gen2=$($GETSTRIPE -g $file2)
10555
10556         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
10557         gen=$($GETSTRIPE -g $file1)
10558         [[ $gen1 != $gen ]] ||
10559                 "Layout generation on $file1 does not change"
10560         gen=$($GETSTRIPE -g $file2)
10561         [[ $gen2 != $gen ]] ||
10562                 "Layout generation on $file2 does not change"
10563
10564         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
10565         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
10566 }
10567 run_test 184a "Basic layout swap"
10568
10569 test_184b() {
10570         check_swap_layouts_support && return 0
10571
10572         dir0=$DIR/$tdir/$testnum
10573         mkdir -p $dir0 || error "creating dir $dir0"
10574         file1=$dir0/f1
10575         file2=$dir0/f2
10576         file3=$dir0/f3
10577         dir1=$dir0/d1
10578         dir2=$dir0/d2
10579         mkdir $dir1 $dir2
10580         $SETSTRIPE -c1 $file1
10581         $SETSTRIPE -c2 $file2
10582         $SETSTRIPE -c1 $file3
10583         chown $RUNAS_ID $file3
10584         gen1=$($GETSTRIPE -g $file1)
10585         gen2=$($GETSTRIPE -g $file2)
10586
10587         $LFS swap_layouts $dir1 $dir2 &&
10588                 error "swap of directories layouts should fail"
10589         $LFS swap_layouts $dir1 $file1 &&
10590                 error "swap of directory and file layouts should fail"
10591         $RUNAS $LFS swap_layouts $file1 $file2 &&
10592                 error "swap of file we cannot write should fail"
10593         $LFS swap_layouts $file1 $file3 &&
10594                 error "swap of file with different owner should fail"
10595         /bin/true # to clear error code
10596 }
10597 run_test 184b "Forbidden layout swap (will generate errors)"
10598
10599 test_184c() {
10600         check_swap_layouts_support && return 0
10601
10602         local dir0=$DIR/$tdir/$testnum
10603         mkdir -p $dir0 || error "creating dir $dir0"
10604
10605         local ref1=$dir0/ref1
10606         local ref2=$dir0/ref2
10607         local file1=$dir0/file1
10608         local file2=$dir0/file2
10609         # create a file large enough for the concurent test
10610         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
10611         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
10612         echo "ref file size: ref1($(stat -c %s $ref1))," \
10613              "ref2($(stat -c %s $ref2))"
10614
10615         cp $ref2 $file2
10616         dd if=$ref1 of=$file1 bs=16k &
10617         local DD_PID=$!
10618
10619         # Make sure dd starts to copy file
10620         while [ ! -f $file1 ]; do sleep 0.1; done
10621
10622         $LFS swap_layouts $file1 $file2
10623         local rc=$?
10624         wait $DD_PID
10625         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
10626         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
10627
10628         # how many bytes copied before swapping layout
10629         local copied=$(stat -c %s $file2)
10630         local remaining=$(stat -c %s $ref1)
10631         remaining=$((remaining - copied))
10632         echo "Copied $copied bytes before swapping layout..."
10633
10634         cmp -n $copied $file1 $ref2 | grep differ &&
10635                 error "Content mismatch [0, $copied) of ref2 and file1"
10636         cmp -n $copied $file2 $ref1 ||
10637                 error "Content mismatch [0, $copied) of ref1 and file2"
10638         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
10639                 error "Content mismatch [$copied, EOF) of ref1 and file1"
10640
10641         # clean up
10642         rm -f $ref1 $ref2 $file1 $file2
10643 }
10644 run_test 184c "Concurrent write and layout swap"
10645
10646 test_184d() {
10647         check_swap_layouts_support && return 0
10648         [ -z "$(which getfattr 2>/dev/null)" ] &&
10649                 skip "no getfattr command" && return 0
10650
10651         local file1=$DIR/$tdir/$tfile-1
10652         local file2=$DIR/$tdir/$tfile-2
10653         local file3=$DIR/$tdir/$tfile-3
10654         local lovea1
10655         local lovea2
10656
10657         mkdir -p $DIR/$tdir
10658         touch $file1 || error "create $file1 failed"
10659         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
10660                 error "create $file2 failed"
10661         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
10662                 error "create $file3 failed"
10663         lovea1=$($LFS getstripe $file1 | sed 1d)
10664
10665         $LFS swap_layouts $file2 $file3 ||
10666                 error "swap $file2 $file3 layouts failed"
10667         $LFS swap_layouts $file1 $file2 ||
10668                 error "swap $file1 $file2 layouts failed"
10669
10670         lovea2=$($LFS getstripe $file2 | sed 1d)
10671         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
10672
10673         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
10674         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
10675 }
10676 run_test 184d "allow stripeless layouts swap"
10677
10678
10679 test_185() { # LU-2441
10680         # LU-3553 - no volatile file support in old servers
10681         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
10682                 { skip "Need MDS version at least 2.3.60"; return 0; }
10683
10684         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10685         touch $DIR/$tdir/spoo
10686         local mtime1=$(stat -c "%Y" $DIR/$tdir)
10687         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
10688                 error "cannot create/write a volatile file"
10689         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
10690                 error "FID is still valid after close"
10691
10692         multiop_bg_pause $DIR/$tdir vVw4096_c
10693         local multi_pid=$!
10694
10695         local OLD_IFS=$IFS
10696         IFS=":"
10697         local fidv=($fid)
10698         IFS=$OLD_IFS
10699         # assume that the next FID for this client is sequential, since stdout
10700         # is unfortunately eaten by multiop_bg_pause
10701         local n=$((${fidv[1]} + 1))
10702         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
10703         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
10704                 error "FID is missing before close"
10705         kill -USR1 $multi_pid
10706         # 1 second delay, so if mtime change we will see it
10707         sleep 1
10708         local mtime2=$(stat -c "%Y" $DIR/$tdir)
10709         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
10710 }
10711 run_test 185 "Volatile file support"
10712
10713 test_187a() {
10714         local dir0=$DIR/$tdir/$testnum
10715         mkdir -p $dir0 || error "creating dir $dir0"
10716
10717         local file=$dir0/file1
10718         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
10719         local dv1=$($LFS data_version $file)
10720         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
10721         local dv2=$($LFS data_version $file)
10722         [[ $dv1 != $dv2 ]] ||
10723                 error "data version did not change on write $dv1 == $dv2"
10724
10725         # clean up
10726         rm -f $file1
10727 }
10728 run_test 187a "Test data version change"
10729
10730 test_187b() {
10731         local dir0=$DIR/$tdir/$testnum
10732         mkdir -p $dir0 || error "creating dir $dir0"
10733
10734         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
10735         [[ ${DV[0]} != ${DV[1]} ]] ||
10736                 error "data version did not change on write"\
10737                       " ${DV[0]} == ${DV[1]}"
10738
10739         # clean up
10740         rm -f $file1
10741 }
10742 run_test 187b "Test data version change on volatile file"
10743
10744 # OST pools tests
10745 check_file_in_pool()
10746 {
10747         local file=$1
10748         local pool=$2
10749         local tlist="$3"
10750         local res=$($GETSTRIPE $file | grep 0x | cut -f2)
10751         for i in $res
10752         do
10753                 for t in $tlist ; do
10754                         [ "$i" -eq "$t" ] && continue 2
10755                 done
10756
10757                 echo "pool list: $tlist"
10758                 echo "striping: $res"
10759                 error_noexit "$file not allocated in $pool"
10760                 return 1
10761         done
10762         return 0
10763 }
10764
10765 pool_add() {
10766         echo "Creating new pool"
10767         local pool=$1
10768
10769         create_pool $FSNAME.$pool ||
10770                 { error_noexit "No pool created, result code $?"; return 1; }
10771         [ $($LFS pool_list $FSNAME | grep -c $pool) -eq 1 ] ||
10772                 { error_noexit "$pool not in lfs pool_list"; return 2; }
10773 }
10774
10775 pool_add_targets() {
10776         echo "Adding targets to pool"
10777         local pool=$1
10778         local first=$2
10779         local last=$3
10780         local step=${4:-1}
10781
10782         local list=$(seq $first $step $last)
10783
10784         local t=$(for i in $list; do printf "$FSNAME-OST%04x_UUID " $i; done)
10785         do_facet mgs $LCTL pool_add \
10786                         $FSNAME.$pool $FSNAME-OST[$first-$last/$step]
10787         wait_update $HOSTNAME "lctl get_param -n lov.$FSNAME-*.pools.$pool \
10788                         | sort -u | tr '\n' ' ' " "$t" || { 
10789                 error_noexit "Add to pool failed"
10790                 return 1
10791         }
10792         local lfscount=$($LFS pool_list $FSNAME.$pool | grep -c "\-OST")
10793         local addcount=$(((last - first) / step + 1))
10794         [ $lfscount -eq $addcount ] || {
10795                 error_noexit "lfs pool_list bad ost count" \
10796                                                 "$lfscount != $addcount"
10797                 return 2
10798         }
10799 }
10800
10801 pool_set_dir() {
10802         local pool=$1
10803         local tdir=$2
10804         echo "Setting pool on directory $tdir"
10805
10806         $SETSTRIPE -c 2 -p $pool $tdir && return 0
10807
10808         error_noexit "Cannot set pool $pool to $tdir"
10809         return 1
10810 }
10811
10812 pool_check_dir() {
10813         local pool=$1
10814         local tdir=$2
10815         echo "Checking pool on directory $tdir"
10816
10817         local res=$($GETSTRIPE --pool $tdir | sed "s/\s*$//")
10818         [ "$res" = "$pool" ] && return 0
10819
10820         error_noexit "Pool on '$tdir' is '$res', not '$pool'"
10821         return 1
10822 }
10823
10824 pool_dir_rel_path() {
10825         echo "Testing relative path works well"
10826         local pool=$1
10827         local tdir=$2
10828         local root=$3
10829
10830         mkdir -p $root/$tdir/$tdir
10831         cd $root/$tdir
10832         pool_set_dir $pool $tdir          || return 1
10833         pool_set_dir $pool ./$tdir        || return 2
10834         pool_set_dir $pool ../$tdir       || return 3
10835         pool_set_dir $pool ../$tdir/$tdir || return 4
10836         rm -rf $tdir; cd - > /dev/null
10837 }
10838
10839 pool_alloc_files() {
10840         echo "Checking files allocation from directory pool"
10841         local pool=$1
10842         local tdir=$2
10843         local count=$3
10844         local tlist="$4"
10845
10846         local failed=0
10847         for i in $(seq -w 1 $count)
10848         do
10849                 local file=$tdir/file-$i
10850                 touch $file
10851                 check_file_in_pool $file $pool "$tlist" || \
10852                         failed=$((failed + 1))
10853         done
10854         [ "$failed" = 0 ] && return 0
10855
10856         error_noexit "$failed files not allocated in $pool"
10857         return 1
10858 }
10859
10860 pool_create_files() {
10861         echo "Creating files in pool"
10862         local pool=$1
10863         local tdir=$2
10864         local count=$3
10865         local tlist="$4"
10866
10867         mkdir -p $tdir
10868         local failed=0
10869         for i in $(seq -w 1 $count)
10870         do
10871                 local file=$tdir/spoo-$i
10872                 $SETSTRIPE -p $pool $file
10873                 check_file_in_pool $file $pool "$tlist" || \
10874                         failed=$((failed + 1))
10875         done
10876         [ "$failed" = 0 ] && return 0
10877
10878         error_noexit "$failed files not allocated in $pool"
10879         return 1
10880 }
10881
10882 pool_lfs_df() {
10883         echo "Checking 'lfs df' output"
10884         local pool=$1
10885
10886         local t=$($LCTL get_param -n lov.$FSNAME-clilov-*.pools.$pool |
10887                         tr '\n' ' ')
10888         local res=$($LFS df --pool $FSNAME.$pool |
10889                         awk '{print $1}' |
10890                         grep "$FSNAME-OST" |
10891                         tr '\n' ' ')
10892         [ "$res" = "$t" ] && return 0
10893
10894         error_noexit "Pools OSTs '$t' is not '$res' that lfs df reports"
10895         return 1
10896 }
10897
10898 pool_file_rel_path() {
10899         echo "Creating files in a pool with relative pathname"
10900         local pool=$1
10901         local tdir=$2
10902
10903         mkdir -p $tdir ||
10904                 { error_noexit "unable to create $tdir"; return 1 ; }
10905         local file="/..$tdir/$tfile-1"
10906         $SETSTRIPE -p $pool $file ||
10907                 { error_noexit "unable to create $file" ; return 2 ; }
10908
10909         cd $tdir
10910         $SETSTRIPE -p $pool $tfile-2 || {
10911                 error_noexit "unable to create $tfile-2 in $tdir"
10912                 return 3
10913         }
10914 }
10915
10916 pool_remove_first_target() {
10917         echo "Removing first target from a pool"
10918         local pool=$1
10919
10920         local pname="lov.$FSNAME-*.pools.$pool"
10921         local t=$($LCTL get_param -n $pname | head -n1)
10922         do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10923         wait_update $HOSTNAME "lctl get_param -n $pname | grep $t" "" || {
10924                 error_noexit "$t not removed from $FSNAME.$pool"
10925                 return 1
10926         }
10927 }
10928
10929 pool_remove_all_targets() {
10930         echo "Removing all targets from pool"
10931         local pool=$1
10932         local file=$2
10933         local pname="lov.$FSNAME-*.pools.$pool"
10934         for t in $($LCTL get_param -n $pname | sort -u)
10935         do
10936                 do_facet mgs $LCTL pool_remove $FSNAME.$pool $t
10937         done
10938         wait_update $HOSTNAME "lctl get_param -n $pname" "" || {
10939                 error_noexit "Pool $FSNAME.$pool cannot be drained"
10940                 return 1
10941         }
10942         # striping on an empty/nonexistant pool should fall back
10943         # to "pool of everything"
10944         touch $file || {
10945                 error_noexit "failed to use fallback striping for empty pool"
10946                 return 2
10947         }
10948         # setstripe on an empty pool should fail
10949         $SETSTRIPE -p $pool $file 2>/dev/null && {
10950                 error_noexit "expected failure when creating file" \
10951                                                         "with empty pool"
10952                 return 3
10953         }
10954         return 0
10955 }
10956
10957 pool_remove() {
10958         echo "Destroying pool"
10959         local pool=$1
10960         local file=$2
10961
10962         do_facet mgs $LCTL pool_destroy $FSNAME.$pool
10963
10964         sleep 2
10965         # striping on an empty/nonexistant pool should fall back 
10966         # to "pool of everything"
10967         touch $file || {
10968                 error_noexit "failed to use fallback striping for missing pool"
10969                 return 1
10970         }
10971         # setstripe on an empty pool should fail
10972         $SETSTRIPE -p $pool $file 2>/dev/null && {
10973                 error_noexit "expected failure when creating file" \
10974                                                         "with missing pool"
10975                 return 2
10976         }
10977
10978         # get param should return err once pool is gone
10979         if wait_update $HOSTNAME "lctl get_param -n \
10980                 lov.$FSNAME-*.pools.$pool 2>/dev/null || echo foo" "foo"
10981         then
10982                 remove_pool_from_list $FSNAME.$pool
10983                 return 0
10984         fi
10985         error_noexit "Pool $FSNAME.$pool is not destroyed"
10986         return 3
10987 }
10988
10989 test_200() {
10990         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10991         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
10992
10993         local POOL=${POOL:-cea1}
10994         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
10995         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
10996         # Pool OST targets
10997         local first_ost=0
10998         local last_ost=$(($OSTCOUNT - 1))
10999         local ost_step=2
11000         local ost_list=$(seq $first_ost $ost_step $last_ost)
11001         local ost_range="$first_ost $last_ost $ost_step"
11002         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11003         local file_dir=$POOL_ROOT/file_tst
11004
11005         local rc=0
11006         while : ; do
11007                 # former test_200a test_200b
11008                 pool_add $POOL                          || { rc=$? ; break; }
11009                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11010                 # former test_200c test_200d
11011                 mkdir -p $test_path
11012                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11013                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11014                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11015                                                         || { rc=$? ; break; }
11016                 # former test_200e test_200f
11017                 local files=$((OSTCOUNT*3))
11018                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11019                                                         || { rc=$? ; break; }
11020                 pool_create_files $POOL $file_dir $files "$ost_list" \
11021                                                         || { rc=$? ; break; }
11022                 # former test_200g test_200h
11023                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11024                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11025
11026                 # former test_201a test_201b test_201c
11027                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11028
11029                 local f=$test_path/$tfile
11030                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11031                 pool_remove $POOL $f                    || { rc=$? ; break; }
11032                 break
11033         done
11034
11035         cleanup_pools
11036         return $rc
11037 }
11038 run_test 200 "OST pools"
11039
11040 # usage: default_attr <count | size | offset>
11041 default_attr() {
11042         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11043 }
11044
11045 # usage: check_default_stripe_attr
11046 check_default_stripe_attr() {
11047         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11048         case $1 in
11049         --stripe-count|--count)
11050                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11051         --stripe-size|--size)
11052                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11053         --stripe-index|--index)
11054                 EXPECTED=-1;;
11055         *)
11056                 error "unknown getstripe attr '$1'"
11057         esac
11058
11059         [ $ACTUAL != $EXPECTED ] &&
11060                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11061 }
11062
11063 test_204a() {
11064         test_mkdir -p $DIR/$tdir
11065         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11066
11067         check_default_stripe_attr --stripe-count
11068         check_default_stripe_attr --stripe-size
11069         check_default_stripe_attr --stripe-index
11070
11071         return 0
11072 }
11073 run_test 204a "Print default stripe attributes ================="
11074
11075 test_204b() {
11076         test_mkdir -p $DIR/$tdir
11077         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11078
11079         check_default_stripe_attr --stripe-size
11080         check_default_stripe_attr --stripe-index
11081
11082         return 0
11083 }
11084 run_test 204b "Print default stripe size and offset  ==========="
11085
11086 test_204c() {
11087         test_mkdir -p $DIR/$tdir
11088         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11089
11090         check_default_stripe_attr --stripe-count
11091         check_default_stripe_attr --stripe-index
11092
11093         return 0
11094 }
11095 run_test 204c "Print default stripe count and offset ==========="
11096
11097 test_204d() {
11098         test_mkdir -p $DIR/$tdir
11099         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11100
11101         check_default_stripe_attr --stripe-count
11102         check_default_stripe_attr --stripe-size
11103
11104         return 0
11105 }
11106 run_test 204d "Print default stripe count and size ============="
11107
11108 test_204e() {
11109         test_mkdir -p $DIR/$tdir
11110         $SETSTRIPE -d $DIR/$tdir
11111
11112         check_default_stripe_attr --stripe-count --raw
11113         check_default_stripe_attr --stripe-size --raw
11114         check_default_stripe_attr --stripe-index --raw
11115
11116         return 0
11117 }
11118 run_test 204e "Print raw stripe attributes ================="
11119
11120 test_204f() {
11121         test_mkdir -p $DIR/$tdir
11122         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11123
11124         check_default_stripe_attr --stripe-size --raw
11125         check_default_stripe_attr --stripe-index --raw
11126
11127         return 0
11128 }
11129 run_test 204f "Print raw stripe size and offset  ==========="
11130
11131 test_204g() {
11132         test_mkdir -p $DIR/$tdir
11133         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11134
11135         check_default_stripe_attr --stripe-count --raw
11136         check_default_stripe_attr --stripe-index --raw
11137
11138         return 0
11139 }
11140 run_test 204g "Print raw stripe count and offset ==========="
11141
11142 test_204h() {
11143         test_mkdir -p $DIR/$tdir
11144         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11145
11146         check_default_stripe_attr --stripe-count --raw
11147         check_default_stripe_attr --stripe-size --raw
11148
11149         return 0
11150 }
11151 run_test 204h "Print raw stripe count and size ============="
11152
11153 # Figure out which job scheduler is being used, if any,
11154 # or use a fake one
11155 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11156         JOBENV=SLURM_JOB_ID
11157 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11158         JOBENV=LSB_JOBID
11159 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11160         JOBENV=PBS_JOBID
11161 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11162         JOBENV=LOADL_STEP_ID
11163 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11164         JOBENV=JOB_ID
11165 else
11166         JOBENV=FAKE_JOBID
11167 fi
11168
11169 verify_jobstats() {
11170         local cmd=$1
11171         local target=$2
11172
11173         # clear old jobstats
11174         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11175         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11176
11177         # use a new JobID for this test, or we might see an old one
11178         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11179
11180         JOBVAL=${!JOBENV}
11181         log "Test: $cmd"
11182         log "Using JobID environment variable $JOBENV=$JOBVAL"
11183
11184         if [ $JOBENV = "FAKE_JOBID" ]; then
11185                 FAKE_JOBID=$JOBVAL $cmd
11186         else
11187                 $cmd
11188         fi
11189
11190         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11191                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11192                 do_facet $FACET lctl get_param mdt.*.job_stats |
11193                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11194         fi
11195         if [ "$target" = "ost" -o "$target" = "both" ]; then
11196                 FACET=ost1
11197                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11198                         grep $JOBVAL || error "No job stats found on OST $FACET"
11199         fi
11200 }
11201
11202 jobstats_set() {
11203         trap 0
11204         NEW_JOBENV=${1:-$OLD_JOBENV}
11205         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11206         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11207 }
11208
11209 test_205() { # Job stats
11210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11211         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11212                 skip "Server doesn't support jobstats" && return 0
11213         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11214
11215         local cmd
11216         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11217         if [ $OLD_JOBENV != $JOBENV ]; then
11218                 jobstats_set $JOBENV
11219                 trap jobstats_set EXIT
11220         fi
11221
11222         # mkdir
11223         cmd="mkdir $DIR/$tfile"
11224         verify_jobstats "$cmd" "mdt"
11225         # rmdir
11226         cmd="rm -fr $DIR/$tfile"
11227         verify_jobstats "$cmd" "mdt"
11228         # mknod
11229         cmd="mknod $DIR/$tfile c 1 3"
11230         verify_jobstats "$cmd" "mdt"
11231         # unlink
11232         cmd="rm -f $DIR/$tfile"
11233         verify_jobstats "$cmd" "mdt"
11234         # open & close
11235         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11236         verify_jobstats "$cmd" "mdt"
11237         # setattr
11238         cmd="touch $DIR/$tfile"
11239         verify_jobstats "$cmd" "both"
11240         # write
11241         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11242         verify_jobstats "$cmd" "ost"
11243         # read
11244         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11245         verify_jobstats "$cmd" "ost"
11246         # truncate
11247         cmd="$TRUNCATE $DIR/$tfile 0"
11248         verify_jobstats "$cmd" "both"
11249         # rename
11250         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11251         verify_jobstats "$cmd" "mdt"
11252
11253         # cleanup
11254         rm -f $DIR/jobstats_test_rename
11255
11256         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11257 }
11258 run_test 205 "Verify job stats"
11259
11260 # LU-1480, LU-1773 and LU-1657
11261 test_206() {
11262         mkdir -p $DIR/$tdir
11263         $SETSTRIPE -c -1 $DIR/$tdir
11264 #define OBD_FAIL_LOV_INIT 0x1403
11265         $LCTL set_param fail_loc=0xa0001403
11266         $LCTL set_param fail_val=1
11267         touch $DIR/$tdir/$tfile || true
11268 }
11269 run_test 206 "fail lov_init_raid0() doesn't lbug"
11270
11271 test_207a() {
11272         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11273         local fsz=`stat -c %s $DIR/$tfile`
11274         cancel_lru_locks mdc
11275
11276         # do not return layout in getattr intent
11277 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11278         $LCTL set_param fail_loc=0x170
11279         local sz=`stat -c %s $DIR/$tfile`
11280
11281         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11282
11283         rm -rf $DIR/$tfile
11284 }
11285 run_test 207a "can refresh layout at glimpse"
11286
11287 test_207b() {
11288         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11289         local cksum=`md5sum $DIR/$tfile`
11290         local fsz=`stat -c %s $DIR/$tfile`
11291         cancel_lru_locks mdc
11292         cancel_lru_locks osc
11293
11294         # do not return layout in getattr intent
11295 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11296         $LCTL set_param fail_loc=0x171
11297
11298         # it will refresh layout after the file is opened but before read issues
11299         echo checksum is "$cksum"
11300         echo "$cksum" |md5sum -c --quiet || error "file differs"
11301
11302         rm -rf $DIR/$tfile
11303 }
11304 run_test 207b "can refresh layout at open"
11305
11306 test_208() {
11307         # FIXME: in this test suite, only RD lease is used. This is okay
11308         # for now as only exclusive open is supported. After generic lease
11309         # is done, this test suite should be revised. - Jinshan
11310
11311         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11312                 { skip "Need MDS version at least 2.4.52"; return 0; }
11313
11314         echo "==== test 1: verify get lease work"
11315         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11316
11317         echo "==== test 2: verify lease can be broken by upcoming open"
11318         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11319         local PID=$!
11320         sleep 1
11321
11322         $MULTIOP $DIR/$tfile oO_RDONLY:c
11323         kill -USR1 $PID && wait $PID || error "break lease error"
11324
11325         echo "==== test 3: verify lease can't be granted if an open already exists"
11326         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11327         local PID=$!
11328         sleep 1
11329
11330         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11331         kill -USR1 $PID && wait $PID || error "open file error"
11332
11333         echo "==== test 4: lease can sustain over recovery"
11334         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11335         PID=$!
11336         sleep 1
11337
11338         fail mds1
11339
11340         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11341
11342         echo "==== test 5: lease broken can't be regained by replay"
11343         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11344         PID=$!
11345         sleep 1
11346
11347         # open file to break lease and then recovery
11348         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11349         fail mds1
11350
11351         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11352
11353         rm -f $DIR/$tfile
11354 }
11355 run_test 208 "Exclusive open"
11356
11357 test_209() {
11358         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11359                 skip_env "must have disp_stripe" && return
11360
11361         touch $DIR/$tfile
11362         sync; sleep 5; sync;
11363
11364         echo 3 > /proc/sys/vm/drop_caches
11365         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11366
11367         # open/close 500 times
11368         for i in $(seq 500); do
11369                 cat $DIR/$tfile
11370         done
11371
11372         echo 3 > /proc/sys/vm/drop_caches
11373         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11374
11375         echo "before: $req_before, after: $req_after"
11376         [ $((req_after - req_before)) -ge 300 ] &&
11377                 error "open/close requests are not freed"
11378         return 0
11379 }
11380 run_test 209 "read-only open/close requests should be freed promptly"
11381
11382 test_212() {
11383         size=`date +%s`
11384         size=$((size % 8192 + 1))
11385         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11386         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11387         rm -f $DIR/f212 $DIR/f212.xyz
11388 }
11389 run_test 212 "Sendfile test ============================================"
11390
11391 test_213() {
11392         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11393         cancel_lru_locks osc
11394         lctl set_param fail_loc=0x8000040f
11395         # generate a read lock
11396         cat $DIR/$tfile > /dev/null
11397         # write to the file, it will try to cancel the above read lock.
11398         cat /etc/hosts >> $DIR/$tfile
11399 }
11400 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11401
11402 test_214() { # for bug 20133
11403         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11404         for (( i=0; i < 340; i++ )) ; do
11405                 touch $DIR/$tdir/d214c/a$i
11406         done
11407
11408         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11409         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11410         ls $DIR/d214c || error "ls $DIR/d214c failed"
11411         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11412         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11413 }
11414 run_test 214 "hash-indexed directory test - bug 20133"
11415
11416 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11417 create_lnet_proc_files() {
11418         cat /proc/sys/lnet/$1 >$TMP/lnet_$1.out || error "cannot read /proc/sys/lnet/$1"
11419         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11420
11421         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11422         rm -f "$TMP/lnet_$1.sys_tmp"
11423 }
11424
11425 # counterpart of create_lnet_proc_files
11426 remove_lnet_proc_files() {
11427         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11428 }
11429
11430 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11431 # 3rd arg as regexp for body
11432 check_lnet_proc_stats() {
11433         local l=$(cat "$TMP/lnet_$1" |wc -l)
11434         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11435
11436         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11437 }
11438
11439 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11440 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11441 # optional and can be regexp for 2nd line (lnet.routes case)
11442 check_lnet_proc_entry() {
11443         local blp=2          # blp stands for 'position of 1st line of body'
11444         [ -z "$5" ] || blp=3 # lnet.routes case
11445
11446         local l=$(cat "$TMP/lnet_$1" |wc -l)
11447         # subtracting one from $blp because the body can be empty
11448         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11449
11450         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11451                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11452
11453         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11454                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11455
11456         # bail out if any unexpected line happened
11457         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11458         [ "$?" != 0 ] || error "$2 misformatted"
11459 }
11460
11461 test_215() { # for bugs 18102, 21079, 21517
11462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11463         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11464         local P='[1-9][0-9]*'           # positive numeric
11465         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11466         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11467         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11468         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11469
11470         local L1 # regexp for 1st line
11471         local L2 # regexp for 2nd line (optional)
11472         local BR # regexp for the rest (body)
11473
11474         # /proc/sys/lnet/stats should look as 11 space-separated non-negative numerics
11475         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11476         create_lnet_proc_files "stats"
11477         check_lnet_proc_stats "stats.out" "/proc/sys/lnet/stats" "$BR"
11478         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11479         remove_lnet_proc_files "stats"
11480
11481         # /proc/sys/lnet/routes should look like this:
11482         # Routing disabled/enabled
11483         # net hops priority state router
11484         # where net is a string like tcp0, hops > 0, priority >= 0,
11485         # state is up/down,
11486         # router is a string like 192.168.1.1@tcp2
11487         L1="^Routing (disabled|enabled)$"
11488         L2="^net +hops +priority +state +router$"
11489         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11490         create_lnet_proc_files "routes"
11491         check_lnet_proc_entry "routes.out" "/proc/sys/lnet/routes" "$BR" "$L1" "$L2"
11492         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11493         remove_lnet_proc_files "routes"
11494
11495         # /proc/sys/lnet/routers should look like this:
11496         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11497         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11498         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11499         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11500         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11501         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11502         create_lnet_proc_files "routers"
11503         check_lnet_proc_entry "routers.out" "/proc/sys/lnet/routers" "$BR" "$L1"
11504         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11505         remove_lnet_proc_files "routers"
11506
11507         # /proc/sys/lnet/peers should look like this:
11508         # nid refs state last max rtr min tx min queue
11509         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11510         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11511         # numeric (0 or >0 or <0), queue >= 0.
11512         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11513         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11514         create_lnet_proc_files "peers"
11515         check_lnet_proc_entry "peers.out" "/proc/sys/lnet/peers" "$BR" "$L1"
11516         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11517         remove_lnet_proc_files "peers"
11518
11519         # /proc/sys/lnet/buffers  should look like this:
11520         # pages count credits min
11521         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11522         L1="^pages +count +credits +min$"
11523         BR="^ +$N +$N +$I +$I$"
11524         create_lnet_proc_files "buffers"
11525         check_lnet_proc_entry "buffers.out" "/proc/sys/lnet/buffers" "$BR" "$L1"
11526         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11527         remove_lnet_proc_files "buffers"
11528
11529         # /proc/sys/lnet/nis should look like this:
11530         # nid status alive refs peer rtr max tx min
11531         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11532         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11533         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11534         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11535         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11536         create_lnet_proc_files "nis"
11537         check_lnet_proc_entry "nis.out" "/proc/sys/lnet/nis" "$BR" "$L1"
11538         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11539         remove_lnet_proc_files "nis"
11540
11541         # can we successfully write to /proc/sys/lnet/stats?
11542         echo "0" >/proc/sys/lnet/stats || error "cannot write to /proc/sys/lnet/stats"
11543         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11544 }
11545 run_test 215 "/proc/sys/lnet exists and has proper content - bugs 18102, 21079, 21517"
11546
11547 test_216() { # bug 20317
11548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11549         remote_ost_nodsh && skip "remote OST with nodsh" && return
11550
11551         local node
11552         local facets=$(get_facets OST)
11553         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11554
11555         save_lustre_params client "osc.*.contention_seconds" > $p
11556         save_lustre_params $facets \
11557                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11558         save_lustre_params $facets \
11559                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11560         save_lustre_params $facets \
11561                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11562         clear_osc_stats
11563
11564         # agressive lockless i/o settings
11565         for node in $(osts_nodes); do
11566                 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'
11567         done
11568         lctl set_param -n osc.*.contention_seconds 60
11569
11570         $DIRECTIO write $DIR/$tfile 0 10 4096
11571         $CHECKSTAT -s 40960 $DIR/$tfile
11572
11573         # disable lockless i/o
11574         for node in $(osts_nodes); do
11575                 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'
11576         done
11577         lctl set_param -n osc.*.contention_seconds 0
11578         clear_osc_stats
11579
11580         dd if=/dev/zero of=$DIR/$tfile count=0
11581         $CHECKSTAT -s 0 $DIR/$tfile
11582
11583         restore_lustre_params <$p
11584         rm -f $p
11585         rm $DIR/$tfile
11586 }
11587 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11588
11589 test_217() { # bug 22430
11590         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11591         local node
11592         local nid
11593
11594         for node in $(nodes_list); do
11595                 nid=$(host_nids_address $node $NETTYPE)
11596                 if [[ $nid = *-* ]] ; then
11597                         echo "lctl ping $nid@$NETTYPE"
11598                         lctl ping $nid@$NETTYPE
11599                 else
11600                         echo "skipping $node (no hyphen detected)"
11601                 fi
11602         done
11603 }
11604 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11605
11606 test_218() {
11607        # do directio so as not to populate the page cache
11608        log "creating a 10 Mb file"
11609        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11610        log "starting reads"
11611        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11612        log "truncating the file"
11613        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11614        log "killing dd"
11615        kill %+ || true # reads might have finished
11616        echo "wait until dd is finished"
11617        wait
11618        log "removing the temporary file"
11619        rm -rf $DIR/$tfile || error "tmp file removal failed"
11620 }
11621 run_test 218 "parallel read and truncate should not deadlock ======================="
11622
11623 test_219() {
11624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11625         # write one partial page
11626         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11627         # set no grant so vvp_io_commit_write will do sync write
11628         $LCTL set_param fail_loc=0x411
11629         # write a full page at the end of file
11630         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11631
11632         $LCTL set_param fail_loc=0
11633         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11634         $LCTL set_param fail_loc=0x411
11635         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11636
11637         # LU-4201
11638         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11639         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11640 }
11641 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11642
11643 test_220() { #LU-325
11644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11645         remote_ost_nodsh && skip "remote OST with nodsh" && return
11646         local OSTIDX=0
11647
11648         test_mkdir -p $DIR/$tdir
11649         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11650                 awk '{print $2}' | sed -e 's/_UUID$//')
11651
11652         # on the mdt's osc
11653         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11654         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11655                         osc.$mdtosc_proc1.prealloc_last_id)
11656         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11657                         osc.$mdtosc_proc1.prealloc_next_id)
11658
11659         $LFS df -i
11660
11661         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11662         #define OBD_FAIL_OST_ENOINO              0x229
11663         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11664         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11665         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11666
11667         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11668
11669         MDSOBJS=$((last_id - next_id))
11670         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11671
11672         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11673         echo "OST still has $count kbytes free"
11674
11675         echo "create $MDSOBJS files @next_id..."
11676         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11677
11678         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11679                         osc.$mdtosc_proc1.prealloc_last_id)
11680         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11681                         osc.$mdtosc_proc1.prealloc_next_id)
11682
11683         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11684         $LFS df -i
11685
11686         echo "cleanup..."
11687
11688         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11689         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11690
11691         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11692         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11693         echo "unlink $MDSOBJS files @$next_id..."
11694         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11695 }
11696 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11697
11698 test_221() {
11699         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11700         dd if=`which date` of=$MOUNT/date oflag=sync
11701         chmod +x $MOUNT/date
11702
11703         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11704         $LCTL set_param fail_loc=0x80001401
11705
11706         $MOUNT/date > /dev/null
11707         rm -f $MOUNT/date
11708 }
11709 run_test 221 "make sure fault and truncate race to not cause OOM"
11710
11711 test_222a () {
11712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11713        rm -rf $DIR/$tdir
11714        test_mkdir -p $DIR/$tdir
11715        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11716        createmany -o $DIR/$tdir/$tfile 10
11717        cancel_lru_locks mdc
11718        cancel_lru_locks osc
11719        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11720        $LCTL set_param fail_loc=0x31a
11721        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
11722        $LCTL set_param fail_loc=0
11723        rm -r $DIR/$tdir
11724 }
11725 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
11726
11727 test_222b () {
11728         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11729        rm -rf $DIR/$tdir
11730        test_mkdir -p $DIR/$tdir
11731        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11732        createmany -o $DIR/$tdir/$tfile 10
11733        cancel_lru_locks mdc
11734        cancel_lru_locks osc
11735        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
11736        $LCTL set_param fail_loc=0x31a
11737        rm -r $DIR/$tdir || "AGL for rmdir failed"
11738        $LCTL set_param fail_loc=0
11739 }
11740 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
11741
11742 test_223 () {
11743         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11744        rm -rf $DIR/$tdir
11745        test_mkdir -p $DIR/$tdir
11746        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11747        createmany -o $DIR/$tdir/$tfile 10
11748        cancel_lru_locks mdc
11749        cancel_lru_locks osc
11750        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
11751        $LCTL set_param fail_loc=0x31b
11752        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
11753        $LCTL set_param fail_loc=0
11754        rm -r $DIR/$tdir
11755 }
11756 run_test 223 "osc reenqueue if without AGL lock granted ======================="
11757
11758 test_224a() { # LU-1039, MRP-303
11759         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11760         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
11761         $LCTL set_param fail_loc=0x508
11762         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
11763         $LCTL set_param fail_loc=0
11764         df $DIR
11765 }
11766 run_test 224a "Don't panic on bulk IO failure"
11767
11768 test_224b() { # LU-1039, MRP-303
11769         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11770         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
11771         cancel_lru_locks osc
11772         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
11773         $LCTL set_param fail_loc=0x515
11774         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
11775         $LCTL set_param fail_loc=0
11776         df $DIR
11777 }
11778 run_test 224b "Don't panic on bulk IO failure"
11779
11780 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
11781 test_225a () {
11782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11783         if [ -z ${MDSSURVEY} ]; then
11784               skip_env "mds-survey not found" && return
11785         fi
11786
11787         [ $MDSCOUNT -ge 2 ] &&
11788                 skip "skipping now for more than one MDT" && return
11789
11790        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11791             { skip "Need MDS version at least 2.2.51"; return; }
11792
11793        local mds=$(facet_host $SINGLEMDS)
11794        local target=$(do_nodes $mds 'lctl dl' | \
11795                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11796
11797        local cmd1="file_count=1000 thrhi=4"
11798        local cmd2="dir_count=2 layer=mdd stripe_count=0"
11799        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11800        local cmd="$cmd1 $cmd2 $cmd3"
11801
11802        rm -f ${TMP}/mds_survey*
11803        echo + $cmd
11804        eval $cmd || error "mds-survey with zero-stripe failed"
11805        cat ${TMP}/mds_survey*
11806        rm -f ${TMP}/mds_survey*
11807 }
11808 run_test 225a "Metadata survey sanity with zero-stripe"
11809
11810 test_225b () {
11811         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11812
11813         if [ -z ${MDSSURVEY} ]; then
11814               skip_env "mds-survey not found" && return
11815         fi
11816         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
11817             { skip "Need MDS version at least 2.2.51"; return; }
11818
11819         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
11820               skip_env "Need to mount OST to test" && return
11821         fi
11822
11823         [ $MDSCOUNT -ge 2 ] &&
11824                 skip "skipping now for more than one MDT" && return
11825        local mds=$(facet_host $SINGLEMDS)
11826        local target=$(do_nodes $mds 'lctl dl' | \
11827                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
11828
11829        local cmd1="file_count=1000 thrhi=4"
11830        local cmd2="dir_count=2 layer=mdd stripe_count=1"
11831        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
11832        local cmd="$cmd1 $cmd2 $cmd3"
11833
11834        rm -f ${TMP}/mds_survey*
11835        echo + $cmd
11836        eval $cmd || error "mds-survey with stripe_count failed"
11837        cat ${TMP}/mds_survey*
11838        rm -f ${TMP}/mds_survey*
11839 }
11840 run_test 225b "Metadata survey sanity with stripe_count = 1"
11841
11842 mcreate_path2fid () {
11843         local mode=$1
11844         local major=$2
11845         local minor=$3
11846         local name=$4
11847         local desc=$5
11848         local path=$DIR/$tdir/$name
11849         local fid
11850         local rc
11851         local fid_path
11852
11853         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
11854                 error "cannot create $desc"
11855
11856         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
11857         rc=$?
11858         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
11859
11860         fid_path=$($LFS fid2path $MOUNT $fid)
11861         rc=$?
11862         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
11863
11864         [ "$path" == "$fid_path" ] ||
11865                 error "fid2path returned $fid_path, expected $path"
11866
11867         echo "pass with $path and $fid"
11868 }
11869
11870 test_226a () {
11871         rm -rf $DIR/$tdir
11872         mkdir -p $DIR/$tdir
11873
11874         mcreate_path2fid 0010666 0 0 fifo "FIFO"
11875         mcreate_path2fid 0020666 1 3 null "character special file (null)"
11876         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
11877         mcreate_path2fid 0040666 0 0 dir "directory"
11878         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
11879         mcreate_path2fid 0100666 0 0 file "regular file"
11880         mcreate_path2fid 0120666 0 0 link "symbolic link"
11881         mcreate_path2fid 0140666 0 0 sock "socket"
11882 }
11883 run_test 226a "call path2fid and fid2path on files of all type"
11884
11885 test_226b () {
11886         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11887         rm -rf $DIR/$tdir
11888         local MDTIDX=1
11889
11890         mkdir -p $DIR/$tdir
11891         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
11892                 error "create remote directory failed"
11893         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
11894         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
11895                                 "character special file (null)"
11896         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
11897                                 "character special file (no device)"
11898         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
11899         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
11900                                 "block special file (loop)"
11901         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
11902         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
11903         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
11904 }
11905 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
11906
11907 # LU-1299 Executing or running ldd on a truncated executable does not
11908 # cause an out-of-memory condition.
11909 test_227() {
11910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11911         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
11912         dd if=$(which date) of=$MOUNT/date bs=1k count=1
11913         chmod +x $MOUNT/date
11914
11915         $MOUNT/date > /dev/null
11916         ldd $MOUNT/date > /dev/null
11917         rm -f $MOUNT/date
11918 }
11919 run_test 227 "running truncated executable does not cause OOM"
11920
11921 # LU-1512 try to reuse idle OI blocks
11922 test_228a() {
11923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11924         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11925                 skip "non-ldiskfs backend" && return
11926
11927         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11928         local myDIR=$DIR/$tdir
11929
11930         mkdir -p $myDIR
11931         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11932         $LCTL set_param fail_loc=0x80001002
11933         createmany -o $myDIR/t- 10000
11934         $LCTL set_param fail_loc=0
11935         # The guard is current the largest FID holder
11936         touch $myDIR/guard
11937         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11938                     tr -d '[')
11939         local IDX=$(($SEQ % 64))
11940
11941         do_facet $SINGLEMDS sync
11942         # Make sure journal flushed.
11943         sleep 6
11944         local blk1=$(do_facet $SINGLEMDS \
11945                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11946                      grep Blockcount | awk '{print $4}')
11947
11948         # Remove old files, some OI blocks will become idle.
11949         unlinkmany $myDIR/t- 10000
11950         # Create new files, idle OI blocks should be reused.
11951         createmany -o $myDIR/t- 2000
11952         do_facet $SINGLEMDS sync
11953         # Make sure journal flushed.
11954         sleep 6
11955         local blk2=$(do_facet $SINGLEMDS \
11956                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11957                      grep Blockcount | awk '{print $4}')
11958
11959         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
11960 }
11961 run_test 228a "try to reuse idle OI blocks"
11962
11963 test_228b() {
11964         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11965         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
11966                 skip "non-ldiskfs backend" && return
11967
11968         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11969         local myDIR=$DIR/$tdir
11970
11971         mkdir -p $myDIR
11972         #define OBD_FAIL_SEQ_EXHAUST             0x1002
11973         $LCTL set_param fail_loc=0x80001002
11974         createmany -o $myDIR/t- 10000
11975         $LCTL set_param fail_loc=0
11976         # The guard is current the largest FID holder
11977         touch $myDIR/guard
11978         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
11979                     tr -d '[')
11980         local IDX=$(($SEQ % 64))
11981
11982         do_facet $SINGLEMDS sync
11983         # Make sure journal flushed.
11984         sleep 6
11985         local blk1=$(do_facet $SINGLEMDS \
11986                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
11987                      grep Blockcount | awk '{print $4}')
11988
11989         # Remove old files, some OI blocks will become idle.
11990         unlinkmany $myDIR/t- 10000
11991
11992         # stop the MDT
11993         stop $SINGLEMDS || error "Fail to stop MDT."
11994         # remount the MDT
11995         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11996
11997         df $MOUNT || error "Fail to df."
11998         # Create new files, idle OI blocks should be reused.
11999         createmany -o $myDIR/t- 2000
12000         do_facet $SINGLEMDS sync
12001         # Make sure journal flushed.
12002         sleep 6
12003         local blk2=$(do_facet $SINGLEMDS \
12004                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12005                      grep Blockcount | awk '{print $4}')
12006
12007         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12008 }
12009 run_test 228b "idle OI blocks can be reused after MDT restart"
12010
12011 #LU-1881
12012 test_228c() {
12013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12014         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12015                 skip "non-ldiskfs backend" && return
12016
12017         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12018         local myDIR=$DIR/$tdir
12019
12020         mkdir -p $myDIR
12021         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12022         $LCTL set_param fail_loc=0x80001002
12023         # 20000 files can guarantee there are index nodes in the OI file
12024         createmany -o $myDIR/t- 20000
12025         $LCTL set_param fail_loc=0
12026         # The guard is current the largest FID holder
12027         touch $myDIR/guard
12028         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12029                     tr -d '[')
12030         local IDX=$(($SEQ % 64))
12031
12032         do_facet $SINGLEMDS sync
12033         # Make sure journal flushed.
12034         sleep 6
12035         local blk1=$(do_facet $SINGLEMDS \
12036                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12037                      grep Blockcount | awk '{print $4}')
12038
12039         # Remove old files, some OI blocks will become idle.
12040         unlinkmany $myDIR/t- 20000
12041         rm -f $myDIR/guard
12042         # The OI file should become empty now
12043
12044         # Create new files, idle OI blocks should be reused.
12045         createmany -o $myDIR/t- 2000
12046         do_facet $SINGLEMDS sync
12047         # Make sure journal flushed.
12048         sleep 6
12049         local blk2=$(do_facet $SINGLEMDS \
12050                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12051                      grep Blockcount | awk '{print $4}')
12052
12053         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12054 }
12055 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12056
12057 test_229() { # LU-2482, LU-3448
12058         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12059                 skip "No HSM support on MDS of $(get_lustre_version)," \
12060                          "need 2.4.53 at least" && return
12061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12062         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12063
12064         rm -f $DIR/$tfile
12065
12066         # Create a file with a released layout and stripe count 2.
12067         $MULTIOP $DIR/$tfile H2c ||
12068                 error "failed to create file with released layout"
12069
12070         $GETSTRIPE -v $DIR/$tfile
12071
12072         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12073         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12074
12075         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12076         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12077         stat $DIR/$tfile || error "failed to stat released file"
12078
12079         chown $RUNAS_ID $DIR/$tfile ||
12080                 error "chown $RUNAS_ID $DIR/$tfile failed"
12081
12082         chgrp $RUNAS_ID $DIR/$tfile ||
12083                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12084
12085         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12086         rm $DIR/$tfile || error "failed to remove released file"
12087 }
12088 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12089
12090 test_230a() {
12091         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12092         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12093         local MDTIDX=1
12094
12095         mkdir -p $DIR/$tdir/test_230_local
12096         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12097         [ $mdt_idx -ne 0 ] &&
12098                 error "create local directory on wrong MDT $mdt_idx"
12099
12100         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12101                         error "create remote directory failed"
12102         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12103         [ $mdt_idx -ne $MDTIDX ] &&
12104                 error "create remote directory on wrong MDT $mdt_idx"
12105
12106         createmany -o $DIR/$tdir/test_230/t- 10 ||
12107                 error "create files on remote directory failed"
12108         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12109         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12110         rm -r $DIR/$tdir || error "unlink remote directory failed"
12111 }
12112 run_test 230a "Create remote directory and files under the remote directory"
12113
12114 test_230b() {
12115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12116         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12117         local MDTIDX=1
12118         local mdt_index
12119         local i
12120         local file
12121         local pid
12122         local stripe_count
12123         local migrate_dir=$DIR/$tdir/migrate_dir
12124         local other_dir=$DIR/$tdir/other_dir
12125
12126         mkdir -p $migrate_dir
12127         mkdir -p $other_dir
12128         for ((i=0; i<10; i++)); do
12129                 mkdir -p $migrate_dir/dir_${i}
12130                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12131                         error "create files under remote dir failed $i"
12132         done
12133
12134         cp /etc/passwd $migrate_dir/$tfile
12135         cp /etc/passwd $other_dir/$tfile
12136         chattr +SAD $migrate_dir
12137         chattr +SAD $migrate_dir/$tfile
12138
12139         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12140         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12141         local old_dir_mode=$(stat -c%f $migrate_dir)
12142         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12143
12144         mkdir -p $migrate_dir/dir_default_stripe2
12145         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12146         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12147
12148         mkdir -p $other_dir
12149         ln $migrate_dir/$tfile $other_dir/luna
12150         ln $migrate_dir/$tfile $migrate_dir/sofia
12151         ln $other_dir/$tfile $migrate_dir/david
12152         ln -s $migrate_dir/$tfile $other_dir/zachary
12153         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12154         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12155
12156         $LFS mv -v -M $MDTIDX $migrate_dir ||
12157                         error "migrate remote dir error"
12158
12159         echo "migratate to MDT1, then checking.."
12160         for ((i=0; i<10; i++)); do
12161                 for file in $(find $migrate_dir/dir_${i}); do
12162                         mdt_index=$($LFS getstripe -M $file)
12163                         [ $mdt_index == $MDTIDX ] ||
12164                                 error "$file is not on MDT${MDTIDX}"
12165                 done
12166         done
12167
12168         # the multiple link file should still in MDT0
12169         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12170         [ $mdt_index == 0 ] ||
12171                 error "$file is not on MDT${MDTIDX}"
12172
12173         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12174         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12175                 error " expect $old_dir_flag get $new_dir_flag"
12176
12177         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12178         [ "$old_file_flag" = "$new_file_flag" ] ||
12179                 error " expect $old_file_flag get $new_file_flag"
12180
12181         local new_dir_mode=$(stat -c%f $migrate_dir)
12182         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12183                 error "expect mode $old_dir_mode get $new_dir_mode"
12184
12185         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12186         [ "$old_file_mode" = "$new_file_mode" ] ||
12187                 error "expect mode $old_file_mode get $new_file_mode"
12188
12189         diff /etc/passwd $migrate_dir/$tfile ||
12190                 error "$tfile different after migration"
12191
12192         diff /etc/passwd $other_dir/luna ||
12193                 error "luna different after migration"
12194
12195         diff /etc/passwd $migrate_dir/sofia ||
12196                 error "sofia different after migration"
12197
12198         diff /etc/passwd $migrate_dir/david ||
12199                 error "david different after migration"
12200
12201         diff /etc/passwd $other_dir/zachary ||
12202                 error "zachary different after migration"
12203
12204         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12205                 error "${tfile}_ln different after migration"
12206
12207         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12208                 error "${tfile}_ln_other different after migration"
12209
12210         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12211         [ $stripe_count = 2 ] ||
12212                         error "dir strpe_count $d != 2 after migration."
12213
12214         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12215         [ $stripe_count = 2 ] ||
12216                         error "file strpe_count $d != 2 after migration."
12217
12218         #migrate back to MDT0
12219         MDTIDX=0
12220         $LFS mv -v -M $MDTIDX $migrate_dir ||
12221                         error "migrate remote dir error"
12222
12223         echo "migrate back to MDT0, checking.."
12224         for file in $(find $migrate_dir); do
12225                 mdt_index=$($LFS getstripe -M $file)
12226                 [ $mdt_index == $MDTIDX ] ||
12227                         error "$file is not on MDT${MDTIDX}"
12228         done
12229
12230         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12231         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12232                 error " expect $old_dir_flag get $new_dir_flag"
12233
12234         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12235         [ "$old_file_flag" = "$new_file_flag" ] ||
12236                 error " expect $old_file_flag get $new_file_flag"
12237
12238         local new_dir_mode=$(stat -c%f $migrate_dir)
12239         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12240                 error "expect mode $old_dir_mode get $new_dir_mode"
12241
12242         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12243         [ "$old_file_mode" = "$new_file_mode" ] ||
12244                 error "expect mode $old_file_mode get $new_file_mode"
12245
12246         diff /etc/passwd ${migrate_dir}/$tfile ||
12247                 error "$tfile different after migration"
12248
12249         diff /etc/passwd ${other_dir}/luna ||
12250                 error "luna different after migration"
12251
12252         diff /etc/passwd ${migrate_dir}/sofia ||
12253                 error "sofia different after migration"
12254
12255         diff /etc/passwd ${other_dir}/zachary ||
12256                 error "zachary different after migration"
12257
12258         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12259                 error "${tfile}_ln different after migration"
12260
12261         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12262                 error "${tfile}_ln_other different after migration"
12263
12264         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12265         [ $stripe_count = 2 ] ||
12266                 error "dir strpe_count $d != 2 after migration."
12267
12268         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12269         [ $stripe_count = 2 ] ||
12270                 error "file strpe_count $d != 2 after migration."
12271
12272         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12273 }
12274 run_test 230b "migrate directory"
12275
12276 test_230c() {
12277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12278         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12279         local MDTIDX=1
12280         local mdt_index
12281         local file
12282
12283         #If migrating directory fails in the middle, all entries of
12284         #the directory is still accessiable.
12285         mkdir -p $DIR/$tdir
12286         stat $DIR/$tdir
12287         createmany -o $DIR/$tdir/f 10 ||
12288                 error "create files under ${tdir} failed"
12289
12290         #failed after migrating 5 entries
12291         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12292         do_facet mds1 lctl set_param fail_loc=0x20001801
12293         do_facet mds1 lctl  set_param fail_val=5
12294         local t=`ls $DIR/$tdir | wc -l`
12295         $LFS mv -M $MDTIDX $DIR/$tdir &&
12296                 error "migrate should failed after 5 entries"
12297         local u=`ls $DIR/$tdir | wc -l`
12298         [ "$u" == "$t" ] || error "$u != $t during migration"
12299
12300         for file in $(find $DIR/$tdir); do
12301                 stat $file || error "stat $file failed"
12302         done
12303
12304         do_facet mds1 lctl set_param fail_loc=0
12305         do_facet mds1 lctl set_param fail_val=0
12306
12307         $LFS mv -M $MDTIDX $DIR/$tdir ||
12308                 error "migrate open files should failed with open files"
12309
12310         echo "Finish migration, then checking.."
12311         for file in $(find $DIR/$tdir); do
12312                 mdt_index=$($LFS getstripe -M $file)
12313                 [ $mdt_index == $MDTIDX ] ||
12314                         error "$file is not on MDT${MDTIDX}"
12315         done
12316
12317         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12318 }
12319 run_test 230c "check directory accessiblity if migration is failed"
12320
12321 test_230d() {
12322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12323         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12324         local MDTIDX=1
12325         local mdt_index
12326         local i
12327         local j
12328
12329         mkdir -p $DIR/$tdir
12330
12331         for ((i=0; i<100; i++)); do
12332                 mkdir -p $DIR/$tdir/dir_${i}
12333                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12334                         error "create files under remote dir failed $i"
12335         done
12336
12337         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12338
12339         echo "Finish migration, then checking.."
12340         for file in $(find $DIR/$tdir); do
12341                 mdt_index=$($LFS getstripe -M $file)
12342                 [ $mdt_index == $MDTIDX ] ||
12343                         error "$file is not on MDT${MDTIDX}"
12344         done
12345
12346         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12347 }
12348 run_test 230d "check migrate big directory"
12349
12350 test_231a()
12351 {
12352         # For simplicity this test assumes that max_pages_per_rpc
12353         # is the same across all OSCs
12354         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12355         local bulk_size=$((max_pages * 4096))
12356
12357         mkdir -p $DIR/$tdir
12358
12359         # clear the OSC stats
12360         $LCTL set_param osc.*.stats=0 &>/dev/null
12361
12362         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12363         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12364                 oflag=direct &>/dev/null || error "dd failed"
12365
12366         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12367         if [ x$nrpcs != "x1" ]; then
12368                 error "found $nrpc ost_write RPCs, not 1 as expected"
12369         fi
12370
12371         # Drop the OSC cache, otherwise we will read from it
12372         cancel_lru_locks osc
12373
12374         # clear the OSC stats
12375         $LCTL set_param osc.*.stats=0 &>/dev/null
12376
12377         # Client reads $bulk_size.
12378         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12379                 iflag=direct &>/dev/null || error "dd failed"
12380
12381         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12382         if [ x$nrpcs != "x1" ]; then
12383                 error "found $nrpc ost_read RPCs, not 1 as expected"
12384         fi
12385 }
12386 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12387
12388 test_231b() {
12389         mkdir -p $DIR/$tdir
12390         local i
12391         for i in {0..1023}; do
12392                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12393                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12394                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12395         done
12396         sync
12397 }
12398 run_test 231b "must not assert on fully utilized OST request buffer"
12399
12400 test_232() {
12401         mkdir -p $DIR/$tdir
12402         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12403         $LCTL set_param fail_loc=0x31c
12404
12405         # ignore dd failure
12406         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12407
12408         $LCTL set_param fail_loc=0
12409         umount_client $MOUNT || error "umount failed"
12410         mount_client $MOUNT || error "mount failed"
12411 }
12412 run_test 232 "failed lock should not block umount"
12413
12414 test_233a() {
12415         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12416         { skip "Need MDS version at least 2.3.64"; return; }
12417
12418         local fid=$($LFS path2fid $MOUNT)
12419         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12420                 error "cannot access $MOUNT using its FID '$fid'"
12421 }
12422 run_test 233a "checking that OBF of the FS root succeeds"
12423
12424 test_233b() {
12425         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12426         { skip "Need MDS version at least 2.5.90"; return; }
12427
12428         local fid=$($LFS path2fid $MOUNT/.lustre)
12429         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12430                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12431
12432         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12433         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12434                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12435 }
12436 run_test 233b "checking that OBF of the FS .lustre succeeds"
12437
12438 test_234() {
12439         local p="$TMP/sanityN-$TESTNAME.parameters"
12440         save_lustre_params client "llite.*.xattr_cache" > $p
12441         lctl set_param llite.*.xattr_cache 1 ||
12442                 { skip "xattr cache is not supported"; return 0; }
12443
12444         mkdir -p $DIR/$tdir || error "mkdir failed"
12445         touch $DIR/$tdir/$tfile || error "touch failed"
12446         # OBD_FAIL_LLITE_XATTR_ENOMEM
12447         $LCTL set_param fail_loc=0x1405
12448         if [ ! -f /etc/SuSE-release ]; then
12449                 # attr pre-2.4.44-7 had a bug with rc
12450                 # LU-3703 - SLES clients have older attr
12451                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12452                         error "getfattr should have failed with ENOMEM"
12453         fi
12454         $LCTL set_param fail_loc=0x0
12455         rm -rf $DIR/$tdir
12456
12457         restore_lustre_params < $p
12458         rm -f $p
12459 }
12460 run_test 234 "xattr cache should not crash on ENOMEM"
12461
12462 test_235() {
12463         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12464                 skip "Need MDS version at least 2.4.52" && return
12465         flock_deadlock $DIR/$tfile
12466         local RC=$?
12467         case $RC in
12468                 0)
12469                 ;;
12470                 124) error "process hangs on a deadlock"
12471                 ;;
12472                 *) error "error executing flock_deadlock $DIR/$tfile"
12473                 ;;
12474         esac
12475 }
12476 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12477
12478 #LU-2935
12479 test_236() {
12480         check_swap_layouts_support && return 0
12481         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12482
12483         local ref1=/etc/passwd
12484         local ref2=/etc/group
12485         local file1=$DIR/$tdir/f1
12486         local file2=$DIR/$tdir/f2
12487
12488         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12489         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12490         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12491         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12492         local fd=$(free_fd)
12493         local cmd="exec $fd<>$file2"
12494         eval $cmd
12495         rm $file2
12496         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12497                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12498         cmd="exec $fd>&-"
12499         eval $cmd
12500         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12501
12502         #cleanup
12503         rm -rf $DIR/$tdir
12504 }
12505 run_test 236 "Layout swap on open unlinked file"
12506
12507 # test to verify file handle related system calls
12508 # (name_to_handle_at/open_by_handle_at)
12509 # The new system calls are supported in glibc >= 2.14.
12510
12511 test_237() {
12512         echo "Test file_handle syscalls" > $DIR/$tfile
12513         check_fhandle_syscalls $DIR/$tfile ||
12514                 error "check_fhandle_syscalls failed"
12515 }
12516 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12517
12518 # LU-4659 linkea consistency
12519 test_238() {
12520         touch $DIR/$tfile
12521         ln $DIR/$tfile $DIR/$tfile.lnk
12522         touch $DIR/$tfile.new
12523         mv $DIR/$tfile.new $DIR/$tfile
12524         local fid1=$(lfs path2fid $DIR/$tfile)
12525         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12526         local path1=$(lfs fid2path $FSNAME $fid1)
12527         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12528         local path2=$(lfs fid2path $FSNAME $fid2)
12529         [ $tfile.lnk == $path2 ] ||
12530                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12531         rm -f $DIR/$tfile*
12532 }
12533 run_test 238 "Verify linkea consistency"
12534
12535 test_striped_dir() {
12536         local mdt_index=$1
12537         local stripe_count
12538         local stripe_index
12539
12540         mkdir -p $DIR/$tdir
12541         $LFS setdirstripe -i $mdt_index -c 2 -t all_char $DIR/$tdir/striped_dir ||
12542                 error "set striped dir error"
12543
12544         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12545         if [ "$stripe_count" != "2" ]; then
12546                 error "stripe_count is $stripe_count, expect 2"
12547         fi
12548
12549         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12550         if [ "$stripe_index" != "$mdt_index" ]; then
12551                 error "stripe_index is $stripe_index, expect $mdt_index"
12552         fi
12553
12554         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12555                 error "nlink error after create striped dir"
12556
12557         mkdir $DIR/$tdir/striped_dir/a
12558         mkdir $DIR/$tdir/striped_dir/b
12559
12560         stat $DIR/$tdir/striped_dir/a ||
12561                 error "create dir under striped dir failed"
12562         stat $DIR/$tdir/striped_dir/b ||
12563                 error "create dir under striped dir failed"
12564
12565         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12566                 error "nlink error after mkdir"
12567
12568         rmdir $DIR/$tdir/striped_dir/a
12569         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12570                 error "nlink error after rmdir"
12571
12572         rmdir $DIR/$tdir/striped_dir/b
12573         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12574                 error "nlink error after rmdir"
12575
12576         rmdir $DIR/$tdir/striped_dir ||
12577                 error "rmdir striped dir error"
12578         true
12579 }
12580
12581 test_300a() {
12582         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12583         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12584
12585         test_striped_dir 0 || error "failed on striped dir on MDT0"
12586         test_striped_dir 1 || error "failed on striped dir on MDT0"
12587 }
12588 run_test 300a "basic striped dir sanity test"
12589
12590 test_300b() {
12591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12592         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12593         local i
12594         local mtime1
12595         local mtime2
12596         local mtime3
12597
12598         test_mkdir $DIR/$tdir || error "mkdir fail"
12599         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12600                 error "set striped dir error"
12601         for ((i=0; i<10; i++)); do
12602                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12603                 sleep 1
12604                 touch $DIR/$tdir/striped_dir/file_$i ||
12605                                         error "touch error $i"
12606                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
12607                 [ $mtime1 -eq $mtime2 ] &&
12608                         error "mtime not change after create"
12609                 sleep 1
12610                 rm -f $DIR/$tdir/striped_dir/file_$i ||
12611                                         error "unlink error $i"
12612                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
12613                 [ $mtime2 -eq $mtime3 ] &&
12614                         error "mtime did not change after unlink"
12615         done
12616         true
12617 }
12618 run_test 300b "check ctime/mtime for striped dir"
12619
12620 test_300c() {
12621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12622         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12623         local file_count
12624
12625         mkdir -p $DIR/$tdir
12626         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
12627                 error "set striped dir error"
12628
12629         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
12630                 error "chown striped dir failed"
12631
12632         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
12633                 error "create 5k files failed"
12634
12635         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
12636
12637         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
12638
12639         rm -rf $DIR/$tdir
12640 }
12641 run_test 300c "chown && check ls under striped directory"
12642
12643 test_300d() {
12644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12645         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12646         local stripe_count
12647         local file
12648
12649         mkdir -p $DIR/$tdir
12650         $SETSTRIPE -c 2 $DIR/$tdir
12651
12652         #local striped directory
12653         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12654                 error "set striped dir error"
12655         createmany -o $DIR/$tdir/striped_dir/f 10 ||
12656                 error "create 10 files failed"
12657
12658         #remote striped directory
12659         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
12660                 error "set striped dir error"
12661         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
12662                 error "create 10 files failed"
12663
12664         for file in $(find $DIR/$tdir); do
12665                 stripe_count=$($GETSTRIPE -c $file)
12666                 [ $stripe_count -eq 2 ] ||
12667                         error "wrong stripe $stripe_count for $file"
12668         done
12669
12670         rm -rf $DIR/$tdir
12671 }
12672 run_test 300d "check default stripe under striped directory"
12673
12674 test_300e() {
12675         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12676         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12677         local stripe_count
12678         local file
12679
12680         mkdir -p $DIR/$tdir
12681
12682         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12683                 error "set striped dir error"
12684
12685         touch $DIR/$tdir/striped_dir/a
12686         touch $DIR/$tdir/striped_dir/b
12687         touch $DIR/$tdir/striped_dir/c
12688
12689         mkdir $DIR/$tdir/striped_dir/dir_a
12690         mkdir $DIR/$tdir/striped_dir/dir_b
12691         mkdir $DIR/$tdir/striped_dir/dir_c
12692
12693         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
12694                 error "set striped dir under striped dir error"
12695
12696         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
12697                 error "set striped dir under striped dir error"
12698
12699         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
12700                 error "set striped dir under striped dir error"
12701
12702         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
12703                 error "rename file under striped dir should fail"
12704
12705         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
12706                 error "rename dir under striped dir should fail"
12707
12708         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
12709                 error "rename dir under different stripes should fail"
12710
12711         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
12712                 error "rename file under striped dir should succeed"
12713
12714         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
12715                 error "rename dir under striped dir should succeed"
12716
12717         rm -rf $DIR/$tdir
12718 }
12719 run_test 300e "check rename under striped directory"
12720
12721 test_300f() {
12722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12723         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12724         local stripe_count
12725         local file
12726
12727         rm -rf $DIR/$tdir
12728         mkdir -p $DIR/$tdir
12729
12730         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12731                 error "set striped dir error"
12732
12733         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
12734                 error "set striped dir error"
12735
12736         touch $DIR/$tdir/striped_dir/a
12737         mkdir $DIR/$tdir/striped_dir/dir_a
12738         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
12739                 error "create striped dir under striped dir fails"
12740
12741         touch $DIR/$tdir/striped_dir1/b
12742         mkdir $DIR/$tdir/striped_dir1/dir_b
12743         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
12744                 error "create striped dir under striped dir fails"
12745
12746         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
12747                 error "rename file under different striped dir should fail"
12748
12749         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
12750                 error "rename dir under different striped dir should fail"
12751
12752         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
12753                 error "rename striped dir under diff striped dir should fail"
12754
12755         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
12756                 error "rename file under diff striped dirs fails"
12757
12758         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
12759                 error "rename dir under diff striped dirs fails"
12760
12761         rm -rf $DIR/$tdir
12762 }
12763 run_test 300f "check rename cross striped directory"
12764
12765 test_300g() {
12766         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12767         local stripe_count
12768         local dir
12769
12770         mkdir $DIR/$tdir
12771         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
12772                                         $DIR/$tdir/striped_dir ||
12773                 error "set striped dir error"
12774
12775         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12776                 error "set default stripe on striped dir error"
12777
12778         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12779
12780         for dir in $(find $DIR/$tdir/striped_dir/*); do
12781                 stripe_count=$($LFS getdirstripe -c $dir)
12782                 [ $stripe_count -eq $MDSCOUNT ] ||
12783                         error "expect $MDSCOUNT get $stripe_count for $dir"
12784         done
12785
12786         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
12787         #change default stripe count to 2
12788         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
12789                 error "set default stripe on striped dir error"
12790
12791         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12792
12793         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
12794
12795         #change default stripe count to 1
12796         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
12797                 error "set default stripe on striped dir error"
12798
12799         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
12800         for dir in $(find $DIR/$tdir/striped_dir/*); do
12801                 stripe_count=$($LFS getdirstripe -c $dir)
12802                 [ $stripe_count -eq 1 ] ||
12803                         error "expect 1 get $stripe_count for $dir"
12804         done
12805         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
12806 }
12807 run_test 300g "check default striped directory for striped directory"
12808
12809 test_400a() { # LU-1606, was conf-sanity test_74
12810         local extra_flags=''
12811         local out=$TMP/$tfile
12812         local prefix=/usr/include/lustre
12813         local prog
12814
12815         if ! which $CC > /dev/null 2>&1; then
12816                 skip_env "$CC is not installed"
12817                 return 0
12818         fi
12819
12820         if ! [[ -d $prefix ]]; then
12821                 # Assume we're running in tree and fixup the include path.
12822                 extra_flags+=" -I$LUSTRE/../libcfs/include"
12823                 extra_flags+=" -I$LUSTRE/include"
12824                 extra_flags+=" -L$LUSTRE/utils"
12825         fi
12826
12827         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
12828                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
12829                         error "client api broken"
12830         done
12831 }
12832 run_test 400a "Lustre client api program can compile and link"
12833
12834 test_400b() { # LU-1606, LU-5011
12835         local header
12836         local out=$TMP/$tfile
12837         local prefix=/usr/include/lustre
12838
12839         # We use a hard coded prefix so that this test will not fail
12840         # when run in tree. There are headers in lustre/include/lustre/
12841         # that are not packaged (like lustre_idl.h) and have more
12842         # complicated include dependencies (like config.h and lnet/types.h).
12843         # Since this test about correct packaging we just skip them when
12844         # they don't exist (see below) rather than try to fixup cppflags.
12845
12846         if ! which $CC > /dev/null 2>&1; then
12847                 skip_env "$CC is not installed"
12848                 return 0
12849         fi
12850
12851         for header in $prefix/*.h; do
12852                 if ! [[ -f "$header" ]]; then
12853                         continue
12854                 fi
12855
12856                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
12857                         continue # liblustreapi.h is deprecated.
12858                 fi
12859
12860                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
12861                         error "cannot compile '$header'"
12862         done
12863 }
12864 run_test 400b "packaged headers can be compiled"
12865
12866 #
12867 # tests that do cleanup/setup should be run at the end
12868 #
12869
12870 test_900() {
12871         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12872         local ls
12873         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
12874         $LCTL set_param fail_loc=0x903
12875         # cancel_lru_locks mgc - does not work due to lctl set_param syntax
12876         for ls in /proc/fs/lustre/ldlm/namespaces/MGC*/lru_size; do
12877                 echo "clear" > $ls
12878         done
12879         FAIL_ON_ERROR=true cleanup
12880         FAIL_ON_ERROR=true setup
12881 }
12882 run_test 900 "umount should not race with any mgc requeue thread"
12883
12884 complete $SECONDS
12885 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
12886 check_and_cleanup_lustre
12887 if [ "$I_MOUNTED" != "yes" ]; then
12888         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
12889 fi
12890 exit_status