Whamcloud - gitweb
66683291f4e2fb8e626e1be9b2e852865bb71e0e
[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 SRCDIR=$(cd $(dirname $0); echo $PWD)
20 export PATH=$PATH:/sbin
21
22 TMP=${TMP:-/tmp}
23
24 CC=${CC:-cc}
25 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
26 CREATETEST=${CREATETEST:-createtest}
27 LFS=${LFS:-lfs}
28 LFIND=${LFIND:-"$LFS find"}
29 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
30 LCTL=${LCTL:-lctl}
31 OPENFILE=${OPENFILE:-openfile}
32 OPENUNLINK=${OPENUNLINK:-openunlink}
33 export MULTIOP=${MULTIOP:-multiop}
34 READS=${READS:-"reads"}
35 MUNLINK=${MUNLINK:-munlink}
36 SOCKETSERVER=${SOCKETSERVER:-socketserver}
37 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
38 MEMHOG=${MEMHOG:-memhog}
39 DIRECTIO=${DIRECTIO:-directio}
40 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
41 UMOUNT=${UMOUNT:-"umount -d"}
42 STRIPES_PER_OBJ=-1
43 CHECK_GRANT=${CHECK_GRANT:-"yes"}
44 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
45 export PARALLEL=${PARALLEL:-"no"}
46
47 export NAME=${NAME:-local}
48
49 SAVE_PWD=$PWD
50
51 CLEANUP=${CLEANUP:-:}
52 SETUP=${SETUP:-:}
53 TRACE=${TRACE:-""}
54 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
55 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
56 . $LUSTRE/tests/test-framework.sh
57 init_test_env $@
58 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
59 init_logging
60
61 [ "$SLOW" = "no" ] && EXCEPT_SLOW="24o 24D 27m 64b 68 71 77f 78 115 124b 230d"
62
63 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
64         # bug number for skipped test: LU-1593  LU-4536 LU-5242  LU-1957 LU-2805
65         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  34h      65ic    78 79 80 180     184c"
66         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b 51ba"
67 fi
68
69 FAIL_ON_ERROR=false
70
71 cleanup() {
72         echo -n "cln.."
73         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
74         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
75 }
76 setup() {
77         echo -n "mnt.."
78         load_modules
79         setupall || exit 10
80         echo "done"
81 }
82
83 check_kernel_version() {
84         WANT_VER=$1
85         GOT_VER=$(lctl get_param -n version | awk '/kernel:/ {print $2}')
86         case $GOT_VER in
87         patchless|patchless_client) return 0;;
88         *) [ $GOT_VER -ge $WANT_VER ] && return 0 ;;
89         esac
90         log "test needs at least kernel version $WANT_VER, running $GOT_VER"
91         return 1
92 }
93
94 check_swap_layouts_support()
95 {
96         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
97                 { skip "Does not support layout lock."; return 0; }
98         return 1
99 }
100
101 if [ "$ONLY" == "cleanup" ]; then
102        sh llmountcleanup.sh
103        exit 0
104 fi
105
106 check_and_setup_lustre
107
108 DIR=${DIR:-$MOUNT}
109 assert_DIR
110
111 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
112         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
113 LOVNAME=$($LCTL get_param -n llite.*.lov.common_name | tail -n 1)
114 OSTCOUNT=$($LCTL get_param -n lov.$LOVNAME.numobd)
115 STRIPECOUNT=$($LCTL get_param -n lov.$LOVNAME.stripecount)
116 STRIPESIZE=$($LCTL get_param -n lov.$LOVNAME.stripesize)
117 ORIGFREE=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
118 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
119
120 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
121 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
122 rm -rf $DIR/[Rdfs][0-9]*
123
124 # $RUNAS_ID may get set incorrectly somewhere else
125 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
126
127 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
128
129 build_test_filter
130
131 if [ "${ONLY}" = "MOUNT" ] ; then
132         echo "Lustre is up, please go on"
133         exit
134 fi
135
136 echo "preparing for tests involving mounts"
137 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
138 touch $EXT2_DEV
139 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
140 echo # add a newline after mke2fs.
141
142 umask 077
143
144 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
145 lctl set_param debug=-1 2> /dev/null || true
146 test_0a() {
147         touch $DIR/$tfile
148         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
149         rm $DIR/$tfile
150         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
151 }
152 run_test 0a "touch; rm ====================="
153
154 test_0b() {
155         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
156         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
157 }
158 run_test 0b "chmod 0755 $DIR ============================="
159
160 test_0c() {
161         $LCTL get_param mdc.*.import | grep "state: FULL" ||
162                 error "import not FULL"
163         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
164                 error "bad target"
165 }
166 run_test 0c "check import proc ============================="
167
168 test_1() {
169         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
170         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
171         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
172         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
173         rmdir $DIR/$tdir/d2
174         rmdir $DIR/$tdir
175         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
176 }
177 run_test 1 "mkdir; remkdir; rmdir =============================="
178
179 test_2() {
180         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
181         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
182         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
183         rm -r $DIR/$tdir
184         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
185 }
186 run_test 2 "mkdir; touch; rmdir; check file ===================="
187
188 test_3() {
189         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
190         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
191         touch $DIR/$tdir/$tfile
192         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
193         rm -r $DIR/$tdir
194         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
195 }
196 run_test 3 "mkdir; touch; rmdir; check dir ====================="
197
198 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
199 test_4() {
200         local MDTIDX=1
201
202         test_mkdir $DIR/$tdir ||
203                 error "Create remote directory failed"
204
205         touch $DIR/$tdir/$tfile ||
206                 error "Create file under remote directory failed"
207
208         rmdir $DIR/$tdir &&
209                 error "Expect error removing in-use dir $DIR/$tdir"
210
211         test -d $DIR/$tdir || error "Remote directory disappeared"
212
213         rm -rf $DIR/$tdir || error "remove remote dir error"
214 }
215 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
216
217 test_5() {
218         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
219         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
220         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
221         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
222         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
223 }
224 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
225
226 test_6a() {
227         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
228         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
230                 error "$tfile does not have perm 0666 or UID $UID"
231         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
232         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
233                 error "$tfile should be 0666 and owned by UID $UID"
234 }
235 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
236
237 test_6c() {
238         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
239         touch $DIR/$tfile
240         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
241         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
242                 error "$tfile should be owned by UID $RUNAS_ID"
243         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
244         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
245                 error "$tfile should be owned by UID $RUNAS_ID"
246 }
247 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
248
249 test_6e() {
250         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
251         touch $DIR/$tfile
252         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
253         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
254                 error "$tfile should be owned by GID $UID"
255         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
256         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
257                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
258 }
259 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
260
261 test_6g() {
262         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
263         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
264         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
265         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
266         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
267         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
268         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
269                 error "$tdir/d/subdir should be GID $RUNAS_GID"
270 }
271 run_test 6g "Is new dir in sgid dir inheriting group?"
272
273 test_6h() { # bug 7331
274         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
275         touch $DIR/$tfile || error "touch failed"
276         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
277         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
278                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
279         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
280                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
281 }
282 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
283
284 test_7a() {
285         test_mkdir $DIR/$tdir
286         $MCREATE $DIR/$tdir/$tfile
287         chmod 0666 $DIR/$tdir/$tfile
288         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
289                 error "$tdir/$tfile should be mode 0666"
290 }
291 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
292
293 test_7b() {
294         if [ ! -d $DIR/$tdir ]; then
295                 mkdir $DIR/$tdir
296         fi
297         $MCREATE $DIR/$tdir/$tfile
298         echo -n foo > $DIR/$tdir/$tfile
299         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
300         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
301 }
302 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
303
304 test_8() {
305         test_mkdir $DIR/$tdir
306         touch $DIR/$tdir/$tfile
307         chmod 0666 $DIR/$tdir/$tfile
308         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
309                 error "$tfile mode not 0666"
310 }
311 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
312
313 test_9() {
314         test_mkdir $DIR/$tdir
315         test_mkdir $DIR/$tdir/d2
316         test_mkdir $DIR/$tdir/d2/d3
317         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
318 }
319 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
320
321 test_10() {
322         test_mkdir $DIR/$tdir
323         test_mkdir $DIR/$tdir/d2
324         touch $DIR/$tdir/d2/$tfile
325         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
326                 error "$tdir/d2/$tfile not a file"
327 }
328 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
329
330 test_11() {
331         test_mkdir $DIR/$tdir
332         test_mkdir $DIR/$tdir/d2
333         chmod 0666 $DIR/$tdir/d2
334         chmod 0705 $DIR/$tdir/d2
335         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
336                 error "$tdir/d2 mode not 0705"
337 }
338 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
339
340 test_12() {
341         test_mkdir $DIR/$tdir
342         touch $DIR/$tdir/$tfile
343         chmod 0666 $DIR/$tdir/$tfile
344         chmod 0654 $DIR/$tdir/$tfile
345         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
346                 error "$tdir/d2 mode not 0654"
347 }
348 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
349
350 test_13() {
351         test_mkdir $DIR/$tdir
352         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
353         >  $DIR/$tdir/$tfile
354         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
355                 error "$tdir/$tfile size not 0 after truncate"
356 }
357 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
358
359 test_14() {
360         test_mkdir $DIR/$tdir
361         touch $DIR/$tdir/$tfile
362         rm $DIR/$tdir/$tfile
363         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
364 }
365 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
366
367 test_15() {
368         test_mkdir $DIR/$tdir
369         touch $DIR/$tdir/$tfile
370         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
371         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
372                 error "$tdir/${tfile_2} not a file after rename"
373 }
374 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
375
376 test_16() {
377         test_mkdir $DIR/$tdir
378         touch $DIR/$tdir/$tfile
379         rm -rf $DIR/$tdir/$tfile
380         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
381 }
382 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
383
384 test_17a() {
385         test_mkdir -p $DIR/$tdir
386         touch $DIR/$tdir/$tfile
387         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
388         ls -l $DIR/$tdir
389         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
390                 error "$tdir/l-exist not a symlink"
391         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
392                 error "$tdir/l-exist not referencing a file"
393         rm -f $DIR/$tdir/l-exist
394         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
395 }
396 run_test 17a "symlinks: create, remove (real) =================="
397
398 test_17b() {
399         test_mkdir -p $DIR/$tdir
400         ln -s no-such-file $DIR/$tdir/l-dangle
401         ls -l $DIR/$tdir
402         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
403                 error "$tdir/l-dangle not referencing no-such-file"
404         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
405                 error "$tdir/l-dangle not referencing non-existent file"
406         rm -f $DIR/$tdir/l-dangle
407         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
408 }
409 run_test 17b "symlinks: create, remove (dangling) =============="
410
411 test_17c() { # bug 3440 - don't save failed open RPC for replay
412         test_mkdir -p $DIR/$tdir
413         ln -s foo $DIR/$tdir/$tfile
414         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
415 }
416 run_test 17c "symlinks: open dangling (should return error) ===="
417
418 test_17d() {
419         test_mkdir -p $DIR/$tdir
420         ln -s foo $DIR/$tdir/$tfile
421         touch $DIR/$tdir/$tfile || error "creating to new symlink"
422 }
423 run_test 17d "symlinks: create dangling ========================"
424
425 test_17e() {
426         test_mkdir -p $DIR/$tdir
427         local foo=$DIR/$tdir/$tfile
428         ln -s $foo $foo || error "create symlink failed"
429         ls -l $foo || error "ls -l failed"
430         ls $foo && error "ls not failed" || true
431 }
432 run_test 17e "symlinks: create recursive symlink (should return error) ===="
433
434 test_17f() {
435         test_mkdir -p $DIR/$tdir
436         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
437         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
438         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
439         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
440         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
441         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
442         ls -l  $DIR/$tdir
443 }
444 run_test 17f "symlinks: long and very long symlink name ========================"
445
446 # str_repeat(S, N) generate a string that is string S repeated N times
447 str_repeat() {
448         local s=$1
449         local n=$2
450         local ret=''
451         while [ $((n -= 1)) -ge 0 ]; do
452                 ret=$ret$s
453         done
454         echo $ret
455 }
456
457 # Long symlinks and LU-2241
458 test_17g() {
459         test_mkdir -p $DIR/$tdir
460         local TESTS="59 60 61 4094 4095"
461
462         # Fix for inode size boundary in 2.1.4
463         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
464                 TESTS="4094 4095"
465
466         # Patch not applied to 2.2 or 2.3 branches
467         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
468         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
469                 TESTS="4094 4095"
470
471         # skip long symlink name for rhel6.5.
472         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
473         grep -q '6.5' /etc/redhat-release &>/dev/null &&
474                 TESTS="59 60 61 4062 4063"
475
476         for i in $TESTS; do
477                 local SYMNAME=$(str_repeat 'x' $i)
478                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
479                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
480         done
481 }
482 run_test 17g "symlinks: really long symlink name and inode boundaries"
483
484 test_17h() { #bug 17378
485         remote_mds_nodsh && skip "remote MDS with nodsh" && return
486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
487         local mdt_idx
488         test_mkdir -p $DIR/$tdir
489         if [[ $MDSCOUNT -gt 1 ]]; then
490                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
491         else
492                 mdt_idx=0
493         fi
494         $SETSTRIPE -c -1 $DIR/$tdir
495 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
496         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
497         touch $DIR/$tdir/$tfile || true
498 }
499 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
500
501 test_17i() { #bug 20018
502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
503         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
504         test_mkdir -c1 $DIR/$tdir
505         local foo=$DIR/$tdir/$tfile
506         local mdt_idx
507         if [[ $MDSCOUNT -gt 1 ]]; then
508                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
509         else
510                 mdt_idx=0
511         fi
512         ln -s $foo $foo || error "create symlink failed"
513 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
514         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
515         ls -l $foo && error "error not detected"
516         return 0
517 }
518 run_test 17i "don't panic on short symlink"
519
520 test_17k() { #bug 22301
521         [[ -z "$(which rsync 2>/dev/null)" ]] &&
522                 skip "no rsync command" && return 0
523         rsync --help | grep -q xattr ||
524                 skip_env "$(rsync --version | head -n1) does not support xattrs"
525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
526         test_mkdir -p $DIR/$tdir
527         test_mkdir -p $DIR/$tdir.new
528         touch $DIR/$tdir/$tfile
529         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
530         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
531                 error "rsync failed with xattrs enabled"
532 }
533 run_test 17k "symlinks: rsync with xattrs enabled ========================="
534
535 test_17l() { # LU-279
536         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
537                 skip "no getfattr command" && return 0
538         mkdir -p $DIR/$tdir
539         touch $DIR/$tdir/$tfile
540         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
541         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
542                 # -h to not follow symlinks. -m '' to list all the xattrs.
543                 # grep to remove first line: '# file: $path'.
544                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
545                 do
546                         lgetxattr_size_check $path $xattr ||
547                                 error "lgetxattr_size_check $path $xattr failed"
548                 done
549         done
550 }
551 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
552
553 # LU-1540
554 test_17m() {
555         local short_sym="0123456789"
556         local WDIR=$DIR/${tdir}m
557         local mds_index
558         local devname
559         local cmd
560         local i
561         local rc=0
562
563         remote_mds_nodsh && skip "remote MDS with nodsh" && return
564         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
565         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
566                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
567
568         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
569                 skip "only for ldiskfs MDT" && return 0
570
571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
572
573         mkdir -p $WDIR
574         long_sym=$short_sym
575         # create a long symlink file
576         for ((i = 0; i < 4; ++i)); do
577                 long_sym=${long_sym}${long_sym}
578         done
579
580         echo "create 512 short and long symlink files under $WDIR"
581         for ((i = 0; i < 256; ++i)); do
582                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
583                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
584         done
585
586         echo "erase them"
587         rm -f $WDIR/*
588         sync
589         wait_delete_completed
590
591         echo "recreate the 512 symlink files with a shorter string"
592         for ((i = 0; i < 512; ++i)); do
593                 # rewrite the symlink file with a shorter string
594                 ln -sf ${long_sym} $WDIR/long-$i
595                 ln -sf ${short_sym} $WDIR/short-$i
596         done
597
598         mds_index=$($LFS getstripe -M $WDIR)
599         mds_index=$((mds_index+1))
600         devname=$(mdsdevname $mds_index)
601         cmd="$E2FSCK -fnvd $devname"
602
603         echo "stop and checking mds${mds_index}: $cmd"
604         # e2fsck should not return error
605         stop mds${mds_index}
606         do_facet mds${mds_index} $cmd || rc=$?
607
608         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
609         df $MOUNT > /dev/null 2>&1
610         [ $rc -ne 0 ] && error "e2fsck should not report error upon "\
611                 "short/long symlink MDT: rc=$rc"
612         return $rc
613 }
614 run_test 17m "run e2fsck against MDT which contains short/long symlink"
615
616 check_fs_consistency_17n() {
617         local mdt_index
618         local devname
619         local cmd
620         local rc=0
621
622         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
623         # so it only check MDT1/MDT2 instead of all of MDTs.
624         for mdt_index in $(seq 1 2); do
625                 devname=$(mdsdevname $mdt_index)
626                 cmd="$E2FSCK -fnvd $devname"
627
628                 echo "stop and checking mds${mdt_index}: $cmd"
629                 # e2fsck should not return error
630                 stop mds${mdt_index}
631                 do_facet mds${mdt_index} $cmd || rc=$?
632
633                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
634                         error "mount mds${mdt_index} failed"
635                 df $MOUNT > /dev/null 2>&1
636                 [ $rc -ne 0 ] && break
637         done
638         return $rc
639 }
640
641 test_17n() {
642         local i
643
644         remote_mds_nodsh && skip "remote MDS with nodsh" && return
645         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
646         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
647                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
648
649         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
650                 skip "only for ldiskfs MDT" && return 0
651
652         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
653
654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
655
656         mkdir $DIR/$tdir
657         for ((i=0; i<10; i++)); do
658                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
659                         error "create remote dir error $i"
660                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
661                         error "create files under remote dir failed $i"
662         done
663
664         check_fs_consistency_17n ||
665                 error "e2fsck report error after create files under remote dir"
666
667         for ((i = 0; i < 10; i++)); do
668                 rm -rf $DIR/$tdir/remote_dir_${i} ||
669                         error "destroy remote dir error $i"
670         done
671
672         check_fs_consistency_17n ||
673                 error "e2fsck report error after unlink files under remote dir"
674
675         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
676                 skip "lustre < 2.4.50 does not support migrate mv " && return
677
678         for ((i = 0; i < 10; i++)); do
679                 mkdir -p $DIR/$tdir/remote_dir_${i}
680                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
681                         error "create files under remote dir failed $i"
682                 $LFS mv --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
683                         error "migrate remote dir error $i"
684         done
685         check_fs_consistency_17n || error "e2fsck report error after migration"
686
687         for ((i = 0; i < 10; i++)); do
688                 rm -rf $DIR/$tdir/remote_dir_${i} ||
689                         error "destroy remote dir error $i"
690         done
691
692         check_fs_consistency_17n || error "e2fsck report error after unlink"
693 }
694 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
695
696 test_17o() {
697         remote_mds_nodsh && skip "remote MDS with nodsh" && return
698         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
699                 skip "Need MDS version at least 2.3.64" && return
700
701         local WDIR=$DIR/${tdir}o
702         local mdt_index
703         local mdtdevname
704         local rc=0
705
706         mkdir -p $WDIR
707         mdt_index=$($LFS getstripe -M $WDIR)
708         mdt_index=$((mdt_index+1))
709         mdtdevname=$(mdsdevname $mdt_index)
710
711         touch $WDIR/$tfile
712         stop mds${mdt_index}
713         start mds${mdt_index} $mdtdevname $MDS_MOUNT_OPTS ||
714                 error "mount mds${mdt_index} failed"
715
716         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
717         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
718         ls -l $WDIR/$tfile && rc=1
719         do_facet mds${mdt_index} lctl set_param fail_loc=0
720         [[ $rc -ne 0 ]] && error "stat file should fail"
721         true
722 }
723 run_test 17o "stat file with incompat LMA feature"
724
725 test_18() {
726         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
727         ls $DIR || error "Failed to ls $DIR: $?"
728 }
729 run_test 18 "touch .../f ; ls ... =============================="
730
731 test_19a() {
732         touch $DIR/$tfile
733         ls -l $DIR
734         rm $DIR/$tfile
735         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
736 }
737 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
738
739 test_19b() {
740         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
741 }
742 run_test 19b "ls -l .../f19 (should return error) =============="
743
744 test_19c() {
745         [ $RUNAS_ID -eq $UID ] &&
746                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
747         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
748 }
749 run_test 19c "$RUNAS touch .../f19 (should return error) =="
750
751 test_19d() {
752         cat $DIR/f19 && error || true
753 }
754 run_test 19d "cat .../f19 (should return error) =============="
755
756 test_20() {
757         touch $DIR/$tfile
758         rm $DIR/$tfile
759         log "1 done"
760         touch $DIR/$tfile
761         rm $DIR/$tfile
762         log "2 done"
763         touch $DIR/$tfile
764         rm $DIR/$tfile
765         log "3 done"
766         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
767 }
768 run_test 20 "touch .../f ; ls -l ... ==========================="
769
770 test_21() {
771         test_mkdir -p $DIR/$tdir
772         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
773         ln -s dangle $DIR/$tdir/link
774         echo foo >> $DIR/$tdir/link
775         cat $DIR/$tdir/dangle
776         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
777         $CHECKSTAT -f -t file $DIR/$tdir/link ||
778                 error "$tdir/link not linked to a file"
779 }
780 run_test 21 "write to dangling link ============================"
781
782 test_22() {
783         WDIR=$DIR/$tdir
784         test_mkdir -p $DIR/$tdir
785         chown $RUNAS_ID:$RUNAS_GID $WDIR
786         (cd $WDIR || error "cd $WDIR failed";
787         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
788         $RUNAS tar xf -)
789         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
790         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
791         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
792 }
793 run_test 22 "unpack tar archive as non-root user ==============="
794
795 # was test_23
796 test_23a() {
797         test_mkdir -p $DIR/$tdir
798         local file=$DIR/$tdir/$tfile
799
800         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
801         openfile -f O_CREAT:O_EXCL $file &&
802                 error "$file recreate succeeded" || true
803 }
804 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
805
806 test_23b() { # bug 18988
807         test_mkdir -p $DIR/$tdir
808         local file=$DIR/$tdir/$tfile
809
810         rm -f $file
811         echo foo > $file || error "write filed"
812         echo bar >> $file || error "append filed"
813         $CHECKSTAT -s 8 $file || error "wrong size"
814         rm $file
815 }
816 run_test 23b "O_APPEND check =========================="
817
818 # rename sanity
819 test_24a() {
820         echo '-- same directory rename'
821         test_mkdir $DIR/$tdir
822         touch $DIR/$tdir/$tfile.1
823         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
824         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
825 }
826 run_test 24a "rename file to non-existent target"
827
828 test_24b() {
829         test_mkdir $DIR/$tdir
830         touch $DIR/$tdir/$tfile.{1,2}
831         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
832         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
833         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
834 }
835 run_test 24b "rename file to existing target"
836
837 test_24c() {
838         test_mkdir $DIR/$tdir
839         test_mkdir $DIR/$tdir/d$testnum.1
840         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
841         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
842         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
843 }
844 run_test 24c "rename directory to non-existent target"
845
846 test_24d() {
847         test_mkdir -c1 $DIR/$tdir
848         test_mkdir -c1 $DIR/$tdir/d$testnum.1
849         test_mkdir -c1 $DIR/$tdir/d$testnum.2
850         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
851         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
852         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
853 }
854 run_test 24d "rename directory to existing target"
855
856 test_24e() {
857         echo '-- cross directory renames --'
858         test_mkdir $DIR/R5a
859         test_mkdir $DIR/R5b
860         touch $DIR/R5a/f
861         mv $DIR/R5a/f $DIR/R5b/g
862         $CHECKSTAT -a $DIR/R5a/f || error
863         $CHECKSTAT -t file $DIR/R5b/g || error
864 }
865 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
866
867 test_24f() {
868         test_mkdir $DIR/R6a
869         test_mkdir $DIR/R6b
870         touch $DIR/R6a/f $DIR/R6b/g
871         mv $DIR/R6a/f $DIR/R6b/g
872         $CHECKSTAT -a $DIR/R6a/f || error
873         $CHECKSTAT -t file $DIR/R6b/g || error
874 }
875 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
876
877 test_24g() {
878         test_mkdir $DIR/R7a
879         test_mkdir $DIR/R7b
880         test_mkdir $DIR/R7a/d
881         mv $DIR/R7a/d $DIR/R7b/e
882         $CHECKSTAT -a $DIR/R7a/d || error
883         $CHECKSTAT -t dir $DIR/R7b/e || error
884 }
885 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
886
887 test_24h() {
888         test_mkdir -c1 $DIR/R8a
889         test_mkdir -c1 $DIR/R8b
890         test_mkdir -c1 $DIR/R8a/d
891         test_mkdir -c1 $DIR/R8b/e
892         mrename $DIR/R8a/d $DIR/R8b/e
893         $CHECKSTAT -a $DIR/R8a/d || error
894         $CHECKSTAT -t dir $DIR/R8b/e || error
895 }
896 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
897
898 test_24i() {
899         echo "-- rename error cases"
900         test_mkdir $DIR/R9
901         test_mkdir $DIR/R9/a
902         touch $DIR/R9/f
903         mrename $DIR/R9/f $DIR/R9/a
904         $CHECKSTAT -t file $DIR/R9/f || error
905         $CHECKSTAT -t dir  $DIR/R9/a || error
906         $CHECKSTAT -a $DIR/R9/a/f || error
907 }
908 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
909
910 test_24j() {
911         test_mkdir $DIR/R10
912         mrename $DIR/R10/f $DIR/R10/g
913         $CHECKSTAT -t dir $DIR/R10 || error
914         $CHECKSTAT -a $DIR/R10/f || error
915         $CHECKSTAT -a $DIR/R10/g || error
916 }
917 run_test 24j "source does not exist ============================"
918
919 test_24k() {
920         test_mkdir $DIR/R11a
921         test_mkdir $DIR/R11a/d
922         touch $DIR/R11a/f
923         mv $DIR/R11a/f $DIR/R11a/d
924         $CHECKSTAT -a $DIR/R11a/f || error
925         $CHECKSTAT -t file $DIR/R11a/d/f || error
926 }
927 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
928
929 # bug 2429 - rename foo foo foo creates invalid file
930 test_24l() {
931         f="$DIR/f24l"
932         $MULTIOP $f OcNs || error
933 }
934 run_test 24l "Renaming a file to itself ========================"
935
936 test_24m() {
937         f="$DIR/f24m"
938         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
939         # on ext3 this does not remove either the source or target files
940         # though the "expected" operation would be to remove the source
941         $CHECKSTAT -t file ${f} || error "${f} missing"
942         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
943 }
944 run_test 24m "Renaming a file to a hard link to itself ========="
945
946 test_24n() {
947     f="$DIR/f24n"
948     # this stats the old file after it was renamed, so it should fail
949     touch ${f}
950     $CHECKSTAT ${f}
951     mv ${f} ${f}.rename
952     $CHECKSTAT ${f}.rename
953     $CHECKSTAT -a ${f}
954 }
955 run_test 24n "Statting the old file after renaming (Posix rename 2)"
956
957 test_24o() {
958         check_kernel_version 37 || return 0
959         test_mkdir -p $DIR/d24o
960         rename_many -s random -v -n 10 $DIR/d24o
961 }
962 run_test 24o "rename of files during htree split ==============="
963
964 test_24p() {
965         test_mkdir $DIR/R12a
966         test_mkdir $DIR/R12b
967         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
968         mrename $DIR/R12a $DIR/R12b
969         $CHECKSTAT -a $DIR/R12a || error
970         $CHECKSTAT -t dir $DIR/R12b || error
971         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
972         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
973 }
974 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
975
976 cleanup_multiop_pause() {
977         trap 0
978         kill -USR1 $MULTIPID
979 }
980
981 test_24q() {
982         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
983         test_mkdir $DIR/R13a
984         test_mkdir $DIR/R13b
985         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
986         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
987         MULTIPID=$!
988
989         trap cleanup_multiop_pause EXIT
990         mrename $DIR/R13a $DIR/R13b
991         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
992         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
993         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
994         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
995         cleanup_multiop_pause
996         wait $MULTIPID || error "multiop close failed"
997 }
998 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
999
1000 test_24r() { #bug 3789
1001         test_mkdir $DIR/R14a
1002         test_mkdir $DIR/R14a/b
1003         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1004         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1005         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1006 }
1007 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1008
1009 test_24s() {
1010         test_mkdir $DIR/R15a
1011         test_mkdir $DIR/R15a/b
1012         test_mkdir $DIR/R15a/b/c
1013         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1014         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1015         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1016 }
1017 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1018 test_24t() {
1019         test_mkdir $DIR/R16a
1020         test_mkdir $DIR/R16a/b
1021         test_mkdir $DIR/R16a/b/c
1022         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1023         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1024         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1025 }
1026 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1027
1028 test_24u() { # bug12192
1029         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1030         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1031 }
1032 run_test 24u "create stripe file"
1033
1034 page_size() {
1035         getconf PAGE_SIZE
1036 }
1037
1038 simple_cleanup_common() {
1039         trap 0
1040         rm -rf $DIR/$tdir
1041         wait_delete_completed
1042 }
1043
1044 max_pages_per_rpc() {
1045         $LCTL get_param -n mdc.*.max_pages_per_rpc | head -n1
1046 }
1047
1048 test_24v() {
1049         local NRFILES=100000
1050         local FREE_INODES=$(mdt_free_inodes 0)
1051         [[ $FREE_INODES -lt $NRFILES ]] &&
1052                 skip "not enough free inodes $FREE_INODES required $NRFILES" &&
1053                 return
1054
1055         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1056         trap simple_cleanup_common EXIT
1057
1058         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1059         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && NRFILES=10000
1060
1061         mkdir -p $DIR/$tdir
1062         createmany -m $DIR/$tdir/$tfile $NRFILES
1063
1064         cancel_lru_locks mdc
1065         lctl set_param mdc.*.stats clear
1066
1067         ls $DIR/$tdir >/dev/null || error "error in listing large dir"
1068
1069         # LU-5 large readdir
1070         # DIRENT_SIZE = 32 bytes for sizeof(struct lu_dirent) +
1071         #               8 bytes for name(filename is mostly 5 in this test) +
1072         #               8 bytes for luda_type
1073         # take into account of overhead in lu_dirpage header and end mark in
1074         # each page, plus one in RPC_NUM calculation.
1075         DIRENT_SIZE=48
1076         RPC_SIZE=$(($(max_pages_per_rpc) * $(page_size)))
1077         RPC_NUM=$(((NRFILES * DIRENT_SIZE + RPC_SIZE - 1) / RPC_SIZE + 1))
1078         mds_readpage=$(lctl get_param mdc.*MDT0000*.stats |
1079                                 awk '/^mds_readpage/ {print $2}')
1080         [[ $mds_readpage -gt $RPC_NUM ]] &&
1081                 error "large readdir doesn't take effect"
1082
1083         simple_cleanup_common
1084 }
1085 run_test 24v "list directory with large files (handle hash collision, bug: 17560)"
1086
1087 test_24w() { # bug21506
1088         SZ1=234852
1089         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1090         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1091         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1092         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1093         [[ "$SZ1" -eq "$SZ2" ]] ||
1094                 error "Error reading at the end of the file $tfile"
1095 }
1096 run_test 24w "Reading a file larger than 4Gb"
1097
1098 test_24x() {
1099         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1101         local MDTIDX=1
1102         local remote_dir=$DIR/$tdir/remote_dir
1103
1104         mkdir -p $DIR/$tdir
1105         $LFS mkdir -i $MDTIDX $remote_dir ||
1106                 error "create remote directory failed"
1107
1108         mkdir -p $DIR/$tdir/src_dir
1109         touch $DIR/$tdir/src_file
1110         mkdir -p $remote_dir/tgt_dir
1111         touch $remote_dir/tgt_file
1112
1113         mrename $remote_dir $DIR/ &&
1114                 error "rename dir cross MDT works!"
1115
1116         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir &&
1117                 error "rename dir cross MDT works!"
1118
1119         mrename $DIR/$tdir/src_file $remote_dir/tgt_file &&
1120                 error "rename file cross MDT works!"
1121
1122         ln $DIR/$tdir/src_file $remote_dir/tgt_file1 &&
1123                 error "ln file cross MDT should not work!"
1124
1125         rm -rf $DIR/$tdir || error "Can not delete directories"
1126 }
1127 run_test 24x "cross rename/link should be failed"
1128
1129 test_24y() {
1130         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1132         local MDTIDX=1
1133         local remote_dir=$DIR/$tdir/remote_dir
1134
1135         mkdir -p $DIR/$tdir
1136         $LFS mkdir -i $MDTIDX $remote_dir ||
1137                    error "create remote directory failed"
1138
1139         mkdir -p $remote_dir/src_dir
1140         touch $remote_dir/src_file
1141         mkdir -p $remote_dir/tgt_dir
1142         touch $remote_dir/tgt_file
1143
1144         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1145                 error "rename subdir in the same remote dir failed!"
1146
1147         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1148                 error "rename files in the same remote dir failed!"
1149
1150         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1151                 error "link files in the same remote dir failed!"
1152
1153         rm -rf $DIR/$tdir || error "Can not delete directories"
1154 }
1155 run_test 24y "rename/link on the same dir should succeed"
1156
1157 test_24z() {
1158         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1160         local MDTIDX=1
1161         local remote_src=$DIR/$tdir/remote_dir
1162         local remote_tgt=$DIR/$tdir/remote_tgt
1163
1164         mkdir -p $DIR/$tdir
1165         $LFS mkdir -i $MDTIDX $remote_src ||
1166                    error "create remote directory failed"
1167
1168         $LFS mkdir -i $MDTIDX $remote_tgt ||
1169                    error "create remote directory failed"
1170
1171         mrename $remote_src $remote_tgt &&
1172                 error "rename remote dirs should not work!"
1173
1174         # If target dir does not exists, it should succeed
1175         rm -rf $remote_tgt
1176         mrename $remote_src $remote_tgt ||
1177                 error "rename remote dirs(tgt dir does not exists) failed!"
1178
1179         rm -rf $DIR/$tdir || error "Can not delete directories"
1180 }
1181 run_test 24z "rename one remote dir to another remote dir should fail"
1182
1183 test_24A() { # LU-3182
1184         local NFILES=5000
1185
1186         rm -rf $DIR/$tdir
1187         mkdir -p $DIR/$tdir
1188         createmany -m $DIR/$tdir/$tfile $NFILES
1189         local t=$(ls $DIR/$tdir | wc -l)
1190         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1191         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1192         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1193                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1194         fi
1195
1196         rm -rf $DIR/$tdir || error "Can not delete directories"
1197 }
1198 run_test 24A "readdir() returns correct number of entries."
1199
1200 test_24B() { # LU-4805
1201         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1202         local count
1203
1204         mkdir $DIR/$tdir
1205         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1206                 error "create striped dir failed"
1207
1208         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1209         [ $count -eq 2 ] || error "Expected 2, got $count"
1210
1211         touch $DIR/$tdir/striped_dir/a
1212
1213         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1214         [ $count -eq 3 ] || error "Expected 3, got $count"
1215
1216         touch $DIR/$tdir/striped_dir/.f
1217
1218         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1219         [ $count -eq 4 ] || error "Expected 4, got $count"
1220
1221         rm -rf $DIR/$tdir || error "Can not delete directories"
1222 }
1223 run_test 24B "readdir for striped dir return correct number of entries"
1224
1225 test_24C() {
1226         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1227
1228         mkdir $DIR/$tdir
1229         mkdir $DIR/$tdir/d0
1230         mkdir $DIR/$tdir/d1
1231
1232         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1233                 error "create striped dir failed"
1234
1235         cd $DIR/$tdir/d0/striped_dir
1236
1237         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1238         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1239         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1240
1241         [ "$d0_ino" = "$parent_ino" ] ||
1242                 error ".. wrong, expect $d0_ino, get $parent_ino"
1243
1244         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1245                 error "mv striped dir failed"
1246
1247         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1248
1249         [ "$d1_ino" = "$parent_ino" ] ||
1250                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1251 }
1252 run_test 24C "check .. in striped dir"
1253
1254 test_24D() { # LU-6101
1255         local NFILES=50000
1256
1257         rm -rf $DIR/$tdir
1258         mkdir -p $DIR/$tdir
1259         createmany -m $DIR/$tdir/$tfile $NFILES
1260         local t=$(ls $DIR/$tdir | wc -l)
1261         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1262         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1263         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1264                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1265         fi
1266
1267         rm -rf $DIR/$tdir || error "Can not delete directories"
1268 }
1269 run_test 24D "readdir() returns correct number of entries after cursor reload"
1270
1271 test_25a() {
1272         echo '== symlink sanity ============================================='
1273
1274         test_mkdir $DIR/d25
1275         ln -s d25 $DIR/s25
1276         touch $DIR/s25/foo || error
1277 }
1278 run_test 25a "create file in symlinked directory ==============="
1279
1280 test_25b() {
1281         [ ! -d $DIR/d25 ] && test_25a
1282         $CHECKSTAT -t file $DIR/s25/foo || error
1283 }
1284 run_test 25b "lookup file in symlinked directory ==============="
1285
1286 test_26a() {
1287         test_mkdir $DIR/d26
1288         test_mkdir $DIR/d26/d26-2
1289         ln -s d26/d26-2 $DIR/s26
1290         touch $DIR/s26/foo || error
1291 }
1292 run_test 26a "multiple component symlink ======================="
1293
1294 test_26b() {
1295         test_mkdir -p $DIR/d26b/d26-2
1296         ln -s d26b/d26-2/foo $DIR/s26-2
1297         touch $DIR/s26-2 || error
1298 }
1299 run_test 26b "multiple component symlink at end of lookup ======"
1300
1301 test_26c() {
1302         test_mkdir $DIR/d26.2
1303         touch $DIR/d26.2/foo
1304         ln -s d26.2 $DIR/s26.2-1
1305         ln -s s26.2-1 $DIR/s26.2-2
1306         ln -s s26.2-2 $DIR/s26.2-3
1307         chmod 0666 $DIR/s26.2-3/foo
1308 }
1309 run_test 26c "chain of symlinks ================================"
1310
1311 # recursive symlinks (bug 439)
1312 test_26d() {
1313         ln -s d26-3/foo $DIR/d26-3
1314 }
1315 run_test 26d "create multiple component recursive symlink ======"
1316
1317 test_26e() {
1318         [ ! -h $DIR/d26-3 ] && test_26d
1319         rm $DIR/d26-3
1320 }
1321 run_test 26e "unlink multiple component recursive symlink ======"
1322
1323 # recursive symlinks (bug 7022)
1324 test_26f() {
1325         test_mkdir -p $DIR/$tdir
1326         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1327         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1328         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1329         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1330         cd $tfile                || error "cd $tfile failed"
1331         ln -s .. dotdot          || error "ln dotdot failed"
1332         ln -s dotdot/lndir lndir || error "ln lndir failed"
1333         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1334         output=`ls $tfile/$tfile/lndir/bar1`
1335         [ "$output" = bar1 ] && error "unexpected output"
1336         rm -r $tfile             || error "rm $tfile failed"
1337         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1338 }
1339 run_test 26f "rm -r of a directory which has recursive symlink ="
1340
1341 test_27a() {
1342         echo '== stripe sanity =============================================='
1343         test_mkdir -p $DIR/d27 || error "mkdir failed"
1344         $GETSTRIPE $DIR/d27
1345         $SETSTRIPE -c 1 $DIR/d27/f0 || error "setstripe failed"
1346         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1347         log "== test_27a: write to one stripe file ========================="
1348         cp /etc/hosts $DIR/d27/f0 || error
1349 }
1350 run_test 27a "one stripe file =================================="
1351
1352 test_27b() {
1353         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
1354         test_mkdir -p $DIR/d27
1355         $SETSTRIPE -c 2 $DIR/d27/f01 || error "setstripe failed"
1356         $GETSTRIPE -c $DIR/d27/f01
1357         [ $($GETSTRIPE -c $DIR/d27/f01) -eq 2 ] ||
1358                 error "two-stripe file doesn't have two stripes"
1359
1360         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1361 }
1362 run_test 27b "create and write to two stripe file"
1363
1364 test_27d() {
1365         test_mkdir -p $DIR/d27
1366         $SETSTRIPE -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1367         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1368         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1369 }
1370 run_test 27d "create file with default settings ================"
1371
1372 test_27e() {
1373         test_mkdir -p $DIR/d27
1374         $SETSTRIPE -c 2 $DIR/d27/f12 || error "setstripe failed"
1375         $SETSTRIPE -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1376         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1377 }
1378 run_test 27e "setstripe existing file (should return error) ======"
1379
1380 test_27f() {
1381         test_mkdir $DIR/$tdir
1382         $SETSTRIPE -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1383                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1384         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1385                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1386         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1387         $GETSTRIPE $DIR/$tdir/$tfile || error "$GETSTRIPE failed"
1388 }
1389 run_test 27f "setstripe with bad stripe size (should return error)"
1390
1391 test_27g() {
1392         test_mkdir -p $DIR/d27
1393         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1394         $GETSTRIPE $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1395                 error "$DIR/d27/fnone has object"
1396 }
1397 run_test 27g "$GETSTRIPE with no objects"
1398
1399 test_27i() {
1400         touch $DIR/d27/fsome || error "touch failed"
1401         [[ $($GETSTRIPE -c $DIR/d27/fsome) -gt 0 ]] || error "missing objects"
1402 }
1403 run_test 27i "$GETSTRIPE with some objects"
1404
1405 test_27j() {
1406         test_mkdir -p $DIR/d27
1407         $SETSTRIPE -i $OSTCOUNT $DIR/d27/f27j && error "setstripe failed"||true
1408 }
1409 run_test 27j "setstripe with bad stripe offset (should return error)"
1410
1411 test_27k() { # bug 2844
1412         test_mkdir -p $DIR/d27
1413         FILE=$DIR/d27/f27k
1414         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1415         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1416         $SETSTRIPE -S 67108864 $FILE || error "setstripe failed"
1417         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1418         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1419         dd if=/dev/zero of=$FILE bs=4k count=1
1420         BLKSIZE=`stat $FILE | awk '/IO Block:/ { print $7 }'`
1421         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1422 }
1423 run_test 27k "limit i_blksize for broken user apps ============="
1424
1425 test_27l() {
1426         test_mkdir -p $DIR/d27
1427         mcreate $DIR/f27l || error "creating file"
1428         $RUNAS $SETSTRIPE -c 1 $DIR/f27l && \
1429                 error "setstripe should have failed" || true
1430 }
1431 run_test 27l "check setstripe permissions (should return error)"
1432
1433 test_27m() {
1434         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
1435                 return
1436         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1437                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1438                 return
1439         fi
1440         trap simple_cleanup_common EXIT
1441         test_mkdir -p $DIR/$tdir
1442         $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_1
1443         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1444                 error "dd should fill OST0"
1445         i=2
1446         while $SETSTRIPE -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1447                 i=$((i + 1))
1448                 [ $i -gt 256 ] && break
1449         done
1450         i=$((i + 1))
1451         touch $DIR/$tdir/f27m_$i
1452         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1453                 error "OST0 was full but new created file still use it"
1454         i=$((i + 1))
1455         touch $DIR/$tdir/f27m_$i
1456         [ `$GETSTRIPE $DIR/$tdir/f27m_$i | grep -A 10 obdidx | awk '{print $1}'| grep -w "0"` ] &&
1457                 error "OST0 was full but new created file still use it"
1458         simple_cleanup_common
1459 }
1460 run_test 27m "create file while OST0 was full =================="
1461
1462 sleep_maxage() {
1463         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage | head -n 1 | awk '{print $1 * 2}')
1464         sleep $DELAY
1465 }
1466
1467 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1468 # if the OST isn't full anymore.
1469 reset_enospc() {
1470         local OSTIDX=${1:-""}
1471
1472         local list=$(comma_list $(osts_nodes))
1473         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1474
1475         do_nodes $list lctl set_param fail_loc=0
1476         sync    # initiate all OST_DESTROYs from MDS to OST
1477         sleep_maxage
1478 }
1479
1480 exhaust_precreations() {
1481         local OSTIDX=$1
1482         local FAILLOC=$2
1483         local FAILIDX=${3:-$OSTIDX}
1484         local ofacet=ost$((OSTIDX + 1))
1485
1486         test_mkdir -p -c1 $DIR/$tdir
1487         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1488         local mfacet=mds$((mdtidx + 1))
1489         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1490
1491         local OST=$(ostname_from_index $OSTIDX)
1492
1493         # on the mdt's osc
1494         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1495         local last_id=$(do_facet $mfacet lctl get_param -n \
1496                         osc.$mdtosc_proc1.prealloc_last_id)
1497         local next_id=$(do_facet $mfacet lctl get_param -n \
1498                         osc.$mdtosc_proc1.prealloc_next_id)
1499
1500         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1501         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1502
1503         test_mkdir -p $DIR/$tdir/${OST}
1504         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1505 #define OBD_FAIL_OST_ENOSPC              0x215
1506         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1507         echo "Creating to objid $last_id on ost $OST..."
1508         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1509         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1510         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1511         sleep_maxage
1512 }
1513
1514 exhaust_all_precreations() {
1515         local i
1516         for (( i=0; i < OSTCOUNT; i++ )) ; do
1517                 exhaust_precreations $i $1 -1
1518         done
1519 }
1520
1521 test_27n() {
1522         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1524         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1525         remote_ost_nodsh && skip "remote OST with nodsh" && return
1526
1527         reset_enospc
1528         rm -f $DIR/$tdir/$tfile
1529         exhaust_precreations 0 0x80000215
1530         $SETSTRIPE -c -1 $DIR/$tdir
1531         touch $DIR/$tdir/$tfile || error
1532         $GETSTRIPE $DIR/$tdir/$tfile
1533         reset_enospc
1534 }
1535 run_test 27n "create file with some full OSTs =================="
1536
1537 test_27o() {
1538         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1539         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1540         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1541         remote_ost_nodsh && skip "remote OST with nodsh" && return
1542
1543         reset_enospc
1544         rm -f $DIR/$tdir/$tfile
1545         exhaust_all_precreations 0x215
1546
1547         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1548
1549         reset_enospc
1550         rm -rf $DIR/$tdir/*
1551 }
1552 run_test 27o "create file with all full OSTs (should error) ===="
1553
1554 test_27p() {
1555         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1556         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1557         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1558         remote_ost_nodsh && skip "remote OST with nodsh" && return
1559
1560         reset_enospc
1561         rm -f $DIR/$tdir/$tfile
1562         test_mkdir -p $DIR/$tdir
1563
1564         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1565         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1566         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1567
1568         exhaust_precreations 0 0x80000215
1569         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1570         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1571         $GETSTRIPE $DIR/$tdir/$tfile
1572
1573         reset_enospc
1574 }
1575 run_test 27p "append to a truncated file with some full OSTs ==="
1576
1577 test_27q() {
1578         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1580         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1581         remote_ost_nodsh && skip "remote OST with nodsh" && return
1582
1583         reset_enospc
1584         rm -f $DIR/$tdir/$tfile
1585
1586         test_mkdir -p $DIR/$tdir
1587         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1588         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||error "truncate $DIR/$tdir/$tfile failed"
1589         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1590
1591         exhaust_all_precreations 0x215
1592
1593         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1594         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1595
1596         reset_enospc
1597 }
1598 run_test 27q "append to truncated file with all OSTs full (should error) ==="
1599
1600 test_27r() {
1601         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1602         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1603         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1604         remote_ost_nodsh && skip "remote OST with nodsh" && return
1605
1606         reset_enospc
1607         rm -f $DIR/$tdir/$tfile
1608         exhaust_precreations 0 0x80000215
1609
1610         $SETSTRIPE -i 0 -c 2 $DIR/$tdir/$tfile # && error
1611
1612         reset_enospc
1613 }
1614 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1615
1616 test_27s() { # bug 10725
1617         test_mkdir -p $DIR/$tdir
1618         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1619         local stripe_count=0
1620         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1621         $SETSTRIPE -S $stripe_size -c $stripe_count $DIR/$tdir &&
1622                 error "stripe width >= 2^32 succeeded" || true
1623
1624 }
1625 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1626
1627 test_27t() { # bug 10864
1628         WDIR=$(pwd)
1629         WLFS=$(which lfs)
1630         cd $DIR
1631         touch $tfile
1632         $WLFS getstripe $tfile
1633         cd $WDIR
1634 }
1635 run_test 27t "check that utils parse path correctly"
1636
1637 test_27u() { # bug 4900
1638         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1639         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1640         local index
1641         local list=$(comma_list $(mdts_nodes))
1642
1643 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1644         do_nodes $list $LCTL set_param fail_loc=0x139
1645         test_mkdir -p $DIR/$tdir
1646         rm -rf $DIR/$tdir/*
1647         createmany -o $DIR/$tdir/t- 1000
1648         do_nodes $list $LCTL set_param fail_loc=0
1649
1650         TLOG=$DIR/$tfile.getstripe
1651         $GETSTRIPE $DIR/$tdir > $TLOG
1652         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1653         unlinkmany $DIR/$tdir/t- 1000
1654         [[ $OBJS -gt 0 ]] &&
1655                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1656 }
1657 run_test 27u "skip object creation on OSC w/o objects =========="
1658
1659 test_27v() { # bug 4900
1660         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
1661         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1662         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1663         remote_ost_nodsh && skip "remote OST with nodsh" && return
1664
1665         exhaust_all_precreations 0x215
1666         reset_enospc
1667
1668         test_mkdir -p $DIR/$tdir
1669         $SETSTRIPE -c 1 $DIR/$tdir         # 1 stripe / file
1670
1671         touch $DIR/$tdir/$tfile
1672         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1673         # all except ost1
1674         for (( i=1; i < OSTCOUNT; i++ )); do
1675                 do_facet ost$i lctl set_param fail_loc=0x705
1676         done
1677         local START=`date +%s`
1678         createmany -o $DIR/$tdir/$tfile 32
1679
1680         local FINISH=`date +%s`
1681         local TIMEOUT=`lctl get_param -n timeout`
1682         local PROCESS=$((FINISH - START))
1683         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1684                error "$FINISH - $START >= $TIMEOUT / 2"
1685         sleep $((TIMEOUT / 2 - PROCESS))
1686         reset_enospc
1687 }
1688 run_test 27v "skip object creation on slow OST ================="
1689
1690 test_27w() { # bug 10997
1691         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1692         $SETSTRIPE -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1693         [ $($GETSTRIPE -S $DIR/$tdir/f0) -ne 65536 ] &&
1694                 error "stripe size $size != 65536" || true
1695         [ $($GETSTRIPE -d $DIR/$tdir | grep -c "stripe_count") -ne 1 ] &&
1696                 error "$GETSTRIPE -d $DIR/$tdir failed" || true
1697 }
1698 run_test 27w "check $SETSTRIPE -S option"
1699
1700 test_27wa() {
1701         [[ $OSTCOUNT -lt 2 ]] &&
1702                 skip_env "skipping multiple stripe count/offset test" && return
1703
1704         test_mkdir -p $DIR/$tdir || error "mkdir failed"
1705         for i in $(seq 1 $OSTCOUNT); do
1706                 offset=$((i - 1))
1707                 $SETSTRIPE -c $i -i $offset $DIR/$tdir/f$i ||
1708                         error "setstripe -c $i -i $offset failed"
1709                 count=$($GETSTRIPE -c $DIR/$tdir/f$i)
1710                 index=$($GETSTRIPE -i $DIR/$tdir/f$i)
1711                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1712                 [ $index -ne $offset ] &&
1713                         error "stripe offset $index != $offset" || true
1714         done
1715 }
1716 run_test 27wa "check $SETSTRIPE -c -i options"
1717
1718 test_27x() {
1719         remote_ost_nodsh && skip "remote OST with nodsh" && return
1720         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
1721         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1722         OFFSET=$(($OSTCOUNT - 1))
1723         OSTIDX=0
1724         local OST=$(ostname_from_index $OSTIDX)
1725
1726         test_mkdir -p $DIR/$tdir
1727         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe per file
1728         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1729         sleep_maxage
1730         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1731         for i in `seq 0 $OFFSET`; do
1732                 [ `$GETSTRIPE $DIR/$tdir/$tfile$i | grep -A 10 obdidx | awk '{print $1}' | grep -w "$OSTIDX"` ] &&
1733                 error "OST0 was degraded but new created file still use it"
1734         done
1735         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1736 }
1737 run_test 27x "create files while OST0 is degraded"
1738
1739 test_27y() {
1740         [[ $OSTCOUNT -lt 2 ]] &&
1741                 skip_env "$OSTCOUNT < 2 OSTs -- skipping" && return
1742         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1743         remote_ost_nodsh && skip "remote OST with nodsh" && return
1744         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1745
1746         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1747         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1748             osc.$mdtosc.prealloc_last_id)
1749         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1750             osc.$mdtosc.prealloc_next_id)
1751         local fcount=$((last_id - next_id))
1752         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1753         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1754
1755         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1756                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1757         local OST_DEACTIVE_IDX=-1
1758         local OSC
1759         local OSTIDX
1760         local OST
1761
1762         for OSC in $MDS_OSCS; do
1763                 OST=$(osc_to_ost $OSC)
1764                 OSTIDX=$(index_from_ostuuid $OST)
1765                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1766                         OST_DEACTIVE_IDX=$OSTIDX
1767                 fi
1768                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1769                         echo $OSC "is Deactivated:"
1770                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1771                 fi
1772         done
1773
1774         OSTIDX=$(index_from_ostuuid $OST)
1775         mkdir -p $DIR/$tdir
1776         $SETSTRIPE -c 1 $DIR/$tdir      # 1 stripe / file
1777
1778         for OSC in $MDS_OSCS; do
1779                 OST=$(osc_to_ost $OSC)
1780                 OSTIDX=$(index_from_ostuuid $OST)
1781                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1782                         echo $OST "is degraded:"
1783                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1784                                                 obdfilter.$OST.degraded=1
1785                 fi
1786         done
1787
1788         sleep_maxage
1789         createmany -o $DIR/$tdir/$tfile $fcount
1790
1791         for OSC in $MDS_OSCS; do
1792                 OST=$(osc_to_ost $OSC)
1793                 OSTIDX=$(index_from_ostuuid $OST)
1794                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1795                         echo $OST "is recovered from degraded:"
1796                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1797                                                 obdfilter.$OST.degraded=0
1798                 else
1799                         do_facet $SINGLEMDS lctl --device %$OSC activate
1800                 fi
1801         done
1802
1803         # all osp devices get activated, hence -1 stripe count restored
1804         local stripecnt=0
1805
1806         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1807         # devices get activated.
1808         sleep_maxage
1809         $SETSTRIPE -c -1 $DIR/$tfile
1810         stripecnt=$($GETSTRIPE -c $DIR/$tfile)
1811         rm -f $DIR/$tfile
1812         [ $stripecnt -ne $OSTCOUNT ] &&
1813                 error "Of $OSTCOUNT OSTs, only $stripecnt is available"
1814         return 0
1815 }
1816 run_test 27y "create files while OST0 is degraded and the rest inactive"
1817
1818 check_seq_oid()
1819 {
1820         log "check file $1"
1821
1822         lmm_count=$($GETSTRIPE -c $1)
1823         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1824         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1825
1826         local old_ifs="$IFS"
1827         IFS=$'[:]'
1828         fid=($($LFS path2fid $1))
1829         IFS="$old_ifs"
1830
1831         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1832         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1833
1834         # compare lmm_seq and lu_fid->f_seq
1835         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1836         # compare lmm_object_id and lu_fid->oid
1837         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1838
1839         # check the trusted.fid attribute of the OST objects of the file
1840         local have_obdidx=false
1841         local stripe_nr=0
1842         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1843                 # skip lines up to and including "obdidx"
1844                 [ -z "$obdidx" ] && break
1845                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1846                 $have_obdidx || continue
1847
1848                 local ost=$((obdidx + 1))
1849                 local dev=$(ostdevname $ost)
1850                 local oid_hex
1851
1852                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1853
1854                 seq=$(echo $seq | sed -e "s/^0x//g")
1855                 if [ $seq == 0 ]; then
1856                         oid_hex=$(echo $oid)
1857                 else
1858                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1859                 fi
1860                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1861
1862                 local ff
1863                 #
1864                 # Don't unmount/remount the OSTs if we don't need to do that.
1865                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1866                 # update too, until that use mount/ll_decode_filter_fid/mount.
1867                 # Re-enable when debugfs will understand new filter_fid.
1868                 #
1869                 if false && [ $(facet_fstype ost$ost) == ldiskfs ]; then
1870                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1871                                 $dev 2>/dev/null" | grep "parent=")
1872                 else
1873                         stop ost$ost
1874                         mount_fstype ost$ost
1875                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1876                                 $(facet_mntpt ost$ost)/$obj_file)
1877                         unmount_fstype ost$ost
1878                         start ost$ost $dev $OST_MOUNT_OPTS
1879                 fi
1880
1881                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1882
1883                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1884
1885                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1886                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1887                 local ff_parent=$(echo $ff|sed -e 's/.*parent=.//')
1888                 local ff_pseq=$(echo $ff_parent | cut -d: -f1)
1889                 local ff_poid=$(echo $ff_parent | cut -d: -f2)
1890                 local ff_pstripe
1891                 if echo $ff_parent | grep -q 'stripe='; then
1892                         ff_pstripe=$(echo $ff_parent | sed -e 's/.*stripe=//')
1893                 else
1894                         #
1895                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1896                         # into f_ver in this case.  See the comment on
1897                         # ff_parent.
1898                         #
1899                         ff_pstripe=$(echo $ff_parent | cut -d: -f3 |
1900                                 sed -e 's/\]//')
1901                 fi
1902
1903                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1904                 [ $ff_pseq = $lmm_seq ] ||
1905                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1906                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1907                 [ $ff_poid = $lmm_oid ] ||
1908                         error "FF parent OID $ff_poid != $lmm_oid"
1909                 (($ff_pstripe == $stripe_nr)) ||
1910                         error "FF stripe $ff_pstripe != $stripe_nr"
1911
1912                 stripe_nr=$((stripe_nr + 1))
1913         done
1914 }
1915
1916 test_27z() {
1917         remote_ost_nodsh && skip "remote OST with nodsh" && return
1918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1919         test_mkdir -p $DIR/$tdir
1920
1921         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1922                 { error "setstripe -c -1 failed"; return 1; }
1923         # We need to send a write to every object to get parent FID info set.
1924         # This _should_ also work for setattr, but does not currently.
1925         # touch $DIR/$tdir/$tfile-1 ||
1926         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1927                 { error "dd $tfile-1 failed"; return 2; }
1928         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1929                 { error "setstripe -c -1 failed"; return 3; }
1930         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1931                 { error "dd $tfile-2 failed"; return 4; }
1932
1933         # make sure write RPCs have been sent to OSTs
1934         sync; sleep 5; sync
1935
1936         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1937         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1938 }
1939 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1940
1941 test_27A() { # b=19102
1942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1943         local restore_size=$($GETSTRIPE -S $MOUNT)
1944         local restore_count=$($GETSTRIPE -c $MOUNT)
1945         local restore_offset=$($GETSTRIPE -i $MOUNT)
1946         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1947         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1948                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1949         local default_size=$($GETSTRIPE -S $MOUNT)
1950         local default_offset=$($GETSTRIPE -i $MOUNT)
1951         local dsize=$((1024 * 1024))
1952         [ $default_size -eq $dsize ] ||
1953                 error "stripe size $default_size != $dsize"
1954         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1955         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1956 }
1957 run_test 27A "check filesystem-wide default LOV EA values"
1958
1959 test_27B() { # LU-2523
1960         test_mkdir -p $DIR/$tdir
1961         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1962         touch $DIR/$tdir/f0
1963         # open f1 with O_LOV_DELAY_CREATE
1964         # rename f0 onto f1
1965         # call setstripe ioctl on open file descriptor for f1
1966         # close
1967         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1968                 $DIR/$tdir/f0
1969
1970         rm -f $DIR/$tdir/f1
1971         # open f1 with O_LOV_DELAY_CREATE
1972         # unlink f1
1973         # call setstripe ioctl on open file descriptor for f1
1974         # close
1975         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
1976
1977         # Allow multiop to fail in imitation of NFS's busted semantics.
1978         true
1979 }
1980 run_test 27B "call setstripe on open unlinked file/rename victim"
1981
1982 test_27C() { #LU-2871
1983         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
1984
1985         declare -a ost_idx
1986         local index
1987         local found
1988         local i
1989         local j
1990
1991         test_mkdir -p $DIR/$tdir
1992         cd $DIR/$tdir
1993         for i in $(seq 0 $((OSTCOUNT - 1))); do
1994                 # set stripe across all OSTs starting from OST$i
1995                 $SETSTRIPE -i $i -c -1 $tfile$i
1996                 # get striping information
1997                 ost_idx=($($GETSTRIPE $tfile$i |
1998                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
1999                 echo ${ost_idx[@]}
2000
2001                 # check the layout
2002                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2003                         error "${#ost_idx[@]} != $OSTCOUNT"
2004
2005                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2006                         found=0
2007                         for j in $(echo ${ost_idx[@]}); do
2008                                 if [ $index -eq $j ]; then
2009                                         found=1
2010                                         break
2011                                 fi
2012                         done
2013                         [ $found = 1 ] ||
2014                                 error "Can not find $index in ${ost_idx[@]}"
2015                 done
2016         done
2017 }
2018 run_test 27C "check full striping across all OSTs"
2019
2020 test_27D() {
2021         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2022         local POOL=${POOL:-testpool}
2023         local first_ost=0
2024         local last_ost=$(($OSTCOUNT - 1))
2025         local ost_step=1
2026         local ost_list=$(seq $first_ost $ost_step $last_ost)
2027         local ost_range="$first_ost $last_ost $ost_step"
2028
2029         mkdir -p $DIR/$tdir
2030         pool_add $POOL || error "pool_add failed"
2031         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2032         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT ||
2033                 error "llapi_layout_test failed"
2034         cleanup_pools || error "cleanup_pools failed"
2035 }
2036 run_test 27D "validate llapi_layout API"
2037
2038 # createtest also checks that device nodes are created and
2039 # then visible correctly (#2091)
2040 test_28() { # bug 2091
2041         test_mkdir $DIR/d28
2042         $CREATETEST $DIR/d28/ct || error
2043 }
2044 run_test 28 "create/mknod/mkdir with bad file types ============"
2045
2046 test_29() {
2047         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2048         cancel_lru_locks mdc
2049         test_mkdir $DIR/d29
2050         touch $DIR/d29/foo
2051         log 'first d29'
2052         ls -l $DIR/d29
2053
2054         declare -i LOCKCOUNTORIG=0
2055         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2056                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2057         done
2058         [ $LOCKCOUNTORIG -eq 0 ] && echo "No mdc lock count" && return 1
2059
2060         declare -i LOCKUNUSEDCOUNTORIG=0
2061         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2062                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2063         done
2064
2065         log 'second d29'
2066         ls -l $DIR/d29
2067         log 'done'
2068
2069         declare -i LOCKCOUNTCURRENT=0
2070         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2071                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2072         done
2073
2074         declare -i LOCKUNUSEDCOUNTCURRENT=0
2075         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2076                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2077         done
2078
2079         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2080                 $LCTL set_param -n ldlm.dump_namespaces ""
2081                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2082                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2083                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2084                 return 2
2085         fi
2086         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2087                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2088                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2089                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2090                 return 3
2091         fi
2092 }
2093 run_test 29 "IT_GETATTR regression  ============================"
2094
2095 test_30a() { # was test_30
2096         cp $(which ls) $DIR || cp /bin/ls $DIR
2097         $DIR/ls / || error
2098         rm $DIR/ls
2099 }
2100 run_test 30a "execute binary from Lustre (execve) =============="
2101
2102 test_30b() {
2103         cp `which ls` $DIR || cp /bin/ls $DIR
2104         chmod go+rx $DIR/ls
2105         $RUNAS $DIR/ls / || error
2106         rm $DIR/ls
2107 }
2108 run_test 30b "execute binary from Lustre as non-root ==========="
2109
2110 test_30c() { # b=22376
2111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2112         cp `which ls` $DIR || cp /bin/ls $DIR
2113         chmod a-rw $DIR/ls
2114         cancel_lru_locks mdc
2115         cancel_lru_locks osc
2116         $RUNAS $DIR/ls / || error
2117         rm -f $DIR/ls
2118 }
2119 run_test 30c "execute binary from Lustre without read perms ===="
2120
2121 test_31a() {
2122         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2123         $CHECKSTAT -a $DIR/f31 || error
2124 }
2125 run_test 31a "open-unlink file =================================="
2126
2127 test_31b() {
2128         touch $DIR/f31 || error
2129         ln $DIR/f31 $DIR/f31b || error
2130         $MULTIOP $DIR/f31b Ouc || error
2131         $CHECKSTAT -t file $DIR/f31 || error
2132 }
2133 run_test 31b "unlink file with multiple links while open ======="
2134
2135 test_31c() {
2136         touch $DIR/f31 || error
2137         ln $DIR/f31 $DIR/f31c || error
2138         multiop_bg_pause $DIR/f31 O_uc || return 1
2139         MULTIPID=$!
2140         $MULTIOP $DIR/f31c Ouc
2141         kill -USR1 $MULTIPID
2142         wait $MULTIPID
2143 }
2144 run_test 31c "open-unlink file with multiple links ============="
2145
2146 test_31d() {
2147         opendirunlink $DIR/d31d $DIR/d31d || error
2148         $CHECKSTAT -a $DIR/d31d || error
2149 }
2150 run_test 31d "remove of open directory ========================="
2151
2152 test_31e() { # bug 2904
2153         check_kernel_version 34 || return 0
2154         openfilleddirunlink $DIR/d31e || error
2155 }
2156 run_test 31e "remove of open non-empty directory ==============="
2157
2158 test_31f() { # bug 4554
2159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2160         set -vx
2161         test_mkdir $DIR/d31f
2162         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2163         cp /etc/hosts $DIR/d31f
2164         ls -l $DIR/d31f
2165         $GETSTRIPE $DIR/d31f/hosts
2166         multiop_bg_pause $DIR/d31f D_c || return 1
2167         MULTIPID=$!
2168
2169         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2170         test_mkdir $DIR/d31f
2171         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2172         cp /etc/hosts $DIR/d31f
2173         ls -l $DIR/d31f
2174         $GETSTRIPE $DIR/d31f/hosts
2175         multiop_bg_pause $DIR/d31f D_c || return 1
2176         MULTIPID2=$!
2177
2178         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2179         wait $MULTIPID || error "first opendir $MULTIPID failed"
2180
2181         sleep 6
2182
2183         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2184         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2185         set +vx
2186 }
2187 run_test 31f "remove of open directory with open-unlink file ==="
2188
2189 test_31g() {
2190         echo "-- cross directory link --"
2191         test_mkdir -c1 $DIR/${tdir}ga
2192         test_mkdir -c1 $DIR/${tdir}gb
2193         touch $DIR/${tdir}ga/f
2194         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2195         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2196         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2197         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2198         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2199 }
2200 run_test 31g "cross directory link==============="
2201
2202 test_31h() {
2203         echo "-- cross directory link --"
2204         test_mkdir -c1 $DIR/${tdir}
2205         test_mkdir -c1 $DIR/${tdir}/dir
2206         touch $DIR/${tdir}/f
2207         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2208         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2209         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2210         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2211         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2212 }
2213 run_test 31h "cross directory link under child==============="
2214
2215 test_31i() {
2216         echo "-- cross directory link --"
2217         test_mkdir -c1 $DIR/$tdir
2218         test_mkdir -c1 $DIR/$tdir/dir
2219         touch $DIR/$tdir/dir/f
2220         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2221         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2222         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2223         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2224         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2225 }
2226 run_test 31i "cross directory link under parent==============="
2227
2228 test_31j() {
2229         test_mkdir -c1 -p $DIR/$tdir
2230         test_mkdir -c1 -p $DIR/$tdir/dir1
2231         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2232         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2233         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2234         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2235         return 0
2236 }
2237 run_test 31j "link for directory==============="
2238
2239 test_31k() {
2240         test_mkdir -c1 -p $DIR/$tdir
2241         touch $DIR/$tdir/s
2242         touch $DIR/$tdir/exist
2243         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2244         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2245         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2246         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2247         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2248         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2249         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2250         return 0
2251 }
2252 run_test 31k "link to file: the same, non-existing, dir==============="
2253
2254 test_31m() {
2255         mkdir $DIR/d31m
2256         touch $DIR/d31m/s
2257         mkdir $DIR/d31m2
2258         touch $DIR/d31m2/exist
2259         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2260         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2261         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2262         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2263         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2264         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2265         return 0
2266 }
2267 run_test 31m "link to file: the same, non-existing, dir==============="
2268
2269 test_31n() {
2270         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2271         nlink=$(stat --format=%h $DIR/$tfile)
2272         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2273         local fd=$(free_fd)
2274         local cmd="exec $fd<$DIR/$tfile"
2275         eval $cmd
2276         cmd="exec $fd<&-"
2277         trap "eval $cmd" EXIT
2278         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2279         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2280         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2281         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2282         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2283         eval $cmd
2284 }
2285 run_test 31n "check link count of unlinked file"
2286
2287 link_one() {
2288         local TEMPNAME=$(mktemp $1_XXXXXX)
2289         mlink $TEMPNAME $1 2> /dev/null &&
2290                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2291         munlink $TEMPNAME
2292 }
2293
2294 test_31o() { # LU-2901
2295         mkdir -p $DIR/$tdir
2296         for LOOP in $(seq 100); do
2297                 rm -f $DIR/$tdir/$tfile*
2298                 for THREAD in $(seq 8); do
2299                         link_one $DIR/$tdir/$tfile.$LOOP &
2300                 done
2301                 wait
2302                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2303                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2304                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2305                         break || true
2306         done
2307 }
2308 run_test 31o "duplicate hard links with same filename"
2309
2310 test_31p() {
2311         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2312
2313         mkdir $DIR/$tdir
2314         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2315         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2316
2317         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2318                 error "open unlink test1 failed"
2319         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2320                 error "open unlink test2 failed"
2321
2322         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2323                 error "test1 still exists"
2324         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2325                 error "test2 still exists"
2326 }
2327 run_test 31p "remove of open striped directory"
2328
2329 cleanup_test32_mount() {
2330         trap 0
2331         $UMOUNT -d $DIR/$tdir/ext2-mountpoint
2332 }
2333
2334 test_32a() {
2335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2336         echo "== more mountpoints and symlinks ================="
2337         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2338         trap cleanup_test32_mount EXIT
2339         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2340         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2341         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2342         cleanup_test32_mount
2343 }
2344 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2345
2346 test_32b() {
2347         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2348         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2349         trap cleanup_test32_mount EXIT
2350         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2351         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2352         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2353         cleanup_test32_mount
2354 }
2355 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2356
2357 test_32c() {
2358         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2359         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2360         trap cleanup_test32_mount EXIT
2361         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2362         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2363         test_mkdir -p $DIR/$tdir/d2/test_dir
2364         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2365         cleanup_test32_mount
2366 }
2367 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2368
2369 test_32d() {
2370         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2371         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2372         trap cleanup_test32_mount EXIT
2373         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2374         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2375         test_mkdir -p $DIR/$tdir/d2/test_dir
2376         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2377         cleanup_test32_mount
2378 }
2379 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2380
2381 test_32e() {
2382         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2383         test_mkdir -p $DIR/d32e/tmp
2384         TMP_DIR=$DIR/d32e/tmp
2385         ln -s $DIR/d32e $TMP_DIR/symlink11
2386         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2387         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2388         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2389 }
2390 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2391
2392 test_32f() {
2393         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2394         test_mkdir -p $DIR/d32f/tmp
2395         TMP_DIR=$DIR/d32f/tmp
2396         ln -s $DIR/d32f $TMP_DIR/symlink11
2397         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2398         ls $DIR/d32f/tmp/symlink11  || error
2399         ls $DIR/d32f/symlink01 || error
2400 }
2401 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2402
2403 test_32g() {
2404         TMP_DIR=$DIR/$tdir/tmp
2405         test_mkdir -p $DIR/$tdir/tmp
2406         test_mkdir $DIR/${tdir}2
2407         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2408         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2409         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2410         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2411         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2412         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2413 }
2414 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2415
2416 test_32h() {
2417         rm -fr $DIR/$tdir $DIR/${tdir}2
2418         TMP_DIR=$DIR/$tdir/tmp
2419         test_mkdir -p $DIR/$tdir/tmp
2420         test_mkdir $DIR/${tdir}2
2421         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2422         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2423         ls $TMP_DIR/symlink12 || error
2424         ls $DIR/$tdir/symlink02  || error
2425 }
2426 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2427
2428 test_32i() {
2429         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2430         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2431         trap cleanup_test32_mount EXIT
2432         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2433         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2434         touch $DIR/$tdir/test_file
2435         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2436         cleanup_test32_mount
2437 }
2438 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2439
2440 test_32j() {
2441         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2442         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2443         trap cleanup_test32_mount EXIT
2444         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2445         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2446         touch $DIR/$tdir/test_file
2447         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2448         cleanup_test32_mount
2449 }
2450 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2451
2452 test_32k() {
2453         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2454         rm -fr $DIR/$tdir
2455         trap cleanup_test32_mount EXIT
2456         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2457         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2458         test_mkdir -p $DIR/$tdir/d2
2459         touch $DIR/$tdir/d2/test_file || error
2460         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2461         cleanup_test32_mount
2462 }
2463 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2464
2465 test_32l() {
2466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2467         rm -fr $DIR/$tdir
2468         trap cleanup_test32_mount EXIT
2469         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2470         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2471         test_mkdir -p $DIR/$tdir/d2
2472         touch $DIR/$tdir/d2/test_file
2473         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2474         cleanup_test32_mount
2475 }
2476 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2477
2478 test_32m() {
2479         rm -fr $DIR/d32m
2480         test_mkdir -p $DIR/d32m/tmp
2481         TMP_DIR=$DIR/d32m/tmp
2482         ln -s $DIR $TMP_DIR/symlink11
2483         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2484         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2485         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2486 }
2487 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2488
2489 test_32n() {
2490         rm -fr $DIR/d32n
2491         test_mkdir -p $DIR/d32n/tmp
2492         TMP_DIR=$DIR/d32n/tmp
2493         ln -s $DIR $TMP_DIR/symlink11
2494         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2495         ls -l $DIR/d32n/tmp/symlink11  || error
2496         ls -l $DIR/d32n/symlink01 || error
2497 }
2498 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2499
2500 test_32o() {
2501         touch $DIR/$tfile
2502         test_mkdir -p $DIR/d32o/tmp
2503         TMP_DIR=$DIR/d32o/tmp
2504         ln -s $DIR/$tfile $TMP_DIR/symlink12
2505         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2506         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2507         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2508         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2509         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2510 }
2511 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2512
2513 test_32p() {
2514     log 32p_1
2515         rm -fr $DIR/d32p
2516     log 32p_2
2517         rm -f $DIR/$tfile
2518     log 32p_3
2519         touch $DIR/$tfile
2520     log 32p_4
2521         test_mkdir -p $DIR/d32p/tmp
2522     log 32p_5
2523         TMP_DIR=$DIR/d32p/tmp
2524     log 32p_6
2525         ln -s $DIR/$tfile $TMP_DIR/symlink12
2526     log 32p_7
2527         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2528     log 32p_8
2529         cat $DIR/d32p/tmp/symlink12 || error
2530     log 32p_9
2531         cat $DIR/d32p/symlink02 || error
2532     log 32p_10
2533 }
2534 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2535
2536 cleanup_testdir_mount() {
2537         trap 0
2538         $UMOUNT -d $DIR/$tdir
2539 }
2540
2541 test_32q() {
2542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2543         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2544         trap cleanup_testdir_mount EXIT
2545         test_mkdir -p $DIR/$tdir
2546         touch $DIR/$tdir/under_the_mount
2547         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2548         ls $DIR/$tdir | grep "\<under_the_mount\>" && error
2549         cleanup_testdir_mount
2550 }
2551 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2552
2553 test_32r() {
2554         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2555         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2556         trap cleanup_testdir_mount EXIT
2557         test_mkdir -p $DIR/$tdir
2558         touch $DIR/$tdir/under_the_mount
2559         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir
2560         ls $DIR/$tdir | grep -q under_the_mount && error || true
2561         cleanup_testdir_mount
2562 }
2563 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2564
2565 test_33aa() {
2566         rm -f $DIR/$tfile
2567         touch $DIR/$tfile
2568         chmod 444 $DIR/$tfile
2569         chown $RUNAS_ID $DIR/$tfile
2570         log 33_1
2571         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2572         log 33_2
2573 }
2574 run_test 33aa "write file with mode 444 (should return error) ===="
2575
2576 test_33a() {
2577         rm -fr $DIR/d33
2578         test_mkdir -p $DIR/d33
2579         chown $RUNAS_ID $DIR/d33
2580         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2581         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2582                 error "open RDWR" || true
2583 }
2584 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2585
2586 test_33b() {
2587         rm -fr $DIR/d33
2588         test_mkdir -p $DIR/d33
2589         chown $RUNAS_ID $DIR/d33
2590         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2591 }
2592 run_test 33b "test open file with malformed flags (No panic)"
2593
2594 test_33c() {
2595         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2596         local ostnum
2597         local ostname
2598         local write_bytes
2599         local all_zeros
2600
2601         remote_ost_nodsh && skip "remote OST with nodsh" && return
2602         all_zeros=:
2603         rm -fr $DIR/d33
2604         test_mkdir -p $DIR/d33
2605         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2606
2607         sync
2608         for ostnum in $(seq $OSTCOUNT); do
2609                 # test-framework's OST numbering is one-based, while Lustre's
2610                 # is zero-based
2611                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2612                 # Parsing llobdstat's output sucks; we could grep the /proc
2613                 # path, but that's likely to not be as portable as using the
2614                 # llobdstat utility.  So we parse lctl output instead.
2615                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2616                         obdfilter/$ostname/stats |
2617                         awk '/^write_bytes/ {print $7}' )
2618                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2619                 if (( ${write_bytes:-0} > 0 ))
2620                 then
2621                         all_zeros=false
2622                         break;
2623                 fi
2624         done
2625
2626         $all_zeros || return 0
2627
2628         # Write four bytes
2629         echo foo > $DIR/d33/bar
2630         # Really write them
2631         sync
2632
2633         # Total up write_bytes after writing.  We'd better find non-zeros.
2634         for ostnum in $(seq $OSTCOUNT); do
2635                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2636                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2637                         obdfilter/$ostname/stats |
2638                         awk '/^write_bytes/ {print $7}' )
2639                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2640                 if (( ${write_bytes:-0} > 0 ))
2641                 then
2642                         all_zeros=false
2643                         break;
2644                 fi
2645         done
2646
2647         if $all_zeros
2648         then
2649                 for ostnum in $(seq $OSTCOUNT); do
2650                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2651                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2652                         do_facet ost$ostnum lctl get_param -n \
2653                                 obdfilter/$ostname/stats
2654                 done
2655                 error "OST not keeping write_bytes stats (b22312)"
2656         fi
2657 }
2658 run_test 33c "test llobdstat and write_bytes"
2659
2660 test_33d() {
2661         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2663         local MDTIDX=1
2664         local remote_dir=$DIR/$tdir/remote_dir
2665
2666         mkdir -p $DIR/$tdir
2667         $LFS mkdir -i $MDTIDX $remote_dir ||
2668                 error "create remote directory failed"
2669
2670         touch $remote_dir/$tfile
2671         chmod 444 $remote_dir/$tfile
2672         chown $RUNAS_ID $remote_dir/$tfile
2673
2674         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2675
2676         chown $RUNAS_ID $remote_dir
2677         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2678                                         error "create" || true
2679         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2680                                     error "open RDWR" || true
2681         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 &&
2682                                     error "create" || true
2683 }
2684 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2685
2686 test_33e() {
2687         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2688
2689         mkdir $DIR/$tdir
2690
2691         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2692         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2693         mkdir $DIR/$tdir/local_dir
2694
2695         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2696         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2697         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2698
2699         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2700                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2701
2702         rmdir $DIR/$tdir/* || error "rmdir failed"
2703
2704         umask 777
2705         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2706         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2707         mkdir $DIR/$tdir/local_dir
2708
2709         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2710         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2711         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2712
2713         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2714                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2715
2716         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2717
2718         umask 000
2719         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2720         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2721         mkdir $DIR/$tdir/local_dir
2722
2723         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2724         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2725         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2726
2727         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2728                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2729 }
2730 run_test 33e "mkdir and striped directory should have same mode"
2731
2732 cleanup_33f() {
2733         trap 0
2734         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2735 }
2736
2737 test_33f() {
2738         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2739
2740         mkdir $DIR/$tdir
2741         chmod go+rwx $DIR/$tdir
2742         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2743         trap cleanup_33f EXIT
2744
2745         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2746                 error "cannot create striped directory"
2747
2748         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2749                 error "cannot create files in striped directory"
2750
2751         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2752                 error "cannot remove files in striped directory"
2753
2754         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2755                 error "cannot remove striped directory"
2756
2757         cleanup_33f
2758 }
2759 run_test 33f "nonroot user can create, access, and remove a striped directory"
2760
2761 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2762 test_34a() {
2763         rm -f $DIR/f34
2764         $MCREATE $DIR/f34 || error
2765         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2766         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2767         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2768         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2769 }
2770 run_test 34a "truncate file that has not been opened ==========="
2771
2772 test_34b() {
2773         [ ! -f $DIR/f34 ] && test_34a
2774         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2775         $OPENFILE -f O_RDONLY $DIR/f34
2776         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2777         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2778 }
2779 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2780
2781 test_34c() {
2782         [ ! -f $DIR/f34 ] && test_34a
2783         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2784         $OPENFILE -f O_RDWR $DIR/f34
2785         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2786         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2787 }
2788 run_test 34c "O_RDWR opening file-with-size works =============="
2789
2790 test_34d() {
2791         [ ! -f $DIR/f34 ] && test_34a
2792         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2793         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2794         rm $DIR/f34
2795 }
2796 run_test 34d "write to sparse file ============================="
2797
2798 test_34e() {
2799         rm -f $DIR/f34e
2800         $MCREATE $DIR/f34e || error
2801         $TRUNCATE $DIR/f34e 1000 || error
2802         $CHECKSTAT -s 1000 $DIR/f34e || error
2803         $OPENFILE -f O_RDWR $DIR/f34e
2804         $CHECKSTAT -s 1000 $DIR/f34e || error
2805 }
2806 run_test 34e "create objects, some with size and some without =="
2807
2808 test_34f() { # bug 6242, 6243
2809         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2810         SIZE34F=48000
2811         rm -f $DIR/f34f
2812         $MCREATE $DIR/f34f || error
2813         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2814         dd if=$DIR/f34f of=$TMP/f34f
2815         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2816         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2817         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2818         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2819         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2820 }
2821 run_test 34f "read from a file with no objects until EOF ======="
2822
2823 test_34g() {
2824         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2825         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2826         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2827         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2828         cancel_lru_locks osc
2829         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2830                 error "wrong size after lock cancel"
2831
2832         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2833         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2834                 error "expanding truncate failed"
2835         cancel_lru_locks osc
2836         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2837                 error "wrong expanded size after lock cancel"
2838 }
2839 run_test 34g "truncate long file ==============================="
2840
2841 test_34h() {
2842         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2843         local gid=10
2844         local sz=1000
2845
2846         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2847         sync # Flush the cache so that multiop below does not block on cache
2848              # flush when getting the group lock
2849         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2850         MULTIPID=$!
2851
2852         # Since just timed wait is not good enough, let's do a sync write
2853         # that way we are sure enough time for a roundtrip + processing
2854         # passed + 2 seconds of extra margin.
2855         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2856         rm $DIR/${tfile}-1
2857         sleep 2
2858
2859         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2860                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2861                 kill -9 $MULTIPID
2862         fi
2863         wait $MULTIPID
2864         local nsz=`stat -c %s $DIR/$tfile`
2865         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2866 }
2867 run_test 34h "ftruncate file under grouplock should not block"
2868
2869 test_35a() {
2870         cp /bin/sh $DIR/f35a
2871         chmod 444 $DIR/f35a
2872         chown $RUNAS_ID $DIR/f35a
2873         $RUNAS $DIR/f35a && error || true
2874         rm $DIR/f35a
2875 }
2876 run_test 35a "exec file with mode 444 (should return and not leak) ====="
2877
2878 test_36a() {
2879         rm -f $DIR/f36
2880         utime $DIR/f36 || error
2881 }
2882 run_test 36a "MDS utime check (mknod, utime) ==================="
2883
2884 test_36b() {
2885         echo "" > $DIR/f36
2886         utime $DIR/f36 || error
2887 }
2888 run_test 36b "OST utime check (open, utime) ===================="
2889
2890 test_36c() {
2891         rm -f $DIR/d36/f36
2892         test_mkdir $DIR/d36
2893         chown $RUNAS_ID $DIR/d36
2894         $RUNAS utime $DIR/d36/f36 || error
2895 }
2896 run_test 36c "non-root MDS utime check (mknod, utime) =========="
2897
2898 test_36d() {
2899         [ ! -d $DIR/d36 ] && test_36c
2900         echo "" > $DIR/d36/f36
2901         $RUNAS utime $DIR/d36/f36 || error
2902 }
2903 run_test 36d "non-root OST utime check (open, utime) ==========="
2904
2905 test_36e() {
2906         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
2907         test_mkdir -p $DIR/$tdir
2908         touch $DIR/$tdir/$tfile
2909         $RUNAS utime $DIR/$tdir/$tfile && \
2910                 error "utime worked, expected failure" || true
2911 }
2912 run_test 36e "utime on non-owned file (should return error) ===="
2913
2914 subr_36fh() {
2915         local fl="$1"
2916         local LANG_SAVE=$LANG
2917         local LC_LANG_SAVE=$LC_LANG
2918         export LANG=C LC_LANG=C # for date language
2919
2920         DATESTR="Dec 20  2000"
2921         test_mkdir -p $DIR/$tdir
2922         lctl set_param fail_loc=$fl
2923         date; date +%s
2924         cp /etc/hosts $DIR/$tdir/$tfile
2925         sync & # write RPC generated with "current" inode timestamp, but delayed
2926         sleep 1
2927         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
2928         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
2929         cancel_lru_locks osc
2930         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
2931         date; date +%s
2932         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
2933                 echo "BEFORE: $LS_BEFORE" && \
2934                 echo "AFTER : $LS_AFTER" && \
2935                 echo "WANT  : $DATESTR" && \
2936                 error "$DIR/$tdir/$tfile timestamps changed" || true
2937
2938         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
2939 }
2940
2941 test_36f() {
2942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2943         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
2944         subr_36fh "0x80000214"
2945 }
2946 run_test 36f "utime on file racing with OST BRW write =========="
2947
2948 test_36g() {
2949         remote_ost_nodsh && skip "remote OST with nodsh" && return
2950         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2951         local fmd_max_age
2952         local fmd_before
2953         local fmd_after
2954
2955         test_mkdir -p $DIR/$tdir
2956         fmd_max_age=$(do_facet ost1 \
2957                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
2958                 head -n 1")
2959
2960         fmd_before=$(do_facet ost1 \
2961                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2962         touch $DIR/$tdir/$tfile
2963         sleep $((fmd_max_age + 12))
2964         fmd_after=$(do_facet ost1 \
2965                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
2966
2967         echo "fmd_before: $fmd_before"
2968         echo "fmd_after: $fmd_after"
2969         [[ $fmd_after -gt $fmd_before ]] &&
2970                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
2971                 error "fmd didn't expire after ping" || true
2972 }
2973 run_test 36g "filter mod data cache expiry ====================="
2974
2975 test_36h() {
2976         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2977         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
2978         subr_36fh "0x80000227"
2979 }
2980 run_test 36h "utime on file racing with OST BRW write =========="
2981
2982 test_36i() {
2983         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2984
2985         mkdir $DIR/$tdir
2986         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
2987
2988         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
2989         local new_mtime=$((mtime + 200))
2990
2991         #change Modify time of striped dir
2992         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
2993                         error "change mtime failed"
2994
2995         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
2996
2997         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
2998 }
2999 run_test 36i "change mtime on striped directory"
3000
3001 # test_37 - duplicate with tests 32q 32r
3002
3003 test_38() {
3004         local file=$DIR/$tfile
3005         touch $file
3006         openfile -f O_DIRECTORY $file
3007         local RC=$?
3008         local ENOTDIR=20
3009         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3010         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3011 }
3012 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3013
3014 test_39() {
3015         touch $DIR/$tfile
3016         touch $DIR/${tfile}2
3017 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3018 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3019 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3020         sleep 2
3021         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3022         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3023                 echo "mtime"
3024                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3025                 echo "atime"
3026                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3027                 echo "ctime"
3028                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3029                 error "O_TRUNC didn't change timestamps"
3030         fi
3031 }
3032 run_test 39 "mtime changed on create ==========================="
3033
3034 test_39b() {
3035         test_mkdir -p -c1 $DIR/$tdir
3036         cp -p /etc/passwd $DIR/$tdir/fopen
3037         cp -p /etc/passwd $DIR/$tdir/flink
3038         cp -p /etc/passwd $DIR/$tdir/funlink
3039         cp -p /etc/passwd $DIR/$tdir/frename
3040         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3041
3042         sleep 1
3043         echo "aaaaaa" >> $DIR/$tdir/fopen
3044         echo "aaaaaa" >> $DIR/$tdir/flink
3045         echo "aaaaaa" >> $DIR/$tdir/funlink
3046         echo "aaaaaa" >> $DIR/$tdir/frename
3047
3048         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3049         local link_new=`stat -c %Y $DIR/$tdir/flink`
3050         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3051         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3052
3053         cat $DIR/$tdir/fopen > /dev/null
3054         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3055         rm -f $DIR/$tdir/funlink2
3056         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3057
3058         for (( i=0; i < 2; i++ )) ; do
3059                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3060                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3061                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3062                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3063
3064                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3065                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3066                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3067                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3068
3069                 cancel_lru_locks osc
3070                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3071         done
3072 }
3073 run_test 39b "mtime change on open, link, unlink, rename  ======"
3074
3075 # this should be set to past
3076 TEST_39_MTIME=`date -d "1 year ago" +%s`
3077
3078 # bug 11063
3079 test_39c() {
3080         touch $DIR1/$tfile
3081         sleep 2
3082         local mtime0=`stat -c %Y $DIR1/$tfile`
3083
3084         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3085         local mtime1=`stat -c %Y $DIR1/$tfile`
3086         [ "$mtime1" = $TEST_39_MTIME ] || \
3087                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3088
3089         local d1=`date +%s`
3090         echo hello >> $DIR1/$tfile
3091         local d2=`date +%s`
3092         local mtime2=`stat -c %Y $DIR1/$tfile`
3093         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3094                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3095
3096         mv $DIR1/$tfile $DIR1/$tfile-1
3097
3098         for (( i=0; i < 2; i++ )) ; do
3099                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3100                 [ "$mtime2" = "$mtime3" ] || \
3101                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3102
3103                 cancel_lru_locks osc
3104                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3105         done
3106 }
3107 run_test 39c "mtime change on rename ==========================="
3108
3109 # bug 21114
3110 test_39d() {
3111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3112         touch $DIR1/$tfile
3113
3114         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3115
3116         for (( i=0; i < 2; i++ )) ; do
3117                 local mtime=`stat -c %Y $DIR1/$tfile`
3118                 [ $mtime = $TEST_39_MTIME ] || \
3119                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3120
3121                 cancel_lru_locks osc
3122                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3123         done
3124 }
3125 run_test 39d "create, utime, stat =============================="
3126
3127 # bug 21114
3128 test_39e() {
3129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3130         touch $DIR1/$tfile
3131         local mtime1=`stat -c %Y $DIR1/$tfile`
3132
3133         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3134
3135         for (( i=0; i < 2; i++ )) ; do
3136                 local mtime2=`stat -c %Y $DIR1/$tfile`
3137                 [ $mtime2 = $TEST_39_MTIME ] || \
3138                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3139
3140                 cancel_lru_locks osc
3141                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3142         done
3143 }
3144 run_test 39e "create, stat, utime, stat ========================"
3145
3146 # bug 21114
3147 test_39f() {
3148         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3149         touch $DIR1/$tfile
3150         mtime1=`stat -c %Y $DIR1/$tfile`
3151
3152         sleep 2
3153         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3154
3155         for (( i=0; i < 2; i++ )) ; do
3156                 local mtime2=`stat -c %Y $DIR1/$tfile`
3157                 [ $mtime2 = $TEST_39_MTIME ] || \
3158                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3159
3160                 cancel_lru_locks osc
3161                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3162         done
3163 }
3164 run_test 39f "create, stat, sleep, utime, stat ================="
3165
3166 # bug 11063
3167 test_39g() {
3168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3169         echo hello >> $DIR1/$tfile
3170         local mtime1=`stat -c %Y $DIR1/$tfile`
3171
3172         sleep 2
3173         chmod o+r $DIR1/$tfile
3174
3175         for (( i=0; i < 2; i++ )) ; do
3176                 local mtime2=`stat -c %Y $DIR1/$tfile`
3177                 [ "$mtime1" = "$mtime2" ] || \
3178                         error "lost mtime: $mtime2, should be $mtime1"
3179
3180                 cancel_lru_locks osc
3181                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3182         done
3183 }
3184 run_test 39g "write, chmod, stat ==============================="
3185
3186 # bug 11063
3187 test_39h() {
3188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3189         touch $DIR1/$tfile
3190         sleep 1
3191
3192         local d1=`date`
3193         echo hello >> $DIR1/$tfile
3194         local mtime1=`stat -c %Y $DIR1/$tfile`
3195
3196         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3197         local d2=`date`
3198         if [ "$d1" != "$d2" ]; then
3199                 echo "write and touch not within one second"
3200         else
3201                 for (( i=0; i < 2; i++ )) ; do
3202                         local mtime2=`stat -c %Y $DIR1/$tfile`
3203                         [ "$mtime2" = $TEST_39_MTIME ] || \
3204                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3205
3206                         cancel_lru_locks osc
3207                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3208                 done
3209         fi
3210 }
3211 run_test 39h "write, utime within one second, stat ============="
3212
3213 test_39i() {
3214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3215         touch $DIR1/$tfile
3216         sleep 1
3217
3218         echo hello >> $DIR1/$tfile
3219         local mtime1=`stat -c %Y $DIR1/$tfile`
3220
3221         mv $DIR1/$tfile $DIR1/$tfile-1
3222
3223         for (( i=0; i < 2; i++ )) ; do
3224                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3225
3226                 [ "$mtime1" = "$mtime2" ] || \
3227                         error "lost mtime: $mtime2, should be $mtime1"
3228
3229                 cancel_lru_locks osc
3230                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3231         done
3232 }
3233 run_test 39i "write, rename, stat =============================="
3234
3235 test_39j() {
3236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3237         start_full_debug_logging
3238         touch $DIR1/$tfile
3239         sleep 1
3240
3241         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3242         lctl set_param fail_loc=0x80000412
3243         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3244                 error "multiop failed"
3245         local multipid=$!
3246         local mtime1=`stat -c %Y $DIR1/$tfile`
3247
3248         mv $DIR1/$tfile $DIR1/$tfile-1
3249
3250         kill -USR1 $multipid
3251         wait $multipid || error "multiop close failed"
3252
3253         for (( i=0; i < 2; i++ )) ; do
3254                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3255                 [ "$mtime1" = "$mtime2" ] ||
3256                         error "mtime is lost on close: $mtime2, " \
3257                               "should be $mtime1"
3258
3259                 cancel_lru_locks osc
3260                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3261         done
3262         lctl set_param fail_loc=0
3263         stop_full_debug_logging
3264 }
3265 run_test 39j "write, rename, close, stat ======================="
3266
3267 test_39k() {
3268         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3269         touch $DIR1/$tfile
3270         sleep 1
3271
3272         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3273         local multipid=$!
3274         local mtime1=`stat -c %Y $DIR1/$tfile`
3275
3276         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3277
3278         kill -USR1 $multipid
3279         wait $multipid || error "multiop close failed"
3280
3281         for (( i=0; i < 2; i++ )) ; do
3282                 local mtime2=`stat -c %Y $DIR1/$tfile`
3283
3284                 [ "$mtime2" = $TEST_39_MTIME ] || \
3285                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3286
3287                 cancel_lru_locks osc
3288                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3289         done
3290 }
3291 run_test 39k "write, utime, close, stat ========================"
3292
3293 # this should be set to future
3294 TEST_39_ATIME=`date -d "1 year" +%s`
3295
3296 test_39l() {
3297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3298         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3299         local atime_diff=$(do_facet $SINGLEMDS \
3300                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3301         rm -rf $DIR/$tdir
3302         mkdir -p $DIR/$tdir
3303
3304         # test setting directory atime to future
3305         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3306         local atime=$(stat -c %X $DIR/$tdir)
3307         [ "$atime" = $TEST_39_ATIME ] || \
3308                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3309
3310         # test setting directory atime from future to now
3311         local d1=$(date +%s)
3312         ls $DIR/$tdir
3313         local d2=$(date +%s)
3314
3315         cancel_lru_locks mdc
3316         atime=$(stat -c %X $DIR/$tdir)
3317         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3318                 error "atime is not updated from future: $atime, $d1<atime<$d2"
3319
3320         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3321         sleep 3
3322
3323         # test setting directory atime when now > dir atime + atime_diff
3324         d1=$(date +%s)
3325         ls $DIR/$tdir
3326         d2=$(date +%s)
3327         cancel_lru_locks mdc
3328         atime=$(stat -c %X $DIR/$tdir)
3329         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3330                 error "atime is not updated  : $atime, should be $d2"
3331
3332         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3333         sleep 3
3334
3335         # test not setting directory atime when now < dir atime + atime_diff
3336         ls $DIR/$tdir
3337         cancel_lru_locks mdc
3338         atime=$(stat -c %X $DIR/$tdir)
3339         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] || \
3340                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3341
3342         do_facet $SINGLEMDS \
3343                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3344 }
3345 run_test 39l "directory atime update ==========================="
3346
3347 test_39m() {
3348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3349         touch $DIR1/$tfile
3350         sleep 2
3351         local far_past_mtime=$(date -d "May 29 1953" +%s)
3352         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3353
3354         touch -m -d @$far_past_mtime $DIR1/$tfile
3355         touch -a -d @$far_past_atime $DIR1/$tfile
3356
3357         for (( i=0; i < 2; i++ )) ; do
3358                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3359                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3360                         error "atime or mtime set incorrectly"
3361
3362                 cancel_lru_locks osc
3363                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3364         done
3365 }
3366 run_test 39m "test atime and mtime before 1970"
3367
3368 test_39n() { # LU-3832
3369         local atime_diff=$(do_facet $SINGLEMDS \
3370                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3371         local atime0
3372         local atime1
3373         local atime2
3374
3375         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3376
3377         rm -rf $DIR/$tfile
3378         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3379         atime0=$(stat -c %X $DIR/$tfile)
3380
3381         sleep 5
3382         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3383         atime1=$(stat -c %X $DIR/$tfile)
3384
3385         sleep 5
3386         cancel_lru_locks mdc
3387         cancel_lru_locks osc
3388         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3389         atime2=$(stat -c %X $DIR/$tfile)
3390
3391         do_facet $SINGLEMDS \
3392                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3393
3394         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3395         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3396 }
3397 run_test 39n "check that O_NOATIME is honored"
3398
3399 test_39o() {
3400         TESTDIR=$DIR/$tdir/$tfile
3401         [ -e $TESTDIR ] && rm -rf $TESTDIR
3402         test_mkdir -p $TESTDIR
3403         cd $TESTDIR
3404         links1=2
3405         ls
3406         mkdir a b
3407         ls
3408         links2=$(stat -c %h .)
3409         [ $(($links1 + 2)) != $links2 ] &&
3410                 error "wrong links count $(($links1 + 2)) != $links2"
3411         rmdir b
3412         links3=$(stat -c %h .)
3413         [ $(($links1 + 1)) != $links3 ] &&
3414                 error "wrong links count $links1 != $links3"
3415         return 0
3416 }
3417 run_test 39o "directory cached attributes updated after create ========"
3418
3419 test_39p() {
3420         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3421         local MDTIDX=1
3422         TESTDIR=$DIR/$tdir/$tfile
3423         [ -e $TESTDIR ] && rm -rf $TESTDIR
3424         mkdir -p $TESTDIR
3425         cd $TESTDIR
3426         links1=2
3427         ls
3428         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3429         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3430         ls
3431         links2=$(stat -c %h .)
3432         [ $(($links1 + 2)) != $links2 ] &&
3433                 error "wrong links count $(($links1 + 2)) != $links2"
3434         rmdir remote_dir2
3435         links3=$(stat -c %h .)
3436         [ $(($links1 + 1)) != $links3 ] &&
3437                 error "wrong links count $links1 != $links3"
3438         return 0
3439 }
3440 run_test 39p "remote directory cached attributes updated after create ========"
3441
3442
3443 test_40() {
3444         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3445         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3446                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3447         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3448                 error "$tfile is not 4096 bytes in size"
3449 }
3450 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3451
3452 test_41() {
3453         # bug 1553
3454         small_write $DIR/f41 18
3455 }
3456 run_test 41 "test small file write + fstat ====================="
3457
3458 count_ost_writes() {
3459         lctl get_param -n osc.*.stats |
3460                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3461                         END { printf("%0.0f", writes) }'
3462 }
3463
3464 # decent default
3465 WRITEBACK_SAVE=500
3466 DIRTY_RATIO_SAVE=40
3467 MAX_DIRTY_RATIO=50
3468 BG_DIRTY_RATIO_SAVE=10
3469 MAX_BG_DIRTY_RATIO=25
3470
3471 start_writeback() {
3472         trap 0
3473         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3474         # dirty_ratio, dirty_background_ratio
3475         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3476                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3477                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3478                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3479         else
3480                 # if file not here, we are a 2.4 kernel
3481                 kill -CONT `pidof kupdated`
3482         fi
3483 }
3484
3485 stop_writeback() {
3486         # setup the trap first, so someone cannot exit the test at the
3487         # exact wrong time and mess up a machine
3488         trap start_writeback EXIT
3489         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3490         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3491                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3492                 sysctl -w vm.dirty_writeback_centisecs=0
3493                 sysctl -w vm.dirty_writeback_centisecs=0
3494                 # save and increase /proc/sys/vm/dirty_ratio
3495                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3496                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3497                 # save and increase /proc/sys/vm/dirty_background_ratio
3498                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3499                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3500         else
3501                 # if file not here, we are a 2.4 kernel
3502                 kill -STOP `pidof kupdated`
3503         fi
3504 }
3505
3506 # ensure that all stripes have some grant before we test client-side cache
3507 setup_test42() {
3508         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3509                 dd if=/dev/zero of=$i bs=4k count=1
3510                 rm $i
3511         done
3512 }
3513
3514 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3515 # file truncation, and file removal.
3516 test_42a() {
3517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3518         setup_test42
3519         cancel_lru_locks osc
3520         stop_writeback
3521         sync; sleep 1; sync # just to be safe
3522         BEFOREWRITES=`count_ost_writes`
3523         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3524         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3525         AFTERWRITES=`count_ost_writes`
3526         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3527                 error "$BEFOREWRITES < $AFTERWRITES"
3528         start_writeback
3529 }
3530 run_test 42a "ensure that we don't flush on close =============="
3531
3532 test_42b() {
3533         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3534         setup_test42
3535         cancel_lru_locks osc
3536         stop_writeback
3537         sync
3538         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3539         BEFOREWRITES=$(count_ost_writes)
3540         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3541         AFTERWRITES=$(count_ost_writes)
3542         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3543                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3544         fi
3545         BEFOREWRITES=$(count_ost_writes)
3546         sync || error "sync: $?"
3547         AFTERWRITES=$(count_ost_writes)
3548         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3549                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3550         fi
3551         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3552         start_writeback
3553         return 0
3554 }
3555 run_test 42b "test destroy of file with cached dirty data ======"
3556
3557 # if these tests just want to test the effect of truncation,
3558 # they have to be very careful.  consider:
3559 # - the first open gets a {0,EOF}PR lock
3560 # - the first write conflicts and gets a {0, count-1}PW
3561 # - the rest of the writes are under {count,EOF}PW
3562 # - the open for truncate tries to match a {0,EOF}PR
3563 #   for the filesize and cancels the PWs.
3564 # any number of fixes (don't get {0,EOF} on open, match
3565 # composite locks, do smarter file size management) fix
3566 # this, but for now we want these tests to verify that
3567 # the cancellation with truncate intent works, so we
3568 # start the file with a full-file pw lock to match against
3569 # until the truncate.
3570 trunc_test() {
3571         test=$1
3572         file=$DIR/$test
3573         offset=$2
3574         cancel_lru_locks osc
3575         stop_writeback
3576         # prime the file with 0,EOF PW to match
3577         touch $file
3578         $TRUNCATE $file 0
3579         sync; sync
3580         # now the real test..
3581         dd if=/dev/zero of=$file bs=1024 count=100
3582         BEFOREWRITES=`count_ost_writes`
3583         $TRUNCATE $file $offset
3584         cancel_lru_locks osc
3585         AFTERWRITES=`count_ost_writes`
3586         start_writeback
3587 }
3588
3589 test_42c() {
3590         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3591         trunc_test 42c 1024
3592         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3593             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3594         rm $file
3595 }
3596 run_test 42c "test partial truncate of file with cached dirty data"
3597
3598 test_42d() {
3599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3600         trunc_test 42d 0
3601         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3602             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3603         rm $file
3604 }
3605 run_test 42d "test complete truncate of file with cached dirty data"
3606
3607 test_42e() { # bug22074
3608         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3609         local TDIR=$DIR/${tdir}e
3610         local pagesz=$(page_size)
3611         local pages=16 # hardcoded 16 pages, don't change it.
3612         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3613         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3614         local max_dirty_mb
3615         local warmup_files
3616
3617         test_mkdir -p $DIR/${tdir}e
3618         $SETSTRIPE -c 1 $TDIR
3619         createmany -o $TDIR/f $files
3620
3621         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3622
3623         # we assume that with $OSTCOUNT files, at least one of them will
3624         # be allocated on OST0.
3625         warmup_files=$((OSTCOUNT * max_dirty_mb))
3626         createmany -o $TDIR/w $warmup_files
3627
3628         # write a large amount of data into one file and sync, to get good
3629         # avail_grant number from OST.
3630         for ((i=0; i<$warmup_files; i++)); do
3631                 idx=$($GETSTRIPE -i $TDIR/w$i)
3632                 [ $idx -ne 0 ] && continue
3633                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3634                 break
3635         done
3636         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3637         sync
3638         $LCTL get_param $proc_osc0/cur_dirty_bytes
3639         $LCTL get_param $proc_osc0/cur_grant_bytes
3640
3641         # create as much dirty pages as we can while not to trigger the actual
3642         # RPCs directly. but depends on the env, VFS may trigger flush during this
3643         # period, hopefully we are good.
3644         for ((i=0; i<$warmup_files; i++)); do
3645                 idx=$($GETSTRIPE -i $TDIR/w$i)
3646                 [ $idx -ne 0 ] && continue
3647                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3648         done
3649         $LCTL get_param $proc_osc0/cur_dirty_bytes
3650         $LCTL get_param $proc_osc0/cur_grant_bytes
3651
3652         # perform the real test
3653         $LCTL set_param $proc_osc0/rpc_stats 0
3654         for ((;i<$files; i++)); do
3655                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3656                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3657         done
3658         sync
3659         $LCTL get_param $proc_osc0/rpc_stats
3660
3661         local percent=0
3662         local have_ppr=false
3663         $LCTL get_param $proc_osc0/rpc_stats |
3664                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3665                         # skip lines until we are at the RPC histogram data
3666                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3667                         $have_ppr || continue
3668
3669                         # we only want the percent stat for < 16 pages
3670                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3671
3672                         percent=$((percent + WPCT))
3673                         if [[ $percent -gt 15 ]]; then
3674                                 error "less than 16-pages write RPCs" \
3675                                       "$percent% > 15%"
3676                                 break
3677                         fi
3678                 done
3679         rm -rf $TDIR
3680 }
3681 run_test 42e "verify sub-RPC writes are not done synchronously"
3682
3683 test_43() {
3684         test_mkdir -p $DIR/$tdir
3685         cp -p /bin/ls $DIR/$tdir/$tfile
3686         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3687         pid=$!
3688         # give multiop a chance to open
3689         sleep 1
3690
3691         $DIR/$tdir/$tfile && error || true
3692         kill -USR1 $pid
3693 }
3694 run_test 43 "execution of file opened for write should return -ETXTBSY"
3695
3696 test_43a() {
3697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3698         test_mkdir -p $DIR/$tdir
3699         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3700                         cp -p multiop $DIR/$tdir/multiop
3701         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3702                         return 1
3703         MULTIOP_PID=$!
3704         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3705         kill -USR1 $MULTIOP_PID || return 2
3706         wait $MULTIOP_PID || return 3
3707         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3708 }
3709 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3710
3711 test_43b() {
3712         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3713         test_mkdir -p $DIR/$tdir
3714         cp -p `which $MULTIOP` $DIR/$tdir/multiop ||
3715                         cp -p multiop $DIR/$tdir/multiop
3716         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3717                         return 1
3718         MULTIOP_PID=$!
3719         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3720         kill -USR1 $MULTIOP_PID || return 2
3721         wait $MULTIOP_PID || return 3
3722         rm $TMP/$tfile.junk $DIR/$tdir/multiop
3723 }
3724 run_test 43b "truncate of file being executed should return -ETXTBSY"
3725
3726 test_43c() {
3727         local testdir="$DIR/$tdir"
3728         test_mkdir -p $DIR/$tdir
3729         cp $SHELL $testdir/
3730         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) | \
3731                 ( cd $testdir && md5sum -c)
3732 }
3733 run_test 43c "md5sum of copy into lustre========================"
3734
3735 test_44() {
3736         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
3737         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3738         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3739 }
3740 run_test 44 "zero length read from a sparse stripe ============="
3741
3742 test_44a() {
3743         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3744                 awk '{ print $2 }')
3745         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3746         [[ $nstripe -gt $OSTCOUNT ]] &&
3747             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3748             return
3749         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3750                 awk '{ print $2 }')
3751         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3752                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3753                         awk '{ print $2 }')
3754         fi
3755
3756         OFFSETS="0 $((stride/2)) $((stride-1))"
3757         for offset in $OFFSETS; do
3758                 for i in $(seq 0 $((nstripe-1))); do
3759                         local GLOBALOFFSETS=""
3760                         # size in Bytes
3761                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3762                         local myfn=$DIR/d44a-$size
3763                         echo "--------writing $myfn at $size"
3764                         ll_sparseness_write $myfn $size ||
3765                                 error "ll_sparseness_write"
3766                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3767                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3768                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3769
3770                         for j in $(seq 0 $((nstripe-1))); do
3771                                 # size in Bytes
3772                                 size=$((((j + $nstripe )*$stride + $offset)))
3773                                 ll_sparseness_write $myfn $size ||
3774                                         error "ll_sparseness_write"
3775                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3776                         done
3777                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3778                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3779                         rm -f $myfn
3780                 done
3781         done
3782 }
3783 run_test 44a "test sparse pwrite ==============================="
3784
3785 dirty_osc_total() {
3786         tot=0
3787         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3788                 tot=$(($tot + $d))
3789         done
3790         echo $tot
3791 }
3792 do_dirty_record() {
3793         before=`dirty_osc_total`
3794         echo executing "\"$*\""
3795         eval $*
3796         after=`dirty_osc_total`
3797         echo before $before, after $after
3798 }
3799 test_45() {
3800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3801         f="$DIR/f45"
3802         # Obtain grants from OST if it supports it
3803         echo blah > ${f}_grant
3804         stop_writeback
3805         sync
3806         do_dirty_record "echo blah > $f"
3807         [[ $before -eq $after ]] && error "write wasn't cached"
3808         do_dirty_record "> $f"
3809         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3810         do_dirty_record "echo blah > $f"
3811         [[ $before -eq $after ]] && error "write wasn't cached"
3812         do_dirty_record "sync"
3813         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3814         do_dirty_record "echo blah > $f"
3815         [[ $before -eq $after ]] && error "write wasn't cached"
3816         do_dirty_record "cancel_lru_locks osc"
3817         [[ $before -gt $after ]] ||
3818                 error "lock cancellation didn't lower dirty count"
3819         start_writeback
3820 }
3821 run_test 45 "osc io page accounting ============================"
3822
3823 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3824 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3825 # objects offset and an assert hit when an rpc was built with 1023's mapped
3826 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3827 test_46() {
3828         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3829         f="$DIR/f46"
3830         stop_writeback
3831         sync
3832         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3833         sync
3834         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3835         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3836         sync
3837         start_writeback
3838 }
3839 run_test 46 "dirtying a previously written page ================"
3840
3841 # test_47 is removed "Device nodes check" is moved to test_28
3842
3843 test_48a() { # bug 2399
3844         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3845         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3846                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3847                 return
3848         check_kernel_version 34 || return 0
3849         test_mkdir -p $DIR/$tdir
3850         cd $DIR/$tdir
3851         mv $DIR/$tdir $DIR/d48.new || error "move directory failed"
3852         test_mkdir $DIR/$tdir || error "recreate directory failed"
3853         touch foo || error "'touch foo' failed after recreating cwd"
3854         test_mkdir $DIR/$tdir/bar ||
3855                      error "'mkdir foo' failed after recreating cwd"
3856         if check_kernel_version 44; then
3857                 touch .foo || error "'touch .foo' failed after recreating cwd"
3858                 test_mkdir $DIR/$tdir/.bar ||
3859                               error "'mkdir .foo' failed after recreating cwd"
3860         fi
3861         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3862         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3863         cd . || error "'cd .' failed after recreating cwd"
3864         test_mkdir . && error "'mkdir .' worked after recreating cwd"
3865         rmdir . && error "'rmdir .' worked after recreating cwd"
3866         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3867         cd .. || error "'cd ..' failed after recreating cwd"
3868 }
3869 run_test 48a "Access renamed working dir (should return errors)="
3870
3871 test_48b() { # bug 2399
3872         check_kernel_version 34 || return 0
3873         rm -rf $DIR/$tdir
3874         test_mkdir -p $DIR/$tdir
3875         cd $DIR/$tdir
3876         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
3877         touch foo && error "'touch foo' worked after removing cwd"
3878         test_mkdir $DIR/$tdir/foo &&
3879                      error "'mkdir foo' worked after removing cwd"
3880         if check_kernel_version 44; then
3881                 touch .foo && error "'touch .foo' worked after removing cwd"
3882                 test_mkdir $DIR/$tdir/.foo &&
3883                               error "'mkdir .foo' worked after removing cwd"
3884         fi
3885         ls . > /dev/null && error "'ls .' worked after removing cwd"
3886         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3887         is_patchless || ( cd . && error "'cd .' worked after removing cwd" )
3888         test_mkdir $DIR/$tdir/. && error "'mkdir .' worked after removing cwd"
3889         rmdir . && error "'rmdir .' worked after removing cwd"
3890         ln -s . foo && error "'ln -s .' worked after removing cwd"
3891         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
3892 }
3893 run_test 48b "Access removed working dir (should return errors)="
3894
3895 test_48c() { # bug 2350
3896         check_kernel_version 36 || return 0
3897         #lctl set_param debug=-1
3898         #set -vx
3899         rm -rf $DIR/$tdir
3900         test_mkdir -p $DIR/$tdir/dir
3901         cd $DIR/$tdir/dir
3902         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3903         $TRACE touch foo && error "touch foo worked after removing cwd"
3904         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
3905         if check_kernel_version 44; then
3906                 touch .foo && error "touch .foo worked after removing cwd"
3907                 test_mkdir .foo && error "mkdir .foo worked after removing cwd"
3908         fi
3909         $TRACE ls . && error "'ls .' worked after removing cwd"
3910         $TRACE ls .. || error "'ls ..' failed after removing cwd"
3911         is_patchless || ( $TRACE cd . &&
3912                         error "'cd .' worked after removing cwd" )
3913         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
3914         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
3915         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
3916         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
3917 }
3918 run_test 48c "Access removed working subdir (should return errors)"
3919
3920 test_48d() { # bug 2350
3921         check_kernel_version 36 || return 0
3922         #lctl set_param debug=-1
3923         #set -vx
3924         rm -rf $DIR/$tdir
3925         test_mkdir -p $DIR/$tdir/dir
3926         cd $DIR/$tdir/dir
3927         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3928         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3929         $TRACE touch foo && error "'touch foo' worked after removing parent"
3930         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
3931         if check_kernel_version 44; then
3932                 touch .foo && error "'touch .foo' worked after removing parent"
3933                 test_mkdir .foo &&
3934                               error "mkdir .foo worked after removing parent"
3935         fi
3936         $TRACE ls . && error "'ls .' worked after removing parent"
3937         $TRACE ls .. && error "'ls ..' worked after removing parent"
3938         is_patchless || ( $TRACE cd . &&
3939                         error "'cd .' worked after recreate parent" )
3940         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
3941         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
3942         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
3943         is_patchless || ( $TRACE cd .. &&
3944                         error "'cd ..' worked after removing parent" || true )
3945 }
3946 run_test 48d "Access removed parent subdir (should return errors)"
3947
3948 test_48e() { # bug 4134
3949         check_kernel_version 41 || return 0
3950         #lctl set_param debug=-1
3951         #set -vx
3952         rm -rf $DIR/$tdir
3953         test_mkdir -p $DIR/$tdir/dir
3954         cd $DIR/$tdir/dir
3955         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
3956         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
3957         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
3958         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
3959         # On a buggy kernel addition of "touch foo" after cd .. will
3960         # produce kernel oops in lookup_hash_it
3961         touch ../foo && error "'cd ..' worked after recreate parent"
3962         cd $DIR
3963         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
3964 }
3965 run_test 48e "Access to recreated parent subdir (should return errors)"
3966
3967 test_49() { # LU-1030
3968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3969         # get ost1 size - lustre-OST0000
3970         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
3971                 awk '{ print $4 }')
3972         # write 800M at maximum
3973         [[ $ost1_size -lt 2 ]] && ost1_size=2
3974         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
3975
3976         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
3977         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
3978         local dd_pid=$!
3979
3980         # change max_pages_per_rpc while writing the file
3981         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
3982         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
3983         # loop until dd process exits
3984         while ps ax -opid | grep -wq $dd_pid; do
3985                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
3986                 sleep $((RANDOM % 5 + 1))
3987         done
3988         # restore original max_pages_per_rpc
3989         $LCTL set_param $osc1_mppc=$orig_mppc
3990         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
3991 }
3992 run_test 49 "Change max_pages_per_rpc won't break osc extent"
3993
3994 test_50() {
3995         # bug 1485
3996         test_mkdir $DIR/$tdir
3997         cd $DIR/$tdir
3998         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
3999 }
4000 run_test 50 "special situations: /proc symlinks  ==============="
4001
4002 test_51a() {    # was test_51
4003         # bug 1516 - create an empty entry right after ".." then split dir
4004         test_mkdir -c1 $DIR/$tdir
4005         touch $DIR/$tdir/foo
4006         $MCREATE $DIR/$tdir/bar
4007         rm $DIR/$tdir/foo
4008         createmany -m $DIR/$tdir/longfile 201
4009         FNUM=202
4010         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4011                 $MCREATE $DIR/$tdir/longfile$FNUM
4012                 FNUM=$(($FNUM + 1))
4013                 echo -n "+"
4014         done
4015         echo
4016         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4017 }
4018 run_test 51a "special situations: split htree with empty entry =="
4019
4020 export NUMTEST=70000
4021 test_51b() {
4022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4023         local BASE=$DIR/d${base}.${TESTSUITE}
4024
4025         # cleanup the directory
4026         rm -fr $BASE
4027
4028         test_mkdir -p -c1 $BASE
4029
4030         $LFS df
4031         $LFS df -i
4032         local mdtidx=$(printf "%04x" $($LFS getstripe -M $BASE))
4033         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4034         [[ $numfree -lt 21000 ]] &&
4035                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4036                 return
4037
4038         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$(($numfree - 50)) &&
4039                 echo "reduced count to $NUMTEST due to inodes on MDT$mdtidx"
4040
4041         # need to check free space for the directories as well
4042         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4043         numfree=$((blkfree / 4))
4044         [[ $numfree -lt $NUMTEST ]] && NUMTEST=$((numfree - 50)) &&
4045                 echo "reduced count to $NUMTEST due to blocks on MDT$mdtidx"
4046
4047         createmany -d $BASE/d $NUMTEST && echo $NUMTEST > $BASE/fnum ||
4048         {
4049                 $LFS df
4050                 $LFS df -i
4051                 echo "failed" > $BASE/fnum
4052                 error "failed to create $NUMTEST subdirs in MDT$mdtidx:$BASE"
4053         }
4054 }
4055 run_test 51b "exceed 64k subdirectory nlink limit"
4056
4057 test_51ba() { # LU-993
4058         local BASE=$DIR/d${base}.${TESTSUITE}
4059         # unlink all but 100 subdirectories, then check it still works
4060         local LEFT=100
4061         [ -f $BASE/fnum ] && local NUMPREV=$(cat $BASE/fnum) && rm $BASE/fnum
4062
4063         [ "$NUMPREV" != "failed" ] && NUMTEST=$NUMPREV
4064         local DELETE=$((NUMTEST - LEFT))
4065
4066         # continue on to run this test even if 51b didn't finish,
4067         # just to delete the many subdirectories created.
4068         [ ! -d "${BASE}/d1" ] && skip "test_51b() not run" && return 0
4069
4070         # for ldiskfs the nlink count should be 1, but this is OSD specific
4071         # and so this is listed for informational purposes only
4072         echo "nlink before: $(stat -c %h $BASE), created before: $NUMTEST"
4073         unlinkmany -d $BASE/d $DELETE
4074         RC=$?
4075
4076         if [ $RC -ne 0 ]; then
4077                 if [ "$NUMPREV" == "failed" ]; then
4078                         skip "previous setup failed"
4079                         return 0
4080                 else
4081                         error "unlink of first $DELETE subdirs failed"
4082                         return $RC
4083                 fi
4084         fi
4085
4086         echo "nlink between: $(stat -c %h $BASE)"
4087         # trim the first line of ls output
4088         local FOUND=$(($(ls -l ${BASE} | wc -l) - 1))
4089         [ $FOUND -ne $LEFT ] &&
4090                 error "can't find subdirs: found only $FOUND/$LEFT"
4091
4092         unlinkmany -d $BASE/d $DELETE $LEFT ||
4093                 error "unlink of second $LEFT subdirs failed"
4094         # regardless of whether the backing filesystem tracks nlink accurately
4095         # or not, the nlink count shouldn't be more than "." and ".." here
4096         local AFTER=$(stat -c %h $BASE)
4097         [[ $AFTER -gt 2 ]] && error "nlink after: $AFTER > 2" ||
4098                 echo "nlink after: $AFTER"
4099 }
4100 run_test 51ba "verify nlink for many subdirectory cleanup"
4101
4102 test_51d() {
4103         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4104         [[ $OSTCOUNT -lt 3 ]] &&
4105                 skip_env "skipping test with few OSTs" && return
4106         test_mkdir -p $DIR/$tdir
4107         createmany -o $DIR/$tdir/t- 1000
4108         $GETSTRIPE $DIR/$tdir > $TMP/files
4109         for N in $(seq 0 $((OSTCOUNT - 1))); do
4110                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4111                         END { printf("%0.0f", objs) }' $TMP/files)
4112                 OBJS0[$N]=$(grep -A 1 idx $TMP/files | awk -vobjs=0 \
4113                         '($1 == '$N') { objs += 1 } \
4114                         END { printf("%0.0f", objs) }')
4115                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4116         done
4117         unlinkmany $DIR/$tdir/t- 1000
4118
4119         NLAST=0
4120         for N in $(seq 1 $((OSTCOUNT - 1))); do
4121                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4122                         error "OST $N has less objects vs OST $NLAST" \
4123                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4124                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4125                         error "OST $N has less objects vs OST $NLAST" \
4126                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4127
4128                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4129                         error "OST $N has less #0 objects vs OST $NLAST" \
4130                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4131                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4132                         error "OST $N has less #0 objects vs OST $NLAST" \
4133                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4134                 NLAST=$N
4135         done
4136 }
4137 run_test 51d "check object distribution ===================="
4138
4139 test_51e() {
4140         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4141                 skip "Only applicable to ldiskfs-based MDTs"
4142                 return
4143         fi
4144
4145         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4146         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4147
4148         touch $DIR/$tdir/d0/foo
4149         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4150                 error "file exceed 65000 nlink limit!"
4151         unlinkmany $DIR/$tdir/d0/f- 65001
4152         return 0
4153 }
4154 run_test 51e "check file nlink limit"
4155
4156 test_52a() {
4157         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4158         test_mkdir -p $DIR/$tdir
4159         touch $DIR/$tdir/foo
4160         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4161         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4162         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4163         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4164         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4165                                         error "link worked"
4166         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4167         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4168         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4169                                                      error "lsattr"
4170         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4171         cp -r $DIR/$tdir /tmp/
4172         rm -fr $DIR/$tdir || error "cleanup rm failed"
4173 }
4174 run_test 52a "append-only flag test (should return errors) ====="
4175
4176 test_52b() {
4177         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4178         test_mkdir -p $DIR/$tdir
4179         touch $DIR/$tdir/foo
4180         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4181         cat test > $DIR/$tdir/foo && error "cat test worked"
4182         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4183         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4184         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4185                                         error "link worked"
4186         echo foo >> $DIR/$tdir/foo && error "echo worked"
4187         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4188         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4189         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4190         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4191                                                         error "lsattr"
4192         chattr -i $DIR/$tdir/foo || error "chattr failed"
4193
4194         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4195 }
4196 run_test 52b "immutable flag test (should return errors) ======="
4197
4198 test_53() {
4199         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4200         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4201         remote_ost_nodsh && skip "remote OST with nodsh" && return
4202
4203         local param
4204         local param_seq
4205         local ostname
4206         local mds_last
4207         local mds_last_seq
4208         local ost_last
4209         local ost_last_seq
4210         local ost_last_id
4211         local ostnum
4212         local node
4213         local found=false
4214         local support_last_seq=true
4215
4216         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4217                 support_last_seq=false
4218
4219         # only test MDT0000
4220         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4221         local value
4222         for value in $(do_facet $SINGLEMDS \
4223                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4224                 param=$(echo ${value[0]} | cut -d "=" -f1)
4225                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4226
4227                 if $support_last_seq; then
4228                         param_seq=$(echo $param |
4229                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4230                         mds_last_seq=$(do_facet $SINGLEMDS \
4231                                        $LCTL get_param -n $param_seq)
4232                 fi
4233                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4234
4235                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4236                 node=$(facet_active_host ost$((ostnum+1)))
4237                 param="obdfilter.$ostname.last_id"
4238                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4239                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4240                         ost_last_id=$ost_last
4241
4242                         if $support_last_seq; then
4243                                 ost_last_id=$(echo $ost_last |
4244                                               awk -F':' '{print $2}' |
4245                                               sed -e "s/^0x//g")
4246                                 ost_last_seq=$(echo $ost_last |
4247                                                awk -F':' '{print $1}')
4248                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4249                         fi
4250
4251                         if [[ $ost_last_id != $mds_last ]]; then
4252                                 error "$ost_last_id != $mds_last"
4253                         else
4254                                 found=true
4255                                 break
4256                         fi
4257                 done
4258         done
4259         $found || error "can not match last_seq/last_id for $mdtosc"
4260         return 0
4261 }
4262 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4263
4264 test_54a() {
4265         $SOCKETSERVER $DIR/socket ||
4266                 error "$SOCKETSERVER $DIR/socket failed: $?"
4267         $SOCKETCLIENT $DIR/socket ||
4268                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4269         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4270 }
4271 run_test 54a "unix domain socket test =========================="
4272
4273 test_54b() {
4274         f="$DIR/f54b"
4275         mknod $f c 1 3
4276         chmod 0666 $f
4277         dd if=/dev/zero of=$f bs=$(page_size) count=1
4278 }
4279 run_test 54b "char device works in lustre ======================"
4280
4281 find_loop_dev() {
4282         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4283         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4284         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4285
4286         for i in $(seq 3 7); do
4287                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4288                 LOOPDEV=$LOOPBASE$i
4289                 LOOPNUM=$i
4290                 break
4291         done
4292 }
4293
4294 cleanup_54c() {
4295         loopdev="$DIR/loop54c"
4296
4297         trap 0
4298         $UMOUNT -d $DIR/$tdir || rc=$?
4299         losetup -d $loopdev || true
4300         losetup -d $LOOPDEV || true
4301         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4302         return $rc
4303 }
4304
4305 test_54c() {
4306         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4307         loopdev="$DIR/loop54c"
4308
4309         find_loop_dev
4310         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4311         trap cleanup_54c EXIT
4312         mknod $loopdev b 7 $LOOPNUM
4313         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4314         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4315         losetup $loopdev $DIR/$tfile ||
4316                 error "can't set up $loopdev for $DIR/$tfile"
4317         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4318         test_mkdir -p $DIR/$tdir
4319         mount -t ext2 $loopdev $DIR/$tdir ||
4320                 error "error mounting $loopdev on $DIR/$tdir"
4321         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4322                 error "dd write"
4323         df $DIR/$tdir
4324         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4325                 error "dd read"
4326         cleanup_54c
4327 }
4328 run_test 54c "block device works in lustre ====================="
4329
4330 test_54d() {
4331         f="$DIR/f54d"
4332         string="aaaaaa"
4333         mknod $f p
4334         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4335 }
4336 run_test 54d "fifo device works in lustre ======================"
4337
4338 test_54e() {
4339         check_kernel_version 46 || return 0
4340         f="$DIR/f54e"
4341         string="aaaaaa"
4342         cp -aL /dev/console $f
4343         echo $string > $f || error "echo $string to $f failed"
4344 }
4345 run_test 54e "console/tty device works in lustre ======================"
4346
4347 #The test_55 used to be iopen test and it was removed by bz#24037.
4348 #run_test 55 "check iopen_connect_dentry() ======================"
4349
4350 test_56a() {    # was test_56
4351         rm -rf $DIR/$tdir
4352         $SETSTRIPE -d $DIR
4353         test_mkdir -p $DIR/$tdir/dir
4354         NUMFILES=3
4355         NUMFILESx2=$(($NUMFILES * 2))
4356         for i in $(seq 1 $NUMFILES); do
4357                 touch $DIR/$tdir/file$i
4358                 touch $DIR/$tdir/dir/file$i
4359         done
4360
4361         # test lfs getstripe with --recursive
4362         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4363         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4364                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4365         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4366         [[ $FILENUM -eq $NUMFILES ]] ||
4367                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4368         echo "$GETSTRIPE --recursive passed."
4369
4370         # test lfs getstripe with file instead of dir
4371         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4372         [[ $FILENUM -eq 1 ]] ||
4373                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4374         echo "$GETSTRIPE file1 passed."
4375
4376         #test lfs getstripe with --verbose
4377         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4378                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4379                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4380         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4381                 rror "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4382         echo "$GETSTRIPE --verbose passed."
4383
4384         #test lfs getstripe with --obd
4385         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4386                 grep -q "unknown obduuid" ||
4387                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4388
4389         [[ $OSTCOUNT -lt 2 ]] &&
4390                 skip_env "skipping other $GETSTRIPE --obd test" && return
4391
4392         OSTIDX=1
4393         OBDUUID=$(ostuuid_from_index $OSTIDX)
4394         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4395         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4396         [[ $FOUND -eq $FILENUM ]] ||
4397                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4398         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4399                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4400                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4401                 error "$GETSTRIPE --obd: should not show file on other obd"
4402         echo "$GETSTRIPE --obd passed"
4403 }
4404 run_test 56a "check $GETSTRIPE"
4405
4406 NUMFILES=3
4407 NUMDIRS=3
4408 setup_56() {
4409         local LOCAL_NUMFILES="$1"
4410         local LOCAL_NUMDIRS="$2"
4411         local MKDIR_PARAMS="$3"
4412         local DIR_STRIPE_PARAMS="$4"
4413
4414         if [ ! -d "$TDIR" ] ; then
4415                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4416                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4417                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4418                         touch $TDIR/file$i
4419                 done
4420                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4421                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4422                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4423                                 touch $TDIR/dir$i/file$j
4424                         done
4425                 done
4426         fi
4427 }
4428
4429 setup_56_special() {
4430         LOCAL_NUMFILES=$1
4431         LOCAL_NUMDIRS=$2
4432         setup_56 $1 $2
4433         if [ ! -e "$TDIR/loop1b" ] ; then
4434                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4435                         mknod $TDIR/loop${i}b b 7 $i
4436                         mknod $TDIR/null${i}c c 1 3
4437                         ln -s $TDIR/file1 $TDIR/link${i}l
4438                 done
4439                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4440                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4441                         mknod $TDIR/dir$i/null${i}c c 1 3
4442                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4443                 done
4444         fi
4445 }
4446
4447 test_56g() {
4448         $SETSTRIPE -d $DIR
4449
4450         TDIR=$DIR/${tdir}g
4451         setup_56 $NUMFILES $NUMDIRS
4452
4453         EXPECTED=$(($NUMDIRS + 2))
4454         # test lfs find with -name
4455         for i in $(seq 1 $NUMFILES) ; do
4456                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4457                 [ $NUMS -eq $EXPECTED ] ||
4458                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4459                               "found $NUMS, expected $EXPECTED"
4460         done
4461 }
4462 run_test 56g "check lfs find -name ============================="
4463
4464 test_56h() {
4465         $SETSTRIPE -d $DIR
4466
4467         TDIR=$DIR/${tdir}g
4468         setup_56 $NUMFILES $NUMDIRS
4469
4470         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4471         # test lfs find with ! -name
4472         for i in $(seq 1 $NUMFILES) ; do
4473                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4474                 [ $NUMS -eq $EXPECTED ] ||
4475                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4476                               "found $NUMS, expected $EXPECTED"
4477         done
4478 }
4479 run_test 56h "check lfs find ! -name ============================="
4480
4481 test_56i() {
4482        tdir=${tdir}i
4483        test_mkdir -p $DIR/$tdir
4484        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4485        CMD="$LFIND -ost $UUID $DIR/$tdir"
4486        OUT=$($CMD)
4487        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4488 }
4489 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4490
4491 test_56j() {
4492         TDIR=$DIR/${tdir}g
4493         setup_56_special $NUMFILES $NUMDIRS
4494
4495         EXPECTED=$((NUMDIRS + 1))
4496         CMD="$LFIND -type d $TDIR"
4497         NUMS=$($CMD | wc -l)
4498         [ $NUMS -eq $EXPECTED ] ||
4499                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4500 }
4501 run_test 56j "check lfs find -type d ============================="
4502
4503 test_56k() {
4504         TDIR=$DIR/${tdir}g
4505         setup_56_special $NUMFILES $NUMDIRS
4506
4507         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4508         CMD="$LFIND -type f $TDIR"
4509         NUMS=$($CMD | wc -l)
4510         [ $NUMS -eq $EXPECTED ] ||
4511                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4512 }
4513 run_test 56k "check lfs find -type f ============================="
4514
4515 test_56l() {
4516         TDIR=$DIR/${tdir}g
4517         setup_56_special $NUMFILES $NUMDIRS
4518
4519         EXPECTED=$((NUMDIRS + NUMFILES))
4520         CMD="$LFIND -type b $TDIR"
4521         NUMS=$($CMD | wc -l)
4522         [ $NUMS -eq $EXPECTED ] ||
4523                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4524 }
4525 run_test 56l "check lfs find -type b ============================="
4526
4527 test_56m() {
4528         TDIR=$DIR/${tdir}g
4529         setup_56_special $NUMFILES $NUMDIRS
4530
4531         EXPECTED=$((NUMDIRS + NUMFILES))
4532         CMD="$LFIND -type c $TDIR"
4533         NUMS=$($CMD | wc -l)
4534         [ $NUMS -eq $EXPECTED ] ||
4535                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4536 }
4537 run_test 56m "check lfs find -type c ============================="
4538
4539 test_56n() {
4540         TDIR=$DIR/${tdir}g
4541         setup_56_special $NUMFILES $NUMDIRS
4542
4543         EXPECTED=$((NUMDIRS + NUMFILES))
4544         CMD="$LFIND -type l $TDIR"
4545         NUMS=$($CMD | wc -l)
4546         [ $NUMS -eq $EXPECTED ] ||
4547                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4548 }
4549 run_test 56n "check lfs find -type l ============================="
4550
4551 test_56o() {
4552         TDIR=$DIR/${tdir}o
4553         setup_56 $NUMFILES $NUMDIRS
4554         utime $TDIR/file1 > /dev/null || error "utime (1)"
4555         utime $TDIR/file2 > /dev/null || error "utime (2)"
4556         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4557         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4558         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4559         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4560
4561         EXPECTED=4
4562         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4563         [ $NUMS -eq $EXPECTED ] || \
4564                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4565
4566         EXPECTED=12
4567         CMD="$LFIND -mtime 0 $TDIR"
4568         NUMS=$($CMD | wc -l)
4569         [ $NUMS -eq $EXPECTED ] ||
4570                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4571 }
4572 run_test 56o "check lfs find -mtime for old files =========================="
4573
4574 test_56p() {
4575         [ $RUNAS_ID -eq $UID ] &&
4576                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4577
4578         TDIR=$DIR/${tdir}p
4579         setup_56 $NUMFILES $NUMDIRS
4580
4581         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4582         EXPECTED=$NUMFILES
4583         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4584         NUMS=$($CMD | wc -l)
4585         [ $NUMS -eq $EXPECTED ] || \
4586                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4587
4588         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4589         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4590         NUMS=$($CMD | wc -l)
4591         [ $NUMS -eq $EXPECTED ] || \
4592                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4593 }
4594 run_test 56p "check lfs find -uid and ! -uid ==============================="
4595
4596 test_56q() {
4597         [ $RUNAS_ID -eq $UID ] &&
4598                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4599
4600         TDIR=$DIR/${tdir}q
4601         setup_56 $NUMFILES $NUMDIRS
4602
4603         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4604
4605         EXPECTED=$NUMFILES
4606         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4607         NUMS=$($CMD | wc -l)
4608         [ $NUMS -eq $EXPECTED ] ||
4609                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4610
4611         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4612         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4613         NUMS=$($CMD | wc -l)
4614         [ $NUMS -eq $EXPECTED ] ||
4615                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4616 }
4617 run_test 56q "check lfs find -gid and ! -gid ==============================="
4618
4619 test_56r() {
4620         TDIR=$DIR/${tdir}r
4621         setup_56 $NUMFILES $NUMDIRS
4622
4623         EXPECTED=12
4624         CMD="$LFIND -size 0 -type f $TDIR"
4625         NUMS=$($CMD | wc -l)
4626         [ $NUMS -eq $EXPECTED ] ||
4627                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4628         EXPECTED=0
4629         CMD="$LFIND ! -size 0 -type f $TDIR"
4630         NUMS=$($CMD | wc -l)
4631         [ $NUMS -eq $EXPECTED ] ||
4632                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4633         echo "test" > $TDIR/$tfile
4634         echo "test2" > $TDIR/$tfile.2 && sync
4635         EXPECTED=1
4636         CMD="$LFIND -size 5 -type f $TDIR"
4637         NUMS=$($CMD | wc -l)
4638         [ $NUMS -eq $EXPECTED ] ||
4639                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4640         EXPECTED=1
4641         CMD="$LFIND -size +5 -type f $TDIR"
4642         NUMS=$($CMD | wc -l)
4643         [ $NUMS -eq $EXPECTED ] ||
4644                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4645         EXPECTED=2
4646         CMD="$LFIND -size +0 -type f $TDIR"
4647         NUMS=$($CMD | wc -l)
4648         [ $NUMS -eq $EXPECTED ] ||
4649                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4650         EXPECTED=2
4651         CMD="$LFIND ! -size -5 -type f $TDIR"
4652         NUMS=$($CMD | wc -l)
4653         [ $NUMS -eq $EXPECTED ] ||
4654                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4655         EXPECTED=12
4656         CMD="$LFIND -size -5 -type f $TDIR"
4657         NUMS=$($CMD | wc -l)
4658         [ $NUMS -eq $EXPECTED ] ||
4659                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4660 }
4661 run_test 56r "check lfs find -size works =========================="
4662
4663 test_56s() { # LU-611
4664         TDIR=$DIR/${tdir}s
4665         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4666
4667         if [[ $OSTCOUNT -gt 1 ]]; then
4668                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4669                 ONESTRIPE=4
4670                 EXTRA=4
4671         else
4672                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4673                 EXTRA=0
4674         fi
4675
4676         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4677         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4678         NUMS=$($CMD | wc -l)
4679         [ $NUMS -eq $EXPECTED ] || {
4680                 $GETSTRIPE -R $TDIR
4681                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4682         }
4683
4684         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4685         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4686         NUMS=$($CMD | wc -l)
4687         [ $NUMS -eq $EXPECTED ] || {
4688                 $GETSTRIPE -R $TDIR
4689                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4690         }
4691
4692         EXPECTED=$ONESTRIPE
4693         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4694         NUMS=$($CMD | wc -l)
4695         [ $NUMS -eq $EXPECTED ] || {
4696                 $GETSTRIPE -R $TDIR
4697                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4698         }
4699
4700         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4701         NUMS=$($CMD | wc -l)
4702         [ $NUMS -eq $EXPECTED ] || {
4703                 $GETSTRIPE -R $TDIR
4704                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4705         }
4706
4707         EXPECTED=0
4708         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4709         NUMS=$($CMD | wc -l)
4710         [ $NUMS -eq $EXPECTED ] || {
4711                 $GETSTRIPE -R $TDIR
4712                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4713         }
4714 }
4715 run_test 56s "check lfs find -stripe-count works"
4716
4717 test_56t() { # LU-611
4718         TDIR=$DIR/${tdir}t
4719         setup_56 $NUMFILES $NUMDIRS "-s 512k"
4720
4721         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4722
4723         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4724         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4725         NUMS=$($CMD | wc -l)
4726         [ $NUMS -eq $EXPECTED ] ||
4727                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4728
4729         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4730         NUMS=$($CMD | wc -l)
4731         [ $NUMS -eq $EXPECTED ] ||
4732                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4733
4734         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4735         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4736         NUMS=$($CMD | wc -l)
4737         [ $NUMS -eq $EXPECTED ] ||
4738                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4739
4740         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4741         NUMS=$($CMD | wc -l)
4742         [ $NUMS -eq $EXPECTED ] ||
4743                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4744
4745         EXPECTED=4
4746         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4747         NUMS=$($CMD | wc -l)
4748         [ $NUMS -eq $EXPECTED ] ||
4749                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4750
4751         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4752         NUMS=$($CMD | wc -l)
4753         [ $NUMS -eq $EXPECTED ] ||
4754                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4755
4756         EXPECTED=0
4757         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4758         NUMS=$($CMD | wc -l)
4759         [ $NUMS -eq $EXPECTED ] ||
4760                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4761 }
4762 run_test 56t "check lfs find -stripe-size works"
4763
4764 test_56u() { # LU-611
4765         TDIR=$DIR/${tdir}u
4766         setup_56 $NUMFILES $NUMDIRS "-i 0"
4767
4768         if [[ $OSTCOUNT -gt 1 ]]; then
4769                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4770                 ONESTRIPE=4
4771         else
4772                 ONESTRIPE=0
4773         fi
4774
4775         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4776         CMD="$LFIND -stripe-index 0 -type f $TDIR"
4777         NUMS=$($CMD | wc -l)
4778         [ $NUMS -eq $EXPECTED ] ||
4779                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4780
4781         EXPECTED=$ONESTRIPE
4782         CMD="$LFIND -stripe-index 1 -type f $TDIR"
4783         NUMS=$($CMD | wc -l)
4784         [ $NUMS -eq $EXPECTED ] ||
4785                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4786
4787         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
4788         NUMS=$($CMD | wc -l)
4789         [ $NUMS -eq $EXPECTED ] ||
4790                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4791
4792         EXPECTED=0
4793         # This should produce an error and not return any files
4794         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
4795         NUMS=$($CMD 2>/dev/null | wc -l)
4796         [ $NUMS -eq $EXPECTED ] ||
4797                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4798
4799         if [[ $OSTCOUNT -gt 1 ]]; then
4800                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
4801                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
4802                 NUMS=$($CMD | wc -l)
4803                 [ $NUMS -eq $EXPECTED ] ||
4804                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4805         fi
4806 }
4807 run_test 56u "check lfs find -stripe-index works"
4808
4809 test_56v() {
4810     local MDT_IDX=0
4811
4812     TDIR=$DIR/${tdir}v
4813     rm -rf $TDIR
4814     setup_56 $NUMFILES $NUMDIRS
4815
4816     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
4817     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
4818
4819     for file in $($LFIND -mdt $UUID $TDIR); do
4820         file_mdt_idx=$($GETSTRIPE -M $file)
4821         [ $file_mdt_idx -eq $MDT_IDX ] ||
4822             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
4823     done
4824 }
4825 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
4826
4827 test_56w() {
4828         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs -- skipping" &&
4829                 return
4830         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4831         TDIR=$DIR/${tdir}w
4832
4833     rm -rf $TDIR || error "remove $TDIR failed"
4834     setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
4835
4836     local stripe_size
4837     stripe_size=$($GETSTRIPE -S -d $TDIR) ||
4838         error "$GETSTRIPE -S -d $TDIR failed"
4839     stripe_size=${stripe_size%% *}
4840
4841     local file_size=$((stripe_size * OSTCOUNT))
4842     local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
4843     local required_space=$((file_num * file_size))
4844     local free_space=$($LCTL get_param -n lov.$LOVNAME.kbytesavail)
4845     [[ $free_space -le $((required_space / 1024)) ]] &&
4846         skip_env "need at least $required_space bytes free space," \
4847                  "have $free_space kbytes" && return
4848
4849     local dd_bs=65536
4850     local dd_count=$((file_size / dd_bs))
4851
4852     # write data into the files
4853     local i
4854     local j
4855     local file
4856     for i in $(seq 1 $NUMFILES); do
4857         file=$TDIR/file$i
4858         yes | dd bs=$dd_bs count=$dd_count of=$file >/dev/null 2>&1 ||
4859             error "write data into $file failed"
4860     done
4861     for i in $(seq 1 $NUMDIRS); do
4862         for j in $(seq 1 $NUMFILES); do
4863             file=$TDIR/dir$i/file$j
4864             yes | dd bs=$dd_bs count=$dd_count of=$file \
4865                 >/dev/null 2>&1 ||
4866                 error "write data into $file failed"
4867         done
4868     done
4869
4870     local expected=-1
4871     [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
4872
4873     # lfs_migrate file
4874     local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
4875     echo "$cmd"
4876     eval $cmd || error "$cmd failed"
4877
4878     check_stripe_count $TDIR/file1 $expected
4879
4880         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
4881         then
4882                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
4883                 # OST 1 if it is on OST 0. This file is small enough to
4884                 # be on only one stripe.
4885                 file=$TDIR/migr_1_ost
4886                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
4887                         error "write data into $file failed"
4888                 local obdidx=$($LFS getstripe -i $file)
4889                 local oldmd5=$(md5sum $file)
4890                 local newobdidx=0
4891                 [[ $obdidx -eq 0 ]] && newobdidx=1
4892                 cmd="$LFS migrate -i $newobdidx $file"
4893                 echo $cmd
4894                 eval $cmd || error "$cmd failed"
4895                 local realobdix=$($LFS getstripe -i $file)
4896                 local newmd5=$(md5sum $file)
4897                 [[ $newobdidx -ne $realobdix ]] &&
4898                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
4899                 [[ "$oldmd5" != "$newmd5" ]] &&
4900                         error "md5sum differ: $oldmd5, $newmd5"
4901         fi
4902
4903     # lfs_migrate dir
4904     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
4905     echo "$cmd"
4906     eval $cmd || error "$cmd failed"
4907
4908     for j in $(seq 1 $NUMFILES); do
4909         check_stripe_count $TDIR/dir1/file$j $expected
4910     done
4911
4912     # lfs_migrate works with lfs find
4913     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
4914          $LFS_MIGRATE -y -c $expected"
4915     echo "$cmd"
4916     eval $cmd || error "$cmd failed"
4917
4918     for i in $(seq 2 $NUMFILES); do
4919         check_stripe_count $TDIR/file$i $expected
4920     done
4921     for i in $(seq 2 $NUMDIRS); do
4922         for j in $(seq 1 $NUMFILES); do
4923             check_stripe_count $TDIR/dir$i/file$j $expected
4924         done
4925     done
4926 }
4927 run_test 56w "check lfs_migrate -c stripe_count works"
4928
4929 test_56x() {
4930         check_swap_layouts_support && return 0
4931         [[ $OSTCOUNT -lt 2 ]] &&
4932                 skip_env "need 2 OST, skipping test" && return
4933
4934         local dir0=$DIR/$tdir/$testnum
4935         mkdir -p $dir0 || error "creating dir $dir0"
4936
4937         local ref1=/etc/passwd
4938         local file1=$dir0/file1
4939
4940         $SETSTRIPE -c 2 $file1
4941         cp $ref1 $file1
4942         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
4943         stripe=$($GETSTRIPE -c $file1)
4944         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
4945         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
4946
4947         # clean up
4948         rm -f $file1
4949 }
4950 run_test 56x "lfs migration support"
4951
4952 test_56y() {
4953         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
4954                 skip "No HSM support on MDS of $(get_lustre_version)," \
4955                          "need 2.4.53 at least" && return
4956         local res=""
4957
4958         local dir0=$DIR/$tdir/$testnum
4959         mkdir -p $dir0 || error "creating dir $dir0"
4960         local f1=$dir0/file1
4961         local f2=$dir0/file2
4962
4963         touch $f1 || error "creating std file $f1"
4964         $MULTIOP $f2 H2c || error "creating released file $f2"
4965
4966         # a directory can be raid0, so ask only for files
4967         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
4968         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
4969
4970         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
4971         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
4972
4973         # only files can be released, so no need to force file search
4974         res=$($LFIND $dir0 -L released)
4975         [[ $res == $f2 ]] || error "search released: found $res != $f2"
4976
4977         res=$($LFIND $dir0 \! -L released)
4978         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
4979
4980 }
4981 run_test 56y "lfs find -L raid0|released"
4982
4983 test_56z() { # LU-4824
4984         # This checks to make sure 'lfs find' continues after errors
4985         # There are two classes of errors that should be caught:
4986         # - If multiple paths are provided, all should be searched even if one
4987         #   errors out
4988         # - If errors are encountered during the search, it should not terminate
4989         #   early
4990         local i
4991         test_mkdir $DIR/$tdir
4992         for i in d{0..9}; do
4993                 test_mkdir $DIR/$tdir/$i
4994         done
4995         touch $DIR/$tdir/d{0..9}/$tfile
4996         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
4997                 error "$LFS find did not return an error"
4998         # Make a directory unsearchable. This should NOT be the last entry in
4999         # directory order.  Arbitrarily pick the 6th entry
5000         chmod 700 $(lfs find $DIR/$tdir -type d | sed '6!d')
5001         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5002         # The user should be able to see 10 directories and 9 files
5003         [ $count == 19 ] || error "$LFS find did not continue after error"
5004 }
5005 run_test 56z "lfs find should continue after an error"
5006
5007 test_57a() {
5008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5009         # note test will not do anything if MDS is not local
5010         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5011                 skip "Only applicable to ldiskfs-based MDTs"
5012                 return
5013         fi
5014
5015         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5016         local MNTDEV="osd*.*MDT*.mntdev"
5017         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5018         [ -z "$DEV" ] && error "can't access $MNTDEV"
5019         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5020                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5021                         error "can't access $DEV"
5022                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5023                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5024                 rm $TMP/t57a.dump
5025         done
5026 }
5027 run_test 57a "verify MDS filesystem created with large inodes =="
5028
5029 test_57b() {
5030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5031         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5032                 skip "Only applicable to ldiskfs-based MDTs"
5033                 return
5034         fi
5035
5036         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5037         local dir=$DIR/$tdir
5038
5039         local FILECOUNT=100
5040         local FILE1=$dir/f1
5041         local FILEN=$dir/f$FILECOUNT
5042
5043         rm -rf $dir || error "removing $dir"
5044         test_mkdir -p -c1 $dir || error "creating $dir"
5045         local mdtidx=$($LFS getstripe -M $dir)
5046         local mdtname=MDT$(printf %04x $mdtidx)
5047         local facet=mds$((mdtidx + 1))
5048
5049         echo "mcreating $FILECOUNT files"
5050         createmany -m $dir/f 1 $FILECOUNT || \
5051                 error "creating files in $dir"
5052
5053         # verify that files do not have EAs yet
5054         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5055         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5056
5057         sync
5058         sleep 1
5059         df $dir  #make sure we get new statfs data
5060         local MDSFREE=$(do_facet $facet \
5061                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5062         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5063         echo "opening files to create objects/EAs"
5064         local FILE
5065         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5066                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5067         done
5068
5069         # verify that files have EAs now
5070         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5071         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5072
5073         sleep 1  #make sure we get new statfs data
5074         df $dir
5075         local MDSFREE2=$(do_facet $facet \
5076                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5077         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5078         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5079                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5080                         error "MDC before $MDCFREE != after $MDCFREE2"
5081                 else
5082                         echo "MDC before $MDCFREE != after $MDCFREE2"
5083                         echo "unable to confirm if MDS has large inodes"
5084                 fi
5085         fi
5086         rm -rf $dir
5087 }
5088 run_test 57b "default LOV EAs are stored inside large inodes ==="
5089
5090 test_58() {
5091         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5092         [ -z "$(which wiretest 2>/dev/null)" ] &&
5093                         skip_env "could not find wiretest" && return
5094         wiretest
5095 }
5096 run_test 58 "verify cross-platform wire constants =============="
5097
5098 test_59() {
5099         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5100         echo "touch 130 files"
5101         createmany -o $DIR/f59- 130
5102         echo "rm 130 files"
5103         unlinkmany $DIR/f59- 130
5104         sync
5105         # wait for commitment of removal
5106         wait_delete_completed
5107 }
5108 run_test 59 "verify cancellation of llog records async ========="
5109
5110 TEST60_HEAD="test_60 run $RANDOM"
5111 test_60a() {
5112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5113         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5114         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5115                 skip_env "missing subtest run-llog.sh" && return
5116         log "$TEST60_HEAD - from kernel mode"
5117         do_facet mgs sh run-llog.sh
5118 }
5119 run_test 60a "llog sanity tests run from kernel module =========="
5120
5121 test_60b() { # bug 6411
5122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5123         dmesg > $DIR/$tfile
5124         LLOG_COUNT=`dmesg | awk "/$TEST60_HEAD/{marker = 1; from_marker = 0;}
5125                                  /llog.test/ {
5126                                          if (marker)
5127                                                  from_marker++
5128                                          from_begin++
5129                                  }
5130                                  END {
5131                                          if (marker)
5132                                                  print from_marker
5133                                          else
5134                                                  print from_begin
5135                                  }"`
5136         [[ $LLOG_COUNT -gt 50 ]] &&
5137                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5138 }
5139 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5140
5141 test_60c() {
5142         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5143         echo "create 5000 files"
5144         createmany -o $DIR/f60c- 5000
5145 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5146         lctl set_param fail_loc=0x80000137
5147         unlinkmany $DIR/f60c- 5000
5148         lctl set_param fail_loc=0
5149 }
5150 run_test 60c "unlink file when mds full"
5151
5152 test_60d() {
5153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5154         SAVEPRINTK=$(lctl get_param -n printk)
5155
5156         # verify "lctl mark" is even working"
5157         MESSAGE="test message ID $RANDOM $$"
5158         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5159         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5160
5161         lctl set_param printk=0 || error "set lnet.printk failed"
5162         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5163         MESSAGE="new test message ID $RANDOM $$"
5164         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5165         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5166         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5167
5168         lctl set_param -n printk="$SAVEPRINTK"
5169 }
5170 run_test 60d "test printk console message masking"
5171
5172 test_61() {
5173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5174         f="$DIR/f61"
5175         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5176         cancel_lru_locks osc
5177         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5178         sync
5179 }
5180 run_test 61 "mmap() writes don't make sync hang ================"
5181
5182 # bug 2330 - insufficient obd_match error checking causes LBUG
5183 test_62() {
5184         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5185         f="$DIR/f62"
5186         echo foo > $f
5187         cancel_lru_locks osc
5188         lctl set_param fail_loc=0x405
5189         cat $f && error "cat succeeded, expect -EIO"
5190         lctl set_param fail_loc=0
5191 }
5192 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5193 # match every page all of the time.
5194 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5195
5196 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5197 # Though this test is irrelevant anymore, it helped to reveal some
5198 # other grant bugs (LU-4482), let's keep it.
5199 test_63a() {   # was test_63
5200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5201         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5202         for i in `seq 10` ; do
5203                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5204                 sleep 5
5205                 kill $!
5206                 sleep 1
5207         done
5208
5209         rm -f $DIR/f63 || true
5210 }
5211 run_test 63a "Verify oig_wait interruption does not crash ======="
5212
5213 # bug 2248 - async write errors didn't return to application on sync
5214 # bug 3677 - async write errors left page locked
5215 test_63b() {
5216         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5217         debugsave
5218         lctl set_param debug=-1
5219
5220         # ensure we have a grant to do async writes
5221         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5222         rm $DIR/$tfile
5223
5224         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5225         lctl set_param fail_loc=0x80000406
5226         $MULTIOP $DIR/$tfile Owy && \
5227                 error "sync didn't return ENOMEM"
5228         sync; sleep 2; sync     # do a real sync this time to flush page
5229         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5230                 error "locked page left in cache after async error" || true
5231         debugrestore
5232 }
5233 run_test 63b "async write errors should be returned to fsync ==="
5234
5235 test_64a () {
5236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5237         df $DIR
5238         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5239 }
5240 run_test 64a "verify filter grant calculations (in kernel) ====="
5241
5242 test_64b () {
5243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5244         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5245 }
5246 run_test 64b "check out-of-space detection on client ==========="
5247
5248 test_64c() {
5249         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5250 }
5251 run_test 64c "verify grant shrink ========================------"
5252
5253 # bug 1414 - set/get directories' stripe info
5254 test_65a() {
5255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5256         test_mkdir -p $DIR/$tdir
5257         touch $DIR/$tdir/f1
5258         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5259 }
5260 run_test 65a "directory with no stripe info ===================="
5261
5262 test_65b() {
5263         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5264         test_mkdir -p $DIR/$tdir
5265         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5266                                                 error "setstripe"
5267         touch $DIR/$tdir/f2
5268         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5269 }
5270 run_test 65b "directory setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1"
5271
5272 test_65c() {
5273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5274         if [[ $OSTCOUNT -gt 1 ]]; then
5275                 test_mkdir -p $DIR/$tdir
5276                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5277                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5278                 touch $DIR/$tdir/f3
5279                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5280         fi
5281 }
5282 run_test 65c "directory setstripe -S $((STRIPESIZE*4)) -i 1 -c $((OSTCOUNT-1))"
5283
5284 test_65d() {
5285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5286         test_mkdir -p $DIR/$tdir
5287         if [[ $STRIPECOUNT -le 0 ]]; then
5288                 sc=1
5289         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5290 #LOV_MAX_STRIPE_COUNT is 2000
5291                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5292         else
5293                 sc=$(($STRIPECOUNT - 1))
5294         fi
5295         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5296         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5297         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5298                 error "lverify failed"
5299 }
5300 run_test 65d "directory setstripe -S $STRIPESIZE -c stripe_count"
5301
5302 test_65e() {
5303         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5304         test_mkdir -p $DIR/$tdir
5305
5306         $SETSTRIPE $DIR/$tdir || error "setstripe"
5307         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5308                                         error "no stripe info failed"
5309         touch $DIR/$tdir/f6
5310         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5311 }
5312 run_test 65e "directory setstripe defaults ======================="
5313
5314 test_65f() {
5315         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5316         test_mkdir -p $DIR/${tdir}f
5317         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5318 }
5319 run_test 65f "dir setstripe permission (should return error) ==="
5320
5321 test_65g() {
5322         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5323         test_mkdir -p $DIR/$tdir
5324         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5325                                                         error "setstripe"
5326         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5327         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5328                 error "delete default stripe failed"
5329 }
5330 run_test 65g "directory setstripe -d ==========================="
5331
5332 test_65h() {
5333         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5334         test_mkdir -p $DIR/$tdir
5335         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5336                                                         error "setstripe"
5337         test_mkdir -p $DIR/$tdir/dd1
5338         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5339                 error "stripe info inherit failed"
5340 }
5341 run_test 65h "directory stripe info inherit ===================="
5342
5343 test_65i() { # bug6367
5344         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5345         $SETSTRIPE -S 65536 -c -1 $MOUNT
5346 }
5347 run_test 65i "set non-default striping on root directory (bug 6367)="
5348
5349 test_65ia() { # bug12836
5350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5351         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5352 }
5353 run_test 65ia "getstripe on -1 default directory striping"
5354
5355 test_65ib() { # bug12836
5356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5357         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5358 }
5359 run_test 65ib "getstripe -v on -1 default directory striping"
5360
5361 test_65ic() { # bug12836
5362         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5363         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5364 }
5365 run_test 65ic "new find on -1 default directory striping"
5366
5367 test_65j() { # bug6367
5368         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5369         sync; sleep 1
5370         # if we aren't already remounting for each test, do so for this test
5371         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5372                 cleanup || error "failed to unmount"
5373                 setup
5374         fi
5375         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5376 }
5377 run_test 65j "set default striping on root directory (bug 6367)="
5378
5379 test_65k() { # bug11679
5380         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5381         [[ $OSTCOUNT -lt 2 ]] && skip_env "too few OSTs" && return
5382         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5383
5384     echo "Check OST status: "
5385     local MDS_OSCS=`do_facet $SINGLEMDS lctl dl |
5386               awk '/[oO][sS][cC].*md[ts]/ { print $4 }'`
5387
5388     for OSC in $MDS_OSCS; do
5389         echo $OSC "is activate"
5390         do_facet $SINGLEMDS lctl --device %$OSC activate
5391     done
5392
5393     mkdir -p $DIR/$tdir
5394     for INACTIVE_OSC in $MDS_OSCS; do
5395         echo "Deactivate: " $INACTIVE_OSC
5396         do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5397         for STRIPE_OSC in $MDS_OSCS; do
5398             OST=`osc_to_ost $STRIPE_OSC`
5399             IDX=`do_facet $SINGLEMDS lctl get_param -n lov.*md*.target_obd |
5400                  awk -F: /$OST/'{ print $1 }' | head -n 1`
5401
5402             [ -f $DIR/$tdir/$IDX ] && continue
5403             echo "$SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX"
5404             $SETSTRIPE -i $IDX -c 1 $DIR/$tdir/$IDX
5405             RC=$?
5406             [ $RC -ne 0 ] && error "setstripe should have succeeded"
5407         done
5408         rm -f $DIR/$tdir/*
5409         echo $INACTIVE_OSC "is Activate."
5410         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5411     done
5412 }
5413 run_test 65k "validate manual striping works properly with deactivated OSCs"
5414
5415 test_65l() { # bug 12836
5416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5417         test_mkdir -p $DIR/$tdir/test_dir
5418         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5419         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5420 }
5421 run_test 65l "lfs find on -1 stripe dir ========================"
5422
5423 # bug 2543 - update blocks count on client
5424 test_66() {
5425         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5426         COUNT=${COUNT:-8}
5427         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5428         sync; sync_all_data; sync; sync_all_data
5429         cancel_lru_locks osc
5430         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5431         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5432 }
5433 run_test 66 "update inode blocks count on client ==============="
5434
5435 LLOOP=
5436 LLITELOOPLOAD=
5437 cleanup_68() {
5438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5439         trap 0
5440         if [ ! -z "$LLOOP" ]; then
5441                 if swapon -s | grep -q $LLOOP; then
5442                         swapoff $LLOOP || error "swapoff failed"
5443                 fi
5444
5445                 $LCTL blockdev_detach $LLOOP || error "detach failed"
5446                 rm -f $LLOOP
5447                 unset LLOOP
5448         fi
5449         if [ ! -z "$LLITELOOPLOAD" ]; then
5450                 rmmod llite_lloop
5451                 unset LLITELOOPLOAD
5452         fi
5453         rm -f $DIR/f68*
5454 }
5455
5456 meminfo() {
5457         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5458 }
5459
5460 swap_used() {
5461         swapon -s | awk '($1 == "'$1'") { print $4 }'
5462 }
5463
5464 # test case for lloop driver, basic function
5465 test_68a() {
5466         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5467         [ "$UID" != 0 ] && skip_env "must run as root" && return
5468         llite_lloop_enabled || \
5469                 { skip_env "llite_lloop module disabled" && return; }
5470
5471         trap cleanup_68 EXIT
5472
5473         if ! module_loaded llite_lloop; then
5474                 if load_module llite/llite_lloop; then
5475                         LLITELOOPLOAD=yes
5476                 else
5477                         skip_env "can't find module llite_lloop"
5478                         return
5479                 fi
5480         fi
5481
5482         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5483         dd if=/dev/zero of=$DIR/f68a bs=4k count=1024
5484         $LCTL blockdev_attach $DIR/f68a $LLOOP || error "attach failed"
5485
5486         directio rdwr $LLOOP 0 1024 4096 || error "direct write failed"
5487         directio rdwr $LLOOP 0 1025 4096 && error "direct write should fail"
5488
5489         cleanup_68
5490 }
5491 run_test 68a "lloop driver - basic test ========================"
5492
5493 # excercise swapping to lustre by adding a high priority swapfile entry
5494 # and then consuming memory until it is used.
5495 test_68b() {  # was test_68
5496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5497         [ "$UID" != 0 ] && skip_env "must run as root" && return
5498         lctl get_param -n devices | grep -q obdfilter && \
5499                 skip "local OST" && return
5500
5501         grep -q llite_lloop /proc/modules
5502         [ $? -ne 0 ] && skip "can't find module llite_lloop" && return
5503
5504         [ -z "`$LCTL list_nids | grep -v tcp`" ] && \
5505                 skip "can't reliably test swap with TCP" && return
5506
5507         MEMTOTAL=`meminfo MemTotal`
5508         NR_BLOCKS=$((MEMTOTAL>>8))
5509         [[ $NR_BLOCKS -le 2048 ]] && NR_BLOCKS=2048
5510
5511         LLOOP=$TMP/lloop.`date +%s`.`date +%N`
5512         dd if=/dev/zero of=$DIR/f68b bs=64k seek=$NR_BLOCKS count=1
5513         mkswap $DIR/f68b
5514
5515         $LCTL blockdev_attach $DIR/f68b $LLOOP || error "attach failed"
5516
5517         trap cleanup_68 EXIT
5518
5519         swapon -p 32767 $LLOOP || error "swapon $LLOOP failed"
5520
5521         echo "before: `swapon -s | grep $LLOOP`"
5522         $MEMHOG $MEMTOTAL || error "error allocating $MEMTOTAL kB"
5523         echo "after: `swapon -s | grep $LLOOP`"
5524         SWAPUSED=`swap_used $LLOOP`
5525
5526         cleanup_68
5527
5528         [ $SWAPUSED -eq 0 ] && echo "no swap used???" || true
5529 }
5530 run_test 68b "support swapping to Lustre ========================"
5531
5532 # bug5265, obdfilter oa2dentry return -ENOENT
5533 # #define OBD_FAIL_OST_ENOENT 0x217
5534 test_69() {
5535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5536         remote_ost_nodsh && skip "remote OST with nodsh" && return
5537
5538         f="$DIR/$tfile"
5539         $SETSTRIPE -c 1 -i 0 $f
5540
5541         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
5542
5543         do_facet ost1 lctl set_param fail_loc=0x217
5544         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
5545         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
5546
5547         do_facet ost1 lctl set_param fail_loc=0
5548         $DIRECTIO write $f 0 2 || error "write error"
5549
5550         cancel_lru_locks osc
5551         $DIRECTIO read $f 0 1 || error "read error"
5552
5553         do_facet ost1 lctl set_param fail_loc=0x217
5554         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
5555
5556         do_facet ost1 lctl set_param fail_loc=0
5557         rm -f $f
5558 }
5559 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
5560
5561 test_71() {
5562     test_mkdir -p $DIR/$tdir
5563     sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
5564 }
5565 run_test 71 "Running dbench on lustre (don't segment fault) ===="
5566
5567 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
5568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5569         [ "$RUNAS_ID" = "$UID" ] &&
5570                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5571
5572         # Check that testing environment is properly set up. Skip if not
5573         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
5574                 skip_env "User $RUNAS_ID does not exist - skipping"
5575                 return 0
5576         }
5577         touch $DIR/$tfile
5578         chmod 777 $DIR/$tfile
5579         chmod ug+s $DIR/$tfile
5580         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
5581                 error "$RUNAS dd $DIR/$tfile failed"
5582         # See if we are still setuid/sgid
5583         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5584                 error "S/gid is not dropped on write"
5585         # Now test that MDS is updated too
5586         cancel_lru_locks mdc
5587         test -u $DIR/$tfile -o -g $DIR/$tfile &&
5588                 error "S/gid is not dropped on MDS"
5589         rm -f $DIR/$tfile
5590 }
5591 run_test 72a "Test that remove suid works properly (bug5695) ===="
5592
5593 test_72b() { # bug 24226 -- keep mode setting when size is not changing
5594         local perm
5595
5596         [ "$RUNAS_ID" = "$UID" ] && \
5597                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
5598         [ "$RUNAS_ID" -eq 0 ] && \
5599                 skip_env "RUNAS_ID = 0 -- skipping" && return
5600
5601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5602         # Check that testing environment is properly set up. Skip if not
5603         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
5604                 skip_env "User $RUNAS_ID does not exist - skipping"
5605                 return 0
5606         }
5607         touch $DIR/${tfile}-f{g,u}
5608         test_mkdir $DIR/${tfile}-dg
5609         test_mkdir $DIR/${tfile}-du
5610         chmod 770 $DIR/${tfile}-{f,d}{g,u}
5611         chmod g+s $DIR/${tfile}-{f,d}g
5612         chmod u+s $DIR/${tfile}-{f,d}u
5613         for perm in 777 2777 4777; do
5614                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
5615                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
5616                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
5617                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
5618         done
5619         true
5620 }
5621 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
5622
5623 # bug 3462 - multiple simultaneous MDC requests
5624 test_73() {
5625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5626         test_mkdir $DIR/d73-1
5627         test_mkdir $DIR/d73-2
5628         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
5629         pid1=$!
5630
5631         lctl set_param fail_loc=0x80000129
5632         $MULTIOP $DIR/d73-1/f73-2 Oc &
5633         sleep 1
5634         lctl set_param fail_loc=0
5635
5636         $MULTIOP $DIR/d73-2/f73-3 Oc &
5637         pid3=$!
5638
5639         kill -USR1 $pid1
5640         wait $pid1 || return 1
5641
5642         sleep 25
5643
5644         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
5645         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
5646         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
5647
5648         rm -rf $DIR/d73-*
5649 }
5650 run_test 73 "multiple MDC requests (should not deadlock)"
5651
5652 test_74a() { # bug 6149, 6184
5653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5654         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5655         #
5656         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5657         # will spin in a tight reconnection loop
5658         touch $DIR/f74a
5659         $LCTL set_param fail_loc=0x8000030e
5660         # get any lock that won't be difficult - lookup works.
5661         ls $DIR/f74a
5662         $LCTL set_param fail_loc=0
5663         rm -f $DIR/f74a
5664         true
5665 }
5666 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
5667
5668 test_74b() { # bug 13310
5669         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5670         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
5671         #
5672         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
5673         # will spin in a tight reconnection loop
5674         $LCTL set_param fail_loc=0x8000030e
5675         # get a "difficult" lock
5676         touch $DIR/f74b
5677         $LCTL set_param fail_loc=0
5678         rm -f $DIR/f74b
5679         true
5680 }
5681 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
5682
5683 test_74c() {
5684         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5685         #define OBD_FAIL_LDLM_NEW_LOCK
5686         $LCTL set_param fail_loc=0x319
5687         touch $DIR/$tfile && error "touch successful"
5688         $LCTL set_param fail_loc=0
5689         true
5690 }
5691 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
5692
5693 num_inodes() {
5694         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
5695 }
5696
5697 get_inode_slab_tunables() {
5698         awk '/lustre_inode_cache/ {print $9," ",$10," ",$11; exit}' /proc/slabinfo
5699 }
5700
5701 set_inode_slab_tunables() {
5702         echo "lustre_inode_cache $1" > /proc/slabinfo
5703 }
5704
5705 test_76() { # Now for bug 20433, added originally in bug 1443
5706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5707         local SLAB_SETTINGS=`get_inode_slab_tunables`
5708         local CPUS=`getconf _NPROCESSORS_ONLN`
5709         # we cannot set limit below 1 which means 1 inode in each
5710         # per-cpu cache is still allowed
5711         set_inode_slab_tunables "1 1 0"
5712         cancel_lru_locks osc
5713         BEFORE_INODES=$(num_inodes)
5714         echo "before inodes: $BEFORE_INODES"
5715         local COUNT=1000
5716         [ "$SLOW" = "no" ] && COUNT=100
5717         for i in $(seq $COUNT); do
5718                 touch $DIR/$tfile
5719                 rm -f $DIR/$tfile
5720         done
5721         cancel_lru_locks osc
5722         AFTER_INODES=$(num_inodes)
5723         echo "after inodes: $AFTER_INODES"
5724         local wait=0
5725         while [[ $((AFTER_INODES-1*CPUS)) -gt $BEFORE_INODES ]]; do
5726                 sleep 2
5727                 AFTER_INODES=$(num_inodes)
5728                 wait=$((wait+2))
5729                 echo "wait $wait seconds inodes: $AFTER_INODES"
5730                 if [ $wait -gt 30 ]; then
5731                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
5732                 fi
5733         done
5734         set_inode_slab_tunables "$SLAB_SETTINGS"
5735 }
5736 run_test 76 "confirm clients recycle inodes properly ===="
5737
5738
5739 export ORIG_CSUM=""
5740 set_checksums()
5741 {
5742         # Note: in sptlrpc modes which enable its own bulk checksum, the
5743         # original crc32_le bulk checksum will be automatically disabled,
5744         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
5745         # will be checked by sptlrpc code against sptlrpc bulk checksum.
5746         # In this case set_checksums() will not be no-op, because sptlrpc
5747         # bulk checksum will be enabled all through the test.
5748
5749         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
5750         lctl set_param -n osc.*.checksums $1
5751         return 0
5752 }
5753
5754 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
5755                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
5756 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
5757 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
5758 set_checksum_type()
5759 {
5760         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
5761         log "set checksum type to $1"
5762         return 0
5763 }
5764 F77_TMP=$TMP/f77-temp
5765 F77SZ=8
5766 setup_f77() {
5767         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
5768                 error "error writing to $F77_TMP"
5769 }
5770
5771 test_77a() { # bug 10889
5772         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5773         $GSS && skip "could not run with gss" && return
5774         [ ! -f $F77_TMP ] && setup_f77
5775         set_checksums 1
5776         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
5777         set_checksums 0
5778         rm -f $DIR/$tfile
5779 }
5780 run_test 77a "normal checksum read/write operation"
5781
5782 test_77b() { # bug 10889
5783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5784         $GSS && skip "could not run with gss" && return
5785         [ ! -f $F77_TMP ] && setup_f77
5786         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5787         $LCTL set_param fail_loc=0x80000409
5788         set_checksums 1
5789
5790         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
5791                 error "dd error: $?"
5792         $LCTL set_param fail_loc=0
5793
5794         for algo in $CKSUM_TYPES; do
5795                 cancel_lru_locks osc
5796                 set_checksum_type $algo
5797                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5798                 $LCTL set_param fail_loc=0x80000408
5799                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5800                 $LCTL set_param fail_loc=0
5801         done
5802         set_checksums 0
5803         set_checksum_type $ORIG_CSUM_TYPE
5804         rm -f $DIR/$tfile
5805 }
5806 run_test 77b "checksum error on client write, read"
5807
5808 test_77d() { # bug 10889
5809         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5810         $GSS && skip "could not run with gss" && return
5811         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5812         $LCTL set_param fail_loc=0x80000409
5813         set_checksums 1
5814         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5815                 error "direct write: rc=$?"
5816         $LCTL set_param fail_loc=0
5817         set_checksums 0
5818
5819         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
5820         $LCTL set_param fail_loc=0x80000408
5821         set_checksums 1
5822         cancel_lru_locks osc
5823         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
5824                 error "direct read: rc=$?"
5825         $LCTL set_param fail_loc=0
5826         set_checksums 0
5827 }
5828 run_test 77d "checksum error on OST direct write, read"
5829
5830 test_77f() { # bug 10889
5831         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5832         $GSS && skip "could not run with gss" && return
5833         set_checksums 1
5834         for algo in $CKSUM_TYPES; do
5835                 cancel_lru_locks osc
5836                 set_checksum_type $algo
5837                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
5838                 $LCTL set_param fail_loc=0x409
5839                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
5840                         error "direct write succeeded"
5841                 $LCTL set_param fail_loc=0
5842         done
5843         set_checksum_type $ORIG_CSUM_TYPE
5844         set_checksums 0
5845 }
5846 run_test 77f "repeat checksum error on write (expect error)"
5847
5848 test_77g() { # bug 10889
5849         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5850         $GSS && skip "could not run with gss" && return
5851         remote_ost_nodsh && skip "remote OST with nodsh" && return
5852
5853         [ ! -f $F77_TMP ] && setup_f77
5854
5855         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
5856         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
5857         do_facet ost1 lctl set_param fail_loc=0x8000021a
5858         set_checksums 1
5859         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
5860                 error "write error: rc=$?"
5861         do_facet ost1 lctl set_param fail_loc=0
5862         set_checksums 0
5863
5864         cancel_lru_locks osc
5865         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
5866         do_facet ost1 lctl set_param fail_loc=0x8000021b
5867         set_checksums 1
5868         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
5869         do_facet ost1 lctl set_param fail_loc=0
5870         set_checksums 0
5871 }
5872 run_test 77g "checksum error on OST write, read"
5873
5874 test_77i() { # bug 13805
5875         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5876         $GSS && skip "could not run with gss" && return
5877         #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
5878         lctl set_param fail_loc=0x40b
5879         remount_client $MOUNT
5880         lctl set_param fail_loc=0
5881         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5882                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5883                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5884                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5885         done
5886         remount_client $MOUNT
5887 }
5888 run_test 77i "client not supporting OSD_CONNECT_CKSUM"
5889
5890 test_77j() { # bug 13805
5891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5892         $GSS && skip "could not run with gss" && return
5893         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
5894         lctl set_param fail_loc=0x40c
5895         remount_client $MOUNT
5896         lctl set_param fail_loc=0
5897         sleep 2 # wait async osc connect to finish
5898         for VALUE in `lctl get_param osc.*osc-[^mM]*.checksum_type`; do
5899                 PARAM=`echo ${VALUE[0]} | cut -d "=" -f1`
5900                 algo=`lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g'`
5901                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
5902         done
5903         remount_client $MOUNT
5904 }
5905 run_test 77j "client only supporting ADLER32"
5906
5907 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
5908 rm -f $F77_TMP
5909 unset F77_TMP
5910
5911 test_78() { # bug 10901
5912         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5913         remote_ost || { skip_env "local OST" && return; }
5914
5915         NSEQ=5
5916         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
5917         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
5918         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
5919         echo "MemTotal: $MEMTOTAL"
5920
5921         # reserve 256MB of memory for the kernel and other running processes,
5922         # and then take 1/2 of the remaining memory for the read/write buffers.
5923         if [ $MEMTOTAL -gt 512 ] ;then
5924                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
5925         else
5926                 # for those poor memory-starved high-end clusters...
5927                 MEMTOTAL=$((MEMTOTAL / 2))
5928         fi
5929         echo "Mem to use for directio: $MEMTOTAL"
5930
5931         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
5932         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
5933         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
5934         SMALLESTOST=$(lfs df $DIR | grep OST | awk '{ print $4 }' | sort -n |
5935                 head -n1)
5936         echo "Smallest OST: $SMALLESTOST"
5937         [[ $SMALLESTOST -lt 10240 ]] &&
5938                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
5939
5940         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
5941                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
5942
5943         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
5944         echo "File size: $F78SIZE"
5945         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
5946         for i in $(seq 1 $NSEQ); do
5947                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
5948                 echo directIO rdwr round $i of $NSEQ
5949                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
5950         done
5951
5952         rm -f $DIR/$tfile
5953 }
5954 run_test 78 "handle large O_DIRECT writes correctly ============"
5955
5956 test_79() { # bug 12743
5957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5958         wait_delete_completed
5959
5960         BKTOTAL=$(calc_osc_kbytes kbytestotal)
5961         BKFREE=$(calc_osc_kbytes kbytesfree)
5962         BKAVAIL=$(calc_osc_kbytes kbytesavail)
5963
5964         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
5965         DFTOTAL=`echo $STRING | cut -d, -f1`
5966         DFUSED=`echo $STRING  | cut -d, -f2`
5967         DFAVAIL=`echo $STRING | cut -d, -f3`
5968         DFFREE=$(($DFTOTAL - $DFUSED))
5969
5970         ALLOWANCE=$((64 * $OSTCOUNT))
5971
5972         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
5973            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
5974                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
5975         fi
5976         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
5977            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
5978                 error "df free($DFFREE) mismatch OST free($BKFREE)"
5979         fi
5980         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
5981            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
5982                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
5983         fi
5984 }
5985 run_test 79 "df report consistency check ======================="
5986
5987 test_80() { # bug 10718
5988         remote_ost_nodsh && skip "remote OST with nodsh" && return
5989         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5990         # relax strong synchronous semantics for slow backends like ZFS
5991         local soc="obdfilter.*.sync_on_lock_cancel"
5992         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
5993         local hosts=
5994         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
5995                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
5996                           facet_active_host $host; done | sort -u)
5997                 do_nodes $hosts lctl set_param $soc=never
5998         fi
5999
6000         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6001         sync; sleep 1; sync
6002         local BEFORE=`date +%s`
6003         cancel_lru_locks osc
6004         local AFTER=`date +%s`
6005         local DIFF=$((AFTER-BEFORE))
6006         if [ $DIFF -gt 1 ] ; then
6007                 error "elapsed for 1M@1T = $DIFF"
6008         fi
6009
6010         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6011
6012         rm -f $DIR/$tfile
6013 }
6014 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6015
6016 test_81a() { # LU-456
6017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6018         remote_ost_nodsh && skip "remote OST with nodsh" && return
6019         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6020         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6021         do_facet ost1 lctl set_param fail_loc=0x80000228
6022
6023         # write should trigger a retry and success
6024         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6025         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6026         RC=$?
6027         if [ $RC -ne 0 ] ; then
6028                 error "write should success, but failed for $RC"
6029         fi
6030 }
6031 run_test 81a "OST should retry write when get -ENOSPC ==============="
6032
6033 test_81b() { # LU-456
6034         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6035         remote_ost_nodsh && skip "remote OST with nodsh" && return
6036         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6037         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6038         do_facet ost1 lctl set_param fail_loc=0x228
6039
6040         # write should retry several times and return -ENOSPC finally
6041         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6042         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6043         RC=$?
6044         ENOSPC=28
6045         if [ $RC -ne $ENOSPC ] ; then
6046                 error "dd should fail for -ENOSPC, but succeed."
6047         fi
6048 }
6049 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6050
6051 test_82() { # LU-1031
6052         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6053         local gid1=14091995
6054         local gid2=16022000
6055
6056         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6057         local MULTIPID1=$!
6058         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6059         local MULTIPID2=$!
6060         kill -USR1 $MULTIPID2
6061         sleep 2
6062         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6063                 error "First grouplock does not block second one"
6064         else
6065                 echo "Second grouplock blocks first one"
6066         fi
6067         kill -USR1 $MULTIPID1
6068         wait $MULTIPID1
6069         wait $MULTIPID2
6070 }
6071 run_test 82 "Basic grouplock test ==============================="
6072
6073 test_99a() {
6074         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6075                 return
6076         test_mkdir -p $DIR/d99cvsroot
6077         chown $RUNAS_ID $DIR/d99cvsroot
6078         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6079         cd $TMP
6080
6081         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6082         cd $oldPWD
6083 }
6084 run_test 99a "cvs init ========================================="
6085
6086 test_99b() {
6087         [ -z "$(which cvs 2>/dev/null)" ] &&
6088                 skip_env "could not find cvs" && return
6089         [ ! -d $DIR/d99cvsroot ] && test_99a
6090         cd /etc/init.d
6091         # some versions of cvs import exit(1) when asked to import links or
6092         # files they can't read.  ignore those files.
6093         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6094                         ! -perm +4 -printf '-I %f\n')
6095         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6096                 d99reposname vtag rtag
6097 }
6098 run_test 99b "cvs import ======================================="
6099
6100 test_99c() {
6101         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6102         [ ! -d $DIR/d99cvsroot ] && test_99b
6103         cd $DIR
6104         test_mkdir -p $DIR/d99reposname
6105         chown $RUNAS_ID $DIR/d99reposname
6106         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6107 }
6108 run_test 99c "cvs checkout ====================================="
6109
6110 test_99d() {
6111         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6112         [ ! -d $DIR/d99cvsroot ] && test_99c
6113         cd $DIR/d99reposname
6114         $RUNAS touch foo99
6115         $RUNAS cvs add -m 'addmsg' foo99
6116 }
6117 run_test 99d "cvs add =========================================="
6118
6119 test_99e() {
6120         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6121         [ ! -d $DIR/d99cvsroot ] && test_99c
6122         cd $DIR/d99reposname
6123         $RUNAS cvs update
6124 }
6125 run_test 99e "cvs update ======================================="
6126
6127 test_99f() {
6128         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6129         [ ! -d $DIR/d99cvsroot ] && test_99d
6130         cd $DIR/d99reposname
6131         $RUNAS cvs commit -m 'nomsg' foo99
6132     rm -fr $DIR/d99cvsroot
6133 }
6134 run_test 99f "cvs commit ======================================="
6135
6136 test_100() {
6137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6138         [ "$NETTYPE" = tcp ] || \
6139                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" && \
6140                         return ; }
6141
6142         remote_ost_nodsh && skip "remote OST with nodsh" && return
6143         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6144         remote_servers || \
6145                 { skip "useless for local single node setup" && return; }
6146
6147         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6148                 [ "$PROT" != "tcp" ] && continue
6149                 RPORT=$(echo $REMOTE | cut -d: -f2)
6150                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6151
6152                 rc=0
6153                 LPORT=`echo $LOCAL | cut -d: -f2`
6154                 if [ $LPORT -ge 1024 ]; then
6155                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6156                         netstat -tna
6157                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6158                 fi
6159         done
6160         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6161 }
6162 run_test 100 "check local port using privileged port ==========="
6163
6164 function get_named_value()
6165 {
6166     local tag
6167
6168     tag=$1
6169     while read ;do
6170         line=$REPLY
6171         case $line in
6172         $tag*)
6173             echo $line | sed "s/^$tag[ ]*//"
6174             break
6175             ;;
6176         esac
6177     done
6178 }
6179
6180 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6181                    awk '/^max_cached_mb/ { print $2 }')
6182
6183 cleanup_101a() {
6184         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6185         trap 0
6186 }
6187
6188 test_101a() {
6189         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6190         [ $MDSCOUNT -ge 2 ] && skip "skip now for >= 2 MDTs" && return #LU-4322
6191         local s
6192         local discard
6193         local nreads=10000
6194         local cache_limit=32
6195
6196         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6197         trap cleanup_101a EXIT
6198         $LCTL set_param -n llite.*.read_ahead_stats 0
6199         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6200
6201         #
6202         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6203         #
6204         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6205         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6206
6207         discard=0
6208         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6209                 get_named_value 'read but discarded' | cut -d" " -f1); do
6210                         discard=$(($discard + $s))
6211         done
6212         cleanup_101a
6213
6214         if [[ $(($discard * 10)) -gt $nreads ]]; then
6215                 $LCTL get_param osc.*-osc*.rpc_stats
6216                 $LCTL get_param llite.*.read_ahead_stats
6217                 error "too many ($discard) discarded pages"
6218         fi
6219         rm -f $DIR/$tfile || true
6220 }
6221 run_test 101a "check read-ahead for random reads ================"
6222
6223 setup_test101bc() {
6224         test_mkdir -p $DIR/$tdir
6225         local STRIPE_SIZE=$1
6226         local FILE_LENGTH=$2
6227         STRIPE_OFFSET=0
6228
6229         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6230
6231         local list=$(comma_list $(osts_nodes))
6232         set_osd_param $list '' read_cache_enable 0
6233         set_osd_param $list '' writethrough_cache_enable 0
6234
6235         trap cleanup_test101bc EXIT
6236         # prepare the read-ahead file
6237         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6238
6239         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6240                                 count=$FILE_SIZE_MB 2> /dev/null
6241
6242 }
6243
6244 cleanup_test101bc() {
6245         trap 0
6246         rm -rf $DIR/$tdir
6247         rm -f $DIR/$tfile
6248
6249         local list=$(comma_list $(osts_nodes))
6250         set_osd_param $list '' read_cache_enable 1
6251         set_osd_param $list '' writethrough_cache_enable 1
6252 }
6253
6254 calc_total() {
6255         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6256 }
6257
6258 ra_check_101() {
6259         local READ_SIZE=$1
6260         local STRIPE_SIZE=$2
6261         local FILE_LENGTH=$3
6262         local RA_INC=1048576
6263         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6264         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6265                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6266         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6267                         get_named_value 'read but discarded' |
6268                         cut -d" " -f1 | calc_total)
6269         if [[ $DISCARD -gt $discard_limit ]]; then
6270                 $LCTL get_param llite.*.read_ahead_stats
6271                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6272         else
6273                 echo "Read-ahead success for size ${READ_SIZE}"
6274         fi
6275 }
6276
6277 test_101b() {
6278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6279         [[ $OSTCOUNT -lt 2 ]] &&
6280                 skip_env "skipping stride IO stride-ahead test" && return
6281         local STRIPE_SIZE=1048576
6282         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6283         if [ $SLOW == "yes" ]; then
6284                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6285         else
6286                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6287         fi
6288
6289         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6290
6291         # prepare the read-ahead file
6292         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6293         cancel_lru_locks osc
6294         for BIDX in 2 4 8 16 32 64 128 256
6295         do
6296                 local BSIZE=$((BIDX*4096))
6297                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6298                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6299                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6300                 $LCTL set_param -n llite.*.read_ahead_stats 0
6301                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6302                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6303                 cancel_lru_locks osc
6304                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6305         done
6306         cleanup_test101bc
6307         true
6308 }
6309 run_test 101b "check stride-io mode read-ahead ================="
6310
6311 test_101c() {
6312         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6313         local STRIPE_SIZE=1048576
6314         local FILE_LENGTH=$((STRIPE_SIZE*100))
6315         local nreads=10000
6316         local osc_rpc_stats
6317
6318         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6319
6320         cancel_lru_locks osc
6321         $LCTL set_param osc.*.rpc_stats 0
6322         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6323         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6324                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6325                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6326                 local size
6327
6328                 if [ $lines -le 20 ]; then
6329                         continue
6330                 fi
6331                 for size in 1 2 4 8; do
6332                         local rpc=$(echo "$stats" |
6333                                     awk '($1 == "'$size':") {print $2; exit; }')
6334                         [ $rpc != 0 ] &&
6335                                 error "Small $((size*4))k read IO $rpc !"
6336                 done
6337                 echo "$osc_rpc_stats check passed!"
6338         done
6339         cleanup_test101bc
6340         true
6341 }
6342 run_test 101c "check stripe_size aligned read-ahead ================="
6343
6344 set_read_ahead() {
6345         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6346         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6347 }
6348
6349 test_101d() {
6350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6351         local file=$DIR/$tfile
6352         local sz_MB=${FILESIZE_101d:-500}
6353         local ra_MB=${READAHEAD_MB:-40}
6354
6355         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6356         [ $free_MB -lt $sz_MB ] &&
6357                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6358
6359         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6360         $SETSTRIPE -c -1 $file || error "setstripe failed"
6361
6362         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6363         echo Cancel LRU locks on lustre client to flush the client cache
6364         cancel_lru_locks osc
6365
6366         echo Disable read-ahead
6367         local old_READAHEAD=$(set_read_ahead 0)
6368
6369         echo Reading the test file $file with read-ahead disabled
6370         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6371
6372         echo Cancel LRU locks on lustre client to flush the client cache
6373         cancel_lru_locks osc
6374         echo Enable read-ahead with ${ra_MB}MB
6375         set_read_ahead $ra_MB
6376
6377         echo Reading the test file $file with read-ahead enabled
6378         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6379
6380         echo "read-ahead disabled time read $raOFF"
6381         echo "read-ahead enabled  time read $raON"
6382
6383         set_read_ahead $old_READAHEAD
6384         rm -f $file
6385         wait_delete_completed
6386
6387         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6388                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6389 }
6390 run_test 101d "file read with and without read-ahead enabled"
6391
6392 test_101e() {
6393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6394         local file=$DIR/$tfile
6395         local size_KB=500  #KB
6396         local count=100
6397         local bsize=1024
6398
6399         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6400         local need_KB=$((count * size_KB))
6401         [[ $free_KB -le $need_KB ]] &&
6402                 skip_env "Need free space $need_KB, have $free_KB" && return
6403
6404         echo "Creating $count ${size_KB}K test files"
6405         for ((i = 0; i < $count; i++)); do
6406                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6407         done
6408
6409         echo "Cancel LRU locks on lustre client to flush the client cache"
6410         cancel_lru_locks osc
6411
6412         echo "Reset readahead stats"
6413         $LCTL set_param -n llite.*.read_ahead_stats 0
6414
6415         for ((i = 0; i < $count; i++)); do
6416                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6417         done
6418
6419         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6420                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6421
6422         for ((i = 0; i < $count; i++)); do
6423                 rm -rf $file.$i 2>/dev/null
6424         done
6425
6426         #10000 means 20% reads are missing in readahead
6427         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6428 }
6429 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6430
6431 cleanup_test101f() {
6432     trap 0
6433     $LCTL set_param -n llite.*.max_read_ahead_whole_mb $MAX_WHOLE_MB
6434     rm -rf $DIR/$tfile 2>/dev/null
6435 }
6436
6437 test_101f() {
6438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6439     local file=$DIR/$tfile
6440     local nreads=1000
6441
6442     MAX_WHOLE_MB=$($LCTL get_param -n llite.*.max_read_ahead_whole_mb)
6443     $LCTL set_param -n llite.*.max_read_ahead_whole_mb 2
6444     dd if=/dev/zero of=${file} bs=2097152 count=1 2>/dev/null
6445     trap cleanup_test101f EXIT
6446
6447     echo Cancel LRU locks on lustre client to flush the client cache
6448     cancel_lru_locks osc
6449
6450     echo Reset readahead stats
6451     $LCTL set_param -n llite.*.read_ahead_stats 0
6452     # Random read in a 2M file, because max_read_ahead_whole_mb = 2M,
6453     # readahead should read in 2M file on second read, so only miss
6454     # 2 pages.
6455     echo Random 4K reads on 2M file for 1000 times
6456     $READS -f $file -s 2097152 -b 4096 -n $nreads
6457
6458     echo checking missing pages
6459     local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6460           get_named_value 'misses' | cut -d" " -f1 | calc_total)
6461
6462     [ $miss -lt 3 ] || error "misses too much pages!"
6463     cleanup_test101f
6464 }
6465 run_test 101f "check read-ahead for max_read_ahead_whole_mb"
6466
6467 setup_test102() {
6468         test_mkdir -p $DIR/$tdir
6469         chown $RUNAS_ID $DIR/$tdir
6470         STRIPE_SIZE=65536
6471         STRIPE_OFFSET=1
6472         STRIPE_COUNT=$OSTCOUNT
6473         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
6474
6475         trap cleanup_test102 EXIT
6476         cd $DIR
6477         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
6478         cd $DIR/$tdir
6479         for num in 1 2 3 4; do
6480                 for count in $(seq 1 $STRIPE_COUNT); do
6481                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
6482                                 local size=`expr $STRIPE_SIZE \* $num`
6483                                 local file=file"$num-$idx-$count"
6484                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
6485                         done
6486                 done
6487         done
6488
6489         cd $DIR
6490         $1 $TAR cf $TMP/f102.tar $tdir --xattrs
6491 }
6492
6493 cleanup_test102() {
6494         trap 0
6495         rm -f $TMP/f102.tar
6496         rm -rf $DIR/d0.sanity/d102
6497 }
6498
6499 test_102a() {
6500         local testfile=$DIR/$tfile
6501
6502         touch $testfile
6503
6504         [ "$UID" != 0 ] && skip_env "must run as root" && return
6505         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
6506                 skip_env "must have user_xattr" && return
6507
6508         [ -z "$(which setfattr 2>/dev/null)" ] &&
6509                 skip_env "could not find setfattr" && return
6510
6511         echo "set/get xattr..."
6512         setfattr -n trusted.name1 -v value1 $testfile ||
6513                 error "setfattr -n trusted.name1=value1 $testfile failed"
6514         getfattr -n trusted.name1 $testfile 2> /dev/null |
6515           grep "trusted.name1=.value1" ||
6516                 error "$testfile missing trusted.name1=value1"
6517
6518         setfattr -n user.author1 -v author1 $testfile ||
6519                 error "setfattr -n user.author1=author1 $testfile failed"
6520         getfattr -n user.author1 $testfile 2> /dev/null |
6521           grep "user.author1=.author1" ||
6522                 error "$testfile missing trusted.author1=author1"
6523
6524         echo "listxattr..."
6525         setfattr -n trusted.name2 -v value2 $testfile ||
6526                 error "$testfile unable to set trusted.name2"
6527         setfattr -n trusted.name3 -v value3 $testfile ||
6528                 error "$testfile unable to set trusted.name3"
6529         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
6530             grep "trusted.name" | wc -l) -eq 3 ] ||
6531                 error "$testfile missing 3 trusted.name xattrs"
6532
6533         setfattr -n user.author2 -v author2 $testfile ||
6534                 error "$testfile unable to set user.author2"
6535         setfattr -n user.author3 -v author3 $testfile ||
6536                 error "$testfile unable to set user.author3"
6537         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
6538             grep "user.author" | wc -l) -eq 3 ] ||
6539                 error "$testfile missing 3 user.author xattrs"
6540
6541         echo "remove xattr..."
6542         setfattr -x trusted.name1 $testfile ||
6543                 error "$testfile error deleting trusted.name1"
6544         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
6545                 error "$testfile did not delete trusted.name1 xattr"
6546
6547         setfattr -x user.author1 $testfile ||
6548                 error "$testfile error deleting user.author1"
6549         getfattr -d -m user $testfile 2> /dev/null | grep "user.author1" &&
6550                 error "$testfile did not delete trusted.name1 xattr"
6551
6552         # b10667: setting lustre special xattr be silently discarded
6553         echo "set lustre special xattr ..."
6554         setfattr -n "trusted.lov" -v "invalid value" $testfile ||
6555                 error "$testfile allowed setting trusted.lov"
6556 }
6557 run_test 102a "user xattr test =================================="
6558
6559 test_102b() {
6560         [ -z "$(which setfattr 2>/dev/null)" ] &&
6561                 skip_env "could not find setfattr" && return
6562
6563         # b10930: get/set/list trusted.lov xattr
6564         echo "get/set/list trusted.lov xattr ..."
6565         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6566         local testfile=$DIR/$tfile
6567         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6568                 error "setstripe failed"
6569         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
6570                 error "getstripe failed"
6571         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
6572                 error "can't get trusted.lov from $testfile"
6573
6574         local testfile2=${testfile}2
6575         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
6576                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
6577
6578         $MCREATE $testfile2
6579         setfattr -n trusted.lov -v $value $testfile2
6580         local stripe_size=$($GETSTRIPE -S $testfile2)
6581         local stripe_count=$($GETSTRIPE -c $testfile2)
6582         [[ $stripe_size -eq 65536 ]] ||
6583                 error "stripe size $stripe_size != 65536"
6584         [[ $stripe_count -eq $STRIPECOUNT ]] ||
6585                 error "stripe count $stripe_count != $STRIPECOUNT"
6586         rm -f $DIR/$tfile
6587 }
6588 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
6589
6590 test_102c() {
6591         [ -z "$(which setfattr 2>/dev/null)" ] &&
6592                 skip_env "could not find setfattr" && return
6593
6594         # b10930: get/set/list lustre.lov xattr
6595         echo "get/set/list lustre.lov xattr ..."
6596         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test" && return
6597         test_mkdir -p $DIR/$tdir
6598         chown $RUNAS_ID $DIR/$tdir
6599         local testfile=$DIR/$tdir/$tfile
6600         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
6601                 error "setstripe failed"
6602         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
6603                 error "getstripe failed"
6604         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
6605         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
6606
6607         local testfile2=${testfile}2
6608         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
6609                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
6610
6611         $RUNAS $MCREATE $testfile2
6612         $RUNAS setfattr -n lustre.lov -v $value $testfile2
6613         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
6614         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
6615         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
6616         [ $stripe_count -eq $STRIPECOUNT ] ||
6617                 error "stripe count $stripe_count != $STRIPECOUNT"
6618 }
6619 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
6620
6621 compare_stripe_info1() {
6622         local stripe_index_all_zero=true
6623
6624         for num in 1 2 3 4; do
6625                 for count in $(seq 1 $STRIPE_COUNT); do
6626                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
6627                                 local size=$((STRIPE_SIZE * num))
6628                                 local file=file"$num-$offset-$count"
6629                                 stripe_size=$(lfs getstripe -S $PWD/$file)
6630                                 [[ $stripe_size -ne $size ]] &&
6631                                     error "$file: size $stripe_size != $size"
6632                                 stripe_count=$(lfs getstripe -c $PWD/$file)
6633                                 # allow fewer stripes to be created, ORI-601
6634                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
6635                                     error "$file: count $stripe_count != $count"
6636                                 stripe_index=$(lfs getstripe -i $PWD/$file)
6637                                 [[ $stripe_index -ne 0 ]] &&
6638                                         stripe_index_all_zero=false
6639                         done
6640                 done
6641         done
6642         $stripe_index_all_zero &&
6643                 error "all files are being extracted starting from OST index 0"
6644         return 0
6645 }
6646
6647 find_lustre_tar() {
6648         [ -n "$(which tar 2>/dev/null)" ] &&
6649                 strings $(which tar) | grep -q "lustre" && echo tar
6650 }
6651
6652 test_102d() {
6653         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6654         # b10930: tar test for trusted.lov xattr
6655         TAR=$(find_lustre_tar)
6656         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6657         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6658         setup_test102
6659         test_mkdir -p $DIR/d102d
6660         $TAR xf $TMP/f102.tar -C $DIR/d102d --xattrs
6661         cd $DIR/d102d/$tdir
6662         compare_stripe_info1
6663 }
6664 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
6665
6666 test_102f() {
6667         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6668         # b10930: tar test for trusted.lov xattr
6669         TAR=$(find_lustre_tar)
6670         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6671         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6672         setup_test102
6673         test_mkdir -p $DIR/d102f
6674         cd $DIR
6675         $TAR cf - --xattrs $tdir | $TAR xf - --xattrs -C $DIR/d102f
6676         cd $DIR/d102f/$tdir
6677         compare_stripe_info1
6678 }
6679 run_test 102f "tar copy files, not keep osts ==========="
6680
6681 grow_xattr() {
6682         local xsize=${1:-1024}  # in bytes
6683         local file=$DIR/$tfile
6684
6685         [ -z $(lctl get_param -n mdc.*.connect_flags | grep xattr) ] &&
6686                 skip "must have user_xattr" && return 0
6687         [ -z "$(which setfattr 2>/dev/null)" ] &&
6688                 skip_env "could not find setfattr" && return 0
6689         [ -z "$(which getfattr 2>/dev/null)" ] &&
6690                 skip_env "could not find getfattr" && return 0
6691
6692         touch $file
6693
6694         local value="$(generate_string $xsize)"
6695
6696         local xbig=trusted.big
6697         log "save $xbig on $file"
6698         setfattr -n $xbig -v $value $file ||
6699                 error "saving $xbig on $file failed"
6700
6701         local orig=$(get_xattr_value $xbig $file)
6702         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
6703
6704         local xsml=trusted.sml
6705         log "save $xsml on $file"
6706         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
6707
6708         local new=$(get_xattr_value $xbig $file)
6709         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
6710
6711         log "grow $xsml on $file"
6712         setfattr -n $xsml -v "$value" $file ||
6713                 error "growing $xsml on $file failed"
6714
6715         new=$(get_xattr_value $xbig $file)
6716         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
6717         log "$xbig still valid after growing $xsml"
6718
6719         rm -f $file
6720 }
6721
6722 test_102h() { # bug 15777
6723         grow_xattr 1024
6724 }
6725 run_test 102h "grow xattr from inside inode to external block"
6726
6727 test_102ha() {
6728         large_xattr_enabled || { skip "large_xattr disabled" && return; }
6729         grow_xattr $(max_xattr_size)
6730 }
6731 run_test 102ha "grow xattr from inside inode to external inode"
6732
6733 test_102i() { # bug 17038
6734         [ -z "$(which getfattr 2>/dev/null)" ] &&
6735                 skip "could not find getfattr" && return
6736         touch $DIR/$tfile
6737         ln -s $DIR/$tfile $DIR/${tfile}link
6738         getfattr -n trusted.lov $DIR/$tfile ||
6739                 error "lgetxattr on $DIR/$tfile failed"
6740         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
6741                 grep -i "no such attr" ||
6742                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
6743         rm -f $DIR/$tfile $DIR/${tfile}link
6744 }
6745 run_test 102i "lgetxattr test on symbolic link ============"
6746
6747 test_102j() {
6748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6749         TAR=$(find_lustre_tar)
6750         [ -z "$TAR" ] && skip_env "lustre-aware tar is not installed" && return
6751         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping N-stripe test" && return
6752         setup_test102 "$RUNAS"
6753         test_mkdir -p $DIR/d102j
6754         chown $RUNAS_ID $DIR/d102j
6755         $RUNAS $TAR xf $TMP/f102.tar -C $DIR/d102j --xattrs
6756         cd $DIR/d102j/$tdir
6757         compare_stripe_info1 "$RUNAS"
6758 }
6759 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
6760
6761 test_102k() {
6762         [ -z "$(which setfattr 2>/dev/null)" ] &&
6763                 skip "could not find setfattr" && return
6764         touch $DIR/$tfile
6765         # b22187 just check that does not crash for regular file.
6766         setfattr -n trusted.lov $DIR/$tfile
6767         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
6768         local test_kdir=$DIR/d102k
6769         test_mkdir $test_kdir
6770         local default_size=`$GETSTRIPE -S $test_kdir`
6771         local default_count=`$GETSTRIPE -c $test_kdir`
6772         local default_offset=`$GETSTRIPE -i $test_kdir`
6773         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
6774                 error 'dir setstripe failed'
6775         setfattr -n trusted.lov $test_kdir
6776         local stripe_size=`$GETSTRIPE -S $test_kdir`
6777         local stripe_count=`$GETSTRIPE -c $test_kdir`
6778         local stripe_offset=`$GETSTRIPE -i $test_kdir`
6779         [ $stripe_size -eq $default_size ] ||
6780                 error "stripe size $stripe_size != $default_size"
6781         [ $stripe_count -eq $default_count ] ||
6782                 error "stripe count $stripe_count != $default_count"
6783         [ $stripe_offset -eq $default_offset ] ||
6784                 error "stripe offset $stripe_offset != $default_offset"
6785         rm -rf $DIR/$tfile $test_kdir
6786 }
6787 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
6788
6789 test_102l() {
6790         [ -z "$(which getfattr 2>/dev/null)" ] &&
6791                 skip "could not find getfattr" && return
6792
6793         # LU-532 trusted. xattr is invisible to non-root
6794         local testfile=$DIR/$tfile
6795
6796         touch $testfile
6797
6798         echo "listxattr as user..."
6799         chown $RUNAS_ID $testfile
6800         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
6801             grep -q "trusted" &&
6802                 error "$testfile trusted xattrs are user visible"
6803
6804         return 0;
6805 }
6806 run_test 102l "listxattr size test =================================="
6807
6808 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
6809         local path=$DIR/$tfile
6810         touch $path
6811
6812         listxattr_size_check $path || error "listattr_size_check $path failed"
6813 }
6814 run_test 102m "Ensure listxattr fails on small bufffer ========"
6815
6816 cleanup_test102
6817
6818 getxattr() { # getxattr path name
6819         # Return the base64 encoding of the value of xattr name on path.
6820         local path=$1
6821         local name=$2
6822
6823         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
6824         # file: $path
6825         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6826         #
6827         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
6828
6829         getfattr --absolute-names --encoding=base64 --name=$name $path |
6830                 awk -F= -v name=$name '$1 == name {
6831                         print substr($0, index($0, "=") + 1);
6832         }'
6833 }
6834
6835 test_102n() { # LU-4101 mdt: protect internal xattrs
6836         local file0=$DIR/$tfile.0
6837         local file1=$DIR/$tfile.1
6838         local xattr0=$TMP/$tfile.0
6839         local xattr1=$TMP/$tfile.1
6840         local name
6841         local value
6842
6843         [ -z "$(which setfattr 2>/dev/null)" ] &&
6844                 skip "could not find setfattr" && return
6845
6846         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
6847         then
6848                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
6849                 return
6850         fi
6851
6852         rm -rf $file0 $file1 $xattr0 $xattr1
6853         touch $file0 $file1
6854
6855         # Get 'before' xattrs of $file1.
6856         getfattr --absolute-names --dump --match=- $file1 > $xattr0
6857
6858         for name in lov lma lmv link fid version som hsm lfsck_namespace; do
6859                 # Try to copy xattr from $file0 to $file1.
6860                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6861
6862                 setfattr --name=trusted.$name --value="$value" $file1 ||
6863                         error "setxattr 'trusted.$name' failed"
6864
6865                 # Try to set a garbage xattr.
6866                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6867
6868                 setfattr --name=trusted.$name --value="$value" $file1 ||
6869                         error "setxattr 'trusted.$name' failed"
6870
6871                 # Try to remove the xattr from $file1. We don't care if this
6872                 # appears to succeed or fail, we just don't want there to be
6873                 # any changes or crashes.
6874                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6875         done
6876
6877         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
6878         then
6879                 name="lfsck_ns"
6880                 # Try to copy xattr from $file0 to $file1.
6881                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
6882
6883                 setfattr --name=trusted.$name --value="$value" $file1 ||
6884                         error "setxattr 'trusted.$name' failed"
6885
6886                 # Try to set a garbage xattr.
6887                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
6888
6889                 setfattr --name=trusted.$name --value="$value" $file1 ||
6890                         error "setxattr 'trusted.$name' failed"
6891
6892                 # Try to remove the xattr from $file1. We don't care if this
6893                 # appears to succeed or fail, we just don't want there to be
6894                 # any changes or crashes.
6895                 setfattr --remove=$trusted.$name $file1 2> /dev/null
6896         fi
6897
6898         # Get 'after' xattrs of file1.
6899         getfattr --absolute-names --dump --match=- $file1 > $xattr1
6900
6901         if ! diff $xattr0 $xattr1; then
6902                 error "before and after xattrs of '$file1' differ"
6903         fi
6904
6905         rm -rf $file0 $file1 $xattr0 $xattr1
6906
6907         return 0
6908 }
6909 run_test 102n "silently ignore setxattr on internal trusted xattrs"
6910
6911 test_102p() { # LU-4703 setxattr did not check ownership
6912         local testfile=$DIR/$tfile
6913
6914         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
6915                 skip "MDS needs to be at least 2.5.56" && return
6916
6917         touch $testfile
6918
6919         echo "setfacl as user..."
6920         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
6921         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
6922
6923         echo "setfattr as user..."
6924         setfacl -m "u:$RUNAS_ID:---" $testfile
6925         $RUNAS setfattr -x system.posix_acl_access $testfile
6926         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
6927 }
6928 run_test 102p "check setxattr(2) correctly fails without permission"
6929
6930 test_102q() {
6931         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
6932 }
6933 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
6934
6935 test_102r() {
6936         touch $DIR/$tfile || error "touch"
6937         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
6938         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
6939         rm $DIR/$tfile || error "rm"
6940 }
6941 run_test 102r "set EAs with empty values"
6942
6943 run_acl_subtest()
6944 {
6945     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
6946     return $?
6947 }
6948
6949 test_103a() {
6950         [ "$UID" != 0 ] && skip_env "must run as root" && return
6951         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
6952                 skip "must have acl enabled" && return
6953         [ -z "$(which setfacl 2>/dev/null)" ] &&
6954                 skip_env "could not find setfacl" && return
6955         $GSS && skip "could not run under gss" && return
6956
6957         gpasswd -a daemon bin                           # LU-5641
6958         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
6959
6960         declare -a identity_old
6961
6962         for num in $(seq $MDSCOUNT); do
6963                 switch_identity $num true || identity_old[$num]=$?
6964         done
6965
6966         SAVE_UMASK=$(umask)
6967         umask 0022
6968         cd $DIR
6969
6970         echo "performing cp ..."
6971         run_acl_subtest cp || error "run_acl_subtest cp failed"
6972         echo "performing getfacl-noacl..."
6973         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
6974         echo "performing misc..."
6975         run_acl_subtest misc || error  "misc test failed"
6976         echo "performing permissions..."
6977         run_acl_subtest permissions || error "permissions failed"
6978         echo "performing setfacl..."
6979         run_acl_subtest setfacl || error  "setfacl test failed"
6980
6981         # inheritance test got from HP
6982         echo "performing inheritance..."
6983         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
6984         chmod +x make-tree || error "chmod +x failed"
6985         run_acl_subtest inheritance || error "inheritance test failed"
6986         rm -f make-tree
6987
6988         echo "LU-974 ignore umask when acl is enabled..."
6989         run_acl_subtest 974 || error "LU-974 umask test failed"
6990         if [ $MDSCOUNT -ge 2 ]; then
6991                 run_acl_subtest 974_remote ||
6992                         error "LU-974 umask test failed under remote dir"
6993         fi
6994
6995         echo "LU-2561 newly created file is same size as directory..."
6996         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
6997                 run_acl_subtest 2561 || error "LU-2561 test failed"
6998         else
6999                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7000         fi
7001
7002         run_acl_subtest 4924 || error "LU-4924 test failed"
7003
7004         cd $SAVE_PWD
7005         umask $SAVE_UMASK
7006
7007         for num in $(seq $MDSCOUNT); do
7008                 if [ "${identity_old[$num]}" = 1 ]; then
7009                         switch_identity $num false || identity_old[$num]=$?
7010                 fi
7011         done
7012 }
7013 run_test 103a "acl test ========================================="
7014
7015 test_103b() {
7016         local noacl=false
7017         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7018         local mountopts=$MDS_MOUNT_OPTS
7019
7020         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7021                 noacl=true
7022         else
7023                 # stop the MDT
7024                 stop $SINGLEMDS || error "failed to stop MDT."
7025                 # remount the MDT
7026                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7027                         MDS_MOUNT_OPTS="-o noacl"
7028                 else
7029                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7030                 fi
7031                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7032                         error "failed to start MDT."
7033                 MDS_MOUNT_OPTS=$mountopts
7034         fi
7035
7036         touch $DIR/$tfile
7037         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7038
7039         if ! $noacl; then
7040                 # stop the MDT
7041                 stop $SINGLEMDS || error "failed to stop MDT."
7042                 # remount the MDT
7043                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7044                         error "failed to start MDT."
7045         fi
7046
7047         true
7048 }
7049 run_test 103b "MDS mount option 'noacl'"
7050
7051 test_103c() {
7052         mkdir -p $DIR/$tdir
7053         cp -rp $DIR/$tdir $DIR/$tdir.bak
7054
7055         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7056                 error "$DIR/$tdir shouldn't contain default ACL"
7057         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7058                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7059         true
7060 }
7061 run_test 103c "'cp -rp' won't set empty acl"
7062
7063 test_104a() {
7064         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7065         touch $DIR/$tfile
7066         lfs df || error "lfs df failed"
7067         lfs df -ih || error "lfs df -ih failed"
7068         lfs df -h $DIR || error "lfs df -h $DIR failed"
7069         lfs df -i $DIR || error "lfs df -i $DIR failed"
7070         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7071         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7072
7073         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7074         lctl --device %$OSC deactivate
7075         lfs df || error "lfs df with deactivated OSC failed"
7076         lctl --device %$OSC activate
7077         # wait the osc back to normal
7078         wait_osc_import_state client ost FULL
7079
7080         lfs df || error "lfs df with reactivated OSC failed"
7081         rm -f $DIR/$tfile
7082 }
7083 run_test 104a "lfs df [-ih] [path] test ========================="
7084
7085 test_104b() {
7086         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7087         [ $RUNAS_ID -eq $UID ] &&
7088                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7089         chmod 666 /dev/obd
7090         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7091                         grep "Permission denied" | wc -l)))
7092         if [ $denied_cnt -ne 0 ]; then
7093                 error "lfs check servers test failed"
7094         fi
7095 }
7096 run_test 104b "$RUNAS lfs check servers test ===================="
7097
7098 test_105a() {
7099         # doesn't work on 2.4 kernels
7100         touch $DIR/$tfile
7101         if $(flock_is_enabled); then
7102                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7103         else
7104                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7105         fi
7106         rm -f $DIR/$tfile
7107 }
7108 run_test 105a "flock when mounted without -o flock test ========"
7109
7110 test_105b() {
7111         touch $DIR/$tfile
7112         if $(flock_is_enabled); then
7113                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7114         else
7115                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7116         fi
7117         rm -f $DIR/$tfile
7118 }
7119 run_test 105b "fcntl when mounted without -o flock test ========"
7120
7121 test_105c() {
7122         touch $DIR/$tfile
7123         if $(flock_is_enabled); then
7124                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7125         else
7126                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7127         fi
7128         rm -f $DIR/$tfile
7129 }
7130 run_test 105c "lockf when mounted without -o flock test ========"
7131
7132 test_105d() { # bug 15924
7133         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7134         test_mkdir -p $DIR/$tdir
7135         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7136         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7137         $LCTL set_param fail_loc=0x80000315
7138         flocks_test 2 $DIR/$tdir
7139 }
7140 run_test 105d "flock race (should not freeze) ========"
7141
7142 test_105e() { # bug 22660 && 22040
7143         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7144         touch $DIR/$tfile
7145         flocks_test 3 $DIR/$tfile
7146 }
7147 run_test 105e "Two conflicting flocks from same process ======="
7148
7149 test_106() { #bug 10921
7150         test_mkdir -p $DIR/$tdir
7151         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7152         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7153 }
7154 run_test 106 "attempt exec of dir followed by chown of that dir"
7155
7156 test_107() {
7157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7158         CDIR=`pwd`
7159         cd $DIR
7160
7161         local file=core
7162         rm -f $file
7163
7164         local save_pattern=$(sysctl -n kernel.core_pattern)
7165         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7166         sysctl -w kernel.core_pattern=$file
7167         sysctl -w kernel.core_uses_pid=0
7168
7169         ulimit -c unlimited
7170         sleep 60 &
7171         SLEEPPID=$!
7172
7173         sleep 1
7174
7175         kill -s 11 $SLEEPPID
7176         wait $SLEEPPID
7177         if [ -e $file ]; then
7178                 size=`stat -c%s $file`
7179                 [ $size -eq 0 ] && error "Fail to create core file $file"
7180         else
7181                 error "Fail to create core file $file"
7182         fi
7183         rm -f $file
7184         sysctl -w kernel.core_pattern=$save_pattern
7185         sysctl -w kernel.core_uses_pid=$save_uses_pid
7186         cd $CDIR
7187 }
7188 run_test 107 "Coredump on SIG"
7189
7190 test_110() {
7191         test_mkdir -p $DIR/$tdir
7192         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7193                 error "mkdir with 255 char failed"
7194         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7195                 error "mkdir with 256 char should fail, but did not"
7196         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7197                 error "create with 255 char failed"
7198         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7199                 error "create with 256 char should fail, but did not"
7200
7201         ls -l $DIR/$tdir
7202         rm -rf $DIR/$tdir
7203 }
7204 run_test 110 "filename length checking"
7205
7206 test_115() {
7207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7208         OSTIO_pre=$(ps -e | grep ll_ost_io | awk '{ print $4 }'| sort -n |
7209                 tail -1 | cut -c11-20)
7210         [ -z "$OSTIO_pre" ] && skip "no OSS threads" && return
7211         echo "Starting with $OSTIO_pre threads"
7212
7213         NUMTEST=20000
7214         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
7215         [[ $NUMFREE -lt $NUMTEST ]] && NUMTEST=$(($NUMFREE - 1000))
7216         echo "$NUMTEST creates/unlinks"
7217         test_mkdir -p $DIR/$tdir
7218         createmany -o $DIR/$tdir/$tfile $NUMTEST
7219         unlinkmany $DIR/$tdir/$tfile $NUMTEST
7220
7221         OSTIO_post=$(ps -e | grep ll_ost_io | awk '{ print $4 }' | sort -n |
7222                 tail -1 | cut -c11-20)
7223
7224         # don't return an error
7225         [ $OSTIO_post == $OSTIO_pre ] && echo \
7226             "WARNING: No new ll_ost_io threads were created ($OSTIO_pre)" &&
7227             echo "This may be fine, depending on what ran before this test" &&
7228             echo "and how fast this system is." && return
7229
7230         echo "Started with $OSTIO_pre threads, ended with $OSTIO_post"
7231 }
7232 run_test 115 "verify dynamic thread creation===================="
7233
7234 free_min_max () {
7235         wait_delete_completed
7236         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7237         echo OST kbytes available: ${AVAIL[@]}
7238         MAXI=0; MAXV=${AVAIL[0]}
7239         MINI=0; MINV=${AVAIL[0]}
7240         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7241                 #echo OST $i: ${AVAIL[i]}kb
7242                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7243                         MAXV=${AVAIL[i]}; MAXI=$i
7244                 fi
7245                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7246                         MINV=${AVAIL[i]}; MINI=$i
7247                 fi
7248         done
7249         echo Min free space: OST $MINI: $MINV
7250         echo Max free space: OST $MAXI: $MAXV
7251 }
7252
7253 test_116a() { # was previously test_116()
7254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7255         [[ $OSTCOUNT -lt 2 ]] && skip_env "$OSTCOUNT < 2 OSTs" && return
7256
7257         echo -n "Free space priority "
7258         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7259                 head -n1
7260         declare -a AVAIL
7261         free_min_max
7262
7263         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7264         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7265                 && return
7266         trap simple_cleanup_common EXIT
7267
7268
7269         # Check if we need to generate uneven OSTs
7270         test_mkdir -p $DIR/$tdir/OST${MINI}
7271         local FILL=$(($MINV / 4))
7272         local DIFF=$(($MAXV - $MINV))
7273         local DIFF2=$(($DIFF * 100 / $MINV))
7274
7275         local threshold=$(do_facet $SINGLEMDS \
7276                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7277         threshold=${threshold%%%}
7278         echo -n "Check for uneven OSTs: "
7279         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7280
7281         if [[ $DIFF2 -gt $threshold ]]; then
7282                 echo "ok"
7283                 echo "Don't need to fill OST$MINI"
7284         else
7285                 # generate uneven OSTs. Write 2% over the QOS threshold value
7286                 echo "no"
7287                 DIFF=$(($threshold - $DIFF2 + 2))
7288                 DIFF2=$(( ($MINV * $DIFF)/100 ))
7289                 echo "Fill ${DIFF}% remaining space in OST${MINI} with ${DIFF2}KB"
7290                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7291                         error "setstripe failed"
7292                 DIFF=$(($DIFF2 / 2048))
7293                 i=0
7294                 while [ $i -lt $DIFF ]; do
7295                         i=$(($i + 1))
7296                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7297                                 bs=2M count=1 2>/dev/null
7298                         echo -n .
7299                 done
7300                 echo .
7301                 sync
7302                 sleep_maxage
7303                 free_min_max
7304         fi
7305
7306         DIFF=$(($MAXV - $MINV))
7307         DIFF2=$(($DIFF * 100 / $MINV))
7308         echo -n "diff=${DIFF}=${DIFF2}% must be > ${threshold}% for QOS mode..."
7309         if [[ $DIFF2 -gt $threshold ]]; then
7310                 echo "ok"
7311         else
7312                 echo "failed - QOS mode won't be used"
7313                 skip "QOS imbalance criteria not met"
7314                 simple_cleanup_common
7315                 return
7316         fi
7317
7318         MINI1=$MINI; MINV1=$MINV
7319         MAXI1=$MAXI; MAXV1=$MAXV
7320
7321         # now fill using QOS
7322         $SETSTRIPE -c 1 $DIR/$tdir
7323         FILL=$(($FILL / 200))
7324         if [ $FILL -gt 600 ]; then
7325                 FILL=600
7326         fi
7327         echo "writing $FILL files to QOS-assigned OSTs"
7328         i=0
7329         while [ $i -lt $FILL ]; do
7330                 i=$((i + 1))
7331                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
7332                         count=1 2>/dev/null
7333                 echo -n .
7334         done
7335         echo "wrote $i 200k files"
7336         sync
7337         sleep_maxage
7338
7339         echo "Note: free space may not be updated, so measurements might be off"
7340         free_min_max
7341         DIFF2=$(($MAXV - $MINV))
7342         echo "free space delta: orig $DIFF final $DIFF2"
7343         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
7344         DIFF=$(($MINV1 - ${AVAIL[$MINI1]}))
7345         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
7346         DIFF2=$(($MAXV1 - ${AVAIL[$MAXI1]}))
7347         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
7348         FILL=$(($DIFF2 * 100 / $DIFF - 100))
7349         [ $DIFF -gt 0 ] &&
7350                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
7351
7352         # Figure out which files were written where
7353         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7354                   awk '/'$MINI1': / {print $2; exit}')
7355         echo $UUID
7356         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7357         echo "$MINC files created on smaller OST $MINI1"
7358         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
7359                   awk '/'$MAXI1': / {print $2; exit}')
7360         echo $UUID
7361         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
7362         echo "$MAXC files created on larger OST $MAXI1"
7363         FILL=$(($MAXC * 100 / $MINC - 100))
7364         [[ $MINC -gt 0 ]] &&
7365                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
7366         [[ $MAXC -gt $MINC ]] ||
7367                 error_ignore LU-9 "stripe QOS didn't balance free space"
7368         simple_cleanup_common
7369 }
7370 run_test 116a "stripe QOS: free space balance ==================="
7371
7372 test_116b() { # LU-2093
7373         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7374 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
7375         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
7376                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
7377         [ -z "$old_rr" ] && skip "no QOS" && return 0
7378         do_facet $SINGLEMDS lctl set_param \
7379                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
7380         mkdir -p $DIR/$tdir
7381         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
7382         createmany -o $DIR/$tdir/f- 20 || error "can't create"
7383         do_facet $SINGLEMDS lctl set_param fail_loc=0
7384         rm -rf $DIR/$tdir
7385         do_facet $SINGLEMDS lctl set_param \
7386                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
7387 }
7388 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
7389
7390 test_117() # bug 10891
7391 {
7392         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7393         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
7394         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
7395         lctl set_param fail_loc=0x21e
7396         > $DIR/$tfile || error "truncate failed"
7397         lctl set_param fail_loc=0
7398         echo "Truncate succeeded."
7399         rm -f $DIR/$tfile
7400 }
7401 run_test 117 "verify osd extend =========="
7402
7403 NO_SLOW_RESENDCOUNT=4
7404 export OLD_RESENDCOUNT=""
7405 set_resend_count () {
7406         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
7407         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
7408         lctl set_param -n $PROC_RESENDCOUNT $1
7409         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
7410 }
7411
7412 # for reduce test_118* time (b=14842)
7413 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7414
7415 # Reset async IO behavior after error case
7416 reset_async() {
7417         FILE=$DIR/reset_async
7418
7419         # Ensure all OSCs are cleared
7420         $SETSTRIPE -c -1 $FILE
7421         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
7422         sync
7423         rm $FILE
7424 }
7425
7426 test_118a() #bug 11710
7427 {
7428         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7429         reset_async
7430
7431         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7432         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7433         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7434
7435         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7436                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7437                 return 1;
7438         fi
7439         rm -f $DIR/$tfile
7440 }
7441 run_test 118a "verify O_SYNC works =========="
7442
7443 test_118b()
7444 {
7445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7446         remote_ost_nodsh && skip "remote OST with nodsh" && return
7447
7448         reset_async
7449
7450         #define OBD_FAIL_OST_ENOENT 0x217
7451         set_nodes_failloc "$(osts_nodes)" 0x217
7452         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7453         RC=$?
7454         set_nodes_failloc "$(osts_nodes)" 0
7455         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7456         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7457                     grep -c writeback)
7458
7459         if [[ $RC -eq 0 ]]; then
7460                 error "Must return error due to dropped pages, rc=$RC"
7461                 return 1;
7462         fi
7463
7464         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7465                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7466                 return 1;
7467         fi
7468
7469         echo "Dirty pages not leaked on ENOENT"
7470
7471         # Due to the above error the OSC will issue all RPCs syncronously
7472         # until a subsequent RPC completes successfully without error.
7473         $MULTIOP $DIR/$tfile Ow4096yc
7474         rm -f $DIR/$tfile
7475
7476         return 0
7477 }
7478 run_test 118b "Reclaim dirty pages on fatal error =========="
7479
7480 test_118c()
7481 {
7482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7483
7484         # for 118c, restore the original resend count, LU-1940
7485         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
7486                                 set_resend_count $OLD_RESENDCOUNT
7487         remote_ost_nodsh && skip "remote OST with nodsh" && return
7488
7489         reset_async
7490
7491         #define OBD_FAIL_OST_EROFS               0x216
7492         set_nodes_failloc "$(osts_nodes)" 0x216
7493
7494         # multiop should block due to fsync until pages are written
7495         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7496         MULTIPID=$!
7497         sleep 1
7498
7499         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7500                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7501         fi
7502
7503         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7504                     grep -c writeback)
7505         if [[ $WRITEBACK -eq 0 ]]; then
7506                 error "No page in writeback, writeback=$WRITEBACK"
7507         fi
7508
7509         set_nodes_failloc "$(osts_nodes)" 0
7510         wait $MULTIPID
7511         RC=$?
7512         if [[ $RC -ne 0 ]]; then
7513                 error "Multiop fsync failed, rc=$RC"
7514         fi
7515
7516         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7517         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7518                     grep -c writeback)
7519         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7520                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7521         fi
7522
7523         rm -f $DIR/$tfile
7524         echo "Dirty pages flushed via fsync on EROFS"
7525         return 0
7526 }
7527 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
7528
7529 # continue to use small resend count to reduce test_118* time (b=14842)
7530 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
7531
7532 test_118d()
7533 {
7534         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7535         remote_ost_nodsh && skip "remote OST with nodsh" && return
7536
7537         reset_async
7538
7539         #define OBD_FAIL_OST_BRW_PAUSE_BULK
7540         set_nodes_failloc "$(osts_nodes)" 0x214
7541         # multiop should block due to fsync until pages are written
7542         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7543         MULTIPID=$!
7544         sleep 1
7545
7546         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
7547                 error "Multiop failed to block on fsync, pid=$MULTIPID"
7548         fi
7549
7550         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7551                     grep -c writeback)
7552         if [[ $WRITEBACK -eq 0 ]]; then
7553                 error "No page in writeback, writeback=$WRITEBACK"
7554         fi
7555
7556         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
7557         set_nodes_failloc "$(osts_nodes)" 0
7558
7559         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7560         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7561                     grep -c writeback)
7562         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7563                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7564         fi
7565
7566         rm -f $DIR/$tfile
7567         echo "Dirty pages gaurenteed flushed via fsync"
7568         return 0
7569 }
7570 run_test 118d "Fsync validation inject a delay of the bulk =========="
7571
7572 test_118f() {
7573         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7574         reset_async
7575
7576         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
7577         lctl set_param fail_loc=0x8000040a
7578
7579         # Should simulate EINVAL error which is fatal
7580         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7581         RC=$?
7582         if [[ $RC -eq 0 ]]; then
7583                 error "Must return error due to dropped pages, rc=$RC"
7584         fi
7585
7586         lctl set_param fail_loc=0x0
7587
7588         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7589         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7590         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7591                     grep -c writeback)
7592         if [[ $LOCKED -ne 0 ]]; then
7593                 error "Locked pages remain in cache, locked=$LOCKED"
7594         fi
7595
7596         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7597                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7598         fi
7599
7600         rm -f $DIR/$tfile
7601         echo "No pages locked after fsync"
7602
7603         reset_async
7604         return 0
7605 }
7606 run_test 118f "Simulate unrecoverable OSC side error =========="
7607
7608 test_118g() {
7609         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7610         reset_async
7611
7612         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7613         lctl set_param fail_loc=0x406
7614
7615         # simulate local -ENOMEM
7616         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7617         RC=$?
7618
7619         lctl set_param fail_loc=0
7620         if [[ $RC -eq 0 ]]; then
7621                 error "Must return error due to dropped pages, rc=$RC"
7622         fi
7623
7624         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7625         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7626         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7627                         grep -c writeback)
7628         if [[ $LOCKED -ne 0 ]]; then
7629                 error "Locked pages remain in cache, locked=$LOCKED"
7630         fi
7631
7632         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7633                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7634         fi
7635
7636         rm -f $DIR/$tfile
7637         echo "No pages locked after fsync"
7638
7639         reset_async
7640         return 0
7641 }
7642 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
7643
7644 test_118h() {
7645         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7646         remote_ost_nodsh && skip "remote OST with nodsh" && return
7647
7648         reset_async
7649
7650         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7651         set_nodes_failloc "$(osts_nodes)" 0x20e
7652         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7653         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7654         RC=$?
7655
7656         set_nodes_failloc "$(osts_nodes)" 0
7657         if [[ $RC -eq 0 ]]; then
7658                 error "Must return error due to dropped pages, rc=$RC"
7659         fi
7660
7661         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7662         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7663         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
7664                     grep -c writeback)
7665         if [[ $LOCKED -ne 0 ]]; then
7666                 error "Locked pages remain in cache, locked=$LOCKED"
7667         fi
7668
7669         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7670                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7671         fi
7672
7673         rm -f $DIR/$tfile
7674         echo "No pages locked after fsync"
7675
7676         return 0
7677 }
7678 run_test 118h "Verify timeout in handling recoverables errors  =========="
7679
7680 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7681
7682 test_118i() {
7683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7684         remote_ost_nodsh && skip "remote OST with nodsh" && return
7685
7686         reset_async
7687
7688         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7689         set_nodes_failloc "$(osts_nodes)" 0x20e
7690
7691         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
7692         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
7693         PID=$!
7694         sleep 5
7695         set_nodes_failloc "$(osts_nodes)" 0
7696
7697         wait $PID
7698         RC=$?
7699         if [[ $RC -ne 0 ]]; then
7700                 error "got error, but should be not, rc=$RC"
7701         fi
7702
7703         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7704         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7705         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7706         if [[ $LOCKED -ne 0 ]]; then
7707                 error "Locked pages remain in cache, locked=$LOCKED"
7708         fi
7709
7710         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7711                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7712         fi
7713
7714         rm -f $DIR/$tfile
7715         echo "No pages locked after fsync"
7716
7717         return 0
7718 }
7719 run_test 118i "Fix error before timeout in recoverable error  =========="
7720
7721 [ "$SLOW" = "no" ] && set_resend_count 4
7722
7723 test_118j() {
7724         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7725         remote_ost_nodsh && skip "remote OST with nodsh" && return
7726
7727         reset_async
7728
7729         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
7730         set_nodes_failloc "$(osts_nodes)" 0x220
7731
7732         # return -EIO from OST
7733         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7734         RC=$?
7735         set_nodes_failloc "$(osts_nodes)" 0x0
7736         if [[ $RC -eq 0 ]]; then
7737                 error "Must return error due to dropped pages, rc=$RC"
7738         fi
7739
7740         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
7741         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
7742         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
7743         if [[ $LOCKED -ne 0 ]]; then
7744                 error "Locked pages remain in cache, locked=$LOCKED"
7745         fi
7746
7747         # in recoverable error on OST we want resend and stay until it finished
7748         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
7749                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
7750         fi
7751
7752         rm -f $DIR/$tfile
7753         echo "No pages locked after fsync"
7754
7755         return 0
7756 }
7757 run_test 118j "Simulate unrecoverable OST side error =========="
7758
7759 test_118k()
7760 {
7761         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7762         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
7763
7764         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
7765         set_nodes_failloc "$(osts_nodes)" 0x20e
7766         test_mkdir -p $DIR/$tdir
7767
7768         for ((i=0;i<10;i++)); do
7769                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
7770                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
7771                 SLEEPPID=$!
7772                 sleep 0.500s
7773                 kill $SLEEPPID
7774                 wait $SLEEPPID
7775         done
7776
7777         set_nodes_failloc "$(osts_nodes)" 0
7778         rm -rf $DIR/$tdir
7779 }
7780 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
7781
7782 test_118l()
7783 {
7784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7785         # LU-646
7786         test_mkdir -p $DIR/$tdir
7787         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
7788         rm -rf $DIR/$tdir
7789 }
7790 run_test 118l "fsync dir ========="
7791
7792 test_118m() # LU-3066
7793 {
7794         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7795         test_mkdir -p $DIR/$tdir
7796         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
7797         rm -rf $DIR/$tdir
7798 }
7799 run_test 118m "fdatasync dir ========="
7800
7801 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
7802
7803 test_119a() # bug 11737
7804 {
7805         BSIZE=$((512 * 1024))
7806         directio write $DIR/$tfile 0 1 $BSIZE
7807         # We ask to read two blocks, which is more than a file size.
7808         # directio will indicate an error when requested and actual
7809         # sizes aren't equeal (a normal situation in this case) and
7810         # print actual read amount.
7811         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
7812         if [ "$NOB" != "$BSIZE" ]; then
7813                 error "read $NOB bytes instead of $BSIZE"
7814         fi
7815         rm -f $DIR/$tfile
7816 }
7817 run_test 119a "Short directIO read must return actual read amount"
7818
7819 test_119b() # bug 11737
7820 {
7821         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping 2-stripe test" && return
7822
7823         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
7824         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
7825         sync
7826         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
7827                 error "direct read failed"
7828         rm -f $DIR/$tfile
7829 }
7830 run_test 119b "Sparse directIO read must return actual read amount"
7831
7832 test_119c() # bug 13099
7833 {
7834         BSIZE=1048576
7835         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
7836         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
7837         rm -f $DIR/$tfile
7838 }
7839 run_test 119c "Testing for direct read hitting hole"
7840
7841 test_119d() # bug 15950
7842 {
7843         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7844         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
7845         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
7846         BSIZE=1048576
7847         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
7848         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
7849         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
7850         lctl set_param fail_loc=0x40d
7851         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
7852         pid_dio=$!
7853         sleep 1
7854         cat $DIR/$tfile > /dev/null &
7855         lctl set_param fail_loc=0
7856         pid_reads=$!
7857         wait $pid_dio
7858         log "the DIO writes have completed, now wait for the reads (should not block very long)"
7859         sleep 2
7860         [ -n "`ps h -p $pid_reads -o comm`" ] && \
7861         error "the read rpcs have not completed in 2s"
7862         rm -f $DIR/$tfile
7863         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
7864 }
7865 run_test 119d "The DIO path should try to send a new rpc once one is completed"
7866
7867 test_120a() {
7868         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7869         test_mkdir -p $DIR/$tdir
7870         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7871                skip "no early lock cancel on server" && return 0
7872
7873         lru_resize_disable mdc
7874         lru_resize_disable osc
7875         cancel_lru_locks mdc
7876         # asynchronous object destroy at MDT could cause bl ast to client
7877         cancel_lru_locks osc
7878
7879         stat $DIR/$tdir > /dev/null
7880         can1=$(do_facet $SINGLEMDS \
7881                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7882                awk '/ldlm_cancel/ {print $2}')
7883         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7884                awk '/ldlm_bl_callback/ {print $2}')
7885         test_mkdir -c1 $DIR/$tdir/d1
7886         can2=$(do_facet $SINGLEMDS \
7887                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7888                awk '/ldlm_cancel/ {print $2}')
7889         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7890                awk '/ldlm_bl_callback/ {print $2}')
7891         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7892         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7893         lru_resize_enable mdc
7894         lru_resize_enable osc
7895 }
7896 run_test 120a "Early Lock Cancel: mkdir test"
7897
7898 test_120b() {
7899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7900         test_mkdir $DIR/$tdir
7901         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7902                skip "no early lock cancel on server" && return 0
7903         lru_resize_disable mdc
7904         lru_resize_disable osc
7905         cancel_lru_locks mdc
7906         stat $DIR/$tdir > /dev/null
7907         can1=$(do_facet $SINGLEMDS \
7908                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7909                awk '/ldlm_cancel/ {print $2}')
7910         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7911                awk '/ldlm_bl_callback/ {print $2}')
7912         touch $DIR/$tdir/f1
7913         can2=$(do_facet $SINGLEMDS \
7914                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7915                awk '/ldlm_cancel/ {print $2}')
7916         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7917                awk '/ldlm_bl_callback/ {print $2}')
7918         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7919         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7920         lru_resize_enable mdc
7921         lru_resize_enable osc
7922 }
7923 run_test 120b "Early Lock Cancel: create test"
7924
7925 test_120c() {
7926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7927         test_mkdir -c1 $DIR/$tdir
7928         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7929                skip "no early lock cancel on server" && return 0
7930         lru_resize_disable mdc
7931         lru_resize_disable osc
7932         test_mkdir -p -c1 $DIR/$tdir/d1
7933         test_mkdir -p -c1 $DIR/$tdir/d2
7934         touch $DIR/$tdir/d1/f1
7935         cancel_lru_locks mdc
7936         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
7937         can1=$(do_facet $SINGLEMDS \
7938                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7939                awk '/ldlm_cancel/ {print $2}')
7940         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7941                awk '/ldlm_bl_callback/ {print $2}')
7942         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
7943         can2=$(do_facet $SINGLEMDS \
7944                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7945                awk '/ldlm_cancel/ {print $2}')
7946         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7947                awk '/ldlm_bl_callback/ {print $2}')
7948         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7949         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7950         lru_resize_enable mdc
7951         lru_resize_enable osc
7952 }
7953 run_test 120c "Early Lock Cancel: link test"
7954
7955 test_120d() {
7956         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7957         test_mkdir -p -c1 $DIR/$tdir
7958         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
7959                skip "no early lock cancel on server" && return 0
7960         lru_resize_disable mdc
7961         lru_resize_disable osc
7962         touch $DIR/$tdir
7963         cancel_lru_locks mdc
7964         stat $DIR/$tdir > /dev/null
7965         can1=$(do_facet $SINGLEMDS \
7966                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7967                awk '/ldlm_cancel/ {print $2}')
7968         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7969                awk '/ldlm_bl_callback/ {print $2}')
7970         chmod a+x $DIR/$tdir
7971         can2=$(do_facet $SINGLEMDS \
7972                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
7973                awk '/ldlm_cancel/ {print $2}')
7974         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
7975                awk '/ldlm_bl_callback/ {print $2}')
7976         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
7977         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
7978         lru_resize_enable mdc
7979         lru_resize_enable osc
7980 }
7981 run_test 120d "Early Lock Cancel: setattr test"
7982
7983 test_120e() {
7984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7985         test_mkdir -p -c1 $DIR/$tdir
7986         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
7987                skip "no early lock cancel on server" && return 0
7988         lru_resize_disable mdc
7989         lru_resize_disable osc
7990         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
7991         cancel_lru_locks mdc
7992         cancel_lru_locks osc
7993         dd if=$DIR/$tdir/f1 of=/dev/null
7994         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
7995         # XXX client can not do early lock cancel of OST lock
7996         # during unlink (LU-4206), so cancel osc lock now.
7997         cancel_lru_locks osc
7998         can1=$(do_facet $SINGLEMDS \
7999                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8000                awk '/ldlm_cancel/ {print $2}')
8001         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8002                awk '/ldlm_bl_callback/ {print $2}')
8003         unlink $DIR/$tdir/f1
8004         sleep 5
8005         can2=$(do_facet $SINGLEMDS \
8006                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8007                awk '/ldlm_cancel/ {print $2}')
8008         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8009                awk '/ldlm_bl_callback/ {print $2}')
8010         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8011         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8012         lru_resize_enable mdc
8013         lru_resize_enable osc
8014 }
8015 run_test 120e "Early Lock Cancel: unlink test"
8016
8017 test_120f() {
8018         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8019         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8020                skip "no early lock cancel on server" && return 0
8021         test_mkdir -p -c1 $DIR/$tdir
8022         lru_resize_disable mdc
8023         lru_resize_disable osc
8024         test_mkdir -p -c1 $DIR/$tdir/d1
8025         test_mkdir -p -c1 $DIR/$tdir/d2
8026         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8027         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8028         cancel_lru_locks mdc
8029         cancel_lru_locks osc
8030         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8031         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8032         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8033         # XXX client can not do early lock cancel of OST lock
8034         # during rename (LU-4206), so cancel osc lock now.
8035         cancel_lru_locks osc
8036         can1=$(do_facet $SINGLEMDS \
8037                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8038                awk '/ldlm_cancel/ {print $2}')
8039         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8040                awk '/ldlm_bl_callback/ {print $2}')
8041         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8042         sleep 5
8043         can2=$(do_facet $SINGLEMDS \
8044                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8045                awk '/ldlm_cancel/ {print $2}')
8046         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8047                awk '/ldlm_bl_callback/ {print $2}')
8048         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8049         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8050         lru_resize_enable mdc
8051         lru_resize_enable osc
8052 }
8053 run_test 120f "Early Lock Cancel: rename test"
8054
8055 test_120g() {
8056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8057         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8058                skip "no early lock cancel on server" && return 0
8059         lru_resize_disable mdc
8060         lru_resize_disable osc
8061         count=10000
8062         echo create $count files
8063         test_mkdir -p $DIR/$tdir
8064         cancel_lru_locks mdc
8065         cancel_lru_locks osc
8066         t0=`date +%s`
8067
8068         can0=$(do_facet $SINGLEMDS \
8069                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8070                awk '/ldlm_cancel/ {print $2}')
8071         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8072                awk '/ldlm_bl_callback/ {print $2}')
8073         createmany -o $DIR/$tdir/f $count
8074         sync
8075         can1=$(do_facet $SINGLEMDS \
8076                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8077                awk '/ldlm_cancel/ {print $2}')
8078         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8079                awk '/ldlm_bl_callback/ {print $2}')
8080         t1=$(date +%s)
8081         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8082         echo rm $count files
8083         rm -r $DIR/$tdir
8084         sync
8085         can2=$(do_facet $SINGLEMDS \
8086                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8087                awk '/ldlm_cancel/ {print $2}')
8088         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8089                awk '/ldlm_bl_callback/ {print $2}')
8090         t2=$(date +%s)
8091         echo total: $count removes in $((t2-t1))
8092         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8093         sleep 2
8094         # wait for commitment of removal
8095         lru_resize_enable mdc
8096         lru_resize_enable osc
8097 }
8098 run_test 120g "Early Lock Cancel: performance test"
8099
8100 test_121() { #bug #10589
8101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8102         rm -rf $DIR/$tfile
8103         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8104 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8105         lctl set_param fail_loc=0x310
8106         cancel_lru_locks osc > /dev/null
8107         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8108         lctl set_param fail_loc=0
8109         [[ $reads -eq $writes ]] ||
8110                 error "read $reads blocks, must be $writes blocks"
8111 }
8112 run_test 121 "read cancel race ========="
8113
8114 test_123a() { # was test 123, statahead(bug 11401)
8115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8116         SLOWOK=0
8117         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8118             log "testing on UP system. Performance may be not as good as expected."
8119                         SLOWOK=1
8120         fi
8121
8122         rm -rf $DIR/$tdir
8123         test_mkdir -p $DIR/$tdir
8124         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8125         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8126         MULT=10
8127         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8128                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8129
8130                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8131                 lctl set_param -n llite.*.statahead_max 0
8132                 lctl get_param llite.*.statahead_max
8133                 cancel_lru_locks mdc
8134                 cancel_lru_locks osc
8135                 stime=`date +%s`
8136                 time ls -l $DIR/$tdir | wc -l
8137                 etime=`date +%s`
8138                 delta=$((etime - stime))
8139                 log "ls $i files without statahead: $delta sec"
8140                 lctl set_param llite.*.statahead_max=$max
8141
8142                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8143                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8144                 cancel_lru_locks mdc
8145                 cancel_lru_locks osc
8146                 stime=`date +%s`
8147                 time ls -l $DIR/$tdir | wc -l
8148                 etime=`date +%s`
8149                 delta_sa=$((etime - stime))
8150                 log "ls $i files with statahead: $delta_sa sec"
8151                 lctl get_param -n llite.*.statahead_stats
8152                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8153
8154                 [[ $swrong -lt $ewrong ]] &&
8155                         log "statahead was stopped, maybe too many locks held!"
8156                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8157
8158                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8159                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8160                     lctl set_param -n llite.*.statahead_max 0
8161                     lctl get_param llite.*.statahead_max
8162                     cancel_lru_locks mdc
8163                     cancel_lru_locks osc
8164                     stime=`date +%s`
8165                     time ls -l $DIR/$tdir | wc -l
8166                     etime=`date +%s`
8167                     delta=$((etime - stime))
8168                     log "ls $i files again without statahead: $delta sec"
8169                     lctl set_param llite.*.statahead_max=$max
8170                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8171                         if [  $SLOWOK -eq 0 ]; then
8172                                 error "ls $i files is slower with statahead!"
8173                         else
8174                                 log "ls $i files is slower with statahead!"
8175                         fi
8176                         break
8177                     fi
8178                 fi
8179
8180                 [ $delta -gt 20 ] && break
8181                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8182                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8183         done
8184         log "ls done"
8185
8186         stime=`date +%s`
8187         rm -r $DIR/$tdir
8188         sync
8189         etime=`date +%s`
8190         delta=$((etime - stime))
8191         log "rm -r $DIR/$tdir/: $delta seconds"
8192         log "rm done"
8193         lctl get_param -n llite.*.statahead_stats
8194 }
8195 run_test 123a "verify statahead work"
8196
8197 test_123b () { # statahead(bug 15027)
8198         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8199         test_mkdir -p $DIR/$tdir
8200         createmany -o $DIR/$tdir/$tfile-%d 1000
8201
8202         cancel_lru_locks mdc
8203         cancel_lru_locks osc
8204
8205 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8206         lctl set_param fail_loc=0x80000803
8207         ls -lR $DIR/$tdir > /dev/null
8208         log "ls done"
8209         lctl set_param fail_loc=0x0
8210         lctl get_param -n llite.*.statahead_stats
8211         rm -r $DIR/$tdir
8212         sync
8213
8214 }
8215 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8216
8217 test_124a() {
8218         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8219         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8220                 skip "no lru resize on server" && return 0
8221         local NR=2000
8222         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
8223
8224         log "create $NR files at $DIR/$tdir"
8225         createmany -o $DIR/$tdir/f $NR ||
8226                 error "failed to create $NR files in $DIR/$tdir"
8227
8228         cancel_lru_locks mdc
8229         ls -l $DIR/$tdir > /dev/null
8230
8231         local NSDIR=""
8232         local LRU_SIZE=0
8233         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8234                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8235                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8236                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8237                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8238                         log "NSDIR=$NSDIR"
8239                         log "NS=$(basename $NSDIR)"
8240                         break
8241                 fi
8242         done
8243
8244         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8245                 skip "Not enough cached locks created!"
8246                 return 0
8247         fi
8248         log "LRU=$LRU_SIZE"
8249
8250         local SLEEP=30
8251
8252         # We know that lru resize allows one client to hold $LIMIT locks
8253         # for 10h. After that locks begin to be killed by client.
8254         local MAX_HRS=10
8255         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8256         log "LIMIT=$LIMIT"
8257         if [ $LIMIT -lt $LRU_SIZE ]; then
8258             skip "Limit is too small $LIMIT"
8259             return 0
8260         fi
8261
8262         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8263         # killing locks. Some time was spent for creating locks. This means
8264         # that up to the moment of sleep finish we must have killed some of
8265         # them (10-100 locks). This depends on how fast ther were created.
8266         # Many of them were touched in almost the same moment and thus will
8267         # be killed in groups.
8268         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8269
8270         # Use $LRU_SIZE_B here to take into account real number of locks
8271         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8272         local LRU_SIZE_B=$LRU_SIZE
8273         log "LVF=$LVF"
8274         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8275         log "OLD_LVF=$OLD_LVF"
8276         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8277
8278         # Let's make sure that we really have some margin. Client checks
8279         # cached locks every 10 sec.
8280         SLEEP=$((SLEEP+20))
8281         log "Sleep ${SLEEP} sec"
8282         local SEC=0
8283         while ((SEC<$SLEEP)); do
8284                 echo -n "..."
8285                 sleep 5
8286                 SEC=$((SEC+5))
8287                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
8288                 echo -n "$LRU_SIZE"
8289         done
8290         echo ""
8291         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
8292         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
8293
8294         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
8295                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
8296                 unlinkmany $DIR/$tdir/f $NR
8297                 return
8298         }
8299
8300         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
8301         log "unlink $NR files at $DIR/$tdir"
8302         unlinkmany $DIR/$tdir/f $NR
8303 }
8304 run_test 124a "lru resize ======================================="
8305
8306 get_max_pool_limit()
8307 {
8308         local limit=$($LCTL get_param \
8309                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
8310         local max=0
8311         for l in $limit; do
8312                 if [[ $l -gt $max ]]; then
8313                         max=$l
8314                 fi
8315         done
8316         echo $max
8317 }
8318
8319 test_124b() {
8320         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8321         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
8322                 skip "no lru resize on server" && return 0
8323
8324         LIMIT=$(get_max_pool_limit)
8325
8326         NR=$(($(default_lru_size)*20))
8327         if [[ $NR -gt $LIMIT ]]; then
8328                 log "Limit lock number by $LIMIT locks"
8329                 NR=$LIMIT
8330         fi
8331         lru_resize_disable mdc
8332         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
8333                 error "failed to create $DIR/$tdir/disable_lru_resize"
8334
8335         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
8336         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
8337         cancel_lru_locks mdc
8338         stime=`date +%s`
8339         PID=""
8340         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8341         PID="$PID $!"
8342         sleep 2
8343         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8344         PID="$PID $!"
8345         sleep 2
8346         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
8347         PID="$PID $!"
8348         wait $PID
8349         etime=`date +%s`
8350         nolruresize_delta=$((etime-stime))
8351         log "ls -la time: $nolruresize_delta seconds"
8352         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8353         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
8354
8355         lru_resize_enable mdc
8356         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
8357                 error "failed to create $DIR/$tdir/enable_lru_resize"
8358
8359         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
8360         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
8361         cancel_lru_locks mdc
8362         stime=`date +%s`
8363         PID=""
8364         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8365         PID="$PID $!"
8366         sleep 2
8367         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8368         PID="$PID $!"
8369         sleep 2
8370         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
8371         PID="$PID $!"
8372         wait $PID
8373         etime=`date +%s`
8374         lruresize_delta=$((etime-stime))
8375         log "ls -la time: $lruresize_delta seconds"
8376         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
8377
8378         if [ $lruresize_delta -gt $nolruresize_delta ]; then
8379                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
8380         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
8381                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
8382         else
8383                 log "lru resize performs the same with no lru resize"
8384         fi
8385         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
8386 }
8387 run_test 124b "lru resize (performance test) ======================="
8388
8389 test_125() { # 13358
8390         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8391         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
8392         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
8393         test_mkdir -p $DIR/d125 || error "mkdir failed"
8394         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
8395         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
8396         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
8397 }
8398 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
8399
8400 test_126() { # bug 12829/13455
8401         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
8402         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
8403         $GSS && skip "must run as gss disabled" && return
8404
8405         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
8406         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
8407         rm -f $DIR/$tfile
8408         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
8409 }
8410 run_test 126 "check that the fsgid provided by the client is taken into account"
8411
8412 test_127a() { # bug 15521
8413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8414         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
8415         $LCTL set_param osc.*.stats=0
8416         FSIZE=$((2048 * 1024))
8417         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8418         cancel_lru_locks osc
8419         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
8420
8421         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
8422         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8423                 echo "got $COUNT $NAME"
8424                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
8425                 eval $NAME=$COUNT || error "Wrong proc format"
8426
8427                 case $NAME in
8428                         read_bytes|write_bytes)
8429                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
8430                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
8431                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
8432                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
8433                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
8434                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
8435                                 error "sumsquare is too small: $SUMSQ"
8436                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
8437                                 error "sumsquare is too big: $SUMSQ"
8438                         ;;
8439                         *) ;;
8440                 esac
8441         done < $DIR/${tfile}.tmp
8442
8443         #check that we actually got some stats
8444         [ "$read_bytes" ] || error "Missing read_bytes stats"
8445         [ "$write_bytes" ] || error "Missing write_bytes stats"
8446         [ "$read_bytes" != 0 ] || error "no read done"
8447         [ "$write_bytes" != 0 ] || error "no write done"
8448 }
8449 run_test 127a "verify the client stats are sane"
8450
8451 test_127b() { # bug LU-333
8452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8453         $LCTL set_param llite.*.stats=0
8454         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
8455         # perform 2 reads and writes so MAX is different from SUM.
8456         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8457         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
8458         cancel_lru_locks osc
8459         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8460         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
8461
8462         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
8463         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
8464                 echo "got $COUNT $NAME"
8465                 eval $NAME=$COUNT || error "Wrong proc format"
8466
8467         case $NAME in
8468                 read_bytes)
8469                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8470                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8471                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8472                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8473                         ;;
8474                 write_bytes)
8475                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
8476                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
8477                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
8478                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
8479                         ;;
8480                         *) ;;
8481                 esac
8482         done < $TMP/${tfile}.tmp
8483
8484         #check that we actually got some stats
8485         [ "$read_bytes" ] || error "Missing read_bytes stats"
8486         [ "$write_bytes" ] || error "Missing write_bytes stats"
8487         [ "$read_bytes" != 0 ] || error "no read done"
8488         [ "$write_bytes" != 0 ] || error "no write done"
8489 }
8490 run_test 127b "verify the llite client stats are sane"
8491
8492 test_128() { # bug 15212
8493         touch $DIR/$tfile
8494         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
8495                 find $DIR/$tfile
8496                 find $DIR/$tfile
8497         EOF
8498
8499         result=$(grep error $TMP/$tfile.log)
8500         rm -f $DIR/$tfile
8501         [ -z "$result" ] || error "consecutive find's under interactive lfs failed"
8502 }
8503 run_test 128 "interactive lfs for 2 consecutive find's"
8504
8505 set_dir_limits () {
8506         local mntdev
8507         local canondev
8508         local node
8509
8510         local LDPROC=/proc/fs/ldiskfs
8511         local facets=$(get_facets MDS)
8512
8513         for facet in ${facets//,/ }; do
8514                 canondev=$(ldiskfs_canon \
8515                            *.$(convert_facet2label $facet).mntdev $facet)
8516                 do_facet $facet "test -e $LDPROC/$canondev/max_dir_size" ||
8517                                                 LDPROC=/sys/fs/ldiskfs
8518                 do_facet $facet "echo $1 >$LDPROC/$canondev/max_dir_size"
8519         done
8520 }
8521
8522 test_129() {
8523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8524         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
8525                 skip "Only applicable to ldiskfs-based MDTs"
8526                 return
8527         fi
8528         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8529         ENOSPC=28
8530         EFBIG=27
8531
8532         rm -rf $DIR/$tdir
8533         test_mkdir -p $DIR/$tdir
8534
8535         # block size of mds1
8536         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
8537         local MDSBLOCKSIZE=$($LCTL get_param -n mdc.*MDT0000*.blocksize)
8538         local MAX=$((MDSBLOCKSIZE * 3))
8539         set_dir_limits $MAX
8540         local I=$(stat -c%s "$DIR/$tdir")
8541         local J=0
8542         local STRIPE_COUNT=1
8543         [[ $MDSCOUNT -ge 2 ]] && STRIPE_COUNT=$($LFS getdirstripe -c $DIR/$tdir)
8544         MAX=$((MAX*STRIPE_COUNT))
8545         while [[ $I -le $MAX ]]; do
8546                 $MULTIOP $DIR/$tdir/$J Oc
8547                 rc=$?
8548                 #check two errors ENOSPC for new version of ext4 max_dir_size patch
8549                 #mainline kernel commit df981d03eeff7971ac7e6ff37000bfa702327ef1
8550                 #and EFBIG for previous versions
8551                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
8552                         set_dir_limits 0
8553                         echo "return code $rc received as expected"
8554                         multiop $DIR/$tdir/$J Oc ||
8555                                 error_exit "multiop failed w/o dir size limit"
8556
8557                         I=$(stat -c%s "$DIR/$tdir")
8558
8559                         if [ $(lustre_version_code $SINGLEMDS) -lt \
8560                                         $(version_code 2.4.51) ]
8561                         then
8562                                 [[ $I -eq $MAX ]] && return 0
8563                         else
8564                                 [[ $I -gt $MAX ]] && return 0
8565                         fi
8566                         error_exit "current dir size $I, previous limit $MAX"
8567                 elif [ $rc -ne 0 ]; then
8568                         set_dir_limits 0
8569                         error_exit "return code $rc received instead of expected " \
8570                                    "$EFBIG or $ENOSPC, files in dir $I"
8571                 fi
8572                 J=$((J+1))
8573                 I=$(stat -c%s "$DIR/$tdir")
8574         done
8575
8576         set_dir_limits 0
8577         error "exceeded dir size limit $MAX($MDSCOUNT) : $I bytes"
8578 }
8579 run_test 129 "test directory size limit ========================"
8580
8581 OLDIFS="$IFS"
8582 cleanup_130() {
8583         trap 0
8584         IFS="$OLDIFS"
8585 }
8586
8587 test_130a() {
8588         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8589         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8590                 return
8591
8592         trap cleanup_130 EXIT RETURN
8593
8594         local fm_file=$DIR/$tfile
8595         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
8596         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
8597                 error "dd failed for $fm_file"
8598
8599         # LU-1795: test filefrag/FIEMAP once, even if unsupported
8600         filefrag -ves $fm_file
8601         RC=$?
8602         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8603                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8604         [ $RC != 0 ] && error "filefrag $fm_file failed"
8605
8606         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8607                       grep -v "ext:" | grep -v "found")
8608         lun=$($GETSTRIPE -i $fm_file)
8609
8610         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
8611         IFS=$'\n'
8612         tot_len=0
8613         for line in $filefrag_op
8614         do
8615                 frag_lun=`echo $line | cut -d: -f5`
8616                 ext_len=`echo $line | cut -d: -f4`
8617                 if (( $frag_lun != $lun )); then
8618                         cleanup_130
8619                         error "FIEMAP on 1-stripe file($fm_file) failed"
8620                         return
8621                 fi
8622                 (( tot_len += ext_len ))
8623         done
8624
8625         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
8626                 cleanup_130
8627                 error "FIEMAP on 1-stripe file($fm_file) failed;"
8628                 return
8629         fi
8630
8631         cleanup_130
8632
8633         echo "FIEMAP on single striped file succeeded"
8634 }
8635 run_test 130a "FIEMAP (1-stripe file)"
8636
8637 test_130b() {
8638         [ "$OSTCOUNT" -lt "2" ] &&
8639                 skip_env "skipping FIEMAP on 2-stripe file test" && return
8640
8641         [ "$OSTCOUNT" -ge "10" ] &&
8642                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8643
8644         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8645         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
8646                 return
8647
8648         trap cleanup_130 EXIT RETURN
8649
8650         local fm_file=$DIR/$tfile
8651         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8652         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8653                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8654
8655         dd if=/dev/zero of=$fm_file bs=1M count=2 ||
8656                 error "dd failed on $fm_file"
8657
8658         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8659         filefrag_op=$(filefrag -ve $fm_file | grep -A 100 "ext:" |
8660                       grep -v "ext:" | grep -v "found")
8661
8662         last_lun=$(echo $filefrag_op | cut -d: -f5)
8663
8664         IFS=$'\n'
8665         tot_len=0
8666         num_luns=1
8667         for line in $filefrag_op
8668         do
8669                 frag_lun=`echo $line | cut -d: -f5`
8670                 ext_len=`echo $line | cut -d: -f4`
8671                 if (( $frag_lun != $last_lun )); then
8672                         if (( tot_len != 1024 )); then
8673                                 cleanup_130
8674                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 256"
8675                                 return
8676                         else
8677                                 (( num_luns += 1 ))
8678                                 tot_len=0
8679                         fi
8680                 fi
8681                 (( tot_len += ext_len ))
8682                 last_lun=$frag_lun
8683         done
8684         if (( num_luns != 2 || tot_len != 1024 )); then
8685                 cleanup_130
8686                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8687                 return
8688         fi
8689
8690         cleanup_130
8691
8692         echo "FIEMAP on 2-stripe file succeeded"
8693 }
8694 run_test 130b "FIEMAP (2-stripe file)"
8695
8696 test_130c() {
8697         [ "$OSTCOUNT" -lt "2" ] &&
8698                 skip_env "skipping FIEMAP on 2-stripe file" && return
8699
8700         [ "$OSTCOUNT" -ge "10" ] &&
8701                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8702
8703         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8704         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
8705                 return
8706
8707         trap cleanup_130 EXIT RETURN
8708
8709         local fm_file=$DIR/$tfile
8710         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
8711         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8712                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8713
8714         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 || error "dd failed on $fm_file"
8715
8716         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8717         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" | grep -v "ext:" | grep -v "found"`
8718
8719         last_lun=`echo $filefrag_op | cut -d: -f5`
8720
8721         IFS=$'\n'
8722         tot_len=0
8723         num_luns=1
8724         for line in $filefrag_op
8725         do
8726                 frag_lun=`echo $line | cut -d: -f5`
8727                 ext_len=`echo $line | cut -d: -f4`
8728                 if (( $frag_lun != $last_lun )); then
8729                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
8730                         if (( logical != 512 )); then
8731                                 cleanup_130
8732                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
8733                                 return
8734                         fi
8735                         if (( tot_len != 512 )); then
8736                                 cleanup_130
8737                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8738                                 return
8739                         else
8740                                 (( num_luns += 1 ))
8741                                 tot_len=0
8742                         fi
8743                 fi
8744                 (( tot_len += ext_len ))
8745                 last_lun=$frag_lun
8746         done
8747         if (( num_luns != 2 || tot_len != 512 )); then
8748                 cleanup_130
8749                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8750                 return
8751         fi
8752
8753         cleanup_130
8754
8755         echo "FIEMAP on 2-stripe file with hole succeeded"
8756 }
8757 run_test 130c "FIEMAP (2-stripe file with hole)"
8758
8759 test_130d() {
8760         [ "$OSTCOUNT" -lt "3" ] && skip_env "skipping FIEMAP on N-stripe file test" && return
8761
8762         [ "$OSTCOUNT" -ge "10" ] &&
8763                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8764
8765         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8766         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8767
8768         trap cleanup_130 EXIT RETURN
8769
8770         local fm_file=$DIR/$tfile
8771         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file||error "setstripe on $fm_file"
8772         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8773                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8774
8775         local actual_stripecnt=$($GETSTRIPE -c $fm_file)
8776         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripecnt ||
8777                 error "dd failed on $fm_file"
8778
8779         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8780         filefrag_op=`filefrag -ve $fm_file | grep -A 100 "ext:" |
8781                 grep -v "ext:" | grep -v "found"`
8782
8783         last_lun=`echo $filefrag_op | cut -d: -f5`
8784
8785         IFS=$'\n'
8786         tot_len=0
8787         num_luns=1
8788         for line in $filefrag_op
8789         do
8790                 frag_lun=`echo $line | cut -d: -f5`
8791                 ext_len=`echo $line | cut -d: -f4`
8792                 if (( $frag_lun != $last_lun )); then
8793                         if (( tot_len != 1024 )); then
8794                                 cleanup_130
8795                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
8796                                 return
8797                         else
8798                                 (( num_luns += 1 ))
8799                                 tot_len=0
8800                         fi
8801                 fi
8802                 (( tot_len += ext_len ))
8803                 last_lun=$frag_lun
8804         done
8805         if (( num_luns != actual_stripecnt || tot_len != 1024 )); then
8806                 cleanup_130
8807                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8808                 return
8809         fi
8810
8811         cleanup_130
8812
8813         echo "FIEMAP on N-stripe file succeeded"
8814 }
8815 run_test 130d "FIEMAP (N-stripe file)"
8816
8817 test_130e() {
8818         [ "$OSTCOUNT" -lt "2" ] && skip_env "skipping continuation FIEMAP test" && return
8819
8820         [ "$OSTCOUNT" -ge "10" ] &&
8821                 skip_env "skipping FIEMAP with >= 10 OSTs" && return
8822
8823         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
8824         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
8825
8826         trap cleanup_130 EXIT RETURN
8827
8828         local fm_file=$DIR/$tfile
8829         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
8830         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
8831                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
8832
8833         NUM_BLKS=512
8834         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
8835         for ((i = 0; i < $NUM_BLKS; i++))
8836         do
8837                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
8838         done
8839
8840         filefrag -ves $fm_file || error "filefrag $fm_file failed"
8841         filefrag_op=`filefrag -ve $fm_file | grep -A 12000 "ext:" | grep -v "ext:" | grep -v "found"`
8842
8843         last_lun=`echo $filefrag_op | cut -d: -f5`
8844
8845         IFS=$'\n'
8846         tot_len=0
8847         num_luns=1
8848         for line in $filefrag_op
8849         do
8850                 frag_lun=`echo $line | cut -d: -f5`
8851                 ext_len=`echo $line | cut -d: -f4`
8852                 if (( $frag_lun != $last_lun )); then
8853                         if (( tot_len != $EXPECTED_LEN )); then
8854                                 cleanup_130
8855                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of $EXPECTED_LEN"
8856                                 return
8857                         else
8858                                 (( num_luns += 1 ))
8859                                 tot_len=0
8860                         fi
8861                 fi
8862                 (( tot_len += ext_len ))
8863                 last_lun=$frag_lun
8864         done
8865         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
8866                 cleanup_130
8867                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
8868                 return
8869         fi
8870
8871         cleanup_130
8872
8873         echo "FIEMAP with continuation calls succeeded"
8874 }
8875 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
8876
8877 # Test for writev/readv
8878 test_131a() {
8879         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 || \
8880         error "writev test failed"
8881         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 || \
8882         error "readv failed"
8883         rm -f $DIR/$tfile
8884 }
8885 run_test 131a "test iov's crossing stripe boundary for writev/readv"
8886
8887 test_131b() {
8888         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 || \
8889         error "append writev test failed"
8890         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 || \
8891         error "append writev test failed"
8892         rm -f $DIR/$tfile
8893 }
8894 run_test 131b "test append writev"
8895
8896 test_131c() {
8897         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
8898         error "NOT PASS"
8899 }
8900 run_test 131c "test read/write on file w/o objects"
8901
8902 test_131d() {
8903         rwv -f $DIR/$tfile -w -n 1 1572864
8904         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
8905         if [ "$NOB" != 1572864 ]; then
8906                 error "Short read filed: read $NOB bytes instead of 1572864"
8907         fi
8908         rm -f $DIR/$tfile
8909 }
8910 run_test 131d "test short read"
8911
8912 test_131e() {
8913         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
8914         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
8915         error "read hitting hole failed"
8916         rm -f $DIR/$tfile
8917 }
8918 run_test 131e "test read hitting hole"
8919
8920 get_ost_param() {
8921         local token=$1
8922         local gl_sum=0
8923         for node in $(osts_nodes); do
8924                 gl=$(do_node $node "$LCTL get_param -n ost.OSS.ost.stats" | awk '/'$token'/ {print $2}' | head -n 1)
8925                 [ x$gl = x"" ] && gl=0
8926                 gl_sum=$((gl_sum + gl))
8927         done
8928         echo $gl_sum
8929 }
8930
8931 som_mode_switch() {
8932         local som=$1
8933         local gl1=$2
8934         local gl2=$3
8935
8936         if [ x$som = x"enabled" ]; then
8937                 [ $((gl2 - gl1)) -gt 0 ] && error "no glimpse RPC is expected"
8938                 MOUNT_OPTS=`echo $MOUNT_OPTS |
8939                     sed 's/som_preview,\|,som_preview\|som_preview//g'`
8940                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=disabled"
8941         else
8942                 [ $((gl2 - gl1)) -gt 0 ] || error "some glimpse RPC is expected"
8943                 MOUNT_OPTS="${MOUNT_OPTS:+$MOUNT_OPTS,}som_preview"
8944                 do_facet mgs "$LCTL conf_param $FSNAME.mdt.som=enabled"
8945         fi
8946
8947         # do remount to make new mount-conf parameters actual
8948         echo remounting...
8949         sync
8950         stopall
8951         setupall
8952 }
8953
8954 test_132() { #1028, SOM
8955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8956         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8957         local mdtidx=$($LFS getstripe -M $DIR)
8958         local facet=mds$((mdtidx + 1))
8959
8960         local MOUNTOPT_SAVE=$MOUNTOPT
8961
8962         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8963         cancel_lru_locks osc
8964         som1=$(do_facet $facet "$LCTL get_param mdt.*.som" |
8965                awk -F= ' {print $2}' | head -n 1)
8966
8967         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8968         stat $DIR/$tfile >/dev/null
8969         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8970         echo "====> SOM is "$som1", "$((gl2 - gl1))" glimpse RPC occured"
8971         rm $DIR/$tfile
8972         som_mode_switch $som1 $gl1 $gl2
8973
8974         dd if=/dev/zero of=$DIR/$tfile count=1 2>/dev/null
8975         cancel_lru_locks osc
8976
8977         som2=$(do_facet $facet "$LCTL get_param mdt.*.som" |
8978                awk -F= ' {print $2}' | head -n 1)
8979         if [ $som1 == $som2 ]; then
8980             error "som is still "$som2
8981             if [ x$som2 = x"enabled" ]; then
8982                 som2="disabled"
8983             else
8984                 som2="enabled"
8985             fi
8986         fi
8987
8988         gl1=$(get_ost_param "ldlm_glimpse_enqueue")
8989         stat $DIR/$tfile >/dev/null
8990         gl2=$(get_ost_param "ldlm_glimpse_enqueue")
8991         echo "====> SOM is "$som2", "$((gl2 - gl1))" glimpse RPC occured"
8992         som_mode_switch $som2 $gl1 $gl2
8993         MOUNT_OPTS=$MOUNT_OPTS_SAVE
8994 }
8995 run_test 132 "som avoids glimpse rpc"
8996
8997 check_stats() {
8998         local res
8999         local count
9000         case $1 in
9001         $SINGLEMDS) res=`do_facet $SINGLEMDS $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$2"`
9002                  ;;
9003         ost) res=`do_facet ost1 $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$2"`
9004                  ;;
9005         *) error "Wrong argument $1" ;;
9006         esac
9007         echo $res
9008         [ -z "$res" ] && error "The counter for $2 on $1 was not incremented"
9009         # if the argument $3 is zero, it means any stat increment is ok.
9010         if [[ $3 -gt 0 ]]; then
9011                 count=$(echo $res | awk '{ print $2 }')
9012                 [[ $count -ne $3 ]] &&
9013                         error "The $2 counter on $1 is wrong - expected $3"
9014         fi
9015 }
9016
9017 test_133a() {
9018         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9019         remote_ost_nodsh && skip "remote OST with nodsh" && return
9020         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9021
9022         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9023                 { skip "MDS doesn't support rename stats"; return; }
9024         local testdir=$DIR/${tdir}/stats_testdir
9025         mkdir -p $DIR/${tdir}
9026
9027         # clear stats.
9028         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9029         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9030
9031         # verify mdt stats first.
9032         mkdir ${testdir} || error "mkdir failed"
9033         check_stats $SINGLEMDS "mkdir" 1
9034         touch ${testdir}/${tfile} || "touch failed"
9035         check_stats $SINGLEMDS "open" 1
9036         check_stats $SINGLEMDS "close" 1
9037         mknod ${testdir}/${tfile}-pipe p || "mknod failed"
9038         check_stats $SINGLEMDS "mknod" 1
9039         rm -f ${testdir}/${tfile}-pipe || "pipe remove failed"
9040         check_stats $SINGLEMDS "unlink" 1
9041         rm -f ${testdir}/${tfile} || error "file remove failed"
9042         check_stats $SINGLEMDS "unlink" 2
9043
9044         # remove working dir and check mdt stats again.
9045         rmdir ${testdir} || error "rmdir failed"
9046         check_stats $SINGLEMDS "rmdir" 1
9047
9048         local testdir1=$DIR/${tdir}/stats_testdir1
9049         mkdir -p ${testdir}
9050         mkdir -p ${testdir1}
9051         touch ${testdir1}/test1
9052         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9053         check_stats $SINGLEMDS "crossdir_rename" 1
9054
9055         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9056         check_stats $SINGLEMDS "samedir_rename" 1
9057
9058         rm -rf $DIR/${tdir}
9059 }
9060 run_test 133a "Verifying MDT stats ========================================"
9061
9062 test_133b() {
9063         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9064         remote_ost_nodsh && skip "remote OST with nodsh" && return
9065         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9066         local testdir=$DIR/${tdir}/stats_testdir
9067         mkdir -p ${testdir} || error "mkdir failed"
9068         touch ${testdir}/${tfile} || "touch failed"
9069         cancel_lru_locks mdc
9070
9071         # clear stats.
9072         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9073         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9074
9075         # extra mdt stats verification.
9076         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9077         check_stats $SINGLEMDS "setattr" 1
9078         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9079         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9080         then            # LU-1740
9081                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9082                 check_stats $SINGLEMDS "getattr" 1
9083         fi
9084         $LFS df || error "lfs failed"
9085         check_stats $SINGLEMDS "statfs" 1
9086
9087         rm -rf $DIR/${tdir}
9088 }
9089 run_test 133b "Verifying extra MDT stats =================================="
9090
9091 test_133c() {
9092         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9093         remote_ost_nodsh && skip "remote OST with nodsh" && return
9094         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9095         local testdir=$DIR/${tdir}/stats_testdir
9096         test_mkdir -p ${testdir} || error "mkdir failed"
9097
9098         # verify obdfilter stats.
9099         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9100         sync
9101         cancel_lru_locks osc
9102         wait_delete_completed
9103
9104         # clear stats.
9105         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9106         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9107
9108         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9109         sync
9110         cancel_lru_locks osc
9111         check_stats ost "write" 1
9112
9113         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9114         check_stats ost "read" 1
9115
9116         > ${testdir}/${tfile} || error "truncate failed"
9117         check_stats ost "punch" 1
9118
9119         rm -f ${testdir}/${tfile} || error "file remove failed"
9120         wait_delete_completed
9121         check_stats ost "destroy" 1
9122
9123         rm -rf $DIR/${tdir}
9124 }
9125 run_test 133c "Verifying OST stats ========================================"
9126
9127 order_2() {
9128         local value=$1
9129         local orig=$value
9130         local order=1
9131
9132         while [ $value -ge 2 ]; do
9133                 order=$((order*2))
9134                 value=$((value/2))
9135         done
9136
9137         if [ $orig -gt $order ]; then
9138                 order=$((order*2))
9139         fi
9140         echo $order
9141 }
9142
9143 size_in_KMGT() {
9144     local value=$1
9145     local size=('K' 'M' 'G' 'T');
9146     local i=0
9147     local size_string=$value
9148
9149     while [ $value -ge 1024 ]; do
9150         if [ $i -gt 3 ]; then
9151             #T is the biggest unit we get here, if that is bigger,
9152             #just return XXXT
9153             size_string=${value}T
9154             break
9155         fi
9156         value=$((value >> 10))
9157         if [ $value -lt 1024 ]; then
9158             size_string=${value}${size[$i]}
9159             break
9160         fi
9161         i=$((i + 1))
9162     done
9163
9164     echo $size_string
9165 }
9166
9167 get_rename_size() {
9168     local size=$1
9169     local context=${2:-.}
9170     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9171                 grep -A1 $context |
9172                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9173     echo $sample
9174 }
9175
9176 test_133d() {
9177         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9178         remote_ost_nodsh && skip "remote OST with nodsh" && return
9179         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9180         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9181         { skip "MDS doesn't support rename stats"; return; }
9182
9183         local testdir1=$DIR/${tdir}/stats_testdir1
9184         local testdir2=$DIR/${tdir}/stats_testdir2
9185
9186         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9187
9188         mkdir -p ${testdir1} || error "mkdir failed"
9189         mkdir -p ${testdir2} || error "mkdir failed"
9190
9191         createmany -o $testdir1/test 512 || error "createmany failed"
9192
9193         # check samedir rename size
9194         mv ${testdir1}/test0 ${testdir1}/test_0
9195
9196         local testdir1_size=$(ls -l $DIR/${tdir} |
9197                 awk '/stats_testdir1/ {print $5}')
9198         local testdir2_size=$(ls -l $DIR/${tdir} |
9199                 awk '/stats_testdir2/ {print $5}')
9200
9201         testdir1_size=$(order_2 $testdir1_size)
9202         testdir2_size=$(order_2 $testdir2_size)
9203
9204         testdir1_size=$(size_in_KMGT $testdir1_size)
9205         testdir2_size=$(size_in_KMGT $testdir2_size)
9206
9207         echo "source rename dir size: ${testdir1_size}"
9208         echo "target rename dir size: ${testdir2_size}"
9209
9210         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9211         eval $cmd || error "$cmd failed"
9212         local samedir=$($cmd | grep 'same_dir')
9213         local same_sample=$(get_rename_size $testdir1_size)
9214         [ -z "$samedir" ] && error "samedir_rename_size count error"
9215         [[ $same_sample -eq 1 ]] ||
9216                 error "samedir_rename_size error $same_sample"
9217         echo "Check same dir rename stats success"
9218
9219         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9220
9221         # check crossdir rename size
9222         mv ${testdir1}/test_0 ${testdir2}/test_0
9223
9224         testdir1_size=$(ls -l $DIR/${tdir} |
9225                 awk '/stats_testdir1/ {print $5}')
9226         testdir2_size=$(ls -l $DIR/${tdir} |
9227                 awk '/stats_testdir2/ {print $5}')
9228
9229         testdir1_size=$(order_2 $testdir1_size)
9230         testdir2_size=$(order_2 $testdir2_size)
9231
9232         testdir1_size=$(size_in_KMGT $testdir1_size)
9233         testdir2_size=$(size_in_KMGT $testdir2_size)
9234
9235         echo "source rename dir size: ${testdir1_size}"
9236         echo "target rename dir size: ${testdir2_size}"
9237
9238         eval $cmd || error "$cmd failed"
9239         local crossdir=$($cmd | grep 'crossdir')
9240         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9241         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9242         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9243         [[ $src_sample -eq 1 ]] ||
9244                 error "crossdir_rename_size error $src_sample"
9245         [[ $tgt_sample -eq 1 ]] ||
9246                 error "crossdir_rename_size error $tgt_sample"
9247         echo "Check cross dir rename stats success"
9248         rm -rf $DIR/${tdir}
9249 }
9250 run_test 133d "Verifying rename_stats ========================================"
9251
9252 test_133e() {
9253         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9254         remote_ost_nodsh && skip "remote OST with nodsh" && return
9255         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9256         local testdir=$DIR/${tdir}/stats_testdir
9257         local ctr f0 f1 bs=32768 count=42 sum
9258
9259         mkdir -p ${testdir} || error "mkdir failed"
9260
9261         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9262
9263         for ctr in {write,read}_bytes; do
9264                 sync
9265                 cancel_lru_locks osc
9266
9267                 do_facet ost1 $LCTL set_param -n \
9268                         "obdfilter.*.exports.clear=clear"
9269
9270                 if [ $ctr = write_bytes ]; then
9271                         f0=/dev/zero
9272                         f1=${testdir}/${tfile}
9273                 else
9274                         f0=${testdir}/${tfile}
9275                         f1=/dev/null
9276                 fi
9277
9278                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
9279                         error "dd failed"
9280                 sync
9281                 cancel_lru_locks osc
9282
9283                 sum=$(do_facet ost1 $LCTL get_param \
9284                         "obdfilter.*.exports.*.stats" |
9285                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
9286                                 $1 == ctr { sum += $7 }
9287                                 END { printf("%0.0f", sum) }')
9288
9289                 if ((sum != bs * count)); then
9290                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
9291                 fi
9292         done
9293
9294         rm -rf $DIR/${tdir}
9295 }
9296 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
9297
9298 test_133f() {
9299         local proc_dirs
9300
9301         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9302 /sys/fs/lustre/ /sys/fs/lnet/"
9303         local dir
9304         for dir in $dirs; do
9305                 if [ -d $dir ]; then
9306                         proc_dirs="$proc_dirs $dir"
9307                 fi
9308         done
9309
9310         local facet
9311
9312         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9313         remote_ost_nodsh && skip "remote OST with nodsh" && return
9314         # First without trusting modes.
9315         find $proc_dirs -exec cat '{}' \; &> /dev/null
9316
9317         # Second verifying readability.
9318         find $proc_dirs \
9319                 -type f \
9320                 -exec cat '{}' \; &> /dev/null ||
9321                         error "proc file read failed"
9322
9323         for facet in $SINGLEMDS ost1; do
9324                 do_facet $facet find $proc_dirs \
9325                         ! -name req_history \
9326                         -exec cat '{}' \\\; &> /dev/null
9327
9328                 do_facet $facet find $proc_dirs \
9329                         ! -name req_history \
9330                         -type f \
9331                         -exec cat '{}' \\\; &> /dev/null ||
9332                                 error "proc file read failed"
9333         done
9334 }
9335 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
9336
9337 test_133g() {
9338         local proc_dirs
9339
9340         local dirs="/proc/fs/lustre/ /proc/sys/lnet/ /proc/sys/lustre/ \
9341 /sys/fs/lustre/ /sys/fs/lnet/"
9342         local dir
9343         for dir in $dirs; do
9344                 if [ -d $dir ]; then
9345                         proc_dirs="$proc_dirs $dir"
9346                 fi
9347         done
9348
9349         local facet
9350
9351         # Second verifying readability.
9352         find $proc_dirs \
9353                 -type f \
9354                 -not -name force_lbug \
9355                 -not -name changelog_mask \
9356                 -exec badarea_io '{}' \; &> /dev/null ||
9357                 error "find $proc_dirs failed"
9358
9359         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.5.54) ] &&
9360                 skip "Too old lustre on MDS" && return
9361
9362         [ $(lustre_version_code ost1) -le $(version_code 2.5.54) ] &&
9363                 skip "Too old lustre on ost1" && return
9364
9365         for facet in $SINGLEMDS ost1; do
9366                 do_facet $facet find $proc_dirs \
9367                         -type f \
9368                         -not -name force_lbug \
9369                         -not -name changelog_mask \
9370                         -exec badarea_io '{}' \\\; &> /dev/null ||
9371                 error "$facet find $proc_dirs failed"
9372
9373         done
9374
9375         # remount the FS in case writes/reads /proc break the FS
9376         cleanup || error "failed to unmount"
9377         setup || error "failed to setup"
9378         true
9379 }
9380 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
9381
9382 test_140() { #bug-17379
9383         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9384         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
9385         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
9386         cp /usr/bin/stat . || error "Copying stat to $DIR/$tdir"
9387
9388         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
9389         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
9390         local i=0
9391         while i=`expr $i + 1`; do
9392                 test_mkdir -p $i || error "Creating dir $i"
9393                 cd $i || error "Changing to $i"
9394                 ln -s ../stat stat || error "Creating stat symlink"
9395                 # Read the symlink until ELOOP present,
9396                 # not LBUGing the system is considered success,
9397                 # we didn't overrun the stack.
9398                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
9399                 [ $ret -ne 0 ] && {
9400                         if [ $ret -eq 40 ]; then
9401                                 break  # -ELOOP
9402                         else
9403                                 error "Open stat symlink"
9404                                 return
9405                         fi
9406                 }
9407         done
9408         i=`expr $i - 1`
9409         echo "The symlink depth = $i"
9410         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
9411                                         error "Invalid symlink depth"
9412
9413         # Test recursive symlink
9414         ln -s symlink_self symlink_self
9415         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
9416         echo "open symlink_self returns $ret"
9417         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
9418 }
9419 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
9420
9421 test_150() {
9422         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9423         local TF="$TMP/$tfile"
9424
9425         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9426         cp $TF $DIR/$tfile
9427         cancel_lru_locks osc
9428         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
9429         remount_client $MOUNT
9430         df -P $MOUNT
9431         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
9432
9433         $TRUNCATE $TF 6000
9434         $TRUNCATE $DIR/$tfile 6000
9435         cancel_lru_locks osc
9436         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
9437
9438         echo "12345" >>$TF
9439         echo "12345" >>$DIR/$tfile
9440         cancel_lru_locks osc
9441         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
9442
9443         echo "12345" >>$TF
9444         echo "12345" >>$DIR/$tfile
9445         cancel_lru_locks osc
9446         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
9447
9448         rm -f $TF
9449         true
9450 }
9451 run_test 150 "truncate/append tests"
9452
9453 #LU-2902 roc_hit was not able to read all values from lproc
9454 function roc_hit_init() {
9455         local list=$(comma_list $(osts_nodes))
9456         local dir=$DIR/$tdir-check
9457         local file=$dir/file
9458         local BEFORE
9459         local AFTER
9460         local idx
9461
9462         test_mkdir -p $dir
9463         #use setstripe to do a write to every ost
9464         for i in $(seq 0 $((OSTCOUNT-1))); do
9465                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
9466                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
9467                 idx=$(printf %04x $i)
9468                 BEFORE=$(get_osd_param $list *OST*$idx stats |
9469                         awk '$1 == "cache_access" {sum += $7}
9470                                 END { printf("%0.0f", sum) }')
9471
9472                 cancel_lru_locks osc
9473                 cat $file >/dev/null
9474
9475                 AFTER=$(get_osd_param $list *OST*$idx stats |
9476                         awk '$1 == "cache_access" {sum += $7}
9477                                 END { printf("%0.0f", sum) }')
9478
9479                 echo BEFORE:$BEFORE AFTER:$AFTER
9480                 if ! let "AFTER - BEFORE == 4"; then
9481                         rm -rf $dir
9482                         error "roc_hit is not safe to use"
9483                 fi
9484                 rm $file
9485         done
9486
9487         rm -rf $dir
9488 }
9489
9490 function roc_hit() {
9491         local list=$(comma_list $(osts_nodes))
9492         echo $(get_osd_param $list '' stats |
9493                 awk '$1 == "cache_hit" {sum += $7}
9494                         END { printf("%0.0f", sum) }')
9495 }
9496
9497 function set_cache() {
9498         local on=1
9499
9500         if [ "$2" == "off" ]; then
9501                 on=0;
9502         fi
9503         local list=$(comma_list $(osts_nodes))
9504         set_osd_param $list '' $1_cache_enable $on
9505
9506         cancel_lru_locks osc
9507 }
9508
9509 test_151() {
9510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9511         remote_ost_nodsh && skip "remote OST with nodsh" && return
9512
9513         local CPAGES=3
9514         local list=$(comma_list $(osts_nodes))
9515
9516         # check whether obdfilter is cache capable at all
9517         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
9518                 echo "not cache-capable obdfilter"
9519                 return 0
9520         fi
9521
9522         # check cache is enabled on all obdfilters
9523         if get_osd_param $list '' read_cache_enable | grep 0; then
9524                 echo "oss cache is disabled"
9525                 return 0
9526         fi
9527
9528         set_osd_param $list '' writethrough_cache_enable 1
9529
9530         # check write cache is enabled on all obdfilters
9531         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
9532                 echo "oss write cache is NOT enabled"
9533                 return 0
9534         fi
9535
9536         roc_hit_init
9537
9538         #define OBD_FAIL_OBD_NO_LRU  0x609
9539         do_nodes $list $LCTL set_param fail_loc=0x609
9540
9541         # pages should be in the case right after write
9542         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
9543                 error "dd failed"
9544
9545         local BEFORE=$(roc_hit)
9546         cancel_lru_locks osc
9547         cat $DIR/$tfile >/dev/null
9548         local AFTER=$(roc_hit)
9549
9550         do_nodes $list $LCTL set_param fail_loc=0
9551
9552         if ! let "AFTER - BEFORE == CPAGES"; then
9553                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
9554         fi
9555
9556         # the following read invalidates the cache
9557         cancel_lru_locks osc
9558         set_osd_param $list '' read_cache_enable 0
9559         cat $DIR/$tfile >/dev/null
9560
9561         # now data shouldn't be found in the cache
9562         BEFORE=$(roc_hit)
9563         cancel_lru_locks osc
9564         cat $DIR/$tfile >/dev/null
9565         AFTER=$(roc_hit)
9566         if let "AFTER - BEFORE != 0"; then
9567                 error "IN CACHE: before: $BEFORE, after: $AFTER"
9568         fi
9569
9570         set_osd_param $list '' read_cache_enable 1
9571         rm -f $DIR/$tfile
9572 }
9573 run_test 151 "test cache on oss and controls ==============================="
9574
9575 test_152() {
9576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9577         local TF="$TMP/$tfile"
9578
9579         # simulate ENOMEM during write
9580 #define OBD_FAIL_OST_NOMEM      0x226
9581         lctl set_param fail_loc=0x80000226
9582         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
9583         cp $TF $DIR/$tfile
9584         sync || error "sync failed"
9585         lctl set_param fail_loc=0
9586
9587         # discard client's cache
9588         cancel_lru_locks osc
9589
9590         # simulate ENOMEM during read
9591         lctl set_param fail_loc=0x80000226
9592         cmp $TF $DIR/$tfile || error "cmp failed"
9593         lctl set_param fail_loc=0
9594
9595         rm -f $TF
9596 }
9597 run_test 152 "test read/write with enomem ============================"
9598
9599 test_153() {
9600         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
9601 }
9602 run_test 153 "test if fdatasync does not crash ======================="
9603
9604 dot_lustre_fid_permission_check() {
9605         local fid=$1
9606         local ffid=$MOUNT/.lustre/fid/$fid
9607         local test_dir=$2
9608
9609         echo "stat fid $fid"
9610         stat $ffid > /dev/null || error "stat $ffid failed."
9611         echo "touch fid $fid"
9612         touch $ffid || error "touch $ffid failed."
9613         echo "write to fid $fid"
9614         cat /etc/hosts > $ffid || error "write $ffid failed."
9615         echo "read fid $fid"
9616         diff /etc/hosts $ffid || error "read $ffid failed."
9617         echo "append write to fid $fid"
9618         cat /etc/hosts >> $ffid || error "append write $ffid failed."
9619         echo "rename fid $fid"
9620         mv $ffid $test_dir/$tfile.1 &&
9621                 error "rename $ffid to $tfile.1 should fail."
9622         touch $test_dir/$tfile.1
9623         mv $test_dir/$tfile.1 $ffid &&
9624                 error "rename $tfile.1 to $ffid should fail."
9625         rm -f $test_dir/$tfile.1
9626         echo "truncate fid $fid"
9627         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
9628         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
9629                 echo "link fid $fid"
9630                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
9631         fi
9632         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
9633                 echo "setfacl fid $fid"
9634                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
9635                 echo "getfacl fid $fid"
9636                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
9637         fi
9638         echo "unlink fid $fid"
9639         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
9640         echo "mknod fid $fid"
9641         mknod $ffid c 1 3 && error "mknod $ffid should fail."
9642
9643         fid=[0xf00000400:0x1:0x0]
9644         ffid=$MOUNT/.lustre/fid/$fid
9645
9646         echo "stat non-exist fid $fid"
9647         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
9648         echo "write to non-exist fid $fid"
9649         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
9650         echo "link new fid $fid"
9651         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
9652
9653         mkdir -p $test_dir/$tdir
9654         touch $test_dir/$tdir/$tfile
9655         fid=$($LFS path2fid $test_dir/$tdir)
9656         rc=$?
9657         [ $rc -ne 0 ] &&
9658                 error "error: could not get fid for $test_dir/$dir/$tfile."
9659
9660         ffid=$MOUNT/.lustre/fid/$fid
9661
9662         echo "ls $fid"
9663         ls $ffid > /dev/null || error "ls $ffid failed."
9664         echo "touch $fid/$tfile.1"
9665         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
9666
9667         echo "touch $MOUNT/.lustre/fid/$tfile"
9668         touch $MOUNT/.lustre/fid/$tfile && \
9669                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
9670
9671         echo "setxattr to $MOUNT/.lustre/fid"
9672         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
9673
9674         echo "listxattr for $MOUNT/.lustre/fid"
9675         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
9676
9677         echo "delxattr from $MOUNT/.lustre/fid"
9678         setfattr -x trusted.name1 $MOUNT/.lustre/fid
9679
9680         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
9681         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
9682                 error "touch invalid fid should fail."
9683
9684         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
9685         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
9686                 error "touch non-normal fid should fail."
9687
9688         echo "rename $tdir to $MOUNT/.lustre/fid"
9689         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
9690                 error "rename to $MOUNT/.lustre/fid should fail."
9691
9692         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
9693         then            # LU-3547
9694                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
9695                 local new_obf_mode=777
9696
9697                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
9698                 chmod $new_obf_mode $DIR/.lustre/fid ||
9699                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
9700
9701                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
9702                 [ $obf_mode -eq $new_obf_mode ] ||
9703                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
9704
9705                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
9706                 chmod $old_obf_mode $DIR/.lustre/fid ||
9707                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
9708         fi
9709
9710         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
9711         fid=$($LFS path2fid $test_dir/$tfile-2)
9712
9713         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
9714         then # LU-5424
9715                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
9716                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
9717                         error "create lov data thru .lustre failed"
9718         fi
9719         echo "cp /etc/passwd $test_dir/$tfile-2"
9720         cp /etc/passwd $test_dir/$tfile-2 ||
9721                 error "copy to $test_dir/$tfile-2 failed."
9722         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
9723         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
9724                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
9725
9726         rm -rf $test_dir/tfile.lnk
9727         rm -rf $test_dir/$tfile-2
9728 }
9729
9730 test_154A() {
9731         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9732                 skip "Need MDS version at least 2.4.1" && return
9733
9734         touch $DIR/$tfile
9735         local FID=$($LFS path2fid $DIR/$tfile)
9736         [ -z "$FID" ] && error "path2fid unable to get $DIR/$tfile FID"
9737
9738         # check that we get the same pathname back
9739         local FOUND=$($LFS fid2path $MOUNT $FID)
9740         [ -z "$FOUND" ] && error "fid2path unable to get $FID path"
9741         [ "$FOUND" != "$DIR/$tfile" ] &&
9742                 error "fid2path(path2fid($DIR/$tfile)) = $FOUND != $DIR/$tfile"
9743
9744         rm -rf $DIR/$tfile
9745 }
9746 run_test 154A "lfs path2fid and fid2path basic checks"
9747
9748 test_154a() {
9749         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9750         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9751                 { skip "Need MDS version at least 2.2.51"; return 0; }
9752         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9753
9754         cp /etc/hosts $DIR/$tfile
9755
9756         fid=$($LFS path2fid $DIR/$tfile)
9757         rc=$?
9758         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
9759
9760         dot_lustre_fid_permission_check "$fid" $DIR ||
9761                 error "dot lustre permission check $fid failed"
9762
9763         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
9764
9765         touch $MOUNT/.lustre/file &&
9766                 error "creation is not allowed under .lustre"
9767
9768         mkdir $MOUNT/.lustre/dir &&
9769                 error "mkdir is not allowed under .lustre"
9770
9771         rm -rf $DIR/$tfile
9772 }
9773 run_test 154a "Open-by-FID"
9774
9775 test_154b() {
9776         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9777         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
9778                 { skip "Need MDS version at least 2.2.51"; return 0; }
9779
9780         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
9781
9782         local remote_dir=$DIR/$tdir/remote_dir
9783         local MDTIDX=1
9784         local rc=0
9785
9786         mkdir -p $DIR/$tdir
9787         $LFS mkdir -i $MDTIDX $remote_dir ||
9788                 error "create remote directory failed"
9789
9790         cp /etc/hosts $remote_dir/$tfile
9791
9792         fid=$($LFS path2fid $remote_dir/$tfile)
9793         rc=$?
9794         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
9795
9796         dot_lustre_fid_permission_check "$fid" $remote_dir ||
9797                 error "dot lustre permission check $fid failed"
9798         rm -rf $DIR/$tdir
9799 }
9800 run_test 154b "Open-by-FID for remote directory"
9801
9802 test_154c() {
9803         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
9804                 skip "Need MDS version at least 2.4.1" && return
9805
9806         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
9807         local FID1=$($LFS path2fid $DIR/$tfile.1)
9808         local FID2=$($LFS path2fid $DIR/$tfile.2)
9809         local FID3=$($LFS path2fid $DIR/$tfile.3)
9810
9811         local N=1
9812         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
9813                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
9814                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
9815                 local want=FID$N
9816                 [ "$FID" = "${!want}" ] ||
9817                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
9818                 N=$((N + 1))
9819         done
9820
9821         $LFS fid2path $MOUNT $FID1 $FID2 $FID3 | while read PATHNAME; do
9822                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
9823                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
9824                 N=$((N + 1))
9825         done
9826 }
9827 run_test 154c "lfs path2fid and fid2path multiple arguments"
9828
9829 test_154d() {
9830         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
9831                 skip "Need MDS version at least 2.5.53" && return
9832
9833         if remote_mds; then
9834                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
9835         else
9836                 nid="0@lo"
9837         fi
9838         local proc_ofile="mdt.*.exports.'$nid'.open_files"
9839         local fd
9840         local cmd
9841
9842         rm -f $DIR/$tfile
9843         touch $DIR/$tfile
9844
9845         fid=$($LFS path2fid $DIR/$tfile)
9846         # Open the file
9847         fd=$(free_fd)
9848         cmd="exec $fd<$DIR/$tfile"
9849         eval $cmd
9850         fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
9851         echo $fid_list | grep $fid
9852         rc=$?
9853
9854         cmd="exec $fd>/dev/null"
9855         eval $cmd
9856         if [ $rc -ne 0 ]; then
9857                 error "FID $fid not found in open files list $fid_list"
9858         fi
9859 }
9860 run_test 154d "Verify open file fid"
9861
9862 test_154e()
9863 {
9864         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
9865                 skip "Need MDS version at least 2.6.50" && return
9866
9867         if ls -a $MOUNT | grep -q '^\.lustre$'; then
9868                 error ".lustre returned by readdir"
9869         fi
9870 }
9871 run_test 154e ".lustre is not returned by readdir"
9872
9873 test_154f() {
9874         # create parent directory on a single MDT to avoid cross-MDT hardlinks
9875         test_mkdir -p -c1 $DIR/$tdir/d
9876         # test dirs inherit from its stripe
9877         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
9878         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
9879         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
9880         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
9881         touch $DIR/f
9882
9883         # get fid of parents
9884         local FID0=$($LFS path2fid $DIR/$tdir/d)
9885         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
9886         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
9887         local FID3=$($LFS path2fid $DIR)
9888
9889         # check that path2fid --parents returns expected <parent_fid>/name
9890         # 1) test for a directory (single parent)
9891         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
9892         [ "$parent" == "$FID0/foo1" ] ||
9893                 error "expected parent: $FID0/foo1, got: $parent"
9894
9895         # 2) test for a file with nlink > 1 (multiple parents)
9896         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
9897         echo "$parent" | grep -F "$FID1/$tfile" ||
9898                 error "$FID1/$tfile not returned in parent list"
9899         echo "$parent" | grep -F "$FID2/link" ||
9900                 error "$FID2/link not returned in parent list"
9901
9902         # 3) get parent by fid
9903         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
9904         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9905         echo "$parent" | grep -F "$FID1/$tfile" ||
9906                 error "$FID1/$tfile not returned in parent list (by fid)"
9907         echo "$parent" | grep -F "$FID2/link" ||
9908                 error "$FID2/link not returned in parent list (by fid)"
9909
9910         # 4) test for entry in root directory
9911         parent=$($LFS path2fid --parents $DIR/f)
9912         echo "$parent" | grep -F "$FID3/f" ||
9913                 error "$FID3/f not returned in parent list"
9914
9915         # 5) test it on root directory
9916         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
9917                 error "$MOUNT should not have parents"
9918
9919         # enable xattr caching and check that linkea is correctly updated
9920         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9921         save_lustre_params client "llite.*.xattr_cache" > $save
9922         lctl set_param llite.*.xattr_cache 1
9923
9924         # 6.1) linkea update on rename
9925         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
9926
9927         # get parents by fid
9928         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9929         # foo1 should no longer be returned in parent list
9930         echo "$parent" | grep -F "$FID1" &&
9931                 error "$FID1 should no longer be in parent list"
9932         # the new path should appear
9933         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
9934                 error "$FID2/$tfile.moved is not in parent list"
9935
9936         # 6.2) linkea update on unlink
9937         rm -f $DIR/$tdir/d/foo2/link
9938         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
9939         # foo2/link should no longer be returned in parent list
9940         echo "$parent" | grep -F "$FID2/link" &&
9941                 error "$FID2/link should no longer be in parent list"
9942         true
9943
9944         rm -f $DIR/f
9945         restore_lustre_params < $save
9946 }
9947 run_test 154f "get parent fids by reading link ea"
9948
9949 test_155_small_load() {
9950     local temp=$TMP/$tfile
9951     local file=$DIR/$tfile
9952
9953     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
9954         error "dd of=$temp bs=6096 count=1 failed"
9955     cp $temp $file
9956     cancel_lru_locks osc
9957     cmp $temp $file || error "$temp $file differ"
9958
9959     $TRUNCATE $temp 6000
9960     $TRUNCATE $file 6000
9961     cmp $temp $file || error "$temp $file differ (truncate1)"
9962
9963     echo "12345" >>$temp
9964     echo "12345" >>$file
9965     cmp $temp $file || error "$temp $file differ (append1)"
9966
9967     echo "12345" >>$temp
9968     echo "12345" >>$file
9969     cmp $temp $file || error "$temp $file differ (append2)"
9970
9971     rm -f $temp $file
9972     true
9973 }
9974
9975 test_155_big_load() {
9976     remote_ost_nodsh && skip "remote OST with nodsh" && return
9977     local temp=$TMP/$tfile
9978     local file=$DIR/$tfile
9979
9980     free_min_max
9981     local cache_size=$(do_facet ost$((MAXI+1)) \
9982         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
9983     local large_file_size=$((cache_size * 2))
9984
9985     echo "OSS cache size: $cache_size KB"
9986     echo "Large file size: $large_file_size KB"
9987
9988     [ $MAXV -le $large_file_size ] && \
9989         skip_env "max available OST size needs > $large_file_size KB" && \
9990         return 0
9991
9992     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
9993
9994     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
9995         error "dd of=$temp bs=$large_file_size count=1k failed"
9996     cp $temp $file
9997     ls -lh $temp $file
9998     cancel_lru_locks osc
9999     cmp $temp $file || error "$temp $file differ"
10000
10001     rm -f $temp $file
10002     true
10003 }
10004
10005 test_155a() {
10006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10007         set_cache read on
10008         set_cache writethrough on
10009         test_155_small_load
10010 }
10011 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10012
10013 test_155b() {
10014         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10015         set_cache read on
10016         set_cache writethrough off
10017         test_155_small_load
10018 }
10019 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10020
10021 test_155c() {
10022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10023         set_cache read off
10024         set_cache writethrough on
10025         test_155_small_load
10026 }
10027 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10028
10029 test_155d() {
10030         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10031         set_cache read off
10032         set_cache writethrough off
10033         test_155_small_load
10034 }
10035 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10036
10037 test_155e() {
10038         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10039         set_cache read on
10040         set_cache writethrough on
10041         test_155_big_load
10042 }
10043 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10044
10045 test_155f() {
10046         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10047         set_cache read on
10048         set_cache writethrough off
10049         test_155_big_load
10050 }
10051 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10052
10053 test_155g() {
10054         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10055         set_cache read off
10056         set_cache writethrough on
10057         test_155_big_load
10058 }
10059 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10060
10061 test_155h() {
10062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10063         set_cache read off
10064         set_cache writethrough off
10065         test_155_big_load
10066 }
10067 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10068
10069 test_156() {
10070         remote_ost_nodsh && skip "remote OST with nodsh" && return
10071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10072         local CPAGES=3
10073         local BEFORE
10074         local AFTER
10075         local file="$DIR/$tfile"
10076
10077         [ "$(facet_fstype ost1)" = "zfs" ] &&
10078                 skip "LU-1956/LU-2261: stats unimplemented on OSD ZFS" &&
10079                 return
10080
10081         roc_hit_init
10082
10083     log "Turn on read and write cache"
10084     set_cache read on
10085     set_cache writethrough on
10086
10087     log "Write data and read it back."
10088     log "Read should be satisfied from the cache."
10089     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10090     BEFORE=`roc_hit`
10091     cancel_lru_locks osc
10092     cat $file >/dev/null
10093     AFTER=`roc_hit`
10094     if ! let "AFTER - BEFORE == CPAGES"; then
10095         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10096     else
10097         log "cache hits:: before: $BEFORE, after: $AFTER"
10098     fi
10099
10100     log "Read again; it should be satisfied from the cache."
10101     BEFORE=$AFTER
10102     cancel_lru_locks osc
10103     cat $file >/dev/null
10104     AFTER=`roc_hit`
10105     if ! let "AFTER - BEFORE == CPAGES"; then
10106         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10107     else
10108         log "cache hits:: before: $BEFORE, after: $AFTER"
10109     fi
10110
10111
10112     log "Turn off the read cache and turn on the write cache"
10113     set_cache read off
10114     set_cache writethrough on
10115
10116     log "Read again; it should be satisfied from the cache."
10117     BEFORE=`roc_hit`
10118     cancel_lru_locks osc
10119     cat $file >/dev/null
10120     AFTER=`roc_hit`
10121     if ! let "AFTER - BEFORE == CPAGES"; then
10122         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10123     else
10124         log "cache hits:: before: $BEFORE, after: $AFTER"
10125     fi
10126
10127     log "Read again; it should not be satisfied from the cache."
10128     BEFORE=$AFTER
10129     cancel_lru_locks osc
10130     cat $file >/dev/null
10131     AFTER=`roc_hit`
10132     if ! let "AFTER - BEFORE == 0"; then
10133         error "IN CACHE: before: $BEFORE, after: $AFTER"
10134     else
10135         log "cache hits:: before: $BEFORE, after: $AFTER"
10136     fi
10137
10138     log "Write data and read it back."
10139     log "Read should be satisfied from the cache."
10140     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10141     BEFORE=`roc_hit`
10142     cancel_lru_locks osc
10143     cat $file >/dev/null
10144     AFTER=`roc_hit`
10145     if ! let "AFTER - BEFORE == CPAGES"; then
10146         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10147     else
10148         log "cache hits:: before: $BEFORE, after: $AFTER"
10149     fi
10150
10151     log "Read again; it should not be satisfied from the cache."
10152     BEFORE=$AFTER
10153     cancel_lru_locks osc
10154     cat $file >/dev/null
10155     AFTER=`roc_hit`
10156     if ! let "AFTER - BEFORE == 0"; then
10157         error "IN CACHE: before: $BEFORE, after: $AFTER"
10158     else
10159         log "cache hits:: before: $BEFORE, after: $AFTER"
10160     fi
10161
10162
10163     log "Turn off read and write cache"
10164     set_cache read off
10165     set_cache writethrough off
10166
10167     log "Write data and read it back"
10168     log "It should not be satisfied from the cache."
10169     rm -f $file
10170     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10171     cancel_lru_locks osc
10172     BEFORE=`roc_hit`
10173     cat $file >/dev/null
10174     AFTER=`roc_hit`
10175         if ! let "AFTER - BEFORE == 0"; then
10176                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10177         else
10178                 log "cache hits:: before: $BEFORE, after: $AFTER"
10179         fi
10180
10181     log "Turn on the read cache and turn off the write cache"
10182     set_cache read on
10183     set_cache writethrough off
10184
10185     log "Write data and read it back"
10186     log "It should not be satisfied from the cache."
10187     rm -f $file
10188     dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10189     BEFORE=`roc_hit`
10190     cancel_lru_locks osc
10191     cat $file >/dev/null
10192     AFTER=`roc_hit`
10193         if ! let "AFTER - BEFORE == 0"; then
10194                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
10195         else
10196                 log "cache hits:: before: $BEFORE, after: $AFTER"
10197         fi
10198
10199     log "Read again; it should be satisfied from the cache."
10200     BEFORE=`roc_hit`
10201     cancel_lru_locks osc
10202     cat $file >/dev/null
10203     AFTER=`roc_hit`
10204     if ! let "AFTER - BEFORE == CPAGES"; then
10205         error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10206     else
10207         log "cache hits:: before: $BEFORE, after: $AFTER"
10208     fi
10209
10210     rm -f $file
10211 }
10212 run_test 156 "Verification of tunables ============================"
10213
10214 #Changelogs
10215 err17935 () {
10216         if [[ $MDSCOUNT -gt 1 ]]; then
10217                 error_ignore bz17935 $*
10218         else
10219                 error $*
10220         fi
10221 }
10222
10223 changelog_chmask()
10224 {
10225         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
10226
10227         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
10228
10229         if [ $MASK -eq 1 ]; then
10230                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
10231         else
10232                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
10233         fi
10234 }
10235
10236 changelog_extract_field() {
10237         local mdt=$1
10238         local cltype=$2
10239         local file=$3
10240         local identifier=$4
10241
10242         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
10243                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
10244                 tail -1
10245 }
10246
10247 test_160a() {
10248         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10249         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10250         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10251                 { skip "Need MDS version at least 2.2.0"; return; }
10252
10253         local CL_USERS="mdd.$MDT0.changelog_users"
10254         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10255         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10256         echo "Registered as changelog user $USER"
10257         $GET_CL_USERS | grep -q $USER ||
10258                 error "User $USER not found in changelog_users"
10259
10260         # change something
10261         test_mkdir -p $DIR/$tdir/pics/2008/zachy
10262         touch $DIR/$tdir/pics/2008/zachy/timestamp
10263         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
10264         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
10265         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
10266         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
10267         rm $DIR/$tdir/pics/desktop.jpg
10268
10269         $LFS changelog $MDT0 | tail -5
10270
10271         echo "verifying changelog mask"
10272         changelog_chmask "MKDIR"
10273         changelog_chmask "CLOSE"
10274
10275         test_mkdir -p $DIR/$tdir/pics/zach/sofia
10276         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10277
10278         changelog_chmask "MKDIR"
10279         changelog_chmask "CLOSE"
10280
10281         test_mkdir -p $DIR/$tdir/pics/2008/sofia
10282         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
10283
10284         $LFS changelog $MDT0
10285         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
10286         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
10287         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
10288         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
10289
10290         # verify contents
10291         echo "verifying target fid"
10292         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
10293         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
10294         [ "$fidc" == "$fidf" ] ||
10295                 err17935 "fid in changelog $fidc != file fid $fidf"
10296         echo "verifying parent fid"
10297         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
10298         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
10299         [ "$fidc" == "$fidf" ] ||
10300                 err17935 "pfid in changelog $fidc != dir fid $fidf"
10301
10302         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10303         $LFS changelog_clear $MDT0 $USER $(($USER_REC1 + 5))
10304         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$USER\" {print \$2}")
10305         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
10306         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
10307                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
10308
10309         MIN_REC=$($GET_CL_USERS |
10310                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
10311         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
10312         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
10313         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
10314                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
10315
10316         # LU-3446 changelog index reset on MDT restart
10317         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
10318         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10319         $LFS changelog_clear $MDT0 $USER 0
10320         stop $SINGLEMDS || error "Fail to stop MDT."
10321         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
10322         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10323         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
10324         [ $CUR_REC1 == $CUR_REC2 ] ||
10325                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
10326
10327         echo "verifying user deregister"
10328         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10329         $GET_CL_USERS | grep -q $USER &&
10330                 error "User $USER still in changelog_users"
10331
10332         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
10333         if [ $USERS -eq 0 ]; then
10334                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10335                 touch $DIR/$tdir/chloe
10336                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
10337                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
10338                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
10339         else
10340                 echo "$USERS other changelog users; can't verify off"
10341         fi
10342 }
10343 run_test 160a "changelog sanity"
10344
10345 test_160b() { # LU-3587
10346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10347         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10348         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
10349                 { skip "Need MDS version at least 2.2.0"; return; }
10350
10351         local CL_USERS="mdd.$MDT0.changelog_users"
10352         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
10353         USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_register -n)
10354         echo "Registered as changelog user $USER"
10355         $GET_CL_USERS | grep -q $USER ||
10356                 error "User $USER not found in changelog_users"
10357
10358         local LONGNAME1=$(str_repeat a 255)
10359         local LONGNAME2=$(str_repeat b 255)
10360
10361         cd $DIR
10362         echo "creating very long named file"
10363         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
10364         echo "moving very long named file"
10365         mv $LONGNAME1 $LONGNAME2
10366
10367         $LFS changelog $MDT0 | grep RENME
10368
10369         echo "deregistering $USER"
10370         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10371
10372         rm -f $LONGNAME2
10373 }
10374 run_test 160b "Verify that very long rename doesn't crash in changelog"
10375
10376 test_160c() {
10377         local rc=0
10378         local server_version=$(lustre_version_code $SINGLEMDS)
10379
10380         [[ $server_version -gt $(version_code 2.5.57) ]] ||
10381                 [[ $server_version -gt $(version_code 2.5.1) &&
10382                    $server_version -lt $(version_code 2.5.50) ]] ||
10383                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
10384         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10385
10386         # Registration step
10387         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10388                 changelog_register -n)
10389
10390         rm -rf $DIR/$tdir
10391         mkdir -p $DIR/$tdir
10392         $MCREATE $DIR/$tdir/foo_160c
10393         changelog_chmask "TRUNC"
10394         $TRUNCATE $DIR/$tdir/foo_160c 200
10395         changelog_chmask "TRUNC"
10396         $TRUNCATE $DIR/$tdir/foo_160c 199
10397         $LFS changelog $MDT0
10398         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
10399         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
10400         $LFS changelog_clear $MDT0 $USER 0
10401
10402         # Deregistration step
10403         echo "deregistering $USER"
10404         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10405 }
10406 run_test 160c "verify that changelog log catch the truncate event"
10407
10408 test_161a() {
10409         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10410         test_mkdir -p -c1 $DIR/$tdir
10411         cp /etc/hosts $DIR/$tdir/$tfile
10412         test_mkdir -c1 $DIR/$tdir/foo1
10413         test_mkdir -c1 $DIR/$tdir/foo2
10414         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
10415         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
10416         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
10417         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
10418         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
10419         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10420                 $LFS fid2path $DIR $FID
10421                 err17935 "bad link ea"
10422         fi
10423     # middle
10424     rm $DIR/$tdir/foo2/zachary
10425     # last
10426     rm $DIR/$tdir/foo2/thor
10427     # first
10428     rm $DIR/$tdir/$tfile
10429     # rename
10430     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
10431     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
10432         then
10433         $LFS fid2path $DIR $FID
10434         err17935 "bad link rename"
10435     fi
10436     rm $DIR/$tdir/foo2/maggie
10437
10438         # overflow the EA
10439         local longname=filename_avg_len_is_thirty_two_
10440         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
10441                 error "failed to hardlink many files"
10442         links=$($LFS fid2path $DIR $FID | wc -l)
10443         echo -n "${links}/1000 links in link EA"
10444         [[ $links -gt 60 ]] ||
10445                 err17935 "expected at least 60 links in link EA"
10446         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
10447                 error "failed to unlink many hardlinks"
10448 }
10449 run_test 161a "link ea sanity"
10450
10451 test_161b() {
10452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10453         [ $MDSCOUNT -lt 2 ] &&
10454                 skip "skipping remote directory test" && return
10455         local MDTIDX=1
10456         local remote_dir=$DIR/$tdir/remote_dir
10457
10458         mkdir -p $DIR/$tdir
10459         $LFS mkdir -i $MDTIDX $remote_dir ||
10460                 error "create remote directory failed"
10461
10462         cp /etc/hosts $remote_dir/$tfile
10463         mkdir -p $remote_dir/foo1
10464         mkdir -p $remote_dir/foo2
10465         ln $remote_dir/$tfile $remote_dir/foo1/sofia
10466         ln $remote_dir/$tfile $remote_dir/foo2/zachary
10467         ln $remote_dir/$tfile $remote_dir/foo1/luna
10468         ln $remote_dir/$tfile $remote_dir/foo2/thor
10469
10470         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
10471                      tr -d ']')
10472         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
10473                 $LFS fid2path $DIR $FID
10474                 err17935 "bad link ea"
10475         fi
10476         # middle
10477         rm $remote_dir/foo2/zachary
10478         # last
10479         rm $remote_dir/foo2/thor
10480         # first
10481         rm $remote_dir/$tfile
10482         # rename
10483         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
10484         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
10485         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
10486                 $LFS fid2path $DIR $FID
10487                 err17935 "bad link rename"
10488         fi
10489         rm $remote_dir/foo2/maggie
10490
10491         # overflow the EA
10492         local longname=filename_avg_len_is_thirty_two_
10493         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
10494                 error "failed to hardlink many files"
10495         links=$($LFS fid2path $DIR $FID | wc -l)
10496         echo -n "${links}/1000 links in link EA"
10497         [[ ${links} -gt 60 ]] ||
10498                 err17935 "expected at least 60 links in link EA"
10499         unlinkmany $remote_dir/foo2/$longname 1000 ||
10500         error "failed to unlink many hardlinks"
10501 }
10502 run_test 161b "link ea sanity under remote directory"
10503
10504 test_161c() {
10505         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10507         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
10508                 skip "Need MDS version at least 2.1.5" && return
10509
10510         # define CLF_RENAME_LAST 0x0001
10511         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
10512         local USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
10513                 changelog_register -n)
10514         rm -rf $DIR/$tdir
10515         mkdir -p $DIR/$tdir
10516         touch $DIR/$tdir/foo_161c
10517         touch $DIR/$tdir/bar_161c
10518         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10519         $LFS changelog $MDT0 | grep RENME
10520         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
10521                 cut -f5 -d' ')
10522         $LFS changelog_clear $MDT0 $USER 0
10523         if [ x$flags != "x0x1" ]; then
10524                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10525                         $USER
10526                 error "flag $flags is not 0x1"
10527         fi
10528         echo "rename overwrite a target having nlink = 1," \
10529                 "changelog record has flags of $flags"
10530
10531         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
10532         touch $DIR/$tdir/foo_161c
10533         touch $DIR/$tdir/bar_161c
10534         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10535         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
10536         $LFS changelog $MDT0 | grep RENME
10537         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10538         $LFS changelog_clear $MDT0 $USER 0
10539         if [ x$flags != "x0x0" ]; then
10540                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10541                         $USER
10542                 error "flag $flags is not 0x0"
10543         fi
10544         echo "rename overwrite a target having nlink > 1," \
10545                 "changelog record has flags of $flags"
10546
10547         # rename doesn't overwrite a target (changelog flag 0x0)
10548         touch $DIR/$tdir/foo_161c
10549         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
10550         $LFS changelog $MDT0 | grep RENME
10551         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
10552         $LFS changelog_clear $MDT0 $USER 0
10553         if [ x$flags != "x0x0" ]; then
10554                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10555                         $USER
10556                 error "flag $flags is not 0x0"
10557         fi
10558         echo "rename doesn't overwrite a target," \
10559                 "changelog record has flags of $flags"
10560
10561         # define CLF_UNLINK_LAST 0x0001
10562         # unlink a file having nlink = 1 (changelog flag 0x1)
10563         rm -f $DIR/$tdir/foo2_161c
10564         $LFS changelog $MDT0 | grep UNLNK
10565         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10566         $LFS changelog_clear $MDT0 $USER 0
10567         if [ x$flags != "x0x1" ]; then
10568                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10569                         $USER
10570                 error "flag $flags is not 0x1"
10571         fi
10572         echo "unlink a file having nlink = 1," \
10573                 "changelog record has flags of $flags"
10574
10575         # unlink a file having nlink > 1 (changelog flag 0x0)
10576         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
10577         rm -f $DIR/$tdir/foobar_161c
10578         $LFS changelog $MDT0 | grep UNLNK
10579         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
10580         $LFS changelog_clear $MDT0 $USER 0
10581         if [ x$flags != "x0x0" ]; then
10582                 do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister \
10583                         $USER
10584                 error "flag $flags is not 0x0"
10585         fi
10586         echo "unlink a file having nlink > 1," \
10587                 "changelog record has flags of $flags"
10588         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $USER
10589 }
10590 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
10591
10592 check_path() {
10593     local expected=$1
10594     shift
10595     local fid=$2
10596
10597     local path=$(${LFS} fid2path $*)
10598     # Remove the '//' indicating a remote directory
10599     path=$(echo $path | sed 's#//#/#g')
10600     RC=$?
10601
10602     if [ $RC -ne 0 ]; then
10603         err17935 "path looked up of $expected failed. Error $RC"
10604         return $RC
10605     elif [ "${path}" != "${expected}" ]; then
10606         err17935 "path looked up \"${path}\" instead of \"${expected}\""
10607         return 2
10608     fi
10609     echo "fid $fid resolves to path $path (expected $expected)"
10610 }
10611
10612 test_162() {
10613         # Make changes to filesystem
10614         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10615         test_mkdir -p -c1 $DIR/$tdir/d2
10616         touch $DIR/$tdir/d2/$tfile
10617         touch $DIR/$tdir/d2/x1
10618         touch $DIR/$tdir/d2/x2
10619         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
10620         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
10621         # regular file
10622         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
10623         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
10624                 error "check path $tdir/d2/$tfile failed"
10625
10626         # softlink
10627         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
10628         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
10629         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
10630                 error "check path $tdir/d2/p/q/r/slink failed"
10631
10632         # softlink to wrong file
10633         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
10634         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
10635         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
10636                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
10637
10638         # hardlink
10639         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
10640         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
10641         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
10642         # fid2path dir/fsname should both work
10643         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
10644                 error "check path $tdir/d2/a/b/c/new_file failed"
10645         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
10646                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
10647
10648         # hardlink count: check that there are 2 links
10649         # Doesnt work with CMD yet: 17935
10650         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
10651                 err17935 "expected 2 links"
10652
10653         # hardlink indexing: remove the first link
10654         rm $DIR/$tdir/d2/p/q/r/hlink
10655         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
10656                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
10657
10658         return 0
10659 }
10660 run_test 162 "path lookup sanity"
10661
10662 test_162b() {
10663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10664         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10665
10666         mkdir $DIR/$tdir
10667         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
10668                                 error "create striped dir failed"
10669
10670         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
10671                                         tail -n 1 | awk '{print $2}')
10672         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
10673
10674         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
10675         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
10676
10677         # regular file
10678         for ((i=0;i<5;i++)); do
10679                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
10680                         error "get fid for f$i failed"
10681                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
10682                         error "check path $tdir/striped_dir/f$i failed"
10683
10684                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
10685                         error "get fid for d$i failed"
10686                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
10687                         error "check path $tdir/striped_dir/d$i failed"
10688         done
10689
10690         return 0
10691 }
10692 run_test 162b "striped directory path lookup sanity"
10693
10694 # LU-4239: Verify fid2path works with paths 100 or more directories deep
10695 test_162c() {
10696         test_mkdir $DIR/$tdir.local
10697         test_mkdir $DIR/$tdir.remote
10698         local lpath=$tdir.local
10699         local rpath=$tdir.remote
10700
10701         for ((i = 0; i <= 101; i++)); do
10702                 lpath="$lpath/$i"
10703                 mkdir $DIR/$lpath
10704                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
10705                         error "get fid for local directory $DIR/$lpath failed"
10706                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
10707                         error "check path for local directory $DIR/$lpath failed"
10708
10709                 rpath="$rpath/$i"
10710                 test_mkdir $DIR/$rpath
10711                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
10712                         error "get fid for remote directory $DIR/$rpath failed"
10713                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
10714                         error "check path for remote directory $DIR/$rpath failed"
10715         done
10716
10717         return 0
10718 }
10719 run_test 162c "fid2path works with paths 100 or more directories deep"
10720
10721 test_169() {
10722         # do directio so as not to populate the page cache
10723         log "creating a 10 Mb file"
10724         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
10725         log "starting reads"
10726         dd if=$DIR/$tfile of=/dev/null bs=4096 &
10727         log "truncating the file"
10728         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
10729         log "killing dd"
10730         kill %+ || true # reads might have finished
10731         echo "wait until dd is finished"
10732         wait
10733         log "removing the temporary file"
10734         rm -rf $DIR/$tfile || error "tmp file removal failed"
10735 }
10736 run_test 169 "parallel read and truncate should not deadlock"
10737
10738 test_170() {
10739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10740         $LCTL clear     # bug 18514
10741         $LCTL debug_daemon start $TMP/${tfile}_log_good
10742         touch $DIR/$tfile
10743         $LCTL debug_daemon stop
10744         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
10745                error "sed failed to read log_good"
10746
10747         $LCTL debug_daemon start $TMP/${tfile}_log_good
10748         rm -rf $DIR/$tfile
10749         $LCTL debug_daemon stop
10750
10751         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
10752                error "lctl df log_bad failed"
10753
10754         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10755         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10756
10757         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
10758         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
10759
10760         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
10761                 error "bad_line good_line1 good_line2 are empty"
10762
10763         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10764         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
10765         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
10766
10767         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
10768         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
10769         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
10770
10771         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
10772                 error "bad_line_new good_line_new are empty"
10773
10774         local expected_good=$((good_line1 + good_line2*2))
10775
10776         rm -f $TMP/${tfile}*
10777         # LU-231, short malformed line may not be counted into bad lines
10778         if [ $bad_line -ne $bad_line_new ] &&
10779                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
10780                 error "expected $bad_line bad lines, but got $bad_line_new"
10781                 return 1
10782         fi
10783
10784         if [ $expected_good -ne $good_line_new ]; then
10785                 error "expected $expected_good good lines, but got $good_line_new"
10786                 return 2
10787         fi
10788         true
10789 }
10790 run_test 170 "test lctl df to handle corrupted log ====================="
10791
10792 test_171() { # bug20592
10793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10794 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
10795         $LCTL set_param fail_loc=0x50e
10796         $LCTL set_param fail_val=3000
10797         multiop_bg_pause $DIR/$tfile O_s || true
10798         local MULTIPID=$!
10799         kill -USR1 $MULTIPID
10800         # cause log dump
10801         sleep 3
10802         wait $MULTIPID
10803         if dmesg | grep "recursive fault"; then
10804                 error "caught a recursive fault"
10805         fi
10806         $LCTL set_param fail_loc=0
10807         true
10808 }
10809 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
10810
10811 # it would be good to share it with obdfilter-survey/iokit-libecho code
10812 setup_obdecho_osc () {
10813         local rc=0
10814         local ost_nid=$1
10815         local obdfilter_name=$2
10816         echo "Creating new osc for $obdfilter_name on $ost_nid"
10817         # make sure we can find loopback nid
10818         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
10819
10820         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
10821                            ${obdfilter_name}_osc_UUID || rc=2; }
10822         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
10823                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
10824         return $rc
10825 }
10826
10827 cleanup_obdecho_osc () {
10828         local obdfilter_name=$1
10829         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
10830         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
10831         return 0
10832 }
10833
10834 obdecho_test() {
10835         local OBD=$1
10836         local node=$2
10837         local pages=${3:-64}
10838         local rc=0
10839         local id
10840
10841         local count=10
10842         local obd_size=$(get_obd_size $node $OBD)
10843         local page_size=$(get_page_size $node)
10844         if [[ -n "$obd_size" ]]; then
10845                 local new_count=$((obd_size / (pages * page_size / 1024)))
10846                 [[ $new_count -ge $count ]] || count=$new_count
10847         fi
10848
10849         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
10850         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
10851                            rc=2; }
10852         if [ $rc -eq 0 ]; then
10853             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
10854             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
10855         fi
10856         echo "New object id is $id"
10857         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
10858                            rc=4; }
10859         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
10860                            "test_brw $count w v $pages $id" || rc=4; }
10861         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
10862                            rc=4; }
10863         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
10864                                         "cleanup" || rc=5; }
10865         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
10866                                         "detach" || rc=6; }
10867         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
10868         return $rc
10869 }
10870
10871 test_180a() {
10872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10873         remote_ost_nodsh && skip "remote OST with nodsh" && return
10874         local rc=0
10875         local rmmod_local=0
10876
10877         if ! module_loaded obdecho; then
10878             load_module obdecho/obdecho
10879             rmmod_local=1
10880         fi
10881
10882         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
10883         local host=$(lctl get_param -n osc.$osc.import |
10884                              awk '/current_connection:/ {print $2}' )
10885         local target=$(lctl get_param -n osc.$osc.import |
10886                              awk '/target:/ {print $2}' )
10887         target=${target%_UUID}
10888
10889         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
10890         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
10891         [[ -n $target ]] && cleanup_obdecho_osc $target
10892         [ $rmmod_local -eq 1 ] && rmmod obdecho
10893         return $rc
10894 }
10895 run_test 180a "test obdecho on osc"
10896
10897 test_180b() {
10898         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10899         remote_ost_nodsh && skip "remote OST with nodsh" && return
10900         local rc=0
10901         local rmmod_remote=0
10902
10903         do_facet ost1 "lsmod | grep -q obdecho || "                      \
10904                       "{ insmod ${LUSTRE}/obdecho/obdecho.ko || "        \
10905                       "modprobe obdecho; }" && rmmod_remote=1
10906         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
10907         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
10908         [ $rmmod_remote -eq 1 ] && do_facet ost1 "rmmod obdecho"
10909         return $rc
10910 }
10911 run_test 180b "test obdecho directly on obdfilter"
10912
10913 test_180c() { # LU-2598
10914         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10915         remote_ost_nodsh && skip "remote OST with nodsh" && return
10916         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
10917                 skip "Need MDS version at least 2.4.0" && return
10918
10919         local rc=0
10920         local rmmod_remote=false
10921         local pages=16384 # 64MB bulk I/O RPC size
10922         local target
10923
10924         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
10925                 rmmod_remote=true || error "failed to load module obdecho"
10926
10927         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
10928                 head -n1)
10929         if [ -n "$target" ]; then
10930                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
10931         else
10932                 echo "there is no obdfilter target on ost1"
10933                 rc=2
10934         fi
10935         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
10936         return $rc
10937 }
10938 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
10939
10940 test_181() { # bug 22177
10941         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10942         # create enough files to index the directory
10943         createmany -o $DIR/$tdir/foobar 4000
10944         # print attributes for debug purpose
10945         lsattr -d .
10946         # open dir
10947         multiop_bg_pause $DIR/$tdir D_Sc || return 1
10948         MULTIPID=$!
10949         # remove the files & current working dir
10950         unlinkmany $DIR/$tdir/foobar 4000
10951         rmdir $DIR/$tdir
10952         kill -USR1 $MULTIPID
10953         wait $MULTIPID
10954         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
10955         return 0
10956 }
10957 run_test 181 "Test open-unlinked dir ========================"
10958
10959 test_182() {
10960         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10961         # disable MDC RPC lock wouldn't crash client
10962         local fcount=1000
10963         local tcount=4
10964
10965         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10966 #define OBD_FAIL_MDC_RPCS_SEM           0x804
10967         $LCTL set_param fail_loc=0x804
10968
10969         for (( i=0; i < $tcount; i++ )) ; do
10970                 mkdir $DIR/$tdir/$i
10971                 createmany -o $DIR/$tdir/$i/f- $fcount &
10972         done
10973         wait
10974
10975         for (( i=0; i < $tcount; i++ )) ; do
10976                 unlinkmany $DIR/$tdir/$i/f- $fcount &
10977         done
10978         wait
10979
10980         rm -rf $DIR/$tdir
10981
10982         $LCTL set_param fail_loc=0
10983 }
10984 run_test 182 "Disable MDC RPCs semaphore wouldn't crash client ================"
10985
10986 test_183() { # LU-2275
10987         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10988         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
10989                 skip "Need MDS version at least 2.3.56" && return
10990
10991         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10992         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
10993         echo aaa > $DIR/$tdir/$tfile
10994
10995 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
10996         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
10997
10998         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
10999         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
11000
11001         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
11002
11003         # Flush negative dentry cache
11004         touch $DIR/$tdir/$tfile
11005
11006         # We are not checking for any leaked references here, they'll
11007         # become evident next time we do cleanup with module unload.
11008         rm -rf $DIR/$tdir
11009 }
11010 run_test 183 "No crash or request leak in case of strange dispositions ========"
11011
11012 # test suite 184 is for LU-2016, LU-2017
11013 test_184a() {
11014         check_swap_layouts_support && return 0
11015
11016         dir0=$DIR/$tdir/$testnum
11017         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
11018         ref1=/etc/passwd
11019         ref2=/etc/group
11020         file1=$dir0/f1
11021         file2=$dir0/f2
11022         $SETSTRIPE -c1 $file1
11023         cp $ref1 $file1
11024         $SETSTRIPE -c2 $file2
11025         cp $ref2 $file2
11026         gen1=$($GETSTRIPE -g $file1)
11027         gen2=$($GETSTRIPE -g $file2)
11028
11029         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
11030         gen=$($GETSTRIPE -g $file1)
11031         [[ $gen1 != $gen ]] ||
11032                 "Layout generation on $file1 does not change"
11033         gen=$($GETSTRIPE -g $file2)
11034         [[ $gen2 != $gen ]] ||
11035                 "Layout generation on $file2 does not change"
11036
11037         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
11038         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
11039 }
11040 run_test 184a "Basic layout swap"
11041
11042 test_184b() {
11043         check_swap_layouts_support && return 0
11044
11045         dir0=$DIR/$tdir/$testnum
11046         mkdir -p $dir0 || error "creating dir $dir0"
11047         file1=$dir0/f1
11048         file2=$dir0/f2
11049         file3=$dir0/f3
11050         dir1=$dir0/d1
11051         dir2=$dir0/d2
11052         mkdir $dir1 $dir2
11053         $SETSTRIPE -c1 $file1
11054         $SETSTRIPE -c2 $file2
11055         $SETSTRIPE -c1 $file3
11056         chown $RUNAS_ID $file3
11057         gen1=$($GETSTRIPE -g $file1)
11058         gen2=$($GETSTRIPE -g $file2)
11059
11060         $LFS swap_layouts $dir1 $dir2 &&
11061                 error "swap of directories layouts should fail"
11062         $LFS swap_layouts $dir1 $file1 &&
11063                 error "swap of directory and file layouts should fail"
11064         $RUNAS $LFS swap_layouts $file1 $file2 &&
11065                 error "swap of file we cannot write should fail"
11066         $LFS swap_layouts $file1 $file3 &&
11067                 error "swap of file with different owner should fail"
11068         /bin/true # to clear error code
11069 }
11070 run_test 184b "Forbidden layout swap (will generate errors)"
11071
11072 test_184c() {
11073         check_swap_layouts_support && return 0
11074
11075         local dir0=$DIR/$tdir/$testnum
11076         mkdir -p $dir0 || error "creating dir $dir0"
11077
11078         local ref1=$dir0/ref1
11079         local ref2=$dir0/ref2
11080         local file1=$dir0/file1
11081         local file2=$dir0/file2
11082         # create a file large enough for the concurent test
11083         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
11084         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
11085         echo "ref file size: ref1($(stat -c %s $ref1))," \
11086              "ref2($(stat -c %s $ref2))"
11087
11088         cp $ref2 $file2
11089         dd if=$ref1 of=$file1 bs=16k &
11090         local DD_PID=$!
11091
11092         # Make sure dd starts to copy file
11093         while [ ! -f $file1 ]; do sleep 0.1; done
11094
11095         $LFS swap_layouts $file1 $file2
11096         local rc=$?
11097         wait $DD_PID
11098         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
11099         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
11100
11101         # how many bytes copied before swapping layout
11102         local copied=$(stat -c %s $file2)
11103         local remaining=$(stat -c %s $ref1)
11104         remaining=$((remaining - copied))
11105         echo "Copied $copied bytes before swapping layout..."
11106
11107         cmp -n $copied $file1 $ref2 | grep differ &&
11108                 error "Content mismatch [0, $copied) of ref2 and file1"
11109         cmp -n $copied $file2 $ref1 ||
11110                 error "Content mismatch [0, $copied) of ref1 and file2"
11111         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
11112                 error "Content mismatch [$copied, EOF) of ref1 and file1"
11113
11114         # clean up
11115         rm -f $ref1 $ref2 $file1 $file2
11116 }
11117 run_test 184c "Concurrent write and layout swap"
11118
11119 test_184d() {
11120         check_swap_layouts_support && return 0
11121         [ -z "$(which getfattr 2>/dev/null)" ] &&
11122                 skip "no getfattr command" && return 0
11123
11124         local file1=$DIR/$tdir/$tfile-1
11125         local file2=$DIR/$tdir/$tfile-2
11126         local file3=$DIR/$tdir/$tfile-3
11127         local lovea1
11128         local lovea2
11129
11130         mkdir -p $DIR/$tdir
11131         touch $file1 || error "create $file1 failed"
11132         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11133                 error "create $file2 failed"
11134         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11135                 error "create $file3 failed"
11136         lovea1=$($LFS getstripe $file1 | sed 1d)
11137
11138         $LFS swap_layouts $file2 $file3 ||
11139                 error "swap $file2 $file3 layouts failed"
11140         $LFS swap_layouts $file1 $file2 ||
11141                 error "swap $file1 $file2 layouts failed"
11142
11143         lovea2=$($LFS getstripe $file2 | sed 1d)
11144         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
11145
11146         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11147         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
11148 }
11149 run_test 184d "allow stripeless layouts swap"
11150
11151 test_184e() {
11152         check_swap_layouts_support && return 0
11153         [ -z "$(which getfattr 2>/dev/null)" ] &&
11154                 skip "no getfattr command" && return 0
11155
11156         local file1=$DIR/$tdir/$tfile-1
11157         local file2=$DIR/$tdir/$tfile-2
11158         local file3=$DIR/$tdir/$tfile-3
11159         local lovea
11160
11161         mkdir -p $DIR/$tdir
11162         touch $file1 || error "create $file1 failed"
11163         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
11164                 error "create $file2 failed"
11165         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
11166                 error "create $file3 failed"
11167
11168         $LFS swap_layouts $file1 $file2 ||
11169                 error "swap $file1 $file2 layouts failed"
11170
11171         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
11172         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
11173
11174         echo 123 > $file1 || error "Should be able to write into $file1"
11175
11176         $LFS swap_layouts $file1 $file3 ||
11177                 error "swap $file1 $file3 layouts failed"
11178
11179         echo 123 > $file1 || error "Should be able to write into $file1"
11180
11181         rm -rf $file1 $file2 $file3
11182 }
11183 run_test 184e "Recreate layout after stripeless layout swaps"
11184
11185 test_185() { # LU-2441
11186         # LU-3553 - no volatile file support in old servers
11187         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
11188                 { skip "Need MDS version at least 2.3.60"; return 0; }
11189
11190         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11191         touch $DIR/$tdir/spoo
11192         local mtime1=$(stat -c "%Y" $DIR/$tdir)
11193         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
11194                 error "cannot create/write a volatile file"
11195         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
11196                 error "FID is still valid after close"
11197
11198         multiop_bg_pause $DIR/$tdir vVw4096_c
11199         local multi_pid=$!
11200
11201         local OLD_IFS=$IFS
11202         IFS=":"
11203         local fidv=($fid)
11204         IFS=$OLD_IFS
11205         # assume that the next FID for this client is sequential, since stdout
11206         # is unfortunately eaten by multiop_bg_pause
11207         local n=$((${fidv[1]} + 1))
11208         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
11209         $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
11210                 error "FID is missing before close"
11211         kill -USR1 $multi_pid
11212         # 1 second delay, so if mtime change we will see it
11213         sleep 1
11214         local mtime2=$(stat -c "%Y" $DIR/$tdir)
11215         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
11216 }
11217 run_test 185 "Volatile file support"
11218
11219 test_187a() {
11220         local dir0=$DIR/$tdir/$testnum
11221         mkdir -p $dir0 || error "creating dir $dir0"
11222
11223         local file=$dir0/file1
11224         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
11225         local dv1=$($LFS data_version $file)
11226         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
11227         local dv2=$($LFS data_version $file)
11228         [[ $dv1 != $dv2 ]] ||
11229                 error "data version did not change on write $dv1 == $dv2"
11230
11231         # clean up
11232         rm -f $file1
11233 }
11234 run_test 187a "Test data version change"
11235
11236 test_187b() {
11237         local dir0=$DIR/$tdir/$testnum
11238         mkdir -p $dir0 || error "creating dir $dir0"
11239
11240         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
11241         [[ ${DV[0]} != ${DV[1]} ]] ||
11242                 error "data version did not change on write"\
11243                       " ${DV[0]} == ${DV[1]}"
11244
11245         # clean up
11246         rm -f $file1
11247 }
11248 run_test 187b "Test data version change on volatile file"
11249
11250 test_200() {
11251         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11252         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11253
11254         local POOL=${POOL:-cea1}
11255         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
11256         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
11257         # Pool OST targets
11258         local first_ost=0
11259         local last_ost=$(($OSTCOUNT - 1))
11260         local ost_step=2
11261         local ost_list=$(seq $first_ost $ost_step $last_ost)
11262         local ost_range="$first_ost $last_ost $ost_step"
11263         local test_path=$POOL_ROOT/$POOL_DIR_NAME
11264         local file_dir=$POOL_ROOT/file_tst
11265         local subdir=$test_path/subdir
11266
11267         local rc=0
11268         while : ; do
11269                 # former test_200a test_200b
11270                 pool_add $POOL                          || { rc=$? ; break; }
11271                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
11272                 # former test_200c test_200d
11273                 mkdir -p $test_path
11274                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
11275                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
11276                 mkdir -p $subdir
11277                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
11278                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
11279                                                         || { rc=$? ; break; }
11280                 # former test_200e test_200f
11281                 local files=$((OSTCOUNT*3))
11282                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
11283                                                         || { rc=$? ; break; }
11284                 pool_create_files $POOL $file_dir $files "$ost_list" \
11285                                                         || { rc=$? ; break; }
11286                 # former test_200g test_200h
11287                 pool_lfs_df $POOL                       || { rc=$? ; break; }
11288                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
11289
11290                 # former test_201a test_201b test_201c
11291                 pool_remove_first_target $POOL          || { rc=$? ; break; }
11292
11293                 local f=$test_path/$tfile
11294                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
11295                 pool_remove $POOL $f                    || { rc=$? ; break; }
11296                 break
11297         done
11298
11299         cleanup_pools
11300         return $rc
11301 }
11302 run_test 200 "OST pools"
11303
11304 # usage: default_attr <count | size | offset>
11305 default_attr() {
11306         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
11307 }
11308
11309 # usage: check_default_stripe_attr
11310 check_default_stripe_attr() {
11311         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
11312         case $1 in
11313         --stripe-count|--count)
11314                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
11315         --stripe-size|--size)
11316                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
11317         --stripe-index|--index)
11318                 EXPECTED=-1;;
11319         *)
11320                 error "unknown getstripe attr '$1'"
11321         esac
11322
11323         [ $ACTUAL != $EXPECTED ] &&
11324                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
11325 }
11326
11327 test_204a() {
11328         test_mkdir -p $DIR/$tdir
11329         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
11330
11331         check_default_stripe_attr --stripe-count
11332         check_default_stripe_attr --stripe-size
11333         check_default_stripe_attr --stripe-index
11334
11335         return 0
11336 }
11337 run_test 204a "Print default stripe attributes ================="
11338
11339 test_204b() {
11340         test_mkdir -p $DIR/$tdir
11341         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11342
11343         check_default_stripe_attr --stripe-size
11344         check_default_stripe_attr --stripe-index
11345
11346         return 0
11347 }
11348 run_test 204b "Print default stripe size and offset  ==========="
11349
11350 test_204c() {
11351         test_mkdir -p $DIR/$tdir
11352         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11353
11354         check_default_stripe_attr --stripe-count
11355         check_default_stripe_attr --stripe-index
11356
11357         return 0
11358 }
11359 run_test 204c "Print default stripe count and offset ==========="
11360
11361 test_204d() {
11362         test_mkdir -p $DIR/$tdir
11363         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11364
11365         check_default_stripe_attr --stripe-count
11366         check_default_stripe_attr --stripe-size
11367
11368         return 0
11369 }
11370 run_test 204d "Print default stripe count and size ============="
11371
11372 test_204e() {
11373         test_mkdir -p $DIR/$tdir
11374         $SETSTRIPE -d $DIR/$tdir
11375
11376         check_default_stripe_attr --stripe-count --raw
11377         check_default_stripe_attr --stripe-size --raw
11378         check_default_stripe_attr --stripe-index --raw
11379
11380         return 0
11381 }
11382 run_test 204e "Print raw stripe attributes ================="
11383
11384 test_204f() {
11385         test_mkdir -p $DIR/$tdir
11386         $SETSTRIPE --stripe-count 1 $DIR/$tdir
11387
11388         check_default_stripe_attr --stripe-size --raw
11389         check_default_stripe_attr --stripe-index --raw
11390
11391         return 0
11392 }
11393 run_test 204f "Print raw stripe size and offset  ==========="
11394
11395 test_204g() {
11396         test_mkdir -p $DIR/$tdir
11397         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
11398
11399         check_default_stripe_attr --stripe-count --raw
11400         check_default_stripe_attr --stripe-index --raw
11401
11402         return 0
11403 }
11404 run_test 204g "Print raw stripe count and offset ==========="
11405
11406 test_204h() {
11407         test_mkdir -p $DIR/$tdir
11408         $SETSTRIPE --stripe-index 0 $DIR/$tdir
11409
11410         check_default_stripe_attr --stripe-count --raw
11411         check_default_stripe_attr --stripe-size --raw
11412
11413         return 0
11414 }
11415 run_test 204h "Print raw stripe count and size ============="
11416
11417 # Figure out which job scheduler is being used, if any,
11418 # or use a fake one
11419 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
11420         JOBENV=SLURM_JOB_ID
11421 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
11422         JOBENV=LSB_JOBID
11423 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
11424         JOBENV=PBS_JOBID
11425 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
11426         JOBENV=LOADL_STEP_ID
11427 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
11428         JOBENV=JOB_ID
11429 else
11430         JOBENV=FAKE_JOBID
11431 fi
11432
11433 verify_jobstats() {
11434         local cmd=$1
11435         local target=$2
11436
11437         # clear old jobstats
11438         do_facet $SINGLEMDS lctl set_param mdt.*.job_stats="clear"
11439         do_facet ost1 lctl set_param obdfilter.*.job_stats="clear"
11440
11441         # use a new JobID for this test, or we might see an old one
11442         [ "$JOBENV" = "FAKE_JOBID" ] && FAKE_JOBID=test_id.$testnum.$RANDOM
11443
11444         JOBVAL=${!JOBENV}
11445         log "Test: $cmd"
11446         log "Using JobID environment variable $JOBENV=$JOBVAL"
11447
11448         if [ $JOBENV = "FAKE_JOBID" ]; then
11449                 FAKE_JOBID=$JOBVAL $cmd
11450         else
11451                 $cmd
11452         fi
11453
11454         if [ "$target" = "mdt" -o "$target" = "both" ]; then
11455                 FACET="$SINGLEMDS" # will need to get MDS number for DNE
11456                 do_facet $FACET lctl get_param mdt.*.job_stats |
11457                         grep $JOBVAL || error "No job stats found on MDT $FACET"
11458         fi
11459         if [ "$target" = "ost" -o "$target" = "both" ]; then
11460                 FACET=ost1
11461                 do_facet $FACET lctl get_param obdfilter.*.job_stats |
11462                         grep $JOBVAL || error "No job stats found on OST $FACET"
11463         fi
11464 }
11465
11466 jobstats_set() {
11467         trap 0
11468         NEW_JOBENV=${1:-$OLD_JOBENV}
11469         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
11470         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
11471 }
11472
11473 test_205() { # Job stats
11474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11475         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
11476         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
11477                 skip "Server doesn't support jobstats" && return 0
11478         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
11479
11480         local cmd
11481         OLD_JOBENV=$($LCTL get_param -n jobid_var)
11482         if [ $OLD_JOBENV != $JOBENV ]; then
11483                 jobstats_set $JOBENV
11484                 trap jobstats_set EXIT
11485         fi
11486
11487         local user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11488                      changelog_register -n)
11489         echo "Registered as changelog user $user"
11490
11491         # mkdir
11492         cmd="mkdir $DIR/$tfile"
11493         verify_jobstats "$cmd" "mdt"
11494         # rmdir
11495         cmd="rm -fr $DIR/$tfile"
11496         verify_jobstats "$cmd" "mdt"
11497         # mknod
11498         cmd="mknod $DIR/$tfile c 1 3"
11499         verify_jobstats "$cmd" "mdt"
11500         # unlink
11501         cmd="rm -f $DIR/$tfile"
11502         verify_jobstats "$cmd" "mdt"
11503         # open & close
11504         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
11505         verify_jobstats "$cmd" "mdt"
11506         # setattr
11507         cmd="touch $DIR/$tfile"
11508         verify_jobstats "$cmd" "both"
11509         # write
11510         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
11511         verify_jobstats "$cmd" "ost"
11512         # read
11513         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
11514         verify_jobstats "$cmd" "ost"
11515         # truncate
11516         cmd="$TRUNCATE $DIR/$tfile 0"
11517         verify_jobstats "$cmd" "both"
11518         # rename
11519         cmd="mv -f $DIR/$tfile $DIR/jobstats_test_rename"
11520         verify_jobstats "$cmd" "mdt"
11521
11522         # Ensure that jobid are present in changelog (if supported by MDS)
11523         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
11524         then
11525                 $LFS changelog $MDT0 | tail -9
11526                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
11527                 [ $jobids -eq 9 ] ||
11528                         error "Wrong changelog jobid count $jobids != 9"
11529
11530                 # LU-5862
11531                 JOBENV="disable"
11532                 jobstats_set $JOBENV
11533                 touch $DIR/$tfile
11534                 $LFS changelog $MDT0 | tail -1
11535                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
11536                 [ $jobids -eq 0 ] ||
11537                         error "Unexpected jobids when jobid_var=$JOBENV"
11538         fi
11539
11540         # cleanup
11541         rm -f $DIR/jobstats_test_rename
11542
11543         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
11544 }
11545 run_test 205 "Verify job stats"
11546
11547 # LU-1480, LU-1773 and LU-1657
11548 test_206() {
11549         mkdir -p $DIR/$tdir
11550         $SETSTRIPE -c -1 $DIR/$tdir
11551 #define OBD_FAIL_LOV_INIT 0x1403
11552         $LCTL set_param fail_loc=0xa0001403
11553         $LCTL set_param fail_val=1
11554         touch $DIR/$tdir/$tfile || true
11555 }
11556 run_test 206 "fail lov_init_raid0() doesn't lbug"
11557
11558 test_207a() {
11559         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11560         local fsz=`stat -c %s $DIR/$tfile`
11561         cancel_lru_locks mdc
11562
11563         # do not return layout in getattr intent
11564 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
11565         $LCTL set_param fail_loc=0x170
11566         local sz=`stat -c %s $DIR/$tfile`
11567
11568         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
11569
11570         rm -rf $DIR/$tfile
11571 }
11572 run_test 207a "can refresh layout at glimpse"
11573
11574 test_207b() {
11575         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
11576         local cksum=`md5sum $DIR/$tfile`
11577         local fsz=`stat -c %s $DIR/$tfile`
11578         cancel_lru_locks mdc
11579         cancel_lru_locks osc
11580
11581         # do not return layout in getattr intent
11582 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
11583         $LCTL set_param fail_loc=0x171
11584
11585         # it will refresh layout after the file is opened but before read issues
11586         echo checksum is "$cksum"
11587         echo "$cksum" |md5sum -c --quiet || error "file differs"
11588
11589         rm -rf $DIR/$tfile
11590 }
11591 run_test 207b "can refresh layout at open"
11592
11593 test_208() {
11594         # FIXME: in this test suite, only RD lease is used. This is okay
11595         # for now as only exclusive open is supported. After generic lease
11596         # is done, this test suite should be revised. - Jinshan
11597
11598         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
11599                 { skip "Need MDS version at least 2.4.52"; return 0; }
11600         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11601
11602         echo "==== test 1: verify get lease work"
11603         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
11604
11605         echo "==== test 2: verify lease can be broken by upcoming open"
11606         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11607         local PID=$!
11608         sleep 1
11609
11610         $MULTIOP $DIR/$tfile oO_RDONLY:c
11611         kill -USR1 $PID && wait $PID || error "break lease error"
11612
11613         echo "==== test 3: verify lease can't be granted if an open already exists"
11614         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
11615         local PID=$!
11616         sleep 1
11617
11618         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
11619         kill -USR1 $PID && wait $PID || error "open file error"
11620
11621         echo "==== test 4: lease can sustain over recovery"
11622         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
11623         PID=$!
11624         sleep 1
11625
11626         fail mds1
11627
11628         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
11629
11630         echo "==== test 5: lease broken can't be regained by replay"
11631         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
11632         PID=$!
11633         sleep 1
11634
11635         # open file to break lease and then recovery
11636         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
11637         fail mds1
11638
11639         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
11640
11641         rm -f $DIR/$tfile
11642 }
11643 run_test 208 "Exclusive open"
11644
11645 test_209() {
11646         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
11647                 skip_env "must have disp_stripe" && return
11648
11649         touch $DIR/$tfile
11650         sync; sleep 5; sync;
11651
11652         echo 3 > /proc/sys/vm/drop_caches
11653         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11654
11655         # open/close 500 times
11656         for i in $(seq 500); do
11657                 cat $DIR/$tfile
11658         done
11659
11660         echo 3 > /proc/sys/vm/drop_caches
11661         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
11662
11663         echo "before: $req_before, after: $req_after"
11664         [ $((req_after - req_before)) -ge 300 ] &&
11665                 error "open/close requests are not freed"
11666         return 0
11667 }
11668 run_test 209 "read-only open/close requests should be freed promptly"
11669
11670 test_212() {
11671         size=`date +%s`
11672         size=$((size % 8192 + 1))
11673         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
11674         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
11675         rm -f $DIR/f212 $DIR/f212.xyz
11676 }
11677 run_test 212 "Sendfile test ============================================"
11678
11679 test_213() {
11680         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
11681         cancel_lru_locks osc
11682         lctl set_param fail_loc=0x8000040f
11683         # generate a read lock
11684         cat $DIR/$tfile > /dev/null
11685         # write to the file, it will try to cancel the above read lock.
11686         cat /etc/hosts >> $DIR/$tfile
11687 }
11688 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
11689
11690 test_214() { # for bug 20133
11691         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
11692         for (( i=0; i < 340; i++ )) ; do
11693                 touch $DIR/$tdir/d214c/a$i
11694         done
11695
11696         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
11697         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
11698         ls $DIR/d214c || error "ls $DIR/d214c failed"
11699         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
11700         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
11701 }
11702 run_test 214 "hash-indexed directory test - bug 20133"
11703
11704 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
11705 create_lnet_proc_files() {
11706         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
11707         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
11708
11709         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
11710         rm -f "$TMP/lnet_$1.sys_tmp"
11711 }
11712
11713 # counterpart of create_lnet_proc_files
11714 remove_lnet_proc_files() {
11715         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
11716 }
11717
11718 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11719 # 3rd arg as regexp for body
11720 check_lnet_proc_stats() {
11721         local l=$(cat "$TMP/lnet_$1" |wc -l)
11722         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
11723
11724         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
11725 }
11726
11727 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
11728 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
11729 # optional and can be regexp for 2nd line (lnet.routes case)
11730 check_lnet_proc_entry() {
11731         local blp=2          # blp stands for 'position of 1st line of body'
11732         [ -z "$5" ] || blp=3 # lnet.routes case
11733
11734         local l=$(cat "$TMP/lnet_$1" |wc -l)
11735         # subtracting one from $blp because the body can be empty
11736         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
11737
11738         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
11739                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
11740
11741         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
11742                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
11743
11744         # bail out if any unexpected line happened
11745         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
11746         [ "$?" != 0 ] || error "$2 misformatted"
11747 }
11748
11749 test_215() { # for bugs 18102, 21079, 21517
11750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11751         local N='(0|[1-9][0-9]*)'       # non-negative numeric
11752         local P='[1-9][0-9]*'           # positive numeric
11753         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
11754         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
11755         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
11756         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
11757
11758         local L1 # regexp for 1st line
11759         local L2 # regexp for 2nd line (optional)
11760         local BR # regexp for the rest (body)
11761
11762         # lnet.stats should look as 11 space-separated non-negative numerics
11763         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
11764         create_lnet_proc_files "stats"
11765         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
11766         remove_lnet_proc_files "stats"
11767
11768         # lnet.routes should look like this:
11769         # Routing disabled/enabled
11770         # net hops priority state router
11771         # where net is a string like tcp0, hops > 0, priority >= 0,
11772         # state is up/down,
11773         # router is a string like 192.168.1.1@tcp2
11774         L1="^Routing (disabled|enabled)$"
11775         L2="^net +hops +priority +state +router$"
11776         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
11777         create_lnet_proc_files "routes"
11778         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
11779         remove_lnet_proc_files "routes"
11780
11781         # lnet.routers should look like this:
11782         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
11783         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
11784         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
11785         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
11786         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
11787         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
11788         create_lnet_proc_files "routers"
11789         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
11790         remove_lnet_proc_files "routers"
11791
11792         # lnet.peers should look like this:
11793         # nid refs state last max rtr min tx min queue
11794         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
11795         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
11796         # numeric (0 or >0 or <0), queue >= 0.
11797         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
11798         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
11799         create_lnet_proc_files "peers"
11800         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
11801         remove_lnet_proc_files "peers"
11802
11803         # lnet.buffers  should look like this:
11804         # pages count credits min
11805         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
11806         L1="^pages +count +credits +min$"
11807         BR="^ +$N +$N +$I +$I$"
11808         create_lnet_proc_files "buffers"
11809         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
11810         remove_lnet_proc_files "buffers"
11811
11812         # lnet.nis should look like this:
11813         # nid status alive refs peer rtr max tx min
11814         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
11815         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
11816         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
11817         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
11818         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
11819         create_lnet_proc_files "nis"
11820         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
11821         remove_lnet_proc_files "nis"
11822
11823         # can we successfully write to lnet.stats?
11824         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
11825         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
11826 }
11827 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
11828
11829 test_216() { # bug 20317
11830         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11831         remote_ost_nodsh && skip "remote OST with nodsh" && return
11832
11833         local node
11834         local facets=$(get_facets OST)
11835         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11836
11837         save_lustre_params client "osc.*.contention_seconds" > $p
11838         save_lustre_params $facets \
11839                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
11840         save_lustre_params $facets \
11841                 "ldlm.namespaces.filter-*.contended_locks" >> $p
11842         save_lustre_params $facets \
11843                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
11844         clear_osc_stats
11845
11846         # agressive lockless i/o settings
11847         for node in $(osts_nodes); do
11848                 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'
11849         done
11850         lctl set_param -n osc.*.contention_seconds 60
11851
11852         $DIRECTIO write $DIR/$tfile 0 10 4096
11853         $CHECKSTAT -s 40960 $DIR/$tfile
11854
11855         # disable lockless i/o
11856         for node in $(osts_nodes); do
11857                 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'
11858         done
11859         lctl set_param -n osc.*.contention_seconds 0
11860         clear_osc_stats
11861
11862         dd if=/dev/zero of=$DIR/$tfile count=0
11863         $CHECKSTAT -s 0 $DIR/$tfile
11864
11865         restore_lustre_params <$p
11866         rm -f $p
11867         rm $DIR/$tfile
11868 }
11869 run_test 216 "check lockless direct write works and updates file size and kms correctly"
11870
11871 test_217() { # bug 22430
11872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11873         local node
11874         local nid
11875
11876         for node in $(nodes_list); do
11877                 nid=$(host_nids_address $node $NETTYPE)
11878                 if [[ $nid = *-* ]] ; then
11879                         echo "lctl ping $nid@$NETTYPE"
11880                         lctl ping $nid@$NETTYPE
11881                 else
11882                         echo "skipping $node (no hyphen detected)"
11883                 fi
11884         done
11885 }
11886 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
11887
11888 test_218() {
11889        # do directio so as not to populate the page cache
11890        log "creating a 10 Mb file"
11891        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11892        log "starting reads"
11893        dd if=$DIR/$tfile of=/dev/null bs=4096 &
11894        log "truncating the file"
11895        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11896        log "killing dd"
11897        kill %+ || true # reads might have finished
11898        echo "wait until dd is finished"
11899        wait
11900        log "removing the temporary file"
11901        rm -rf $DIR/$tfile || error "tmp file removal failed"
11902 }
11903 run_test 218 "parallel read and truncate should not deadlock ======================="
11904
11905 test_219() {
11906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11907         # write one partial page
11908         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
11909         # set no grant so vvp_io_commit_write will do sync write
11910         $LCTL set_param fail_loc=0x411
11911         # write a full page at the end of file
11912         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
11913
11914         $LCTL set_param fail_loc=0
11915         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
11916         $LCTL set_param fail_loc=0x411
11917         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
11918
11919         # LU-4201
11920         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
11921         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
11922 }
11923 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
11924
11925 test_220() { #LU-325
11926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11927         remote_ost_nodsh && skip "remote OST with nodsh" && return
11928         local OSTIDX=0
11929
11930         test_mkdir -p $DIR/$tdir
11931         local OST=$(lfs osts | grep ${OSTIDX}": " | \
11932                 awk '{print $2}' | sed -e 's/_UUID$//')
11933
11934         # on the mdt's osc
11935         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
11936         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
11937                         osc.$mdtosc_proc1.prealloc_last_id)
11938         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
11939                         osc.$mdtosc_proc1.prealloc_next_id)
11940
11941         $LFS df -i
11942
11943         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
11944         #define OBD_FAIL_OST_ENOINO              0x229
11945         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
11946         do_facet mgs $LCTL pool_new $FSNAME.$TESTNAME || return 1
11947         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
11948
11949         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
11950
11951         MDSOBJS=$((last_id - next_id))
11952         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
11953
11954         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
11955         echo "OST still has $count kbytes free"
11956
11957         echo "create $MDSOBJS files @next_id..."
11958         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
11959
11960         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11961                         osc.$mdtosc_proc1.prealloc_last_id)
11962         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
11963                         osc.$mdtosc_proc1.prealloc_next_id)
11964
11965         echo "after creation, last_id=$last_id2, next_id=$next_id2"
11966         $LFS df -i
11967
11968         echo "cleanup..."
11969
11970         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
11971         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
11972
11973         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST || return 4
11974         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME || return 5
11975         echo "unlink $MDSOBJS files @$next_id..."
11976         unlinkmany $DIR/$tdir/f $MDSOBJS || return 6
11977 }
11978 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
11979
11980 test_221() {
11981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11982         dd if=`which date` of=$MOUNT/date oflag=sync
11983         chmod +x $MOUNT/date
11984
11985         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
11986         $LCTL set_param fail_loc=0x80001401
11987
11988         $MOUNT/date > /dev/null
11989         rm -f $MOUNT/date
11990 }
11991 run_test 221 "make sure fault and truncate race to not cause OOM"
11992
11993 test_222a () {
11994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11995        rm -rf $DIR/$tdir
11996        test_mkdir -p $DIR/$tdir
11997        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
11998        createmany -o $DIR/$tdir/$tfile 10
11999        cancel_lru_locks mdc
12000        cancel_lru_locks osc
12001        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12002        $LCTL set_param fail_loc=0x31a
12003        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
12004        $LCTL set_param fail_loc=0
12005        rm -r $DIR/$tdir
12006 }
12007 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
12008
12009 test_222b () {
12010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12011        rm -rf $DIR/$tdir
12012        test_mkdir -p $DIR/$tdir
12013        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12014        createmany -o $DIR/$tdir/$tfile 10
12015        cancel_lru_locks mdc
12016        cancel_lru_locks osc
12017        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
12018        $LCTL set_param fail_loc=0x31a
12019        rm -r $DIR/$tdir || "AGL for rmdir failed"
12020        $LCTL set_param fail_loc=0
12021 }
12022 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
12023
12024 test_223 () {
12025         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12026        rm -rf $DIR/$tdir
12027        test_mkdir -p $DIR/$tdir
12028        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
12029        createmany -o $DIR/$tdir/$tfile 10
12030        cancel_lru_locks mdc
12031        cancel_lru_locks osc
12032        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
12033        $LCTL set_param fail_loc=0x31b
12034        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
12035        $LCTL set_param fail_loc=0
12036        rm -r $DIR/$tdir
12037 }
12038 run_test 223 "osc reenqueue if without AGL lock granted ======================="
12039
12040 test_224a() { # LU-1039, MRP-303
12041         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12042         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
12043         $LCTL set_param fail_loc=0x508
12044         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
12045         $LCTL set_param fail_loc=0
12046         df $DIR
12047 }
12048 run_test 224a "Don't panic on bulk IO failure"
12049
12050 test_224b() { # LU-1039, MRP-303
12051         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12052         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
12053         cancel_lru_locks osc
12054         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
12055         $LCTL set_param fail_loc=0x515
12056         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
12057         $LCTL set_param fail_loc=0
12058         df $DIR
12059 }
12060 run_test 224b "Don't panic on bulk IO failure"
12061
12062 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
12063 test_225a () {
12064         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12065         if [ -z ${MDSSURVEY} ]; then
12066               skip_env "mds-survey not found" && return
12067         fi
12068
12069         [ $MDSCOUNT -ge 2 ] &&
12070                 skip "skipping now for more than one MDT" && return
12071
12072        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12073             { skip "Need MDS version at least 2.2.51"; return; }
12074
12075        local mds=$(facet_host $SINGLEMDS)
12076        local target=$(do_nodes $mds 'lctl dl' | \
12077                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12078
12079        local cmd1="file_count=1000 thrhi=4"
12080        local cmd2="dir_count=2 layer=mdd stripe_count=0"
12081        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12082        local cmd="$cmd1 $cmd2 $cmd3"
12083
12084        rm -f ${TMP}/mds_survey*
12085        echo + $cmd
12086        eval $cmd || error "mds-survey with zero-stripe failed"
12087        cat ${TMP}/mds_survey*
12088        rm -f ${TMP}/mds_survey*
12089 }
12090 run_test 225a "Metadata survey sanity with zero-stripe"
12091
12092 test_225b () {
12093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12094
12095         if [ -z ${MDSSURVEY} ]; then
12096               skip_env "mds-survey not found" && return
12097         fi
12098         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
12099             { skip "Need MDS version at least 2.2.51"; return; }
12100
12101         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
12102               skip_env "Need to mount OST to test" && return
12103         fi
12104
12105         [ $MDSCOUNT -ge 2 ] &&
12106                 skip "skipping now for more than one MDT" && return
12107        local mds=$(facet_host $SINGLEMDS)
12108        local target=$(do_nodes $mds 'lctl dl' | \
12109                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
12110
12111        local cmd1="file_count=1000 thrhi=4"
12112        local cmd2="dir_count=2 layer=mdd stripe_count=1"
12113        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
12114        local cmd="$cmd1 $cmd2 $cmd3"
12115
12116        rm -f ${TMP}/mds_survey*
12117        echo + $cmd
12118        eval $cmd || error "mds-survey with stripe_count failed"
12119        cat ${TMP}/mds_survey*
12120        rm -f ${TMP}/mds_survey*
12121 }
12122 run_test 225b "Metadata survey sanity with stripe_count = 1"
12123
12124 mcreate_path2fid () {
12125         local mode=$1
12126         local major=$2
12127         local minor=$3
12128         local name=$4
12129         local desc=$5
12130         local path=$DIR/$tdir/$name
12131         local fid
12132         local rc
12133         local fid_path
12134
12135         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
12136                 error "cannot create $desc"
12137
12138         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
12139         rc=$?
12140         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
12141
12142         fid_path=$($LFS fid2path $MOUNT $fid)
12143         rc=$?
12144         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
12145
12146         [ "$path" == "$fid_path" ] ||
12147                 error "fid2path returned $fid_path, expected $path"
12148
12149         echo "pass with $path and $fid"
12150 }
12151
12152 test_226a () {
12153         rm -rf $DIR/$tdir
12154         mkdir -p $DIR/$tdir
12155
12156         mcreate_path2fid 0010666 0 0 fifo "FIFO"
12157         mcreate_path2fid 0020666 1 3 null "character special file (null)"
12158         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
12159         mcreate_path2fid 0040666 0 0 dir "directory"
12160         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
12161         mcreate_path2fid 0100666 0 0 file "regular file"
12162         mcreate_path2fid 0120666 0 0 link "symbolic link"
12163         mcreate_path2fid 0140666 0 0 sock "socket"
12164 }
12165 run_test 226a "call path2fid and fid2path on files of all type"
12166
12167 test_226b () {
12168         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12169         rm -rf $DIR/$tdir
12170         local MDTIDX=1
12171
12172         mkdir -p $DIR/$tdir
12173         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
12174                 error "create remote directory failed"
12175         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
12176         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
12177                                 "character special file (null)"
12178         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
12179                                 "character special file (no device)"
12180         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
12181         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
12182                                 "block special file (loop)"
12183         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
12184         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
12185         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
12186 }
12187 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
12188
12189 # LU-1299 Executing or running ldd on a truncated executable does not
12190 # cause an out-of-memory condition.
12191 test_227() {
12192         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12193         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
12194         dd if=$(which date) of=$MOUNT/date bs=1k count=1
12195         chmod +x $MOUNT/date
12196
12197         $MOUNT/date > /dev/null
12198         ldd $MOUNT/date > /dev/null
12199         rm -f $MOUNT/date
12200 }
12201 run_test 227 "running truncated executable does not cause OOM"
12202
12203 # LU-1512 try to reuse idle OI blocks
12204 test_228a() {
12205         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12206         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12207         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12208                 skip "non-ldiskfs backend" && return
12209
12210         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12211         local myDIR=$DIR/$tdir
12212
12213         mkdir -p $myDIR
12214         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12215         $LCTL set_param fail_loc=0x80001002
12216         createmany -o $myDIR/t- 10000
12217         $LCTL set_param fail_loc=0
12218         # The guard is current the largest FID holder
12219         touch $myDIR/guard
12220         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12221                     tr -d '[')
12222         local IDX=$(($SEQ % 64))
12223
12224         do_facet $SINGLEMDS sync
12225         # Make sure journal flushed.
12226         sleep 6
12227         local blk1=$(do_facet $SINGLEMDS \
12228                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12229                      grep Blockcount | awk '{print $4}')
12230
12231         # Remove old files, some OI blocks will become idle.
12232         unlinkmany $myDIR/t- 10000
12233         # Create new files, idle OI blocks should be reused.
12234         createmany -o $myDIR/t- 2000
12235         do_facet $SINGLEMDS sync
12236         # Make sure journal flushed.
12237         sleep 6
12238         local blk2=$(do_facet $SINGLEMDS \
12239                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12240                      grep Blockcount | awk '{print $4}')
12241
12242         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12243 }
12244 run_test 228a "try to reuse idle OI blocks"
12245
12246 test_228b() {
12247         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12248         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12249         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12250                 skip "non-ldiskfs backend" && return
12251
12252         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12253         local myDIR=$DIR/$tdir
12254
12255         mkdir -p $myDIR
12256         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12257         $LCTL set_param fail_loc=0x80001002
12258         createmany -o $myDIR/t- 10000
12259         $LCTL set_param fail_loc=0
12260         # The guard is current the largest FID holder
12261         touch $myDIR/guard
12262         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12263                     tr -d '[')
12264         local IDX=$(($SEQ % 64))
12265
12266         do_facet $SINGLEMDS sync
12267         # Make sure journal flushed.
12268         sleep 6
12269         local blk1=$(do_facet $SINGLEMDS \
12270                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12271                      grep Blockcount | awk '{print $4}')
12272
12273         # Remove old files, some OI blocks will become idle.
12274         unlinkmany $myDIR/t- 10000
12275
12276         # stop the MDT
12277         stop $SINGLEMDS || error "Fail to stop MDT."
12278         # remount the MDT
12279         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
12280
12281         df $MOUNT || error "Fail to df."
12282         # Create new files, idle OI blocks should be reused.
12283         createmany -o $myDIR/t- 2000
12284         do_facet $SINGLEMDS sync
12285         # Make sure journal flushed.
12286         sleep 6
12287         local blk2=$(do_facet $SINGLEMDS \
12288                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12289                      grep Blockcount | awk '{print $4}')
12290
12291         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12292 }
12293 run_test 228b "idle OI blocks can be reused after MDT restart"
12294
12295 #LU-1881
12296 test_228c() {
12297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12298         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12299         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
12300                 skip "non-ldiskfs backend" && return
12301
12302         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
12303         local myDIR=$DIR/$tdir
12304
12305         mkdir -p $myDIR
12306         #define OBD_FAIL_SEQ_EXHAUST             0x1002
12307         $LCTL set_param fail_loc=0x80001002
12308         # 20000 files can guarantee there are index nodes in the OI file
12309         createmany -o $myDIR/t- 20000
12310         $LCTL set_param fail_loc=0
12311         # The guard is current the largest FID holder
12312         touch $myDIR/guard
12313         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
12314                     tr -d '[')
12315         local IDX=$(($SEQ % 64))
12316
12317         do_facet $SINGLEMDS sync
12318         # Make sure journal flushed.
12319         sleep 6
12320         local blk1=$(do_facet $SINGLEMDS \
12321                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12322                      grep Blockcount | awk '{print $4}')
12323
12324         # Remove old files, some OI blocks will become idle.
12325         unlinkmany $myDIR/t- 20000
12326         rm -f $myDIR/guard
12327         # The OI file should become empty now
12328
12329         # Create new files, idle OI blocks should be reused.
12330         createmany -o $myDIR/t- 2000
12331         do_facet $SINGLEMDS sync
12332         # Make sure journal flushed.
12333         sleep 6
12334         local blk2=$(do_facet $SINGLEMDS \
12335                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
12336                      grep Blockcount | awk '{print $4}')
12337
12338         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
12339 }
12340 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
12341
12342 test_229() { # LU-2482, LU-3448
12343         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
12344                 skip "No HSM support on MDS of $(get_lustre_version)," \
12345                          "need 2.4.53 at least" && return
12346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12347         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
12348
12349         rm -f $DIR/$tfile
12350
12351         # Create a file with a released layout and stripe count 2.
12352         $MULTIOP $DIR/$tfile H2c ||
12353                 error "failed to create file with released layout"
12354
12355         $GETSTRIPE -v $DIR/$tfile
12356
12357         local pattern=$($GETSTRIPE -L $DIR/$tfile)
12358         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
12359
12360         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
12361         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
12362         stat $DIR/$tfile || error "failed to stat released file"
12363
12364         chown $RUNAS_ID $DIR/$tfile ||
12365                 error "chown $RUNAS_ID $DIR/$tfile failed"
12366
12367         chgrp $RUNAS_ID $DIR/$tfile ||
12368                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
12369
12370         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
12371         rm $DIR/$tfile || error "failed to remove released file"
12372 }
12373 run_test 229 "getstripe/stat/rm/attr changes work on released files"
12374
12375 test_230a() {
12376         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12377         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12378         local MDTIDX=1
12379
12380         mkdir -p $DIR/$tdir/test_230_local
12381         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
12382         [ $mdt_idx -ne 0 ] &&
12383                 error "create local directory on wrong MDT $mdt_idx"
12384
12385         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
12386                         error "create remote directory failed"
12387         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
12388         [ $mdt_idx -ne $MDTIDX ] &&
12389                 error "create remote directory on wrong MDT $mdt_idx"
12390
12391         createmany -o $DIR/$tdir/test_230/t- 10 ||
12392                 error "create files on remote directory failed"
12393         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
12394         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
12395         rm -r $DIR/$tdir || error "unlink remote directory failed"
12396 }
12397 run_test 230a "Create remote directory and files under the remote directory"
12398
12399 test_230b() {
12400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12401         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12402         local MDTIDX=1
12403         local mdt_index
12404         local i
12405         local file
12406         local pid
12407         local stripe_count
12408         local migrate_dir=$DIR/$tdir/migrate_dir
12409         local other_dir=$DIR/$tdir/other_dir
12410
12411         mkdir -p $migrate_dir
12412         mkdir -p $other_dir
12413         for ((i=0; i<10; i++)); do
12414                 mkdir -p $migrate_dir/dir_${i}
12415                 createmany -o $migrate_dir/dir_${i}/f 10 ||
12416                         error "create files under remote dir failed $i"
12417         done
12418
12419         cp /etc/passwd $migrate_dir/$tfile
12420         cp /etc/passwd $other_dir/$tfile
12421         chattr +SAD $migrate_dir
12422         chattr +SAD $migrate_dir/$tfile
12423
12424         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12425         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12426         local old_dir_mode=$(stat -c%f $migrate_dir)
12427         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
12428
12429         mkdir -p $migrate_dir/dir_default_stripe2
12430         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
12431         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
12432
12433         mkdir -p $other_dir
12434         ln $migrate_dir/$tfile $other_dir/luna
12435         ln $migrate_dir/$tfile $migrate_dir/sofia
12436         ln $other_dir/$tfile $migrate_dir/david
12437         ln -s $migrate_dir/$tfile $other_dir/zachary
12438         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
12439         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
12440
12441         $LFS mv -v -M $MDTIDX $migrate_dir ||
12442                 error "migrate remote dir error"
12443
12444         echo "migratate to MDT1, then checking.."
12445         for ((i = 0; i < 10; i++)); do
12446                 for file in $(find $migrate_dir/dir_${i}); do
12447                         mdt_index=$($LFS getstripe -M $file)
12448                         [ $mdt_index == $MDTIDX ] ||
12449                                 error "$file is not on MDT${MDTIDX}"
12450                 done
12451         done
12452
12453         # the multiple link file should still in MDT0
12454         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
12455         [ $mdt_index == 0 ] ||
12456                 error "$file is not on MDT${MDTIDX}"
12457
12458         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12459         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12460                 error " expect $old_dir_flag get $new_dir_flag"
12461
12462         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12463         [ "$old_file_flag" = "$new_file_flag" ] ||
12464                 error " expect $old_file_flag get $new_file_flag"
12465
12466         local new_dir_mode=$(stat -c%f $migrate_dir)
12467         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12468                 error "expect mode $old_dir_mode get $new_dir_mode"
12469
12470         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12471         [ "$old_file_mode" = "$new_file_mode" ] ||
12472                 error "expect mode $old_file_mode get $new_file_mode"
12473
12474         diff /etc/passwd $migrate_dir/$tfile ||
12475                 error "$tfile different after migration"
12476
12477         diff /etc/passwd $other_dir/luna ||
12478                 error "luna different after migration"
12479
12480         diff /etc/passwd $migrate_dir/sofia ||
12481                 error "sofia different after migration"
12482
12483         diff /etc/passwd $migrate_dir/david ||
12484                 error "david different after migration"
12485
12486         diff /etc/passwd $other_dir/zachary ||
12487                 error "zachary different after migration"
12488
12489         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12490                 error "${tfile}_ln different after migration"
12491
12492         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12493                 error "${tfile}_ln_other different after migration"
12494
12495         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
12496         [ $stripe_count = 2 ] ||
12497                 error "dir strpe_count $d != 2 after migration."
12498
12499         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
12500         [ $stripe_count = 2 ] ||
12501                 error "file strpe_count $d != 2 after migration."
12502
12503         #migrate back to MDT0
12504         MDTIDX=0
12505         $LFS mv -v -M $MDTIDX $migrate_dir ||
12506                 error "migrate remote dir error"
12507
12508         echo "migrate back to MDT0, checking.."
12509         for file in $(find $migrate_dir); do
12510                 mdt_index=$($LFS getstripe -M $file)
12511                 [ $mdt_index == $MDTIDX ] ||
12512                         error "$file is not on MDT${MDTIDX}"
12513         done
12514
12515         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
12516         [ "$old_dir_flag" = "$new_dir_flag" ] ||
12517                 error " expect $old_dir_flag get $new_dir_flag"
12518
12519         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
12520         [ "$old_file_flag" = "$new_file_flag" ] ||
12521                 error " expect $old_file_flag get $new_file_flag"
12522
12523         local new_dir_mode=$(stat -c%f $migrate_dir)
12524         [ "$old_dir_mode" = "$new_dir_mode" ] ||
12525                 error "expect mode $old_dir_mode get $new_dir_mode"
12526
12527         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
12528         [ "$old_file_mode" = "$new_file_mode" ] ||
12529                 error "expect mode $old_file_mode get $new_file_mode"
12530
12531         diff /etc/passwd ${migrate_dir}/$tfile ||
12532                 error "$tfile different after migration"
12533
12534         diff /etc/passwd ${other_dir}/luna ||
12535                 error "luna different after migration"
12536
12537         diff /etc/passwd ${migrate_dir}/sofia ||
12538                 error "sofia different after migration"
12539
12540         diff /etc/passwd ${other_dir}/zachary ||
12541                 error "zachary different after migration"
12542
12543         diff /etc/passwd $migrate_dir/${tfile}_ln ||
12544                 error "${tfile}_ln different after migration"
12545
12546         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
12547                 error "${tfile}_ln_other different after migration"
12548
12549         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
12550         [ $stripe_count = 2 ] ||
12551                 error "dir strpe_count $d != 2 after migration."
12552
12553         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
12554         [ $stripe_count = 2 ] ||
12555                 error "file strpe_count $d != 2 after migration."
12556
12557         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12558 }
12559 run_test 230b "migrate directory"
12560
12561 test_230c() {
12562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12563         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12564         local MDTIDX=1
12565         local mdt_index
12566         local file
12567
12568         #If migrating directory fails in the middle, all entries of
12569         #the directory is still accessiable.
12570         mkdir -p $DIR/$tdir
12571         stat $DIR/$tdir
12572         createmany -o $DIR/$tdir/f 10 ||
12573                 error "create files under ${tdir} failed"
12574
12575         #failed after migrating 5 entries
12576         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
12577         do_facet mds1 lctl set_param fail_loc=0x20001801
12578         do_facet mds1 lctl  set_param fail_val=5
12579         local t=$(ls $DIR/$tdir | wc -l)
12580         $LFS mv --mdt-index $MDTIDX $DIR/$tdir &&
12581                 error "migrate should fail after 5 entries"
12582         local u=$(ls $DIR/$tdir | wc -l)
12583         [ "$u" == "$t" ] || error "$u != $t during migration"
12584
12585         for file in $(find $DIR/$tdir); do
12586                 stat $file || error "stat $file failed"
12587         done
12588
12589         do_facet mds1 lctl set_param fail_loc=0
12590         do_facet mds1 lctl set_param fail_val=0
12591
12592         $LFS mv -M $MDTIDX $DIR/$tdir ||
12593                 error "migrate open files should failed with open files"
12594
12595         echo "Finish migration, then checking.."
12596         for file in $(find $DIR/$tdir); do
12597                 mdt_index=$($LFS getstripe -M $file)
12598                 [ $mdt_index == $MDTIDX ] ||
12599                         error "$file is not on MDT${MDTIDX}"
12600         done
12601
12602         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12603 }
12604 run_test 230c "check directory accessiblity if migration is failed"
12605
12606 test_230d() {
12607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12608         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12609         local MDTIDX=1
12610         local mdt_index
12611         local i
12612         local j
12613
12614         mkdir -p $DIR/$tdir
12615
12616         for ((i=0; i<100; i++)); do
12617                 mkdir -p $DIR/$tdir/dir_${i}
12618                 createmany -o $DIR/$tdir/dir_${i}/f 100 ||
12619                         error "create files under remote dir failed $i"
12620         done
12621
12622         $LFS mv -M $MDTIDX -v $DIR/$tdir || error "migrate remote dir error"
12623
12624         echo "Finish migration, then checking.."
12625         for file in $(find $DIR/$tdir); do
12626                 mdt_index=$($LFS getstripe -M $file)
12627                 [ $mdt_index == $MDTIDX ] ||
12628                         error "$file is not on MDT${MDTIDX}"
12629         done
12630
12631         rm -rf $DIR/$tdir || error "rm dir failed after migration"
12632 }
12633 run_test 230d "check migrate big directory"
12634
12635 test_231a()
12636 {
12637         # For simplicity this test assumes that max_pages_per_rpc
12638         # is the same across all OSCs
12639         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
12640         local bulk_size=$((max_pages * 4096))
12641
12642         mkdir -p $DIR/$tdir
12643
12644         # clear the OSC stats
12645         $LCTL set_param osc.*.stats=0 &>/dev/null
12646
12647         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
12648         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
12649                 oflag=direct &>/dev/null || error "dd failed"
12650
12651         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
12652         if [ x$nrpcs != "x1" ]; then
12653                 error "found $nrpc ost_write RPCs, not 1 as expected"
12654         fi
12655
12656         # Drop the OSC cache, otherwise we will read from it
12657         cancel_lru_locks osc
12658
12659         # clear the OSC stats
12660         $LCTL set_param osc.*.stats=0 &>/dev/null
12661
12662         # Client reads $bulk_size.
12663         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
12664                 iflag=direct &>/dev/null || error "dd failed"
12665
12666         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
12667         if [ x$nrpcs != "x1" ]; then
12668                 error "found $nrpc ost_read RPCs, not 1 as expected"
12669         fi
12670 }
12671 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
12672
12673 test_231b() {
12674         mkdir -p $DIR/$tdir
12675         local i
12676         for i in {0..1023}; do
12677                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
12678                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
12679                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
12680         done
12681         sync
12682 }
12683 run_test 231b "must not assert on fully utilized OST request buffer"
12684
12685 test_232() {
12686         mkdir -p $DIR/$tdir
12687         #define OBD_FAIL_LDLM_OST_LVB            0x31c
12688         $LCTL set_param fail_loc=0x31c
12689
12690         # ignore dd failure
12691         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
12692
12693         $LCTL set_param fail_loc=0
12694         umount_client $MOUNT || error "umount failed"
12695         mount_client $MOUNT || error "mount failed"
12696 }
12697 run_test 232 "failed lock should not block umount"
12698
12699 test_233a() {
12700         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
12701         { skip "Need MDS version at least 2.3.64"; return; }
12702
12703         local fid=$($LFS path2fid $MOUNT)
12704         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12705                 error "cannot access $MOUNT using its FID '$fid'"
12706 }
12707 run_test 233a "checking that OBF of the FS root succeeds"
12708
12709 test_233b() {
12710         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
12711         { skip "Need MDS version at least 2.5.90"; return; }
12712
12713         local fid=$($LFS path2fid $MOUNT/.lustre)
12714         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12715                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
12716
12717         fid=$($LFS path2fid $MOUNT/.lustre/fid)
12718         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
12719                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
12720 }
12721 run_test 233b "checking that OBF of the FS .lustre succeeds"
12722
12723 test_234() {
12724         local p="$TMP/sanityN-$TESTNAME.parameters"
12725         save_lustre_params client "llite.*.xattr_cache" > $p
12726         lctl set_param llite.*.xattr_cache 1 ||
12727                 { skip "xattr cache is not supported"; return 0; }
12728
12729         mkdir -p $DIR/$tdir || error "mkdir failed"
12730         touch $DIR/$tdir/$tfile || error "touch failed"
12731         # OBD_FAIL_LLITE_XATTR_ENOMEM
12732         $LCTL set_param fail_loc=0x1405
12733         # output of the form: attr 2 4 44 3 fc13 x86_64
12734         V=($(IFS=".-" rpm -q attr))
12735         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
12736               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
12737                 # attr pre-2.4.44-7 had a bug with rc
12738                 # LU-3703 - SLES 11 and FC13 clients have older attr
12739                 getfattr -n user.attr $DIR/$tdir/$tfile &&
12740                         error "getfattr should have failed with ENOMEM"
12741         else
12742                 skip "LU-3703: attr version $(getfattr --version) too old"
12743         fi
12744         $LCTL set_param fail_loc=0x0
12745         rm -rf $DIR/$tdir
12746
12747         restore_lustre_params < $p
12748         rm -f $p
12749 }
12750 run_test 234 "xattr cache should not crash on ENOMEM"
12751
12752 test_235() {
12753         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
12754                 skip "Need MDS version at least 2.4.52" && return
12755         flock_deadlock $DIR/$tfile
12756         local RC=$?
12757         case $RC in
12758                 0)
12759                 ;;
12760                 124) error "process hangs on a deadlock"
12761                 ;;
12762                 *) error "error executing flock_deadlock $DIR/$tfile"
12763                 ;;
12764         esac
12765 }
12766 run_test 235 "LU-1715: flock deadlock detection does not work properly"
12767
12768 #LU-2935
12769 test_236() {
12770         check_swap_layouts_support && return 0
12771         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
12772
12773         local ref1=/etc/passwd
12774         local ref2=/etc/group
12775         local file1=$DIR/$tdir/f1
12776         local file2=$DIR/$tdir/f2
12777
12778         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
12779         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
12780         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
12781         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
12782         local fd=$(free_fd)
12783         local cmd="exec $fd<>$file2"
12784         eval $cmd
12785         rm $file2
12786         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
12787                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
12788         cmd="exec $fd>&-"
12789         eval $cmd
12790         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12791
12792         #cleanup
12793         rm -rf $DIR/$tdir
12794 }
12795 run_test 236 "Layout swap on open unlinked file"
12796
12797 # test to verify file handle related system calls
12798 # (name_to_handle_at/open_by_handle_at)
12799 # The new system calls are supported in glibc >= 2.14.
12800
12801 test_237() {
12802         echo "Test file_handle syscalls" > $DIR/$tfile ||
12803                 error "write failed"
12804         check_fhandle_syscalls $DIR/$tfile ||
12805                 error "check_fhandle_syscalls failed"
12806 }
12807 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
12808
12809 # LU-4659 linkea consistency
12810 test_238() {
12811         local server_version=$(lustre_version_code $SINGLEMDS)
12812
12813         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12814                 [[ $server_version -gt $(version_code 2.5.1) &&
12815                    $server_version -lt $(version_code 2.5.50) ]] ||
12816                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
12817
12818         touch $DIR/$tfile
12819         ln $DIR/$tfile $DIR/$tfile.lnk
12820         touch $DIR/$tfile.new
12821         mv $DIR/$tfile.new $DIR/$tfile
12822         local fid1=$(lfs path2fid $DIR/$tfile)
12823         local fid2=$(lfs path2fid $DIR/$tfile.lnk)
12824         local path1=$(lfs fid2path $FSNAME $fid1)
12825         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
12826         local path2=$(lfs fid2path $FSNAME $fid2)
12827         [ $tfile.lnk == $path2 ] ||
12828                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
12829         rm -f $DIR/$tfile*
12830 }
12831 run_test 238 "Verify linkea consistency"
12832
12833 test_239() {
12834         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
12835                 skip "Need MDS version at least 2.5.60" && return
12836         local list=$(comma_list $(mdts_nodes))
12837
12838         mkdir -p $DIR/$tdir
12839         createmany -o $DIR/$tdir/f- 5000
12840         unlinkmany $DIR/$tdir/f- 5000
12841         do_nodes $list "lctl set_param -n osp*.*.sync_changes 1"
12842         changes=$(do_nodes $list "lctl get_param -n osc.*MDT*.sync_changes \
12843                         osc.*MDT*.sync_in_flight" | calc_sum)
12844         [ "$changes" -eq 0 ] || error "$changes not synced"
12845 }
12846 run_test 239 "osp_sync test"
12847
12848 test_240() {
12849         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12850
12851         mkdir -p $DIR/$tdir
12852
12853         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
12854                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
12855         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
12856                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
12857
12858         umount_client $MOUNT || error "umount failed"
12859         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
12860         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
12861         mount_client $MOUNT || error "failed to mount client"
12862
12863         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
12864         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
12865 }
12866 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
12867
12868 test_241_bio() {
12869         for LOOP in $(seq $1); do
12870                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
12871                 cancel_lru_locks osc
12872         done
12873 }
12874
12875 test_241_dio() {
12876         for LOOP in $(seq $1); do
12877                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
12878                                                 iflag=direct 2>/dev/null
12879         done
12880 }
12881
12882 test_241() {
12883         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
12884         ls -la $DIR/$tfile
12885         cancel_lru_locks osc
12886         test_241_bio 1000 &
12887         PID=$!
12888         test_241_dio 1000
12889         wait $PID
12890 }
12891 run_test 241 "bio vs dio"
12892
12893 test_242() {
12894         mkdir -p $DIR/$tdir
12895         touch $DIR/$tdir/$tfile
12896
12897         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
12898         do_facet mds1 lctl set_param fail_loc=0x105
12899         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
12900
12901         do_facet mds1 lctl set_param fail_loc=0
12902         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
12903 }
12904 run_test 242 "mdt_readpage failure should not cause directory unreadable"
12905
12906 test_243()
12907 {
12908         test_mkdir -p $DIR/$tdir
12909         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
12910 }
12911 run_test 243 "various group lock tests"
12912
12913 cleanup_test_300() {
12914         trap 0
12915         umask $SAVE_UMASK
12916 }
12917 test_striped_dir() {
12918         local mdt_index=$1
12919         local stripe_count
12920         local stripe_index
12921
12922         mkdir -p $DIR/$tdir
12923
12924         SAVE_UMASK=$(umask)
12925         trap cleanup_test_300 RETURN EXIT
12926
12927         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
12928                                                 $DIR/$tdir/striped_dir ||
12929                 error "set striped dir error"
12930
12931         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
12932         [ "$mode" = "755" ] || error "expect 755 got $mode"
12933
12934         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
12935         if [ "$stripe_count" != "2" ]; then
12936                 error "stripe_count is $stripe_count, expect 2"
12937         fi
12938
12939         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
12940         if [ "$stripe_index" != "$mdt_index" ]; then
12941                 error "stripe_index is $stripe_index, expect $mdt_index"
12942         fi
12943
12944         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12945                 error "nlink error after create striped dir"
12946
12947         mkdir $DIR/$tdir/striped_dir/a
12948         mkdir $DIR/$tdir/striped_dir/b
12949
12950         stat $DIR/$tdir/striped_dir/a ||
12951                 error "create dir under striped dir failed"
12952         stat $DIR/$tdir/striped_dir/b ||
12953                 error "create dir under striped dir failed"
12954
12955         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
12956                 error "nlink error after mkdir"
12957
12958         rmdir $DIR/$tdir/striped_dir/a
12959         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
12960                 error "nlink error after rmdir"
12961
12962         rmdir $DIR/$tdir/striped_dir/b
12963         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
12964                 error "nlink error after rmdir"
12965
12966         rmdir $DIR/$tdir/striped_dir ||
12967                 error "rmdir striped dir error"
12968
12969         cleanup_test_300
12970
12971         true
12972 }
12973
12974 test_300a() {
12975         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12976         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12977
12978         test_striped_dir 0 || error "failed on striped dir on MDT0"
12979         test_striped_dir 1 || error "failed on striped dir on MDT0"
12980 }
12981 run_test 300a "basic striped dir sanity test"
12982
12983 test_300b() {
12984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12985         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12986         local i
12987         local mtime1
12988         local mtime2
12989         local mtime3
12990
12991         test_mkdir $DIR/$tdir || error "mkdir fail"
12992         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
12993                 error "set striped dir error"
12994         for ((i=0; i<10; i++)); do
12995                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
12996                 sleep 1
12997                 touch $DIR/$tdir/striped_dir/file_$i ||
12998                                         error "touch error $i"
12999                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
13000                 [ $mtime1 -eq $mtime2 ] &&
13001                         error "mtime not change after create"
13002                 sleep 1
13003                 rm -f $DIR/$tdir/striped_dir/file_$i ||
13004                                         error "unlink error $i"
13005                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
13006                 [ $mtime2 -eq $mtime3 ] &&
13007                         error "mtime did not change after unlink"
13008         done
13009         true
13010 }
13011 run_test 300b "check ctime/mtime for striped dir"
13012
13013 test_300c() {
13014         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13015         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13016         local file_count
13017
13018         mkdir -p $DIR/$tdir
13019         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
13020                 error "set striped dir error"
13021
13022         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
13023                 error "chown striped dir failed"
13024
13025         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
13026                 error "create 5k files failed"
13027
13028         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
13029
13030         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
13031
13032         rm -rf $DIR/$tdir
13033 }
13034 run_test 300c "chown && check ls under striped directory"
13035
13036 test_300d() {
13037         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13038         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13039         local stripe_count
13040         local file
13041
13042         mkdir -p $DIR/$tdir
13043         $SETSTRIPE -c 2 $DIR/$tdir
13044
13045         #local striped directory
13046         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13047                 error "set striped dir error"
13048         createmany -o $DIR/$tdir/striped_dir/f 10 ||
13049                 error "create 10 files failed"
13050
13051         #remote striped directory
13052         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
13053                 error "set striped dir error"
13054         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
13055                 error "create 10 files failed"
13056
13057         for file in $(find $DIR/$tdir); do
13058                 stripe_count=$($GETSTRIPE -c $file)
13059                 [ $stripe_count -eq 2 ] ||
13060                         error "wrong stripe $stripe_count for $file"
13061         done
13062
13063         rm -rf $DIR/$tdir
13064 }
13065 run_test 300d "check default stripe under striped directory"
13066
13067 test_300e() {
13068         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13069         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13070         local stripe_count
13071         local file
13072
13073         mkdir -p $DIR/$tdir
13074
13075         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13076                 error "set striped dir error"
13077
13078         touch $DIR/$tdir/striped_dir/a
13079         touch $DIR/$tdir/striped_dir/b
13080         touch $DIR/$tdir/striped_dir/c
13081
13082         mkdir $DIR/$tdir/striped_dir/dir_a
13083         mkdir $DIR/$tdir/striped_dir/dir_b
13084         mkdir $DIR/$tdir/striped_dir/dir_c
13085
13086         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
13087                 error "set striped dir under striped dir error"
13088
13089         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_b ||
13090                 error "set striped dir under striped dir error"
13091
13092         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
13093                 error "set striped dir under striped dir error"
13094
13095         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/b &&
13096                 error "rename file under striped dir should fail"
13097
13098         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b &&
13099                 error "rename dir under striped dir should fail"
13100
13101         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b &&
13102                 error "rename dir under different stripes should fail"
13103
13104         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
13105                 error "rename file under striped dir should succeed"
13106
13107         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_c ||
13108                 error "rename dir under striped dir should succeed"
13109
13110         rm -rf $DIR/$tdir
13111 }
13112 run_test 300e "check rename under striped directory"
13113
13114 test_300f() {
13115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13116         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13117         local stripe_count
13118         local file
13119
13120         rm -rf $DIR/$tdir
13121         mkdir -p $DIR/$tdir
13122
13123         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
13124                 error "set striped dir error"
13125
13126         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
13127                 error "set striped dir error"
13128
13129         touch $DIR/$tdir/striped_dir/a
13130         mkdir $DIR/$tdir/striped_dir/dir_a
13131         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
13132                 error "create striped dir under striped dir fails"
13133
13134         touch $DIR/$tdir/striped_dir1/b
13135         mkdir $DIR/$tdir/striped_dir1/dir_b
13136         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
13137                 error "create striped dir under striped dir fails"
13138
13139         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/b &&
13140                 error "rename file under different striped dir should fail"
13141
13142         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b &&
13143                 error "rename dir under different striped dir should fail"
13144
13145         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b &&
13146                 error "rename striped dir under diff striped dir should fail"
13147
13148         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
13149                 error "rename file under diff striped dirs fails"
13150
13151         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_a ||
13152                 error "rename dir under diff striped dirs fails"
13153
13154         rm -rf $DIR/$tdir
13155 }
13156 run_test 300f "check rename cross striped directory"
13157
13158 test_300g() {
13159         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13160         local stripe_count
13161         local dir
13162
13163         mkdir $DIR/$tdir
13164         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
13165                                         $DIR/$tdir/striped_dir ||
13166                 error "set striped dir error"
13167
13168         $LFS setdirstripe -D -c $MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13169                 error "set default stripe on striped dir error"
13170
13171         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/striped_dir)
13172         [ $stripe_count -eq $MDSCOUNT ] ||
13173                 error "default stripe wrong expect $MDSCOUNT get $stripe_count"
13174
13175         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13176
13177         for dir in $(find $DIR/$tdir/striped_dir/*); do
13178                 stripe_count=$($LFS getdirstripe -c $dir)
13179                 [ $stripe_count -eq $MDSCOUNT ] ||
13180                         error "expect $MDSCOUNT get $stripe_count for $dir"
13181         done
13182
13183         rmdir $DIR/$tdir/striped_dir/* || error "rmdir1 failed"
13184         #change default stripe count to 2
13185         $LFS setdirstripe -D -c 2 -t all_char $DIR/$tdir/striped_dir ||
13186                 error "set default stripe on striped dir error"
13187
13188         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13189
13190         rmdir $DIR/$tdir/striped_dir/* || error "rmdir2 failed"
13191
13192         #change default stripe count to 1
13193         $LFS setdirstripe -D -c 1 -t all_char $DIR/$tdir/striped_dir ||
13194                 error "set default stripe on striped dir error"
13195
13196         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
13197         for dir in $(find $DIR/$tdir/striped_dir/*); do
13198                 stripe_count=$($LFS getdirstripe -c $dir)
13199                 [ $stripe_count -eq 1 ] ||
13200                         error "expect 1 get $stripe_count for $dir"
13201         done
13202         rmdir $DIR/$tdir/striped_dir/* || error "rmdir3 failed"
13203 }
13204 run_test 300g "check default striped directory for striped directory"
13205
13206 test_300h() {
13207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13208         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13209         local stripe_count
13210         local file
13211
13212         mkdir $DIR/$tdir
13213
13214         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
13215                 error "set striped dir error"
13216
13217         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
13218                 error "create files under striped dir failed"
13219
13220         # unfortunately, we need to umount to clear dir layout cache for now
13221         # once we fully implement dir layout, we can drop this
13222         umount_client $MOUNT || error "umount failed"
13223         mount_client $MOUNT || error "mount failed"
13224
13225         #set the stripe to be unknown hash type
13226         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
13227         $LCTL set_param fail_loc=0x1901
13228         for ((i = 0; i < 10; i++)); do
13229                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
13230                         error "stat f-$i failed"
13231                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
13232         done
13233
13234         touch $DIR/$tdir/striped_dir/f0 &&
13235                 error "create under striped dir with unknown hash should fail"
13236
13237         $LCTL set_param fail_loc=0
13238
13239         umount_client $MOUNT || error "umount failed"
13240         mount_client $MOUNT || error "mount failed"
13241
13242         return 0
13243 }
13244 run_test 300h "client handle unknown hash type striped directory"
13245
13246 test_400a() { # LU-1606, was conf-sanity test_74
13247         local extra_flags=''
13248         local out=$TMP/$tfile
13249         local prefix=/usr/include/lustre
13250         local prog
13251
13252         if ! which $CC > /dev/null 2>&1; then
13253                 skip_env "$CC is not installed"
13254                 return 0
13255         fi
13256
13257         if ! [[ -d $prefix ]]; then
13258                 # Assume we're running in tree and fixup the include path.
13259                 extra_flags+=" -I$LUSTRE/../libcfs/include"
13260                 extra_flags+=" -I$LUSTRE/include"
13261                 extra_flags+=" -L$LUSTRE/utils"
13262         fi
13263
13264         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
13265                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
13266                         error "client api broken"
13267         done
13268 }
13269 run_test 400a "Lustre client api program can compile and link"
13270
13271 test_400b() { # LU-1606, LU-5011
13272         local header
13273         local out=$TMP/$tfile
13274         local prefix=/usr/include/lustre
13275
13276         # We use a hard coded prefix so that this test will not fail
13277         # when run in tree. There are headers in lustre/include/lustre/
13278         # that are not packaged (like lustre_idl.h) and have more
13279         # complicated include dependencies (like config.h and lnet/types.h).
13280         # Since this test about correct packaging we just skip them when
13281         # they don't exist (see below) rather than try to fixup cppflags.
13282
13283         if ! which $CC > /dev/null 2>&1; then
13284                 skip_env "$CC is not installed"
13285                 return 0
13286         fi
13287
13288         for header in $prefix/*.h; do
13289                 if ! [[ -f "$header" ]]; then
13290                         continue
13291                 fi
13292
13293                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
13294                         continue # liblustreapi.h is deprecated.
13295                 fi
13296
13297                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
13298                         error "cannot compile '$header'"
13299         done
13300 }
13301 run_test 400b "packaged headers can be compiled"
13302
13303 #
13304 # tests that do cleanup/setup should be run at the end
13305 #
13306
13307 test_900() {
13308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13309         local ls
13310         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
13311         $LCTL set_param fail_loc=0x903
13312
13313         cancel_lru_locks MGC
13314
13315         FAIL_ON_ERROR=true cleanup
13316         FAIL_ON_ERROR=true setup
13317 }
13318 run_test 900 "umount should not race with any mgc requeue thread"
13319
13320 complete $SECONDS
13321 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
13322 check_and_cleanup_lustre
13323 if [ "$I_MOUNTED" != "yes" ]; then
13324         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
13325 fi
13326 exit_status